MySQL 8.3.0
Source Code Documentation
geometry_extraction.h
Go to the documentation of this file.
1#ifndef SQL_GIS_GEOMETRY_EXTRACTION_H_INCLUDED
2#define SQL_GIS_GEOMETRY_EXTRACTION_H_INCLUDED
3
4// Copyright (c) 2021, 2023, Oracle and/or its affiliates.
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License, version 2.0,
8// as published by the Free Software Foundation.
9//
10// This program is also distributed with certain software (including
11// but not limited to OpenSSL) that is licensed under separate terms,
12// as designated in a particular file or component or in included license
13// documentation. The authors of MySQL hereby grant you an additional
14// permission to link the program and your derivative works with the
15// separately licensed software that they have included with MySQL.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License, version 2.0, for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25
26#include "my_sys.h" // my_error
28#include "sql/gis/geometries.h"
29#include "sql/gis/wkb.h"
30#include "sql/sql_class.h" // THD
31
32#include <algorithm>
33#include <memory>
34/// @file
35///
36/// This file contains a few convenience functions for working with Geometries,
37/// to avoid boilerplate and mishandling of Geometries.
38
39/// Type used to differentiate the three cases that can happen when parsing a
40/// geometry.
42
43/// Type used to handle both the result of the decoding of a geometry and the
44/// geometry in the case of success.
46 private:
48 std::unique_ptr<gis::Geometry> m_value;
50
51 public:
55 return m_srs;
56 }
57 std::unique_ptr<gis::Geometry> GetValue() {
59 return std::move(m_value);
60 }
62 : m_resultType(resultType) {
63 if (resultType == ResultType::Value) {
65 }
66 }
67 explicit GeometryExtractionResult(std::unique_ptr<gis::Geometry> geometry,
70 m_value(std::move(geometry)),
71 m_srs(srs) {}
72};
73
74/// ExtractGeometry takes an Item or a Field, attempts to parse a geometry out
75/// of it and returns a value combining the result of the parsing process with
76/// the geometry in case it is a success.
77///
78/// @param[in] fieldOrItem The Field or Item we want a geometry from.
79/// @param[in] thd THD* to report errors on
80/// @param[in] func_name C-string to report errors as.
81/// @returns GeometryExtractionResult which holds a result and an optional
82/// Geometry
83
84template <typename FieldOrItem>
85GeometryExtractionResult ExtractGeometry(FieldOrItem *fieldOrItem, THD *thd,
86 const char *func_name) {
87 String backing_arg_wkb;
88 if (fieldOrItem->result_type() != STRING_RESULT) {
89 my_error(ER_GIS_INVALID_DATA, MYF(0), func_name);
91 }
92 if (fieldOrItem->is_null()) {
94 }
95 String *arg_wkb = fieldOrItem->val_str(&backing_arg_wkb);
96 if (thd->is_error()) {
98 }
99 if (nullptr == arg_wkb) {
100 my_error(ER_GIS_INVALID_DATA, MYF(0), func_name);
102 }
103
104 std::unique_ptr<dd::cache::Dictionary_client::Auto_releaser> releaser(
107 const dd::Spatial_reference_system *srs = nullptr;
108 std::unique_ptr<gis::Geometry> geo;
109 bool result = gis::parse_geometry(thd, func_name, arg_wkb, &srs, &geo);
110
111 if (result == true) {
113 } else {
114 return GeometryExtractionResult(std::move(geo), srs);
115 }
116}
117
118#endif // SQL_GIS_GEOMETRY_EXTRACTION_H_INCLUDED
Type used to handle both the result of the decoding of a geometry and the geometry in the case of suc...
Definition: geometry_extraction.h:45
std::unique_ptr< gis::Geometry > m_value
Definition: geometry_extraction.h:48
const ResultType m_resultType
Definition: geometry_extraction.h:47
std::unique_ptr< gis::Geometry > GetValue()
Definition: geometry_extraction.h:57
const dd::Spatial_reference_system * GetSrs() const
Definition: geometry_extraction.h:53
const dd::Spatial_reference_system * m_srs
Definition: geometry_extraction.h:49
GeometryExtractionResult(ResultType resultType)
Definition: geometry_extraction.h:61
GeometryExtractionResult(std::unique_ptr< gis::Geometry > geometry, const dd::Spatial_reference_system *srs)
Definition: geometry_extraction.h:67
ResultType GetResultType() const
Definition: geometry_extraction.h:52
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
dd::cache::Dictionary_client * dd_client() const
Definition: sql_class.h:983
bool is_error() const
true if there is an error in the error stack.
Definition: sql_class.h:3227
a nullable SQL value.
Definition: sql_value.h:39
Definition: spatial_reference_system.h:52
Class to help releasing and deleting objects.
Definition: dictionary_client.h:176
thread_local THD * current_thd
Definition: current_thd.cc:25
This file declares the geometry class hierarchy used by the server as the internal representation of ...
ResultType
Type used to differentiate the three cases that can happen when parsing a geometry.
Definition: geometry_extraction.h:41
GeometryExtractionResult ExtractGeometry(FieldOrItem *fieldOrItem, THD *thd, const char *func_name)
ExtractGeometry takes an Item or a Field, attempts to parse a geometry out of it and returns a value ...
Definition: geometry_extraction.h:85
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:215
#define MYF(v)
Definition: my_inttypes.h:96
Common header for many mysys elements.
bool parse_geometry(THD *thd, const char *func_name, const String *str, const dd::Spatial_reference_system **srs, std::unique_ptr< Geometry > *geometry, bool treat_unknown_srid_as_cartesian)
Parses a little-endian geometry string (SRID + WKB).
Definition: wkb.cc:436
Definition: varlen_sort.h:174
Definition: result.h:29
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:40
This file declares the interface of the WKB parser for geometries and the parser for the internal geo...