1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence
12 ///////////////////////////////////////////////////////////////////////////////
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "dbtable.h"
23 #include "wx/wxprec.h"
29 #ifdef DBDEBUG_CONSOLE
35 #include "wx/ioswrap.h"
39 #include "wx/string.h"
40 #include "wx/object.h"
45 #include "wx/filefn.h"
53 #include "wx/dbtable.h"
56 // The HPUX preprocessor lines below were commented out on 8/20/97
57 // because macros.h currently redefines DEBUG and is unneeded.
59 // # include <macros.h>
62 # include <sys/minmax.h>
66 ULONG lastTableID
= 0;
74 void csstrncpyt(wxChar
*target
, const wxChar
*source
, int n
)
76 while ( (*target
++ = *source
++) != '\0' && --n
)
84 /********** wxDbColDef::wxDbColDef() Constructor **********/
85 wxDbColDef::wxDbColDef()
91 bool wxDbColDef::Initialize()
94 DbDataType
= DB_DATA_TYPE_INTEGER
;
95 SqlCtype
= SQL_C_LONG
;
100 InsertAllowed
= false;
106 } // wxDbColDef::Initialize()
109 /********** wxDbTable::wxDbTable() Constructor **********/
110 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
111 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
113 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
115 } // wxDbTable::wxDbTable()
118 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
119 #if WXWIN_COMPATIBILITY_2_4
120 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
121 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
123 wxString tempQryTblName
;
124 tempQryTblName
= qryTblName
;
125 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
127 } // wxDbTable::wxDbTable()
128 #endif // WXWIN_COMPATIBILITY_2_4
131 /********** wxDbTable::~wxDbTable() **********/
132 wxDbTable::~wxDbTable()
135 } // wxDbTable::~wxDbTable()
138 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
139 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
141 // Initializing member variables
142 pDb
= pwxDb
; // Pointer to the wxDb object
146 m_hstmtGridQuery
= 0;
147 hstmtDefault
= 0; // Initialized below
148 hstmtCount
= 0; // Initialized first time it is needed
155 m_numCols
= numColumns
; // Number of columns in the table
156 where
.Empty(); // Where clause
157 orderBy
.Empty(); // Order By clause
158 from
.Empty(); // From clause
159 selectForUpdate
= false; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
164 queryTableName
.Empty();
166 wxASSERT(tblName
.Length());
172 tableName
= tblName
; // Table Name
173 if ((pDb
->Dbms() == dbmsORACLE
) ||
174 (pDb
->Dbms() == dbmsFIREBIRD
) ||
175 (pDb
->Dbms() == dbmsINTERBASE
))
176 tableName
= tableName
.Upper();
178 if (tblPath
.Length())
179 tablePath
= tblPath
; // Table Path - used for dBase files
183 if (qryTblName
.Length()) // Name of the table/view to query
184 queryTableName
= qryTblName
;
186 queryTableName
= tblName
;
188 if ((pDb
->Dbms() == dbmsORACLE
) ||
189 (pDb
->Dbms() == dbmsFIREBIRD
) ||
190 (pDb
->Dbms() == dbmsINTERBASE
))
191 queryTableName
= queryTableName
.Upper();
193 pDb
->incrementTableCount();
196 tableID
= ++lastTableID
;
197 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
200 wxTablesInUse
*tableInUse
;
201 tableInUse
= new wxTablesInUse();
202 tableInUse
->tableName
= tblName
;
203 tableInUse
->tableID
= tableID
;
204 tableInUse
->pDb
= pDb
;
205 TablesInUse
.Append(tableInUse
);
210 // Grab the HENV and HDBC from the wxDb object
211 henv
= pDb
->GetHENV();
212 hdbc
= pDb
->GetHDBC();
214 // Allocate space for column definitions
216 colDefs
= new wxDbColDef
[m_numCols
]; // Points to the first column definition
218 // Allocate statement handles for the table
221 // Allocate a separate statement handle for performing inserts
222 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
223 pDb
->DispAllErrors(henv
, hdbc
);
224 // Allocate a separate statement handle for performing deletes
225 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
226 pDb
->DispAllErrors(henv
, hdbc
);
227 // Allocate a separate statement handle for performing updates
228 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
229 pDb
->DispAllErrors(henv
, hdbc
);
231 // Allocate a separate statement handle for internal use
232 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
233 pDb
->DispAllErrors(henv
, hdbc
);
235 // Set the cursor type for the statement handles
236 cursorType
= SQL_CURSOR_STATIC
;
238 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
240 // Check to see if cursor type is supported
241 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
242 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
244 // Datasource does not support static cursors. Driver
245 // will substitute a cursor type. Call SQLGetStmtOption()
246 // to determine which cursor type was selected.
247 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
248 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
249 #ifdef DBDEBUG_CONSOLE
250 cout
<< wxT("Static cursor changed to: ");
253 case SQL_CURSOR_FORWARD_ONLY
:
254 cout
<< wxT("Forward Only");
256 case SQL_CURSOR_STATIC
:
257 cout
<< wxT("Static");
259 case SQL_CURSOR_KEYSET_DRIVEN
:
260 cout
<< wxT("Keyset Driven");
262 case SQL_CURSOR_DYNAMIC
:
263 cout
<< wxT("Dynamic");
266 cout
<< endl
<< endl
;
269 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
271 // Force the use of a forward only cursor...
272 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
273 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
275 // Should never happen
276 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
283 pDb
->DispNextError();
284 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
287 #ifdef DBDEBUG_CONSOLE
289 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
294 // Set the cursor type for the INSERT statement handle
295 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
296 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
297 // Set the cursor type for the DELETE statement handle
298 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
299 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
300 // Set the cursor type for the UPDATE statement handle
301 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
302 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
305 // Make the default cursor the active cursor
306 hstmtDefault
= GetNewCursor(false,false);
307 wxASSERT(hstmtDefault
);
308 hstmt
= *hstmtDefault
;
312 } // wxDbTable::initialize()
315 void wxDbTable::cleanup()
320 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
329 wxList::compatibility_iterator pNode
;
330 pNode
= TablesInUse
.GetFirst();
331 while (pNode
&& !found
)
333 if (((wxTablesInUse
*)pNode
->GetData())->tableID
== tableID
)
336 delete (wxTablesInUse
*)pNode
->GetData();
337 TablesInUse
.Erase(pNode
);
340 pNode
= pNode
->GetNext();
345 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
346 wxLogDebug (msg
,wxT("NOTICE..."));
351 // Decrement the wxDb table count
353 pDb
->decrementTableCount();
355 // Delete memory allocated for column definitions
359 // Free statement handles
365 ODBC 3.0 says to use this form
366 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
368 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
369 pDb
->DispAllErrors(henv
, hdbc
);
375 ODBC 3.0 says to use this form
376 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
378 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
379 pDb
->DispAllErrors(henv
, hdbc
);
385 ODBC 3.0 says to use this form
386 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
388 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
389 pDb
->DispAllErrors(henv
, hdbc
);
395 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
396 pDb
->DispAllErrors(henv
, hdbc
);
399 // Delete dynamically allocated cursors
401 DeleteCursor(hstmtDefault
);
404 DeleteCursor(hstmtCount
);
406 if (m_hstmtGridQuery
)
407 DeleteCursor(m_hstmtGridQuery
);
409 } // wxDbTable::cleanup()
412 /***************************** PRIVATE FUNCTIONS *****************************/
415 void wxDbTable::setCbValueForColumn(int columnIndex
)
417 switch(colDefs
[columnIndex
].DbDataType
)
419 case DB_DATA_TYPE_VARCHAR
:
420 if (colDefs
[columnIndex
].Null
)
421 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
423 colDefs
[columnIndex
].CbValue
= SQL_NTS
;
425 case DB_DATA_TYPE_INTEGER
:
426 if (colDefs
[columnIndex
].Null
)
427 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
429 colDefs
[columnIndex
].CbValue
= 0;
431 case DB_DATA_TYPE_FLOAT
:
432 if (colDefs
[columnIndex
].Null
)
433 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
435 colDefs
[columnIndex
].CbValue
= 0;
437 case DB_DATA_TYPE_DATE
:
438 if (colDefs
[columnIndex
].Null
)
439 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
441 colDefs
[columnIndex
].CbValue
= 0;
443 case DB_DATA_TYPE_BLOB
:
444 if (colDefs
[columnIndex
].Null
)
445 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
447 if (colDefs
[columnIndex
].SqlCtype
== SQL_C_WXCHAR
)
448 colDefs
[columnIndex
].CbValue
= SQL_NTS
;
450 colDefs
[columnIndex
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[columnIndex
].SzDataObj
);
455 /********** wxDbTable::bindParams() **********/
456 bool wxDbTable::bindParams(bool forUpdate
)
458 wxASSERT(!queryOnly
);
463 SDWORD precision
= 0;
466 // Bind each column of the table that should be bound
467 // to a parameter marker
471 for (i
=0, colNumber
=1; i
< m_numCols
; i
++)
475 if (!colDefs
[i
].Updateable
)
480 if (!colDefs
[i
].InsertAllowed
)
484 switch(colDefs
[i
].DbDataType
)
486 case DB_DATA_TYPE_VARCHAR
:
487 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
488 precision
= colDefs
[i
].SzDataObj
;
491 case DB_DATA_TYPE_INTEGER
:
492 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
493 precision
= pDb
->GetTypeInfInteger().Precision
;
496 case DB_DATA_TYPE_FLOAT
:
497 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
498 precision
= pDb
->GetTypeInfFloat().Precision
;
499 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
500 // SQL Sybase Anywhere v5.5 returned a negative number for the
501 // MaxScale. This caused ODBC to kick out an error on ibscale.
502 // I check for this here and set the scale = precision.
504 // scale = (short) precision;
506 case DB_DATA_TYPE_DATE
:
507 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
508 precision
= pDb
->GetTypeInfDate().Precision
;
511 case DB_DATA_TYPE_BLOB
:
512 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
513 precision
= colDefs
[i
].SzDataObj
;
518 setCbValueForColumn(i
);
522 if (SQLBindParameter(hstmtUpdate
, colNumber
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
523 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
524 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
526 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
531 if (SQLBindParameter(hstmtInsert
, colNumber
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
532 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
533 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
535 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
540 // Completed successfully
543 } // wxDbTable::bindParams()
546 /********** wxDbTable::bindInsertParams() **********/
547 bool wxDbTable::bindInsertParams(void)
549 return bindParams(false);
550 } // wxDbTable::bindInsertParams()
553 /********** wxDbTable::bindUpdateParams() **********/
554 bool wxDbTable::bindUpdateParams(void)
556 return bindParams(true);
557 } // wxDbTable::bindUpdateParams()
560 /********** wxDbTable::bindCols() **********/
561 bool wxDbTable::bindCols(HSTMT cursor
)
565 // Bind each column of the table to a memory address for fetching data
567 for (i
= 0; i
< m_numCols
; i
++)
569 cb
= colDefs
[i
].CbValue
;
570 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
571 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
572 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
575 // Completed successfully
578 } // wxDbTable::bindCols()
581 /********** wxDbTable::getRec() **********/
582 bool wxDbTable::getRec(UWORD fetchType
)
586 if (!pDb
->FwdOnlyCursors())
588 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
592 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
593 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
595 if (retcode
== SQL_NO_DATA_FOUND
)
598 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
602 // Set the Null member variable to indicate the Null state
603 // of each column just read in.
605 for (i
= 0; i
< m_numCols
; i
++)
606 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
611 // Fetch the next record from the record set
612 retcode
= SQLFetch(hstmt
);
613 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
615 if (retcode
== SQL_NO_DATA_FOUND
)
618 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
622 // Set the Null member variable to indicate the Null state
623 // of each column just read in.
625 for (i
= 0; i
< m_numCols
; i
++)
626 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
630 // Completed successfully
633 } // wxDbTable::getRec()
636 /********** wxDbTable::execDelete() **********/
637 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
641 // Execute the DELETE statement
642 retcode
= SQLExecDirect(hstmtDelete
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
644 if (retcode
== SQL_SUCCESS
||
645 retcode
== SQL_NO_DATA_FOUND
||
646 retcode
== SQL_SUCCESS_WITH_INFO
)
648 // Record deleted successfully
652 // Problem deleting record
653 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
655 } // wxDbTable::execDelete()
658 /********** wxDbTable::execUpdate() **********/
659 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
663 // Execute the UPDATE statement
664 retcode
= SQLExecDirect(hstmtUpdate
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
666 if (retcode
== SQL_SUCCESS
||
667 retcode
== SQL_NO_DATA_FOUND
||
668 retcode
== SQL_SUCCESS_WITH_INFO
)
670 // Record updated successfully
673 else if (retcode
== SQL_NEED_DATA
)
676 retcode
= SQLParamData(hstmtUpdate
, &pParmID
);
677 while (retcode
== SQL_NEED_DATA
)
679 // Find the parameter
681 for (i
=0; i
< m_numCols
; i
++)
683 if (colDefs
[i
].PtrDataObj
== pParmID
)
685 // We found it. Store the parameter.
686 retcode
= SQLPutData(hstmtUpdate
, pParmID
, colDefs
[i
].SzDataObj
);
687 if (retcode
!= SQL_SUCCESS
)
689 pDb
->DispNextError();
690 return pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
695 retcode
= SQLParamData(hstmtUpdate
, &pParmID
);
697 if (retcode
== SQL_SUCCESS
||
698 retcode
== SQL_NO_DATA_FOUND
||
699 retcode
== SQL_SUCCESS_WITH_INFO
)
701 // Record updated successfully
706 // Problem updating record
707 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
709 } // wxDbTable::execUpdate()
712 /********** wxDbTable::query() **********/
713 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
718 // The user may wish to select for update, but the DBMS may not be capable
719 selectForUpdate
= CanSelectForUpdate();
721 selectForUpdate
= false;
723 // Set the SQL SELECT string
724 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
725 { // so generate a select statement.
726 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
727 pDb
->WriteSqlLog(sqlStmt
);
730 // Make sure the cursor is closed first
731 if (!CloseCursor(hstmt
))
734 // Execute the SQL SELECT statement
736 retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
737 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
738 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
740 // Completed successfully
743 } // wxDbTable::query()
746 /***************************** PUBLIC FUNCTIONS *****************************/
749 /********** wxDbTable::Open() **********/
750 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
760 // Calculate the maximum size of the concatenated
761 // keys for use with wxDbGrid
763 for (i
=0; i
< m_numCols
; i
++)
765 if (colDefs
[i
].KeyField
)
768 m_keysize
+= colDefs
[i
].SzDataObj
;
773 // Verify that the table exists in the database
774 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
776 s
= wxT("Table/view does not exist in the database");
777 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
778 s
+= wxT(", or you have no permissions.\n");
782 else if (checkPrivileges
)
784 // Verify the user has rights to access the table.
785 // Shortcut boolean evaluation to optimize out call to
788 // Unfortunately this optimization doesn't seem to be
790 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
791 !pDb
->TablePrivileges(tableName
, wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
792 s
= wxT("Connecting user does not have sufficient privileges to access this table.\n");
799 if (!tablePath
.IsEmpty())
800 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
802 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
805 pDb
->LogError(p
.GetData());
810 // Bind the member variables for field exchange between
811 // the wxDbTable object and the ODBC record.
814 if (!bindInsertParams()) // Inserts
817 if (!bindUpdateParams()) // Updates
821 if (!bindCols(*hstmtDefault
)) // Selects
824 if (!bindCols(hstmtInternal
)) // Internal use only
828 * Do NOT bind the hstmtCount cursor!!!
831 // Build an insert statement using parameter markers
832 if (!queryOnly
&& m_numCols
> 0)
834 bool needComma
= false;
835 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
836 pDb
->SQLTableName(tableName
.c_str()).c_str());
837 for (i
= 0; i
< m_numCols
; i
++)
839 if (! colDefs
[i
].InsertAllowed
)
843 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
844 // sqlStmt += colDefs[i].ColName;
848 sqlStmt
+= wxT(") VALUES (");
850 int insertableCount
= 0;
852 for (i
= 0; i
< m_numCols
; i
++)
854 if (! colDefs
[i
].InsertAllowed
)
864 // Prepare the insert statement for execution
867 if (SQLPrepare(hstmtInsert
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
868 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
874 // Completed successfully
877 } // wxDbTable::Open()
880 /********** wxDbTable::Query() **********/
881 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
884 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
886 } // wxDbTable::Query()
889 /********** wxDbTable::QueryBySqlStmt() **********/
890 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
892 pDb
->WriteSqlLog(pSqlStmt
);
894 return(query(DB_SELECT_STATEMENT
, false, false, pSqlStmt
));
896 } // wxDbTable::QueryBySqlStmt()
899 /********** wxDbTable::QueryMatching() **********/
900 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
903 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
905 } // wxDbTable::QueryMatching()
908 /********** wxDbTable::QueryOnKeyFields() **********/
909 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
912 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
914 } // wxDbTable::QueryOnKeyFields()
917 /********** wxDbTable::GetPrev() **********/
918 bool wxDbTable::GetPrev(void)
920 if (pDb
->FwdOnlyCursors())
922 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
926 return(getRec(SQL_FETCH_PRIOR
));
928 } // wxDbTable::GetPrev()
931 /********** wxDbTable::operator-- **********/
932 bool wxDbTable::operator--(int)
934 if (pDb
->FwdOnlyCursors())
936 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
940 return(getRec(SQL_FETCH_PRIOR
));
942 } // wxDbTable::operator--
945 /********** wxDbTable::GetFirst() **********/
946 bool wxDbTable::GetFirst(void)
948 if (pDb
->FwdOnlyCursors())
950 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
954 return(getRec(SQL_FETCH_FIRST
));
956 } // wxDbTable::GetFirst()
959 /********** wxDbTable::GetLast() **********/
960 bool wxDbTable::GetLast(void)
962 if (pDb
->FwdOnlyCursors())
964 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
968 return(getRec(SQL_FETCH_LAST
));
970 } // wxDbTable::GetLast()
973 /********** wxDbTable::BuildDeleteStmt() **********/
974 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
976 wxASSERT(!queryOnly
);
980 wxString whereClause
;
984 // Handle the case of DeleteWhere() and the where clause is blank. It should
985 // delete all records from the database in this case.
986 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
988 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
989 pDb
->SQLTableName(tableName
.c_str()).c_str());
993 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
994 pDb
->SQLTableName(tableName
.c_str()).c_str());
996 // Append the WHERE clause to the SQL DELETE statement
999 case DB_DEL_KEYFIELDS
:
1000 // If the datasource supports the ROWID column, build
1001 // the where on ROWID for efficiency purposes.
1002 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1003 if (CanUpdateByROWID())
1006 wxChar rowid
[wxDB_ROWID_LEN
+1];
1008 // Get the ROWID value. If not successful retreiving the ROWID,
1009 // simply fall down through the code and build the WHERE clause
1010 // based on the key fields.
1011 if (SQLGetData(hstmt
, (UWORD
)(m_numCols
+1), SQL_C_WXCHAR
, (UCHAR
*) rowid
, sizeof(rowid
), &cb
) == SQL_SUCCESS
)
1013 pSqlStmt
+= wxT("ROWID = '");
1015 pSqlStmt
+= wxT("'");
1019 // Unable to delete by ROWID, so build a WHERE
1020 // clause based on the keyfields.
1021 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1022 pSqlStmt
+= whereClause
;
1025 pSqlStmt
+= pWhereClause
;
1027 case DB_DEL_MATCHING
:
1028 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1029 pSqlStmt
+= whereClause
;
1033 } // BuildDeleteStmt()
1036 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
1037 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
1039 wxString tempSqlStmt
;
1040 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
1041 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1042 } // wxDbTable::BuildDeleteStmt()
1045 /********** wxDbTable::BuildSelectStmt() **********/
1046 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
1048 wxString whereClause
;
1049 whereClause
.Empty();
1051 // Build a select statement to query the database
1052 pSqlStmt
= wxT("SELECT ");
1054 // SELECT DISTINCT values only?
1056 pSqlStmt
+= wxT("DISTINCT ");
1058 // Was a FROM clause specified to join tables to the base table?
1059 // Available for ::Query() only!!!
1060 bool appendFromClause
= false;
1061 #if wxODBC_BACKWARD_COMPATABILITY
1062 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
1063 appendFromClause
= true;
1065 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
1066 appendFromClause
= true;
1069 // Add the column list
1072 for (i
= 0; i
< m_numCols
; i
++)
1074 tStr
= colDefs
[i
].ColName
;
1075 // If joining tables, the base table column names must be qualified to avoid ambiguity
1076 if ((appendFromClause
|| pDb
->Dbms() == dbmsACCESS
) && tStr
.Find(wxT('.')) == wxNOT_FOUND
)
1078 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1079 pSqlStmt
+= wxT(".");
1081 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1082 if (i
+ 1 < m_numCols
)
1083 pSqlStmt
+= wxT(",");
1086 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1087 // the ROWID if querying distinct records. The rowid will always be unique.
1088 if (!distinct
&& CanUpdateByROWID())
1090 // If joining tables, the base table column names must be qualified to avoid ambiguity
1091 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1093 pSqlStmt
+= wxT(",");
1094 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1095 // pSqlStmt += queryTableName;
1096 pSqlStmt
+= wxT(".ROWID");
1099 pSqlStmt
+= wxT(",ROWID");
1102 // Append the FROM tablename portion
1103 pSqlStmt
+= wxT(" FROM ");
1104 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1105 // pSqlStmt += queryTableName;
1107 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1108 // The HOLDLOCK keyword follows the table name in the from clause.
1109 // Each table in the from clause must specify HOLDLOCK or
1110 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1111 // is parsed but ignored in SYBASE Transact-SQL.
1112 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1113 pSqlStmt
+= wxT(" HOLDLOCK");
1115 if (appendFromClause
)
1118 // Append the WHERE clause. Either append the where clause for the class
1119 // or build a where clause. The typeOfSelect determines this.
1120 switch(typeOfSelect
)
1122 case DB_SELECT_WHERE
:
1123 #if wxODBC_BACKWARD_COMPATABILITY
1124 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1126 if (where
.Length()) // May not want a where clause!!!
1129 pSqlStmt
+= wxT(" WHERE ");
1133 case DB_SELECT_KEYFIELDS
:
1134 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1135 if (whereClause
.Length())
1137 pSqlStmt
+= wxT(" WHERE ");
1138 pSqlStmt
+= whereClause
;
1141 case DB_SELECT_MATCHING
:
1142 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1143 if (whereClause
.Length())
1145 pSqlStmt
+= wxT(" WHERE ");
1146 pSqlStmt
+= whereClause
;
1151 // Append the ORDER BY clause
1152 #if wxODBC_BACKWARD_COMPATABILITY
1153 if (orderBy
&& wxStrlen(orderBy
))
1155 if (orderBy
.Length())
1158 pSqlStmt
+= wxT(" ORDER BY ");
1159 pSqlStmt
+= orderBy
;
1162 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1163 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1164 // HOLDLOCK for Sybase.
1165 if (selectForUpdate
&& CanSelectForUpdate())
1166 pSqlStmt
+= wxT(" FOR UPDATE");
1168 } // wxDbTable::BuildSelectStmt()
1171 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1172 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1174 wxString tempSqlStmt
;
1175 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1176 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1177 } // wxDbTable::BuildSelectStmt()
1180 /********** wxDbTable::BuildUpdateStmt() **********/
1181 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpdate
, const wxString
&pWhereClause
)
1183 wxASSERT(!queryOnly
);
1187 wxString whereClause
;
1188 whereClause
.Empty();
1190 bool firstColumn
= true;
1192 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1193 pDb
->SQLTableName(tableName
.c_str()).c_str());
1195 // Append a list of columns to be updated
1197 for (i
= 0; i
< m_numCols
; i
++)
1199 // Only append Updateable columns
1200 if (colDefs
[i
].Updateable
)
1203 pSqlStmt
+= wxT(",");
1205 firstColumn
= false;
1207 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1208 // pSqlStmt += colDefs[i].ColName;
1209 pSqlStmt
+= wxT(" = ?");
1213 // Append the WHERE clause to the SQL UPDATE statement
1214 pSqlStmt
+= wxT(" WHERE ");
1215 switch(typeOfUpdate
)
1217 case DB_UPD_KEYFIELDS
:
1218 // If the datasource supports the ROWID column, build
1219 // the where on ROWID for efficiency purposes.
1220 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1221 if (CanUpdateByROWID())
1224 wxChar rowid
[wxDB_ROWID_LEN
+1];
1226 // Get the ROWID value. If not successful retreiving the ROWID,
1227 // simply fall down through the code and build the WHERE clause
1228 // based on the key fields.
1229 if (SQLGetData(hstmt
, (UWORD
)(m_numCols
+1), SQL_C_WXCHAR
, (UCHAR
*) rowid
, sizeof(rowid
), &cb
) == SQL_SUCCESS
)
1231 pSqlStmt
+= wxT("ROWID = '");
1233 pSqlStmt
+= wxT("'");
1237 // Unable to delete by ROWID, so build a WHERE
1238 // clause based on the keyfields.
1239 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1240 pSqlStmt
+= whereClause
;
1243 pSqlStmt
+= pWhereClause
;
1246 } // BuildUpdateStmt()
1249 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1250 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpdate
, const wxString
&pWhereClause
)
1252 wxString tempSqlStmt
;
1253 BuildUpdateStmt(tempSqlStmt
, typeOfUpdate
, pWhereClause
);
1254 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1255 } // BuildUpdateStmt()
1258 /********** wxDbTable::BuildWhereClause() **********/
1259 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1260 const wxString
&qualTableName
, bool useLikeComparison
)
1262 * Note: BuildWhereClause() currently ignores timestamp columns.
1263 * They are not included as part of the where clause.
1266 bool moreThanOneColumn
= false;
1269 // Loop through the columns building a where clause as you go
1271 for (colNumber
= 0; colNumber
< m_numCols
; colNumber
++)
1273 // Determine if this column should be included in the WHERE clause
1274 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[colNumber
].KeyField
) ||
1275 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull((UWORD
)colNumber
))))
1277 // Skip over timestamp columns
1278 if (colDefs
[colNumber
].SqlCtype
== SQL_C_TIMESTAMP
)
1280 // If there is more than 1 column, join them with the keyword "AND"
1281 if (moreThanOneColumn
)
1282 pWhereClause
+= wxT(" AND ");
1284 moreThanOneColumn
= true;
1286 // Concatenate where phrase for the column
1287 wxString tStr
= colDefs
[colNumber
].ColName
;
1289 if (qualTableName
.Length() && tStr
.Find(wxT('.')) == wxNOT_FOUND
)
1291 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1292 pWhereClause
+= wxT(".");
1294 pWhereClause
+= pDb
->SQLColumnName(colDefs
[colNumber
].ColName
);
1296 if (useLikeComparison
&& (colDefs
[colNumber
].SqlCtype
== SQL_C_WXCHAR
))
1297 pWhereClause
+= wxT(" LIKE ");
1299 pWhereClause
+= wxT(" = ");
1301 switch(colDefs
[colNumber
].SqlCtype
)
1307 //case SQL_C_WXCHAR: SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
1308 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[colNumber
].PtrDataObj
);
1312 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[colNumber
].PtrDataObj
));
1315 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[colNumber
].PtrDataObj
));
1319 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[colNumber
].PtrDataObj
));
1322 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[colNumber
].PtrDataObj
));
1325 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[colNumber
].PtrDataObj
));
1328 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[colNumber
].PtrDataObj
));
1333 strMsg
.Printf(wxT("wxDbTable::bindParams(): Unknown column type for colDefs %d colName %s"),
1334 colNumber
,colDefs
[colNumber
].ColName
);
1335 wxFAIL_MSG(strMsg
.c_str());
1339 pWhereClause
+= colValue
;
1342 } // wxDbTable::BuildWhereClause()
1345 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1346 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1347 const wxString
&qualTableName
, bool useLikeComparison
)
1349 wxString tempSqlStmt
;
1350 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1351 wxStrcpy(pWhereClause
, tempSqlStmt
);
1352 } // wxDbTable::BuildWhereClause()
1355 /********** wxDbTable::GetRowNum() **********/
1356 UWORD
wxDbTable::GetRowNum(void)
1360 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1362 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1366 // Completed successfully
1367 return((UWORD
) rowNum
);
1369 } // wxDbTable::GetRowNum()
1372 /********** wxDbTable::CloseCursor() **********/
1373 bool wxDbTable::CloseCursor(HSTMT cursor
)
1375 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1376 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1378 // Completed successfully
1381 } // wxDbTable::CloseCursor()
1384 /********** wxDbTable::CreateTable() **********/
1385 bool wxDbTable::CreateTable(bool attemptDrop
)
1393 #ifdef DBDEBUG_CONSOLE
1394 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1398 if (attemptDrop
&& !DropTable())
1402 #ifdef DBDEBUG_CONSOLE
1403 for (i
= 0; i
< m_numCols
; i
++)
1405 // Exclude derived columns since they are NOT part of the base table
1406 if (colDefs
[i
].DerivedCol
)
1408 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1409 switch(colDefs
[i
].DbDataType
)
1411 case DB_DATA_TYPE_VARCHAR
:
1412 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)) << wxT(")");
1414 case DB_DATA_TYPE_INTEGER
:
1415 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1417 case DB_DATA_TYPE_FLOAT
:
1418 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1420 case DB_DATA_TYPE_DATE
:
1421 cout
<< pDb
->GetTypeInfDate().TypeName
;
1423 case DB_DATA_TYPE_BLOB
:
1424 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1431 // Build a CREATE TABLE string from the colDefs structure.
1432 bool needComma
= false;
1434 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1435 pDb
->SQLTableName(tableName
.c_str()).c_str());
1437 for (i
= 0; i
< m_numCols
; i
++)
1439 // Exclude derived columns since they are NOT part of the base table
1440 if (colDefs
[i
].DerivedCol
)
1444 sqlStmt
+= wxT(",");
1446 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1447 // sqlStmt += colDefs[i].ColName;
1448 sqlStmt
+= wxT(" ");
1450 switch(colDefs
[i
].DbDataType
)
1452 case DB_DATA_TYPE_VARCHAR
:
1453 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1455 case DB_DATA_TYPE_INTEGER
:
1456 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1458 case DB_DATA_TYPE_FLOAT
:
1459 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1461 case DB_DATA_TYPE_DATE
:
1462 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1464 case DB_DATA_TYPE_BLOB
:
1465 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1468 // For varchars, append the size of the string
1469 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
&&
1470 (pDb
->Dbms() != dbmsMY_SQL
|| pDb
->GetTypeInfVarchar().TypeName
!= _T("text")))// ||
1471 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1474 s
.Printf(wxT("(%d)"), (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)));
1478 if (pDb
->Dbms() == dbmsDB2
||
1479 pDb
->Dbms() == dbmsMY_SQL
||
1480 pDb
->Dbms() == dbmsSYBASE_ASE
||
1481 pDb
->Dbms() == dbmsINTERBASE
||
1482 pDb
->Dbms() == dbmsFIREBIRD
||
1483 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1485 if (colDefs
[i
].KeyField
)
1487 sqlStmt
+= wxT(" NOT NULL");
1493 // If there is a primary key defined, include it in the create statement
1494 for (i
= j
= 0; i
< m_numCols
; i
++)
1496 if (colDefs
[i
].KeyField
)
1502 if ( j
&& (pDb
->Dbms() != dbmsDBASE
)
1503 && (pDb
->Dbms() != dbmsXBASE_SEQUITER
) ) // Found a keyfield
1505 switch (pDb
->Dbms())
1509 case dbmsSYBASE_ASA
:
1510 case dbmsSYBASE_ASE
:
1514 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1515 sqlStmt
+= wxT(",PRIMARY KEY (");
1520 sqlStmt
+= wxT(",CONSTRAINT ");
1521 // DB2 is limited to 18 characters for index names
1522 if (pDb
->Dbms() == dbmsDB2
)
1524 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."));
1525 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1526 // sqlStmt += tableName.substr(0, 13);
1529 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1530 // sqlStmt += tableName;
1532 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1537 // List column name(s) of column(s) comprising the primary key
1538 for (i
= j
= 0; i
< m_numCols
; i
++)
1540 if (colDefs
[i
].KeyField
)
1542 if (j
++) // Multi part key, comma separate names
1543 sqlStmt
+= wxT(",");
1544 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1546 if (pDb
->Dbms() == dbmsMY_SQL
&&
1547 colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1550 s
.Printf(wxT("(%d)"), (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)));
1555 sqlStmt
+= wxT(")");
1557 if (pDb
->Dbms() == dbmsINFORMIX
||
1558 pDb
->Dbms() == dbmsSYBASE_ASA
||
1559 pDb
->Dbms() == dbmsSYBASE_ASE
)
1561 sqlStmt
+= wxT(" CONSTRAINT ");
1562 sqlStmt
+= pDb
->SQLTableName(tableName
);
1563 // sqlStmt += tableName;
1564 sqlStmt
+= wxT("_PIDX");
1567 // Append the closing parentheses for the create table statement
1568 sqlStmt
+= wxT(")");
1570 pDb
->WriteSqlLog(sqlStmt
);
1572 #ifdef DBDEBUG_CONSOLE
1573 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1576 // Execute the CREATE TABLE statement
1577 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1578 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1580 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1581 pDb
->RollbackTrans();
1586 // Commit the transaction and close the cursor
1587 if (!pDb
->CommitTrans())
1589 if (!CloseCursor(hstmt
))
1592 // Database table created successfully
1595 } // wxDbTable::CreateTable()
1598 /********** wxDbTable::DropTable() **********/
1599 bool wxDbTable::DropTable()
1601 // NOTE: This function returns true if the Table does not exist, but
1602 // only for identified databases. Code will need to be added
1603 // below for any other databases when those databases are defined
1604 // to handle this situation consistently
1608 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1609 pDb
->SQLTableName(tableName
.c_str()).c_str());
1611 pDb
->WriteSqlLog(sqlStmt
);
1613 #ifdef DBDEBUG_CONSOLE
1614 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1617 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1618 if (retcode
!= SQL_SUCCESS
)
1620 // Check for "Base table not found" error and ignore
1621 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1622 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1623 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1625 // Check for product specific error codes
1626 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1627 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1628 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1629 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1631 pDb
->DispNextError();
1632 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1633 pDb
->RollbackTrans();
1634 // CloseCursor(hstmt);
1640 // Commit the transaction and close the cursor
1641 if (! pDb
->CommitTrans())
1643 if (! CloseCursor(hstmt
))
1647 } // wxDbTable::DropTable()
1650 /********** wxDbTable::CreateIndex() **********/
1651 bool wxDbTable::CreateIndex(const wxString
&indexName
, bool unique
, UWORD numIndexColumns
,
1652 wxDbIdxDef
*pIndexDefs
, bool attemptDrop
)
1656 // Drop the index first
1657 if (attemptDrop
&& !DropIndex(indexName
))
1660 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1661 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1662 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1663 // table was created, then months later you determine that an additional index while
1664 // give better performance, so you want to add an index).
1666 // The following block of code will modify the column definition to make the column be
1667 // defined with the "NOT NULL" qualifier.
1668 if (pDb
->Dbms() == dbmsMY_SQL
)
1673 for (i
= 0; i
< numIndexColumns
&& ok
; i
++)
1677 // Find the column definition that has the ColName that matches the
1678 // index column name. We need to do this to get the DB_DATA_TYPE of
1679 // the index column, as MySQL's syntax for the ALTER column requires
1681 while (!found
&& (j
< this->m_numCols
))
1683 if (wxStrcmp(colDefs
[j
].ColName
,pIndexDefs
[i
].ColName
) == 0)
1691 ok
= pDb
->ModifyColumn(tableName
, pIndexDefs
[i
].ColName
,
1692 colDefs
[j
].DbDataType
, (int)(colDefs
[j
].SzDataObj
/ sizeof(wxChar
)),
1698 // retcode is not used
1699 wxODBC_ERRORS retcode
;
1700 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1701 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1702 // This line is just here for debug checking of the value
1703 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1714 pDb
->RollbackTrans();
1719 // Build a CREATE INDEX statement
1720 sqlStmt
= wxT("CREATE ");
1722 sqlStmt
+= wxT("UNIQUE ");
1724 sqlStmt
+= wxT("INDEX ");
1725 sqlStmt
+= pDb
->SQLTableName(indexName
);
1726 sqlStmt
+= wxT(" ON ");
1728 sqlStmt
+= pDb
->SQLTableName(tableName
);
1729 // sqlStmt += tableName;
1730 sqlStmt
+= wxT(" (");
1732 // Append list of columns making up index
1734 for (i
= 0; i
< numIndexColumns
; i
++)
1736 sqlStmt
+= pDb
->SQLColumnName(pIndexDefs
[i
].ColName
);
1737 // sqlStmt += pIndexDefs[i].ColName;
1739 // MySQL requires a key length on VARCHAR keys
1740 if ( pDb
->Dbms() == dbmsMY_SQL
)
1742 // Find the details on this column
1744 for ( j
= 0; j
< m_numCols
; ++j
)
1746 if ( wxStrcmp( pIndexDefs
[i
].ColName
, colDefs
[j
].ColName
) == 0 )
1751 if ( colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1754 s
.Printf(wxT("(%d)"), (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)));
1759 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1760 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (wxStrncmp(pDb
->dbInf
.dbmsVer
,_T("07"),2)==0)) &&
1761 !(pDb
->Dbms() == dbmsFIREBIRD
) &&
1762 !(pDb
->Dbms() == dbmsPOSTGRES
))
1764 if (pIndexDefs
[i
].Ascending
)
1765 sqlStmt
+= wxT(" ASC");
1767 sqlStmt
+= wxT(" DESC");
1770 wxASSERT_MSG(pIndexDefs
[i
].Ascending
, _T("Datasource does not support DESCending index columns"));
1772 if ((i
+ 1) < numIndexColumns
)
1773 sqlStmt
+= wxT(",");
1776 // Append closing parentheses
1777 sqlStmt
+= wxT(")");
1779 pDb
->WriteSqlLog(sqlStmt
);
1781 #ifdef DBDEBUG_CONSOLE
1782 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1785 // Execute the CREATE INDEX statement
1786 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1787 if (retcode
!= SQL_SUCCESS
)
1789 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1790 pDb
->RollbackTrans();
1795 // Commit the transaction and close the cursor
1796 if (! pDb
->CommitTrans())
1798 if (! CloseCursor(hstmt
))
1801 // Index Created Successfully
1804 } // wxDbTable::CreateIndex()
1807 /********** wxDbTable::DropIndex() **********/
1808 bool wxDbTable::DropIndex(const wxString
&indexName
)
1810 // NOTE: This function returns true if the Index does not exist, but
1811 // only for identified databases. Code will need to be added
1812 // below for any other databases when those databases are defined
1813 // to handle this situation consistently
1817 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1818 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1819 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1820 pDb
->SQLTableName(indexName
.c_str()).c_str(),
1821 pDb
->SQLTableName(tableName
.c_str()).c_str());
1822 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1823 (pDb
->Dbms() == dbmsSYBASE_ASE
) ||
1824 (pDb
->Dbms() == dbmsXBASE_SEQUITER
))
1825 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1826 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1827 pDb
->SQLTableName(indexName
.c_str()).c_str());
1829 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1830 pDb
->SQLTableName(indexName
.c_str()).c_str());
1832 pDb
->WriteSqlLog(sqlStmt
);
1834 #ifdef DBDEBUG_CONSOLE
1835 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1837 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1838 if (retcode
!= SQL_SUCCESS
)
1840 // Check for "Index not found" error and ignore
1841 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1842 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1844 // Check for product specific error codes
1845 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1846 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1847 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1848 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1849 (pDb
->Dbms() == dbmsFIREBIRD
&& !wxStrcmp(pDb
->sqlState
,wxT("HY000"))) ||
1850 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1851 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1852 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1855 pDb
->DispNextError();
1856 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1857 pDb
->RollbackTrans();
1864 // Commit the transaction and close the cursor
1865 if (! pDb
->CommitTrans())
1867 if (! CloseCursor(hstmt
))
1871 } // wxDbTable::DropIndex()
1874 /********** wxDbTable::SetOrderByColNums() **********/
1875 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1877 int colNumber
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1883 va_start(argptr
, first
); /* Initialize variable arguments. */
1884 while (!abort
&& (colNumber
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1886 // Make sure the passed in column number
1887 // is within the valid range of columns
1889 // Valid columns are 0 thru m_numCols-1
1890 if (colNumber
>= m_numCols
|| colNumber
< 0)
1896 if (colNumber
!= first
)
1897 tempStr
+= wxT(",");
1899 tempStr
+= colDefs
[colNumber
].ColName
;
1900 colNumber
= va_arg (argptr
, int);
1902 va_end (argptr
); /* Reset variable arguments. */
1904 SetOrderByClause(tempStr
);
1907 } // wxDbTable::SetOrderByColNums()
1910 /********** wxDbTable::Insert() **********/
1911 int wxDbTable::Insert(void)
1913 wxASSERT(!queryOnly
);
1914 if (queryOnly
|| !insertable
)
1919 // Insert the record by executing the already prepared insert statement
1921 retcode
= SQLExecute(hstmtInsert
);
1922 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
&&
1923 retcode
!= SQL_NEED_DATA
)
1925 // Check to see if integrity constraint was violated
1926 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1927 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1928 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1931 pDb
->DispNextError();
1932 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1936 if (retcode
== SQL_NEED_DATA
)
1939 retcode
= SQLParamData(hstmtInsert
, &pParmID
);
1940 while (retcode
== SQL_NEED_DATA
)
1942 // Find the parameter
1944 for (i
=0; i
< m_numCols
; i
++)
1946 if (colDefs
[i
].PtrDataObj
== pParmID
)
1948 // We found it. Store the parameter.
1949 retcode
= SQLPutData(hstmtInsert
, pParmID
, colDefs
[i
].SzDataObj
);
1950 if (retcode
!= SQL_SUCCESS
)
1952 pDb
->DispNextError();
1953 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1959 retcode
= SQLParamData(hstmtInsert
, &pParmID
);
1960 if (retcode
!= SQL_SUCCESS
&&
1961 retcode
!= SQL_SUCCESS_WITH_INFO
)
1963 // record was not inserted
1964 pDb
->DispNextError();
1965 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1971 // Record inserted into the datasource successfully
1974 } // wxDbTable::Insert()
1977 /********** wxDbTable::Update() **********/
1978 bool wxDbTable::Update(void)
1980 wxASSERT(!queryOnly
);
1986 // Build the SQL UPDATE statement
1987 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1989 pDb
->WriteSqlLog(sqlStmt
);
1991 #ifdef DBDEBUG_CONSOLE
1992 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1995 // Execute the SQL UPDATE statement
1996 return(execUpdate(sqlStmt
));
1998 } // wxDbTable::Update()
2001 /********** wxDbTable::Update(pSqlStmt) **********/
2002 bool wxDbTable::Update(const wxString
&pSqlStmt
)
2004 wxASSERT(!queryOnly
);
2008 pDb
->WriteSqlLog(pSqlStmt
);
2010 return(execUpdate(pSqlStmt
));
2012 } // wxDbTable::Update(pSqlStmt)
2015 /********** wxDbTable::UpdateWhere() **********/
2016 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
2018 wxASSERT(!queryOnly
);
2024 // Build the SQL UPDATE statement
2025 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
2027 pDb
->WriteSqlLog(sqlStmt
);
2029 #ifdef DBDEBUG_CONSOLE
2030 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
2033 // Execute the SQL UPDATE statement
2034 return(execUpdate(sqlStmt
));
2036 } // wxDbTable::UpdateWhere()
2039 /********** wxDbTable::Delete() **********/
2040 bool wxDbTable::Delete(void)
2042 wxASSERT(!queryOnly
);
2049 // Build the SQL DELETE statement
2050 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
2052 pDb
->WriteSqlLog(sqlStmt
);
2054 // Execute the SQL DELETE statement
2055 return(execDelete(sqlStmt
));
2057 } // wxDbTable::Delete()
2060 /********** wxDbTable::DeleteWhere() **********/
2061 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
2063 wxASSERT(!queryOnly
);
2070 // Build the SQL DELETE statement
2071 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
2073 pDb
->WriteSqlLog(sqlStmt
);
2075 // Execute the SQL DELETE statement
2076 return(execDelete(sqlStmt
));
2078 } // wxDbTable::DeleteWhere()
2081 /********** wxDbTable::DeleteMatching() **********/
2082 bool wxDbTable::DeleteMatching(void)
2084 wxASSERT(!queryOnly
);
2091 // Build the SQL DELETE statement
2092 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
2094 pDb
->WriteSqlLog(sqlStmt
);
2096 // Execute the SQL DELETE statement
2097 return(execDelete(sqlStmt
));
2099 } // wxDbTable::DeleteMatching()
2102 /********** wxDbTable::IsColNull() **********/
2103 bool wxDbTable::IsColNull(UWORD colNumber
) const
2106 This logic is just not right. It would indicate true
2107 if a numeric field were set to a value of 0.
2109 switch(colDefs[colNumber].SqlCtype)
2113 //case SQL_C_WXCHAR: SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
2114 return(((UCHAR FAR *) colDefs[colNumber].PtrDataObj)[0] == 0);
2116 return(( *((SWORD *) colDefs[colNumber].PtrDataObj)) == 0);
2118 return(( *((UWORD*) colDefs[colNumber].PtrDataObj)) == 0);
2120 return(( *((SDWORD *) colDefs[colNumber].PtrDataObj)) == 0);
2122 return(( *((UDWORD *) colDefs[colNumber].PtrDataObj)) == 0);
2124 return(( *((SFLOAT *) colDefs[colNumber].PtrDataObj)) == 0);
2126 return((*((SDOUBLE *) colDefs[colNumber].PtrDataObj)) == 0);
2127 case SQL_C_TIMESTAMP:
2128 TIMESTAMP_STRUCT *pDt;
2129 pDt = (TIMESTAMP_STRUCT *) colDefs[colNumber].PtrDataObj;
2130 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
2138 return (colDefs
[colNumber
].Null
);
2139 } // wxDbTable::IsColNull()
2142 /********** wxDbTable::CanSelectForUpdate() **********/
2143 bool wxDbTable::CanSelectForUpdate(void)
2148 if (pDb
->Dbms() == dbmsMY_SQL
)
2151 if ((pDb
->Dbms() == dbmsORACLE
) ||
2152 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
2157 } // wxDbTable::CanSelectForUpdate()
2160 /********** wxDbTable::CanUpdateByROWID() **********/
2161 bool wxDbTable::CanUpdateByROWID(void)
2164 * NOTE: Returning false for now until this can be debugged,
2165 * as the ROWID is not getting updated correctly
2169 if (pDb->Dbms() == dbmsORACLE)
2174 } // wxDbTable::CanUpdateByROWID()
2177 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2178 bool wxDbTable::IsCursorClosedOnCommit(void)
2180 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2185 } // wxDbTable::IsCursorClosedOnCommit()
2189 /********** wxDbTable::ClearMemberVar() **********/
2190 void wxDbTable::ClearMemberVar(UWORD colNumber
, bool setToNull
)
2192 wxASSERT(colNumber
< m_numCols
);
2194 switch(colDefs
[colNumber
].SqlCtype
)
2200 //case SQL_C_WXCHAR: SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
2201 ((UCHAR FAR
*) colDefs
[colNumber
].PtrDataObj
)[0] = 0;
2204 *((SWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2207 *((UWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2211 *((SDWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2214 *((UDWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2217 *((SFLOAT
*) colDefs
[colNumber
].PtrDataObj
) = 0.0f
;
2220 *((SDOUBLE
*) colDefs
[colNumber
].PtrDataObj
) = 0.0f
;
2222 case SQL_C_TIMESTAMP
:
2223 TIMESTAMP_STRUCT
*pDt
;
2224 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNumber
].PtrDataObj
;
2236 SetColNull(colNumber
);
2237 } // wxDbTable::ClearMemberVar()
2240 /********** wxDbTable::ClearMemberVars() **********/
2241 void wxDbTable::ClearMemberVars(bool setToNull
)
2245 // Loop through the columns setting each member variable to zero
2246 for (i
=0; i
< m_numCols
; i
++)
2247 ClearMemberVar((UWORD
)i
,setToNull
);
2249 } // wxDbTable::ClearMemberVars()
2252 /********** wxDbTable::SetQueryTimeout() **********/
2253 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2255 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2256 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2257 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2258 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2259 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2260 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2261 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2262 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2264 // Completed Successfully
2267 } // wxDbTable::SetQueryTimeout()
2270 /********** wxDbTable::SetColDefs() **********/
2271 bool wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2272 SWORD cType
, int size
, bool keyField
, bool updateable
,
2273 bool insertAllowed
, bool derivedColumn
)
2277 if (index
>= m_numCols
) // Columns numbers are zero based....
2279 tmpStr
.Printf(wxT("Specified column index (%d) exceeds the maximum number of columns (%d) registered for this table definition. Column definition not added."), index
, m_numCols
);
2285 if (!colDefs
) // May happen if the database connection fails
2288 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2290 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2291 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0; // Prevent buffer overrun
2293 tmpStr
.Printf(wxT("Column name '%s' is too long. Truncated to '%s'."),
2294 fieldName
.c_str(),colDefs
[index
].ColName
);
2299 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2301 colDefs
[index
].DbDataType
= dataType
;
2302 colDefs
[index
].PtrDataObj
= pData
;
2303 colDefs
[index
].SqlCtype
= cType
;
2304 colDefs
[index
].SzDataObj
= size
; //TODO: glt ??? * sizeof(wxChar) ???
2305 colDefs
[index
].KeyField
= keyField
;
2306 colDefs
[index
].DerivedCol
= derivedColumn
;
2307 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2310 colDefs
[index
].Updateable
= false;
2311 colDefs
[index
].InsertAllowed
= false;
2315 colDefs
[index
].Updateable
= updateable
;
2316 colDefs
[index
].InsertAllowed
= insertAllowed
;
2319 colDefs
[index
].Null
= false;
2323 } // wxDbTable::SetColDefs()
2326 /********** wxDbTable::SetColDefs() **********/
2327 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2330 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2336 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2338 for (index
= 0; index
< numCols
; index
++)
2340 // Process the fields
2341 switch (pColInfs
[index
].dbDataType
)
2343 case DB_DATA_TYPE_VARCHAR
:
2344 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferSize
+(1*sizeof(wxChar
))];
2345 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferSize
+(1*sizeof(wxChar
));
2346 pColDataPtrs
[index
].SqlCtype
= SQL_C_WXCHAR
;
2348 case DB_DATA_TYPE_INTEGER
:
2349 // Can be long or short
2350 if (pColInfs
[index
].bufferSize
== sizeof(long))
2352 pColDataPtrs
[index
].PtrDataObj
= new long;
2353 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2354 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2358 pColDataPtrs
[index
].PtrDataObj
= new short;
2359 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2360 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2363 case DB_DATA_TYPE_FLOAT
:
2364 // Can be float or double
2365 if (pColInfs
[index
].bufferSize
== sizeof(float))
2367 pColDataPtrs
[index
].PtrDataObj
= new float;
2368 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2369 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2373 pColDataPtrs
[index
].PtrDataObj
= new double;
2374 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2375 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2378 case DB_DATA_TYPE_DATE
:
2379 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2380 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2381 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2383 case DB_DATA_TYPE_BLOB
:
2384 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2385 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2386 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2387 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2390 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2391 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2394 // Unable to build all the column definitions, as either one of
2395 // the calls to "new" failed above, or there was a BLOB field
2396 // to have a column definition for. If BLOBs are to be used,
2397 // the other form of ::SetColDefs() must be used, as it is impossible
2398 // to know the maximum size to create the PtrDataObj to be.
2399 delete [] pColDataPtrs
;
2405 return (pColDataPtrs
);
2407 } // wxDbTable::SetColDefs()
2410 /********** wxDbTable::SetCursor() **********/
2411 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2413 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2414 hstmt
= *hstmtDefault
;
2416 hstmt
= *hstmtActivate
;
2418 } // wxDbTable::SetCursor()
2421 /********** wxDbTable::Count(const wxString &) **********/
2422 ULONG
wxDbTable::Count(const wxString
&args
)
2428 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2429 sqlStmt
= wxT("SELECT COUNT(");
2431 sqlStmt
+= wxT(") FROM ");
2432 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2433 // sqlStmt += queryTableName;
2434 #if wxODBC_BACKWARD_COMPATABILITY
2435 if (from
&& wxStrlen(from
))
2441 // Add the where clause if one is provided
2442 #if wxODBC_BACKWARD_COMPATABILITY
2443 if (where
&& wxStrlen(where
))
2448 sqlStmt
+= wxT(" WHERE ");
2452 pDb
->WriteSqlLog(sqlStmt
);
2454 // Initialize the Count cursor if it's not already initialized
2457 hstmtCount
= GetNewCursor(false,false);
2458 wxASSERT(hstmtCount
);
2463 // Execute the SQL statement
2464 if (SQLExecDirect(*hstmtCount
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2466 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2471 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2473 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2477 // Obtain the result
2478 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2480 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2485 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2486 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2488 // Return the record count
2491 } // wxDbTable::Count()
2494 /********** wxDbTable::Refresh() **********/
2495 bool wxDbTable::Refresh(void)
2499 // Switch to the internal cursor so any active cursors are not corrupted
2500 HSTMT currCursor
= GetCursor();
2501 hstmt
= hstmtInternal
;
2502 #if wxODBC_BACKWARD_COMPATABILITY
2503 // Save the where and order by clauses
2504 wxChar
*saveWhere
= where
;
2505 wxChar
*saveOrderBy
= orderBy
;
2507 wxString saveWhere
= where
;
2508 wxString saveOrderBy
= orderBy
;
2510 // Build a where clause to refetch the record with. Try and use the
2511 // ROWID if it's available, ow use the key fields.
2512 wxString whereClause
;
2513 whereClause
.Empty();
2515 if (CanUpdateByROWID())
2518 wxChar rowid
[wxDB_ROWID_LEN
+1];
2520 // Get the ROWID value. If not successful retreiving the ROWID,
2521 // simply fall down through the code and build the WHERE clause
2522 // based on the key fields.
2523 if (SQLGetData(hstmt
, (UWORD
)(m_numCols
+1), SQL_C_WXCHAR
, (UCHAR
*) rowid
, sizeof(rowid
), &cb
) == SQL_SUCCESS
)
2525 whereClause
+= pDb
->SQLTableName(queryTableName
);
2526 // whereClause += queryTableName;
2527 whereClause
+= wxT(".ROWID = '");
2528 whereClause
+= rowid
;
2529 whereClause
+= wxT("'");
2533 // If unable to use the ROWID, build a where clause from the keyfields
2534 if (wxStrlen(whereClause
) == 0)
2535 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2537 // Requery the record
2538 where
= whereClause
;
2543 if (result
&& !GetNext())
2546 // Switch back to original cursor
2547 SetCursor(&currCursor
);
2549 // Free the internal cursor
2550 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2551 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2553 // Restore the original where and order by clauses
2555 orderBy
= saveOrderBy
;
2559 } // wxDbTable::Refresh()
2562 /********** wxDbTable::SetColNull() **********/
2563 bool wxDbTable::SetColNull(UWORD colNumber
, bool set
)
2565 if (colNumber
< m_numCols
)
2567 colDefs
[colNumber
].Null
= set
;
2568 if (set
) // Blank out the values in the member variable
2569 ClearMemberVar(colNumber
, false); // Must call with false here, or infinite recursion will happen
2571 setCbValueForColumn(colNumber
);
2578 } // wxDbTable::SetColNull()
2581 /********** wxDbTable::SetColNull() **********/
2582 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2585 for (colNumber
= 0; colNumber
< m_numCols
; colNumber
++)
2587 if (!wxStricmp(colName
, colDefs
[colNumber
].ColName
))
2591 if (colNumber
< m_numCols
)
2593 colDefs
[colNumber
].Null
= set
;
2594 if (set
) // Blank out the values in the member variable
2595 ClearMemberVar((UWORD
)colNumber
,false); // Must call with false here, or infinite recursion will happen
2597 setCbValueForColumn(colNumber
);
2604 } // wxDbTable::SetColNull()
2607 /********** wxDbTable::GetNewCursor() **********/
2608 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2610 HSTMT
*newHSTMT
= new HSTMT
;
2615 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2617 pDb
->DispAllErrors(henv
, hdbc
);
2622 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2624 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2631 if (!bindCols(*newHSTMT
))
2639 SetCursor(newHSTMT
);
2643 } // wxDbTable::GetNewCursor()
2646 /********** wxDbTable::DeleteCursor() **********/
2647 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2651 if (!hstmtDel
) // Cursor already deleted
2655 ODBC 3.0 says to use this form
2656 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2659 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2661 pDb
->DispAllErrors(henv
, hdbc
);
2669 } // wxDbTable::DeleteCursor()
2671 //////////////////////////////////////////////////////////////
2672 // wxDbGrid support functions
2673 //////////////////////////////////////////////////////////////
2675 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2677 if (!m_hstmtGridQuery
)
2679 m_hstmtGridQuery
= GetNewCursor(false,false);
2680 if (!bindCols(*m_hstmtGridQuery
))
2684 m_rowmode
= rowmode
;
2687 case WX_ROW_MODE_QUERY
:
2688 SetCursor(m_hstmtGridQuery
);
2690 case WX_ROW_MODE_INDIVIDUAL
:
2691 SetCursor(hstmtDefault
);
2696 } // wxDbTable::SetRowMode()
2699 wxVariant
wxDbTable::GetColumn(const int colNumber
) const
2702 if ((colNumber
< m_numCols
) && (!IsColNull((UWORD
)colNumber
)))
2704 switch (colDefs
[colNumber
].SqlCtype
)
2708 val
= (wxChar
*)(colDefs
[colNumber
].PtrDataObj
);
2712 val
= *(long *)(colDefs
[colNumber
].PtrDataObj
);
2716 val
= (long int )(*(short *)(colDefs
[colNumber
].PtrDataObj
));
2719 val
= (long)(*(unsigned long *)(colDefs
[colNumber
].PtrDataObj
));
2722 val
= (long)(*(wxChar
*)(colDefs
[colNumber
].PtrDataObj
));
2724 case SQL_C_UTINYINT
:
2725 val
= (long)(*(wxChar
*)(colDefs
[colNumber
].PtrDataObj
));
2728 val
= (long)(*(UWORD
*)(colDefs
[colNumber
].PtrDataObj
));
2731 val
= (DATE_STRUCT
*)(colDefs
[colNumber
].PtrDataObj
);
2734 val
= (TIME_STRUCT
*)(colDefs
[colNumber
].PtrDataObj
);
2736 case SQL_C_TIMESTAMP
:
2737 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNumber
].PtrDataObj
);
2740 val
= *(double *)(colDefs
[colNumber
].PtrDataObj
);
2747 } // wxDbTable::GetCol()
2750 void wxDbTable::SetColumn(const int colNumber
, const wxVariant val
)
2752 //FIXME: Add proper wxDateTime support to wxVariant..
2755 SetColNull((UWORD
)colNumber
, val
.IsNull());
2759 if ((colDefs
[colNumber
].SqlCtype
== SQL_C_DATE
)
2760 || (colDefs
[colNumber
].SqlCtype
== SQL_C_TIME
)
2761 || (colDefs
[colNumber
].SqlCtype
== SQL_C_TIMESTAMP
))
2763 //Returns null if invalid!
2764 if (!dateval
.ParseDate(val
.GetString()))
2765 SetColNull((UWORD
)colNumber
, true);
2768 switch (colDefs
[colNumber
].SqlCtype
)
2772 csstrncpyt((wxChar
*)(colDefs
[colNumber
].PtrDataObj
),
2773 val
.GetString().c_str(),
2774 colDefs
[colNumber
].SzDataObj
-1); //TODO: glt ??? * sizeof(wxChar) ???
2778 *(long *)(colDefs
[colNumber
].PtrDataObj
) = val
;
2782 *(short *)(colDefs
[colNumber
].PtrDataObj
) = (short)val
.GetLong();
2785 *(unsigned long *)(colDefs
[colNumber
].PtrDataObj
) = val
.GetLong();
2788 *(wxChar
*)(colDefs
[colNumber
].PtrDataObj
) = val
.GetChar();
2790 case SQL_C_UTINYINT
:
2791 *(wxChar
*)(colDefs
[colNumber
].PtrDataObj
) = val
.GetChar();
2794 *(unsigned short *)(colDefs
[colNumber
].PtrDataObj
) = (unsigned short)val
.GetLong();
2796 //FIXME: Add proper wxDateTime support to wxVariant..
2799 DATE_STRUCT
*dataptr
=
2800 (DATE_STRUCT
*)colDefs
[colNumber
].PtrDataObj
;
2802 dataptr
->year
= (SWORD
)dateval
.GetYear();
2803 dataptr
->month
= (UWORD
)(dateval
.GetMonth()+1);
2804 dataptr
->day
= (UWORD
)dateval
.GetDay();
2809 TIME_STRUCT
*dataptr
=
2810 (TIME_STRUCT
*)colDefs
[colNumber
].PtrDataObj
;
2812 dataptr
->hour
= dateval
.GetHour();
2813 dataptr
->minute
= dateval
.GetMinute();
2814 dataptr
->second
= dateval
.GetSecond();
2817 case SQL_C_TIMESTAMP
:
2819 TIMESTAMP_STRUCT
*dataptr
=
2820 (TIMESTAMP_STRUCT
*)colDefs
[colNumber
].PtrDataObj
;
2821 dataptr
->year
= (SWORD
)dateval
.GetYear();
2822 dataptr
->month
= (UWORD
)(dateval
.GetMonth()+1);
2823 dataptr
->day
= (UWORD
)dateval
.GetDay();
2825 dataptr
->hour
= dateval
.GetHour();
2826 dataptr
->minute
= dateval
.GetMinute();
2827 dataptr
->second
= dateval
.GetSecond();
2831 *(double *)(colDefs
[colNumber
].PtrDataObj
) = val
;
2836 } // if (!val.IsNull())
2837 } // wxDbTable::SetCol()
2840 GenericKey
wxDbTable::GetKey()
2845 blk
= malloc(m_keysize
);
2846 blkptr
= (wxChar
*) blk
;
2849 for (i
=0; i
< m_numCols
; i
++)
2851 if (colDefs
[i
].KeyField
)
2853 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2854 blkptr
+= colDefs
[i
].SzDataObj
;
2858 GenericKey k
= GenericKey(blk
, m_keysize
);
2862 } // wxDbTable::GetKey()
2865 void wxDbTable::SetKey(const GenericKey
& k
)
2871 blkptr
= (wxChar
*)blk
;
2874 for (i
=0; i
< m_numCols
; i
++)
2876 if (colDefs
[i
].KeyField
)
2878 SetColNull((UWORD
)i
, false);
2879 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2880 blkptr
+= colDefs
[i
].SzDataObj
;
2883 } // wxDbTable::SetKey()
2886 #endif // wxUSE_ODBC