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 // Use this line for wxWindows v1.x
30 // Use this line for wxWindows v2.x
31 #include "wx/wxprec.h"
32 #include "wx/version.h"
34 #if wxMAJOR_VERSION == 2
36 #pragma implementation "dbtable.h"
40 #ifdef DBDEBUG_CONSOLE
41 #include "wx/ioswrap.h"
49 #if wxMAJOR_VERSION == 2
51 #include "wx/string.h"
52 #include "wx/object.h"
55 #include "wx/msgdlg.h"
58 #include "wx/filefn.h"
61 #if wxMAJOR_VERSION == 1
62 # if defined(wx_msw) || defined(wx_x)
80 #if wxMAJOR_VERSION == 1
82 #elif wxMAJOR_VERSION == 2
83 #include "wx/dbtable.h"
87 // The HPUX preprocessor lines below were commented out on 8/20/97
88 // because macros.h currently redefines DEBUG and is unneeded.
90 // # include <macros.h>
93 # include <sys/minmax.h>
97 ULONG lastTableID
= 0;
105 /********** wxDbColDef::wxDbColDef() Constructor **********/
106 wxDbColDef::wxDbColDef()
112 bool wxDbColDef::Initialize()
115 DbDataType
= DB_DATA_TYPE_INTEGER
;
116 SqlCtype
= SQL_C_LONG
;
121 InsertAllowed
= FALSE
;
127 } // wxDbColDef::Initialize()
130 /********** wxDbTable::wxDbTable() Constructor **********/
131 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
132 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
134 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
136 } // wxDbTable::wxDbTable()
139 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
140 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
141 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
143 wxString tempQryTblName
;
144 tempQryTblName
= qryTblName
;
145 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
147 } // wxDbTable::wxDbTable()
150 /********** wxDbTable::~wxDbTable() **********/
151 wxDbTable::~wxDbTable()
154 } // wxDbTable::~wxDbTable()
157 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
158 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
160 // Initializing member variables
161 pDb
= pwxDb
; // Pointer to the wxDb object
165 hstmtDefault
= 0; // Initialized below
166 hstmtCount
= 0; // Initialized first time it is needed
173 noCols
= numColumns
; // Number of cols in the table
174 where
.Empty(); // Where clause
175 orderBy
.Empty(); // Order By clause
176 from
.Empty(); // From clause
177 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
182 queryTableName
.Empty();
184 wxASSERT(tblName
.Length());
190 tableName
= tblName
; // Table Name
191 if (tblPath
.Length())
192 tablePath
= tblPath
; // Table Path - used for dBase files
196 if (qryTblName
.Length()) // Name of the table/view to query
197 queryTableName
= qryTblName
;
199 queryTableName
= tblName
;
201 pDb
->incrementTableCount();
204 tableID
= ++lastTableID
;
205 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
208 wxTablesInUse
*tableInUse
;
209 tableInUse
= new wxTablesInUse();
210 tableInUse
->tableName
= tblName
;
211 tableInUse
->tableID
= tableID
;
212 tableInUse
->pDb
= pDb
;
213 TablesInUse
.Append(tableInUse
);
218 // Grab the HENV and HDBC from the wxDb object
219 henv
= pDb
->GetHENV();
220 hdbc
= pDb
->GetHDBC();
222 // Allocate space for column definitions
224 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
226 // Allocate statement handles for the table
229 // Allocate a separate statement handle for performing inserts
230 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
231 pDb
->DispAllErrors(henv
, hdbc
);
232 // Allocate a separate statement handle for performing deletes
233 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
234 pDb
->DispAllErrors(henv
, hdbc
);
235 // Allocate a separate statement handle for performing updates
236 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
237 pDb
->DispAllErrors(henv
, hdbc
);
239 // Allocate a separate statement handle for internal use
240 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
241 pDb
->DispAllErrors(henv
, hdbc
);
243 // Set the cursor type for the statement handles
244 cursorType
= SQL_CURSOR_STATIC
;
246 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
248 // Check to see if cursor type is supported
249 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
250 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
252 // Datasource does not support static cursors. Driver
253 // will substitute a cursor type. Call SQLGetStmtOption()
254 // to determine which cursor type was selected.
255 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
256 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
257 #ifdef DBDEBUG_CONSOLE
258 cout
<< wxT("Static cursor changed to: ");
261 case SQL_CURSOR_FORWARD_ONLY
:
262 cout
<< wxT("Forward Only");
264 case SQL_CURSOR_STATIC
:
265 cout
<< wxT("Static");
267 case SQL_CURSOR_KEYSET_DRIVEN
:
268 cout
<< wxT("Keyset Driven");
270 case SQL_CURSOR_DYNAMIC
:
271 cout
<< wxT("Dynamic");
274 cout
<< endl
<< endl
;
277 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
279 // Force the use of a forward only cursor...
280 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
281 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
283 // Should never happen
284 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
291 pDb
->DispNextError();
292 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
295 #ifdef DBDEBUG_CONSOLE
297 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
302 // Set the cursor type for the INSERT statement handle
303 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
304 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
305 // Set the cursor type for the DELETE statement handle
306 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
307 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
308 // Set the cursor type for the UPDATE statement handle
309 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
310 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
313 // Make the default cursor the active cursor
314 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
315 wxASSERT(hstmtDefault
);
316 hstmt
= *hstmtDefault
;
320 } // wxDbTable::initialize()
323 void wxDbTable::cleanup()
328 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
335 TablesInUse
.DeleteContents(TRUE
);
339 pNode
= TablesInUse
.First();
340 while (pNode
&& !found
)
342 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
345 if (!TablesInUse
.DeleteNode(pNode
))
346 wxLogDebug (s
,wxT("Unable to delete node!"));
349 pNode
= pNode
->Next();
354 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
);
355 wxLogDebug (msg
,wxT("NOTICE..."));
360 // Decrement the wxDb table count
362 pDb
->decrementTableCount();
364 // Delete memory allocated for column definitions
368 // Free statement handles
374 ODBC 3.0 says to use this form
375 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
377 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
378 pDb
->DispAllErrors(henv
, hdbc
);
384 ODBC 3.0 says to use this form
385 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
387 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
388 pDb
->DispAllErrors(henv
, hdbc
);
394 ODBC 3.0 says to use this form
395 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
397 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
398 pDb
->DispAllErrors(henv
, hdbc
);
404 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
405 pDb
->DispAllErrors(henv
, hdbc
);
408 // Delete dynamically allocated cursors
410 DeleteCursor(hstmtDefault
);
413 DeleteCursor(hstmtCount
);
414 } // wxDbTable::cleanup()
417 /***************************** PRIVATE FUNCTIONS *****************************/
420 /********** wxDbTable::bindParams() **********/
421 bool wxDbTable::bindParams(bool forUpdate
)
423 wxASSERT(!queryOnly
);
428 UDWORD precision
= 0;
431 // Bind each column of the table that should be bound
432 // to a parameter marker
436 for (i
=0, colNo
=1; i
< noCols
; i
++)
440 if (!colDefs
[i
].Updateable
)
445 if (!colDefs
[i
].InsertAllowed
)
449 switch(colDefs
[i
].DbDataType
)
451 case DB_DATA_TYPE_VARCHAR
:
452 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
453 precision
= colDefs
[i
].SzDataObj
;
456 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
458 colDefs
[i
].CbValue
= SQL_NTS
;
460 case DB_DATA_TYPE_INTEGER
:
461 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
462 precision
= pDb
->GetTypeInfInteger().Precision
;
465 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
467 colDefs
[i
].CbValue
= 0;
469 case DB_DATA_TYPE_FLOAT
:
470 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
471 precision
= pDb
->GetTypeInfFloat().Precision
;
472 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
473 // SQL Sybase Anywhere v5.5 returned a negative number for the
474 // MaxScale. This caused ODBC to kick out an error on ibscale.
475 // I check for this here and set the scale = precision.
477 // scale = (short) precision;
479 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
481 colDefs
[i
].CbValue
= 0;
483 case DB_DATA_TYPE_DATE
:
484 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
485 precision
= pDb
->GetTypeInfDate().Precision
;
488 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
490 colDefs
[i
].CbValue
= 0;
492 case DB_DATA_TYPE_BLOB
:
493 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
497 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
499 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
504 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
505 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
506 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
508 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
513 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
514 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
515 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
517 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
522 // Completed successfully
525 } // wxDbTable::bindParams()
528 /********** wxDbTable::bindInsertParams() **********/
529 bool wxDbTable::bindInsertParams(void)
531 return bindParams(FALSE
);
532 } // wxDbTable::bindInsertParams()
535 /********** wxDbTable::bindUpdateParams() **********/
536 bool wxDbTable::bindUpdateParams(void)
538 return bindParams(TRUE
);
539 } // wxDbTable::bindUpdateParams()
542 /********** wxDbTable::bindCols() **********/
543 bool wxDbTable::bindCols(HSTMT cursor
)
545 // Bind each column of the table to a memory address for fetching data
547 for (i
= 0; i
< noCols
; i
++)
549 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
550 colDefs
[i
].SzDataObj
, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
552 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
556 // Completed successfully
559 } // wxDbTable::bindCols()
562 /********** wxDbTable::getRec() **********/
563 bool wxDbTable::getRec(UWORD fetchType
)
567 if (!pDb
->FwdOnlyCursors())
569 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
573 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
574 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
576 if (retcode
== SQL_NO_DATA_FOUND
)
579 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
583 // Set the Null member variable to indicate the Null state
584 // of each column just read in.
586 for (i
= 0; i
< noCols
; i
++)
587 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
592 // Fetch the next record from the record set
593 retcode
= SQLFetch(hstmt
);
594 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
596 if (retcode
== SQL_NO_DATA_FOUND
)
599 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
603 // Set the Null member variable to indicate the Null state
604 // of each column just read in.
606 for (i
= 0; i
< noCols
; i
++)
607 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
611 // Completed successfully
614 } // wxDbTable::getRec()
617 /********** wxDbTable::execDelete() **********/
618 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
622 // Execute the DELETE statement
623 retcode
= SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
625 if (retcode
== SQL_SUCCESS
||
626 retcode
== SQL_NO_DATA_FOUND
||
627 retcode
== SQL_SUCCESS_WITH_INFO
)
629 // Record deleted successfully
633 // Problem deleting record
634 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
636 } // wxDbTable::execDelete()
639 /********** wxDbTable::execUpdate() **********/
640 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
644 // Execute the UPDATE statement
645 retcode
= SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
647 if (retcode
== SQL_SUCCESS
||
648 retcode
== SQL_NO_DATA_FOUND
||
649 retcode
== SQL_SUCCESS_WITH_INFO
)
651 // Record updated successfully
655 // Problem updating record
656 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
658 } // wxDbTable::execUpdate()
661 /********** wxDbTable::query() **********/
662 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
667 // The user may wish to select for update, but the DBMS may not be capable
668 selectForUpdate
= CanSelectForUpdate();
670 selectForUpdate
= FALSE
;
672 // Set the SQL SELECT string
673 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
674 { // so generate a select statement.
675 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
676 pDb
->WriteSqlLog(sqlStmt
);
679 This is the block of code that got added during the 2.2.1 merge with
680 the 2.2 main branch that somehow got added here when it should not have. - gt
683 wxStrcpy(sqlStmt, pSqlStmt);
685 SQLFreeStmt(hstmt, SQL_CLOSE);
686 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) == SQL_SUCCESS)
690 pDb->DispAllErrors(henv, hdbc, hstmt);
694 // Make sure the cursor is closed first
695 if (!CloseCursor(hstmt
))
698 // Execute the SQL SELECT statement
700 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
701 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
702 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
704 // Completed successfully
707 } // wxDbTable::query()
710 /***************************** PUBLIC FUNCTIONS *****************************/
713 /********** wxDbTable::Open() **********/
714 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
724 // Verify that the table exists in the database
725 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
727 s
= wxT("Table/view does not exist in the database");
728 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
729 s
+= wxT(", or you have no permissions.\n");
733 else if (checkPrivileges
)
735 // Verify the user has rights to access the table.
736 // Shortcut boolean evaluation to optimize out call to
739 // Unfortunately this optimization doesn't seem to be
741 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
742 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
743 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
750 if (!tablePath
.IsEmpty())
751 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
753 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
756 pDb
->LogError(p
.GetData());
761 // Bind the member variables for field exchange between
762 // the wxDbTable object and the ODBC record.
765 if (!bindInsertParams()) // Inserts
768 if (!bindUpdateParams()) // Updates
772 if (!bindCols(*hstmtDefault
)) // Selects
775 if (!bindCols(hstmtInternal
)) // Internal use only
779 * Do NOT bind the hstmtCount cursor!!!
782 // Build an insert statement using parameter markers
783 if (!queryOnly
&& noCols
> 0)
785 bool needComma
= FALSE
;
786 sqlStmt
.Printf(wxT("INSERT INTO %s ("), tableName
.c_str());
787 for (i
= 0; i
< noCols
; i
++)
789 if (! colDefs
[i
].InsertAllowed
)
793 sqlStmt
+= colDefs
[i
].ColName
;
797 sqlStmt
+= wxT(") VALUES (");
799 int insertableCount
= 0;
801 for (i
= 0; i
< noCols
; i
++)
803 if (! colDefs
[i
].InsertAllowed
)
813 // Prepare the insert statement for execution
816 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
817 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
823 // Completed successfully
826 } // wxDbTable::Open()
829 /********** wxDbTable::Query() **********/
830 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
833 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
835 } // wxDbTable::Query()
838 /********** wxDbTable::QueryBySqlStmt() **********/
839 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
841 pDb
->WriteSqlLog(pSqlStmt
);
843 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
845 } // wxDbTable::QueryBySqlStmt()
848 /********** wxDbTable::QueryMatching() **********/
849 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
852 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
854 } // wxDbTable::QueryMatching()
857 /********** wxDbTable::QueryOnKeyFields() **********/
858 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
861 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
863 } // wxDbTable::QueryOnKeyFields()
866 /********** wxDbTable::GetPrev() **********/
867 bool wxDbTable::GetPrev(void)
869 if (pDb
->FwdOnlyCursors())
871 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
875 return(getRec(SQL_FETCH_PRIOR
));
877 } // wxDbTable::GetPrev()
880 /********** wxDbTable::operator-- **********/
881 bool wxDbTable::operator--(int)
883 if (pDb
->FwdOnlyCursors())
885 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
889 return(getRec(SQL_FETCH_PRIOR
));
891 } // wxDbTable::operator--
894 /********** wxDbTable::GetFirst() **********/
895 bool wxDbTable::GetFirst(void)
897 if (pDb
->FwdOnlyCursors())
899 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
903 return(getRec(SQL_FETCH_FIRST
));
905 } // wxDbTable::GetFirst()
908 /********** wxDbTable::GetLast() **********/
909 bool wxDbTable::GetLast(void)
911 if (pDb
->FwdOnlyCursors())
913 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
917 return(getRec(SQL_FETCH_LAST
));
919 } // wxDbTable::GetLast()
922 /********** wxDbTable::BuildDeleteStmt() **********/
923 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
925 wxASSERT(!queryOnly
);
929 wxString whereClause
;
933 // Handle the case of DeleteWhere() and the where clause is blank. It should
934 // delete all records from the database in this case.
935 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
937 pSqlStmt
.Printf(wxT("DELETE FROM %s"), tableName
.c_str());
941 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "), tableName
.c_str());
943 // Append the WHERE clause to the SQL DELETE statement
946 case DB_DEL_KEYFIELDS
:
947 // If the datasource supports the ROWID column, build
948 // the where on ROWID for efficiency purposes.
949 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
953 wxChar rowid
[wxDB_ROWID_LEN
+1];
955 // Get the ROWID value. If not successful retreiving the ROWID,
956 // simply fall down through the code and build the WHERE clause
957 // based on the key fields.
958 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
960 pSqlStmt
+= wxT("ROWID = '");
962 pSqlStmt
+= wxT("'");
966 // Unable to delete by ROWID, so build a WHERE
967 // clause based on the keyfields.
968 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
969 pSqlStmt
+= whereClause
;
972 pSqlStmt
+= pWhereClause
;
974 case DB_DEL_MATCHING
:
975 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
976 pSqlStmt
+= whereClause
;
980 } // BuildDeleteStmt()
983 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
984 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
986 wxString tempSqlStmt
;
987 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
988 wxStrcpy(pSqlStmt
, tempSqlStmt
);
989 } // wxDbTable::BuildDeleteStmt()
992 /********** wxDbTable::BuildSelectStmt() **********/
993 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
995 wxString whereClause
;
998 // Build a select statement to query the database
999 pSqlStmt
= wxT("SELECT ");
1001 // SELECT DISTINCT values only?
1003 pSqlStmt
+= wxT("DISTINCT ");
1005 // Was a FROM clause specified to join tables to the base table?
1006 // Available for ::Query() only!!!
1007 bool appendFromClause
= FALSE
;
1008 #if wxODBC_BACKWARD_COMPATABILITY
1009 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
1010 appendFromClause
= TRUE
;
1012 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
1013 appendFromClause
= TRUE
;
1016 // Add the column list
1018 for (i
= 0; i
< noCols
; i
++)
1020 // If joining tables, the base table column names must be qualified to avoid ambiguity
1021 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1023 pSqlStmt
+= queryTableName
;
1024 pSqlStmt
+= wxT(".");
1026 pSqlStmt
+= colDefs
[i
].ColName
;
1028 pSqlStmt
+= wxT(",");
1031 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1032 // the ROWID if querying distinct records. The rowid will always be unique.
1033 if (!distinct
&& CanUpdByROWID())
1035 // If joining tables, the base table column names must be qualified to avoid ambiguity
1036 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1038 pSqlStmt
+= wxT(",");
1039 pSqlStmt
+= queryTableName
;
1040 pSqlStmt
+= wxT(".ROWID");
1043 pSqlStmt
+= wxT(",ROWID");
1046 // Append the FROM tablename portion
1047 pSqlStmt
+= wxT(" FROM ");
1048 pSqlStmt
+= queryTableName
;
1050 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1051 // The HOLDLOCK keyword follows the table name in the from clause.
1052 // Each table in the from clause must specify HOLDLOCK or
1053 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1054 // is parsed but ignored in SYBASE Transact-SQL.
1055 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1056 pSqlStmt
+= wxT(" HOLDLOCK");
1058 if (appendFromClause
)
1061 // Append the WHERE clause. Either append the where clause for the class
1062 // or build a where clause. The typeOfSelect determines this.
1063 switch(typeOfSelect
)
1065 case DB_SELECT_WHERE
:
1066 #if wxODBC_BACKWARD_COMPATABILITY
1067 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1069 if (where
.Length()) // May not want a where clause!!!
1072 pSqlStmt
+= wxT(" WHERE ");
1076 case DB_SELECT_KEYFIELDS
:
1077 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1078 if (whereClause
.Length())
1080 pSqlStmt
+= wxT(" WHERE ");
1081 pSqlStmt
+= whereClause
;
1084 case DB_SELECT_MATCHING
:
1085 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1086 if (whereClause
.Length())
1088 pSqlStmt
+= wxT(" WHERE ");
1089 pSqlStmt
+= whereClause
;
1094 // Append the ORDER BY clause
1095 #if wxODBC_BACKWARD_COMPATABILITY
1096 if (orderBy
&& wxStrlen(orderBy
))
1098 if (orderBy
.Length())
1101 pSqlStmt
+= wxT(" ORDER BY ");
1102 pSqlStmt
+= orderBy
;
1105 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1106 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1107 // HOLDLOCK for Sybase.
1108 if (selectForUpdate
&& CanSelectForUpdate())
1109 pSqlStmt
+= wxT(" FOR UPDATE");
1111 } // wxDbTable::BuildSelectStmt()
1114 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1115 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1117 wxString tempSqlStmt
;
1118 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1119 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1120 } // wxDbTable::BuildSelectStmt()
1123 /********** wxDbTable::BuildUpdateStmt() **********/
1124 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1126 wxASSERT(!queryOnly
);
1130 wxString whereClause
;
1131 whereClause
.Empty();
1133 bool firstColumn
= TRUE
;
1135 pSqlStmt
.Printf(wxT("UPDATE %s SET "), tableName
.Upper().c_str());
1137 // Append a list of columns to be updated
1139 for (i
= 0; i
< noCols
; i
++)
1141 // Only append Updateable columns
1142 if (colDefs
[i
].Updateable
)
1145 pSqlStmt
+= wxT(",");
1147 firstColumn
= FALSE
;
1148 pSqlStmt
+= colDefs
[i
].ColName
;
1149 pSqlStmt
+= wxT(" = ?");
1153 // Append the WHERE clause to the SQL UPDATE statement
1154 pSqlStmt
+= wxT(" WHERE ");
1157 case DB_UPD_KEYFIELDS
:
1158 // If the datasource supports the ROWID column, build
1159 // the where on ROWID for efficiency purposes.
1160 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1161 if (CanUpdByROWID())
1164 wxChar rowid
[wxDB_ROWID_LEN
+1];
1166 // Get the ROWID value. If not successful retreiving the ROWID,
1167 // simply fall down through the code and build the WHERE clause
1168 // based on the key fields.
1169 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1171 pSqlStmt
+= wxT("ROWID = '");
1173 pSqlStmt
+= wxT("'");
1177 // Unable to delete by ROWID, so build a WHERE
1178 // clause based on the keyfields.
1179 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1180 pSqlStmt
+= whereClause
;
1183 pSqlStmt
+= pWhereClause
;
1186 } // BuildUpdateStmt()
1189 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1190 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1192 wxString tempSqlStmt
;
1193 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1194 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1195 } // BuildUpdateStmt()
1198 /********** wxDbTable::BuildWhereClause() **********/
1199 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1200 const wxString
&qualTableName
, bool useLikeComparison
)
1202 * Note: BuildWhereClause() currently ignores timestamp columns.
1203 * They are not included as part of the where clause.
1206 bool moreThanOneColumn
= FALSE
;
1209 // Loop through the columns building a where clause as you go
1211 for (i
= 0; i
< noCols
; i
++)
1213 // Determine if this column should be included in the WHERE clause
1214 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1215 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1217 // Skip over timestamp columns
1218 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1220 // If there is more than 1 column, join them with the keyword "AND"
1221 if (moreThanOneColumn
)
1222 pWhereClause
+= wxT(" AND ");
1224 moreThanOneColumn
= TRUE
;
1225 // Concatenate where phrase for the column
1226 if (qualTableName
.Length())
1228 pWhereClause
+= qualTableName
;
1229 pWhereClause
+= wxT(".");
1231 pWhereClause
+= colDefs
[i
].ColName
;
1232 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1233 pWhereClause
+= wxT(" LIKE ");
1235 pWhereClause
+= wxT(" = ");
1236 switch(colDefs
[i
].SqlCtype
)
1239 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1242 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[i
].PtrDataObj
));
1245 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[i
].PtrDataObj
));
1248 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1251 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1254 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1257 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1260 pWhereClause
+= colValue
;
1263 } // wxDbTable::BuildWhereClause()
1266 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1267 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1268 const wxString
&qualTableName
, bool useLikeComparison
)
1270 wxString tempSqlStmt
;
1271 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1272 wxStrcpy(pWhereClause
, tempSqlStmt
);
1273 } // wxDbTable::BuildWhereClause()
1276 /********** wxDbTable::GetRowNum() **********/
1277 UWORD
wxDbTable::GetRowNum(void)
1281 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1283 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1287 // Completed successfully
1288 return((UWORD
) rowNum
);
1290 } // wxDbTable::GetRowNum()
1293 /********** wxDbTable::CloseCursor() **********/
1294 bool wxDbTable::CloseCursor(HSTMT cursor
)
1296 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1297 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1299 // Completed successfully
1302 } // wxDbTable::CloseCursor()
1305 /********** wxDbTable::CreateTable() **********/
1306 bool wxDbTable::CreateTable(bool attemptDrop
)
1314 #ifdef DBDEBUG_CONSOLE
1315 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1319 if (attemptDrop
&& !DropTable())
1323 #ifdef DBDEBUG_CONSOLE
1324 for (i
= 0; i
< noCols
; i
++)
1326 // Exclude derived columns since they are NOT part of the base table
1327 if (colDefs
[i
].DerivedCol
)
1329 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1330 switch(colDefs
[i
].DbDataType
)
1332 case DB_DATA_TYPE_VARCHAR
:
1333 cout
<< pDb
->typeInfVarchar
.TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1335 case DB_DATA_TYPE_INTEGER
:
1336 cout
<< pDb
->typeInfInteger
.TypeName
;
1338 case DB_DATA_TYPE_FLOAT
:
1339 cout
<< pDb
->typeInfFloat
.TypeName
;
1341 case DB_DATA_TYPE_DATE
:
1342 cout
<< pDb
->typeInfDate
.TypeName
;
1344 case DB_DATA_TYPE_BLOB
:
1345 cout
<< pDb
->typeInfBlob
.TypeName
;
1352 // Build a CREATE TABLE string from the colDefs structure.
1353 bool needComma
= FALSE
;
1354 sqlStmt
.Printf(wxT("CREATE TABLE %s ("), tableName
.c_str());
1356 for (i
= 0; i
< noCols
; i
++)
1358 // Exclude derived columns since they are NOT part of the base table
1359 if (colDefs
[i
].DerivedCol
)
1363 sqlStmt
+= wxT(",");
1365 sqlStmt
+= colDefs
[i
].ColName
;
1366 sqlStmt
+= wxT(" ");
1368 switch(colDefs
[i
].DbDataType
)
1370 case DB_DATA_TYPE_VARCHAR
:
1371 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1373 case DB_DATA_TYPE_INTEGER
:
1374 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1376 case DB_DATA_TYPE_FLOAT
:
1377 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1379 case DB_DATA_TYPE_DATE
:
1380 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1382 case DB_DATA_TYPE_BLOB
:
1383 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1386 // For varchars, append the size of the string
1387 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)// ||
1388 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1391 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1395 if (pDb
->Dbms() == dbmsDB2
||
1396 pDb
->Dbms() == dbmsMY_SQL
||
1397 pDb
->Dbms() == dbmsSYBASE_ASE
||
1398 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1400 if (colDefs
[i
].KeyField
)
1402 sqlStmt
+= wxT(" NOT NULL");
1408 // If there is a primary key defined, include it in the create statement
1409 for (i
= j
= 0; i
< noCols
; i
++)
1411 if (colDefs
[i
].KeyField
)
1417 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1419 switch (pDb
->Dbms())
1421 case dbmsSYBASE_ASA
:
1422 case dbmsSYBASE_ASE
:
1425 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1426 sqlStmt
+= wxT(",PRIMARY KEY (");
1431 sqlStmt
+= wxT(",CONSTRAINT ");
1432 // DB2 is limited to 18 characters for index names
1433 if (pDb
->Dbms() == dbmsDB2
)
1435 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."));
1436 sqlStmt
+= tableName
.substr(0, 13);
1439 sqlStmt
+= tableName
;
1441 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1446 // List column name(s) of column(s) comprising the primary key
1447 for (i
= j
= 0; i
< noCols
; i
++)
1449 if (colDefs
[i
].KeyField
)
1451 if (j
++) // Multi part key, comma separate names
1452 sqlStmt
+= wxT(",");
1453 sqlStmt
+= colDefs
[i
].ColName
;
1456 sqlStmt
+= wxT(")");
1458 if (pDb
->Dbms() == dbmsSYBASE_ASA
||
1459 pDb
->Dbms() == dbmsSYBASE_ASE
)
1461 sqlStmt
+= wxT(" CONSTRAINT ");
1462 sqlStmt
+= tableName
;
1463 sqlStmt
+= wxT("_PIDX");
1466 // Append the closing parentheses for the create table statement
1467 sqlStmt
+= wxT(")");
1469 pDb
->WriteSqlLog(sqlStmt
);
1471 #ifdef DBDEBUG_CONSOLE
1472 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1475 // Execute the CREATE TABLE statement
1476 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1477 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1479 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1480 pDb
->RollbackTrans();
1485 // Commit the transaction and close the cursor
1486 if (!pDb
->CommitTrans())
1488 if (!CloseCursor(hstmt
))
1491 // Database table created successfully
1494 } // wxDbTable::CreateTable()
1497 /********** wxDbTable::DropTable() **********/
1498 bool wxDbTable::DropTable()
1500 // NOTE: This function returns TRUE if the Table does not exist, but
1501 // only for identified databases. Code will need to be added
1502 // below for any other databases when those databases are defined
1503 // to handle this situation consistently
1507 sqlStmt
.Printf(wxT("DROP TABLE %s"), tableName
.c_str());
1509 pDb
->WriteSqlLog(sqlStmt
);
1511 #ifdef DBDEBUG_CONSOLE
1512 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1518 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1519 if (retcode
!= SQL_SUCCESS
)
1521 // Check for "Base table not found" error and ignore
1522 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1523 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1524 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1526 // Check for product specific error codes
1527 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1528 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1529 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1530 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1532 pDb
->DispNextError();
1533 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1534 pDb
->RollbackTrans();
1535 // CloseCursor(hstmt);
1541 // Commit the transaction and close the cursor
1542 if (! pDb
->CommitTrans())
1544 if (! CloseCursor(hstmt
))
1548 } // wxDbTable::DropTable()
1551 /********** wxDbTable::CreateIndex() **********/
1552 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1553 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1557 // Drop the index first
1558 if (attemptDrop
&& !DropIndex(idxName
))
1561 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1562 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1563 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1564 // table was created, then months later you determine that an additional index while
1565 // give better performance, so you want to add an index).
1567 // The following block of code will modify the column definition to make the column be
1568 // defined with the "NOT NULL" qualifier.
1569 if (pDb
->Dbms() == dbmsMY_SQL
)
1574 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1578 // Find the column definition that has the ColName that matches the
1579 // index column name. We need to do this to get the DB_DATA_TYPE of
1580 // the index column, as MySQL's syntax for the ALTER column requires
1582 while (!found
&& (j
< this->noCols
))
1584 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1592 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1593 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1598 wxODBC_ERRORS retcode
;
1599 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1600 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1601 // This line is just here for debug checking of the value
1602 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1612 pDb
->RollbackTrans();
1617 // Build a CREATE INDEX statement
1618 sqlStmt
= wxT("CREATE ");
1620 sqlStmt
+= wxT("UNIQUE ");
1622 sqlStmt
+= wxT("INDEX ");
1624 sqlStmt
+= wxT(" ON ");
1625 sqlStmt
+= tableName
;
1626 sqlStmt
+= wxT(" (");
1628 // Append list of columns making up index
1630 for (i
= 0; i
< noIdxCols
; i
++)
1632 sqlStmt
+= pIdxDefs
[i
].ColName
;
1633 /* Postgres doesn't cope with ASC */
1634 if (pDb
->Dbms() != dbmsPOSTGRES
)
1636 if (pIdxDefs
[i
].Ascending
)
1637 sqlStmt
+= wxT(" ASC");
1639 sqlStmt
+= wxT(" DESC");
1642 if ((i
+ 1) < noIdxCols
)
1643 sqlStmt
+= wxT(",");
1646 // Append closing parentheses
1647 sqlStmt
+= wxT(")");
1649 pDb
->WriteSqlLog(sqlStmt
);
1651 #ifdef DBDEBUG_CONSOLE
1652 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1655 // Execute the CREATE INDEX statement
1656 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1658 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1659 pDb
->RollbackTrans();
1664 // Commit the transaction and close the cursor
1665 if (! pDb
->CommitTrans())
1667 if (! CloseCursor(hstmt
))
1670 // Index Created Successfully
1673 } // wxDbTable::CreateIndex()
1676 /********** wxDbTable::DropIndex() **********/
1677 bool wxDbTable::DropIndex(const wxString
&idxName
)
1679 // NOTE: This function returns TRUE if the Index does not exist, but
1680 // only for identified databases. Code will need to be added
1681 // below for any other databases when those databases are defined
1682 // to handle this situation consistently
1686 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1687 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1688 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),idxName
.c_str(), tableName
.c_str());
1689 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1690 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1691 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),tableName
.c_str(), idxName
.c_str());
1693 sqlStmt
.Printf(wxT("DROP INDEX %s"),idxName
.c_str());
1695 pDb
->WriteSqlLog(sqlStmt
);
1697 #ifdef DBDEBUG_CONSOLE
1698 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1701 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1703 // Check for "Index not found" error and ignore
1704 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1705 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1707 // Check for product specific error codes
1708 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1709 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1710 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1711 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1712 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1713 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1716 pDb
->DispNextError();
1717 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1718 pDb
->RollbackTrans();
1725 // Commit the transaction and close the cursor
1726 if (! pDb
->CommitTrans())
1728 if (! CloseCursor(hstmt
))
1732 } // wxDbTable::DropIndex()
1735 /********** wxDbTable::SetOrderByColNums() **********/
1736 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1738 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1744 va_start(argptr
, first
); /* Initialize variable arguments. */
1745 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1747 // Make sure the passed in column number
1748 // is within the valid range of columns
1750 // Valid columns are 0 thru noCols-1
1751 if (colNo
>= noCols
|| colNo
< 0)
1758 tempStr
+= wxT(",");
1760 tempStr
+= colDefs
[colNo
].ColName
;
1761 colNo
= va_arg (argptr
, int);
1763 va_end (argptr
); /* Reset variable arguments. */
1765 SetOrderByClause(tempStr
);
1768 } // wxDbTable::SetOrderByColNums()
1771 /********** wxDbTable::Insert() **********/
1772 int wxDbTable::Insert(void)
1774 wxASSERT(!queryOnly
);
1775 if (queryOnly
|| !insertable
)
1780 // Insert the record by executing the already prepared insert statement
1782 retcode
=SQLExecute(hstmtInsert
);
1783 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1785 // Check to see if integrity constraint was violated
1786 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1787 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1788 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1791 pDb
->DispNextError();
1792 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1797 // Record inserted into the datasource successfully
1800 } // wxDbTable::Insert()
1803 /********** wxDbTable::Update() **********/
1804 bool wxDbTable::Update(void)
1806 wxASSERT(!queryOnly
);
1812 // Build the SQL UPDATE statement
1813 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1815 pDb
->WriteSqlLog(sqlStmt
);
1817 #ifdef DBDEBUG_CONSOLE
1818 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1821 // Execute the SQL UPDATE statement
1822 return(execUpdate(sqlStmt
));
1824 } // wxDbTable::Update()
1827 /********** wxDbTable::Update(pSqlStmt) **********/
1828 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1830 wxASSERT(!queryOnly
);
1834 pDb
->WriteSqlLog(pSqlStmt
);
1836 return(execUpdate(pSqlStmt
));
1838 } // wxDbTable::Update(pSqlStmt)
1841 /********** wxDbTable::UpdateWhere() **********/
1842 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1844 wxASSERT(!queryOnly
);
1850 // Build the SQL UPDATE statement
1851 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1853 pDb
->WriteSqlLog(sqlStmt
);
1855 #ifdef DBDEBUG_CONSOLE
1856 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1859 // Execute the SQL UPDATE statement
1860 return(execUpdate(sqlStmt
));
1862 } // wxDbTable::UpdateWhere()
1865 /********** wxDbTable::Delete() **********/
1866 bool wxDbTable::Delete(void)
1868 wxASSERT(!queryOnly
);
1875 // Build the SQL DELETE statement
1876 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1878 pDb
->WriteSqlLog(sqlStmt
);
1880 // Execute the SQL DELETE statement
1881 return(execDelete(sqlStmt
));
1883 } // wxDbTable::Delete()
1886 /********** wxDbTable::DeleteWhere() **********/
1887 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1889 wxASSERT(!queryOnly
);
1896 // Build the SQL DELETE statement
1897 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1899 pDb
->WriteSqlLog(sqlStmt
);
1901 // Execute the SQL DELETE statement
1902 return(execDelete(sqlStmt
));
1904 } // wxDbTable::DeleteWhere()
1907 /********** wxDbTable::DeleteMatching() **********/
1908 bool wxDbTable::DeleteMatching(void)
1910 wxASSERT(!queryOnly
);
1917 // Build the SQL DELETE statement
1918 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1920 pDb
->WriteSqlLog(sqlStmt
);
1922 // Execute the SQL DELETE statement
1923 return(execDelete(sqlStmt
));
1925 } // wxDbTable::DeleteMatching()
1928 /********** wxDbTable::IsColNull() **********/
1929 bool wxDbTable::IsColNull(UWORD colNo
)
1932 This logic is just not right. It would indicate TRUE
1933 if a numeric field were set to a value of 0.
1935 switch(colDefs[colNo].SqlCtype)
1938 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1940 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1942 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1944 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1946 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1948 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1950 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1951 case SQL_C_TIMESTAMP:
1952 TIMESTAMP_STRUCT *pDt;
1953 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1954 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1962 return (colDefs
[colNo
].Null
);
1963 } // wxDbTable::IsColNull()
1966 /********** wxDbTable::CanSelectForUpdate() **********/
1967 bool wxDbTable::CanSelectForUpdate(void)
1972 if (pDb
->Dbms() == dbmsMY_SQL
)
1975 if ((pDb
->Dbms() == dbmsORACLE
) ||
1976 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
1981 } // wxDbTable::CanSelectForUpdate()
1984 /********** wxDbTable::CanUpdByROWID() **********/
1985 bool wxDbTable::CanUpdByROWID(void)
1988 * NOTE: Returning FALSE for now until this can be debugged,
1989 * as the ROWID is not getting updated correctly
1993 if (pDb->Dbms() == dbmsORACLE)
1998 } // wxDbTable::CanUpdByROWID()
2001 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2002 bool wxDbTable::IsCursorClosedOnCommit(void)
2004 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2009 } // wxDbTable::IsCursorClosedOnCommit()
2013 /********** wxDbTable::ClearMemberVar() **********/
2014 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2016 wxASSERT(colNo
< noCols
);
2018 switch(colDefs
[colNo
].SqlCtype
)
2021 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2024 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2027 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2030 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2033 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2036 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2039 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2041 case SQL_C_TIMESTAMP
:
2042 TIMESTAMP_STRUCT
*pDt
;
2043 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2056 } // wxDbTable::ClearMemberVar()
2059 /********** wxDbTable::ClearMemberVars() **********/
2060 void wxDbTable::ClearMemberVars(bool setToNull
)
2064 // Loop through the columns setting each member variable to zero
2065 for (i
=0; i
< noCols
; i
++)
2066 ClearMemberVar(i
,setToNull
);
2068 } // wxDbTable::ClearMemberVars()
2071 /********** wxDbTable::SetQueryTimeout() **********/
2072 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2074 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2075 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2076 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2077 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2078 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2079 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2080 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2081 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2083 // Completed Successfully
2086 } // wxDbTable::SetQueryTimeout()
2089 /********** wxDbTable::SetColDefs() **********/
2090 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2091 SWORD cType
, int size
, bool keyField
, bool upd
,
2092 bool insAllow
, bool derivedCol
)
2094 if (!colDefs
) // May happen if the database connection fails
2097 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2099 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2100 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2103 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2105 colDefs
[index
].DbDataType
= dataType
;
2106 colDefs
[index
].PtrDataObj
= pData
;
2107 colDefs
[index
].SqlCtype
= cType
;
2108 colDefs
[index
].SzDataObj
= size
;
2109 colDefs
[index
].KeyField
= keyField
;
2110 colDefs
[index
].DerivedCol
= derivedCol
;
2111 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2114 colDefs
[index
].Updateable
= FALSE
;
2115 colDefs
[index
].InsertAllowed
= FALSE
;
2119 colDefs
[index
].Updateable
= upd
;
2120 colDefs
[index
].InsertAllowed
= insAllow
;
2123 colDefs
[index
].Null
= FALSE
;
2125 } // wxDbTable::SetColDefs()
2128 /********** wxDbTable::SetColDefs() **********/
2129 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2132 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2138 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2140 for (index
= 0; index
< numCols
; index
++)
2142 // Process the fields
2143 switch (pColInfs
[index
].dbDataType
)
2145 case DB_DATA_TYPE_VARCHAR
:
2146 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2147 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2148 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2150 case DB_DATA_TYPE_INTEGER
:
2151 // Can be long or short
2152 if (pColInfs
[index
].bufferLength
== sizeof(long))
2154 pColDataPtrs
[index
].PtrDataObj
= new long;
2155 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2156 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2160 pColDataPtrs
[index
].PtrDataObj
= new short;
2161 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2162 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2165 case DB_DATA_TYPE_FLOAT
:
2166 // Can be float or double
2167 if (pColInfs
[index
].bufferLength
== sizeof(float))
2169 pColDataPtrs
[index
].PtrDataObj
= new float;
2170 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2171 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2175 pColDataPtrs
[index
].PtrDataObj
= new double;
2176 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2177 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2180 case DB_DATA_TYPE_DATE
:
2181 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2182 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2183 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2185 case DB_DATA_TYPE_BLOB
:
2186 int notSupportedYet
= 0;
2187 wxASSERT_MSG(notSupportedYet
, wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2188 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2189 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2190 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2193 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2194 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2197 // Unable to build all the column definitions, as either one of
2198 // the calls to "new" failed above, or there was a BLOB field
2199 // to have a column definition for. If BLOBs are to be used,
2200 // the other form of ::SetColDefs() must be used, as it is impossible
2201 // to know the maximum size to create the PtrDataObj to be.
2202 delete [] pColDataPtrs
;
2208 return (pColDataPtrs
);
2210 } // wxDbTable::SetColDefs()
2213 /********** wxDbTable::SetCursor() **********/
2214 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2216 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2217 hstmt
= *hstmtDefault
;
2219 hstmt
= *hstmtActivate
;
2221 } // wxDbTable::SetCursor()
2224 /********** wxDbTable::Count(const wxString &) **********/
2225 ULONG
wxDbTable::Count(const wxString
&args
)
2231 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2232 sqlStmt
= wxT("SELECT COUNT(");
2234 sqlStmt
+= wxT(") FROM ");
2235 sqlStmt
+= queryTableName
;
2236 #if wxODBC_BACKWARD_COMPATABILITY
2237 if (from
&& wxStrlen(from
))
2243 // Add the where clause if one is provided
2244 #if wxODBC_BACKWARD_COMPATABILITY
2245 if (where
&& wxStrlen(where
))
2250 sqlStmt
+= wxT(" WHERE ");
2254 pDb
->WriteSqlLog(sqlStmt
);
2256 // Initialize the Count cursor if it's not already initialized
2259 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2260 wxASSERT(hstmtCount
);
2265 // Execute the SQL statement
2266 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2268 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2273 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2275 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2279 // Obtain the result
2280 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2282 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2287 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2288 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2290 // Return the record count
2293 } // wxDbTable::Count()
2296 /********** wxDbTable::Refresh() **********/
2297 bool wxDbTable::Refresh(void)
2301 // Switch to the internal cursor so any active cursors are not corrupted
2302 HSTMT currCursor
= GetCursor();
2303 hstmt
= hstmtInternal
;
2304 #if wxODBC_BACKWARD_COMPATABILITY
2305 // Save the where and order by clauses
2306 char *saveWhere
= where
;
2307 char *saveOrderBy
= orderBy
;
2309 wxString saveWhere
= where
;
2310 wxString saveOrderBy
= orderBy
;
2312 // Build a where clause to refetch the record with. Try and use the
2313 // ROWID if it's available, ow use the key fields.
2314 wxString whereClause
;
2315 whereClause
.Empty();
2317 if (CanUpdByROWID())
2320 wxChar rowid
[wxDB_ROWID_LEN
+1];
2322 // Get the ROWID value. If not successful retreiving the ROWID,
2323 // simply fall down through the code and build the WHERE clause
2324 // based on the key fields.
2325 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2327 whereClause
+= queryTableName
;
2328 whereClause
+= wxT(".ROWID = '");
2329 whereClause
+= rowid
;
2330 whereClause
+= wxT("'");
2334 // If unable to use the ROWID, build a where clause from the keyfields
2335 if (wxStrlen(whereClause
) == 0)
2336 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2338 // Requery the record
2339 where
= whereClause
;
2344 if (result
&& !GetNext())
2347 // Switch back to original cursor
2348 SetCursor(&currCursor
);
2350 // Free the internal cursor
2351 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2352 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2354 // Restore the original where and order by clauses
2356 orderBy
= saveOrderBy
;
2360 } // wxDbTable::Refresh()
2363 /********** wxDbTable::SetColNull() **********/
2364 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2368 colDefs
[colNo
].Null
= set
;
2369 if (set
) // Blank out the values in the member variable
2370 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2376 } // wxDbTable::SetColNull()
2379 /********** wxDbTable::SetColNull() **********/
2380 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2383 for (i
= 0; i
< noCols
; i
++)
2385 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2391 colDefs
[i
].Null
= set
;
2392 if (set
) // Blank out the values in the member variable
2393 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2399 } // wxDbTable::SetColNull()
2402 /********** wxDbTable::GetNewCursor() **********/
2403 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2405 HSTMT
*newHSTMT
= new HSTMT
;
2410 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2412 pDb
->DispAllErrors(henv
, hdbc
);
2417 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2419 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2426 if(!bindCols(*newHSTMT
))
2434 SetCursor(newHSTMT
);
2438 } // wxDbTable::GetNewCursor()
2441 /********** wxDbTable::DeleteCursor() **********/
2442 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2446 if (!hstmtDel
) // Cursor already deleted
2450 ODBC 3.0 says to use this form
2451 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2454 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2456 pDb
->DispAllErrors(henv
, hdbc
);
2464 } // wxDbTable::DeleteCursor()
2466 #endif // wxUSE_ODBC