MySQL  8.0.28
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, 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/sql_class.h" // THD
30 
31 #include <algorithm>
32 #include <memory>
33 /// @file
34 ///
35 /// This file contains a few convenience functions for working with Geometries,
36 /// to avoid boilerplate and mishandling of Geometries.
37 
38 /// Type used to differentiate the three cases that can happen when parsing a
39 /// geometry.
40 enum class ResultType { Error, NullValue, Value };
41 
42 /// Type used to handle both the result of the decoding of a geometry and the
43 /// geometry in the case of success.
45  private:
47  std::unique_ptr<gis::Geometry> m_value;
49 
50  public:
52  gis::srid_t GetSrid() const {
54  return srid;
55  }
56  std::unique_ptr<gis::Geometry> GetValue() {
58  return std::move(m_value);
59  }
60  explicit GeometryExtractionResult(ResultType resultType)
61  : m_resultType(resultType) {
62  if (resultType == ResultType::Value) {
63  throw ResultType::Error;
64  }
65  }
66  explicit GeometryExtractionResult(std::unique_ptr<gis::Geometry> geometry,
69  m_value(std::move(geometry)),
70  srid(srid) {}
71 };
72 
73 /// ExtractGeometry takes an Item or a Field, attempts to parse a geometry out
74 /// of it and returns a value combining the result of the parsing process with
75 /// the geometry in case it is a success.
76 ///
77 /// @param[in] fieldOrItem The Field or Item we want a geometry from.
78 /// @param[in] thd THD* to report errors on
79 /// @param[in] func_name C-string to report errors as.
80 /// @returns GeometryExtractionResult which holds a result and an optional
81 /// Geometry
82 
83 template <typename FieldOrItem>
84 GeometryExtractionResult ExtractGeometry(FieldOrItem *fieldOrItem, THD *thd,
85  const char *func_name) {
86  String backing_arg_wkb;
87  if (fieldOrItem->result_type() != STRING_RESULT) {
88  my_error(ER_GIS_INVALID_DATA, MYF(0), func_name);
90  }
91  if (fieldOrItem->is_null()) {
93  }
94  String *arg_wkb = fieldOrItem->val_str(&backing_arg_wkb);
95  if (thd->is_error()) {
97  }
98  if (nullptr == arg_wkb) {
99  my_error(ER_GIS_INVALID_DATA, MYF(0), func_name);
101  }
102 
103  std::unique_ptr<dd::cache::Dictionary_client::Auto_releaser> releaser(
105  current_thd->dd_client()));
106  const dd::Spatial_reference_system *srs = nullptr;
107  std::unique_ptr<gis::Geometry> geo;
108  bool result = gis::parse_geometry(thd, func_name, arg_wkb, &srs, &geo);
109 
110  if (result == true) {
112  } else {
113  return GeometryExtractionResult(std::move(geo), srs ? srs->id() : 0);
114  }
115 }
116 
117 #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:44
std::unique_ptr< gis::Geometry > m_value
Definition: geometry_extraction.h:47
gis::srid_t srid
Definition: geometry_extraction.h:48
const ResultType m_resultType
Definition: geometry_extraction.h:46
std::unique_ptr< gis::Geometry > GetValue()
Definition: geometry_extraction.h:56
GeometryExtractionResult(ResultType resultType)
Definition: geometry_extraction.h:60
GeometryExtractionResult(std::unique_ptr< gis::Geometry > geometry, gis::srid_t srid)
Definition: geometry_extraction.h:66
ResultType GetResultType() const
Definition: geometry_extraction.h:51
gis::srid_t GetSrid() 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:165
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:945
bool is_error() const
true if there is an error in the error stack.
Definition: sql_class.h:3155
dd::cache::Dictionary_client * dd_client() const
Definition: sql_class.h:1004
virtual Object_id id() const =0
The unique dictionary object id.
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:40
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:84
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
std::uint32_t srid_t
A spatial reference system ID (SRID).
Definition: srid.h:32
Definition: varlen_sort.h:183
Definition: result.h:29
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:40