// SYNOPSIS START
// SYNOPSIS STOP
*/
+#ifdef __GNUG__
+ #pragma implementation "dbtable.h"
+#endif
-// Use this line for wxWindows v1.x
-//#include "wx_ver.h"
-// Use this line for wxWindows v2.x
#include "wx/wxprec.h"
-#include "wx/version.h"
-#if wxMAJOR_VERSION == 2
- #ifdef __GNUG__
- #pragma implementation "dbtable.h"
- #endif
+#ifdef __BORLANDC__
+ #pragma hdrstop
#endif
#ifdef DBDEBUG_CONSOLE
+ #include "iostream.h"
#include "wx/ioswrap.h"
#endif
-
-#ifdef __BORLANDC__
- #pragma hdrstop
-#endif //__BORLANDC__
-
-#if wxMAJOR_VERSION == 2
- #ifndef WX_PRECOMP
- #include "wx/string.h"
- #include "wx/object.h"
- #include "wx/list.h"
- #include "wx/utils.h"
- #include "wx/msgdlg.h"
- #include "wx/log.h"
- #endif
- #include "wx/filefn.h"
-#endif
-
-#if wxMAJOR_VERSION == 1
-# if defined(wx_msw) || defined(wx_x)
-# ifdef WX_PRECOMP
-# include "wx_prec.h"
-# else
-# include "wx.h"
-# endif
-# endif
-# define wxUSE_ODBC 1
+#ifndef WX_PRECOMP
+ #include "wx/string.h"
+ #include "wx/object.h"
+ #include "wx/list.h"
+ #include "wx/utils.h"
+ #include "wx/msgdlg.h"
+ #include "wx/log.h"
#endif
-
+#include "wx/filefn.h"
#if wxUSE_ODBC
#include <string.h>
//#include <assert.h>
-#if wxMAJOR_VERSION == 1
- #include "table.h"
-#elif wxMAJOR_VERSION == 2
- #include "wx/dbtable.h"
-#endif
+#include "wx/dbtable.h"
#ifdef __UNIX__
// The HPUX preprocessor lines below were commented out on 8/20/97
SqlCtype = SQL_C_LONG;
PtrDataObj = NULL;
SzDataObj = 0;
- KeyField = FALSE;
- Updateable = FALSE;
- InsertAllowed = FALSE;
- DerivedCol = FALSE;
+ KeyField = false;
+ Updateable = false;
+ InsertAllowed = false;
+ DerivedCol = false;
CbValue = 0;
- Null = FALSE;
+ Null = false;
- return TRUE;
+ return true;
} // wxDbColDef::Initialize()
henv = 0;
hdbc = 0;
hstmt = 0;
+ m_hstmtGridQuery = 0;
hstmtDefault = 0; // Initialized below
hstmtCount = 0; // Initialized first time it is needed
hstmtInsert = 0;
where.Empty(); // Where clause
orderBy.Empty(); // Order By clause
from.Empty(); // From clause
- selectForUpdate = FALSE; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
+ selectForUpdate = false; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
queryOnly = qryOnly;
- insertable = TRUE;
+ insertable = true;
tablePath.Empty();
tableName.Empty();
queryTableName.Empty();
wxASSERT(pDb);
if (!pDb)
- return FALSE;
+ return false;
tableName = tblName; // Table Name
if (tblPath.Length())
wxString s;
tableID = ++lastTableID;
s.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName.c_str(), tableID, pDb);
-
+
#ifdef __WXDEBUG__
wxTablesInUse *tableInUse;
tableInUse = new wxTablesInUse();
{
// Should never happen
pDb->GetNextError(henv, hdbc, hstmtInternal);
- return FALSE;
+ return false;
}
}
}
}
// Make the default cursor the active cursor
- hstmtDefault = GetNewCursor(FALSE,FALSE);
+ hstmtDefault = GetNewCursor(false,false);
wxASSERT(hstmtDefault);
hstmt = *hstmtDefault;
- return TRUE;
+ return true;
} // wxDbTable::initialize()
#ifdef __WXDEBUG__
if (tableID)
{
- TablesInUse.DeleteContents(TRUE);
- bool found = FALSE;
+ TablesInUse.DeleteContents(true);
+ bool found = false;
wxNode *pNode;
pNode = TablesInUse.First();
{
if (((wxTablesInUse *)pNode->Data())->tableID == tableID)
{
- found = TRUE;
+ found = true;
if (!TablesInUse.DeleteNode(pNode))
wxLogDebug (s,wxT("Unable to delete node!"));
}
if (!found)
{
wxString msg;
- msg.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s);
+ 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..."));
}
}
if (hstmtCount)
DeleteCursor(hstmtCount);
+
+ if (m_hstmtGridQuery)
+ DeleteCursor(m_hstmtGridQuery);
+
} // wxDbTable::cleanup()
/***************************** PRIVATE FUNCTIONS *****************************/
-/********** wxDbTable::bindUpdateParams() **********/
+/********** wxDbTable::bindParams() **********/
bool wxDbTable::bindParams(bool forUpdate)
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
SWORD fSqlType = 0;
UDWORD precision = 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++)
+ UWORD colNo;
+
+ for (i=0, colNo=1; i < noCols; i++)
{
if (forUpdate)
{
- if (! colDefs[i].Updateable)
+ if (!colDefs[i].Updateable)
continue;
}
else
{
- if (! colDefs[i].InsertAllowed)
+ if (!colDefs[i].InsertAllowed)
continue;
}
}
// Completed successfully
- return(TRUE);
+ return(true);
} // wxDbTable::bindParams()
/********** wxDbTable::bindInsertParams() **********/
bool wxDbTable::bindInsertParams(void)
{
- return bindParams(FALSE);
+ return bindParams(false);
} // wxDbTable::bindInsertParams()
/********** wxDbTable::bindUpdateParams() **********/
bool wxDbTable::bindUpdateParams(void)
{
- return bindParams(TRUE);
+ return bindParams(true);
} // wxDbTable::bindUpdateParams()
/********** wxDbTable::bindCols() **********/
bool wxDbTable::bindCols(HSTMT cursor)
{
-//RG-NULL static SDWORD cb;
-
// Bind each column of the table to a memory address for fetching data
UWORD i;
for (i = 0; i < noCols; i++)
}
// Completed successfully
- return(TRUE);
+ return(true);
} // wxDbTable::bindCols()
UWORD rowStatus;
retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
- if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
+ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
if (retcode == SQL_NO_DATA_FOUND)
- return(FALSE);
+ return(false);
else
return(pDb->DispAllErrors(henv, hdbc, hstmt));
}
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
if (retcode == SQL_NO_DATA_FOUND)
- return(FALSE);
+ return(false);
else
return(pDb->DispAllErrors(henv, hdbc, hstmt));
}
}
// Completed successfully
- return(TRUE);
+ return(true);
} // wxDbTable::getRec()
/********** wxDbTable::execDelete() **********/
bool wxDbTable::execDelete(const wxString &pSqlStmt)
{
+ RETCODE retcode;
+
// Execute the DELETE statement
- if (SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
- return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
+ retcode = SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
- // Record deleted successfully
- return(TRUE);
+ 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
- if (SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
- return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
+ retcode = SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
- // Record deleted successfully
- return(TRUE);
+ 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()
// The user may wish to select for update, but the DBMS may not be capable
selectForUpdate = CanSelectForUpdate();
else
- selectForUpdate = FALSE;
+ selectForUpdate = false;
// Set the SQL SELECT string
if (queryType != DB_SELECT_STATEMENT) // A select statement was not passed in,
BuildSelectStmt(sqlStmt, queryType, distinct);
pDb->WriteSqlLog(sqlStmt);
}
-/*
- This is the block of code that got added during the 2.2.1 merge with
- the 2.2 main branch that somehow got added here when it should not have. - gt
- 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);
+ return(false);
// Execute the SQL SELECT statement
int retcode;
return(pDb->DispAllErrors(henv, hdbc, hstmt));
// Completed successfully
- return(TRUE);
+ return(true);
} // wxDbTable::query()
bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists)
{
if (!pDb)
- return FALSE;
+ return false;
int i;
wxString sqlStmt;
wxString s;
+// int NumKeyCols=0;
+
+ // Calculate the maximum size of the concatenated
+ // keys for use with wxDbGrid
+ m_keysize = 0;
+ for (i=0; i < noCols; i++)
+ {
+ if (colDefs[i].KeyField)
+ {
+// NumKeyCols++;
+ m_keysize += colDefs[i].SzDataObj;
+ }
+ }
s.Empty();
// Verify that the table exists in the database
p += s;
pDb->LogError(p.GetData());
- return(FALSE);
+ return(false);
}
// Bind the member variables for field exchange between
if (!queryOnly)
{
if (!bindInsertParams()) // Inserts
- return(FALSE);
+ return(false);
if (!bindUpdateParams()) // Updates
- return(FALSE);
+ return(false);
}
if (!bindCols(*hstmtDefault)) // Selects
- return(FALSE);
+ return(false);
if (!bindCols(hstmtInternal)) // Internal use only
- return(FALSE);
+ return(false);
/*
* Do NOT bind the hstmtCount cursor!!!
// Build an insert statement using parameter markers
if (!queryOnly && noCols > 0)
{
- bool needComma = FALSE;
+ bool needComma = false;
sqlStmt.Printf(wxT("INSERT INTO %s ("), tableName.c_str());
for (i = 0; i < noCols; i++)
{
if (needComma)
sqlStmt += wxT(",");
sqlStmt += colDefs[i].ColName;
- needComma = TRUE;
+ needComma = true;
}
- needComma = FALSE;
+ needComma = false;
sqlStmt += wxT(") VALUES (");
int insertableCount = 0;
if (needComma)
sqlStmt += wxT(",");
sqlStmt += wxT("?");
- needComma = TRUE;
+ needComma = true;
insertableCount++;
}
sqlStmt += wxT(")");
return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
}
else
- insertable= FALSE;
+ insertable= false;
}
// Completed successfully
- return(TRUE);
+ return(true);
} // wxDbTable::Open()
{
pDb->WriteSqlLog(pSqlStmt);
- return(query(DB_SELECT_STATEMENT, FALSE, FALSE, pSqlStmt));
+ return(query(DB_SELECT_STATEMENT, false, false, pSqlStmt));
} // wxDbTable::QueryBySqlStmt()
if (pDb->FwdOnlyCursors())
{
wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
- return FALSE;
+ return false;
}
else
return(getRec(SQL_FETCH_PRIOR));
if (pDb->FwdOnlyCursors())
{
wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
- return FALSE;
+ return false;
}
else
return(getRec(SQL_FETCH_PRIOR));
if (pDb->FwdOnlyCursors())
{
wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
- return FALSE;
+ return false;
}
else
return(getRec(SQL_FETCH_FIRST));
if (pDb->FwdOnlyCursors())
{
wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
- return FALSE;
+ return false;
}
else
return(getRec(SQL_FETCH_LAST));
// Was a FROM clause specified to join tables to the base table?
// Available for ::Query() only!!!
- bool appendFromClause = FALSE;
+ bool appendFromClause = false;
#if wxODBC_BACKWARD_COMPATABILITY
if (typeOfSelect == DB_SELECT_WHERE && from && wxStrlen(from))
- appendFromClause = TRUE;
+ appendFromClause = true;
#else
if (typeOfSelect == DB_SELECT_WHERE && from.Length())
- appendFromClause = TRUE;
+ appendFromClause = true;
#endif
// Add the column list
for (i = 0; i < noCols; i++)
{
// If joining tables, the base table column names must be qualified to avoid ambiguity
- if (appendFromClause)
+ if (appendFromClause || pDb->Dbms() == dbmsACCESS)
{
pSqlStmt += queryTableName;
pSqlStmt += wxT(".");
if (!distinct && CanUpdByROWID())
{
// If joining tables, the base table column names must be qualified to avoid ambiguity
- if (appendFromClause)
+ if (appendFromClause || pDb->Dbms() == dbmsACCESS)
{
pSqlStmt += wxT(",");
pSqlStmt += queryTableName;
wxString whereClause;
whereClause.Empty();
- bool firstColumn = TRUE;
+ bool firstColumn = true;
- pSqlStmt.Printf(wxT("UPDATE %s SET "), tableName.c_str());
+ pSqlStmt.Printf(wxT("UPDATE %s SET "), tableName.Upper().c_str());
// Append a list of columns to be updated
int i;
// Only append Updateable columns
if (colDefs[i].Updateable)
{
- if (! firstColumn)
+ if (!firstColumn)
pSqlStmt += wxT(",");
else
- firstColumn = FALSE;
+ firstColumn = false;
pSqlStmt += colDefs[i].ColName;
pSqlStmt += wxT(" = ?");
}
* They are not included as part of the where clause.
*/
{
- bool moreThanOneColumn = FALSE;
+ bool moreThanOneColumn = false;
wxString colValue;
// Loop through the columns building a where clause as you go
if (moreThanOneColumn)
pWhereClause += wxT(" AND ");
else
- moreThanOneColumn = TRUE;
+ moreThanOneColumn = true;
// Concatenate where phrase for the column
if (qualTableName.Length())
{
return(pDb->DispAllErrors(henv, hdbc, cursor));
// Completed successfully
- return(TRUE);
+ return(true);
} // wxDbTable::CloseCursor()
bool wxDbTable::CreateTable(bool attemptDrop)
{
if (!pDb)
- return FALSE;
+ return false;
int i, j;
wxString sqlStmt;
// Drop table first
if (attemptDrop && !DropTable())
- return FALSE;
+ return false;
// Create the table
#ifdef DBDEBUG_CONSOLE
switch(colDefs[i].DbDataType)
{
case DB_DATA_TYPE_VARCHAR:
- cout << pDb->typeInfVarchar.TypeName << wxT("(") << colDefs[i].SzDataObj << wxT(")");
+ cout << pDb->GetTypeInfVarchar().TypeName << wxT("(") << colDefs[i].SzDataObj << wxT(")");
break;
case DB_DATA_TYPE_INTEGER:
- cout << pDb->typeInfInteger.TypeName;
+ cout << pDb->GetTypeInfInteger().TypeName;
break;
case DB_DATA_TYPE_FLOAT:
- cout << pDb->typeInfFloat.TypeName;
+ cout << pDb->GetTypeInfFloat().TypeName;
break;
case DB_DATA_TYPE_DATE:
- cout << pDb->typeInfDate.TypeName;
+ cout << pDb->GetTypeInfDate().TypeName;
break;
case DB_DATA_TYPE_BLOB:
- cout << pDb->typeInfBlob.TypeName;
+ cout << pDb->GetTypeInfBlob().TypeName;
break;
}
cout << endl;
#endif
// Build a CREATE TABLE string from the colDefs structure.
- bool needComma = FALSE;
+ bool needComma = false;
sqlStmt.Printf(wxT("CREATE TABLE %s ("), tableName.c_str());
for (i = 0; i < noCols; i++)
if (pDb->Dbms() == dbmsDB2 ||
pDb->Dbms() == dbmsMY_SQL ||
pDb->Dbms() == dbmsSYBASE_ASE ||
+ pDb->Dbms() == dbmsINTERBASE ||
pDb->Dbms() == dbmsMS_SQL_SERVER)
{
if (colDefs[i].KeyField)
}
}
- needComma = TRUE;
+ needComma = true;
}
// If there is a primary key defined, include it in the create statement
for (i = j = 0; i < noCols; i++)
}
if (j && pDb->Dbms() != dbmsDBASE) // Found a keyfield
{
- if (pDb->Dbms() != dbmsMY_SQL)
- {
- sqlStmt += wxT(",CONSTRAINT ");
- sqlStmt += tableName;
- sqlStmt += wxT("_PIDX PRIMARY KEY (");
- }
- else
+ switch (pDb->Dbms())
{
- /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
- sqlStmt += wxT(", PRIMARY KEY (");
+ case dbmsINFORMIX:
+ case dbmsSYBASE_ASA:
+ case dbmsSYBASE_ASE:
+ case dbmsMY_SQL:
+ {
+ // MySQL goes out on this one. We also declare the relevant key NON NULL above
+ sqlStmt += wxT(",PRIMARY KEY (");
+ break;
+ }
+ default:
+ {
+ sqlStmt += wxT(",CONSTRAINT ");
+ // DB2 is limited to 18 characters for index names
+ if (pDb->Dbms() == dbmsDB2)
+ {
+ wxASSERT_MSG((tableName && wxStrlen(tableName) <= 13), wxT("DB2 table/index names must be no longer than 13 characters in length.\n\nTruncating table name to 13 characters."));
+ sqlStmt += tableName.substr(0, 13);
+ }
+ else
+ sqlStmt += tableName;
+
+ sqlStmt += wxT("_PIDX PRIMARY KEY (");
+ break;
+ }
}
// List column name(s) of column(s) comprising the primary key
sqlStmt += colDefs[i].ColName;
}
}
- sqlStmt += wxT(")");
+ sqlStmt += wxT(")");
+
+ if (pDb->Dbms() == dbmsINFORMIX ||
+ pDb->Dbms() == dbmsSYBASE_ASA ||
+ pDb->Dbms() == dbmsSYBASE_ASE)
+ {
+ sqlStmt += wxT(" CONSTRAINT ");
+ sqlStmt += tableName;
+ sqlStmt += wxT("_PIDX");
+ }
}
// Append the closing parentheses for the create table statement
sqlStmt += wxT(")");
pDb->DispAllErrors(henv, hdbc, hstmt);
pDb->RollbackTrans();
CloseCursor(hstmt);
- return(FALSE);
+ return(false);
}
// Commit the transaction and close the cursor
if (!pDb->CommitTrans())
- return(FALSE);
+ return(false);
if (!CloseCursor(hstmt))
- return(FALSE);
+ return(false);
// Database table created successfully
- return(TRUE);
+ return(true);
} // wxDbTable::CreateTable()
/********** wxDbTable::DropTable() **********/
bool wxDbTable::DropTable()
{
- // NOTE: This function returns TRUE if the Table does not exist, but
+ // NOTE: This function returns true if the Table does not exist, but
// only for identified databases. Code will need to be added
// below for any other databases when those databases are defined
// to handle this situation consistently
cout << endl << sqlStmt.c_str() << endl;
#endif
- if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+
+
+
+ RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
+ if (retcode != SQL_SUCCESS)
{
// Check for "Base table not found" error and ignore
pDb->GetNextError(henv, hdbc, hstmt);
- if (wxStrcmp(pDb->sqlState, wxT("S0002")) &&
- wxStrcmp(pDb->sqlState, wxT("S1000"))) // "Base table not found"
- {
+ if (wxStrcmp(pDb->sqlState, wxT("S0002")) /*&&
+ wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
+ {
// Check for product specific error codes
- if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // 5.x (and lower?)
- (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
- (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))))
+ if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // 5.x (and lower?)
+ (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
+ (pDb->Dbms() == dbmsPERVASIVE_SQL && !wxStrcmp(pDb->sqlState,wxT("S1000"))) || // Returns an S1000 then an S0002
+ (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))))
{
pDb->DispNextError();
pDb->DispAllErrors(henv, hdbc, hstmt);
pDb->RollbackTrans();
- CloseCursor(hstmt);
- return(FALSE);
+// CloseCursor(hstmt);
+ return(false);
}
}
}
// Commit the transaction and close the cursor
if (! pDb->CommitTrans())
- return(FALSE);
+ return(false);
if (! CloseCursor(hstmt))
- return(FALSE);
+ return(false);
- return(TRUE);
+ return(true);
} // wxDbTable::DropTable()
/********** wxDbTable::CreateIndex() **********/
-bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, int noIdxCols, wxDbIdxDef *pIdxDefs, bool attemptDrop)
+bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCols,
+ wxDbIdxDef *pIdxDefs, bool attemptDrop)
{
wxString sqlStmt;
// Drop the index first
if (attemptDrop && !DropIndex(idxName))
- return (FALSE);
+ return (false);
// MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
// of an index have the columns defined as "NOT NULL". During initial table creation though,
{
wxString sqlStmt;
int i;
- bool ok = TRUE;
+ bool ok = true;
for (i = 0; i < noIdxCols && ok; i++)
{
int j = 0;
- bool found = FALSE;
+ bool found = false;
// Find the column definition that has the ColName that matches the
// index column name. We need to do this to get the DB_DATA_TYPE of
// the index column, as MySQL's syntax for the ALTER column requires
while (!found && (j < this->noCols))
{
if (wxStrcmp(colDefs[j].ColName,pIdxDefs[i].ColName) == 0)
- found = TRUE;
+ found = true;
if (!found)
j++;
}
}
}
else
- ok = FALSE;
+ ok = false;
}
if (ok)
pDb->CommitTrans();
else
{
pDb->RollbackTrans();
- return(FALSE);
+ return(false);
}
}
for (i = 0; i < noIdxCols; i++)
{
sqlStmt += pIdxDefs[i].ColName;
- /* Postgres doesn't cope with ASC */
- if (pDb->Dbms() != dbmsPOSTGRES)
+
+ // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
+ if (!((pDb->Dbms() == dbmsMS_SQL_SERVER) && (strncmp(pDb->dbInf.dbmsVer,"07",2)==0)) &&
+ !(pDb->Dbms() == dbmsPOSTGRES))
{
if (pIdxDefs[i].Ascending)
sqlStmt += wxT(" ASC");
else
sqlStmt += wxT(" DESC");
}
+ else
+ wxASSERT_MSG(pIdxDefs[i].Ascending, "Datasource does not support DESCending index columns");
if ((i + 1) < noIdxCols)
sqlStmt += wxT(",");
pDb->DispAllErrors(henv, hdbc, hstmt);
pDb->RollbackTrans();
CloseCursor(hstmt);
- return(FALSE);
+ return(false);
}
// Commit the transaction and close the cursor
if (! pDb->CommitTrans())
- return(FALSE);
+ return(false);
if (! CloseCursor(hstmt))
- return(FALSE);
+ return(false);
// Index Created Successfully
- return(TRUE);
+ return(true);
} // wxDbTable::CreateIndex()
/********** wxDbTable::DropIndex() **********/
bool wxDbTable::DropIndex(const wxString &idxName)
{
- // NOTE: This function returns TRUE if the Index does not exist, but
+ // NOTE: This function returns true if the Index does not exist, but
// only for identified databases. Code will need to be added
- // below for any other databases when those databases are defined
+ // below for any other databases when those databases are defined
// to handle this situation consistently
wxString sqlStmt;
- if (pDb->Dbms() == dbmsACCESS || pDb->Dbms() == dbmsMY_SQL)
+ if (pDb->Dbms() == dbmsACCESS || pDb->Dbms() == dbmsMY_SQL ||
+ pDb->Dbms() == dbmsDBASE /*|| Paradox needs this syntax too when we add support*/)
sqlStmt.Printf(wxT("DROP INDEX %s ON %s"),idxName.c_str(), tableName.c_str());
else if ((pDb->Dbms() == dbmsMS_SQL_SERVER) ||
(pDb->Dbms() == dbmsSYBASE_ASE))
if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // v5.x (and lower?)
(pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
(pDb->Dbms() == dbmsMS_SQL_SERVER && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
+ (pDb->Dbms() == dbmsINTERBASE && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
(pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("S0002"))) || // Base table not found
(pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
(pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))
pDb->DispAllErrors(henv, hdbc, hstmt);
pDb->RollbackTrans();
CloseCursor(hstmt);
- return(FALSE);
+ return(false);
}
}
}
// Commit the transaction and close the cursor
if (! pDb->CommitTrans())
- return(FALSE);
+ return(false);
if (! CloseCursor(hstmt))
- return(FALSE);
+ return(false);
- return(TRUE);
+ return(true);
} // wxDbTable::DropIndex()
/********** wxDbTable::SetOrderByColNums() **********/
-bool wxDbTable::SetOrderByColNums(int first, ... )
+bool wxDbTable::SetOrderByColNums(UWORD first, ... )
{
- int colNo = first;
+ int colNo = first; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
va_list argptr;
- bool abort = FALSE;
+ bool abort = false;
wxString tempStr;
va_start(argptr, first); /* Initialize variable arguments. */
// Valid columns are 0 thru noCols-1
if (colNo >= noCols || colNo < 0)
{
- abort = TRUE;
+ abort = true;
continue;
}
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
wxString sqlStmt;
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
pDb->WriteSqlLog(pSqlStmt);
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
wxString sqlStmt;
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
wxString sqlStmt;
sqlStmt.Empty();
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
wxString sqlStmt;
sqlStmt.Empty();
{
wxASSERT(!queryOnly);
if (queryOnly)
- return(FALSE);
+ return(false);
wxString sqlStmt;
sqlStmt.Empty();
/********** wxDbTable::IsColNull() **********/
-bool wxDbTable::IsColNull(int colNo)
+bool wxDbTable::IsColNull(UWORD colNo) const
{
/*
- This logic is just not right. It would indicate TRUE
+ This logic is just not right. It would indicate true
if a numeric field were set to a value of 0.
switch(colDefs[colNo].SqlCtype)
TIMESTAMP_STRUCT *pDt;
pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
- return(TRUE);
+ return(true);
else
- return(FALSE);
+ return(false);
default:
- return(TRUE);
+ return(true);
}
*/
return (colDefs[colNo].Null);
bool wxDbTable::CanSelectForUpdate(void)
{
if (queryOnly)
- return FALSE;
+ return false;
if (pDb->Dbms() == dbmsMY_SQL)
- return FALSE;
+ return false;
if ((pDb->Dbms() == dbmsORACLE) ||
(pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE))
- return(TRUE);
+ return(true);
else
- return(FALSE);
+ return(false);
} // wxDbTable::CanSelectForUpdate()
bool wxDbTable::CanUpdByROWID(void)
{
/*
- * NOTE: Returning FALSE for now until this can be debugged,
+ * NOTE: Returning false for now until this can be debugged,
* as the ROWID is not getting updated correctly
*/
- return FALSE;
+ return false;
/*
if (pDb->Dbms() == dbmsORACLE)
- return(TRUE);
+ return(true);
else
- return(FALSE);
+ return(false);
*/
} // wxDbTable::CanUpdByROWID()
bool wxDbTable::IsCursorClosedOnCommit(void)
{
if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
- return(FALSE);
+ return(false);
else
- return(TRUE);
+ return(true);
} // wxDbTable::IsCursorClosedOnCommit()
/********** wxDbTable::ClearMemberVar() **********/
-void wxDbTable::ClearMemberVar(int colNo, bool setToNull)
+void wxDbTable::ClearMemberVar(UWORD colNo, bool setToNull)
{
wxASSERT(colNo < noCols);
return(pDb->DispAllErrors(henv, hdbc, hstmtInternal));
// Completed Successfully
- return(TRUE);
+ return(true);
} // wxDbTable::SetQueryTimeout()
/********** wxDbTable::SetColDefs() **********/
-void wxDbTable::SetColDefs(int index, const wxString &fieldName, int dataType, void *pData,
+void wxDbTable::SetColDefs(UWORD index, const wxString &fieldName, int dataType, void *pData,
SWORD cType, int size, bool keyField, bool upd,
bool insAllow, bool derivedCol)
{
if (fieldName.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
{
- wxStrncpy (colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
+ int assertColumnNameTooLong = 0;
+ wxStrncpy(colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;
+ wxString tmpMsg;
+ tmpMsg.Printf("Column name '%s' is too long. Truncated to '%s'.",fieldName.c_str(),colDefs[index].ColName);
+ wxASSERT_MSG(assertColumnNameTooLong,tmpMsg.c_str());
}
else
wxStrcpy(colDefs[index].ColName, fieldName);
// Derived columns by definition would NOT be "Insertable" or "Updateable"
if (derivedCol)
{
- colDefs[index].Updateable = FALSE;
- colDefs[index].InsertAllowed = FALSE;
+ colDefs[index].Updateable = false;
+ colDefs[index].InsertAllowed = false;
}
else
{
colDefs[index].InsertAllowed = insAllow;
}
- colDefs[index].Null = FALSE;
+ colDefs[index].Null = false;
} // wxDbTable::SetColDefs()
/********** wxDbTable::SetColDefs() **********/
-wxDbColDataPtr* wxDbTable::SetColDefs(wxDbColInf *pColInfs, ULONG numCols)
+wxDbColDataPtr* wxDbTable::SetColDefs(wxDbColInf *pColInfs, UWORD numCols)
{
wxASSERT(pColInfs);
wxDbColDataPtr *pColDataPtrs = NULL;
if (pColInfs)
{
- ULONG index;
+ UWORD index;
pColDataPtrs = new wxDbColDataPtr[numCols+1];
pColDataPtrs[index].SqlCtype = SQL_C_TIMESTAMP;
break;
case DB_DATA_TYPE_BLOB:
- int notSupportedYet = 0;
+ int notSupportedYet = 0;
wxASSERT_MSG(notSupportedYet, wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
pColDataPtrs[index].PtrDataObj = /*BLOB ADDITION NEEDED*/NULL;
pColDataPtrs[index].SzDataObj = /*BLOB ADDITION NEEDED*/sizeof(void *);
// Initialize the Count cursor if it's not already initialized
if (!hstmtCount)
{
- hstmtCount = GetNewCursor(FALSE,FALSE);
+ hstmtCount = GetNewCursor(false,false);
wxASSERT(hstmtCount);
if (!hstmtCount)
return(0);
/********** wxDbTable::Refresh() **********/
bool wxDbTable::Refresh(void)
{
- bool result = TRUE;
+ bool result = true;
// Switch to the internal cursor so any active cursors are not corrupted
HSTMT currCursor = GetCursor();
where = whereClause;
orderBy.Empty();
if (!Query())
- result = FALSE;
+ result = false;
if (result && !GetNext())
- result = FALSE;
+ result = false;
// Switch back to original cursor
SetCursor(&currCursor);
} // wxDbTable::Refresh()
-/********** wxDbTable::SetColNull(int colNo, bool set) **********/
-bool wxDbTable::SetColNull(int colNo, bool set)
+/********** wxDbTable::SetColNull() **********/
+bool wxDbTable::SetColNull(UWORD colNo, bool set)
{
if (colNo < noCols)
{
colDefs[colNo].Null = set;
if (set) // Blank out the values in the member variable
- ClearMemberVar(colNo,FALSE); // Must call with FALSE, or infinite recursion will happen
- return(TRUE);
+ ClearMemberVar(colNo,false); // Must call with false, or infinite recursion will happen
+ return(true);
}
else
- return(FALSE);
+ return(false);
} // wxDbTable::SetColNull()
-/********** wxDbTable::SetColNull(const wxString &colName, bool set) **********/
+/********** wxDbTable::SetColNull() **********/
bool wxDbTable::SetColNull(const wxString &colName, bool set)
{
int i;
{
colDefs[i].Null = set;
if (set) // Blank out the values in the member variable
- ClearMemberVar(i,FALSE); // Must call with FALSE, or infinite recursion will happen
- return(TRUE);
+ ClearMemberVar(i,false); // Must call with false, or infinite recursion will happen
+ return(true);
}
else
- return(FALSE);
+ return(false);
} // wxDbTable::SetColNull()
if (bindColumns)
{
- if(!bindCols(*newHSTMT))
+ if (!bindCols(*newHSTMT))
{
delete newHSTMT;
return(0);
/********** wxDbTable::DeleteCursor() **********/
bool wxDbTable::DeleteCursor(HSTMT *hstmtDel)
{
- bool result = TRUE;
+ bool result = true;
if (!hstmtDel) // Cursor already deleted
return(result);
if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
{
pDb->DispAllErrors(henv, hdbc);
- result = FALSE;
+ result = false;
}
delete hstmtDel;
} // wxDbTable::DeleteCursor()
+//////////////////////////////////////////////////////////////
+// wxDbGrid support functions
+//////////////////////////////////////////////////////////////
+
+void wxDbTable::SetRowMode(const rowmode_t rowmode)
+{
+ if (!m_hstmtGridQuery)
+ {
+ m_hstmtGridQuery = GetNewCursor(false,false);
+ if (!bindCols(*m_hstmtGridQuery))
+ return;
+ }
+
+ m_rowmode = rowmode;
+ switch (m_rowmode)
+ {
+ case WX_ROW_MODE_QUERY:
+ SetCursor(m_hstmtGridQuery);
+ break;
+ case WX_ROW_MODE_INDIVIDUAL:
+ SetCursor(hstmtDefault);
+ break;
+ default:
+ assert(0);
+ }
+} // wxDbTable::SetRowMode()
+
+
+wxVariant wxDbTable::GetCol(const int col) const
+{
+ wxVariant val;
+ if ((col < noCols) && (!IsColNull(col)))
+ {
+ switch (colDefs[col].SqlCtype)
+ {
+ case SQL_CHAR:
+ case SQL_VARCHAR:
+ val = (char *)(colDefs[col].PtrDataObj);
+ break;
+ case SQL_C_LONG:
+ case SQL_C_SLONG:
+ val = *(long *)(colDefs[col].PtrDataObj);
+ break;
+ case SQL_C_SHORT:
+ case SQL_C_SSHORT:
+ val = (long int )(*(short *)(colDefs[col].PtrDataObj));
+ break;
+ case SQL_C_ULONG:
+ val = (long)(*(unsigned long *)(colDefs[col].PtrDataObj));
+ break;
+ case SQL_C_TINYINT:
+ val = (long)(*(char *)(colDefs[col].PtrDataObj));
+ break;
+ case SQL_C_UTINYINT:
+ val = (long)(*(unsigned char *)(colDefs[col].PtrDataObj));
+ break;
+ case SQL_C_USHORT:
+ val = (long)(*(UWORD *)(colDefs[col].PtrDataObj));
+ break;
+ case SQL_C_DATE:
+ val = (DATE_STRUCT *)(colDefs[col].PtrDataObj);
+ break;
+ case SQL_C_TIME:
+ val = (TIME_STRUCT *)(colDefs[col].PtrDataObj);
+ break;
+ case SQL_C_TIMESTAMP:
+ val = (TIMESTAMP_STRUCT *)(colDefs[col].PtrDataObj);
+ break;
+ case SQL_C_DOUBLE:
+ val = *(double *)(colDefs[col].PtrDataObj);
+ break;
+ default:
+ assert(0);
+ }
+ }
+ return val;
+} // wxDbTable::GetCol()
+
+
+void csstrncpyt(char *s, const char *t, int n)
+{
+ while ((*s++ = *t++) && --n)
+ {};
+
+ *s = '\0';
+}
+
+void wxDbTable::SetCol(const int col, const wxVariant val)
+{
+ //FIXME: Add proper wxDateTime support to wxVariant..
+ wxDateTime dateval;
+
+ SetColNull(col, val.IsNull());
+
+ if (!val.IsNull())
+ {
+ if ((colDefs[col].SqlCtype == SQL_C_DATE)
+ || (colDefs[col].SqlCtype == SQL_C_TIME)
+ || (colDefs[col].SqlCtype == SQL_C_TIMESTAMP))
+ {
+ //Returns null if invalid!
+ if (!dateval.ParseDate(val.GetString()))
+ SetColNull(col,true);
+ }
+
+ switch (colDefs[col].SqlCtype)
+ {
+ case SQL_CHAR:
+ case SQL_VARCHAR:
+ csstrncpyt((char *)(colDefs[col].PtrDataObj),
+ val.GetString().c_str(),
+ colDefs[col].SzDataObj-1);
+ break;
+ case SQL_C_LONG:
+ case SQL_C_SLONG:
+ *(long *)(colDefs[col].PtrDataObj) = val;
+ break;
+ case SQL_C_SHORT:
+ case SQL_C_SSHORT:
+ *(short *)(colDefs[col].PtrDataObj) = val.GetLong();
+ break;
+ case SQL_C_ULONG:
+ *(unsigned long *)(colDefs[col].PtrDataObj) = val.GetLong();
+ break;
+ case SQL_C_TINYINT:
+ *(char *)(colDefs[col].PtrDataObj) = val.GetChar();
+ break;
+ case SQL_C_UTINYINT:
+ *(unsigned char *)(colDefs[col].PtrDataObj) = val.GetChar();
+ break;
+ case SQL_C_USHORT:
+ *(unsigned short *)(colDefs[col].PtrDataObj) = val.GetLong();
+ break;
+ //FIXME: Add proper wxDateTime support to wxVariant..
+ case SQL_C_DATE:
+ {
+ DATE_STRUCT *dataptr =
+ (DATE_STRUCT *)colDefs[col].PtrDataObj;
+
+ dataptr->year = dateval.GetYear();
+ dataptr->month = dateval.GetMonth()+1;
+ dataptr->day = dateval.GetDay();
+ }
+ break;
+ case SQL_C_TIME:
+ {
+ TIME_STRUCT *dataptr =
+ (TIME_STRUCT *)colDefs[col].PtrDataObj;
+
+ dataptr->hour = dateval.GetHour();
+ dataptr->minute = dateval.GetMinute();
+ dataptr->second = dateval.GetSecond();
+ }
+ break;
+ case SQL_C_TIMESTAMP:
+ {
+ TIMESTAMP_STRUCT *dataptr =
+ (TIMESTAMP_STRUCT *)colDefs[col].PtrDataObj;
+ dataptr->year = dateval.GetYear();
+ dataptr->month = dateval.GetMonth()+1;
+ dataptr->day = dateval.GetDay();
+
+ dataptr->hour = dateval.GetHour();
+ dataptr->minute = dateval.GetMinute();
+ dataptr->second = dateval.GetSecond();
+ }
+ break;
+ case SQL_C_DOUBLE:
+ *(double *)(colDefs[col].PtrDataObj) = val;
+ break;
+ default:
+ assert(0);
+ } // switch
+ } // if (!val.IsNull())
+} // wxDbTable::SetCol()
+
+
+GenericKey wxDbTable::GetKey()
+{
+ void *blk;
+ char *blkptr;
+
+ blk = malloc(m_keysize);
+ blkptr = (char *) blk;
+
+ int i;
+ for (i=0; i < noCols; i++)
+ {
+ if (colDefs[i].KeyField)
+ {
+ memcpy(blkptr,colDefs[i].PtrDataObj, colDefs[i].SzDataObj);
+ blkptr += colDefs[i].SzDataObj;
+ }
+ }
+
+ GenericKey k = GenericKey(blk, m_keysize);
+ free(blk);
+
+ return k;
+} // wxDbTable::GetKey()
+
+
+void wxDbTable::SetKey(const GenericKey& k)
+{
+ void *blk;
+ char *blkptr;
+
+ blk = k.GetBlk();
+ blkptr = (char *)blk;
+
+ int i;
+ for (i=0; i < noCols; i++)
+ {
+ if (colDefs[i].KeyField)
+ {
+ SetColNull(i, false);
+ memcpy(colDefs[i].PtrDataObj, blkptr, colDefs[i].SzDataObj);
+ blkptr += colDefs[i].SzDataObj;
+ }
+ }
+} // wxDbTable::SetKey()
+
+
#endif // wxUSE_ODBC