Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Implements OGRMySQLDriver class. 5 : * Author: Frank Warmerdam, warmerdam@pobox.com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com> 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "ogr_mysql.h" 30 : #include "cpl_conv.h" 31 : #include "cpl_multiproc.h" 32 : 33 : #include "ogrmysqldrivercore.h" 34 : 35 : static CPLMutex *hMutex = nullptr; 36 : static int bInitialized = FALSE; 37 : 38 : /************************************************************************/ 39 : /* OGRMySQLDriverUnload() */ 40 : /************************************************************************/ 41 : 42 6 : static void OGRMySQLDriverUnload(CPL_UNUSED GDALDriver *poDriver) 43 : { 44 6 : if (bInitialized) 45 : { 46 2 : mysql_library_end(); 47 2 : bInitialized = FALSE; 48 : } 49 6 : if (hMutex != nullptr) 50 : { 51 2 : CPLDestroyMutex(hMutex); 52 2 : hMutex = nullptr; 53 : } 54 6 : } 55 : 56 : /************************************************************************/ 57 : /* Open() */ 58 : /************************************************************************/ 59 : 60 63 : static GDALDataset *OGRMySQLDriverOpen(GDALOpenInfo *poOpenInfo) 61 : 62 : { 63 : OGRMySQLDataSource *poDS; 64 : 65 63 : if (!OGRMySQLDriverIdentify(poOpenInfo)) 66 0 : return nullptr; 67 : 68 : { 69 63 : CPLMutexHolderD(&hMutex); 70 63 : if (!bInitialized) 71 : { 72 5 : if (mysql_library_init(0, nullptr, nullptr)) 73 : { 74 0 : CPLError(CE_Failure, CPLE_AppDefined, 75 : "Could not initialize MySQL library"); 76 0 : return nullptr; 77 : } 78 5 : bInitialized = TRUE; 79 : } 80 : } 81 : 82 63 : poDS = new OGRMySQLDataSource(); 83 : 84 63 : if (!poDS->Open(poOpenInfo->pszFilename, poOpenInfo->papszOpenOptions, 85 63 : poOpenInfo->eAccess == GA_Update)) 86 : { 87 1 : delete poDS; 88 1 : return nullptr; 89 : } 90 : else 91 62 : return poDS; 92 : } 93 : 94 : /************************************************************************/ 95 : /* Create() */ 96 : /************************************************************************/ 97 : 98 0 : static GDALDataset *OGRMySQLDriverCreate(const char *pszName, 99 : CPL_UNUSED int nBands, 100 : CPL_UNUSED int nXSize, 101 : CPL_UNUSED int nYSize, 102 : CPL_UNUSED GDALDataType eDT, 103 : CPL_UNUSED char **papszOptions) 104 : { 105 : OGRMySQLDataSource *poDS; 106 : 107 0 : poDS = new OGRMySQLDataSource(); 108 : 109 0 : if (!poDS->Open(pszName, nullptr, TRUE)) 110 : { 111 0 : delete poDS; 112 0 : CPLError(CE_Failure, CPLE_AppDefined, 113 : "MySQL driver doesn't currently support database creation.\n" 114 : "Please create database before using."); 115 0 : return nullptr; 116 : } 117 : 118 0 : return poDS; 119 : } 120 : 121 : /************************************************************************/ 122 : /* RegisterOGRMySQL() */ 123 : /************************************************************************/ 124 : 125 11 : void RegisterOGRMySQL() 126 : 127 : { 128 11 : if (!GDAL_CHECK_VERSION("MySQL driver")) 129 0 : return; 130 : 131 11 : if (GDALGetDriverByName(DRIVER_NAME) != nullptr) 132 0 : return; 133 : 134 11 : GDALDriver *poDriver = new GDALDriver(); 135 11 : OGRMySQLDriverSetCommonMetadata(poDriver); 136 : 137 11 : poDriver->pfnOpen = OGRMySQLDriverOpen; 138 11 : poDriver->pfnCreate = OGRMySQLDriverCreate; 139 11 : poDriver->pfnUnloadDriver = OGRMySQLDriverUnload; 140 : 141 11 : GetGDALDriverManager()->RegisterDriver(poDriver); 142 : }