+
+/********** wxDb::Close() **********/
+void wxDb::Close(void)
+{
+ // Close the Sql Log file
+ if (fpSqlLog)
+ {
+ fclose(fpSqlLog);
+ fpSqlLog = 0;
+ }
+
+ // Free statement handle
+ if (dbIsOpen)
+ {
+ if (SQLFreeStmt(hstmt, SQL_DROP) != SQL_SUCCESS)
+ DispAllErrors(henv, hdbc);
+ }
+
+ // Disconnect from the datasource
+ if (SQLDisconnect(hdbc) != SQL_SUCCESS)
+ DispAllErrors(henv, hdbc);
+
+ // Free the connection to the datasource
+ if (SQLFreeConnect(hdbc) != SQL_SUCCESS)
+ DispAllErrors(henv, hdbc);
+
+ // There should be zero Ctable objects still connected to this db object
+ wxASSERT(nTables == 0);
+
+#ifdef __WXDEBUG__
+ wxTablesInUse *tiu;
+ wxNode *pNode;
+ pNode = TablesInUse.First();
+ wxString s,s2;
+ while (pNode)
+ {
+ tiu = (wxTablesInUse *)pNode->Data();
+ if (tiu->pDb == this)
+ {
+ s.Printf(wxT("(%-20s) tableID:[%6lu] pDb:[%p]"), tiu->tableName,tiu->tableID,tiu->pDb);
+ s2.Printf(wxT("Orphaned found using pDb:[%p]"),this);
+ wxLogDebug (s,s2);
+ }
+ pNode = pNode->Next();
+ }
+#endif
+
+ // Copy the error messages to a global variable
+ int i;
+ for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
+ wxStrcpy(DBerrorList[i], errorList[i]);
+
+ dbmsType = dbmsUNIDENTIFIED;
+ dbIsOpen = FALSE;
+
+} // wxDb::Close()
+
+
+/********** wxDb::CommitTrans() **********/
+bool wxDb::CommitTrans(void)
+{
+ if (this)
+ {
+ // Commit the transaction
+ if (SQLTransact(henv, hdbc, SQL_COMMIT) != SQL_SUCCESS)
+ return(DispAllErrors(henv, hdbc));
+ }
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDb::CommitTrans()
+
+
+/********** wxDb::RollbackTrans() **********/
+bool wxDb::RollbackTrans(void)
+{
+ // Rollback the transaction
+ if (SQLTransact(henv, hdbc, SQL_ROLLBACK) != SQL_SUCCESS)
+ return(DispAllErrors(henv, hdbc));
+
+ // Completed successfully
+ return(TRUE);
+
+} // wxDb::RollbackTrans()
+
+
+/********** wxDb::DispAllErrors() **********/
+bool wxDb::DispAllErrors(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
+/*
+ * This function is called internally whenever an error condition prevents the user's
+ * request from being executed. This function will query the datasource as to the
+ * actual error(s) that just occured on the previous request of the datasource.
+ *
+ * The function will retrieve each error condition from the datasource and
+ * Printf the codes/text values into a string which it then logs via logError().
+ * If in DBDEBUG_CONSOLE mode, the constructed string will be displayed in the console
+ * window and program execution will be paused until the user presses a key.
+ *
+ * This function always returns a FALSE, so that functions which call this function
+ * can have a line like "return (DispAllErrors(henv, hdbc));" to indicate the failure
+ * of the users request, so that the calling code can then process the error msg log
+ */
+{
+ wxString odbcErrMsg;
+
+ while (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
+ {
+ odbcErrMsg.Printf(wxT("SQL State = %s\nNative Error Code = %li\nError Message = %s\n"), sqlState, nativeError, errorMsg);
+ logError(odbcErrMsg, sqlState);
+ if (!silent)
+ {
+#ifdef DBDEBUG_CONSOLE
+ // When run in console mode, use standard out to display errors.
+ cout << odbcErrMsg.c_str() << endl;
+ cout << wxT("Press any key to continue...") << endl;
+ getchar();
+#endif
+
+#ifdef __WXDEBUG__
+ wxLogDebug(odbcErrMsg,wxT("ODBC DEBUG MESSAGE from DispAllErrors()"));
+#endif
+ }
+ }
+
+ return(FALSE); // This function always returns false.
+
+} // wxDb::DispAllErrors()
+
+
+/********** wxDb::GetNextError() **********/
+bool wxDb::GetNextError(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
+{
+ if (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
+ return(TRUE);
+ else
+ return(FALSE);
+
+} // wxDb::GetNextError()
+
+
+/********** wxDb::DispNextError() **********/
+void wxDb::DispNextError(void)
+{
+ wxString odbcErrMsg;
+
+ odbcErrMsg.Printf(wxT("SQL State = %s\nNative Error Code = %li\nError Message = %s\n"), sqlState, nativeError, errorMsg);
+ logError(odbcErrMsg, sqlState);
+
+ if (silent)
+ return;
+
+#ifdef DBDEBUG_CONSOLE
+ // When run in console mode, use standard out to display errors.
+ cout << odbcErrMsg.c_str() << endl;
+ cout << wxT("Press any key to continue...") << endl;
+ getchar();
+#endif
+
+#ifdef __WXDEBUG__
+ wxLogDebug(odbcErrMsg,wxT("ODBC DEBUG MESSAGE"));
+#endif // __WXDEBUG__
+
+} // wxDb::DispNextError()
+
+
+/********** wxDb::logError() **********/
+void wxDb::logError(const wxString &errMsg, const wxString &SQLState)
+{
+ wxASSERT(errMsg.Length());
+
+ static int pLast = -1;
+ int dbStatus;
+
+ if (++pLast == DB_MAX_ERROR_HISTORY)
+ {
+ int i;
+ for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
+ wxStrcpy(errorList[i], errorList[i+1]);
+ pLast--;
+ }
+
+ wxStrcpy(errorList[pLast], errMsg);
+
+ if (SQLState.Length())
+ if ((dbStatus = TranslateSqlState(SQLState)) != DB_ERR_FUNCTION_SEQUENCE_ERROR)
+ DB_STATUS = dbStatus;
+
+ // Add the errmsg to the sql log
+ WriteSqlLog(errMsg);
+
+} // wxDb::logError()
+
+
+/**********wxDb::TranslateSqlState() **********/
+int wxDb::TranslateSqlState(const wxString &SQLState)
+{
+ if (!wxStrcmp(SQLState, wxT("01000")))
+ return(DB_ERR_GENERAL_WARNING);
+ if (!wxStrcmp(SQLState, wxT("01002")))
+ return(DB_ERR_DISCONNECT_ERROR);
+ if (!wxStrcmp(SQLState, wxT("01004")))
+ return(DB_ERR_DATA_TRUNCATED);
+ if (!wxStrcmp(SQLState, wxT("01006")))
+ return(DB_ERR_PRIV_NOT_REVOKED);
+ if (!wxStrcmp(SQLState, wxT("01S00")))
+ return(DB_ERR_INVALID_CONN_STR_ATTR);
+ if (!wxStrcmp(SQLState, wxT("01S01")))
+ return(DB_ERR_ERROR_IN_ROW);
+ if (!wxStrcmp(SQLState, wxT("01S02")))
+ return(DB_ERR_OPTION_VALUE_CHANGED);
+ if (!wxStrcmp(SQLState, wxT("01S03")))
+ return(DB_ERR_NO_ROWS_UPD_OR_DEL);
+ if (!wxStrcmp(SQLState, wxT("01S04")))
+ return(DB_ERR_MULTI_ROWS_UPD_OR_DEL);
+ if (!wxStrcmp(SQLState, wxT("07001")))
+ return(DB_ERR_WRONG_NO_OF_PARAMS);
+ if (!wxStrcmp(SQLState, wxT("07006")))
+ return(DB_ERR_DATA_TYPE_ATTR_VIOL);
+ if (!wxStrcmp(SQLState, wxT("08001")))
+ return(DB_ERR_UNABLE_TO_CONNECT);
+ if (!wxStrcmp(SQLState, wxT("08002")))
+ return(DB_ERR_CONNECTION_IN_USE);
+ if (!wxStrcmp(SQLState, wxT("08003")))
+ return(DB_ERR_CONNECTION_NOT_OPEN);
+ if (!wxStrcmp(SQLState, wxT("08004")))
+ return(DB_ERR_REJECTED_CONNECTION);
+ if (!wxStrcmp(SQLState, wxT("08007")))
+ return(DB_ERR_CONN_FAIL_IN_TRANS);
+ if (!wxStrcmp(SQLState, wxT("08S01")))
+ return(DB_ERR_COMM_LINK_FAILURE);
+ if (!wxStrcmp(SQLState, wxT("21S01")))
+ return(DB_ERR_INSERT_VALUE_LIST_MISMATCH);
+ if (!wxStrcmp(SQLState, wxT("21S02")))
+ return(DB_ERR_DERIVED_TABLE_MISMATCH);
+ if (!wxStrcmp(SQLState, wxT("22001")))
+ return(DB_ERR_STRING_RIGHT_TRUNC);
+ if (!wxStrcmp(SQLState, wxT("22003")))
+ return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG);
+ if (!wxStrcmp(SQLState, wxT("22005")))
+ return(DB_ERR_ERROR_IN_ASSIGNMENT);
+ if (!wxStrcmp(SQLState, wxT("22008")))
+ return(DB_ERR_DATETIME_FLD_OVERFLOW);
+ if (!wxStrcmp(SQLState, wxT("22012")))
+ return(DB_ERR_DIVIDE_BY_ZERO);
+ if (!wxStrcmp(SQLState, wxT("22026")))
+ return(DB_ERR_STR_DATA_LENGTH_MISMATCH);
+ if (!wxStrcmp(SQLState, wxT("23000")))
+ return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
+ if (!wxStrcmp(SQLState, wxT("24000")))
+ return(DB_ERR_INVALID_CURSOR_STATE);
+ if (!wxStrcmp(SQLState, wxT("25000")))
+ return(DB_ERR_INVALID_TRANS_STATE);
+ if (!wxStrcmp(SQLState, wxT("28000")))
+ return(DB_ERR_INVALID_AUTH_SPEC);
+ if (!wxStrcmp(SQLState, wxT("34000")))
+ return(DB_ERR_INVALID_CURSOR_NAME);
+ if (!wxStrcmp(SQLState, wxT("37000")))
+ return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL);
+ if (!wxStrcmp(SQLState, wxT("3C000")))
+ return(DB_ERR_DUPLICATE_CURSOR_NAME);
+ if (!wxStrcmp(SQLState, wxT("40001")))
+ return(DB_ERR_SERIALIZATION_FAILURE);
+ if (!wxStrcmp(SQLState, wxT("42000")))
+ return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2);
+ if (!wxStrcmp(SQLState, wxT("70100")))
+ return(DB_ERR_OPERATION_ABORTED);
+ if (!wxStrcmp(SQLState, wxT("IM001")))
+ return(DB_ERR_UNSUPPORTED_FUNCTION);
+ if (!wxStrcmp(SQLState, wxT("IM002")))
+ return(DB_ERR_NO_DATA_SOURCE);
+ if (!wxStrcmp(SQLState, wxT("IM003")))
+ return(DB_ERR_DRIVER_LOAD_ERROR);
+ if (!wxStrcmp(SQLState, wxT("IM004")))
+ return(DB_ERR_SQLALLOCENV_FAILED);
+ if (!wxStrcmp(SQLState, wxT("IM005")))
+ return(DB_ERR_SQLALLOCCONNECT_FAILED);
+ if (!wxStrcmp(SQLState, wxT("IM006")))
+ return(DB_ERR_SQLSETCONNECTOPTION_FAILED);
+ if (!wxStrcmp(SQLState, wxT("IM007")))
+ return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB);
+ if (!wxStrcmp(SQLState, wxT("IM008")))
+ return(DB_ERR_DIALOG_FAILED);
+ if (!wxStrcmp(SQLState, wxT("IM009")))
+ return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL);
+ if (!wxStrcmp(SQLState, wxT("IM010")))
+ return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG);
+ if (!wxStrcmp(SQLState, wxT("IM011")))
+ return(DB_ERR_DRIVER_NAME_TOO_LONG);
+ if (!wxStrcmp(SQLState, wxT("IM012")))
+ return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR);
+ if (!wxStrcmp(SQLState, wxT("IM013")))
+ return(DB_ERR_TRACE_FILE_ERROR);
+ if (!wxStrcmp(SQLState, wxT("S0001")))
+ return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS);
+ if (!wxStrcmp(SQLState, wxT("S0002")))
+ return(DB_ERR_TABLE_NOT_FOUND);
+ if (!wxStrcmp(SQLState, wxT("S0011")))
+ return(DB_ERR_INDEX_ALREADY_EXISTS);
+ if (!wxStrcmp(SQLState, wxT("S0012")))
+ return(DB_ERR_INDEX_NOT_FOUND);
+ if (!wxStrcmp(SQLState, wxT("S0021")))
+ return(DB_ERR_COLUMN_ALREADY_EXISTS);
+ if (!wxStrcmp(SQLState, wxT("S0022")))
+ return(DB_ERR_COLUMN_NOT_FOUND);
+ if (!wxStrcmp(SQLState, wxT("S0023")))
+ return(DB_ERR_NO_DEFAULT_FOR_COLUMN);
+ if (!wxStrcmp(SQLState, wxT("S1000")))
+ return(DB_ERR_GENERAL_ERROR);
+ if (!wxStrcmp(SQLState, wxT("S1001")))
+ return(DB_ERR_MEMORY_ALLOCATION_FAILURE);
+ if (!wxStrcmp(SQLState, wxT("S1002")))
+ return(DB_ERR_INVALID_COLUMN_NUMBER);
+ if (!wxStrcmp(SQLState, wxT("S1003")))
+ return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1004")))
+ return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1008")))
+ return(DB_ERR_OPERATION_CANCELLED);
+ if (!wxStrcmp(SQLState, wxT("S1009")))
+ return(DB_ERR_INVALID_ARGUMENT_VALUE);
+ if (!wxStrcmp(SQLState, wxT("S1010")))
+ return(DB_ERR_FUNCTION_SEQUENCE_ERROR);
+ if (!wxStrcmp(SQLState, wxT("S1011")))
+ return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME);
+ if (!wxStrcmp(SQLState, wxT("S1012")))
+ return(DB_ERR_INVALID_TRANS_OPERATION_CODE);
+ if (!wxStrcmp(SQLState, wxT("S1015")))
+ return(DB_ERR_NO_CURSOR_NAME_AVAIL);
+ if (!wxStrcmp(SQLState, wxT("S1090")))
+ return(DB_ERR_INVALID_STR_OR_BUF_LEN);
+ if (!wxStrcmp(SQLState, wxT("S1091")))
+ return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1092")))
+ return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1093")))
+ return(DB_ERR_INVALID_PARAM_NO);
+ if (!wxStrcmp(SQLState, wxT("S1094")))
+ return(DB_ERR_INVALID_SCALE_VALUE);
+ if (!wxStrcmp(SQLState, wxT("S1095")))
+ return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1096")))
+ return(DB_ERR_INF_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1097")))
+ return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1098")))
+ return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1099")))
+ return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1100")))
+ return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1101")))
+ return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1103")))
+ return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1104")))
+ return(DB_ERR_INVALID_PRECISION_VALUE);
+ if (!wxStrcmp(SQLState, wxT("S1105")))
+ return(DB_ERR_INVALID_PARAM_TYPE);
+ if (!wxStrcmp(SQLState, wxT("S1106")))
+ return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1107")))
+ return(DB_ERR_ROW_VALUE_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1108")))
+ return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE);
+ if (!wxStrcmp(SQLState, wxT("S1109")))
+ return(DB_ERR_INVALID_CURSOR_POSITION);
+ if (!wxStrcmp(SQLState, wxT("S1110")))
+ return(DB_ERR_INVALID_DRIVER_COMPLETION);
+ if (!wxStrcmp(SQLState, wxT("S1111")))
+ return(DB_ERR_INVALID_BOOKMARK_VALUE);
+ if (!wxStrcmp(SQLState, wxT("S1C00")))
+ return(DB_ERR_DRIVER_NOT_CAPABLE);
+ if (!wxStrcmp(SQLState, wxT("S1T00")))
+ return(DB_ERR_TIMEOUT_EXPIRED);
+
+ // No match
+ return(0);
+
+} // wxDb::TranslateSqlState()
+
+
+/********** wxDb::Grant() **********/
+bool wxDb::Grant(int privileges, const wxString &tableName, const wxString &userList)
+{
+ wxString sqlStmt;
+
+ // Build the grant statement
+ sqlStmt = wxT("GRANT ");
+ if (privileges == DB_GRANT_ALL)
+ sqlStmt += wxT("ALL");
+ else
+ {
+ int c = 0;
+ if (privileges & DB_GRANT_SELECT)
+ {
+ sqlStmt += wxT("SELECT");
+ c++;
+ }
+ if (privileges & DB_GRANT_INSERT)
+ {
+ if (c++)
+ sqlStmt += wxT(", ");
+ sqlStmt += wxT("INSERT");
+ }
+ if (privileges & DB_GRANT_UPDATE)
+ {
+ if (c++)
+ sqlStmt += wxT(", ");
+ sqlStmt += wxT("UPDATE");
+ }
+ if (privileges & DB_GRANT_DELETE)
+ {
+ if (c++)
+ sqlStmt += wxT(", ");
+ sqlStmt += wxT("DELETE");
+ }
+ }
+
+ sqlStmt += wxT(" ON ");
+ sqlStmt += tableName;
+ sqlStmt += wxT(" TO ");
+ sqlStmt += userList;
+
+#ifdef DBDEBUG_CONSOLE
+ cout << endl << sqlStmt.c_str() << endl;
+#endif
+
+ WriteSqlLog(sqlStmt);
+
+ return(ExecSql(sqlStmt));
+
+} // wxDb::Grant()
+
+
+/********** wxDb::CreateView() **********/
+bool wxDb::CreateView(const wxString &viewName, const wxString &colList,
+ const wxString &pSqlStmt, bool attemptDrop)
+{
+ wxString sqlStmt;
+
+ // Drop the view first
+ if (attemptDrop && !DropView(viewName))
+ return FALSE;
+
+ // Build the create view statement
+ sqlStmt = wxT("CREATE VIEW ");
+ sqlStmt += viewName;
+
+ if (colList.Length())
+ {
+ sqlStmt += wxT(" (");
+ sqlStmt += colList;
+ sqlStmt += wxT(")");
+ }
+
+ sqlStmt += wxT(" AS ");
+ sqlStmt += pSqlStmt;
+
+ WriteSqlLog(sqlStmt);
+
+#ifdef DBDEBUG_CONSOLE
+ cout << sqlStmt.c_str() << endl;
+#endif
+
+ return(ExecSql(sqlStmt));
+
+} // wxDb::CreateView()
+
+
+/********** wxDb::DropView() **********/
+bool wxDb::DropView(const wxString &viewName)