예제 #1
0
int DDBDataHandler::Get(void* key,int length,void*& value,int& value_len,struct sockaddr*& sock_addr,int& addr_len)
{
  int ret=0;
  connpool_conn* conn = GetConn(sock_addr,addr_len,1);
  if (!conn)
    return -1;
  DDBProtocolAdapter adapter;
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 50*1000;
  ret =adapter.Get(conn->sockfd,key,length,sock_addr,addr_len,0,&tv);
  FreeConn(conn,ret);
	return ret;  
}
예제 #2
0
int DDBDataHandler::Del(void* key,int length,struct sockaddr* sock_addr,int addr_len,int timeout=50)
{
  int ret=0;
  connpool_conn* conn = GetConn(sock_addr,addr_len,1);
  if (!conn)
    return -1;
  DDBProtocolAdapter adapter;
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = timeout*1000;
  ret =adapter.Del(conn->sockfd,key,length,0,&tv);
  FreeConn(conn,ret);
	return ret;  
}
예제 #3
0
void COnlineConnManager::SendPacketToClent(evutil_socket_t fd, const char* buf, int iBufSize)
{
	if (!buf || iBufSize == 0 || iBufSize > emMaxBuffLen)
	{
		return;
	}
	Conn *pConn = GetConn(fd);
	if (pConn)
	{
		if (pConn->bufev)
		{
			struct evbuffer * output = bufferevent_get_output(pConn->bufev);
			evbuffer_add(output, buf, iBufSize);
			printf("LOGIN_SC_IN has send!\n");
		}
	}
}
예제 #4
0
/**< 处���交易 */
int TransProcess::Process(POS_TRADE_TYPE tradeType,TRADEDATA *pData)
{
    trace_log(DBG,"start get DBConn");
    if(GetConn()!=0)
    {
        trace_log(ERR,"GetSQLConn fail");
        return -1;
    }
    trace_log(DBG,"get DBConn success");
    map<POS_TRADE_TYPE,TranFunc>::iterator itor;
    itor=mapTran.find(tradeType);
    if(itor==mapTran.end())
    {
        trace_log(ERR,"no the trade_type:%d",tradeType);
        return -1;
    }
    return itor->second(&dbConn,pData);

}
예제 #5
0
int OGRMySQLDataSource::DeleteLayer( int iLayer)

{
    if( iLayer < 0 || iLayer >= nLayers )
        return OGRERR_FAILURE;
        
/* -------------------------------------------------------------------- */
/*      Blow away our OGR structures related to the layer.  This is     */
/*      pretty dangerous if anything has a reference to this layer!     */
/* -------------------------------------------------------------------- */
    CPLString osLayerName = papoLayers[iLayer]->GetLayerDefn()->GetName();
    
    CPLDebug( "MYSQL", "DeleteLayer(%s)", osLayerName.c_str() );

    delete papoLayers[iLayer];
    memmove( papoLayers + iLayer, papoLayers + iLayer + 1,
             sizeof(void *) * (nLayers - iLayer - 1) );
    nLayers--;

/* -------------------------------------------------------------------- */
/*      Remove from the database.                                       */
/* -------------------------------------------------------------------- */
    CPLString osCommand;

    osCommand.Printf(
             "DROP TABLE `%s` ",
             osLayerName.c_str() );

    if( !mysql_query(GetConn(), osCommand ) )
    {
        CPLDebug("MYSQL","Dropped table %s.", osLayerName.c_str());
        return OGRERR_NONE;
    }
    else
    {
        ReportError( osCommand );
        return OGRERR_FAILURE;
    }

}
예제 #6
0
파일: kguidb.cpp 프로젝트: t0mac0/kgui
/* read tables and return array of names */
int kGUIDb::ReadTables(kGUIString **names)
{
    MYSQL_RES *res_set;
    int i,num;
    MYSQL_ROW row;

    res_set=mysql_list_tables(GetConn(), 0);
    num=(int)mysql_num_rows(res_set);
    if(!num)
        names[0]=0;
    else
    {
        names[0]=new kGUIString[num];
        /* populate table */
        for(i=0; i<num; ++i)
        {
            row=mysql_fetch_row(res_set);
            names[0][i].SetString(row[0]);
        }
    }
    mysql_free_result(res_set);
    return(num);
}
예제 #7
0
int OGRMySQLDataSource::FetchSRSId( OGRSpatialReference * poSRS )

