MySQL 8.0.39
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, 2024, 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 designed to work 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 either included with
16// the program or referenced in the documentation.
17//
18// This program is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License, version 2.0, for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with this program; if not, write to the Free Software
25// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26
27#include "my_sys.h" // my_error
29#include "sql/gis/geometries.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 srid;
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 srid(srid) {}
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 ? srs->id() : 0);
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
gis::srid_t srid
Definition: geometry_extraction.h:49
const ResultType m_resultType
Definition: geometry_extraction.h:47
std::unique_ptr< gis::Geometry > GetValue()
Definition: geometry_extraction.h:57
GeometryExtractionResult(ResultType resultType)
Definition: geometry_extraction.h:61
GeometryExtractionResult(std::unique_ptr< gis::Geometry > geometry, gis::srid_t srid)
Definition: geometry_extraction.h:67
ResultType GetResultType() const
Definition: geometry_extraction.h:52
gis::srid_t GetSrid() const
Definition: geometry_extraction.h:53
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
dd::cache::Dictionary_client * dd_client() const
Definition: sql_class.h:982
bool is_error() const
true if there is an error in the error stack.
Definition: sql_class.h:3209
a nullable SQL value.
Definition: sql_value.h:40
virtual Object_id id() const =0
The unique dictionary object id.
Definition: spatial_reference_system.h:53
Class to help releasing and deleting objects.
Definition: dictionary_client.h:177
thread_local THD * current_thd
Definition: current_thd.cc:26
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:216
#define MYF(v)
Definition: my_inttypes.h:97
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:437
std::uint32_t srid_t
A spatial reference system ID (SRID).
Definition: srid.h:33
Definition: gcs_xcom_synode.h:64
Definition: result.h:30
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:41