Debian Codegraph @

ogr/ogrsf_frmts/mysql/ogrmysqldatasource.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Project: OpenGIS Simple Features Reference Implementation
4 * Purpose: Implements OGRMySQLDataSource class.
5 * Author: Frank Warmerdam, [email protected]
6 * Author: Howard Butler, [email protected]
7 *
8 ******************************************************************************
9 * Copyright (c) 2004, Frank Warmerdam <[email protected]>
10 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
11 *
12 * SPDX-License-Identifier: MIT
13 ****************************************************************************/
14
15 #include <string>
16 #include "ogr_mysql.h"cxx . . $ `<file>/ogr/ogrsf_frmts/mysql/ogr_mysql.h`/
17
18 #include "cpl_conv.h"cxx . . $ `<file>/port/cpl_conv.h`/
19 #include "cpl_string.h"cxx . . $ `<file>/port/cpl_string.h`/
20
21 /************************************************************************/
22 /* FreeResultAndNullify() */
23 /************************************************************************/
24
25 inline void FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(MYSQL_RES *&hResult)
26 {
27 if (hResult)
28 {
29 mysql_free_result(hResult);
30 hResult = nullptr;
31 }
32 }
33
34 /************************************************************************/
35 /* OGRMySQLDataSource() */
36 /************************************************************************/
37
38 OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#OGRMySQLDataSource(49f6e7a06ebc5aa8).()
39 : papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.(nullptr), nLayerscxx . . $ OGRMySQLDataSource#nLayers.(0), bDSUpdatecxx . . $ OGRMySQLDataSource#bDSUpdate.(FALSEcxx . . $ `port/cpl_port.h:1180:9`!), hConncxx . . $ OGRMySQLDataSource#hConn.(nullptr),
40 poLongResultLayercxx . . $ OGRMySQLDataSource#poLongResultLayer.(nullptr)
41 {
42 }
43
44 /************************************************************************/
45 /* ~OGRMySQLDataSource() */
46 /************************************************************************/
47
48 OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::~cxx . . $ OGRMySQLDataSource#`~OGRMySQLDataSource`(ced63f7c635d850d).OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#()
49
50 {
51 InterruptLongResultcxx . . $ OGRMySQLDataSource#InterruptLongResult(49f6e7a06ebc5aa8).();
52
53 for (int i = 0; i < nLayerscxx . . $ OGRMySQLDataSource#nLayers.; i++)
54 delete papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.[i];
55
56 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.);
57
58 if (hConncxx . . $ OGRMySQLDataSource#hConn. != nullptr)
59 mysql_close(hConncxx . . $ OGRMySQLDataSource#hConn.);
60 }
61
62 /************************************************************************/
63 /* GetUnknownSRID() */
64 /************************************************************************/
65
66 int OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).() const
67 {
68 return m_nMajorcxx . . $ OGRMySQLDataSource#m_nMajor. >= 8 && !m_bIsMariaDBcxx . . $ OGRMySQLDataSource#m_bIsMariaDB. ? 0 : -1;
69 }
70
71 /************************************************************************/
72 /* ReportError() */
73 /************************************************************************/
74
75 void OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(const char *pszDescription)
76
77 {
78 if (pszDescription)
79 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
80 "MySQL error message:%s Description: %s", mysql_error(hConncxx . . $ OGRMySQLDataSource#hConn.),
81 pszDescription);
82 else
83 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "%s", mysql_error(hConncxx . . $ OGRMySQLDataSource#hConn.));
84 }
85
86 /************************************************************************/
87 /* Open() */
88 /************************************************************************/
89
90 int OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::Opencxx . . $ OGRMySQLDataSource#Open(db37ecbbd998ad97).(const char *pszNewName, char **papszOpenOptionsIn,
91 int bUpdate)
92
93 {
94 CPLAssertcxx . . $ `port/cpl_error.h:353:9`!(nLayers == 0);
95
96 /* -------------------------------------------------------------------- */
97 /* Use options process to get .my.cnf file contents. */
98 /* -------------------------------------------------------------------- */
99 int nPort = 0;
100 char **papszTableNames = nullptr;
101 stdcxx . . $ std/::stringcxx . . $ std/string# oHostcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391)., oPasswordcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391)., oUsercxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391)., oDBcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
102
103 CPLString osNewName(pszNewName);
104 const char *apszOpenOptions[] = {"dbname", "port", "user",
105 "password", "host", "tables"};
106 for (int i = 0; i < (int)(sizeof(apszOpenOptions) / sizeof(char *)); i++)
107 {
108 const char *pszVal =
109 CSLFetchNameValue(papszOpenOptionsIn, apszOpenOptions[i]);
110 if (pszVal)
111 {
112 if (osNewName.backcxx . . $ std/__cxx11/basic_string#back(da5fe8e926c570ad).() != ':')
113 osNewName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). ",";
114 if (i > 0)
115 {
116 osNewName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). apszOpenOptions[i];
117 osNewName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "=";
118 }
119 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(apszOpenOptions[i], "tables"))
120 {
121 for (; *pszVal; ++pszVal)
122 {
123 if (*pszVal == ',')
124 osNewName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). ";";
125 else
126 osNewName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). *pszVal;
127 }
128 }
129 else
130 osNewName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). pszVal;
131 }
132 }
133
134 /* -------------------------------------------------------------------- */
135 /* Parse out connection information. */
136 /* -------------------------------------------------------------------- */
137 char **papszItems =
138 CSLTokenizeString2(osNewName + 6, ",", CSLT_HONOURSTRINGScxx . . $ `port/cpl_string.h:70:9`!);
139
140 if (CSLCount(papszItems) < 1)
141 {
142 CSLDestroy(papszItems);
143 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
144 "MYSQL: request missing databasename.");
145 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
146 }
147
148 oDB =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). papszItems[0];
149
150 for (int i = 1; papszItems[i] != nullptr; i++)
151 {
152 if (STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(papszItems[i], "user="))
153 oUser =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). papszItems[i] + 5;
154 else if (STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(papszItems[i], "password="))
155 oPassword =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). papszItems[i] + 9;
156 else if (STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(papszItems[i], "host="))
157 oHost =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). papszItems[i] + 5;
158 else if (STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(papszItems[i], "port="))
159 nPort = atoicxx . . $ atoi(f5ecf3191ed0cd93).(papszItems[i] + 5);
160 else if (STARTS_WITH_CIcxx . . $ `port/cpl_port.h:543:9`!(papszItems[i], "tables="))
161 {
162 CSLDestroy(papszTableNames);
163 papszTableNames =
164 CSLTokenizeStringComplex(papszItems[i] + 7, ";", FALSEcxx . . $ `port/cpl_port.h:1180:9`!, FALSEcxx . . $ `port/cpl_port.h:1180:9`!);
165 }
166 else
167 CPLError(CE_Warning, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
168 "'%s' in MYSQL datasource definition not recognised and "
169 "ignored.",
170 papszItems[i]);
171 }
172
173 CSLDestroy(papszItems);
174
175 /* -------------------------------------------------------------------- */
176 /* Try to establish connection. */
177 /* -------------------------------------------------------------------- */
178 hConncxx . . $ OGRMySQLDataSource#hConn. = mysql_init(nullptr);
179
180 if (hConncxx . . $ OGRMySQLDataSource#hConn. == nullptr)
181 {
182 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "mysql_init() failed.");
183 }
184
185 /* -------------------------------------------------------------------- */
186 /* Set desired options on the connection: charset and timeout. */
187 /* -------------------------------------------------------------------- */
188 if (hConncxx . . $ OGRMySQLDataSource#hConn.)
189 {
190 const char *pszTimeoutLength = CPLGetConfigOption("MYSQL_TIMEOUT", "0");
191
192 unsigned int timeout = atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszTimeoutLength);
193 mysql_options(hConncxx . . $ OGRMySQLDataSource#hConn., MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
194
195 mysql_options(hConncxx . . $ OGRMySQLDataSource#hConn., MYSQL_SET_CHARSET_NAME, "utf8");
196 }
197
198 /* -------------------------------------------------------------------- */
199 /* Perform connection. */
200 /* -------------------------------------------------------------------- */
201 if (hConncxx . . $ OGRMySQLDataSource#hConn. &&
202 mysql_real_connect(hConncxx . . $ OGRMySQLDataSource#hConn., oHost.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() ? oHost.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() : nullptr,
203 oUser.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() ? oUser.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() : nullptr,
204 oPassword.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() ? oPassword.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() : nullptr,
205 oDB.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() ? oDB.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() : nullptr, nPort, nullptr,
206 CLIENT_INTERACTIVEcxx . . $ `<external>/6883290445123297894/mariadb_com.h:150:9`!) == nullptr)
207 {
208 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
209 "MySQL connect failed for: %s\n%s", pszNewName + 6,
210 mysql_error(hConncxx . . $ OGRMySQLDataSource#hConn.));
211 mysql_close(hConncxx . . $ OGRMySQLDataSource#hConn.);
212 hConncxx . . $ OGRMySQLDataSource#hConn. = nullptr;
213 }
214
215 if (hConncxx . . $ OGRMySQLDataSource#hConn. == nullptr)
216 {
217 CSLDestroy(papszTableNames);
218 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
219 }
220 #if defined(LIBMYSQL_VERSION_ID) && (LIBMYSQL_VERSION_ID < 80034)
221 else
222 {
223 // Enable automatic reconnection
224 #if defined(LIBMYSQL_VERSION_ID) && (LIBMYSQL_VERSION_ID >= 80000)
225 bool reconnect = 1;
226 #else
227 my_bool reconnect = 1;
228 #endif
229 // Must be called after mysql_real_connect() on MySQL < 5.0.19
230 // and at any point on more recent versions.
231 mysql_options(hConn, MYSQL_OPT_RECONNECT, &reconnect);
232 }
233 #endif
234
235 bDSUpdatecxx . . $ OGRMySQLDataSource#bDSUpdate. = bUpdate;
236
237 /* -------------------------------------------------------------------- */
238 /* Check version. */
239 /* -------------------------------------------------------------------- */
240 auto versionLyr = ExecuteSQLcxx . . $ OGRMySQLDataSource#ExecuteSQL(1e911e025f8b6f61).("SELECT VERSION()", nullptr, nullptr);
241 if (versionLyr)
242 {
243 auto versionFeat = versionLyr->GetNextFeaturecxx . . $ OGRLayer#GetNextFeature(ec9eceddec1d9563).();
244 if (versionFeat)
245 {
246 const char *pszVersion = versionFeat->GetFieldAsStringcxx . . $ OGRFeature#GetFieldAsString(afbf07f2bc1a9437).(0);
247 m_nMajorcxx . . $ OGRMySQLDataSource#m_nMajor. = atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszVersion);
248 const char *pszDot = strchr(pszVersion, '.');
249 if (pszDot)
250 m_nMinorcxx . . $ OGRMySQLDataSource#m_nMinor. = atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszDot + 1);
251 m_bIsMariaDBcxx . . $ OGRMySQLDataSource#m_bIsMariaDB. = strstr(pszVersion, "MariaDB") != nullptr;
252 }
253 delete versionFeat;
254 ReleaseResultSetcxx . . $ OGRMySQLDataSource#ReleaseResultSet(74233f8ea7d835ac).(versionLyr);
255 }
256
257 /* -------------------------------------------------------------------- */
258 /* Get a list of available tables. */
259 /* -------------------------------------------------------------------- */
260 if (papszTableNames == nullptr)
261 {
262 MYSQL_RES *hResultSet;
263 MYSQL_ROW papszRow;
264
265 if (mysql_query(hConncxx . . $ OGRMySQLDataSource#hConn., "SHOW TABLES"))
266 {
267 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).("SHOW TABLES Failed");
268 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
269 }
270
271 hResultSet = mysql_store_result(hConncxx . . $ OGRMySQLDataSource#hConn.);
272 if (hResultSet == nullptr)
273 {
274 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).("mysql_store_result() failed on SHOW TABLES result.");
275 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
276 }
277
278 while ((papszRow = mysql_fetch_row(hResultSet)) != nullptr)
279 {
280 if (papszRow[0] == nullptr)
281 continue;
282
283 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(papszRow[0], "spatial_ref_sys") ||
284 EQUALcxx . . $ `port/cpl_port.h:532:9`!(papszRow[0], "geometry_columns"))
285 continue;
286
287 papszTableNames = CSLAddString(papszTableNames, papszRow[0]);
288 }
289
290 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResultSet);
291 }
292
293 /* -------------------------------------------------------------------- */
294 /* Get the schema of the available tables. */
295 /* -------------------------------------------------------------------- */
296 for (int iRecord = 0;
297 papszTableNames != nullptr && papszTableNames[iRecord] != nullptr;
298 iRecord++)
299 {
300 // FIXME: This should be fixed to deal with tables
301 // for which we can't open because the name is bad/
302 OpenTablecxx . . $ OGRMySQLDataSource#OpenTable(cb7559238bb26814).(papszTableNames[iRecord], bUpdate);
303 }
304
305 CSLDestroy(papszTableNames);
306
307 return nLayerscxx . . $ OGRMySQLDataSource#nLayers. > 0 || bUpdate;
308 }
309
310 /************************************************************************/
311 /* OpenTable() */
312 /************************************************************************/
313
314 int OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::OpenTablecxx . . $ OGRMySQLDataSource#OpenTable(cb7559238bb26814).(const char *pszNewName, int bUpdate)
315
316 {
317 /* -------------------------------------------------------------------- */
318 /* Create the layer object. */
319 /* -------------------------------------------------------------------- */
320 OGRMySQLTableLayercxx . . $ OGRMySQLTableLayer# *poLayer;
321 OGRErr eErr;
322
323 poLayer = new OGRMySQLTableLayercxx . . $ OGRMySQLTableLayer#(this, pszNewName, bUpdate);
324 eErr = poLayer->Initializecxx . . $ OGRMySQLTableLayer#Initialize(5c4306d8553b3696).(pszNewName);
325 if (eErr == OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!)
326 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
327
328 /* -------------------------------------------------------------------- */
329 /* Add layer to data source layer list. */
330 /* -------------------------------------------------------------------- */
331 papoLayerscxx . . $ OGRMySQLDataSource#papoLayers. = (OGRMySQLLayercxx . . $ OGRMySQLLayer# **)CPLRealloc(
332 papoLayerscxx . . $ OGRMySQLDataSource#papoLayers., sizeof(OGRMySQLLayercxx . . $ OGRMySQLLayer# *) * (nLayerscxx . . $ OGRMySQLDataSource#nLayers. + 1));
333 papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.[nLayerscxx . . $ OGRMySQLDataSource#nLayers.++] = poLayer;
334
335 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
336 }
337
338 /************************************************************************/
339 /* TestCapability() */
340 /************************************************************************/
341
342 int OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::TestCapabilitycxx . . $ OGRMySQLDataSource#TestCapability(78f035161ffa9fe2).(const char *pszCap) const
343
344 {
345 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszCap, ODsCCreateLayercxx . . $ `ogr/ogr_core.h:1040:9`!))
346 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
347 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszCap, ODsCDeleteLayercxx . . $ `ogr/ogr_core.h:1042:9`!))
348 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
349 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszCap, ODsCRandomLayerWritecxx . . $ `ogr/ogr_core.h:1065:9`!))
350 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
351 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszCap, ODsCMeasuredGeometriescxx . . $ `ogr/ogr_core.h:1055:9`!))
352 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
353 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszCap, ODsCZGeometriescxx . . $ `ogr/ogr_core.h:1058:9`!))
354 return TRUEcxx . . $ `port/cpl_port.h:1184:9`!;
355
356 return FALSEcxx . . $ `port/cpl_port.h:1180:9`!;
357 }
358
359 /************************************************************************/
360 /* GetLayer() */
361 /************************************************************************/
362
363 const OGRLayercxx . . $ OGRLayer# *OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::GetLayercxx . . $ OGRMySQLDataSource#GetLayer(3ead67c79a3185c4).(int iLayer) const
364
365 {
366 if (iLayer < 0 || iLayer >= nLayerscxx . . $ OGRMySQLDataSource#nLayers.)
367 return nullptr;
368 else
369 return papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.[iLayer];
370 }
371
372 /* =====================================================================*/
373 /* Handle spatial reference id and index for older MySQL and MariaDB */
374 /* =====================================================================*/
375
376 /************************************************************************/
377 /* InitializeMetadataTables() */
378 /* */
379 /* Create the metadata tables (SPATIAL_REF_SYS and */
380 /* GEOMETRY_COLUMNS). This method "does no harm" if the tables */
381 /* exist and can be called at will. */
382 /************************************************************************/
383
384 OGRErr OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::InitializeMetadataTablescxx . . $ OGRMySQLDataSource#InitializeMetadataTables(b126dc7c1de90089).()
385
386 {
387 const char *pszCommand;
388 MYSQL_RES *hResult;
389 OGRErr eErr = OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!;
390
391 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() < 8 || IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).())
392 {
393 pszCommand = "DESCRIBE geometry_columns";
394 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), pszCommand))
395 {
396 pszCommand = "CREATE TABLE geometry_columns "
397 "( F_TABLE_CATALOG VARCHAR(256), "
398 "F_TABLE_SCHEMA VARCHAR(256), "
399 "F_TABLE_NAME VARCHAR(256) NOT NULL,"
400 "F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL, "
401 "COORD_DIMENSION INT, "
402 "SRID INT,"
403 "TYPE VARCHAR(256) NOT NULL)";
404 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), pszCommand))
405 {
406 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(pszCommand);
407 eErr = OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!;
408 }
409 else
410 CPLDebug("MYSQL", "Creating geometry_columns metadata table");
411 }
412
413 // make sure to attempt to free results of successful queries
414 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
415 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
416
417 pszCommand = "DESCRIBE spatial_ref_sys";
418 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), pszCommand))
419 {
420 pszCommand = "CREATE TABLE spatial_ref_sys "
421 "(SRID INT NOT NULL, "
422 "AUTH_NAME VARCHAR(256), "
423 "AUTH_SRID INT, "
424 "SRTEXT VARCHAR(2048))";
425 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), pszCommand))
426 {
427 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(pszCommand);
428 eErr = OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!;
429 }
430 else
431 CPLDebug("MYSQL", "Creating spatial_ref_sys metadata table");
432 }
433
434 // make sure to attempt to free results of successful queries
435 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
436 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
437 }
438
439 return eErr;
440 }
441
442 OGRErr OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::UpdateMetadataTablescxx . . $ OGRMySQLDataSource#UpdateMetadataTables(46d5f2e6fd84b551).(const char *pszLayerName,
443 OGRwkbGeometryType eType,
444 const char *pszGeomColumnName,
445 const int nSRSId)
446
447 {
448 MYSQL_RES *hResult = nullptr;
449 CPLString osCommand;
450 const char *pszGeometryType;
451
452 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() < 8 || IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).())
453 {
454 /* --------------------------------------------------------------------
455 */
456 /* Sometimes there is an old crufty entry in the geometry_columns
457 */
458 /* table if things were not properly cleaned up before. We make */
459 /* an effort to clean out such cruft. */
460 /* */
461 /* --------------------------------------------------------------------
462 */
463 osCommand.Printf(
464 "DELETE FROM geometry_columns WHERE f_table_name = '%s'",
465 pszLayerName);
466
467 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
468 {
469 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(osCommand);
470 return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!;
471 }
472
473 // make sure to attempt to free results of successful queries
474 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
475 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
476
477 /* --------------------------------------------------------------------
478 */
479 /* Attempt to add this table to the geometry_columns table, if */
480 /* it is a spatial layer. */
481 /* --------------------------------------------------------------------
482 */
483 if (eType != wkbNone)
484 {
485 const int nCoordDimension = eType == wkbFlattencxx . . $ `ogr/ogr_core.h:576:9`!(eType) ? 2 : 3;
486
487 pszGeometryType = OGRToOGCGeomTypecxx . . $ OGRToOGCGeomType(59e7cc99d9ae6078).(eType);
488
489 if (nSRSId == GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).())
490 osCommand.Printf("INSERT INTO geometry_columns "
491 " (F_TABLE_NAME, "
492 " F_GEOMETRY_COLUMN, "
493 " COORD_DIMENSION, "
494 " TYPE) values "
495 " ('%s', '%s', %d, '%s')",
496 pszLayerName, pszGeomColumnName,
497 nCoordDimension, pszGeometryType);
498 else
499 osCommand.Printf("INSERT INTO geometry_columns "
500 " (F_TABLE_NAME, "
501 " F_GEOMETRY_COLUMN, "
502 " COORD_DIMENSION, "
503 " SRID, "
504 " TYPE) values "
505 " ('%s', '%s', %d, %d, '%s')",
506 pszLayerName, pszGeomColumnName,
507 nCoordDimension, nSRSId, pszGeometryType);
508
509 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
510 {
511 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(osCommand);
512 return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!;
513 }
514
515 // make sure to attempt to free results of successful queries
516 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
517 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
518 }
519 }
520 return OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!;
521 }
522
523 /* =====================================================================*/
524
525 /************************************************************************/
526 /* FetchSRS() */
527 /* */
528 /* Return a SRS corresponding to a particular id. Note that */
529 /* reference counting should be honoured on the returned */
530 /* OGRSpatialReference, as handles may be cached. */
531 /************************************************************************/
532
533 OGRSpatialReferenceRefCountedPtrcxx . . $ OGRSpatialReferenceRefCountedPtr# OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::FetchSRScxx . . $ OGRMySQLDataSource#FetchSRS(3853944a26d3d92b).(int nId)
534 {
535 if (nId < 0)
536 return nullptrcxx . . $ OGRRefCountedPtr#OGRRefCountedPtr(49b4c06ce5d54a60).;
537
538 /* -------------------------------------------------------------------- */
539 /* First, we look through our SRID cache, is it there? */
540 /* -------------------------------------------------------------------- */
541 auto oIter = m_oSRSCachecxx . . $ OGRMySQLDataSource#m_oSRSCache..findcxx . . $ std/map#find(36f8d56706583706).(nId);
542 if (oIter !=cxx . . $ std/`operator!=`(be6a71a859368080). m_oSRSCachecxx . . $ OGRMySQLDataSource#m_oSRSCache..endcxx . . $ std/map#end(7e4de33083b6e738).())
543 {
544 return oIter->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second.;
545 }
546
547 // make sure to attempt to free any old results
548 MYSQL_RES *hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
549 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
550
551 char szCommand[128] = {};
552 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() < 8 || IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).())
553 {
554 snprintf(szCommand, sizeof(szCommand),
555 "SELECT srtext FROM spatial_ref_sys WHERE srid = %d", nId);
556 }
557 else
558 {
559 snprintf(
560 szCommand, sizeof(szCommand),
561 "SELECT DEFINITION FROM "
562 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS WHERE SRS_ID = %d",
563 nId);
564 }
565
566 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), szCommand))
567 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
568
569 char *pszWKT = nullptr;
570 char **papszRow = nullptr;
571
572 if (hResult != nullptr)
573 papszRow = mysql_fetch_row(hResult);
574
575 if (papszRow != nullptr && papszRow[0] != nullptr)
576 {
577 pszWKT = CPLStrdup(papszRow[0]);
578 }
579
580 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
581
582 auto poSRS = OGRSpatialReferenceRefCountedPtrcxx . . $ OGRSpatialReferenceRefCountedPtr#::makeInstancecxx . . $ OGRRefCountedPtr#makeInstance(1adaeb8070080dba).();
583 poSRS->cxx . . $ OGRRefCountedPtrBase#`operator->`(5a2a78a048fb49a8).SetAxisMappingStrategycxx . . $ OGRSpatialReference#SetAxisMappingStrategy(3d39f43974738d6a).(OAMS_TRADITIONAL_GIS_ORDER);
584 if (pszWKT == nullptr || poSRS->cxx . . $ OGRRefCountedPtrBase#`operator->`(5a2a78a048fb49a8).importFromWktcxx . . $ OGRSpatialReference#importFromWkt(5c4306d8553b3696).(pszWKT) != OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!)
585 {
586 poSRS.resetcxx . . $ OGRRefCountedPtr#reset(49f6e7a06ebc5aa8).();
587 }
588
589 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT);
590
591 if (poSRS)
592 {
593 // The WKT found in MySQL 8 ST_SPATIAL_REFERENCE_SYSTEMS is not
594 // compatible of what GDAL understands.
595 const char *pszAuthorityName = poSRS->cxx . . $ OGRRefCountedPtrBase#`operator->`(5a2a78a048fb49a8).GetAuthorityNamecxx . . $ OGRSpatialReference#GetAuthorityName(865ea18671f48fc1).();
596 const char *pszAuthorityCode = poSRS->cxx . . $ OGRRefCountedPtrBase#`operator->`(5a2a78a048fb49a8).GetAuthorityCodecxx . . $ OGRSpatialReference#GetAuthorityCode(865ea18671f48fc1).();
597 if (pszAuthorityName != nullptr && EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszAuthorityName, "EPSG") &&
598 pszAuthorityCode != nullptr && strlen(pszAuthorityCode) > 0)
599 {
600 /* Import 'clean' SRS */
601 poSRS->cxx . . $ OGRRefCountedPtrBase#`operator->`(5a2a78a048fb49a8).importFromEPSGcxx . . $ OGRSpatialReference#importFromEPSG(7864480464b09eea).(atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszAuthorityCode));
602 }
603 }
604
605 /* -------------------------------------------------------------------- */
606 /* Add to the cache. */
607 /* -------------------------------------------------------------------- */
608 oIter =cxx . . $ std/_Rb_tree_iterator#`operator=`(6856fb145ac9be87). m_oSRSCachecxx . . $ OGRMySQLDataSource#m_oSRSCache..emplacecxx . . $ std/map#emplace(58eea87a368e5c96).(nId, stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(poSRS)).firstcxx . . $ std/pair#first.;
609 return oIter->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second.;
610 }
611
612 /************************************************************************/
613 /* FetchSRSId() */
614 /* */
615 /* Fetch the id corresponding to an SRS, and if not found, add */
616 /* it to the table. */
617 /************************************************************************/
618
619 int OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::FetchSRSIdcxx . . $ OGRMySQLDataSource#FetchSRSId(8a145e066b724d94).(const OGRSpatialReferencecxx . . $ OGRSpatialReference# *poSRSIn)
620
621 {
622 if (poSRSIn == nullptr)
623 return GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).();
624
625 OGRSpatialReferencecxx . . $ OGRSpatialReference# oSRScxx . . $ OGRSpatialReference#OGRSpatialReference(92326a3924bcc3c0).(*poSRSIn);
626 // cppcheck-suppress uselessAssignmentPtrArg
627 poSRSIn = nullptr;
628
629 const char *pszAuthorityName = oSRS.GetAuthorityNamecxx . . $ OGRSpatialReference#GetAuthorityName(865ea18671f48fc1).();
630 int nAuthorityCode = 0;
631 if (pszAuthorityName == nullptr || strlen(pszAuthorityName) == 0)
632 {
633 /* --------------------------------------------------------------------
634 */
635 /* Try to identify an EPSG code */
636 /* --------------------------------------------------------------------
637 */
638 oSRS.AutoIdentifyEPSGcxx . . $ OGRSpatialReference#AutoIdentifyEPSG(b126dc7c1de90089).();
639
640 pszAuthorityName = oSRS.GetAuthorityNamecxx . . $ OGRSpatialReference#GetAuthorityName(865ea18671f48fc1).();
641 if (pszAuthorityName != nullptr && EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszAuthorityName, "EPSG"))
642 {
643 const char *pszAuthorityCode = oSRS.GetAuthorityCodecxx . . $ OGRSpatialReference#GetAuthorityCode(865ea18671f48fc1).();
644 if (pszAuthorityCode != nullptr && strlen(pszAuthorityCode) > 0)
645 {
646 /* Import 'clean' SRS */
647 nAuthorityCode = atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszAuthorityCode);
648 oSRS.importFromEPSGcxx . . $ OGRSpatialReference#importFromEPSG(7864480464b09eea).(nAuthorityCode);
649
650 pszAuthorityName = oSRS.GetAuthorityNamecxx . . $ OGRSpatialReference#GetAuthorityName(865ea18671f48fc1).();
651 }
652 }
653 }
654 else
655 {
656 const char *pszAuthorityCode = oSRS.GetAuthorityCodecxx . . $ OGRSpatialReference#GetAuthorityCode(865ea18671f48fc1).();
657 if (pszAuthorityCode)
658 nAuthorityCode = atoicxx . . $ atoi(f5ecf3191ed0cd93).(pszAuthorityCode);
659 }
660
661 /* -------------------------------------------------------------------- */
662 /* Check whether the authority name/code is already mapped to a */
663 /* SRS ID. */
664 /* -------------------------------------------------------------------- */
665 CPLString osCommand;
666 if (pszAuthorityName != nullptr)
667 {
668 /* Check that the authority code is integral */
669 if (nAuthorityCode > 0)
670 {
671 const char *pszTableName =
672 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS";
673 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() < 8 || IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).())
674 {
675 pszTableName = "spatial_ref_sys";
676 osCommand.Printf(
677 "SELECT srid FROM spatial_ref_sys WHERE "
678 "auth_name = '%s' AND auth_srid = %d",
679 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszAuthorityName).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(),
680 nAuthorityCode);
681 }
682 else
683 {
684 osCommand.Printf(
685 "SELECT SRS_ID FROM "
686 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS "
687 "WHERE ORGANIZATION = '%s' AND ORGANIZATION_COORDSYS_ID = "
688 "%d",
689 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszAuthorityName).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(),
690 nAuthorityCode);
691 }
692
693 MYSQL_RES *hResult = nullptr;
694 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
695 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
696
697 if (hResult != nullptr && !mysql_num_rows(hResult))
698 {
699 CPLDebug("MYSQL",
700 "No rows exist currently exist in %s for %s:%d",
701 pszTableName, pszAuthorityName, nAuthorityCode);
702 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
703 }
704 char **papszRow = nullptr;
705 if (hResult != nullptr)
706 papszRow = mysql_fetch_row(hResult);
707
708 if (papszRow != nullptr && papszRow[0] != nullptr)
709 {
710 const int nSRSId = atoicxx . . $ atoi(f5ecf3191ed0cd93).(papszRow[0]);
711 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
712 return nSRSId;
713 }
714
715 // make sure to attempt to free results of successful queries
716 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
717 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
718 }
719 }
720
721 /* -------------------------------------------------------------------- */
722 /* Translate SRS to WKT. */
723 /* -------------------------------------------------------------------- */
724 char *pszWKT = nullptr;
725 if (oSRS.exportToWktcxx . . $ OGRSpatialReference#exportToWkt(7572283d65580925).(&pszWKT) != OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!)
726 {
727 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT);
728 return GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).();
729 }
730
731 // MySQL 8 requires AXIS[] node in PROJCS.GEOGCS
732 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() >= 8 && !IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).() && oSRS.IsProjectedcxx . . $ OGRSpatialReference#IsProjected(455f465bc33b4cdf).())
733 {
734 OGR_SRSNodecxx . . $ OGR_SRSNode# oNodecxx . . $ OGR_SRSNode#OGR_SRSNode(85c52e162fed56f9).;
735 const char *pszWKTTmp = pszWKT;
736 oNode.importFromWktcxx . . $ OGR_SRSNode#importFromWkt(15355eb6db2855df).(&pszWKTTmp);
737
738 OGRSpatialReferencecxx . . $ OGRSpatialReference# oSRSGeogcxx . . $ OGRSpatialReference#OGRSpatialReference(85c52e162fed56f9).;
739 oSRSGeog.CopyGeogCSFromcxx . . $ OGRSpatialReference#CopyGeogCSFrom(8a145e066b724d94).(&oSRS);
740 char *pszWKTGeog = nullptr;
741 oSRSGeog.exportToWktcxx . . $ OGRSpatialReference#exportToWkt(7572283d65580925).(&pszWKTGeog);
742
743 int iChild = oNode.FindChildcxx . . $ OGR_SRSNode#FindChild(78f035161ffa9fe2).("GEOGCS");
744 if (iChild >= 0)
745 {
746 oNode.DestroyChildcxx . . $ OGR_SRSNode#DestroyChild(d4f767463ce0a6b3).(iChild);
747 auto poGeogNode = new OGR_SRSNodecxx . . $ OGR_SRSNode#();
748 pszWKTTmp = pszWKTGeog;
749 poGeogNode->importFromWktcxx . . $ OGR_SRSNode#importFromWkt(15355eb6db2855df).(&pszWKTTmp);
750 oNode.InsertChildcxx . . $ OGR_SRSNode#InsertChild(d87f371930318f8f).(poGeogNode, iChild);
751 }
752 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKTGeog);
753
754 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT);
755 oNode.exportToWktcxx . . $ OGR_SRSNode#exportToWkt(7572283d65580925).(&pszWKT);
756 }
757
758 /* -------------------------------------------------------------------- */
759 /* Try to find in the existing record. */
760 /* -------------------------------------------------------------------- */
761 const char *pszTableName =
762 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS";
763 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() < 8 || IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).())
764 {
765 pszTableName = "spatial_ref_sys";
766 osCommand.Printf("SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
767 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszWKT).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
768 }
769 else
770 {
771 osCommand.Printf("SELECT SRS_ID FROM "
772 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS "
773 "WHERE DEFINITION = '%s'",
774 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszWKT).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
775 }
776
777 MYSQL_RES *hResult = nullptr;
778 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
779 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
780
781 if (hResult != nullptr && !mysql_num_rows(hResult))
782 {
783 CPLDebug("MYSQL", "No rows exist currently exist in %s with WKT = %s",
784 pszTableName, pszWKT);
785 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
786 }
787 char **papszRow = nullptr;
788 if (hResult != nullptr)
789 papszRow = mysql_fetch_row(hResult);
790
791 if (papszRow != nullptr && papszRow[0] != nullptr)
792 {
793 const int nSRSId = atoicxx . . $ atoi(f5ecf3191ed0cd93).(papszRow[0]);
794 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
795 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT);
796 return nSRSId;
797 }
798
799 // make sure to attempt to free results of successful queries
800 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
801 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
802
803 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() >= 8 && !IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).())
804 {
805 int nSRSId = -1;
806 if (pszAuthorityName != nullptr && EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszAuthorityName, "EPSG") &&
807 nAuthorityCode > 0)
808 {
809 /* ------------------------------------------------------------ */
810 /* If it is an EPSG code, check if we can use the entry of */
811 /* SRS_ID equal to the EPSG code. */
812 /* ------------------------------------------------------------ */
813 osCommand.Printf("SELECT SRS_ID FROM INFORMATION_SCHEMA."
814 "ST_SPATIAL_REFERENCE_SYSTEMS "
815 "WHERE SRS_ID = %d",
816 nAuthorityCode);
817 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
818 {
819 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
820 papszRow = mysql_fetch_row(hResult);
821 if (!(papszRow != nullptr && papszRow[0] != nullptr))
822 {
823 // No row matching SRS_ID = nAuthorityCode ? Then
824 // we can use it
825 nSRSId = nAuthorityCode;
826 }
827 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
828 }
829 }
830 if (nSRSId < 0)
831 {
832 nSRSId = 1;
833
834 /* ------------------------------------------------------------- */
835 /* Get the current maximum srid in the srs table. */
836 /* ------------------------------------------------------------- */
837 osCommand = "SELECT MAX(SRS_ID) FROM "
838 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS";
839 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
840 {
841 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
842 papszRow = mysql_fetch_row(hResult);
843 if (papszRow != nullptr && papszRow[0] != nullptr)
844 {
845 nSRSId = atoicxx . . $ atoi(f5ecf3191ed0cd93).(papszRow[0]) + 1;
846 }
847 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
848 }
849 }
850 else
851 {
852 nSRSId = nAuthorityCode;
853 }
854
855 /* ----------------------------------------------------------------- */
856 /* Check if there's an existing record with same name */
857 /* ----------------------------------------------------------------- */
858 CPLString osName(oSRS.GetNamecxx . . $ OGRSpatialReference#GetName(a9f41ea0e82d88cf).());
859
860 osCommand.Printf(
861 "SELECT SRS_ID FROM "
862 "INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS WHERE NAME = '%s'",
863 osName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
864 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
865 {
866 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
867 papszRow = mysql_fetch_row(hResult);
868 if (papszRow != nullptr && papszRow[0] != nullptr)
869 {
870 osName +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). CPLSPrintf("_srid_%d", nSRSId);
871 }
872 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
873 }
874
875 /* ----------------------------------------------------------------- */
876 /* Try adding the SRS to the SRS table. */
877 /* ----------------------------------------------------------------- */
878 if (pszAuthorityName != nullptr && nAuthorityCode > 0)
879 {
880 osCommand.Printf(
881 "CREATE SPATIAL REFERENCE SYSTEM %d NAME '%s' "
882 "ORGANIZATION '%s' "
883 "IDENTIFIED BY %d "
884 "DEFINITION '%s'",
885 nSRSId, OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(osName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(),
886 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszAuthorityName).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), nAuthorityCode,
887 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszWKT).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
888 }
889 else
890 {
891 osCommand.Printf("CREATE SPATIAL REFERENCE SYSTEM %d NAME '%s' "
892 "DEFINITION '%s'",
893 nSRSId,
894 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(osName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(),
895 OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(pszWKT).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
896 }
897
898 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
899 {
900 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).((osCommand +cxx . . $ std/operator+(40fc1db9226233c6). " failed").c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
901 nSRSId = GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).();
902 }
903
904 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
905 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
906
907 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT);
908 return nSRSId;
909 }
910
911 /* -------------------------------------------------------------------- */
912 /* Get the current maximum srid in the srs table. */
913 /* -------------------------------------------------------------------- */
914 osCommand = "SELECT MAX(srid) FROM spatial_ref_sys";
915 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
916 {
917 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
918 papszRow = mysql_fetch_row(hResult);
919 }
920
921 int nSRSId = papszRow != nullptr && papszRow[0] != nullptr
922 ? atoicxx . . $ atoi(f5ecf3191ed0cd93).(papszRow[0]) + 1
923 : 1;
924
925 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
926
927 /* -------------------------------------------------------------------- */
928 /* Try adding the SRS to the SRS table. */
929 /* -------------------------------------------------------------------- */
930 osCommand.Printf(
931 "INSERT INTO spatial_ref_sys (srid,srtext) VALUES (%d,'%s')", nSRSId,
932 pszWKT);
933
934 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
935 {
936 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).((osCommand +cxx . . $ std/operator+(40fc1db9226233c6). " failed").c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
937 nSRSId = GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).();
938 }
939
940 // make sure to attempt to free results of successful queries
941 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
942 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
943
944 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszWKT);
945
946 return nSRSId;
947 }
948
949 /************************************************************************/
950 /* ExecuteSQL() */
951 /************************************************************************/
952
953 OGRLayercxx . . $ OGRLayer# *OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::ExecuteSQLcxx . . $ OGRMySQLDataSource#ExecuteSQL(1e911e025f8b6f61).(const char *pszSQLCommand,
954 OGRGeometrycxx . . $ OGRGeometry# *poSpatialFilter,
955 const char *pszDialect)
956
957 {
958 if (poSpatialFilter != nullptr)
959 {
960 CPLDebug("OGR_MYSQL", "Spatial filter ignored for now in "
961 "OGRMySQLDataSource::ExecuteSQL()");
962 }
963
964 /* -------------------------------------------------------------------- */
965 /* Use generic implementation for recognized dialects */
966 /* -------------------------------------------------------------------- */
967 if (IsGenericSQLDialectcxx . . $ GDALDataset#IsGenericSQLDialect(5c4306d8553b3696).(pszDialect))
968 return GDALDatasetcxx . . $ GDALDataset#::ExecuteSQLcxx . . $ GDALDataset#ExecuteSQL(1e911e025f8b6f61).(pszSQLCommand, poSpatialFilter,
969 pszDialect);
970
971 /* -------------------------------------------------------------------- */
972 /* Special case DELLAYER: command. */
973 /* -------------------------------------------------------------------- */
974 #ifdef notdef
975 if (STARTS_WITH_CI(pszSQLCommand, "DELLAYER:"))
976 {
977 const char *pszLayerName = pszSQLCommand + 9;
978
979 while (*pszLayerName == ' ')
980 pszLayerName++;
981
982 DeleteLayer(pszLayerName);
983 return NULL;
984 }
985 #endif
986
987 /* -------------------------------------------------------------------- */
988 /* Make sure there isn't an active transaction already. */
989 /* -------------------------------------------------------------------- */
990 InterruptLongResultcxx . . $ OGRMySQLDataSource#InterruptLongResult(49f6e7a06ebc5aa8).();
991
992 /* -------------------------------------------------------------------- */
993 /* Execute the statement. */
994 /* -------------------------------------------------------------------- */
995 MYSQL_RES *hResultSet;
996
997 if (mysql_query(hConncxx . . $ OGRMySQLDataSource#hConn., pszSQLCommand))
998 {
999 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(pszSQLCommand);
1000 return nullptr;
1001 }
1002
1003 hResultSet = mysql_use_result(hConncxx . . $ OGRMySQLDataSource#hConn.);
1004 if (hResultSet == nullptr)
1005 {
1006 if (mysql_field_count(hConncxx . . $ OGRMySQLDataSource#hConn.) == 0)
1007 {
1008 CPLDebug("MYSQL", "Command '%s' succeeded, %d rows affected.",
1009 pszSQLCommand, (int)mysql_affected_rows(hConncxx . . $ OGRMySQLDataSource#hConn.));
1010 return nullptr;
1011 }
1012 else
1013 {
1014 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(pszSQLCommand);
1015 return nullptr;
1016 }
1017 }
1018
1019 /* -------------------------------------------------------------------- */
1020 /* Do we have a tuple result? If so, instantiate a results */
1021 /* layer for it. */
1022 /* -------------------------------------------------------------------- */
1023
1024 OGRMySQLResultLayercxx . . $ OGRMySQLResultLayer# *poLayer =
1025 new OGRMySQLResultLayercxx . . $ OGRMySQLResultLayer#(this, pszSQLCommand, hResultSet);
1026
1027 return poLayer;
1028 }
1029
1030 /************************************************************************/
1031 /* ReleaseResultSet() */
1032 /************************************************************************/
1033
1034 void OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::ReleaseResultSetcxx . . $ OGRMySQLDataSource#ReleaseResultSet(74233f8ea7d835ac).(OGRLayercxx . . $ OGRLayer# *poLayer)
1035
1036 {
1037 delete poLayer;
1038 }
1039
1040 /************************************************************************/
1041 /* LaunderName() */
1042 /************************************************************************/
1043
1044 char *OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::LaunderNamecxx . . $ OGRMySQLDataSource#LaunderName(a8b8e32347182212).(const char *pszSrcName)
1045
1046 {
1047 char *pszSafeName = CPLStrdup(pszSrcName);
1048
1049 for (int i = 0; pszSafeName[i] != '\0'; i++)
1050 {
1051 pszSafeName[i] =
1052 (char)CPLTolower(static_cast<unsigned char>(pszSafeName[i]));
1053 if (pszSafeName[i] == '-' || pszSafeName[i] == '#')
1054 pszSafeName[i] = '_';
1055 }
1056
1057 return pszSafeName;
1058 }
1059
1060 /************************************************************************/
1061 /* RequestLongResult() */
1062 /* */
1063 /* Layers need to use mysql_use_result() instead of */
1064 /* mysql_store_result() so that we won't have to load entire */
1065 /* result sets into RAM. But only one "streamed" resultset can */
1066 /* be active on a database connection at a time. So we need to */
1067 /* maintain a way of closing off an active streaming resultset */
1068 /* before any other sort of query with a resultset is */
1069 /* executable. This method (and InterruptLongResult()) */
1070 /* implement that exclusion. */
1071 /************************************************************************/
1072
1073 void OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::RequestLongResultcxx . . $ OGRMySQLDataSource#RequestLongResult(2b4f650d47eaae7e).(OGRMySQLLayercxx . . $ OGRMySQLLayer# *poNewLayer)
1074
1075 {
1076 InterruptLongResultcxx . . $ OGRMySQLDataSource#InterruptLongResult(49f6e7a06ebc5aa8).();
1077 poLongResultLayercxx . . $ OGRMySQLDataSource#poLongResultLayer. = poNewLayer;
1078 }
1079
1080 /************************************************************************/
1081 /* InterruptLongResult() */
1082 /************************************************************************/
1083
1084 void OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::InterruptLongResultcxx . . $ OGRMySQLDataSource#InterruptLongResult(49f6e7a06ebc5aa8).()
1085
1086 {
1087 if (poLongResultLayercxx . . $ OGRMySQLDataSource#poLongResultLayer. != nullptr)
1088 {
1089 poLongResultLayercxx . . $ OGRMySQLDataSource#poLongResultLayer.->ResetReadingcxx . . $ OGRMySQLLayer#ResetReading(49f6e7a06ebc5aa8).();
1090 poLongResultLayercxx . . $ OGRMySQLDataSource#poLongResultLayer. = nullptr;
1091 }
1092 }
1093
1094 /************************************************************************/
1095 /* DeleteLayer() */
1096 /************************************************************************/
1097
1098 OGRErr OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::DeleteLayercxx . . $ OGRMySQLDataSource#DeleteLayer(7864480464b09eea).(int iLayer)
1099
1100 {
1101 if (iLayer < 0 || iLayer >= nLayerscxx . . $ OGRMySQLDataSource#nLayers.)
1102 return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!;
1103
1104 /* -------------------------------------------------------------------- */
1105 /* Blow away our OGR structures related to the layer. This is */
1106 /* pretty dangerous if anything has a reference to this layer! */
1107 /* -------------------------------------------------------------------- */
1108 CPLString osLayerName = papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.[iLayer]->GetLayerDefncxx . . $ OGRLayer#GetLayerDefn(407fccde18759965).()->GetNamecxx . . $ OGRFeatureDefn#GetName(a9f41ea0e82d88cf).();
1109
1110 CPLDebug("MYSQL", "DeleteLayer(%s)", osLayerName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
1111
1112 delete papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.[iLayer];
1113 memmove(papoLayerscxx . . $ OGRMySQLDataSource#papoLayers. + iLayer, papoLayerscxx . . $ OGRMySQLDataSource#papoLayers. + iLayer + 1,
1114 sizeof(void *) * (nLayerscxx . . $ OGRMySQLDataSource#nLayers. - iLayer - 1));
1115 nLayerscxx . . $ OGRMySQLDataSource#nLayers.--;
1116
1117 /* -------------------------------------------------------------------- */
1118 /* Remove from the database. */
1119 /* -------------------------------------------------------------------- */
1120 CPLString osCommand;
1121
1122 osCommand.Printf("DROP TABLE `%s` ", osLayerName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
1123
1124 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
1125 {
1126 CPLDebug("MYSQL", "Dropped table %s.", osLayerName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
1127 return OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!;
1128 }
1129 else
1130 {
1131 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(osCommand);
1132 return OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!;
1133 }
1134 }
1135
1136 /************************************************************************/
1137 /* ICreateLayer() */
1138 /************************************************************************/
1139
1140 OGRLayercxx . . $ OGRLayer# *
1141 OGRMySQLDataSourcecxx . . $ OGRMySQLDataSource#::ICreateLayercxx . . $ OGRMySQLDataSource#ICreateLayer(d0342318947b6f88).(const char *pszLayerNameIn,
1142 const OGRGeomFieldDefncxx . . $ OGRGeomFieldDefn# *poGeomFieldDefn,
1143 CSLConstListcxx . . $ CSLConstList# papszOptions)
1144
1145 {
1146 MYSQL_RES *hResult = nullptr;
1147 CPLString osCommand;
1148 const char *pszGeomColumnName;
1149 const char *pszExpectedFIDName;
1150 char *pszLayerName;
1151 // int nDimension = 3; // MySQL only supports 2d currently
1152
1153 /* -------------------------------------------------------------------- */
1154 /* Make sure there isn't an active transaction already. */
1155 /* -------------------------------------------------------------------- */
1156 InterruptLongResultcxx . . $ OGRMySQLDataSource#InterruptLongResult(49f6e7a06ebc5aa8).();
1157
1158 const auto eType = poGeomFieldDefn ? poGeomFieldDefn->GetTypecxx . . $ OGRGeomFieldDefn#GetType(5a40c54c5ff210ce).() : wkbNone;
1159 const auto poSRS =
1160 poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRefcxx . . $ OGRGeomFieldDefn#GetSpatialRef(8a5496c2eeefaeaf).() : nullptr;
1161
1162 if (CPLFetchBool(papszOptions, "LAUNDER", true))
1163 pszLayerName = LaunderNamecxx . . $ OGRMySQLDataSource#LaunderName(a8b8e32347182212).(pszLayerNameIn);
1164 else
1165 pszLayerName = CPLStrdup(pszLayerNameIn);
1166
1167 // if( wkbFlatten(eType) == eType )
1168 // nDimension = 2;
1169
1170 CPLDebug("MYSQL", "Creating layer %s.", pszLayerName);
1171
1172 /* -------------------------------------------------------------------- */
1173 /* Do we already have this layer? If so, should we blow it */
1174 /* away? */
1175 /* -------------------------------------------------------------------- */
1176
1177 for (int iLayer = 0; iLayer < nLayerscxx . . $ OGRMySQLDataSource#nLayers.; iLayer++)
1178 {
1179 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszLayerName, papoLayers[iLayer]->GetLayerDefn()->GetName()))
1180 {
1181
1182 if (CSLFetchNameValue(papszOptions, "OVERWRITE") != nullptr &&
1183 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(CSLFetchNameValue(papszOptions, "OVERWRITE"), "NO"))
1184 {
1185 DeleteLayercxx . . $ OGRMySQLDataSource#DeleteLayer(7864480464b09eea).(iLayer);
1186 }
1187 else
1188 {
1189 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
1190 "Layer %s already exists, CreateLayer failed.\n"
1191 "Use the layer creation option OVERWRITE=YES to "
1192 "replace it.",
1193 pszLayerName);
1194 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszLayerName);
1195 return nullptr;
1196 }
1197 }
1198 }
1199
1200 pszGeomColumnName = CSLFetchNameValue(papszOptions, "GEOMETRY_NAME");
1201 if (!pszGeomColumnName)
1202 pszGeomColumnName = "SHAPE";
1203
1204 pszExpectedFIDName = CSLFetchNameValue(papszOptions, "FID");
1205 if (!pszExpectedFIDName)
1206 pszExpectedFIDName = CSLFetchNameValue(papszOptions, "MYSQL_FID");
1207 if (!pszExpectedFIDName)
1208 pszExpectedFIDName = "OGR_FID";
1209
1210 const bool bFID64 = CPLFetchBool(papszOptions, "FID64", false);
1211 const char *pszFIDType = bFID64 ? "BIGINT" : "INT";
1212
1213 CPLDebug("MYSQL", "Geometry Column Name %s.", pszGeomColumnName);
1214 CPLDebug("MYSQL", "FID Column Name %s.", pszExpectedFIDName);
1215
1216 const char *pszSI = CSLFetchNameValue(papszOptions, "SPATIAL_INDEX");
1217 const bool bHasSI =
1218 (eType != wkbNone && (pszSI == nullptr || CPLTestBool(pszSI)));
1219
1220 // Calling this does no harm
1221 InitializeMetadataTablescxx . . $ OGRMySQLDataSource#InitializeMetadataTables(b126dc7c1de90089).();
1222
1223 /* -------------------------------------------------------------------- */
1224 /* Try to get the SRS Id of this spatial reference system, */
1225 /* adding to the srs table if needed. */
1226 /* -------------------------------------------------------------------- */
1227
1228 int nSRSId = GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).();
1229 if (poSRS != nullptr)
1230 nSRSId = FetchSRSIdcxx . . $ OGRMySQLDataSource#FetchSRSId(8a145e066b724d94).(poSRS);
1231
1232 if (wkbFlattencxx . . $ `ogr/ogr_core.h:576:9`!(eType) == wkbNone)
1233 {
1234 osCommand.Printf("CREATE TABLE `%s` ( "
1235 " %s %s UNIQUE NOT NULL AUTO_INCREMENT )",
1236 pszLayerName, pszExpectedFIDName, pszFIDType);
1237 }
1238 else
1239 {
1240 // when using mysql8 and SRS is specified, use SRID option for geometry.
1241 if (GetMajorVersioncxx . . $ OGRMySQLDataSource#GetMajorVersion(455f465bc33b4cdf).() < 8 || IsMariaDBcxx . . $ OGRMySQLDataSource#IsMariaDB(50ce9a9e25b4a850).() || nSRSId == GetUnknownSRIDcxx . . $ OGRMySQLDataSource#GetUnknownSRID(455f465bc33b4cdf).())
1242 osCommand.Printf("CREATE TABLE `%s` ( "
1243 " %s %s UNIQUE NOT NULL AUTO_INCREMENT, "
1244 " %s GEOMETRY %s)",
1245 pszLayerName, pszExpectedFIDName, pszFIDType,
1246 pszGeomColumnName, bHasSI ? "NOT NULL" : "");
1247 else
1248 osCommand.Printf("CREATE TABLE `%s` ( "
1249 " %s %s UNIQUE NOT NULL AUTO_INCREMENT, "
1250 " %s GEOMETRY %s /*!80003 SRID %d */)",
1251 pszLayerName, pszExpectedFIDName, pszFIDType,
1252 pszGeomColumnName, bHasSI ? "NOT NULL" : "",
1253 nSRSId);
1254 }
1255
1256 if (CSLFetchNameValue(papszOptions, "ENGINE") != nullptr)
1257 {
1258 osCommand +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). " ENGINE = ";
1259 osCommand +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). CSLFetchNameValue(papszOptions, "ENGINE");
1260 }
1261
1262 if (!mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
1263 {
1264 if (mysql_field_count(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).()) == 0)
1265 CPLDebug("MYSQL", "Created table %s.", pszLayerName);
1266 else
1267 {
1268 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(osCommand);
1269 return nullptr;
1270 }
1271 }
1272 else
1273 {
1274 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(osCommand);
1275 return nullptr;
1276 }
1277
1278 // make sure to attempt to free results of successful queries
1279 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
1280 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
1281
1282 if (UpdateMetadataTablescxx . . $ OGRMySQLDataSource#UpdateMetadataTables(46d5f2e6fd84b551).(pszLayerName, eType, pszGeomColumnName, nSRSId) !=
1283 OGRERR_NONEcxx . . $ `ogr/ogr_core.h:390:9`!)
1284 return nullptr;
1285
1286 /* -------------------------------------------------------------------- */
1287 /* Create the spatial index. */
1288 /* */
1289 /* We're doing this before we add geometry and record to the table */
1290 /* so this may not be exactly the best way to do it. */
1291 /* -------------------------------------------------------------------- */
1292 if (bHasSI)
1293 {
1294 osCommand.Printf("ALTER TABLE `%s` ADD SPATIAL INDEX(`%s`) ",
1295 pszLayerName, pszGeomColumnName);
1296
1297 if (mysql_query(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).(), osCommand))
1298 {
1299 ReportErrorcxx . . $ OGRMySQLDataSource#ReportError(85c52e162fed56f9).(osCommand);
1300 return nullptr;
1301 }
1302
1303 // make sure to attempt to free results of successful queries
1304 hResult = mysql_store_result(GetConncxx . . $ OGRMySQLDataSource#GetConn(11f039e992169c28).());
1305 FreeResultAndNullifycxx . . $ FreeResultAndNullify(f222db2ec283f6e1).(hResult);
1306 }
1307
1308 /* -------------------------------------------------------------------- */
1309 /* Create the layer object. */
1310 /* -------------------------------------------------------------------- */
1311 OGRMySQLTableLayercxx . . $ OGRMySQLTableLayer# *poLayer;
1312 OGRErr eErr;
1313
1314 poLayer = new OGRMySQLTableLayercxx . . $ OGRMySQLTableLayer#(this, pszLayerName, TRUEcxx . . $ `port/cpl_port.h:1184:9`!, nSRSId);
1315 eErr = poLayer->Initializecxx . . $ OGRMySQLTableLayer#Initialize(5c4306d8553b3696).(pszLayerName);
1316 if (eErr == OGRERR_FAILUREcxx . . $ `ogr/ogr_core.h:396:9`!)
1317 return nullptr;
1318 if (eType != wkbNone)
1319 poLayer->GetLayerDefncxx . . $ OGRLayer#GetLayerDefn(407fccde18759965).()->GetGeomFieldDefncxx . . $ OGRFeatureDefn#GetGeomFieldDefn(39768c424f32bab2).(0)->SetNullablecxx . . $ OGRGeomFieldDefn#SetNullable(d4f767463ce0a6b3).(FALSEcxx . . $ `port/cpl_port.h:1180:9`!);
1320
1321 poLayer->SetLaunderFlagcxx . . $ OGRMySQLTableLayer#SetLaunderFlag(d4f767463ce0a6b3).(CPLFetchBool(papszOptions, "LAUNDER", true));
1322 poLayer->SetPrecisionFlagcxx . . $ OGRMySQLTableLayer#SetPrecisionFlag(d4f767463ce0a6b3).(CPLFetchBool(papszOptions, "PRECISION", true));
1323
1324 /* -------------------------------------------------------------------- */
1325 /* Add layer to data source layer list. */
1326 /* -------------------------------------------------------------------- */
1327 papoLayerscxx . . $ OGRMySQLDataSource#papoLayers. = (OGRMySQLLayercxx . . $ OGRMySQLLayer# **)CPLRealloc(
1328 papoLayerscxx . . $ OGRMySQLDataSource#papoLayers., sizeof(OGRMySQLLayercxx . . $ OGRMySQLLayer# *) * (nLayerscxx . . $ OGRMySQLDataSource#nLayers. + 1));
1329
1330 papoLayerscxx . . $ OGRMySQLDataSource#papoLayers.[nLayerscxx . . $ OGRMySQLDataSource#nLayers.++] = poLayer;
1331
1332 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszLayerName);
1333
1334 return poLayer;
1335 }
1336
1337 /************************************************************************/
1338 /* OGRMySQLEscapeLiteral() */
1339 /************************************************************************/
1340
1341 stdcxx . . $ std/::stringcxx . . $ std/string# OGRMySQLEscapeLiteralcxx . . $ OGRMySQLEscapeLiteral(d06bf7adff8c4caf).(const char *pszLiteral)
1342 {
1343 stdcxx . . $ std/::stringcxx . . $ std/string# osValcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
1344 for (int i = 0; pszLiteral[i] != '\0'; i++)
1345 {
1346 if (pszLiteral[i] == '\'')
1347 osVal +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). '\'';
1348 osVal +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). pszLiteral[i];
1349 }
1350 return osValcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
1351 }