{
    char           **papszRow;  
    MYSQL_RES       *hResult=NULL;
    
    CPLString            osCommand;
    char                *pszWKT = NULL;
    int                 nSRSId;

    if( poSRS == NULL )
        return -1;

/* -------------------------------------------------------------------- */
/*      Translate SRS to WKT.                                           */
/* -------------------------------------------------------------------- */
    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
        return -1;
    
/* -------------------------------------------------------------------- */
/*      Try to find in the existing table.                              */
/* -------------------------------------------------------------------- */
    osCommand.Printf( 
             "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
             pszWKT );

    if( !mysql_query( GetConn(), osCommand ) )
        hResult = mysql_store_result( GetConn() );

    if (!mysql_num_rows(hResult))
    {
        CPLDebug("MYSQL", "No rows exist currently exist in spatial_ref_sys");
        mysql_free_result( hResult );
        hResult = NULL;
    }
    papszRow = NULL;
    if( hResult != NULL )
        papszRow = mysql_fetch_row( hResult );
        
    if( papszRow != NULL && papszRow[0] != NULL )
    {
        nSRSId = atoi(papszRow[0]);
        if( hResult != NULL )
            mysql_free_result( hResult );
        hResult = NULL;
        CPLFree(pszWKT);
        return nSRSId;
    }

    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;

/* -------------------------------------------------------------------- */
/*      Get the current maximum srid in the srs table.                  */
/* -------------------------------------------------------------------- */
    osCommand = "SELECT MAX(srid) FROM spatial_ref_sys";
    if( !mysql_query( GetConn(), osCommand ) )
    {
        hResult = mysql_store_result( GetConn() );
        papszRow = mysql_fetch_row( hResult );
    }
        
    if( papszRow != NULL && papszRow[0] != NULL )
    {
        nSRSId = atoi(papszRow[0]) + 1;
    }
    else
        nSRSId = 1;

    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;

/* -------------------------------------------------------------------- */
/*      Try adding the SRS to the SRS table.                            */
/* -------------------------------------------------------------------- */
    osCommand.Printf(
             "INSERT INTO spatial_ref_sys (srid,srtext) VALUES (%d,'%s')",
             nSRSId, pszWKT );

    if( !mysql_query( GetConn(), osCommand ) )
        hResult = mysql_store_result( GetConn() );

    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;

    CPLFree(pszWKT);

    return nSRSId;
}
예제 #8
0
OGRSpatialReference *OGRMySQLDataSource::FetchSRS( int nId )
{
    char         szCommand[128];
    char           **papszRow;  
    MYSQL_RES       *hResult;
            
    if( nId < 0 )
        return NULL;

/* -------------------------------------------------------------------- */
/*      First, we look through our SRID cache, is it there?             */
/* -------------------------------------------------------------------- */
    int  i;

    for( i = 0; i < nKnownSRID; i++ )
    {
        if( panSRID[i] == nId )
            return papoSRS[i];
    }

    OGRSpatialReference *poSRS = NULL;
 
    // make sure to attempt to free any old results
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;   
                        
    sprintf( szCommand,
         "SELECT srtext FROM spatial_ref_sys WHERE srid = %d",
         nId );
    
    if( !mysql_query( GetConn(), szCommand ) )
        hResult = mysql_store_result( GetConn() );
        
    char  *pszWKT = NULL;
    papszRow = NULL;
    

    if( hResult != NULL )
        papszRow = mysql_fetch_row( hResult );

    if( papszRow != NULL && papszRow[0] != NULL )
    {
        pszWKT = CPLStrdup(papszRow[0]);
    }

    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;

    poSRS = new OGRSpatialReference();
    char* pszWKTOri = pszWKT;
    if( pszWKT == NULL || poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE )
    {
        delete poSRS;
        poSRS = NULL;
    }

    CPLFree(pszWKTOri);

/* -------------------------------------------------------------------- */
/*      Add to the cache.                                               */
/* -------------------------------------------------------------------- */
    panSRID = (int *) CPLRealloc(panSRID,sizeof(int) * (nKnownSRID+1) );
    papoSRS = (OGRSpatialReference **) 
        CPLRealloc(papoSRS, sizeof(void*) * (nKnownSRID + 1) );
    panSRID[nKnownSRID] = nId;
    papoSRS[nKnownSRID] = poSRS;
    nKnownSRID ++;

    return poSRS;
}
예제 #9
0
OGRErr OGRMySQLDataSource::InitializeMetadataTables()

