// RCS-ID: $Id$
// Copyright: (c) 1996 Remstar International, Inc.
// Licence: wxWindows licence, plus:
-// Notice: This class library and its intellectual design are free of charge for use,
+// Notice: This class library and its intellectual design are free of charge for use,
// modification, enhancement, debugging under the following conditions:
// 1) These classes may only be used as part of the implementation of a
// wxWindows-based application
#endif
#ifdef DBDEBUG_CONSOLE
- #include <iostream.h>
+ #include "wx/ioswrap.h"
#endif
#ifdef __BORLANDC__
- #pragma hdrstop
+ #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"
+ #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
pDb->nTables++;
- char s[200];
+ wxString s;
tableID = ++lastTableID;
- sprintf(s, "wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName,tableID,pDb);
+ s.sprintf("wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName,tableID,pDb);
#ifdef __WXDEBUG__
CstructTablesInUse *tableInUse;
- tableInUse = new CstructTablesInUse();
+ tableInUse = new CstructTablesInUse();
tableInUse->tableName = tblName;
- tableInUse->tableID = tableID;
- tableInUse->pDb = pDb;
+ tableInUse->tableID = tableID;
+ tableInUse->pDb = pDb;
TablesInUse.Append(tableInUse);
#endif
- pDb->WriteSqlLog(s);
+ pDb->WriteSqlLog(s.GetData());
// Grab the HENV and HDBC from the wxDB object
henv = pDb->henv;
case SQL_CURSOR_FORWARD_ONLY:
cout << "Forward Only"; break;
case SQL_CURSOR_STATIC:
- cout << "Static"; break;
+ cout << "Static"; break;
case SQL_CURSOR_KEYSET_DRIVEN:
cout << "Keyset Driven"; break;
case SQL_CURSOR_DYNAMIC:
/********** wxTable::~wxTable() **********/
wxTable::~wxTable()
{
- char s[80];
+ wxString s;
if (pDb)
{
- sprintf(s, "wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName,tableID,pDb);
- pDb->WriteSqlLog(s);
+ s.sprintf("wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName,tableID,pDb);
+ pDb->WriteSqlLog(s.GetData());
}
#ifdef __WXDEBUG__
if (tableID)
{
+ TablesInUse.DeleteContents(TRUE);
bool found = FALSE;
+
wxNode *pNode;
pNode = TablesInUse.First();
while (pNode && !found)
{
found = TRUE;
if (!TablesInUse.DeleteNode(pNode))
- wxMessageBox (s,"Unable to delete node!");
+ wxLogDebug (s.c_str(),"Unable to delete node!");
}
else
pNode = pNode->Next();
}
if (!found)
{
- char msg[250];
- sprintf(msg,"Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s);
- wxMessageBox (msg,"NOTICE...");
+ wxString msg;
+ msg.sprintf("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s.GetData());
+ wxLogDebug (msg.GetData(),"NOTICE...");
}
}
#endif
+
+
// Decrement the wxDB table count
if (pDb)
pDb->nTables--;
if (hstmtInsert)
if (SQLFreeStmt(hstmtInsert, SQL_DROP) != SQL_SUCCESS)
pDb->DispAllErrors(henv, hdbc);
+
if (hstmtDelete)
if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
- pDb->DispAllErrors(henv, hdbc);
+
if (hstmtUpdate)
if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
pDb->DispAllErrors(henv, hdbc);
+
}
if (hstmtInternal)
if (SQLFreeStmt(hstmtInternal, SQL_DROP) != SQL_SUCCESS)
// Delete dynamically allocated cursors
if (hstmtDefault)
DeleteCursor(hstmtDefault);
+
if (hstmtCount)
DeleteCursor(hstmtCount);
+
} // wxTable::~wxTable()
break;
}
if (SQLBindParameter(hstmtUpdate, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
- fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
- precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
+ fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
+ precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
}
for (i = 0; i < noCols; i++)
{
if (SQLBindCol(cursor, i+1, colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
- colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
+ colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
return(pDb->DispAllErrors(henv, hdbc, cursor));
}
return FALSE;
int i;
- char sqlStmt[DB_MAX_STATEMENT_LEN];
+ wxString sqlStmt;
// Verify that the table exists in the database
if (!pDb->TableExists(tableName,pDb->GetUsername(),tablePath))
{
- char s[250];
+ wxString s;
if (wxStrcmp(tablePath,""))
- sprintf(s, "Error opening '%s/%s'.\n",tablePath,tableName);
+ s.sprintf("Error opening '%s/%s'.\n",tablePath,tableName);
else
- sprintf(s, "Error opening '%s'.\n", tableName);
+ s.sprintf("Error opening '%s'.\n", tableName);
if (!pDb->TableExists(tableName,NULL,tablePath))
- wxStrcat(s,"Table/view does not exist in the database.\n");
+ s += "Table/view does not exist in the database.\n";
else
- wxStrcat(s,"Current logged in user does not have sufficient privileges to access this table.\n");
- pDb->LogError(s);
+ s += "Current logged in user does not have sufficient privileges to access this table.\n";
+ pDb->LogError(s.GetData());
return(FALSE);
}
if (!queryOnly && noCols > 0)
{
bool needComma = FALSE;
- sprintf(sqlStmt, "INSERT INTO %s (", tableName);
+ sqlStmt.sprintf("INSERT INTO %s (", tableName);
for (i = 0; i < noCols; i++)
{
if (! colDefs[i].InsertAllowed)
continue;
if (needComma)
- wxStrcat(sqlStmt, ",");
- wxStrcat(sqlStmt, colDefs[i].ColName);
+ sqlStmt += ",";
+ sqlStmt += colDefs[i].ColName;
needComma = TRUE;
}
needComma = FALSE;
- wxStrcat(sqlStmt, ") VALUES (");
+ sqlStmt += ") VALUES (";
for (i = 0; i < noCols; i++)
{
if (! colDefs[i].InsertAllowed)
continue;
if (needComma)
- wxStrcat(sqlStmt, ",");
- wxStrcat(sqlStmt, "?");
+ sqlStmt += ",";
+ sqlStmt += "?";
needComma = TRUE;
}
- wxStrcat(sqlStmt, ")");
+ sqlStmt += ")";
// pDb->WriteSqlLog(sqlStmt);
// Prepare the insert statement for execution
- if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
+ if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
}
return FALSE;
int i, j;
- char sqlStmt[DB_MAX_STATEMENT_LEN];
+// char sqlStmt[DB_MAX_STATEMENT_LEN];
+ wxString sqlStmt;
#ifdef DBDEBUG_CONSOLE
cout << "Creating Table " << tableName << "..." << endl;
// Build a CREATE TABLE string from the colDefs structure.
bool needComma = FALSE;
- sprintf(sqlStmt, "CREATE TABLE %s (", tableName);
+ sqlStmt.sprintf("CREATE TABLE %s (", tableName);
+
for (i = 0; i < noCols; i++)
{
// Exclude derived columns since they are NOT part of the base table
continue;
// Comma Delimiter
if (needComma)
- wxStrcat(sqlStmt, ",");
+ sqlStmt += ",";
// Column Name
- wxStrcat(sqlStmt, colDefs[i].ColName);
- wxStrcat(sqlStmt, " ");
+ sqlStmt += colDefs[i].ColName;
+ sqlStmt += " ";
// Column Type
switch(colDefs[i].DbDataType)
{
case DB_DATA_TYPE_VARCHAR:
- wxStrcat(sqlStmt, pDb->typeInfVarchar.TypeName); break;
+ sqlStmt += pDb->typeInfVarchar.TypeName; break;
case DB_DATA_TYPE_INTEGER:
- wxStrcat(sqlStmt, pDb->typeInfInteger.TypeName); break;
+ sqlStmt += pDb->typeInfInteger.TypeName; break;
case DB_DATA_TYPE_FLOAT:
- wxStrcat(sqlStmt, pDb->typeInfFloat.TypeName); break;
+ sqlStmt += pDb->typeInfFloat.TypeName; break;
case DB_DATA_TYPE_DATE:
- wxStrcat(sqlStmt, pDb->typeInfDate.TypeName); break;
+ sqlStmt += pDb->typeInfDate.TypeName; break;
}
// For varchars, append the size of the string
if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)
{
- char s[10];
+ wxString s;
// wxStrcat(sqlStmt, "(");
// wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
// wxStrcat(sqlStmt, ")");
- sprintf(s, "(%d)", colDefs[i].SzDataObj);
- wxStrcat(sqlStmt, s);
+ s.sprintf("(%d)", colDefs[i].SzDataObj);
+ sqlStmt += s.GetData();
}
if (pDb->Dbms() == dbmsSYBASE_ASE || pDb->Dbms() == dbmsMY_SQL)
{
if (colDefs[i].KeyField)
{
- wxStrcat(sqlStmt, " NOT NULL");
+ sqlStmt += " NOT NULL";
}
}
{
if (pDb->Dbms() != dbmsMY_SQL)
{
- wxStrcat(sqlStmt, ",CONSTRAINT ");
- wxStrcat(sqlStmt, tableName);
- wxStrcat(sqlStmt, "_PIDX PRIMARY KEY (");
+ sqlStmt += ",CONSTRAINT ";
+ sqlStmt += tableName;
+ sqlStmt += "_PIDX PRIMARY KEY (";
}
else
{
/* MySQL goes out on this one. We also declare the relevant key NON NULL above */
- wxStrcat(sqlStmt, ", PRIMARY KEY (");
+ sqlStmt += ", PRIMARY KEY (";
}
// List column name(s) of column(s) comprising the primary key
if (colDefs[i].KeyField)
{
if (j++) // Multi part key, comma separate names
- wxStrcat(sqlStmt, ",");
- wxStrcat(sqlStmt, colDefs[i].ColName);
+ sqlStmt += ",";
+ sqlStmt += colDefs[i].ColName;
}
}
- wxStrcat(sqlStmt, ")");
+ sqlStmt += ")";
}
// Append the closing parentheses for the create table statement
- wxStrcat(sqlStmt, ")");
+ sqlStmt += ")";
- pDb->WriteSqlLog(sqlStmt);
+ pDb->WriteSqlLog(sqlStmt.GetData());
#ifdef DBDEBUG_CONSOLE
- cout << endl << sqlStmt << endl;
+ cout << endl << sqlStmt.GetData() << endl;
#endif
// Execute the CREATE TABLE statement
- RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS);
+ RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
pDb->DispAllErrors(henv, hdbc, hstmt);
{
// 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
+ // below for any other databases when those databases are defined
// to handle this situation consistently
- char sqlStmt[DB_MAX_STATEMENT_LEN];
+ wxString sqlStmt;
- sprintf(sqlStmt, "DROP TABLE %s", tableName);
+ sqlStmt.sprintf("DROP TABLE %s", tableName);
- pDb->WriteSqlLog(sqlStmt);
+ pDb->WriteSqlLog(sqlStmt.GetData());
#ifdef DBDEBUG_CONSOLE
- cout << endl << sqlStmt << endl;
+ cout << endl << sqlStmt.GetData() << endl;
#endif
- if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
+ if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
{
// Check for "Base table not found" error and ignore
pDb->GetNextError(henv, hdbc, hstmt);
if (wxStrcmp(pDb->sqlState,"S0002")) // "Base table not found"
{
// Check for product specific error codes
- if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // 5.x (and lower?)
- (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"S1000")) || // untested
- (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,"08S01")))) // untested
+ if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // 5.x (and lower?)
+ (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"S1000")) || // untested
+ (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,"08S01")))) // untested
{
pDb->DispNextError();
pDb->DispAllErrors(henv, hdbc, hstmt);
/********** wxTable::CreateIndex() **********/
bool wxTable::CreateIndex(const char * idxName, bool unique, int noIdxCols, CidxDef *pIdxDefs, bool attemptDrop)
{
- char sqlStmt[DB_MAX_STATEMENT_LEN];
+// char sqlStmt[DB_MAX_STATEMENT_LEN];
+ wxString sqlStmt;
// Drop the index first
if (attemptDrop && !DropIndex(idxName))
return (FALSE);
// Build a CREATE INDEX statement
- wxStrcpy(sqlStmt, "CREATE ");
+ sqlStmt = "CREATE ";
if (unique)
- wxStrcat(sqlStmt, "UNIQUE ");
+ sqlStmt += "UNIQUE ";
- wxStrcat(sqlStmt, "INDEX ");
- wxStrcat(sqlStmt, idxName);
- wxStrcat(sqlStmt, " ON ");
- wxStrcat(sqlStmt, tableName);
- wxStrcat(sqlStmt, " (");
+ sqlStmt += "INDEX ";
+ sqlStmt += idxName;
+ sqlStmt += " ON ";
+ sqlStmt += tableName;
+ sqlStmt += " (";
// Append list of columns making up index
int i;
for (i = 0; i < noIdxCols; i++)
{
- wxStrcat(sqlStmt, pIdxDefs[i].ColName);
- /* Postgres doesn't cope with ASC */
+ sqlStmt += pIdxDefs[i].ColName;
+ /* Postgres doesn't cope with ASC */
if (pDb->Dbms() != dbmsPOSTGRES)
{
if (pIdxDefs[i].Ascending)
- wxStrcat(sqlStmt, " ASC");
+ sqlStmt += " ASC";
else
- wxStrcat(sqlStmt, " DESC");
+ sqlStmt += " DESC";
}
if ((i + 1) < noIdxCols)
- wxStrcat(sqlStmt, ",");
+ sqlStmt += ",";
}
// Append closing parentheses
- wxStrcat(sqlStmt, ")");
+ sqlStmt += ")";
- pDb->WriteSqlLog(sqlStmt);
+ pDb->WriteSqlLog(sqlStmt.GetData());
#ifdef DBDEBUG_CONSOLE
- cout << endl << sqlStmt << endl << endl;
+ cout << endl << sqlStmt.GetData() << endl << endl;
#endif
// Execute the CREATE INDEX statement
- if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
+ if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
{
pDb->DispAllErrors(henv, hdbc, hstmt);
pDb->RollbackTrans();
// below for any other databases when those databases are defined
// to handle this situation consistently
- char sqlStmt[DB_MAX_STATEMENT_LEN];
+ wxString sqlStmt;
if (pDb->Dbms() == dbmsACCESS)
- sprintf(sqlStmt, "DROP INDEX %s ON %s",idxName,tableName);
+ sqlStmt.sprintf("DROP INDEX %s ON %s",idxName,tableName);
else if (pDb->Dbms() == dbmsSYBASE_ASE)
- sprintf(sqlStmt, "DROP INDEX %s.%s",tableName,idxName);
+ sqlStmt.sprintf("DROP INDEX %s.%s",tableName,idxName);
else
- sprintf(sqlStmt, "DROP INDEX %s",idxName);
+ sqlStmt.sprintf("DROP INDEX %s",idxName);
- pDb->WriteSqlLog(sqlStmt);
+ pDb->WriteSqlLog(sqlStmt.GetData());
#ifdef DBDEBUG_CONSOLE
- cout << endl << sqlStmt << endl;
+ cout << endl << sqlStmt.GetData() << endl;
#endif
- if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
+ if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
{
// Check for "Index not found" error and ignore
pDb->GetNextError(henv, hdbc, hstmt);
if (wxStrcmp(pDb->sqlState,"S0012")) // "Index not found"
{
// Check for product specific error codes
- if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // v5.x (and lower?)
- (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,"S0002")) || // Base table not found
- (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"42S02")) // untested
+ if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // v5.x (and lower?)
+ (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,"S0002")) || // Base table not found
+ (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"42S02")) // untested
))
{
pDb->DispNextError();
pDt->second = 0;
pDt->fraction = 0;
break;
+
}
}
/********** wxTable::SetColDef() **********/
-// BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
-//bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
wxColDataPtr* wxTable::SetColDefs (wxColInf *pColInfs, ULONG numCols)
{
assert(pColInfs);
for (index = 0; index < numCols; index++)
{
-/*
- wxString title,msg;
- title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
- msg.sprintf("Column name: %s\nData type: %04d\nType name: %s\nColumn size: %d\nBuffer len: %d\nDecimals:%d\nRadix: %d\nNullable: %d\nRemarks: %s",
- pColInfs[index].colName,pColInfs[index].sqlDataType,pColInfs[index].typeName,pColInfs[index].columnSize,pColInfs[index].bufferLength,pColInfs[index].decimalDigits,pColInfs[index].numPrecRadix,pColInfs[index].nullable,pColInfs[index].remarks);
- msg += " \nDB_DATA_TYPE: ";
- switch(pColInfs[index].dbDataType)
- {
- case DB_DATA_TYPE_VARCHAR:
- msg += pDb->typeInfVarchar.TypeName; break;
- case DB_DATA_TYPE_INTEGER:
- msg += pDb->typeInfInteger.TypeName; break;
- case DB_DATA_TYPE_FLOAT:
- msg += pDb->typeInfFloat.TypeName; break;
- case DB_DATA_TYPE_DATE:
- msg += pDb->typeInfDate.TypeName; break;
- }
- wxMessageBox(msg.GetData(),title.GetData());
-*/
// Process the fields
switch (pColInfs[index].dbDataType)
{
case DB_DATA_TYPE_VARCHAR:
{
- pColDataPtrs[index].PtrDataObj = new char[pColInfs[index].bufferLength+1];
- pColDataPtrs[index].SzDataObj = pColInfs[index].bufferLength;
- pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
- break;
+ pColDataPtrs[index].PtrDataObj = new char[pColInfs[index].bufferLength+1];
+ pColDataPtrs[index].SzDataObj = pColInfs[index].columnSize;
+ pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
+ break;
}
case DB_DATA_TYPE_INTEGER:
{
ULONG wxTable::Count(const char *args)
{
ULONG l;
- char sqlStmt[DB_MAX_STATEMENT_LEN];
+ wxString sqlStmt;
SDWORD cb;
// Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
- wxStrcpy(sqlStmt, "SELECT COUNT(");
- wxStrcat(sqlStmt, args);
- wxStrcat(sqlStmt, ") FROM ");
- wxStrcat(sqlStmt, queryTableName);
+ sqlStmt = "SELECT COUNT(";
+ sqlStmt += args;
+ sqlStmt += ") FROM ";
+ sqlStmt += queryTableName;
if (from && wxStrlen(from))
- wxStrcat(sqlStmt, from);
+ sqlStmt += from;
// Add the where clause if one is provided
if (where && wxStrlen(where))
{
- wxStrcat(sqlStmt, " WHERE ");
- wxStrcat(sqlStmt, where);
+ sqlStmt += " WHERE ";
+ sqlStmt += where;
}
- pDb->WriteSqlLog(sqlStmt);
+ pDb->WriteSqlLog(sqlStmt.GetData());
// Initialize the Count cursor if it's not already initialized
if (!hstmtCount)
}
// Execute the SQL statement
- if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
+ if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
{
pDb->DispAllErrors(henv, hdbc, *hstmtCount);
return(0);
pDb->DispAllErrors(henv, hdbc, hstmtInternal);
// Restore the original where and order by clauses
- where = saveWhere;
+ where = saveWhere;
orderBy = saveOrderBy;
return(result);