LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/mysql - ogrmysqldriver.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 29 41 70.7 %
Date: 2024-11-21 22:18:42 Functions: 3 4 75.0 %

          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             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "ogr_mysql.h"
      14             : #include "cpl_conv.h"
      15             : #include "cpl_multiproc.h"
      16             : 
      17             : #include "ogrmysqldrivercore.h"
      18             : 
      19             : static CPLMutex *hMutex = nullptr;
      20             : static int bInitialized = FALSE;
      21             : 
      22             : /************************************************************************/
      23             : /*                        OGRMySQLDriverUnload()                        */
      24             : /************************************************************************/
      25             : 
      26           7 : static void OGRMySQLDriverUnload(CPL_UNUSED GDALDriver *poDriver)
      27             : {
      28           7 :     if (bInitialized)
      29             :     {
      30           2 :         mysql_library_end();
      31           2 :         bInitialized = FALSE;
      32             :     }
      33           7 :     if (hMutex != nullptr)
      34             :     {
      35           2 :         CPLDestroyMutex(hMutex);
      36           2 :         hMutex = nullptr;
      37             :     }
      38           7 : }
      39             : 
      40             : /************************************************************************/
      41             : /*                                Open()                                */
      42             : /************************************************************************/
      43             : 
      44          63 : static GDALDataset *OGRMySQLDriverOpen(GDALOpenInfo *poOpenInfo)
      45             : 
      46             : {
      47             :     OGRMySQLDataSource *poDS;
      48             : 
      49          63 :     if (!OGRMySQLDriverIdentify(poOpenInfo))
      50           0 :         return nullptr;
      51             : 
      52             :     {
      53          63 :         CPLMutexHolderD(&hMutex);
      54          63 :         if (!bInitialized)
      55             :         {
      56           5 :             if (mysql_library_init(0, nullptr, nullptr))
      57             :             {
      58           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
      59             :                          "Could not initialize MySQL library");
      60           0 :                 return nullptr;
      61             :             }
      62           5 :             bInitialized = TRUE;
      63             :         }
      64             :     }
      65             : 
      66          63 :     poDS = new OGRMySQLDataSource();
      67             : 
      68          63 :     if (!poDS->Open(poOpenInfo->pszFilename, poOpenInfo->papszOpenOptions,
      69          63 :                     poOpenInfo->eAccess == GA_Update))
      70             :     {
      71           1 :         delete poDS;
      72           1 :         return nullptr;
      73             :     }
      74             :     else
      75          62 :         return poDS;
      76             : }
      77             : 
      78             : /************************************************************************/
      79             : /*                               Create()                               */
      80             : /************************************************************************/
      81             : 
      82           0 : static GDALDataset *OGRMySQLDriverCreate(const char *pszName,
      83             :                                          CPL_UNUSED int nBands,
      84             :                                          CPL_UNUSED int nXSize,
      85             :                                          CPL_UNUSED int nYSize,
      86             :                                          CPL_UNUSED GDALDataType eDT,
      87             :                                          CPL_UNUSED char **papszOptions)
      88             : {
      89             :     OGRMySQLDataSource *poDS;
      90             : 
      91           0 :     poDS = new OGRMySQLDataSource();
      92             : 
      93           0 :     if (!poDS->Open(pszName, nullptr, TRUE))
      94             :     {
      95           0 :         delete poDS;
      96           0 :         CPLError(CE_Failure, CPLE_AppDefined,
      97             :                  "MySQL driver doesn't currently support database creation.\n"
      98             :                  "Please create database before using.");
      99           0 :         return nullptr;
     100             :     }
     101             : 
     102           0 :     return poDS;
     103             : }
     104             : 
     105             : /************************************************************************/
     106             : /*                          RegisterOGRMySQL()                          */
     107             : /************************************************************************/
     108             : 
     109          13 : void RegisterOGRMySQL()
     110             : 
     111             : {
     112          13 :     if (!GDAL_CHECK_VERSION("MySQL driver"))
     113           0 :         return;
     114             : 
     115          13 :     if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
     116           0 :         return;
     117             : 
     118          13 :     GDALDriver *poDriver = new GDALDriver();
     119          13 :     OGRMySQLDriverSetCommonMetadata(poDriver);
     120             : 
     121          13 :     poDriver->pfnOpen = OGRMySQLDriverOpen;
     122          13 :     poDriver->pfnCreate = OGRMySQLDriverCreate;
     123          13 :     poDriver->pfnUnloadDriver = OGRMySQLDriverUnload;
     124             : 
     125          13 :     GetGDALDriverManager()->RegisterDriver(poDriver);
     126             : }

Generated by: LCOV version 1.14