{
    const char*      pszCommand;
    MYSQL_RES       *hResult;
    OGRErr	    eErr = OGRERR_NONE;
 
    pszCommand = "DESCRIBE geometry_columns";
    if( mysql_query(GetConn(), pszCommand ) )
    {
        pszCommand =
                "CREATE TABLE geometry_columns "
                "( F_TABLE_CATALOG VARCHAR(256), "
                "F_TABLE_SCHEMA VARCHAR(256), "
                "F_TABLE_NAME VARCHAR(256) NOT NULL," 
                "F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL, "
                "COORD_DIMENSION INT, "
                "SRID INT,"
                "TYPE VARCHAR(256) NOT NULL)";
        if( mysql_query(GetConn(), pszCommand ) )
        {
            ReportError( pszCommand );
            eErr = OGRERR_FAILURE;
        }
        else
            CPLDebug("MYSQL","Creating geometry_columns metadata table");
 
    }
 
    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
    {
        mysql_free_result( hResult );
        hResult = NULL;   
    }
 
    pszCommand = "DESCRIBE spatial_ref_sys";
    if( mysql_query(GetConn(), pszCommand ) )
    {
        pszCommand =
                "CREATE TABLE spatial_ref_sys "
                "(SRID INT NOT NULL, "
                "AUTH_NAME VARCHAR(256), "
                "AUTH_SRID INT, "
                "SRTEXT VARCHAR(2048))";
        if( mysql_query(GetConn(), pszCommand ) )
        {
            ReportError( pszCommand );
            eErr = OGRERR_FAILURE;
        }
        else
            CPLDebug("MYSQL","Creating spatial_ref_sys metadata table");
 
    }    
 
    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
    {
        mysql_free_result( hResult );
        hResult = NULL;
    }
 
    return eErr;
}
예제 #10
0
파일: webpgfetch.cpp 프로젝트: Zala/qminer
PUrl TWebPgFetch::GetConnUrl(const int& ConnFId) const {
  TWebPgFetchEvent* Event = (TWebPgFetchEvent*)GetConn(ConnFId)();
  return Event->GetUrl();
}
예제 #11
0
/**< 业务处理 */
int Dataprocess::Process(int nForwardKey)
{
    try
    {
        int     nReturn = 0;
        string  sMapKey;
        char    cTypeCode[3] = {0};
        map<string,S_TYPEFUNC>::iterator itor;
                                                                    // 获取数据连接
        trace_log(NML,"start get DBConn");
        if(GetConn() != 0)
        {
            trace_log(ERR,"GetSQLConn fail");
            pMsg8583recv->SetValueByStr(39,"ZZ");//交易失败
            pMsg8583recv->SetValueByStr(56,"GetSQLConn fail");      //交易失败
            return -1;
        }
        trace_log(NML,"get DBConn success");

        // 匹配交易类型
        sMapKey.append((char *)myData.cMsgType);
        if(pMsg8583recv->FieldExist(3))
        {
            sMapKey.append((char*)myData.cTransProcess,2);
        }

        if( pMsg8583recv->FieldExist(60) )
        {
            memcpy(cTypeCode,myData.cRerervedPri60,2);
            sMapKey.append(cTypeCode);
        }

        itor = mapTran.find(sMapKey);
        if(itor == mapTran.end())
        {
            trace_log(ERR, "msgtype[%s] or sMapkey[%s] error", myData.cMsgType, sMapKey.c_str());
            pMsg8583recv->SetValueByStr(39,"ZZ");                                                   //交易失败
            pMsg8583recv->SetValueByStr(56,"无此交易类型");//PosCenter No the Trade
            return -1;
        }

        // 处理交易类型 
        trace_log(NML, "sMapKey is  %s---value:%d", sMapKey.c_str(), itor->second.tradeType);
        myData.tradeType                =   itor->second.tradeType;
        trasData.HeadData.RTradeType    =   (char)myData.tradeType;
        myData.nForwardKey              =   nForwardKey;
        trace_log(NML, "run to second.func!");
        nReturn = itor->second.func(myData, pMsg8583recv, trasData, &dbConn);
		
        char cResp[4]={0};
        if( POS_UNCONSUME == myData.tradeType	&&	pMsg8583recv->FieldExist(39) )
        {
            pMsg8583recv->GetValueByStr(39,cResp,sizeof(cResp));
            StoreForward* pStore = StoreForward::GetInstancePtr();
            if( 0 >= myData.nForwardKey	&&	0 == memcmp(cResp,"00",2) )			// 消费冲正非存储转发 
            {
                pStore->PutStore(ucRecBuffer, nRecDataLen);
            }
			else if( 1 <= myData.nForwardKey	&&	0 == memcmp(cResp,"00",2) )
            {																	// 消费冲正存储转发
                pStore->RemoveStore(myData.nForwardKey);
            }
        }
	
        return nReturn;
    }
	catch(...)
	{
        trace_log(SYS, "Error in Dataprocess::Process");
        pMsg8583recv->SetValueByStr(39,"ZZ");//交易失败
        pMsg8583recv->SetValueByStr(56,"System Exception:Process err");
        return -1;
    }
}
예제 #12
0
int OGRMySQLDataSource::FetchSRSId( OGRSpatialReference * poSRS )

