+
+/********** wxDB::GetKeyFields() **********/
+int wxDB::GetKeyFields(char *tableName, wxColInf* colInf,int noCols)
+{
+ char szPkTable[DB_MAX_TABLE_NAME_LEN+1]; /* Primary key table name */
+ char szFkTable[DB_MAX_TABLE_NAME_LEN+1]; /* Foreign key table name */
+ short iKeySeq;
+// SQLSMALLINT iKeySeq;
+ char szPkCol[DB_MAX_COLUMN_NAME_LEN+1]; /* Primary key column */
+ char szFkCol[DB_MAX_COLUMN_NAME_LEN+1]; /* Foreign key column */
+ SQLRETURN retcode;
+ SDWORD cb;
+ int i;
+ wxString Temp0;
+ /*
+ * ---------------------------------------------------------------------
+ * -- 19991224 : mj10777@gmx.net : Create ------
+ * -- : Three things are done and stored here : ------
+ * -- : 1) which Column(s) is/are Primary Key(s) ------
+ * -- : 2) which tables use this Key as a Foreign Key ------
+ * -- : 3) which columns are Foreign Key and the name ------
+ * -- : of the Table where the Key is the Primary Key -----
+ * -- : Called from GetColumns(char *tableName, ------
+ * -- int *numCols,const char *userID ) ------
+ * ---------------------------------------------------------------------
+ */
+
+ /*---------------------------------------------------------------------*/
+ /* Get the names of the columns in the primary key. */
+ /*---------------------------------------------------------------------*/
+ retcode = SQLPrimaryKeys(hstmt,
+ NULL, 0, /* Catalog name */
+ NULL, 0, /* Schema name */
+ (UCHAR *) tableName, SQL_NTS); /* Table name */
+
+ /*---------------------------------------------------------------------*/
+ /* Fetch and display the result set. This will be a list of the */
+ /* columns in the primary key of the tableName table. */
+ /*---------------------------------------------------------------------*/
+ while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
+ {
+ retcode = SQLFetch(hstmt);
+ if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
+ {
+ GetData( 4, SQL_C_CHAR, szPkCol, DB_MAX_COLUMN_NAME_LEN+1, &cb);
+ GetData( 5, SQL_C_SSHORT, &iKeySeq, 0, &cb);
+ //-------
+ for (i=0;i<noCols;i++) // Find the Column name
+ if (!wxStrcmp(colInf[i].colName,szPkCol)) // We have found the Column
+ colInf[i].PkCol = iKeySeq; // Which Primary Key is this (first, second usw.) ?
+ } // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
+ } // while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
+ SQLFreeStmt(hstmt, SQL_CLOSE); /* Close the cursor (the hstmt is still allocated). */
+
+ /*---------------------------------------------------------------------*/
+ /* Get all the foreign keys that refer to tableName primary key. */
+ /*---------------------------------------------------------------------*/
+ retcode = SQLForeignKeys(hstmt,
+ NULL, 0, /* Primary catalog */
+ NULL, 0, /* Primary schema */
+ (UCHAR *)tableName, SQL_NTS, /* Primary table */
+ NULL, 0, /* Foreign catalog */
+ NULL, 0, /* Foreign schema */
+ NULL, 0); /* Foreign table */
+
+ /*---------------------------------------------------------------------*/
+ /* Fetch and display the result set. This will be all of the foreign */
+ /* keys in other tables that refer to the tableName primary key. */
+ /*---------------------------------------------------------------------*/
+ Temp0.Empty();
+ szPkCol[0] = 0;
+ while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
+ {
+ retcode = SQLFetch(hstmt);
+ if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
+ {
+ GetData( 3, SQL_C_CHAR, szPkTable, DB_MAX_TABLE_NAME_LEN+1, &cb);
+ GetData( 4, SQL_C_CHAR, szPkCol, DB_MAX_COLUMN_NAME_LEN+1, &cb);
+ GetData( 5, SQL_C_SSHORT, &iKeySeq, 0, &cb);
+ GetData( 7, SQL_C_CHAR, szFkTable, DB_MAX_TABLE_NAME_LEN+1, &cb);
+ GetData( 8, SQL_C_CHAR, szFkCol, DB_MAX_COLUMN_NAME_LEN+1, &cb);
+ Temp0.Printf("%s[%s] ",Temp0.c_str(),szFkTable); // [ ] in case there is a blank in the Table name
+ } // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
+ } // while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
+ Temp0.Trim(); // Get rid of any unneeded blanks
+ if (Temp0 != "")
+ {
+ for (i=0;i<noCols;i++) // Find the Column name
+ if (!wxStrcmp(colInf[i].colName,szPkCol)) // We have found the Column, store the Information
+ strcpy(colInf[i].PkTableName,Temp0); // Name of the Tables where this Primary Key is used as a Foreign Key
+ } // if (Temp0 != "")
+ SQLFreeStmt(hstmt, SQL_CLOSE); /* Close the cursor (the hstmt is still allocated). */
+
+ /*---------------------------------------------------------------------*/
+ /* Get all the foreign keys in the tablename table. */
+ /*---------------------------------------------------------------------*/
+ retcode = SQLForeignKeys(hstmt,
+ NULL, 0, /* Primary catalog */
+ NULL, 0, /* Primary schema */
+ NULL, 0, /* Primary table */
+ NULL, 0, /* Foreign catalog */
+ NULL, 0, /* Foreign schema */
+ (UCHAR *)tableName, SQL_NTS); /* Foreign table */
+
+ /*---------------------------------------------------------------------*/
+ /* Fetch and display the result set. This will be all of the */
+ /* primary keys in other tables that are referred to by foreign */
+ /* keys in the tableName table. */
+ /*---------------------------------------------------------------------*/
+ i = 0;
+ while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
+ {
+ retcode = SQLFetch(hstmt);
+ if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
+ {
+ GetData( 3, SQL_C_CHAR, szPkTable, DB_MAX_TABLE_NAME_LEN+1, &cb);
+ GetData( 5, SQL_C_SSHORT, &iKeySeq, 0, &cb);
+ GetData( 8, SQL_C_CHAR, szFkCol, DB_MAX_COLUMN_NAME_LEN+1, &cb);
+ //-------
+ for (i=0;i<noCols;i++) // Find the Column name
+ {
+ if (!wxStrcmp(colInf[i].colName,szFkCol)) // We have found the (Foreign Key) Column
+ {
+ colInf[i].FkCol = iKeySeq; // Which Foreign Key is this (first, second usw.) ?
+ strcpy(colInf[i].FkTableName,szPkTable); // Name of the Table where this Foriegn is the Primary Key
+ } // if (!wxStrcmp(colInf[i].colName,szFkCol))
+ } // for (i=0;i<noCols;i++)
+ } // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
+ } // while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
+ SQLFreeStmt(hstmt, SQL_CLOSE); /* Close the cursor (the hstmt is still allocated). */
+
+ /*---------------------------------------------------------------------*/
+ return TRUE;
+} // wxDB::GetKeyFields()
+
+
+/********** wxDB::GetColumns() **********/
+wxColInf *wxDB::GetColumns(char *tableName[], const char *userID)
+/*
+ * 1) The last array element of the tableName[] argument must be zero (null).
+ * This is how the end of the array is detected.
+ * 2) This function returns an array of wxColInf structures. If no columns
+ * were found, or an error occured, this pointer will be zero (null). THE
+ * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
+ * IS FINISHED WITH IT. i.e.
+ *
+ * wxColInf *colInf = pDb->GetColumns(tableList, userID);
+ * if (colInf)
+ * {
+ * // Use the column inf
+ * .......
+ * // Destroy the memory
+ * delete [] colInf;
+ * }
+ *
+ * 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.
+ */
+{
+ int noCols = 0;
+ int colNo = 0;
+ wxColInf *colInf = 0;
+
+ RETCODE retcode;
+ SDWORD cb;
+
+ wxString UserID;
+ wxString TableName;
+
+ if (userID)
+ {
+ if (!wxStrlen(userID))
+ UserID = uid;
+ else
+ UserID = userID;
+ }
+ else
+ UserID = "";
+
+ // dBase does not use user names, and some drivers fail if you try to pass one
+ if (Dbms() == dbmsDBASE)
+ UserID = "";
+
+ // Oracle user names may only be in uppercase, so force
+ // the name to uppercase
+ if (Dbms() == dbmsORACLE)
+ UserID = UserID.Upper();
+
+ // Pass 1 - Determine how many columns there are.
+ // Pass 2 - Allocate the wxColInf array and fill in
+ // the array with the column information.
+ int pass;
+ for (pass = 1; pass <= 2; pass++)
+ {
+ if (pass == 2)
+ {
+ if (noCols == 0) // Probably a bogus table name(s)
+ break;
+ // Allocate n wxColInf objects to hold the column information
+ colInf = new wxColInf[noCols+1];
+ if (!colInf)
+ break;
+ // Mark the end of the array
+ wxStrcpy(colInf[noCols].tableName, "");
+ wxStrcpy(colInf[noCols].colName, "");
+ colInf[noCols].sqlDataType = 0;
+ }
+ // Loop through each table name
+ int tbl;
+ for (tbl = 0; tableName[tbl]; tbl++)
+ {
+ TableName = tableName[tbl];
+ // Oracle table names are uppercase only, so force
+ // the name to uppercase just in case programmer forgot to do this
+ if (Dbms() == dbmsORACLE)
+ TableName = TableName.Upper();
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+
+ // MySQL 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 (wxStrcmp(UserID.GetData(),"") &&
+ Dbms() != dbmsMY_SQL &&
+ Dbms() != dbmsACCESS)
+ {
+ retcode = SQLColumns(hstmt,
+ NULL, 0, // All qualifiers
+ (UCHAR *) UserID.GetData(), SQL_NTS, // Owner
+ (UCHAR *) TableName.GetData(), SQL_NTS,
+ NULL, 0); // All columns
+ }
+ else
+ {
+ retcode = SQLColumns(hstmt,
+ NULL, 0, // All qualifiers
+ NULL, 0, // Owner
+ (UCHAR *) TableName.GetData(), SQL_NTS,
+ NULL, 0); // All columns
+ }
+ if (retcode != SQL_SUCCESS)
+ { // Error occured, abort
+ DispAllErrors(henv, hdbc, hstmt);
+ if (colInf)
+ delete [] colInf;
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return(0);
+ }
+
+ while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
+ {
+ if (pass == 1) // First pass, just add up the number of columns
+ noCols++;
+ else // Pass 2; Fill in the array of structures
+ {
+ if (colNo < noCols) // Some extra error checking to prevent memory overwrites
+ {
+ // NOTE: Only the ODBC 1.x fields are retrieved
+ GetData( 1, SQL_C_CHAR, (UCHAR*) colInf[colNo].catalog, 128+1, &cb);
+ GetData( 2, SQL_C_CHAR, (UCHAR*) colInf[colNo].schema, 128+1, &cb);
+ GetData( 3, SQL_C_CHAR, (UCHAR*) colInf[colNo].tableName, DB_MAX_TABLE_NAME_LEN+1, &cb);
+ GetData( 4, SQL_C_CHAR, (UCHAR*) colInf[colNo].colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
+ GetData( 5, SQL_C_SSHORT, (UCHAR*) &colInf[colNo].sqlDataType, 0, &cb);
+ GetData( 6, SQL_C_CHAR, (UCHAR*) colInf[colNo].typeName, 128+1, &cb);
+ GetData( 7, SQL_C_SLONG, (UCHAR*) &colInf[colNo].columnSize, 0, &cb);
+ GetData( 8, SQL_C_SLONG, (UCHAR*) &colInf[colNo].bufferLength, 0, &cb);
+ GetData( 9, SQL_C_SSHORT, (UCHAR*) &colInf[colNo].decimalDigits,0, &cb);
+ GetData(10, SQL_C_SSHORT, (UCHAR*) &colInf[colNo].numPrecRadix, 0, &cb);
+ GetData(11, SQL_C_SSHORT, (UCHAR*) &colInf[colNo].nullable, 0, &cb);
+ GetData(12, SQL_C_CHAR, (UCHAR*) colInf[colNo].remarks, 254+1, &cb);
+
+ // Determine the wxDB data type that is used to represent the native data type of this data source
+ colInf[colNo].dbDataType = 0;
+ if (!wxStricmp(typeInfVarchar.TypeName,colInf[colNo].typeName))
+ {
+ if (colInf[colNo].columnSize < 1)
+ {
+ // IODBC does not return a correct columnSize, so we set
+ // columnSize = bufferLength if no column size was returned
+ colInf[colNo].columnSize = colInf[colNo].bufferLength;
+ }
+ colInf[colNo].dbDataType = DB_DATA_TYPE_VARCHAR;
+ }
+ else if (!wxStricmp(typeInfInteger.TypeName,colInf[colNo].typeName))
+ colInf[colNo].dbDataType = DB_DATA_TYPE_INTEGER;
+ else if (!wxStricmp(typeInfFloat.TypeName,colInf[colNo].typeName))
+ colInf[colNo].dbDataType = DB_DATA_TYPE_FLOAT;
+ else if (!wxStricmp(typeInfDate.TypeName,colInf[colNo].typeName))
+ colInf[colNo].dbDataType = DB_DATA_TYPE_DATE;
+
+ colNo++;
+ }
+ }
+ }
+ if (retcode != SQL_NO_DATA_FOUND)
+ { // Error occured, abort
+ DispAllErrors(henv, hdbc, hstmt);
+ if (colInf)
+ delete [] colInf;
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return(0);
+ }
+ }
+ }
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ return colInf;
+
+} // wxDB::GetColumns()
+
+