+ // BJO20000425
+ if (pDb->FwdOnlyCursors() && cursorType != SQL_CURSOR_FORWARD_ONLY)
+ {
+ // Force the use of a forward only cursor...
+ cursorType = SQL_CURSOR_FORWARD_ONLY;
+ if (SQLSetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
+ {
+ // Should never happen
+ pDb->GetNextError(henv, hdbc, hstmtInternal);
+ return FALSE;
+ }
+ }
+ }
+ else
+ {
+ pDb->DispNextError();
+ pDb->DispAllErrors(henv, hdbc, hstmtInternal);
+ }
+ }
+#ifdef DBDEBUG_CONSOLE
+ else
+ cout << wxT("Cursor Type set to STATIC") << endl << endl;
+#endif
+
+ if (!queryOnly)
+ {
+ // Set the cursor type for the INSERT statement handle
+ if (SQLSetStmtOption(hstmtInsert, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc, hstmtInsert);
+ // Set the cursor type for the DELETE statement handle
+ if (SQLSetStmtOption(hstmtDelete, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc, hstmtDelete);
+ // Set the cursor type for the UPDATE statement handle
+ if (SQLSetStmtOption(hstmtUpdate, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc, hstmtUpdate);
+ }
+
+ // Make the default cursor the active cursor
+ hstmtDefault = GetNewCursor(FALSE,FALSE);
+ wxASSERT(hstmtDefault);
+ hstmt = *hstmtDefault;
+
+ return TRUE;
+
+} // wxDbTable::initialize()
+
+
+void wxDbTable::cleanup()
+{
+ wxString s;
+ if (pDb)
+ {
+ s.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName.c_str(), tableID, pDb);
+ pDb->WriteSqlLog(s);
+ }
+
+#ifdef __WXDEBUG__
+ if (tableID)
+ {
+ TablesInUse.DeleteContents(TRUE);
+ bool found = FALSE;
+
+ wxNode *pNode;
+ pNode = TablesInUse.First();
+ while (pNode && !found)
+ {
+ if (((wxTablesInUse *)pNode->Data())->tableID == tableID)
+ {
+ found = TRUE;
+ if (!TablesInUse.DeleteNode(pNode))
+ wxLogDebug (s,wxT("Unable to delete node!"));
+ }
+ else
+ pNode = pNode->Next();
+ }
+ if (!found)
+ {
+ wxString msg;
+ msg.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s.c_str());
+ wxLogDebug (msg,wxT("NOTICE..."));
+ }
+ }
+#endif
+
+ // Decrement the wxDb table count
+ if (pDb)
+ pDb->decrementTableCount();
+
+ // Delete memory allocated for column definitions
+ if (colDefs)
+ delete [] colDefs;
+
+ // Free statement handles
+ if (!queryOnly)
+ {
+ if (hstmtInsert)
+ {
+/*
+ODBC 3.0 says to use this form
+ if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+*/
+ if (SQLFreeStmt(hstmtInsert, SQL_DROP) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc);
+ }
+
+ if (hstmtDelete)
+ {
+/*
+ODBC 3.0 says to use this form
+ if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+*/
+ if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc);
+ }
+
+ if (hstmtUpdate)
+ {
+/*
+ODBC 3.0 says to use this form
+ if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+*/
+ if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc);
+ }
+ }
+
+ if (hstmtInternal)
+ {
+ if (SQLFreeStmt(hstmtInternal, SQL_DROP) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc);
+ }
+
+ // Delete dynamically allocated cursors
+ if (hstmtDefault)
+ DeleteCursor(hstmtDefault);
+
+ if (hstmtCount)
+ DeleteCursor(hstmtCount);
+
+ if (m_hstmtGridQuery)
+ DeleteCursor(m_hstmtGridQuery);
+
+} // wxDbTable::cleanup()
+
+
+/***************************** PRIVATE FUNCTIONS *****************************/
+
+
+/********** wxDbTable::bindParams() **********/
+bool wxDbTable::bindParams(bool forUpdate)
+{
+ wxASSERT(!queryOnly);
+ if (queryOnly)
+ return(FALSE);
+
+ SWORD fSqlType = 0;
+ UDWORD precision = 0;
+ SWORD scale = 0;
+
+ // Bind each column of the table that should be bound
+ // to a parameter marker
+ int i;
+ UWORD colNo;
+
+ for (i=0, colNo=1; i < noCols; i++)
+ {
+ if (forUpdate)
+ {
+ if (!colDefs[i].Updateable)
+ continue;
+ }
+ else
+ {
+ if (!colDefs[i].InsertAllowed)
+ continue;
+ }
+
+ switch(colDefs[i].DbDataType)
+ {
+ case DB_DATA_TYPE_VARCHAR:
+ fSqlType = pDb->GetTypeInfVarchar().FsqlType;
+ precision = colDefs[i].SzDataObj;
+ scale = 0;
+ if (colDefs[i].Null)
+ colDefs[i].CbValue = SQL_NULL_DATA;
+ else
+ colDefs[i].CbValue = SQL_NTS;
+ break;
+ case DB_DATA_TYPE_INTEGER:
+ fSqlType = pDb->GetTypeInfInteger().FsqlType;
+ precision = pDb->GetTypeInfInteger().Precision;
+ scale = 0;
+ if (colDefs[i].Null)
+ colDefs[i].CbValue = SQL_NULL_DATA;
+ else
+ colDefs[i].CbValue = 0;
+ break;
+ case DB_DATA_TYPE_FLOAT:
+ fSqlType = pDb->GetTypeInfFloat().FsqlType;
+ precision = pDb->GetTypeInfFloat().Precision;
+ scale = pDb->GetTypeInfFloat().MaximumScale;
+ // SQL Sybase Anywhere v5.5 returned a negative number for the
+ // MaxScale. This caused ODBC to kick out an error on ibscale.
+ // I check for this here and set the scale = precision.
+ //if (scale < 0)
+ // scale = (short) precision;
+ if (colDefs[i].Null)
+ colDefs[i].CbValue = SQL_NULL_DATA;
+ else
+ colDefs[i].CbValue = 0;
+ break;
+ case DB_DATA_TYPE_DATE:
+ fSqlType = pDb->GetTypeInfDate().FsqlType;
+ precision = pDb->GetTypeInfDate().Precision;
+ scale = 0;
+ if (colDefs[i].Null)
+ colDefs[i].CbValue = SQL_NULL_DATA;
+ else
+ colDefs[i].CbValue = 0;
+ break;
+ case DB_DATA_TYPE_BLOB:
+ fSqlType = pDb->GetTypeInfBlob().FsqlType;
+ precision = 50000;
+ scale = 0;
+ if (colDefs[i].Null)
+ colDefs[i].CbValue = SQL_NULL_DATA;
+ else
+ colDefs[i].CbValue = SQL_LEN_DATA_AT_EXEC(colDefs[i].SzDataObj);
+ break;
+ }
+ if (forUpdate)
+ {
+ if (SQLBindParameter(hstmtUpdate, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
+ fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
+ precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
+ {
+ return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
+ }
+ }
+ else
+ {
+ if (SQLBindParameter(hstmtInsert, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
+ fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
+ precision+1,&colDefs[i].CbValue) != SQL_SUCCESS)
+ {
+ return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
+ }
+ }
+ }
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDbTable::bindParams()
+
+
+/********** wxDbTable::bindInsertParams() **********/
+bool wxDbTable::bindInsertParams(void)
+{
+ return bindParams(FALSE);
+} // wxDbTable::bindInsertParams()
+
+
+/********** wxDbTable::bindUpdateParams() **********/
+bool wxDbTable::bindUpdateParams(void)
+{
+ return bindParams(TRUE);
+} // wxDbTable::bindUpdateParams()
+
+
+/********** wxDbTable::bindCols() **********/
+bool wxDbTable::bindCols(HSTMT cursor)
+{
+ static SDWORD cb;
+
+ // Bind each column of the table to a memory address for fetching data
+ UWORD i;
+ for (i = 0; i < noCols; i++)
+ {
+ cb = colDefs[i].CbValue;
+ if (SQLBindCol(cursor, (UWORD)(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
+ {
+ // Set the Null member variable to indicate the Null state
+ // of each column just read in.
+ int i;
+ for (i = 0; i < noCols; i++)
+ colDefs[i].Null = (colDefs[i].CbValue == SQL_NULL_DATA);
+ }
+ }
+ 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));
+ }
+ else
+ {
+ // Set the Null member variable to indicate the Null state
+ // of each column just read in.
+ int i;
+ for (i = 0; i < noCols; i++)
+ colDefs[i].Null = (colDefs[i].CbValue == SQL_NULL_DATA);
+ }
+ }
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDbTable::getRec()
+
+
+/********** wxDbTable::execDelete() **********/
+bool wxDbTable::execDelete(const wxString &pSqlStmt)
+{
+ RETCODE retcode;
+
+ // Execute the DELETE statement
+ retcode = SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
+
+ if (retcode == SQL_SUCCESS ||
+ retcode == SQL_NO_DATA_FOUND ||
+ retcode == SQL_SUCCESS_WITH_INFO)
+ {
+ // Record deleted successfully
+ return(TRUE);
+ }
+
+ // Problem deleting record
+ return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
+
+} // wxDbTable::execDelete()
+
+
+/********** wxDbTable::execUpdate() **********/
+bool wxDbTable::execUpdate(const wxString &pSqlStmt)
+{
+ RETCODE retcode;
+
+ // Execute the UPDATE statement
+ retcode = SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
+
+ if (retcode == SQL_SUCCESS ||
+ retcode == SQL_NO_DATA_FOUND ||
+ retcode == SQL_SUCCESS_WITH_INFO)
+ {
+ // Record updated successfully
+ return(TRUE);
+ }
+
+ // Problem updating record
+ return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
+
+} // wxDbTable::execUpdate()
+
+
+/********** wxDbTable::query() **********/
+bool wxDbTable::query(int queryType, bool forUpdate, bool distinct, const wxString &pSqlStmt)
+{
+ wxString sqlStmt;
+
+ 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);
+ }
+
+ // Make sure the cursor is closed first
+ if (!CloseCursor(hstmt))
+ return(FALSE);