{
    if( poSRS == nullptr )
        return GetUnknownSRID();

    OGRSpatialReference oSRS(*poSRS);
    // cppcheck-suppress uselessAssignmentPtrArg
    poSRS = nullptr;

    const char* pszAuthorityName = oSRS.GetAuthorityName(nullptr);

    if( pszAuthorityName == nullptr || strlen(pszAuthorityName) == 0 )
    {
/* -------------------------------------------------------------------- */
/*      Try to identify an EPSG code                                    */
/* -------------------------------------------------------------------- */
        oSRS.AutoIdentifyEPSG();

        pszAuthorityName = oSRS.GetAuthorityName(nullptr);
        if (pszAuthorityName != nullptr && EQUAL(pszAuthorityName, "EPSG"))
        {
            const char* pszAuthorityCode = oSRS.GetAuthorityCode(nullptr);
            if ( pszAuthorityCode != nullptr && strlen(pszAuthorityCode) > 0 )
            {
                /* Import 'clean' SRS */
                oSRS.importFromEPSG( atoi(pszAuthorityCode) );

                pszAuthorityName = oSRS.GetAuthorityName(nullptr);
            }
        }
    }
/* -------------------------------------------------------------------- */
/*      Check whether the authority name/code is already mapped to a    */
/*      SRS ID.                                                         */
/* -------------------------------------------------------------------- */
    CPLString osCommand;
    int nAuthorityCode = 0;
    if( pszAuthorityName != nullptr )
    {
        /* Check that the authority code is integral */
        nAuthorityCode = atoi( oSRS.GetAuthorityCode(nullptr) );
        if( nAuthorityCode > 0 )
        {
            if( GetMajorVersion() < 8 || IsMariaDB() )
            {
                osCommand.Printf(
                        "SELECT srid FROM spatial_ref_sys WHERE "
                        "auth_name = '%s' AND auth_srid = %d",
                        pszAuthorityName,
                        nAuthorityCode );
            }
            else
            {
                osCommand.Printf(
                        "SELECT SRS_ID FROM INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS "
                        "WHERE ORGANIZATION = '%s' AND ORGANIZATION_COORDSYS_ID = %d",
                        pszAuthorityName,
                        nAuthorityCode );
            }

            MYSQL_RES *hResult = nullptr;
            if( !mysql_query( GetConn(), osCommand ) )
                hResult = mysql_store_result( GetConn() );

            if ( hResult != nullptr && !mysql_num_rows(hResult))
            {
                CPLDebug("MYSQL", "No rows exist currently exist in spatial_ref_sys");
                mysql_free_result( hResult );
                hResult = nullptr;
            }
            char **papszRow = nullptr;
            if( hResult != nullptr )
                papszRow = mysql_fetch_row( hResult );

            if( papszRow != nullptr && papszRow[0] != nullptr )
            {
                const int nSRSId = atoi(papszRow[0]);
                if( hResult != nullptr )
                    mysql_free_result( hResult );
                hResult = nullptr;
                return nSRSId;
            }

            // make sure to attempt to free results of successful queries
            hResult = mysql_store_result( GetConn() );
            if( hResult != nullptr )
                mysql_free_result( hResult );
        }
    }

/* -------------------------------------------------------------------- */
/*      Translate SRS to WKT.                                           */
/* -------------------------------------------------------------------- */
    char *pszWKT = nullptr;
    if( oSRS.exportToWkt( &pszWKT ) != OGRERR_NONE )
        return GetUnknownSRID();

/* -------------------------------------------------------------------- */
/*      Try to find in the existing record.                             */
/* -------------------------------------------------------------------- */
    if( GetMajorVersion() < 8 || IsMariaDB() )
    {
        osCommand.Printf(
                "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
                pszWKT );
    }
    else
    {
        osCommand.Printf(
                "SELECT SRS_ID FROM INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS WHERE DEFINITION = '%s'",
                pszWKT );
    }

    MYSQL_RES *hResult = nullptr;
    if( !mysql_query( GetConn(), osCommand ) )
        hResult = mysql_store_result( GetConn() );

    if ( hResult != nullptr && !mysql_num_rows(hResult))
    {
        CPLDebug("MYSQL", "No rows exist currently exist in spatial_ref_sys");
        mysql_free_result( hResult );
        hResult = nullptr;
    }
    char **papszRow = nullptr;
    if( hResult != nullptr )
        papszRow = mysql_fetch_row( hResult );

    if( papszRow != nullptr && papszRow[0] != nullptr )
    {
        const int nSRSId = atoi(papszRow[0]);
        if( hResult != nullptr )
            mysql_free_result( hResult );
        hResult = nullptr;
        CPLFree(pszWKT);
        return nSRSId;
    }

    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != nullptr )
        mysql_free_result( hResult );
    hResult = nullptr;

    // TODO: try to insert in INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS
    if( GetMajorVersion() >= 8 && !IsMariaDB() )
    {
        CPLFree(pszWKT);
        return GetUnknownSRID();
    }

/* -------------------------------------------------------------------- */
/*      Get the current maximum srid in the srs table.                  */
/* -------------------------------------------------------------------- */
    osCommand = "SELECT MAX(srid) FROM spatial_ref_sys";
    if( !mysql_query( GetConn(), osCommand ) )
    {
        hResult = mysql_store_result( GetConn() );
        papszRow = mysql_fetch_row( hResult );
    }

    const int nSRSId = papszRow != nullptr && papszRow[0] != nullptr
        ? atoi(papszRow[0]) + 1
        : 1;

    if( hResult != nullptr )
        mysql_free_result( hResult );
    hResult = nullptr;

/* -------------------------------------------------------------------- */
/*      Try adding the SRS to the SRS table.                            */
/* -------------------------------------------------------------------- */
    osCommand.Printf(
             "INSERT INTO spatial_ref_sys (srid,srtext) VALUES (%d,'%s')",
             nSRSId, pszWKT );

    if( !mysql_query( GetConn(), osCommand ) )
        /*hResult = */ mysql_store_result( GetConn() ); /* FIXME ? */

    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != nullptr )
        mysql_free_result( hResult );
    hResult = nullptr;

    CPLFree(pszWKT);

    return nSRSId;
}
예제 #13
0
OGRSpatialReference *OGRIngresDataSource::FetchSRS( int nId )
{
    char         szCommand[1024];
    char           **papszRow;
    OGRIngresStatement oStatement(GetConn());
            
    if( nId < 0 )
        return NULL;

    /*
     * Only the new Ingres Geospatial library
     */
    if(IsNewIngres() == FALSE)
        return NULL;

/* -------------------------------------------------------------------- */
/*      First, we look through our SRID cache, is it there?             */
/* -------------------------------------------------------------------- */
    int  i;

    for( i = 0; i < nKnownSRID; i++ )
    {
        if( panSRID[i] == nId )
            return papoSRS[i];
    }

    OGRSpatialReference *poSRS = NULL;

    sprintf( szCommand,
         "SELECT srtext FROM spatial_ref_sys WHERE srid = %d",
         nId );

    oStatement.ExecuteSQL(szCommand);
        
    char    *pszWKT = NULL;
    papszRow = NULL;
    

    papszRow = oStatement.GetRow();

    if( papszRow != NULL)
    {
        if(papszRow[0] != NULL )
        {
            //VARCHAR uses the first two bytes for length
            pszWKT = &papszRow[0][2];
        }
    }

     poSRS = new OGRSpatialReference();
     if( pszWKT == NULL || poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE )
     {
         delete poSRS;
         poSRS = NULL;
     }

/* -------------------------------------------------------------------- */
/*      Add to the cache.                                               */
/* -------------------------------------------------------------------- */
    panSRID = (int *) CPLRealloc(panSRID,sizeof(int) * (nKnownSRID+1) );
    papoSRS = (OGRSpatialReference **) 
        CPLRealloc(papoSRS, sizeof(void*) * (nKnownSRID + 1) );
    panSRID[nKnownSRID] = nId;
    papoSRS[nKnownSRID] = poSRS;

    return poSRS;
}
예제 #14
0
OGRErr OGRIngresDataSource::InitializeMetadataTables()

