📄 cpl_odbc.cpp
字号:
m_panColValueLengths = NULL; }}/************************************************************************//* GetColumns() *//************************************************************************//** * Fetch column definitions for a table. * * The SQLColumn() method is used to fetch the definitions for the columns * of a table (or other queriable object such as a view). The column * definitions are digested and used to populate the CPLODBCStatement * column definitions essentially as if a "SELECT * FROM tablename" had * been done; however, no resultset will be available. * * @param pszTable the name of the table to query information on. This * should not be empty. * * @param pszCatalog the catalog to find the table in, use NULL (the * default) if no catalog is available. * * @param pszSchema the schema to find the table in, use NULL (the * default) if no schema is available. * * @return TRUE on success or FALSE on failure. */int CPLODBCStatement::GetColumns( const char *pszTable, const char *pszCatalog, const char *pszSchema ){#ifdef notdef if( pszCatalog == NULL ) pszCatalog = ""; if( pszSchema == NULL ) pszSchema = "";#endif/* -------------------------------------------------------------------- *//* Fetch columns resultset for this table. *//* -------------------------------------------------------------------- */ if( Failed( SQLColumns( m_hStmt, (SQLCHAR *) pszCatalog, SQL_NTS, (SQLCHAR *) pszSchema, SQL_NTS, (SQLCHAR *) pszTable, SQL_NTS, (SQLCHAR *) NULL /* "" */, SQL_NTS ) ) ) return FALSE;/* -------------------------------------------------------------------- *//* Allocate per column information. *//* -------------------------------------------------------------------- */#ifdef notdef // SQLRowCount() is too unreliable (with unixodbc on AIX for instance) // so we now avoid it. SQLINTEGER nResultCount=0; if( Failed(SQLRowCount( m_hStmt, &nResultCount ) ) ) nResultCount = 0; if( nResultCount < 1 ) m_nColCount = 500; // Hopefully lots. else m_nColCount = nResultCount;#endif m_nColCount = 500; m_papszColNames = (char **) CPLCalloc(sizeof(char *),(m_nColCount+1)); m_papszColValues = (char **) CPLCalloc(sizeof(char *),(m_nColCount+1)); m_panColType = (short *) CPLCalloc(sizeof(short),m_nColCount); m_papszColTypeNames = (char **) CPLCalloc(sizeof(char *),(m_nColCount+1)); m_panColSize = (_SQLULEN *) CPLCalloc(sizeof(_SQLULEN),m_nColCount); m_panColPrecision = (short *) CPLCalloc(sizeof(short),m_nColCount); m_panColNullable = (short *) CPLCalloc(sizeof(short),m_nColCount);/* -------------------------------------------------------------------- *//* Establish columns to use for key information. *//* -------------------------------------------------------------------- */ SQLUSMALLINT iCol; for( iCol = 0; iCol < m_nColCount; iCol++ ) { char szWrkData[8193]; _SQLLEN cbDataLen; if( Failed( SQLFetch( m_hStmt ) ) ) { m_nColCount = iCol; break; } szWrkData[0] = '\0'; SQLGetData( m_hStmt, SQLColumns_COLUMN_NAME, SQL_C_CHAR, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); m_papszColNames[iCol] = CPLStrdup(szWrkData); SQLGetData( m_hStmt, SQLColumns_DATA_TYPE, SQL_C_CHAR, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); m_panColType[iCol] = (short) atoi(szWrkData); SQLGetData( m_hStmt, SQLColumns_TYPE_NAME, SQL_C_CHAR, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); m_papszColTypeNames[iCol] = CPLStrdup(szWrkData); SQLGetData( m_hStmt, SQLColumns_COLUMN_SIZE, SQL_C_CHAR, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); m_panColSize[iCol] = atoi(szWrkData); SQLGetData( m_hStmt, SQLColumns_DECIMAL_DIGITS, SQL_C_CHAR, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); m_panColPrecision[iCol] = (short) atoi(szWrkData); SQLGetData( m_hStmt, SQLColumns_NULLABLE, SQL_C_CHAR, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); m_panColNullable[iCol] = atoi(szWrkData) == SQL_NULLABLE; } return TRUE;}/************************************************************************//* GetPrimaryKeys() *//************************************************************************//** * Fetch primary keys for a table. * * The SQLPrimaryKeys() function is used to fetch a list of fields * forming the primary key. The result is returned as a result set matching * the SQLPrimaryKeys() function result set. The 4th column in the result * set is the column name of the key, and if the result set contains only * one record then that single field will be the complete primary key. * * @param pszTable the name of the table to query information on. This * should not be empty. * * @param pszCatalog the catalog to find the table in, use NULL (the * default) if no catalog is available. * * @param pszSchema the schema to find the table in, use NULL (the * default) if no schema is available. * * @return TRUE on success or FALSE on failure. */int CPLODBCStatement::GetPrimaryKeys( const char *pszTable, const char *pszCatalog, const char *pszSchema ){ if( pszCatalog == NULL ) pszCatalog = ""; if( pszSchema == NULL ) pszSchema = "";/* -------------------------------------------------------------------- *//* Fetch columns resultset for this table. *//* -------------------------------------------------------------------- */ if( Failed( SQLPrimaryKeys( m_hStmt, (SQLCHAR *) pszCatalog, SQL_NTS, (SQLCHAR *) pszSchema, SQL_NTS, (SQLCHAR *) pszTable, SQL_NTS ) ) ) return FALSE; else return CollectResultsInfo();}/************************************************************************//* GetTables() *//************************************************************************//** * Fetch tables in database. * * The SQLTables() function is used to fetch a list tables in the * database. The result is returned as a result set matching * the SQLTables() function result set. The 3rd column in the result * set is the table name. Only tables of type "TABLE" are returned. * * @param pszCatalog the catalog to find the table in, use NULL (the * default) if no catalog is available. * * @param pszSchema the schema to find the table in, use NULL (the * default) if no schema is available. * * @return TRUE on success or FALSE on failure. */int CPLODBCStatement::GetTables( const char *pszCatalog, const char *pszSchema ){ CPLDebug( "ODBC", "CatalogNameL: %s\nSchema name: %s\n", pszCatalog, pszSchema );/* -------------------------------------------------------------------- *//* Fetch columns resultset for this table. *//* -------------------------------------------------------------------- */ if( Failed( SQLTables( m_hStmt, (SQLCHAR *) pszCatalog, SQL_NTS, (SQLCHAR *) pszSchema, SQL_NTS, (SQLCHAR *) NULL, SQL_NTS, (SQLCHAR *) "'TABLE','VIEW'", SQL_NTS ) ) ) return FALSE; else return CollectResultsInfo();}/************************************************************************//* DumpResult() *//************************************************************************//** * Dump resultset to file. * * The contents of the current resultset are dumped in a simply formatted * form to the provided file. If requested, the schema definition will * be written first. * * @param fp the file to write to. stdout or stderr are acceptable. * * @param bShowSchema TRUE to force writing schema information for the rowset * before the rowset data itself. Default is FALSE. */void CPLODBCStatement::DumpResult( FILE *fp, int bShowSchema ){ int iCol;/* -------------------------------------------------------------------- *//* Display schema *//* -------------------------------------------------------------------- */ if( bShowSchema ) { fprintf( fp, "Column Definitions:\n" ); for( iCol = 0; iCol < GetColCount(); iCol++ ) { fprintf( fp, " %2d: %-24s ", iCol, GetColName(iCol) ); if( GetColPrecision(iCol) > 0 && GetColPrecision(iCol) != GetColSize(iCol) ) fprintf( fp, " Size:%3d.%d", GetColSize(iCol), GetColPrecision(iCol) ); else fprintf( fp, " Size:%5d", GetColSize(iCol) ); CPLString osType = GetTypeName( GetColType(iCol) ); fprintf( fp, " Type:%s", osType.c_str() ); if( GetColNullable(iCol) ) fprintf( fp, " NULLABLE" ); fprintf( fp, "\n" ); } fprintf( fp, "\n" ); }/* -------------------------------------------------------------------- *//* Display results *//* -------------------------------------------------------------------- */ int iRecord = 0; while( Fetch() ) { fprintf( fp, "Record %d\n", iRecord++ ); for( iCol = 0; iCol < GetColCount(); iCol++ ) { fprintf( fp, " %s: %s\n", GetColName(iCol), GetColData(iCol) ); } }}/************************************************************************//* GetTypeName() *//************************************************************************//** * Get name for SQL column type. * * Returns a string name for the indicated type code (as returned * from CPLODBCStatement::GetColType()). * * @param nTypeCode the SQL_ code, such as SQL_CHAR. * * @return internal string, "UNKNOWN" if code not recognised. */CPLString CPLODBCStatement::GetTypeName( int nTypeCode ){ switch( nTypeCode ) { case SQL_CHAR: return "CHAR"; case SQL_NUMERIC: return "NUMERIC"; case SQL_DECIMAL: return "DECIMAL"; case SQL_INTEGER: return "INTEGER"; case SQL_SMALLINT: return "SMALLINT"; case SQL_FLOAT: return "FLOAT"; case SQL_REAL: return "REAL"; case SQL_DOUBLE: return "DOUBLE"; case SQL_DATETIME: return "DATETIME"; case SQL_VARCHAR: return "VARCHAR"; case SQL_TYPE_DATE: return "DATE"; case SQL_TYPE_TIME: return "TIME"; case SQL_TYPE_TIMESTAMP: return "TIMESTAMP"; default: CPLString osResult; osResult.Printf( "UNKNOWN:%d", nTypeCode ); return osResult; }}/************************************************************************//* GetTypeMapping() *//************************************************************************//** * Get appropriate C data type for SQL column type. * * Returns a C data type code, corresponding to the indicated SQL data * type code (as returned from CPLODBCStatement::GetColType()). * * @param nTypeCode the SQL_ code, such as SQL_CHAR. * * @return data type code. The valid code is always returned. If SQL * code is not recognised, SQL_C_BINARY will be returned. */SQLSMALLINT CPLODBCStatement::GetTypeMapping( SQLSMALLINT nTypeCode ){ switch( nTypeCode ) { case SQL_CHAR: case SQL_VARCHAR: case SQL_LONGVARCHAR: case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR: return SQL_C_CHAR; case SQL_DECIMAL: case SQL_NUMERIC: return SQL_C_NUMERIC; case SQL_SMALLINT: return SQL_C_SSHORT; case SQL_INTEGER: return SQL_C_SLONG; case SQL_REAL: return SQL_C_FLOAT; case SQL_FLOAT: case SQL_DOUBLE: return SQL_C_DOUBLE; case SQL_BIT: case SQL_TINYINT: case SQL_BIGINT: case SQL_TYPE_DATE: case SQL_TYPE_TIME: case SQL_TYPE_TIMESTAMP:/* case SQL_TYPE_UTCDATETIME: case SQL_TYPE_UTCTIME:*/ case SQL_INTERVAL_MONTH: case SQL_INTERVAL_YEAR: case SQL_INTERVAL_YEAR_TO_MONTH: case SQL_INTERVAL_DAY: case SQL_INTERVAL_HOUR: case SQL_INTERVAL_MINUTE: case SQL_INTERVAL_SECOND: case SQL_INTERVAL_DAY_TO_HOUR: case SQL_INTERVAL_DAY_TO_MINUTE: case SQL_INTERVAL_DAY_TO_SECOND: case SQL_INTERVAL_HOUR_TO_MINUTE: case SQL_INTERVAL_HOUR_TO_SECOND: case SQL_INTERVAL_MINUTE_TO_SECOND: case SQL_GUID: return SQL_C_CHAR; case SQL_BINARY: case SQL_VARBINARY: case SQL_LONGVARBINARY: return SQL_C_BINARY; default: return SQL_C_CHAR; }}#endif /* #ifndef WIN32CE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -