- if (!pDb)
- return FALSE;
-
- int i;
- char sqlStmt[DB_MAX_STATEMENT_LEN];
-
- // Verify that the table exists in the database
- if (!pDb->TableExists(tableName,NULL,tablePath))
- {
- char s[128];
- sprintf(s, "Error opening '%s', table/view does not exist in the database.", tableName);
- pDb->LogError(s);
- return(FALSE);
- }
-
- // Bind the member variables for field exchange between
- // the wxTable object and the ODBC record.
- if (!queryOnly)
- {
- if (!bindInsertParams()) // Inserts
- return(FALSE);
- if (!bindUpdateParams()) // Updates
- return(FALSE);
- }
- if (!bindCols(*hstmtDefault)) // Selects
- return(FALSE);
- if (!bindCols(hstmtInternal)) // Internal use only
- return(FALSE);
- /*
- * Do NOT bind the hstmtCount cursor!!!
- */
-
- // Build an insert statement using parameter markers
- if (!queryOnly && noCols > 0)
- {
- bool needComma = FALSE;
- sprintf(sqlStmt, "INSERT INTO %s (", tableName);
- for (i = 0; i < noCols; i++)
- {
- if (! colDefs[i].InsertAllowed)
- continue;
- if (needComma)
- strcat(sqlStmt, ",");
- strcat(sqlStmt, colDefs[i].ColName);
- needComma = TRUE;
- }
- needComma = FALSE;
- strcat(sqlStmt, ") VALUES (");
- for (i = 0; i < noCols; i++)
- {
- if (! colDefs[i].InsertAllowed)
- continue;
- if (needComma)
- strcat(sqlStmt, ",");
- strcat(sqlStmt, "?");
- needComma = TRUE;
- }
- strcat(sqlStmt, ")");
-
-// pDb->WriteSqlLog(sqlStmt);
-
- // Prepare the insert statement for execution
- if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
- return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
- }
-
- // Completed successfully
- return(TRUE);
-
-} // wxTable::Open()
-
-/********** wxTable::Query() **********/
-bool wxTable::Query(bool forUpdate, bool distinct)
+ static SDWORD cb;
+
+ // Bind each column of the table to a memory address for fetching data
+ int i;
+ for (i = 0; i < noCols; i++)
+ {
+ if (SQLBindCol(cursor, i+1, colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
+ colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
+ {
+ return (pDb->DispAllErrors(henv, hdbc, cursor));
+ }
+ }
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDbTable::bindCols()
+
+
+/********** wxDbTable::getRec() **********/
+bool wxDbTable::getRec(UWORD fetchType)
+{
+ RETCODE retcode;
+
+ if (!pDb->FwdOnlyCursors())
+ {
+ // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
+ UDWORD cRowsFetched;
+ UWORD rowStatus;
+
+ retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
+ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
+ {
+ if (retcode == SQL_NO_DATA_FOUND)
+ return(FALSE);
+ else
+ return(pDb->DispAllErrors(henv, hdbc, hstmt));
+ }
+ }
+ else
+ {
+ // Fetch the next record from the record set
+ retcode = SQLFetch(hstmt);
+ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
+ {
+ if (retcode == SQL_NO_DATA_FOUND)
+ return(FALSE);
+ else
+ return(pDb->DispAllErrors(henv, hdbc, hstmt));
+ }
+ }
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDbTable::getRec()
+
+
+/********** wxDbTable::execDelete() **********/
+bool wxDbTable::execDelete(const char *pSqlStmt)
+{
+ // Execute the DELETE statement
+ if (SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
+ return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
+
+ // Record deleted successfully
+ return(TRUE);
+
+} // wxDbTable::execDelete()
+
+
+/********** wxDbTable::execUpdate() **********/
+bool wxDbTable::execUpdate(const char *pSqlStmt)
+{
+ // Execute the UPDATE statement
+ if (SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
+ return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
+
+ // Record deleted successfully
+ return(TRUE);
+
+} // wxDbTable::execUpdate()
+
+
+/********** wxDbTable::query() **********/
+bool wxDbTable::query(int queryType, bool forUpdate, bool distinct, const char *pSqlStmt)
+{
+ char sqlStmt[DB_MAX_STATEMENT_LEN];
+
+ // Set the selectForUpdate member variable
+ if (forUpdate)
+ // The user may wish to select for update, but the DBMS may not be capable
+ selectForUpdate = CanSelectForUpdate();
+ else
+ selectForUpdate = FALSE;
+
+ // Set the SQL SELECT string
+ if (queryType != DB_SELECT_STATEMENT) // A select statement was not passed in,
+ { // so generate a select statement.
+ BuildSelectStmt(sqlStmt, queryType, distinct);
+ pDb->WriteSqlLog(sqlStmt);
+ }
+ else
+ wxStrcpy(sqlStmt, pSqlStmt);
+
+ SQLFreeStmt(hstmt, SQL_CLOSE);
+ if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) == SQL_SUCCESS)
+ return(TRUE);
+ else
+ {
+ pDb->DispAllErrors(henv, hdbc, hstmt);
+ return(FALSE);
+ }
+
+ // Make sure the cursor is closed first
+ if (! CloseCursor(hstmt))
+ return(FALSE);
+
+ // Execute the SQL SELECT statement
+ int retcode;
+ retcode = SQLExecDirect(hstmt, (UCHAR FAR *) (queryType == DB_SELECT_STATEMENT ? pSqlStmt : sqlStmt), SQL_NTS);
+ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
+ return(pDb->DispAllErrors(henv, hdbc, hstmt));
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDbTable::query()
+
+
+/***************************** PUBLIC FUNCTIONS *****************************/
+
+
+/********** wxDbTable::Open() **********/
+bool wxDbTable::Open(void)
+{
+ if (!pDb)
+ return FALSE;
+
+ int i;
+ wxString sqlStmt;
+
+ // Verify that the table exists in the database
+ if (!pDb->TableExists(tableName,pDb->GetUsername(),tablePath))
+ {
+ wxString s;
+ if (wxStrcmp(tablePath,""))
+ s.sprintf(wxT("Error opening '%s/%s'.\n"),tablePath,tableName);
+ else
+ s.sprintf(wxT("Error opening '%s'.\n"), tableName);
+ if (!pDb->TableExists(tableName,NULL,tablePath))
+ s += wxT("Table/view does not exist in the database.\n");
+ else
+ s += wxT("Current logged in user does not have sufficient privileges to access this table.\n");
+ pDb->LogError(s.c_str());
+ return(FALSE);
+ }
+
+ // Bind the member variables for field exchange between
+ // the wxDbTable object and the ODBC record.
+ if (!queryOnly)
+ {
+ if (!bindInsertParams()) // Inserts
+ return(FALSE);
+
+ if (!bindUpdateParams()) // Updates
+ return(FALSE);
+ }
+
+ if (!bindCols(*hstmtDefault)) // Selects
+ return(FALSE);
+
+ if (!bindCols(hstmtInternal)) // Internal use only
+ return(FALSE);
+
+ /*
+ * Do NOT bind the hstmtCount cursor!!!
+ */
+
+ // Build an insert statement using parameter markers
+ if (!queryOnly && noCols > 0)
+ {
+ bool needComma = FALSE;
+ sqlStmt.sprintf("INSERT INTO %s (", tableName);
+ for (i = 0; i < noCols; i++)
+ {
+ if (! colDefs[i].InsertAllowed)
+ continue;
+ if (needComma)
+ sqlStmt += ",";
+ sqlStmt += colDefs[i].ColName;
+ needComma = TRUE;
+ }
+ needComma = FALSE;
+ sqlStmt += ") VALUES (";
+
+ int insertableCount = 0;
+
+ for (i = 0; i < noCols; i++)
+ {
+ if (! colDefs[i].InsertAllowed)
+ continue;
+ if (needComma)
+ sqlStmt += ",";
+ sqlStmt += "?";
+ needComma = TRUE;
+ insertableCount++;
+ }
+ sqlStmt += ")";
+
+ // Prepare the insert statement for execution
+ if (insertableCount)
+ {
+ if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+ return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
+ }
+ else
+ insertable= FALSE;
+ }
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDbTable::Open()
+
+
+/********** wxDbTable::Query() **********/
+bool wxDbTable::Query(bool forUpdate, bool distinct)