MySQL 8.4.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, 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/gis/wkb.h"
31#include "sql/sql_class.h" // THD
32
33#include <algorithm>
34#include <memory>
35/// @file
36///
37/// This file contains a few convenience functions for working with Geometries,
38/// to avoid boilerplate and mishandling of Geometries.
39
40/// Type used to differentiate the three cases that can happen when parsing a
41/// geometry.
43
44/// Type used to handle both the result of the decoding of a geometry and the
45/// geometry in the case of success.
47 private:
49 std::unique_ptr<gis::Geometry> m_value;
51
52 public:
56 return m_srs;
57 }
58 std::unique_ptr<gis::Geometry> GetValue() {
60 return std::move(m_value);
61 }
63 : m_resultType(resultType) {
64 if (resultType == ResultType::Value) {
66 }
67 }
68 explicit GeometryExtractionResult(std::unique_ptr<gis::Geometry> geometry,
71 m_value(std::move(geometry)),
72 m_srs(srs) {}
73};
74
75/// ExtractGeometry takes an Item or a Field, attempts to parse a geometry out
76/// of it and returns a value combining the result of the parsing process with
77/// the geometry in case it is a success.
78///
79/// @param[in] fieldOrItem The Field or Item we want a geometry from.
80/// @param[in] thd THD* to report errors on
81/// @param[in] func_name C-string to report errors as.
82/// @returns GeometryExtractionResult which holds a result and an optional
83/// Geometry
84
85template <typename FieldOrItem>
86GeometryExtractionResult ExtractGeometry(FieldOrItem *fieldOrItem, THD *thd,
87 const char *func_name) {
88 String backing_arg_wkb;
89 if (fieldOrItem->result_type() != STRING_RESULT) {
90 my_error(ER_GIS_INVALID_DATA, MYF(0), func_name);
92 }
93 if (fieldOrItem->is_null()) {
95 }
96 String *arg_wkb = fieldOrItem->val_str(&backing_arg_wkb);
97 if (thd->is_error()) {
99 }
100 if (nullptr == arg_wkb) {
101 my_error(ER_GIS_INVALID_DATA, MYF(0), func_name);
103 }
104
105 std::unique_ptr<dd::cache::Dictionary_client::Auto_releaser> releaser(
108 const dd::Spatial_reference_system *srs = nullptr;
109 std::unique_ptr<gis::Geometry> geo;
110 bool result = gis::parse_geometry(thd, func_name, arg_wkb, &srs, &geo);
111
112 if (result == true) {
114 } else {
115 return GeometryExtractionResult(std::move(geo), srs);
116 }
117}
118
119#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:46
std::unique_ptr< gis::Geometry > m_value
Definition: geometry_extraction.h:49
const ResultType m_resultType
Definition: geometry_extraction.h:48
std::unique_ptr< gis::Geometry > GetValue()
Definition: geometry_extraction.h:58
const dd::Spatial_reference_system * GetSrs() const
Definition: geometry_extraction.h:54
const dd::Spatial_reference_system * m_srs
Definition: geometry_extraction.h:50
GeometryExtractionResult(ResultType resultType)
Definition: geometry_extraction.h:62
GeometryExtractionResult(std::unique_ptr< gis::Geometry > geometry, const dd::Spatial_reference_system *srs)
Definition: geometry_extraction.h:68
ResultType GetResultType() 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:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
dd::cache::Dictionary_client * dd_client() const
Definition: sql_class.h:1002
bool is_error() const
true if there is an error in the error stack.
Definition: sql_class.h:3281
a nullable SQL value.
Definition: sql_value.h:40
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:42
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:86
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
Definition: gcs_xcom_synode.h:64
Definition: result.h:30
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:41
This file declares the interface of the WKB parser for geometries and the parser for the internal geo...