{
#ifdef notdef
    char            szCommand[1024];
    INGRES_RES       *hResult;
    OGRErr	    eErr = OGRERR_NONE;
 
    sprintf( szCommand, "DESCRIBE geometry_columns" );
    if( ingres_query(GetConn(), szCommand ) )
    {
        sprintf(szCommand,
                "CREATE TABLE geometry_columns "
                "( F_TABLE_CATALOG VARCHAR(256), "
                "F_TABLE_SCHEMA VARCHAR(256), "
                "F_TABLE_NAME VARCHAR(256) NOT NULL," 
                "F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL, "
                "COORD_DIMENSION INT, "
                "SRID INT,"
                "TYPE VARCHAR(256) NOT NULL)");
        if( ingres_query(GetConn(), szCommand ) )
        {
            ReportError( szCommand );
            eErr = OGRERR_FAILURE;
        }
        else
            CPLDebug("INGRES","Creating geometry_columns metadata table");
 
    }
 
    // make sure to attempt to free results of successful queries
    hResult = ingres_store_result( GetConn() );
    if( hResult != NULL )
    {
        ingres_free_result( hResult );
        hResult = NULL;   
    }
 
    sprintf( szCommand, "DESCRIBE spatial_ref_sys" );
    if( ingres_query(GetConn(), szCommand ) )
    {
        sprintf(szCommand,
                "CREATE TABLE spatial_ref_sys "
                "(SRID INT NOT NULL, "
                "AUTH_NAME VARCHAR(256), "
                "AUTH_SRID INT, "
                "SRTEXT VARCHAR(2048))");
        if( ingres_query(GetConn(), szCommand ) )
        {
            ReportError( szCommand );
            eErr = OGRERR_FAILURE;
        }
        else
            CPLDebug("INGRES","Creating spatial_ref_sys metadata table");
 
    }    
 
    // make sure to attempt to free results of successful queries
    hResult = ingres_store_result( GetConn() );
    if( hResult != NULL )
    {
        ingres_free_result( hResult );
        hResult = NULL;
    }
 
    return eErr;
#endif
    return OGRERR_NONE;
}
예제 #15
0
int OGRIngresDataSource::FetchSRSId( OGRSpatialReference * poSRS )

{
    char            **papszRow;
    char            szCommand[10000];
    char            *pszWKT = NULL;
    char            *pszProj4 = NULL;
    const char      *pszAuthName;
    const char      *pszAuthID;
    int             nSRSId;

    if( poSRS == NULL )
        return -1;

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to WKT.                                           */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
        return -1;

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to Proj4.                                         */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToProj4( &pszProj4 ) != OGRERR_NONE )
        return -1;

    CPLAssert( strlen(pszProj4) + strlen(pszWKT) < sizeof(szCommand) - 500 );

/* -------------------------------------------------------------------- */
/*      Try to find in the existing table.                              */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
             pszWKT );

    {
        OGRIngresStatement  oStateSRID(GetConn());
        oStateSRID.ExecuteSQL(szCommand);

        papszRow = oStateSRID.GetRow();
        if (papszRow == NULL)
        {
            CPLDebug("INGRES", "No rows exist currently exist in spatial_ref_sys");
        }
        else if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]);
            return nSRSId;
        }
    }

/* -------------------------------------------------------------------- */
/*      Get the current maximum srid in the srs table.                  */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT MAX(srid) FROM spatial_ref_sys");    

    {
        OGRIngresStatement  oStateMaxSRID(GetConn());
        oStateMaxSRID.ExecuteSQL(szCommand);
        papszRow = oStateMaxSRID.GetRow();

        if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]) + 1;
        }
        else
            nSRSId = 1;

    }
    
    pszAuthName = poSRS->GetAuthorityName(NULL);
    pszAuthID = poSRS->GetAuthorityCode(NULL);

    if(pszAuthName == NULL || strlen(pszAuthName) == 0)
    {
        poSRS->AutoIdentifyEPSG();
        pszAuthName = poSRS->GetAuthorityName(NULL);
        if (pszAuthName != NULL && EQUAL(pszAuthName, "EPSG"))
        {
            const char* pszAuthorityCode = poSRS->GetAuthorityCode(NULL);
            if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
            {
                /* Import 'clean' SRS */
                poSRS->importFromEPSG( atoi(pszAuthorityCode) );

                pszAuthName = poSRS->GetAuthorityName(NULL);
                pszAuthID = poSRS->GetAuthorityCode(NULL);
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      Try adding the SRS to the SRS table.                            */
/* -------------------------------------------------------------------- */
    if(pszAuthName != NULL)
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,'%s',%s,'%s','%s')",
                 nSRSId, pszAuthName, pszAuthID, pszWKT, pszProj4 );
    }
    else
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,NULL,NULL,'%s','%s')",
                 nSRSId, pszWKT, pszProj4 );
    }

    {
        OGRIngresStatement  oStateNewSRID(GetConn());
        if(!oStateNewSRID.ExecuteSQL(szCommand))
        {
            CPLDebug("INGRES", "Failed to create new spatial reference system");
        }
    }

    return nSRSId;
}
예제 #16
0
int OGRIngresDataSource::FetchSRSId( OGRSpatialReference * poSRS )

