1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence, plus:
12 // Notice: This class library and its intellectual design are free of charge for use,
13 // modification, enhancement, debugging under the following conditions:
14 // 1) These classes may only be used as part of the implementation of a
15 // wxWindows-based application
16 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17 // user groups free of all charges for use with the wxWindows library.
18 // 3) These classes may not be distributed as part of any other class library,
19 // DLL, text (written or electronic), other than a complete distribution of
20 // the wxWindows GUI development toolkit.
21 ///////////////////////////////////////////////////////////////////////////////
28 #pragma implementation "dbtable.h"
31 #include "wx/wxprec.h"
37 #ifdef DBDEBUG_CONSOLE
39 #include "wx/ioswrap.h"
43 #include "wx/string.h"
44 #include "wx/object.h"
48 #include "wx/msgdlg.h"
52 #include "wx/filefn.h"
61 #include "wx/dbtable.h"
64 // The HPUX preprocessor lines below were commented out on 8/20/97
65 // because macros.h currently redefines DEBUG and is unneeded.
67 // # include <macros.h>
70 # include <sys/minmax.h>
74 ULONG lastTableID
= 0;
82 /********** wxDbColDef::wxDbColDef() Constructor **********/
83 wxDbColDef::wxDbColDef()
89 bool wxDbColDef::Initialize()
92 DbDataType
= DB_DATA_TYPE_INTEGER
;
93 SqlCtype
= SQL_C_LONG
;
98 InsertAllowed
= FALSE
;
104 } // wxDbColDef::Initialize()
107 /********** wxDbTable::wxDbTable() Constructor **********/
108 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
109 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
111 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
113 } // wxDbTable::wxDbTable()
116 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
117 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
118 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
120 wxString tempQryTblName
;
121 tempQryTblName
= qryTblName
;
122 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
124 } // wxDbTable::wxDbTable()
127 /********** wxDbTable::~wxDbTable() **********/
128 wxDbTable::~wxDbTable()
131 } // wxDbTable::~wxDbTable()
134 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
135 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
137 // Initializing member variables
138 pDb
= pwxDb
; // Pointer to the wxDb object
142 m_hstmtGridQuery
= 0;
143 hstmtDefault
= 0; // Initialized below
144 hstmtCount
= 0; // Initialized first time it is needed
151 noCols
= numColumns
; // Number of cols in the table
152 where
.Empty(); // Where clause
153 orderBy
.Empty(); // Order By clause
154 from
.Empty(); // From clause
155 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
160 queryTableName
.Empty();
162 wxASSERT(tblName
.Length());
168 tableName
= tblName
; // Table Name
169 if (tblPath
.Length())
170 tablePath
= tblPath
; // Table Path - used for dBase files
174 if (qryTblName
.Length()) // Name of the table/view to query
175 queryTableName
= qryTblName
;
177 queryTableName
= tblName
;
179 pDb
->incrementTableCount();
182 tableID
= ++lastTableID
;
183 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
186 wxTablesInUse
*tableInUse
;
187 tableInUse
= new wxTablesInUse();
188 tableInUse
->tableName
= tblName
;
189 tableInUse
->tableID
= tableID
;
190 tableInUse
->pDb
= pDb
;
191 TablesInUse
.Append(tableInUse
);
196 // Grab the HENV and HDBC from the wxDb object
197 henv
= pDb
->GetHENV();
198 hdbc
= pDb
->GetHDBC();
200 // Allocate space for column definitions
202 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
204 // Allocate statement handles for the table
207 // Allocate a separate statement handle for performing inserts
208 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
209 pDb
->DispAllErrors(henv
, hdbc
);
210 // Allocate a separate statement handle for performing deletes
211 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
212 pDb
->DispAllErrors(henv
, hdbc
);
213 // Allocate a separate statement handle for performing updates
214 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
215 pDb
->DispAllErrors(henv
, hdbc
);
217 // Allocate a separate statement handle for internal use
218 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
219 pDb
->DispAllErrors(henv
, hdbc
);
221 // Set the cursor type for the statement handles
222 cursorType
= SQL_CURSOR_STATIC
;
224 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
226 // Check to see if cursor type is supported
227 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
228 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
230 // Datasource does not support static cursors. Driver
231 // will substitute a cursor type. Call SQLGetStmtOption()
232 // to determine which cursor type was selected.
233 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
234 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
235 #ifdef DBDEBUG_CONSOLE
236 cout
<< wxT("Static cursor changed to: ");
239 case SQL_CURSOR_FORWARD_ONLY
:
240 cout
<< wxT("Forward Only");
242 case SQL_CURSOR_STATIC
:
243 cout
<< wxT("Static");
245 case SQL_CURSOR_KEYSET_DRIVEN
:
246 cout
<< wxT("Keyset Driven");
248 case SQL_CURSOR_DYNAMIC
:
249 cout
<< wxT("Dynamic");
252 cout
<< endl
<< endl
;
255 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
257 // Force the use of a forward only cursor...
258 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
259 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
261 // Should never happen
262 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
269 pDb
->DispNextError();
270 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
273 #ifdef DBDEBUG_CONSOLE
275 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
280 // Set the cursor type for the INSERT statement handle
281 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
282 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
283 // Set the cursor type for the DELETE statement handle
284 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
285 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
286 // Set the cursor type for the UPDATE statement handle
287 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
288 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
291 // Make the default cursor the active cursor
292 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
293 wxASSERT(hstmtDefault
);
294 hstmt
= *hstmtDefault
;
298 } // wxDbTable::initialize()
301 void wxDbTable::cleanup()
306 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
313 TablesInUse
.DeleteContents(TRUE
);
317 pNode
= TablesInUse
.First();
318 while (pNode
&& !found
)
320 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
323 if (!TablesInUse
.DeleteNode(pNode
))
324 wxLogDebug (s
,wxT("Unable to delete node!"));
327 pNode
= pNode
->Next();
332 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
333 wxLogDebug (msg
,wxT("NOTICE..."));
338 // Decrement the wxDb table count
340 pDb
->decrementTableCount();
342 // Delete memory allocated for column definitions
346 // Free statement handles
352 ODBC 3.0 says to use this form
353 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
355 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
356 pDb
->DispAllErrors(henv
, hdbc
);
362 ODBC 3.0 says to use this form
363 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
365 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
366 pDb
->DispAllErrors(henv
, hdbc
);
372 ODBC 3.0 says to use this form
373 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
375 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
376 pDb
->DispAllErrors(henv
, hdbc
);
382 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
383 pDb
->DispAllErrors(henv
, hdbc
);
386 // Delete dynamically allocated cursors
388 DeleteCursor(hstmtDefault
);
391 DeleteCursor(hstmtCount
);
393 if (m_hstmtGridQuery
)
394 DeleteCursor(m_hstmtGridQuery
);
396 } // wxDbTable::cleanup()
399 /***************************** PRIVATE FUNCTIONS *****************************/
402 /********** wxDbTable::bindParams() **********/
403 bool wxDbTable::bindParams(bool forUpdate
)
405 wxASSERT(!queryOnly
);
410 UDWORD precision
= 0;
413 // Bind each column of the table that should be bound
414 // to a parameter marker
418 for (i
=0, colNo
=1; i
< noCols
; i
++)
422 if (!colDefs
[i
].Updateable
)
427 if (!colDefs
[i
].InsertAllowed
)
431 switch(colDefs
[i
].DbDataType
)
433 case DB_DATA_TYPE_VARCHAR
:
434 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
435 precision
= colDefs
[i
].SzDataObj
;
438 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
440 colDefs
[i
].CbValue
= SQL_NTS
;
442 case DB_DATA_TYPE_INTEGER
:
443 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
444 precision
= pDb
->GetTypeInfInteger().Precision
;
447 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
449 colDefs
[i
].CbValue
= 0;
451 case DB_DATA_TYPE_FLOAT
:
452 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
453 precision
= pDb
->GetTypeInfFloat().Precision
;
454 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
455 // SQL Sybase Anywhere v5.5 returned a negative number for the
456 // MaxScale. This caused ODBC to kick out an error on ibscale.
457 // I check for this here and set the scale = precision.
459 // scale = (short) precision;
461 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
463 colDefs
[i
].CbValue
= 0;
465 case DB_DATA_TYPE_DATE
:
466 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
467 precision
= pDb
->GetTypeInfDate().Precision
;
470 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
472 colDefs
[i
].CbValue
= 0;
474 case DB_DATA_TYPE_BLOB
:
475 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
479 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
481 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
486 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
487 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
488 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
490 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
495 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
496 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
497 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
499 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
504 // Completed successfully
507 } // wxDbTable::bindParams()
510 /********** wxDbTable::bindInsertParams() **********/
511 bool wxDbTable::bindInsertParams(void)
513 return bindParams(FALSE
);
514 } // wxDbTable::bindInsertParams()
517 /********** wxDbTable::bindUpdateParams() **********/
518 bool wxDbTable::bindUpdateParams(void)
520 return bindParams(TRUE
);
521 } // wxDbTable::bindUpdateParams()
524 /********** wxDbTable::bindCols() **********/
525 bool wxDbTable::bindCols(HSTMT cursor
)
527 // Bind each column of the table to a memory address for fetching data
529 for (i
= 0; i
< noCols
; i
++)
531 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
532 colDefs
[i
].SzDataObj
, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
534 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
538 // Completed successfully
541 } // wxDbTable::bindCols()
544 /********** wxDbTable::getRec() **********/
545 bool wxDbTable::getRec(UWORD fetchType
)
549 if (!pDb
->FwdOnlyCursors())
551 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
555 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
556 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
558 if (retcode
== SQL_NO_DATA_FOUND
)
561 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
565 // Set the Null member variable to indicate the Null state
566 // of each column just read in.
568 for (i
= 0; i
< noCols
; i
++)
569 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
574 // Fetch the next record from the record set
575 retcode
= SQLFetch(hstmt
);
576 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
578 if (retcode
== SQL_NO_DATA_FOUND
)
581 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
585 // Set the Null member variable to indicate the Null state
586 // of each column just read in.
588 for (i
= 0; i
< noCols
; i
++)
589 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
593 // Completed successfully
596 } // wxDbTable::getRec()
599 /********** wxDbTable::execDelete() **********/
600 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
604 // Execute the DELETE statement
605 retcode
= SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
607 if (retcode
== SQL_SUCCESS
||
608 retcode
== SQL_NO_DATA_FOUND
||
609 retcode
== SQL_SUCCESS_WITH_INFO
)
611 // Record deleted successfully
615 // Problem deleting record
616 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
618 } // wxDbTable::execDelete()
621 /********** wxDbTable::execUpdate() **********/
622 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
626 // Execute the UPDATE statement
627 retcode
= SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
629 if (retcode
== SQL_SUCCESS
||
630 retcode
== SQL_NO_DATA_FOUND
||
631 retcode
== SQL_SUCCESS_WITH_INFO
)
633 // Record updated successfully
637 // Problem updating record
638 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
640 } // wxDbTable::execUpdate()
643 /********** wxDbTable::query() **********/
644 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
649 // The user may wish to select for update, but the DBMS may not be capable
650 selectForUpdate
= CanSelectForUpdate();
652 selectForUpdate
= FALSE
;
654 // Set the SQL SELECT string
655 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
656 { // so generate a select statement.
657 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
658 pDb
->WriteSqlLog(sqlStmt
);
661 // Make sure the cursor is closed first
662 if (!CloseCursor(hstmt
))
665 // Execute the SQL SELECT statement
667 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
668 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
669 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
671 // Completed successfully
674 } // wxDbTable::query()
677 /***************************** PUBLIC FUNCTIONS *****************************/
680 /********** wxDbTable::Open() **********/
681 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
691 // Calculate the maximum size of the concatenated
692 // keys for use with wxDbGrid
694 for (i
=0; i
< noCols
; i
++)
696 if (colDefs
[i
].KeyField
)
699 m_keysize
+= colDefs
[i
].SzDataObj
;
704 // Verify that the table exists in the database
705 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
707 s
= wxT("Table/view does not exist in the database");
708 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
709 s
+= wxT(", or you have no permissions.\n");
713 else if (checkPrivileges
)
715 // Verify the user has rights to access the table.
716 // Shortcut boolean evaluation to optimize out call to
719 // Unfortunately this optimization doesn't seem to be
721 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
722 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
723 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
730 if (!tablePath
.IsEmpty())
731 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
733 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
736 pDb
->LogError(p
.GetData());
741 // Bind the member variables for field exchange between
742 // the wxDbTable object and the ODBC record.
745 if (!bindInsertParams()) // Inserts
748 if (!bindUpdateParams()) // Updates
752 if (!bindCols(*hstmtDefault
)) // Selects
755 if (!bindCols(hstmtInternal
)) // Internal use only
759 * Do NOT bind the hstmtCount cursor!!!
762 // Build an insert statement using parameter markers
763 if (!queryOnly
&& noCols
> 0)
765 bool needComma
= FALSE
;
766 sqlStmt
.Printf(wxT("INSERT INTO %s ("), pDb
->SQLTableName(tableName
.c_str()));
767 for (i
= 0; i
< noCols
; i
++)
769 if (! colDefs
[i
].InsertAllowed
)
773 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
774 // sqlStmt += colDefs[i].ColName;
778 sqlStmt
+= wxT(") VALUES (");
780 int insertableCount
= 0;
782 for (i
= 0; i
< noCols
; i
++)
784 if (! colDefs
[i
].InsertAllowed
)
794 // Prepare the insert statement for execution
797 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
798 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
804 // Completed successfully
807 } // wxDbTable::Open()
810 /********** wxDbTable::Query() **********/
811 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
814 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
816 } // wxDbTable::Query()
819 /********** wxDbTable::QueryBySqlStmt() **********/
820 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
822 pDb
->WriteSqlLog(pSqlStmt
);
824 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
826 } // wxDbTable::QueryBySqlStmt()
829 /********** wxDbTable::QueryMatching() **********/
830 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
833 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
835 } // wxDbTable::QueryMatching()
838 /********** wxDbTable::QueryOnKeyFields() **********/
839 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
842 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
844 } // wxDbTable::QueryOnKeyFields()
847 /********** wxDbTable::GetPrev() **********/
848 bool wxDbTable::GetPrev(void)
850 if (pDb
->FwdOnlyCursors())
852 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
856 return(getRec(SQL_FETCH_PRIOR
));
858 } // wxDbTable::GetPrev()
861 /********** wxDbTable::operator-- **********/
862 bool wxDbTable::operator--(int)
864 if (pDb
->FwdOnlyCursors())
866 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
870 return(getRec(SQL_FETCH_PRIOR
));
872 } // wxDbTable::operator--
875 /********** wxDbTable::GetFirst() **********/
876 bool wxDbTable::GetFirst(void)
878 if (pDb
->FwdOnlyCursors())
880 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
884 return(getRec(SQL_FETCH_FIRST
));
886 } // wxDbTable::GetFirst()
889 /********** wxDbTable::GetLast() **********/
890 bool wxDbTable::GetLast(void)
892 if (pDb
->FwdOnlyCursors())
894 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
898 return(getRec(SQL_FETCH_LAST
));
900 } // wxDbTable::GetLast()
903 /********** wxDbTable::BuildDeleteStmt() **********/
904 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
906 wxASSERT(!queryOnly
);
910 wxString whereClause
;
914 // Handle the case of DeleteWhere() and the where clause is blank. It should
915 // delete all records from the database in this case.
916 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
918 pSqlStmt
.Printf(wxT("DELETE FROM %s"), pDb
->SQLTableName(tableName
.c_str()));
922 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "), pDb
->SQLTableName(tableName
.c_str()));
924 // Append the WHERE clause to the SQL DELETE statement
927 case DB_DEL_KEYFIELDS
:
928 // If the datasource supports the ROWID column, build
929 // the where on ROWID for efficiency purposes.
930 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
934 wxChar rowid
[wxDB_ROWID_LEN
+1];
936 // Get the ROWID value. If not successful retreiving the ROWID,
937 // simply fall down through the code and build the WHERE clause
938 // based on the key fields.
939 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
941 pSqlStmt
+= wxT("ROWID = '");
943 pSqlStmt
+= wxT("'");
947 // Unable to delete by ROWID, so build a WHERE
948 // clause based on the keyfields.
949 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
950 pSqlStmt
+= whereClause
;
953 pSqlStmt
+= pWhereClause
;
955 case DB_DEL_MATCHING
:
956 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
957 pSqlStmt
+= whereClause
;
961 } // BuildDeleteStmt()
964 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
965 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
967 wxString tempSqlStmt
;
968 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
969 wxStrcpy(pSqlStmt
, tempSqlStmt
);
970 } // wxDbTable::BuildDeleteStmt()
973 /********** wxDbTable::BuildSelectStmt() **********/
974 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
976 wxString whereClause
;
979 // Build a select statement to query the database
980 pSqlStmt
= wxT("SELECT ");
982 // SELECT DISTINCT values only?
984 pSqlStmt
+= wxT("DISTINCT ");
986 // Was a FROM clause specified to join tables to the base table?
987 // Available for ::Query() only!!!
988 bool appendFromClause
= FALSE
;
989 #if wxODBC_BACKWARD_COMPATABILITY
990 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
991 appendFromClause
= TRUE
;
993 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
994 appendFromClause
= TRUE
;
997 // Add the column list
999 for (i
= 0; i
< noCols
; i
++)
1001 // If joining tables, the base table column names must be qualified to avoid ambiguity
1002 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1004 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1005 // pSqlStmt += queryTableName;
1006 pSqlStmt
+= wxT(".");
1008 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1009 // pSqlStmt += colDefs[i].ColName;
1011 pSqlStmt
+= wxT(",");
1014 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1015 // the ROWID if querying distinct records. The rowid will always be unique.
1016 if (!distinct
&& CanUpdByROWID())
1018 // If joining tables, the base table column names must be qualified to avoid ambiguity
1019 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1021 pSqlStmt
+= wxT(",");
1022 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1023 // pSqlStmt += queryTableName;
1024 pSqlStmt
+= wxT(".ROWID");
1027 pSqlStmt
+= wxT(",ROWID");
1030 // Append the FROM tablename portion
1031 pSqlStmt
+= wxT(" FROM ");
1032 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1033 // pSqlStmt += queryTableName;
1035 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1036 // The HOLDLOCK keyword follows the table name in the from clause.
1037 // Each table in the from clause must specify HOLDLOCK or
1038 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1039 // is parsed but ignored in SYBASE Transact-SQL.
1040 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1041 pSqlStmt
+= wxT(" HOLDLOCK");
1043 if (appendFromClause
)
1046 // Append the WHERE clause. Either append the where clause for the class
1047 // or build a where clause. The typeOfSelect determines this.
1048 switch(typeOfSelect
)
1050 case DB_SELECT_WHERE
:
1051 #if wxODBC_BACKWARD_COMPATABILITY
1052 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1054 if (where
.Length()) // May not want a where clause!!!
1057 pSqlStmt
+= wxT(" WHERE ");
1061 case DB_SELECT_KEYFIELDS
:
1062 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1063 if (whereClause
.Length())
1065 pSqlStmt
+= wxT(" WHERE ");
1066 pSqlStmt
+= whereClause
;
1069 case DB_SELECT_MATCHING
:
1070 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1071 if (whereClause
.Length())
1073 pSqlStmt
+= wxT(" WHERE ");
1074 pSqlStmt
+= whereClause
;
1079 // Append the ORDER BY clause
1080 #if wxODBC_BACKWARD_COMPATABILITY
1081 if (orderBy
&& wxStrlen(orderBy
))
1083 if (orderBy
.Length())
1086 pSqlStmt
+= wxT(" ORDER BY ");
1087 pSqlStmt
+= orderBy
;
1090 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1091 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1092 // HOLDLOCK for Sybase.
1093 if (selectForUpdate
&& CanSelectForUpdate())
1094 pSqlStmt
+= wxT(" FOR UPDATE");
1096 } // wxDbTable::BuildSelectStmt()
1099 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1100 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1102 wxString tempSqlStmt
;
1103 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1104 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1105 } // wxDbTable::BuildSelectStmt()
1108 /********** wxDbTable::BuildUpdateStmt() **********/
1109 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1111 wxASSERT(!queryOnly
);
1115 wxString whereClause
;
1116 whereClause
.Empty();
1118 bool firstColumn
= TRUE
;
1120 pSqlStmt
.Printf(wxT("UPDATE %s SET "), pDb
->SQLTableName(tableName
.Upper().c_str()));
1122 // Append a list of columns to be updated
1124 for (i
= 0; i
< noCols
; i
++)
1126 // Only append Updateable columns
1127 if (colDefs
[i
].Updateable
)
1130 pSqlStmt
+= wxT(",");
1132 firstColumn
= FALSE
;
1134 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1135 // pSqlStmt += colDefs[i].ColName;
1136 pSqlStmt
+= wxT(" = ?");
1140 // Append the WHERE clause to the SQL UPDATE statement
1141 pSqlStmt
+= wxT(" WHERE ");
1144 case DB_UPD_KEYFIELDS
:
1145 // If the datasource supports the ROWID column, build
1146 // the where on ROWID for efficiency purposes.
1147 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1148 if (CanUpdByROWID())
1151 wxChar rowid
[wxDB_ROWID_LEN
+1];
1153 // Get the ROWID value. If not successful retreiving the ROWID,
1154 // simply fall down through the code and build the WHERE clause
1155 // based on the key fields.
1156 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1158 pSqlStmt
+= wxT("ROWID = '");
1160 pSqlStmt
+= wxT("'");
1164 // Unable to delete by ROWID, so build a WHERE
1165 // clause based on the keyfields.
1166 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1167 pSqlStmt
+= whereClause
;
1170 pSqlStmt
+= pWhereClause
;
1173 } // BuildUpdateStmt()
1176 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1177 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1179 wxString tempSqlStmt
;
1180 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1181 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1182 } // BuildUpdateStmt()
1185 /********** wxDbTable::BuildWhereClause() **********/
1186 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1187 const wxString
&qualTableName
, bool useLikeComparison
)
1189 * Note: BuildWhereClause() currently ignores timestamp columns.
1190 * They are not included as part of the where clause.
1193 bool moreThanOneColumn
= FALSE
;
1196 // Loop through the columns building a where clause as you go
1198 for (i
= 0; i
< noCols
; i
++)
1200 // Determine if this column should be included in the WHERE clause
1201 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1202 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1204 // Skip over timestamp columns
1205 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1207 // If there is more than 1 column, join them with the keyword "AND"
1208 if (moreThanOneColumn
)
1209 pWhereClause
+= wxT(" AND ");
1211 moreThanOneColumn
= TRUE
;
1212 // Concatenate where phrase for the column
1213 if (qualTableName
.Length())
1215 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1216 // pWhereClause += qualTableName;
1217 pWhereClause
+= wxT(".");
1219 pWhereClause
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1220 // pWhereClause += colDefs[i].ColName;
1221 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1222 pWhereClause
+= wxT(" LIKE ");
1224 pWhereClause
+= wxT(" = ");
1225 switch(colDefs
[i
].SqlCtype
)
1228 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1231 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[i
].PtrDataObj
));
1234 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[i
].PtrDataObj
));
1237 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1240 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1243 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1246 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1249 pWhereClause
+= colValue
;
1252 } // wxDbTable::BuildWhereClause()
1255 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1256 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1257 const wxString
&qualTableName
, bool useLikeComparison
)
1259 wxString tempSqlStmt
;
1260 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1261 wxStrcpy(pWhereClause
, tempSqlStmt
);
1262 } // wxDbTable::BuildWhereClause()
1265 /********** wxDbTable::GetRowNum() **********/
1266 UWORD
wxDbTable::GetRowNum(void)
1270 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1272 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1276 // Completed successfully
1277 return((UWORD
) rowNum
);
1279 } // wxDbTable::GetRowNum()
1282 /********** wxDbTable::CloseCursor() **********/
1283 bool wxDbTable::CloseCursor(HSTMT cursor
)
1285 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1286 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1288 // Completed successfully
1291 } // wxDbTable::CloseCursor()
1294 /********** wxDbTable::CreateTable() **********/
1295 bool wxDbTable::CreateTable(bool attemptDrop
)
1303 #ifdef DBDEBUG_CONSOLE
1304 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1308 if (attemptDrop
&& !DropTable())
1312 #ifdef DBDEBUG_CONSOLE
1313 for (i
= 0; i
< noCols
; i
++)
1315 // Exclude derived columns since they are NOT part of the base table
1316 if (colDefs
[i
].DerivedCol
)
1318 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1319 switch(colDefs
[i
].DbDataType
)
1321 case DB_DATA_TYPE_VARCHAR
:
1322 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1324 case DB_DATA_TYPE_INTEGER
:
1325 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1327 case DB_DATA_TYPE_FLOAT
:
1328 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1330 case DB_DATA_TYPE_DATE
:
1331 cout
<< pDb
->GetTypeInfDate().TypeName
;
1333 case DB_DATA_TYPE_BLOB
:
1334 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1341 // Build a CREATE TABLE string from the colDefs structure.
1342 bool needComma
= FALSE
;
1344 sqlStmt
.Printf(wxT("CREATE TABLE %s ("), pDb
->SQLTableName(tableName
.c_str()));
1346 for (i
= 0; i
< noCols
; i
++)
1348 // Exclude derived columns since they are NOT part of the base table
1349 if (colDefs
[i
].DerivedCol
)
1353 sqlStmt
+= wxT(",");
1355 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1356 // sqlStmt += colDefs[i].ColName;
1357 sqlStmt
+= wxT(" ");
1359 switch(colDefs
[i
].DbDataType
)
1361 case DB_DATA_TYPE_VARCHAR
:
1362 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1364 case DB_DATA_TYPE_INTEGER
:
1365 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1367 case DB_DATA_TYPE_FLOAT
:
1368 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1370 case DB_DATA_TYPE_DATE
:
1371 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1373 case DB_DATA_TYPE_BLOB
:
1374 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1377 // For varchars, append the size of the string
1378 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)// ||
1379 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1382 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1386 if (pDb
->Dbms() == dbmsDB2
||
1387 pDb
->Dbms() == dbmsMY_SQL
||
1388 pDb
->Dbms() == dbmsSYBASE_ASE
||
1389 pDb
->Dbms() == dbmsINTERBASE
||
1390 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1392 if (colDefs
[i
].KeyField
)
1394 sqlStmt
+= wxT(" NOT NULL");
1400 // If there is a primary key defined, include it in the create statement
1401 for (i
= j
= 0; i
< noCols
; i
++)
1403 if (colDefs
[i
].KeyField
)
1409 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1411 switch (pDb
->Dbms())
1415 case dbmsSYBASE_ASA
:
1416 case dbmsSYBASE_ASE
:
1419 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1420 sqlStmt
+= wxT(",PRIMARY KEY (");
1425 sqlStmt
+= wxT(",CONSTRAINT ");
1426 // DB2 is limited to 18 characters for index names
1427 if (pDb
->Dbms() == dbmsDB2
)
1429 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."));
1430 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1431 // sqlStmt += tableName.substr(0, 13);
1434 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1435 // sqlStmt += tableName;
1437 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1442 // List column name(s) of column(s) comprising the primary key
1443 for (i
= j
= 0; i
< noCols
; i
++)
1445 if (colDefs
[i
].KeyField
)
1447 if (j
++) // Multi part key, comma separate names
1448 sqlStmt
+= wxT(",");
1449 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1450 // sqlStmt += colDefs[i].ColName;
1453 sqlStmt
+= wxT(")");
1455 if (pDb
->Dbms() == dbmsINFORMIX
||
1456 pDb
->Dbms() == dbmsSYBASE_ASA
||
1457 pDb
->Dbms() == dbmsSYBASE_ASE
)
1459 sqlStmt
+= wxT(" CONSTRAINT ");
1460 sqlStmt
+= pDb
->SQLTableName(tableName
);
1461 // sqlStmt += tableName;
1462 sqlStmt
+= wxT("_PIDX");
1465 // Append the closing parentheses for the create table statement
1466 sqlStmt
+= wxT(")");
1468 pDb
->WriteSqlLog(sqlStmt
);
1470 #ifdef DBDEBUG_CONSOLE
1471 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1474 // Execute the CREATE TABLE statement
1475 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1476 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1478 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1479 pDb
->RollbackTrans();
1484 // Commit the transaction and close the cursor
1485 if (!pDb
->CommitTrans())
1487 if (!CloseCursor(hstmt
))
1490 // Database table created successfully
1493 } // wxDbTable::CreateTable()
1496 /********** wxDbTable::DropTable() **********/
1497 bool wxDbTable::DropTable()
1499 // NOTE: This function returns TRUE if the Table does not exist, but
1500 // only for identified databases. Code will need to be added
1501 // below for any other databases when those databases are defined
1502 // to handle this situation consistently
1506 sqlStmt
.Printf(wxT("DROP TABLE %s"), pDb
->SQLTableName(tableName
.c_str()));
1508 pDb
->WriteSqlLog(sqlStmt
);
1510 #ifdef DBDEBUG_CONSOLE
1511 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1514 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1515 if (retcode
!= SQL_SUCCESS
)
1517 // Check for "Base table not found" error and ignore
1518 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1519 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1520 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1522 // Check for product specific error codes
1523 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1524 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1525 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1526 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1528 pDb
->DispNextError();
1529 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1530 pDb
->RollbackTrans();
1531 // CloseCursor(hstmt);
1537 // Commit the transaction and close the cursor
1538 if (! pDb
->CommitTrans())
1540 if (! CloseCursor(hstmt
))
1544 } // wxDbTable::DropTable()
1547 /********** wxDbTable::CreateIndex() **********/
1548 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1549 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1553 // Drop the index first
1554 if (attemptDrop
&& !DropIndex(idxName
))
1557 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1558 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1559 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1560 // table was created, then months later you determine that an additional index while
1561 // give better performance, so you want to add an index).
1563 // The following block of code will modify the column definition to make the column be
1564 // defined with the "NOT NULL" qualifier.
1565 if (pDb
->Dbms() == dbmsMY_SQL
)
1570 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1574 // Find the column definition that has the ColName that matches the
1575 // index column name. We need to do this to get the DB_DATA_TYPE of
1576 // the index column, as MySQL's syntax for the ALTER column requires
1578 while (!found
&& (j
< this->noCols
))
1580 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1588 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1589 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1594 wxODBC_ERRORS retcode
;
1595 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1596 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1597 // This line is just here for debug checking of the value
1598 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1608 pDb
->RollbackTrans();
1613 // Build a CREATE INDEX statement
1614 sqlStmt
= wxT("CREATE ");
1616 sqlStmt
+= wxT("UNIQUE ");
1618 sqlStmt
+= wxT("INDEX ");
1619 sqlStmt
+= pDb
->SQLTableName(idxName
);
1620 sqlStmt
+= wxT(" ON ");
1622 sqlStmt
+= pDb
->SQLTableName(tableName
);
1623 // sqlStmt += tableName;
1624 sqlStmt
+= wxT(" (");
1626 // Append list of columns making up index
1628 for (i
= 0; i
< noIdxCols
; i
++)
1630 sqlStmt
+= pDb
->SQLColumnName(pIdxDefs
[i
].ColName
);
1631 // sqlStmt += pIdxDefs[i].ColName;
1633 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1634 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (strncmp(pDb
->dbInf
.dbmsVer
,"07",2)==0)) &&
1635 !(pDb
->Dbms() == dbmsPOSTGRES
))
1637 if (pIdxDefs
[i
].Ascending
)
1638 sqlStmt
+= wxT(" ASC");
1640 sqlStmt
+= wxT(" DESC");
1643 wxASSERT_MSG(pIdxDefs
[i
].Ascending
, "Datasource does not support DESCending index columns");
1645 if ((i
+ 1) < noIdxCols
)
1646 sqlStmt
+= wxT(",");
1649 // Append closing parentheses
1650 sqlStmt
+= wxT(")");
1652 pDb
->WriteSqlLog(sqlStmt
);
1654 #ifdef DBDEBUG_CONSOLE
1655 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1658 // Execute the CREATE INDEX statement
1659 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1661 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1662 pDb
->RollbackTrans();
1667 // Commit the transaction and close the cursor
1668 if (! pDb
->CommitTrans())
1670 if (! CloseCursor(hstmt
))
1673 // Index Created Successfully
1676 } // wxDbTable::CreateIndex()
1679 /********** wxDbTable::DropIndex() **********/
1680 bool wxDbTable::DropIndex(const wxString
&idxName
)
1682 // NOTE: This function returns TRUE if the Index does not exist, but
1683 // only for identified databases. Code will need to be added
1684 // below for any other databases when those databases are defined
1685 // to handle this situation consistently
1689 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1690 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1691 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),pDb
->SQLTableName(idxName
.c_str()), pDb
->SQLTableName(tableName
.c_str()));
1692 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1693 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1694 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"), pDb
->SQLTableName(tableName
.c_str()), pDb
->SQLTableName(idxName
.c_str()));
1696 sqlStmt
.Printf(wxT("DROP INDEX %s"),pDb
->SQLTableName(idxName
.c_str()));
1698 pDb
->WriteSqlLog(sqlStmt
);
1700 #ifdef DBDEBUG_CONSOLE
1701 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1704 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1706 // Check for "Index not found" error and ignore
1707 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1708 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1710 // Check for product specific error codes
1711 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1712 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1713 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1714 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1715 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1716 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1717 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1720 pDb
->DispNextError();
1721 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1722 pDb
->RollbackTrans();
1729 // Commit the transaction and close the cursor
1730 if (! pDb
->CommitTrans())
1732 if (! CloseCursor(hstmt
))
1736 } // wxDbTable::DropIndex()
1739 /********** wxDbTable::SetOrderByColNums() **********/
1740 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1742 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1748 va_start(argptr
, first
); /* Initialize variable arguments. */
1749 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1751 // Make sure the passed in column number
1752 // is within the valid range of columns
1754 // Valid columns are 0 thru noCols-1
1755 if (colNo
>= noCols
|| colNo
< 0)
1762 tempStr
+= wxT(",");
1764 tempStr
+= colDefs
[colNo
].ColName
;
1765 colNo
= va_arg (argptr
, int);
1767 va_end (argptr
); /* Reset variable arguments. */
1769 SetOrderByClause(tempStr
);
1772 } // wxDbTable::SetOrderByColNums()
1775 /********** wxDbTable::Insert() **********/
1776 int wxDbTable::Insert(void)
1778 wxASSERT(!queryOnly
);
1779 if (queryOnly
|| !insertable
)
1784 // Insert the record by executing the already prepared insert statement
1786 retcode
=SQLExecute(hstmtInsert
);
1787 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1789 // Check to see if integrity constraint was violated
1790 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1791 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1792 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1795 pDb
->DispNextError();
1796 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1801 // Record inserted into the datasource successfully
1804 } // wxDbTable::Insert()
1807 /********** wxDbTable::Update() **********/
1808 bool wxDbTable::Update(void)
1810 wxASSERT(!queryOnly
);
1816 // Build the SQL UPDATE statement
1817 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1819 pDb
->WriteSqlLog(sqlStmt
);
1821 #ifdef DBDEBUG_CONSOLE
1822 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1825 // Execute the SQL UPDATE statement
1826 return(execUpdate(sqlStmt
));
1828 } // wxDbTable::Update()
1831 /********** wxDbTable::Update(pSqlStmt) **********/
1832 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1834 wxASSERT(!queryOnly
);
1838 pDb
->WriteSqlLog(pSqlStmt
);
1840 return(execUpdate(pSqlStmt
));
1842 } // wxDbTable::Update(pSqlStmt)
1845 /********** wxDbTable::UpdateWhere() **********/
1846 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1848 wxASSERT(!queryOnly
);
1854 // Build the SQL UPDATE statement
1855 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1857 pDb
->WriteSqlLog(sqlStmt
);
1859 #ifdef DBDEBUG_CONSOLE
1860 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1863 // Execute the SQL UPDATE statement
1864 return(execUpdate(sqlStmt
));
1866 } // wxDbTable::UpdateWhere()
1869 /********** wxDbTable::Delete() **********/
1870 bool wxDbTable::Delete(void)
1872 wxASSERT(!queryOnly
);
1879 // Build the SQL DELETE statement
1880 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1882 pDb
->WriteSqlLog(sqlStmt
);
1884 // Execute the SQL DELETE statement
1885 return(execDelete(sqlStmt
));
1887 } // wxDbTable::Delete()
1890 /********** wxDbTable::DeleteWhere() **********/
1891 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1893 wxASSERT(!queryOnly
);
1900 // Build the SQL DELETE statement
1901 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1903 pDb
->WriteSqlLog(sqlStmt
);
1905 // Execute the SQL DELETE statement
1906 return(execDelete(sqlStmt
));
1908 } // wxDbTable::DeleteWhere()
1911 /********** wxDbTable::DeleteMatching() **********/
1912 bool wxDbTable::DeleteMatching(void)
1914 wxASSERT(!queryOnly
);
1921 // Build the SQL DELETE statement
1922 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1924 pDb
->WriteSqlLog(sqlStmt
);
1926 // Execute the SQL DELETE statement
1927 return(execDelete(sqlStmt
));
1929 } // wxDbTable::DeleteMatching()
1932 /********** wxDbTable::IsColNull() **********/
1933 bool wxDbTable::IsColNull(UWORD colNo
) const
1936 This logic is just not right. It would indicate TRUE
1937 if a numeric field were set to a value of 0.
1939 switch(colDefs[colNo].SqlCtype)
1942 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1944 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1946 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1948 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1950 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1952 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1954 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1955 case SQL_C_TIMESTAMP:
1956 TIMESTAMP_STRUCT *pDt;
1957 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1958 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1966 return (colDefs
[colNo
].Null
);
1967 } // wxDbTable::IsColNull()
1970 /********** wxDbTable::CanSelectForUpdate() **********/
1971 bool wxDbTable::CanSelectForUpdate(void)
1976 if (pDb
->Dbms() == dbmsMY_SQL
)
1979 if ((pDb
->Dbms() == dbmsORACLE
) ||
1980 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
1985 } // wxDbTable::CanSelectForUpdate()
1988 /********** wxDbTable::CanUpdByROWID() **********/
1989 bool wxDbTable::CanUpdByROWID(void)
1992 * NOTE: Returning FALSE for now until this can be debugged,
1993 * as the ROWID is not getting updated correctly
1997 if (pDb->Dbms() == dbmsORACLE)
2002 } // wxDbTable::CanUpdByROWID()
2005 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2006 bool wxDbTable::IsCursorClosedOnCommit(void)
2008 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2013 } // wxDbTable::IsCursorClosedOnCommit()
2017 /********** wxDbTable::ClearMemberVar() **********/
2018 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2020 wxASSERT(colNo
< noCols
);
2022 switch(colDefs
[colNo
].SqlCtype
)
2025 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2028 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2031 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2034 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2037 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2040 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2043 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2045 case SQL_C_TIMESTAMP
:
2046 TIMESTAMP_STRUCT
*pDt
;
2047 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2060 } // wxDbTable::ClearMemberVar()
2063 /********** wxDbTable::ClearMemberVars() **********/
2064 void wxDbTable::ClearMemberVars(bool setToNull
)
2068 // Loop through the columns setting each member variable to zero
2069 for (i
=0; i
< noCols
; i
++)
2070 ClearMemberVar(i
,setToNull
);
2072 } // wxDbTable::ClearMemberVars()
2075 /********** wxDbTable::SetQueryTimeout() **********/
2076 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2078 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2079 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2080 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2081 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2082 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2083 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2084 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2085 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2087 // Completed Successfully
2090 } // wxDbTable::SetQueryTimeout()
2093 /********** wxDbTable::SetColDefs() **********/
2094 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2095 SWORD cType
, int size
, bool keyField
, bool upd
,
2096 bool insAllow
, bool derivedCol
)
2098 if (!colDefs
) // May happen if the database connection fails
2101 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2103 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2104 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2108 tmpMsg
.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2109 fieldName
.c_str(),colDefs
[index
].ColName
);
2111 #endif // __WXDEBUG__
2114 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2116 colDefs
[index
].DbDataType
= dataType
;
2117 colDefs
[index
].PtrDataObj
= pData
;
2118 colDefs
[index
].SqlCtype
= cType
;
2119 colDefs
[index
].SzDataObj
= size
;
2120 colDefs
[index
].KeyField
= keyField
;
2121 colDefs
[index
].DerivedCol
= derivedCol
;
2122 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2125 colDefs
[index
].Updateable
= FALSE
;
2126 colDefs
[index
].InsertAllowed
= FALSE
;
2130 colDefs
[index
].Updateable
= upd
;
2131 colDefs
[index
].InsertAllowed
= insAllow
;
2134 colDefs
[index
].Null
= FALSE
;
2136 } // wxDbTable::SetColDefs()
2139 /********** wxDbTable::SetColDefs() **********/
2140 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2143 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2149 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2151 for (index
= 0; index
< numCols
; index
++)
2153 // Process the fields
2154 switch (pColInfs
[index
].dbDataType
)
2156 case DB_DATA_TYPE_VARCHAR
:
2157 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2158 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2159 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2161 case DB_DATA_TYPE_INTEGER
:
2162 // Can be long or short
2163 if (pColInfs
[index
].bufferLength
== sizeof(long))
2165 pColDataPtrs
[index
].PtrDataObj
= new long;
2166 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2167 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2171 pColDataPtrs
[index
].PtrDataObj
= new short;
2172 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2173 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2176 case DB_DATA_TYPE_FLOAT
:
2177 // Can be float or double
2178 if (pColInfs
[index
].bufferLength
== sizeof(float))
2180 pColDataPtrs
[index
].PtrDataObj
= new float;
2181 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2182 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2186 pColDataPtrs
[index
].PtrDataObj
= new double;
2187 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2188 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2191 case DB_DATA_TYPE_DATE
:
2192 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2193 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2194 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2196 case DB_DATA_TYPE_BLOB
:
2197 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2198 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2199 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2200 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2203 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2204 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2207 // Unable to build all the column definitions, as either one of
2208 // the calls to "new" failed above, or there was a BLOB field
2209 // to have a column definition for. If BLOBs are to be used,
2210 // the other form of ::SetColDefs() must be used, as it is impossible
2211 // to know the maximum size to create the PtrDataObj to be.
2212 delete [] pColDataPtrs
;
2218 return (pColDataPtrs
);
2220 } // wxDbTable::SetColDefs()
2223 /********** wxDbTable::SetCursor() **********/
2224 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2226 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2227 hstmt
= *hstmtDefault
;
2229 hstmt
= *hstmtActivate
;
2231 } // wxDbTable::SetCursor()
2234 /********** wxDbTable::Count(const wxString &) **********/
2235 ULONG
wxDbTable::Count(const wxString
&args
)
2241 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2242 sqlStmt
= wxT("SELECT COUNT(");
2244 sqlStmt
+= wxT(") FROM ");
2245 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2246 // sqlStmt += queryTableName;
2247 #if wxODBC_BACKWARD_COMPATABILITY
2248 if (from
&& wxStrlen(from
))
2254 // Add the where clause if one is provided
2255 #if wxODBC_BACKWARD_COMPATABILITY
2256 if (where
&& wxStrlen(where
))
2261 sqlStmt
+= wxT(" WHERE ");
2265 pDb
->WriteSqlLog(sqlStmt
);
2267 // Initialize the Count cursor if it's not already initialized
2270 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2271 wxASSERT(hstmtCount
);
2276 // Execute the SQL statement
2277 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2279 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2284 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2286 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2290 // Obtain the result
2291 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2293 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2298 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2299 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2301 // Return the record count
2304 } // wxDbTable::Count()
2307 /********** wxDbTable::Refresh() **********/
2308 bool wxDbTable::Refresh(void)
2312 // Switch to the internal cursor so any active cursors are not corrupted
2313 HSTMT currCursor
= GetCursor();
2314 hstmt
= hstmtInternal
;
2315 #if wxODBC_BACKWARD_COMPATABILITY
2316 // Save the where and order by clauses
2317 char *saveWhere
= where
;
2318 char *saveOrderBy
= orderBy
;
2320 wxString saveWhere
= where
;
2321 wxString saveOrderBy
= orderBy
;
2323 // Build a where clause to refetch the record with. Try and use the
2324 // ROWID if it's available, ow use the key fields.
2325 wxString whereClause
;
2326 whereClause
.Empty();
2328 if (CanUpdByROWID())
2331 wxChar rowid
[wxDB_ROWID_LEN
+1];
2333 // Get the ROWID value. If not successful retreiving the ROWID,
2334 // simply fall down through the code and build the WHERE clause
2335 // based on the key fields.
2336 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2338 whereClause
+= pDb
->SQLTableName(queryTableName
);
2339 // whereClause += queryTableName;
2340 whereClause
+= wxT(".ROWID = '");
2341 whereClause
+= rowid
;
2342 whereClause
+= wxT("'");
2346 // If unable to use the ROWID, build a where clause from the keyfields
2347 if (wxStrlen(whereClause
) == 0)
2348 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2350 // Requery the record
2351 where
= whereClause
;
2356 if (result
&& !GetNext())
2359 // Switch back to original cursor
2360 SetCursor(&currCursor
);
2362 // Free the internal cursor
2363 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2364 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2366 // Restore the original where and order by clauses
2368 orderBy
= saveOrderBy
;
2372 } // wxDbTable::Refresh()
2375 /********** wxDbTable::SetColNull() **********/
2376 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2380 colDefs
[colNo
].Null
= set
;
2381 if (set
) // Blank out the values in the member variable
2382 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2388 } // wxDbTable::SetColNull()
2391 /********** wxDbTable::SetColNull() **********/
2392 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2395 for (i
= 0; i
< noCols
; i
++)
2397 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2403 colDefs
[i
].Null
= set
;
2404 if (set
) // Blank out the values in the member variable
2405 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2411 } // wxDbTable::SetColNull()
2414 /********** wxDbTable::GetNewCursor() **********/
2415 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2417 HSTMT
*newHSTMT
= new HSTMT
;
2422 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2424 pDb
->DispAllErrors(henv
, hdbc
);
2429 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2431 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2438 if (!bindCols(*newHSTMT
))
2446 SetCursor(newHSTMT
);
2450 } // wxDbTable::GetNewCursor()
2453 /********** wxDbTable::DeleteCursor() **********/
2454 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2458 if (!hstmtDel
) // Cursor already deleted
2462 ODBC 3.0 says to use this form
2463 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2466 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2468 pDb
->DispAllErrors(henv
, hdbc
);
2476 } // wxDbTable::DeleteCursor()
2478 //////////////////////////////////////////////////////////////
2479 // wxDbGrid support functions
2480 //////////////////////////////////////////////////////////////
2482 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2484 if (!m_hstmtGridQuery
)
2486 m_hstmtGridQuery
= GetNewCursor(FALSE
,FALSE
);
2487 if (!bindCols(*m_hstmtGridQuery
))
2491 m_rowmode
= rowmode
;
2494 case WX_ROW_MODE_QUERY
:
2495 SetCursor(m_hstmtGridQuery
);
2497 case WX_ROW_MODE_INDIVIDUAL
:
2498 SetCursor(hstmtDefault
);
2503 } // wxDbTable::SetRowMode()
2506 wxVariant
wxDbTable::GetCol(const int colNo
) const
2509 if ((colNo
< noCols
) && (!IsColNull(colNo
)))
2511 switch (colDefs
[colNo
].SqlCtype
)
2515 val
= (wxChar
*)(colDefs
[colNo
].PtrDataObj
);
2519 val
= *(long *)(colDefs
[colNo
].PtrDataObj
);
2523 val
= (long int )(*(short *)(colDefs
[colNo
].PtrDataObj
));
2526 val
= (long)(*(unsigned long *)(colDefs
[colNo
].PtrDataObj
));
2529 val
= (long)(*(char *)(colDefs
[colNo
].PtrDataObj
));
2531 case SQL_C_UTINYINT
:
2532 val
= (long)(*(unsigned char *)(colDefs
[colNo
].PtrDataObj
));
2535 val
= (long)(*(UWORD
*)(colDefs
[colNo
].PtrDataObj
));
2538 val
= (DATE_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2541 val
= (TIME_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2543 case SQL_C_TIMESTAMP
:
2544 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2547 val
= *(double *)(colDefs
[colNo
].PtrDataObj
);
2554 } // wxDbTable::GetCol()
2557 void csstrncpyt(char *s
, const char *t
, int n
)
2559 while ((*s
++ = *t
++) && --n
)
2565 void wxDbTable::SetCol(const int colNo
, const wxVariant val
)
2567 //FIXME: Add proper wxDateTime support to wxVariant..
2570 SetColNull(colNo
, val
.IsNull());
2574 if ((colDefs
[colNo
].SqlCtype
== SQL_C_DATE
)
2575 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIME
)
2576 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
))
2578 //Returns null if invalid!
2579 if (!dateval
.ParseDate(val
.GetString()))
2580 SetColNull(colNo
, TRUE
);
2583 switch (colDefs
[colNo
].SqlCtype
)
2587 csstrncpyt((char *)(colDefs
[colNo
].PtrDataObj
),
2588 val
.GetString().c_str(),
2589 colDefs
[colNo
].SzDataObj
-1);
2593 *(long *)(colDefs
[colNo
].PtrDataObj
) = val
;
2597 *(short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2600 *(unsigned long *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2603 *(char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2605 case SQL_C_UTINYINT
:
2606 *(unsigned char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2609 *(unsigned short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2611 //FIXME: Add proper wxDateTime support to wxVariant..
2614 DATE_STRUCT
*dataptr
=
2615 (DATE_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2617 dataptr
->year
= dateval
.GetYear();
2618 dataptr
->month
= dateval
.GetMonth()+1;
2619 dataptr
->day
= dateval
.GetDay();
2624 TIME_STRUCT
*dataptr
=
2625 (TIME_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2627 dataptr
->hour
= dateval
.GetHour();
2628 dataptr
->minute
= dateval
.GetMinute();
2629 dataptr
->second
= dateval
.GetSecond();
2632 case SQL_C_TIMESTAMP
:
2634 TIMESTAMP_STRUCT
*dataptr
=
2635 (TIMESTAMP_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2636 dataptr
->year
= dateval
.GetYear();
2637 dataptr
->month
= dateval
.GetMonth()+1;
2638 dataptr
->day
= dateval
.GetDay();
2640 dataptr
->hour
= dateval
.GetHour();
2641 dataptr
->minute
= dateval
.GetMinute();
2642 dataptr
->second
= dateval
.GetSecond();
2646 *(double *)(colDefs
[colNo
].PtrDataObj
) = val
;
2651 } // if (!val.IsNull())
2652 } // wxDbTable::SetCol()
2655 GenericKey
wxDbTable::GetKey()
2660 blk
= malloc(m_keysize
);
2661 blkptr
= (wxChar
*) blk
;
2664 for (i
=0; i
< noCols
; i
++)
2666 if (colDefs
[i
].KeyField
)
2668 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2669 blkptr
+= colDefs
[i
].SzDataObj
;
2673 GenericKey k
= GenericKey(blk
, m_keysize
);
2677 } // wxDbTable::GetKey()
2680 void wxDbTable::SetKey(const GenericKey
& k
)
2686 blkptr
= (wxChar
*)blk
;
2689 for (i
=0; i
< noCols
; i
++)
2691 if (colDefs
[i
].KeyField
)
2693 SetColNull(i
, FALSE
);
2694 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2695 blkptr
+= colDefs
[i
].SzDataObj
;
2698 } // wxDbTable::SetKey()
2701 #endif // wxUSE_ODBC