+ int noCols = 0;
+
+ RETCODE retcode;
+
+ wxString TableName;
+
+ wxString UserID;
+ convertUserID(userID,UserID);
+
+ TableName = tableName;
+ // Oracle and Interbase table names are uppercase only, so force
+ // the name to uppercase just in case programmer forgot to do this
+ if ((Dbms() == dbmsORACLE) ||
+ (Dbms() == dbmsINTERBASE))
+ TableName = TableName.Upper();
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ // MySQL, SQLServer, and Access cannot accept a user name when looking up column names, so we
+ // use the call below that leaves out the user name
+ if (!UserID.IsEmpty() &&
+ Dbms() != dbmsMY_SQL &&
+ Dbms() != dbmsACCESS &&
+ Dbms() != dbmsMS_SQL_SERVER)
+ {
+ retcode = SQLColumns(hstmt,
+ NULL, 0, // All qualifiers
+ (UCHAR *) UserID.c_str(), SQL_NTS, // Owner
+ (UCHAR *) TableName.c_str(), SQL_NTS,
+ NULL, 0); // All columns
+ }
+ else
+ {
+ retcode = SQLColumns(hstmt,
+ NULL, 0, // All qualifiers
+ NULL, 0, // Owner
+ (UCHAR *) TableName.c_str(), SQL_NTS,
+ NULL, 0); // All columns
+ }
+ if (retcode != SQL_SUCCESS)
+ { // Error occured, abort
+ DispAllErrors(henv, hdbc, hstmt);
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return(-1);
+ }
+
+ // Count the columns
+ while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
+ noCols++;
+
+ if (retcode != SQL_NO_DATA_FOUND)
+ { // Error occured, abort
+ DispAllErrors(henv, hdbc, hstmt);
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return(-1);
+ }
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return noCols;
+
+} // wxDb::GetColumnCount()
+
+
+/********** wxDb::GetCatalog() *******/
+wxDbInf *wxDb::GetCatalog(const wxChar *userID)
+/*
+ * ---------------------------------------------------------------------
+ * -- 19991203 : mj10777 : Create ------
+ * -- : Creates a wxDbInf with Tables / Cols Array ------
+ * -- : uses SQLTables and fills pTableInf; ------
+ * -- : pColInf is set to NULL and numCols to 0; ------
+ * -- : returns pDbInf (wxDbInf) ------
+ * -- - if unsuccesfull (pDbInf == NULL) ------
+ * -- : pColInf can be filled with GetColumns(..); ------
+ * -- : numCols can be filled with GetColumnCount(..); ------
+ * ---------------------------------------------------------------------
+ *
+ * userID is evaluated in the following manner:
+ * userID == NULL ... UserID is ignored
+ * userID == "" ... UserID set equal to 'this->uid'
+ * userID != "" ... UserID set equal to 'userID'
+ *
+ * NOTE: ALL column bindings associated with this wxDb instance are unbound
+ * by this function. This function should use its own wxDb instance
+ * to avoid undesired unbinding of columns.
+ */
+{
+ wxDbInf *pDbInf = NULL; // Array of catalog entries
+ int noTab = 0; // Counter while filling table entries
+ int pass;
+ RETCODE retcode;
+ SDWORD cb;
+ wxString tblNameSave;
+
+ wxString UserID;
+ convertUserID(userID,UserID);
+
+ //-------------------------------------------------------------
+ pDbInf = new wxDbInf; // Create the Database Array
+ //-------------------------------------------------------------
+ // Table Information
+ // Pass 1 - Determine how many Tables there are.
+ // Pass 2 - Create the Table array and fill it
+ // - Create the Cols array = NULL
+ //-------------------------------------------------------------
+
+ for (pass = 1; pass <= 2; pass++)
+ {
+ SQLFreeStmt(hstmt, SQL_CLOSE); // Close if Open
+ tblNameSave.Empty();
+
+ if (!UserID.IsEmpty() &&
+ Dbms() != dbmsMY_SQL &&
+ Dbms() != dbmsACCESS &&
+ Dbms() != dbmsMS_SQL_SERVER)
+ {
+ retcode = SQLTables(hstmt,
+ NULL, 0, // All qualifiers
+ (UCHAR *) UserID.c_str(), SQL_NTS, // User specified
+ NULL, 0, // All tables
+ NULL, 0); // All columns
+ }
+ else
+ {
+ retcode = SQLTables(hstmt,
+ NULL, 0, // All qualifiers
+ NULL, 0, // User specified
+ NULL, 0, // All tables
+ NULL, 0); // All columns
+ }
+
+ if (retcode != SQL_SUCCESS)
+ {
+ DispAllErrors(henv, hdbc, hstmt);
+ pDbInf = NULL;
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return pDbInf;
+ }
+
+ while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS) // Table Information
+ {
+ if (pass == 1) // First pass, just count the Tables
+ {
+ if (pDbInf->numTables == 0)
+ {
+ GetData( 1, SQL_C_CHAR, (UCHAR*) pDbInf->catalog, 128+1, &cb);
+ GetData( 2, SQL_C_CHAR, (UCHAR*) pDbInf->schema, 128+1, &cb);
+ }
+ pDbInf->numTables++; // Counter for Tables
+ } // if (pass == 1)
+ if (pass == 2) // Create and fill the Table entries
+ {
+ if (pDbInf->pTableInf == NULL) // Has the Table Array been created
+ { // no, then create the Array
+ pDbInf->pTableInf = new wxDbTableInf[pDbInf->numTables];
+ noTab = 0;
+ } // if (pDbInf->pTableInf == NULL) // Has the Table Array been created
+
+ GetData( 3, SQL_C_CHAR, (UCHAR*) (pDbInf->pTableInf+noTab)->tableName, DB_MAX_TABLE_NAME_LEN+1, &cb);
+ GetData( 4, SQL_C_CHAR, (UCHAR*) (pDbInf->pTableInf+noTab)->tableType, 30+1, &cb);
+ GetData( 5, SQL_C_CHAR, (UCHAR*) (pDbInf->pTableInf+noTab)->tableRemarks, 254+1, &cb);
+
+ noTab++;
+ } // if
+ } // while
+ } // for
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ // Query how many columns are in each table
+ for (noTab=0;noTab<pDbInf->numTables;noTab++)
+ {
+ (pDbInf->pTableInf+noTab)->numCols = GetColumnCount((pDbInf->pTableInf+noTab)->tableName,UserID);
+ }
+
+ return pDbInf;
+
+} // wxDb::GetCatalog()
+
+
+/********** wxDb::Catalog() **********/
+bool wxDb::Catalog(const wxChar *userID, const wxString &fileName)
+/*
+ * Creates the text file specified in 'filename' which will contain
+ * a minimal data dictionary of all tables accessible by the user specified
+ * in 'userID'
+ *
+ * userID is evaluated in the following manner:
+ * userID == NULL ... UserID is ignored
+ * userID == "" ... UserID set equal to 'this->uid'
+ * userID != "" ... UserID set equal to 'userID'
+ *
+ * NOTE: ALL column bindings associated with this wxDb instance are unbound
+ * by this function. This function should use its own wxDb instance
+ * to avoid undesired unbinding of columns.
+ */
+{
+ wxASSERT(fileName.Length());
+
+ RETCODE retcode;
+ SDWORD cb;
+ wxChar tblName[DB_MAX_TABLE_NAME_LEN+1];
+ wxString tblNameSave;
+ wxChar colName[DB_MAX_COLUMN_NAME_LEN+1];
+ SWORD sqlDataType;
+ wxChar typeName[30+1];
+ SWORD precision, length;
+
+ FILE *fp = fopen(fileName.c_str(),wxT("wt"));
+ if (fp == NULL)
+ return(FALSE);
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ wxString UserID;
+ convertUserID(userID,UserID);
+
+ if (!UserID.IsEmpty() &&
+ Dbms() != dbmsMY_SQL &&
+ Dbms() != dbmsACCESS &&
+ Dbms() != dbmsMS_SQL_SERVER)
+ {
+ retcode = SQLColumns(hstmt,
+ NULL, 0, // All qualifiers
+ (UCHAR *) UserID.c_str(), SQL_NTS, // User specified
+ NULL, 0, // All tables
+ NULL, 0); // All columns
+ }
+ else
+ {
+ retcode = SQLColumns(hstmt,
+ NULL, 0, // All qualifiers
+ NULL, 0, // User specified
+ NULL, 0, // All tables
+ NULL, 0); // All columns
+ }
+ if (retcode != SQL_SUCCESS)
+ {
+ DispAllErrors(henv, hdbc, hstmt);
+ fclose(fp);
+ return(FALSE);
+ }
+
+ wxString outStr;
+ tblNameSave.Empty();
+ int cnt = 0;
+
+ while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
+ {
+ if (wxStrcmp(tblName, tblNameSave.c_str()))
+ {
+ if (cnt)
+ fputs(wxT("\n"), fp);
+ fputs(wxT("================================ "), fp);
+ fputs(wxT("================================ "), fp);
+ fputs(wxT("===================== "), fp);
+ fputs(wxT("========= "), fp);
+ fputs(wxT("=========\n"), fp);
+ outStr.Printf(wxT("%-32s %-32s %-21s %9s %9s\n"),
+ wxT("TABLE NAME"), wxT("COLUMN NAME"), wxT("DATA TYPE"), wxT("PRECISION"), wxT("LENGTH"));
+ fputs(outStr.c_str(), fp);
+ fputs(wxT("================================ "), fp);
+ fputs(wxT("================================ "), fp);
+ fputs(wxT("===================== "), fp);
+ fputs(wxT("========= "), fp);
+ fputs(wxT("=========\n"), fp);
+ tblNameSave = tblName;
+ }
+
+ GetData(3,SQL_C_CHAR, (UCHAR *)tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
+ GetData(4,SQL_C_CHAR, (UCHAR *)colName, DB_MAX_COLUMN_NAME_LEN+1,&cb);
+ GetData(5,SQL_C_SSHORT,(UCHAR *)&sqlDataType,0, &cb);
+ GetData(6,SQL_C_CHAR, (UCHAR *)typeName, sizeof(typeName), &cb);
+ GetData(7,SQL_C_SSHORT,(UCHAR *)&precision, 0, &cb);
+ GetData(8,SQL_C_SSHORT,(UCHAR *)&length, 0, &cb);
+
+ outStr.Printf(wxT("%-32s %-32s (%04d)%-15s %9d %9d\n"),
+ tblName, colName, sqlDataType, typeName, precision, length);
+ if (fputs(outStr.c_str(), fp) == EOF)
+ {
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ fclose(fp);
+ return(FALSE);
+ }
+ cnt++;
+ }
+
+ if (retcode != SQL_NO_DATA_FOUND)
+ DispAllErrors(henv, hdbc, hstmt);
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ fclose(fp);
+ return(retcode == SQL_NO_DATA_FOUND);
+
+} // wxDb::Catalog()
+
+
+bool wxDb::TableExists(const wxString &tableName, const wxChar *userID, const wxString &tablePath)
+/*
+ * Table name can refer to a table, view, alias or synonym. Returns true
+ * if the object exists in the database. This function does not indicate
+ * whether or not the user has privleges to query or perform other functions
+ * on the table.
+ *
+ * userID is evaluated in the following manner:
+ * userID == NULL ... UserID is ignored
+ * userID == "" ... UserID set equal to 'this->uid'
+ * userID != "" ... UserID set equal to 'userID'
+ */
+{
+ wxASSERT(tableName.Length());
+
+ wxString TableName;
+
+ if (Dbms() == dbmsDBASE)
+ {
+ wxString dbName;
+ if (tablePath.Length())
+ dbName.Printf(wxT("%s/%s.dbf"), tablePath.c_str(), tableName.c_str());
+ else
+ dbName.Printf(wxT("%s.dbf"), tableName.c_str());
+
+ bool exists;
+ exists = wxFileExists(dbName);
+ return exists;
+ }
+
+ wxString UserID;
+ convertUserID(userID,UserID);
+
+ TableName = tableName;
+ // Oracle and Interbase table names are uppercase only, so force
+ // the name to uppercase just in case programmer forgot to do this
+ if ((Dbms() == dbmsORACLE) ||
+ (Dbms() == dbmsINTERBASE))
+ TableName = TableName.Upper();
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ RETCODE retcode;
+
+ // Some databases cannot accept a user name when looking up table names,
+ // so we use the call below that leaves out the user name
+ if (!UserID.IsEmpty() &&
+ Dbms() != dbmsMY_SQL &&
+ Dbms() != dbmsACCESS &&
+ Dbms() != dbmsMS_SQL_SERVER)
+ {
+ retcode = SQLTables(hstmt,
+ NULL, 0, // All qualifiers
+ (UCHAR *) UserID.c_str(), SQL_NTS, // Only tables owned by this user
+ (UCHAR FAR *)TableName.c_str(), SQL_NTS,
+ NULL, 0); // All table types
+ }
+ else
+ {
+ retcode = SQLTables(hstmt,
+ NULL, 0, // All qualifiers
+ NULL, 0, // All owners
+ (UCHAR FAR *)TableName.c_str(), SQL_NTS,
+ NULL, 0); // All table types
+ }
+ if (retcode != SQL_SUCCESS)
+ return(DispAllErrors(henv, hdbc, hstmt));
+
+ retcode = SQLFetch(hstmt);
+ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
+ {
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return(DispAllErrors(henv, hdbc, hstmt));
+ }
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ return(TRUE);
+
+} // wxDb::TableExists()
+
+
+/********** wxDb::TablePrivileges() **********/
+bool wxDb::TablePrivileges(const wxString &tableName, const wxString &priv, const wxChar *userID,
+ const wxChar *schema, const wxString &tablePath)
+{
+ wxASSERT(tableName.Length());
+
+ wxDbTablePrivilegeInfo result;
+ SDWORD cbRetVal;
+ RETCODE retcode;
+
+ // We probably need to be able to dynamically set this based on
+ // the driver type, and state.
+ wxChar curRole[]=wxT("public");
+
+ wxString TableName;
+
+ wxString UserID;
+ convertUserID(userID,UserID);
+
+ TableName = tableName;
+ // Oracle and Interbase table names are uppercase only, so force
+ // the name to uppercase just in case programmer forgot to do this
+ if ((Dbms() == dbmsORACLE) ||
+ (Dbms() == dbmsINTERBASE))
+ TableName = TableName.Upper();
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ if (!schema)
+ {
+ retcode = SQLTablePrivileges(hstmt,
+ NULL, 0, // Catalog
+ NULL, 0, // Schema
+ (UCHAR FAR *)TableName.c_str(), SQL_NTS);
+ }
+ else
+ {
+ retcode = SQLTablePrivileges(hstmt,
+ NULL, 0, // Catalog
+ (UCHAR FAR *)schema, SQL_NTS, // Schema
+ (UCHAR FAR *)TableName.c_str(), SQL_NTS);
+ }