{
    char            **papszRow;
    char            szCommand[10000];
    char            *pszWKT = NULL;
    char            *pszProj4 = NULL;
    const char      *pszAuthName;
    const char      *pszAuthID;
    int             nSRSId;

    if( poSRS == NULL )
        return -1;

    /* -------------------------------------------------------------------- */
    /*  If it is a EPSG	Spatial Reference, search with special type			*/
    /* -------------------------------------------------------------------- */
    pszAuthName = poSRS->GetAuthorityName(NULL);
    pszAuthID = poSRS->GetAuthorityCode(NULL);

    if (pszAuthName && pszAuthID && EQUAL(pszAuthName, "EPSG"))
    {
         sprintf( szCommand, 
             "SELECT srid FROM spatial_ref_sys WHERE auth_name = 'EPSG' and auth_srid= %s",
             pszAuthID );


        OGRIngresStatement  oStateSRID(GetConn());
        oStateSRID.ExecuteSQL(szCommand);

        papszRow = oStateSRID.GetRow();
        if (papszRow == NULL)
        {
            CPLDebug("INGRES", "No rows exists matching EPSG:%s in spatial_ref_sys", pszAuthID );
        }
        else if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]);
            return nSRSId;
        }
    }

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to WKT.                                           */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
        return -1;

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to Proj4.                                         */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToProj4( &pszProj4 ) != OGRERR_NONE )
        return -1;

    CPLAssert( strlen(pszProj4) + strlen(pszWKT) < sizeof(szCommand) - 500 );

/* -------------------------------------------------------------------- */
/*      Try to find in the existing table.                              */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
             pszWKT );

    {
        OGRIngresStatement  oStateSRID(GetConn());
        oStateSRID.ExecuteSQL(szCommand);

        papszRow = oStateSRID.GetRow();
        if (papszRow == NULL)
        {
            CPLDebug("INGRES", "No rows exist currently exist in spatial_ref_sys");
        }
        else if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]);
            return nSRSId;
        }
    }

/* -------------------------------------------------------------------- */
/*      Get the current maximum srid in the srs table.                  */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT MAX(srid) FROM spatial_ref_sys");    

    {
        OGRIngresStatement  oStateMaxSRID(GetConn());
        oStateMaxSRID.ExecuteSQL(szCommand);
        papszRow = oStateMaxSRID.GetRow();

        // The spatial reference created by user must be greater than
        // the 10000. The below are system maintained.
#define USER_DEFINED_SR_START   10000
        if( papszRow != NULL && papszRow[0] != NULL )
        {
            // if there is no row in spatial reference, a random value 
            // will be return, how to judge?
            nSRSId = *((II_INT4 *)papszRow[0]) ;
            if (nSRSId <= 0)
            {
                nSRSId = USER_DEFINED_SR_START+1; 
            }
            else
            {
                nSRSId = *((II_INT4 *)papszRow[0]) + 1;
            }
             
        }
        else
            nSRSId = USER_DEFINED_SR_START+1;

    }  
    
    if(pszAuthName == NULL || strlen(pszAuthName) == 0)
    {
        poSRS->AutoIdentifyEPSG();
        pszAuthName = poSRS->GetAuthorityName(NULL);
        if (pszAuthName != NULL && EQUAL(pszAuthName, "EPSG"))
        {
            const char* pszAuthorityCode = poSRS->GetAuthorityCode(NULL);
            if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
            {
                /* Import 'clean' SRS */
                poSRS->importFromEPSG( atoi(pszAuthorityCode) );

                pszAuthName = poSRS->GetAuthorityName(NULL);
                pszAuthID = poSRS->GetAuthorityCode(NULL);
            }
        }
    }
/* -------------------------------------------------------------------- */
/*      Try adding the SRS to the SRS table.                            */
/* -------------------------------------------------------------------- */
    if(pszAuthName != NULL)
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,'%s',%s,'%s','%s')",
                 nSRSId, pszAuthName, pszAuthID, pszWKT, pszProj4 );
    }
    else
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,NULL,NULL,'%s','%s')",
                 nSRSId, pszWKT, pszProj4 );
    }

    {
        OGRIngresStatement  oStateNewSRID(GetConn());
        if(!oStateNewSRID.ExecuteSQL(szCommand))
        {
            CPLDebug("INGRES", "Failed to create new spatial reference system");
        }
    }

    return nSRSId;
}
예제 #17
0
OGRLayer *
OGRMySQLDataSource::CreateLayer( const char * pszLayerNameIn,
                              OGRSpatialReference *poSRS,
                              OGRwkbGeometryType eType,
                              char ** papszOptions )

