1 /* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "sql/dd/impl/system_views/st_units_of_measure.h"
24 
25 #include <m_string.h>
26 #include <algorithm>
27 #include <iomanip>
28 #include <string>
29 
30 #include "sql/gis/st_units_of_measure.h"
31 namespace dd {
32 namespace system_views {
33 
34 /// Escapes (only) apostrophes
35 /// @return string with apostrophes escaped with backslash
escape(const std::string & str)36 static std::string escape(const std::string &str) {
37   std::string res = "";
38   for (auto letter : str) {
39     if (letter == '\'') {
40       res += "\\'";
41     } else {
42       res += letter;
43     }
44   }
45   return res;
46 }
47 
48 /// Returns a string represention of the Unit_type
49 /// As there is currently only one Unit_Type it will return "Linear".
50 /// @return "Linear" if unit type is linear
to_string(const gis::Unit_Type unit_type)51 static std::string to_string(const gis::Unit_Type unit_type) {
52   if (unit_type == gis::Unit_Type::kLinear) {
53     return "LINEAR";
54   } else {
55     DBUG_ASSERT(false);
56     return "";
57   }
58 }
59 
instance()60 const St_units_of_measure &St_units_of_measure::instance() {
61   static St_units_of_measure *m_instance = new St_units_of_measure();
62   return *m_instance;
63 }
64 
St_units_of_measure()65 St_units_of_measure::St_units_of_measure() {
66   Stringstream_type ss;
67   ss << "JSON_TABLE('[";
68   collation_unordered_map<std::string, gis::Unit> units = gis::units();
69   char buffer[FLOATING_POINT_BUFFER];
70   for (std::pair<const std::string, gis::Unit> &unit_conversion : units) {
71     if (unit_conversion != *units.begin()) {
72       ss << ",";
73     }
74     my_fcvt_compact(unit_conversion.second.conversion_factor, buffer, nullptr);
75     ss << "[\"" << dd::system_views::escape(unit_conversion.first) << "\","
76        << '\"' << dd::system_views::to_string(unit_conversion.second.unit_type)
77        << "\"," << '\"' << unit_conversion.second.description << "\"," << buffer
78        << "]";
79   }
80 
81   ss << "]', '$[*]' COLUMNS(UNIT_NAME VARCHAR(255) CHARSET utf8mb4 PATH '$[0]'"
82      << ", "
83      << "UNIT_TYPE VARCHAR(7) CHARSET utf8mb4 PATH '$[1]'"
84      << ", "
85      << "DESCRIPTION VARCHAR(255) CHARSET utf8mb4 PATH '$[2]'"
86      << ", "
87      << "CONVERSION_FACTOR DOUBLE PRECISION PATH '$[3]'"
88      << ")) AS ST_UNITS_OF_MEASURE";
89   m_target_def.set_view_name(view_name());
90   m_target_def.add_field(FIELD_UNIT_NAME, "UNIT_NAME", "UNIT_NAME");
91   m_target_def.add_field(FIELD_UNIT_TYPE, "UNIT_TYPE", "UNIT_TYPE");
92   m_target_def.add_field(FIELD_CONVERSION_FACTOR, "CONVERSION_FACTOR",
93                          "CONVERSION_FACTOR");
94   m_target_def.add_field(FIELD_DESCRIPTION, "DESCRIPTION", "DESCRIPTION");
95   m_target_def.add_from(ss.str());
96 }
97 }  // namespace system_views
98 }  // namespace dd
99