static void IIdemo_conn( char *dbname, II_PTR *connHandle ) { IIAPI_CONNPARM connParm; IIAPI_WAITPARM waitParm = { -1 }; printf( "IIdemo_conn: establishing connection\n" ); connParm.co_genParm.gp_callback = NULL; connParm.co_genParm.gp_closure = NULL; connParm.co_target = dbname; connParm.co_connHandle = NULL; connParm.co_tranHandle = NULL; connParm.co_username = NULL; connParm.co_password = NULL; connParm.co_timeout = -1; IIapi_connect( &connParm ); while( connParm.co_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); *connHandle = connParm.co_connHandle; return; }
void gcd_api_connect( GCD_PCB *pcb ) { pcb->api.name = "IIapi_connect()"; pcb->api.parm.conn.co_genParm.gp_callback = gcd_conn_cmpl; pcb->api.parm.conn.co_genParm.gp_closure = (PTR)pcb; pcb->api.parm.conn.co_type = IIAPI_CT_SQL; pcb->api.parm.conn.co_connHandle = pcb->ccb->api.conn; pcb->api.parm.conn.co_tranHandle = pcb->ccb->xact.distXID; pcb->api.parm.conn.co_target = pcb->data.conn.database; pcb->api.parm.conn.co_username = pcb->data.conn.username; pcb->api.parm.conn.co_password = pcb->data.conn.password; pcb->api.parm.conn.co_timeout = pcb->data.conn.timeout; pcb->ccb->api.conn = NULL; IIapi_connect( &pcb->api.parm.conn ); return; }
int OGRIngresDataSource::Open( const char *pszFullName, char **papszOptions, int bUpdate ) { CPLAssert( nLayers == 0 ); #define MAX_TARGET_STRING_LENGTH 512 char pszDBTarget[MAX_TARGET_STRING_LENGTH]; /* -------------------------------------------------------------------- */ /* Verify we have a dbname, this parameter is required. */ /* -------------------------------------------------------------------- */ const char *pszDBName = CSLFetchNameValue(papszOptions,"dbname"); if( pszDBName == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "No DBNAME item provided in INGRES datasource name." ); return FALSE; } /* -------------------------------------------------------------------- */ /* Do we have a table list? */ /* -------------------------------------------------------------------- */ char **papszTableNames = NULL; const char *pszTables = CSLFetchNameValue(papszOptions,"tables"); if( pszTables != NULL ) papszTableNames = CSLTokenizeStringComplex(pszTables,"/",TRUE,FALSE); /* -------------------------------------------------------------------- */ /* Add support to dynamic vnode if passed */ /* -------------------------------------------------------------------- */ const char *pszHost = CSLFetchNameValue(papszOptions,"host"); if (pszHost) { const char *pszInstance = CSLFetchNameValue(papszOptions,"instance"); if (pszInstance == NULL || strlen(pszInstance) != 2) { CPLError( CE_Failure, CPLE_OpenFailed, "instance name must be specified with host." ); return FALSE; } /* ** make sure the user name and password are passed too, ** note it could not be zero length. */ const char *pszUsername = CSLFetchNameValue(papszOptions,"username"); const char *pszPassword = CSLFetchNameValue(papszOptions,"password"); if (pszUsername == NULL || strlen(pszUsername) == 0) { CPLError( CE_Failure, CPLE_OpenFailed, "user name must be specified in dynamic vnode." ); return FALSE; } if (pszPassword == NULL || strlen(pszPassword) == 0) { CPLError( CE_Failure, CPLE_OpenFailed, "password must be specified in dynamic vnode." ); return FALSE; } /* ** construct the vnode string, like : ** @host,protocol,port[;attribute=value{;attribute=value}][[user,password]], ** visit for detail ** http://docs.actian.com/ingres/10.0/command-reference-guide/1207-dynamic-vnode-specificationconnect-to-remote-node */ sprintf(pszDBTarget, "@%s,%s,%s;%s[%s,%s]::%s ", pszHost, /* host, compute name or IP address */ "TCP_IP", /* protocal, default with TCP/IP */ pszInstance, /* instance Name */ "" , /* option, Null */ pszUsername, /* user name, could not be empty */ pszPassword, /* pwd */ pszDBName /* database name */ ); CPLDebug("INGRES", pszDBTarget); } else { /* Remain the database name */ strcpy(pszDBTarget, pszDBName); } /* -------------------------------------------------------------------- */ /* Initialize the Ingres API. Should we only do this once per */ /* program run? Really we should also try to terminate the api */ /* on program exit. */ /* -------------------------------------------------------------------- */ IIAPI_INITPARM initParm; initParm.in_version = IIAPI_VERSION_1; initParm.in_timeout = -1; IIapi_initialize( &initParm ); /* -------------------------------------------------------------------- */ /* check effective user and db password */ /* -------------------------------------------------------------------- */ hConn = NULL; const char *pszEffuser = CSLFetchNameValue(papszOptions,"effuser"); const char *pszDBpwd = CSLFetchNameValue(papszOptions,"dbpwd"); if ( pszEffuser && strlen(pszEffuser) > 0 && pszDBpwd && strlen(pszDBpwd) > 0 ) { if (SetConnParam(&hConn, IIAPI_CP_EFFECTIVE_USER, (II_PTR)pszEffuser) != IIAPI_ST_SUCCESS || SetConnParam(&hConn, IIAPI_CP_DBMS_PASSWORD, (II_PTR)pszDBpwd) != IIAPI_ST_SUCCESS ) { return FALSE; } } /* -------------------------------------------------------------------- */ /* Try to connect to the database. */ /* -------------------------------------------------------------------- */ IIAPI_CONNPARM connParm; IIAPI_WAITPARM waitParm = { -1 }; memset( &connParm, 0, sizeof(connParm) ); connParm.co_genParm.gp_callback = NULL; connParm.co_genParm.gp_closure = NULL; connParm.co_target = (II_CHAR *) pszDBTarget; connParm.co_connHandle = hConn; connParm.co_tranHandle = NULL; connParm.co_username = (II_CHAR*) CSLFetchNameValue(papszOptions,"username"); connParm.co_password = (II_CHAR*)CSLFetchNameValue(papszOptions,"password"); connParm.co_timeout = -1; if( CSLFetchNameValue(papszOptions,"timeout") != NULL ) connParm.co_timeout = atoi(CSLFetchNameValue(papszOptions,"timeout")); IIapi_connect( &connParm ); while( connParm.co_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); hConn = connParm.co_connHandle; if( connParm.co_genParm.gp_status != IIAPI_ST_SUCCESS || hConn == NULL ) { OGRIngresStatement::ReportError( &(connParm.co_genParm), "Failed to connect to Ingres database." ); return FALSE; } pszName = CPLStrdup( pszFullName ); bDSUpdate = bUpdate; // Check for new or old Ingres spatial library { OGRIngresStatement oStmt( hConn ); if( oStmt.ExecuteSQL("SELECT COUNT(*) FROM iicolumns WHERE table_name = 'iiattribute' AND column_name = 'attgeomtype'" ) ) { char **papszFields; while( (papszFields = oStmt.GetRow()) ) { CPLString osCount = papszFields[0]; if( osCount[0] == '0' ) { bNewIngres = FALSE; } else { bNewIngres = TRUE; } } } } /* -------------------------------------------------------------------- */ /* Get a list of available tables. */ /* -------------------------------------------------------------------- */ if( papszTableNames == NULL ) { OGRIngresStatement oStmt( hConn ); if( oStmt.ExecuteSQL( "select table_name from iitables where system_use = 'U' and table_name not like 'iietab_%'" ) ) { char **papszFields; while( (papszFields = oStmt.GetRow()) ) { CPLString osTableName = papszFields[0]; osTableName.Trim(); papszTableNames = CSLAddString( papszTableNames, osTableName ); } } } /* -------------------------------------------------------------------- */ /* Get the schema of the available tables. */ /* -------------------------------------------------------------------- */ int iRecord; for( iRecord = 0; papszTableNames != NULL && papszTableNames[iRecord] != NULL; iRecord++ ) { OpenTable( papszTableNames[iRecord], bUpdate ); } CSLDestroy( papszTableNames ); return TRUE; }
int OGRIngresDataSource::Open( const char *pszFullName, char **papszOptions, int bUpdate ) { CPLAssert( nLayers == 0 ); /* -------------------------------------------------------------------- */ /* Verify we have a dbname, this parameter is required. */ /* -------------------------------------------------------------------- */ const char *pszDBName = CSLFetchNameValue(papszOptions,"dbname"); if( pszDBName == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "No DBNAME item provided in INGRES datasource name." ); return FALSE; } /* -------------------------------------------------------------------- */ /* Do we have a table list? */ /* -------------------------------------------------------------------- */ char **papszTableNames = NULL; const char *pszTables = CSLFetchNameValue(papszOptions,"tables"); if( pszTables != NULL ) papszTableNames = CSLTokenizeStringComplex(pszTables,"/",TRUE,FALSE); /* -------------------------------------------------------------------- */ /* Initialize the Ingres API. Should we only do this once per */ /* program run? Really we should also try to terminate the api */ /* on program exit. */ /* -------------------------------------------------------------------- */ IIAPI_INITPARM initParm; initParm.in_version = IIAPI_VERSION_1; initParm.in_timeout = -1; IIapi_initialize( &initParm ); /* -------------------------------------------------------------------- */ /* Try to connect to the database. */ /* -------------------------------------------------------------------- */ IIAPI_CONNPARM connParm; IIAPI_WAITPARM waitParm = { -1 }; memset( &connParm, 0, sizeof(connParm) ); connParm.co_genParm.gp_callback = NULL; connParm.co_genParm.gp_closure = NULL; connParm.co_target = (II_CHAR *) pszDBName; connParm.co_connHandle = NULL; connParm.co_tranHandle = NULL; connParm.co_username = (II_CHAR*) CSLFetchNameValue(papszOptions,"username"); connParm.co_password = (II_CHAR*)CSLFetchNameValue(papszOptions,"password"); connParm.co_timeout = -1; if( CSLFetchNameValue(papszOptions,"timeout") != NULL ) connParm.co_timeout = atoi(CSLFetchNameValue(papszOptions,"timeout")); IIapi_connect( &connParm ); while( connParm.co_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); hConn = connParm.co_connHandle; if( connParm.co_genParm.gp_status != IIAPI_ST_SUCCESS || hConn == NULL ) { OGRIngresStatement::ReportError( &(connParm.co_genParm), "Failed to connect to Ingres database." ); return FALSE; } pszName = CPLStrdup( pszFullName ); bDSUpdate = bUpdate; // Check for new or old Ingres spatial library { OGRIngresStatement oStmt( hConn ); if( oStmt.ExecuteSQL("SELECT COUNT(*) FROM iicolumns WHERE table_name = 'iiattribute' AND column_name = 'attgeomtype'" ) ) { char **papszFields; while( (papszFields = oStmt.GetRow()) ) { CPLString osCount = papszFields[0]; if( osCount[0] == '0' ) { bNewIngres = FALSE; } else { bNewIngres = TRUE; } } } } /* -------------------------------------------------------------------- */ /* Get a list of available tables. */ /* -------------------------------------------------------------------- */ if( papszTableNames == NULL ) { OGRIngresStatement oStmt( hConn ); if( oStmt.ExecuteSQL( "select table_name from iitables where system_use = 'U' and table_name not like 'iietab_%'" ) ) { char **papszFields; while( (papszFields = oStmt.GetRow()) ) { CPLString osTableName = papszFields[0]; osTableName.Trim(); papszTableNames = CSLAddString( papszTableNames, osTableName ); } } } /* -------------------------------------------------------------------- */ /* Get the schema of the available tables. */ /* -------------------------------------------------------------------- */ int iRecord; for( iRecord = 0; papszTableNames != NULL && papszTableNames[iRecord] != NULL; iRecord++ ) { OpenTable( papszTableNames[iRecord], bUpdate ); } CSLDestroy( papszTableNames ); return TRUE; }
int main( int argc, char** argv ) { II_PTR connHandle = (II_PTR)NULL; II_PTR tranHandle = (II_PTR)NULL; II_PTR stmtHandle = (II_PTR)NULL; II_PTR envHandle = (II_PTR)NULL; IIAPI_CONNPARM connParm; IIAPI_AUTOPARM autoparm; IIAPI_QUERYPARM queryParm; IIAPI_GETDESCRPARM getDescrParm; IIAPI_GETCOLPARM getColParm; IIAPI_GETQINFOPARM getQInfoParm; IIAPI_CLOSEPARM closeParm; IIAPI_WAITPARM waitParm = { -1 }; IIAPI_DESCRIPTOR DescrBuffer[ 5 ]; IIAPI_DATAVALUE DataBuffer[ 5 ]; char var[5][129]; short i, len; IIdemo_init(&envHandle); /* ** Connect to local Name Server */ printf( "apisname: establishing connection to Name Server\n" ); connParm.co_genParm.gp_callback = NULL; connParm.co_genParm.gp_closure = NULL; connParm.co_target = NULL; connParm.co_type = IIAPI_CT_NS; connParm.co_connHandle = envHandle; connParm.co_tranHandle = NULL; connParm.co_username = NULL; connParm.co_password = NULL; connParm.co_timeout = -1; IIapi_connect( &connParm ); while( connParm.co_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); connHandle = connParm.co_connHandle; tranHandle = connParm.co_tranHandle; /* ** Enable autocommit */ printf( "apisauto: enable autocommit\n" ); autoparm.ac_genParm.gp_callback = NULL; autoparm.ac_genParm.gp_closure = NULL; autoparm.ac_connHandle = connHandle; autoparm.ac_tranHandle = NULL; IIapi_autocommit( &autoparm ); while( autoparm.ac_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); tranHandle = autoparm.ac_tranHandle; /* ** Execute 'show' statement. */ printf( "apisname: retrieving VNODE connection info\n"); queryParm.qy_genParm.gp_callback = NULL; queryParm.qy_genParm.gp_closure = NULL; queryParm.qy_connHandle = connHandle; queryParm.qy_queryType = IIAPI_QT_QUERY; queryParm.qy_queryText = showText; queryParm.qy_parameters = FALSE; queryParm.qy_tranHandle = tranHandle; queryParm.qy_stmtHandle = NULL; IIapi_query( &queryParm ); while( queryParm.qy_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); stmtHandle = queryParm.qy_stmtHandle; /* ** Get result row descriptors. */ getDescrParm.gd_genParm.gp_callback = NULL; getDescrParm.gd_genParm.gp_closure = NULL; getDescrParm.gd_stmtHandle = stmtHandle; getDescrParm.gd_descriptorCount = 0; getDescrParm.gd_descriptor = NULL; IIapi_getDescriptor( &getDescrParm ); while( getDescrParm.gd_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); /* ** Retrieve result rows. */ getColParm.gc_genParm.gp_callback = NULL; getColParm.gc_genParm.gp_closure = NULL; getColParm.gc_rowCount = 1; getColParm.gc_columnCount = getDescrParm.gd_descriptorCount; getColParm.gc_columnData = DataBuffer; getColParm.gc_stmtHandle = stmtHandle; getColParm.gc_moreSegments = 0; for( i = 0; i < getDescrParm.gd_descriptorCount; i++ ) getColParm.gc_columnData[i].dv_value = var[i]; do { IIapi_getColumns( &getColParm ); while( getColParm.gc_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); if ( getColParm.gc_genParm.gp_status >= IIAPI_ST_NO_DATA ) break; for( i = 0; i < getDescrParm.gd_descriptorCount; i++ ) { if ( getDescrParm.gd_descriptor[i].ds_dataType == IIAPI_VCH_TYPE ) { memcpy( (char *)&len, var[i], 2 ); var[i][ len + 2 ] = '\0'; strcpy( var[i], &var[i][2] ); } else { var[i][ getColParm.gc_columnData[ i ].dv_length ] = '\0'; } } printf( "\tG/P = %s vnode = %s host = %s prot = %s addr = %s\n", var[0],var[1],var[2],var[3],var[4]); } while (1); /* ** Get query results. */ getQInfoParm.gq_genParm.gp_callback = NULL; getQInfoParm.gq_genParm.gp_closure = NULL; getQInfoParm.gq_stmtHandle = stmtHandle; IIapi_getQueryInfo( &getQInfoParm ); while( getQInfoParm.gq_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); /* ** Close query. */ closeParm.cl_genParm.gp_callback = NULL; closeParm.cl_genParm.gp_closure = NULL; closeParm.cl_stmtHandle = stmtHandle; IIapi_close( &closeParm ); while( closeParm.cl_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); /* ** Disable autocommit. */ printf( "apisname: disable autocommit\n" ); autoparm.ac_connHandle = NULL; autoparm.ac_tranHandle = tranHandle; IIapi_autocommit( &autoparm ); while( autoparm.ac_genParm.gp_completed == FALSE ) IIapi_wait( &waitParm ); IIdemo_disconn(&connHandle); IIdemo_term(&envHandle); return( 0 ); }