{
    MYSQL_RES           *hResult=NULL;
    CPLString            osCommand;
    const char          *pszGeometryType;
    const char			*pszGeomColumnName;
    const char 			*pszExpectedFIDName; 
	
    char                *pszLayerName;
    int                 nDimension = 3; // MySQL only supports 2d currently


/* -------------------------------------------------------------------- */
/*      Make sure there isn't an active transaction already.            */
/* -------------------------------------------------------------------- */
    InterruptLongResult();


    if( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) )
        pszLayerName = LaunderName( pszLayerNameIn );
    else
        pszLayerName = CPLStrdup( pszLayerNameIn );

    if( wkbFlatten(eType) == eType )
        nDimension = 2;

    CPLDebug("MYSQL","Creating layer %s.", pszLayerName);

/* -------------------------------------------------------------------- */
/*      Do we already have this layer?  If so, should we blow it        */
/*      away?                                                           */
/* -------------------------------------------------------------------- */

    int iLayer;
    for( iLayer = 0; iLayer < nLayers; iLayer++ )
    {
        if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) )
        {
			
            if( CSLFetchNameValue( papszOptions, "OVERWRITE" ) != NULL
                && !EQUAL(CSLFetchNameValue(papszOptions,"OVERWRITE"),"NO") )
            {
                DeleteLayer( iLayer );
            }
            else
            {
                CPLError( CE_Failure, CPLE_AppDefined,
                          "Layer %s already exists, CreateLayer failed.\n"
                          "Use the layer creation option OVERWRITE=YES to "
                          "replace it.",
                          pszLayerName );
                CPLFree( pszLayerName );
                return NULL;
            }
        }
    }

    pszGeomColumnName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME" );
    if (!pszGeomColumnName)
        pszGeomColumnName="SHAPE";

    pszExpectedFIDName = CSLFetchNameValue( papszOptions, "MYSQL_FID" );
    if (!pszExpectedFIDName)
        pszExpectedFIDName="OGR_FID";


    CPLDebug("MYSQL","Geometry Column Name %s.", pszGeomColumnName);
    CPLDebug("MYSQL","FID Column Name %s.", pszExpectedFIDName);

    if( wkbFlatten(eType) == wkbNone )
    {
        osCommand.Printf(
                 "CREATE TABLE `%s` ( "
                 "   %s INT UNIQUE NOT NULL AUTO_INCREMENT )",
                 pszLayerName, pszExpectedFIDName );
    }
    else
    {
        osCommand.Printf(
                 "CREATE TABLE `%s` ( "
                 "   %s INT UNIQUE NOT NULL AUTO_INCREMENT, "
                 "   %s GEOMETRY NOT NULL )",
                 pszLayerName, pszExpectedFIDName, pszGeomColumnName );
    }

    if( CSLFetchNameValue( papszOptions, "ENGINE" ) != NULL )
    {
        osCommand += " ENGINE = ";
        osCommand += CSLFetchNameValue( papszOptions, "ENGINE" );
    }
	
    if( !mysql_query(GetConn(), osCommand ) )
    {
        if( mysql_field_count( GetConn() ) == 0 )
            CPLDebug("MYSQL","Created table %s.", pszLayerName);
        else
        {
            ReportError( osCommand );
            return NULL;
        }
    }
    else
    {
        ReportError( osCommand );
        return NULL;
    }

    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;
    
    // Calling this does no harm
    InitializeMetadataTables();
    
/* -------------------------------------------------------------------- */
/*      Try to get the SRS Id of this spatial reference system,         */
/*      adding tot the srs table if needed.                             */
/* -------------------------------------------------------------------- */
    int nSRSId = -1;

    if( poSRS != NULL )
        nSRSId = FetchSRSId( poSRS );

/* -------------------------------------------------------------------- */
/*      Sometimes there is an old crufty entry in the geometry_columns  */
/*      table if things were not properly cleaned up before.  We make   */
/*      an effort to clean out such cruft.                              */
/*                                                                      */
/* -------------------------------------------------------------------- */
    osCommand.Printf(
             "DELETE FROM geometry_columns WHERE f_table_name = '%s'",
             pszLayerName );

    if( mysql_query(GetConn(), osCommand ) )
    {
        ReportError( osCommand );
        return NULL;
    }

    // make sure to attempt to free results of successful queries
    hResult = mysql_store_result( GetConn() );
    if( hResult != NULL )
        mysql_free_result( hResult );
    hResult = NULL;   
        
/* -------------------------------------------------------------------- */
/*      Attempt to add this table to the geometry_columns table, if     */
/*      it is a spatial layer.                                          */
/* -------------------------------------------------------------------- */
    if( eType != wkbNone )
    {
        int nCoordDimension;
        if( eType == wkbFlatten(eType) )
            nCoordDimension = 2;
        else
            nCoordDimension = 3;

        pszGeometryType = OGRToOGCGeomType(eType);

        if( nSRSId == -1 )
            osCommand.Printf(
                     "INSERT INTO geometry_columns "
                     " (F_TABLE_NAME, "
                     "  F_GEOMETRY_COLUMN, "
                     "  COORD_DIMENSION, "
                     "  TYPE) values "
                     "  ('%s', '%s', %d, '%s')",
                     pszLayerName,
                     pszGeomColumnName,
                     nCoordDimension,
                     pszGeometryType );
        else
            osCommand.Printf(
                     "INSERT INTO geometry_columns "
                     " (F_TABLE_NAME, "
                     "  F_GEOMETRY_COLUMN, "
                     "  COORD_DIMENSION, "
                     "  SRID, "
                     "  TYPE) values "
                     "  ('%s', '%s', %d, %d, '%s')",
                     pszLayerName,
                     pszGeomColumnName,
                     nCoordDimension,
                     nSRSId,
                     pszGeometryType );

        if( mysql_query(GetConn(), osCommand ) )
        {
            ReportError( osCommand );
            return NULL;
        }

        // make sure to attempt to free results of successful queries
        hResult = mysql_store_result( GetConn() );
        if( hResult != NULL )
            mysql_free_result( hResult );
        hResult = NULL;   
    }

/* -------------------------------------------------------------------- */
/*      Create the spatial index.                                       */
/*                                                                      */
/*      We're doing this before we add geometry and record to the table */
/*      so this may not be exactly the best way to do it.               */
/* -------------------------------------------------------------------- */
    const char *pszSI = CSLFetchNameValue( papszOptions, "SPATIAL_INDEX" );

    if( eType != wkbNone && (pszSI == NULL || CSLTestBoolean(pszSI)) )
    {
        osCommand.Printf(
                 "ALTER TABLE `%s` ADD SPATIAL INDEX(`%s`) ",
                 pszLayerName,
                 pszGeomColumnName);

        if( mysql_query(GetConn(), osCommand ) )
        {
            ReportError( osCommand );
            return NULL;
        }

        // make sure to attempt to free results of successful queries
        hResult = mysql_store_result( GetConn() );
        if( hResult != NULL )
            mysql_free_result( hResult );
        hResult = NULL;   
    }
        
/* -------------------------------------------------------------------- */
/*      Create the layer object.                                        */
/* -------------------------------------------------------------------- */
    OGRMySQLTableLayer     *poLayer;
    OGRErr                  eErr;

    poLayer = new OGRMySQLTableLayer( this, pszLayerName, TRUE, nSRSId );
    eErr = poLayer->Initialize(pszLayerName);
    if (eErr == OGRERR_FAILURE)
        return NULL;

    poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) );
    poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));

/* -------------------------------------------------------------------- */
/*      Add layer to data source layer list.                            */
/* -------------------------------------------------------------------- */
    papoLayers = (OGRMySQLLayer **)
        CPLRealloc( papoLayers,  sizeof(OGRMySQLLayer *) * (nLayers+1) );

    papoLayers[nLayers++] = poLayer;

    CPLFree( pszLayerName );

    return poLayer;
}
예제 #18
0
OGRSpatialReference *OGRMySQLDataSource::FetchSRS( int nId )
{
    if( nId < 0 )
        return nullptr;

/* -------------------------------------------------------------------- */
/*      First, we look through our SRID cache, is it there?             */
/* -------------------------------------------------------------------- */
    for( int i = 0; i < nKnownSRID; i++ )
    {
        if( panSRID[i] == nId )
            return papoSRS[i];
    }

    OGRSpatialReference *poSRS = nullptr;

    // make sure to attempt to free any old results
    MYSQL_RES *hResult = mysql_store_result( GetConn() );
    if( hResult != nullptr )
        mysql_free_result( hResult );
    hResult = nullptr;

    char szCommand[128] = {};
    if( GetMajorVersion() < 8 || IsMariaDB() )
    {
        snprintf( szCommand, sizeof(szCommand),
                "SELECT srtext FROM spatial_ref_sys WHERE srid = %d",
                nId );
    }
    else
    {
        snprintf( szCommand, sizeof(szCommand),
                "SELECT DEFINITION FROM INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS WHERE SRS_ID = %d",
                nId );
    }

    if( !mysql_query( GetConn(), szCommand ) )
        hResult = mysql_store_result( GetConn() );

    char  *pszWKT = nullptr;
    char **papszRow = nullptr;

    if( hResult != nullptr )
        papszRow = mysql_fetch_row( hResult );

    if( papszRow != nullptr && papszRow[0] != nullptr )
    {
        pszWKT = CPLStrdup(papszRow[0]);
    }

    if( hResult != nullptr )
        mysql_free_result( hResult );
    hResult = nullptr;

    poSRS = new OGRSpatialReference();
    poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
    if( pszWKT == nullptr || poSRS->importFromWkt( pszWKT ) != OGRERR_NONE )
    {
        delete poSRS;
        poSRS = nullptr;
    }

    CPLFree(pszWKT);

    if( poSRS )
    {
        // The WKT found in MySQL 8 ST_SPATIAL_REFERENCE_SYSTEMS is not
        // compatible of what GDAL understands.
        const char* pszAuthorityName = poSRS->GetAuthorityName(nullptr);
        const char* pszAuthorityCode = poSRS->GetAuthorityCode(nullptr);
        if (pszAuthorityName != nullptr && EQUAL(pszAuthorityName, "EPSG") &&
            pszAuthorityCode != nullptr && strlen(pszAuthorityCode) > 0 )
        {
            /* Import 'clean' SRS */
            poSRS->importFromEPSG( atoi(pszAuthorityCode) );
        }
    }

/* -------------------------------------------------------------------- */
/*      Add to the cache.                                               */
/* -------------------------------------------------------------------- */
    panSRID = (int *) CPLRealloc(panSRID,sizeof(int) * (nKnownSRID+1) );
    papoSRS = (OGRSpatialReference **)
        CPLRealloc(papoSRS, sizeof(void*) * (nKnownSRID + 1) );
    panSRID[nKnownSRID] = nId;
    papoSRS[nKnownSRID] = poSRS;
    nKnownSRID ++;

    return poSRS;
}