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::bindUpdateParams() **********/
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
435 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
439 if (! colDefs
[i
].Updateable
)
444 if (! colDefs
[i
].InsertAllowed
)
448 switch(colDefs
[i
].DbDataType
)
450 case DB_DATA_TYPE_VARCHAR
:
451 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
452 precision
= colDefs
[i
].SzDataObj
;
455 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
457 colDefs
[i
].CbValue
= SQL_NTS
;
459 case DB_DATA_TYPE_INTEGER
:
460 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
461 precision
= pDb
->GetTypeInfInteger().Precision
;
464 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
466 colDefs
[i
].CbValue
= 0;
468 case DB_DATA_TYPE_FLOAT
:
469 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
470 precision
= pDb
->GetTypeInfFloat().Precision
;
471 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
472 // SQL Sybase Anywhere v5.5 returned a negative number for the
473 // MaxScale. This caused ODBC to kick out an error on ibscale.
474 // I check for this here and set the scale = precision.
476 // scale = (short) precision;
478 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
480 colDefs
[i
].CbValue
= 0;
482 case DB_DATA_TYPE_DATE
:
483 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
484 precision
= pDb
->GetTypeInfDate().Precision
;
487 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
489 colDefs
[i
].CbValue
= 0;
491 case DB_DATA_TYPE_BLOB
:
492 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
496 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
498 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
503 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
504 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
505 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
507 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
512 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
513 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
514 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
516 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
521 // Completed successfully
524 } // wxDbTable::bindParams()
527 /********** wxDbTable::bindInsertParams() **********/
528 bool wxDbTable::bindInsertParams(void)
530 return bindParams(FALSE
);
531 } // wxDbTable::bindInsertParams()
534 /********** wxDbTable::bindUpdateParams() **********/
535 bool wxDbTable::bindUpdateParams(void)
537 return bindParams(TRUE
);
538 } // wxDbTable::bindUpdateParams()
541 /********** wxDbTable::bindCols() **********/
542 bool wxDbTable::bindCols(HSTMT cursor
)
544 //RG-NULL static SDWORD cb;
546 // Bind each column of the table to a memory address for fetching data
548 for (i
= 0; i
< noCols
; i
++)
550 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
551 colDefs
[i
].SzDataObj
, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
553 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
557 // Completed successfully
560 } // wxDbTable::bindCols()
563 /********** wxDbTable::getRec() **********/
564 bool wxDbTable::getRec(UWORD fetchType
)
568 if (!pDb
->FwdOnlyCursors())
570 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
574 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
575 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
577 if (retcode
== SQL_NO_DATA_FOUND
)
580 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
584 // Set the Null member variable to indicate the Null state
585 // of each column just read in.
587 for (i
= 0; i
< noCols
; i
++)
588 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
593 // Fetch the next record from the record set
594 retcode
= SQLFetch(hstmt
);
595 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
597 if (retcode
== SQL_NO_DATA_FOUND
)
600 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
604 // Set the Null member variable to indicate the Null state
605 // of each column just read in.
607 for (i
= 0; i
< noCols
; i
++)
608 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
612 // Completed successfully
615 } // wxDbTable::getRec()
618 /********** wxDbTable::execDelete() **********/
619 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
621 // Execute the DELETE statement
622 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
623 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
625 // Record deleted successfully
628 } // wxDbTable::execDelete()
631 /********** wxDbTable::execUpdate() **********/
632 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
634 // Execute the UPDATE statement
635 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
636 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
638 // Record deleted successfully
641 } // wxDbTable::execUpdate()
644 /********** wxDbTable::query() **********/
645 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
650 // The user may wish to select for update, but the DBMS may not be capable
651 selectForUpdate
= CanSelectForUpdate();
653 selectForUpdate
= FALSE
;
655 // Set the SQL SELECT string
656 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
657 { // so generate a select statement.
658 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
659 pDb
->WriteSqlLog(sqlStmt
);
662 This is the block of code that got added during the 2.2.1 merge with
663 the 2.2 main branch that somehow got added here when it should not have. - gt
666 wxStrcpy(sqlStmt, pSqlStmt);
668 SQLFreeStmt(hstmt, SQL_CLOSE);
669 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) == SQL_SUCCESS)
673 pDb->DispAllErrors(henv, hdbc, hstmt);
677 // Make sure the cursor is closed first
678 if (!CloseCursor(hstmt
))
681 // Execute the SQL SELECT statement
683 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
684 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
685 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
687 // Completed successfully
690 } // wxDbTable::query()
693 /***************************** PUBLIC FUNCTIONS *****************************/
696 /********** wxDbTable::Open() **********/
697 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
707 // Verify that the table exists in the database
708 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
710 s
= wxT("Table/view does not exist in the database");
711 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
712 s
+= wxT(", or you have no permissions.\n");
716 else if (checkPrivileges
)
718 // Verify the user has rights to access the table.
719 // Shortcut boolean evaluation to optimize out call to
722 // Unfortunately this optimization doesn't seem to be
724 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
725 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
726 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
733 if (!tablePath
.IsEmpty())
734 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
736 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
739 pDb
->LogError(p
.GetData());
744 // Bind the member variables for field exchange between
745 // the wxDbTable object and the ODBC record.
748 if (!bindInsertParams()) // Inserts
751 if (!bindUpdateParams()) // Updates
755 if (!bindCols(*hstmtDefault
)) // Selects
758 if (!bindCols(hstmtInternal
)) // Internal use only
762 * Do NOT bind the hstmtCount cursor!!!
765 // Build an insert statement using parameter markers
766 if (!queryOnly
&& noCols
> 0)
768 bool needComma
= FALSE
;
769 sqlStmt
.Printf(wxT("INSERT INTO %s ("), tableName
.c_str());
770 for (i
= 0; i
< noCols
; i
++)
772 if (! colDefs
[i
].InsertAllowed
)
776 sqlStmt
+= colDefs
[i
].ColName
;
780 sqlStmt
+= wxT(") VALUES (");
782 int insertableCount
= 0;
784 for (i
= 0; i
< noCols
; i
++)
786 if (! colDefs
[i
].InsertAllowed
)
796 // Prepare the insert statement for execution
799 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
800 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
806 // Completed successfully
809 } // wxDbTable::Open()
812 /********** wxDbTable::Query() **********/
813 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
816 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
818 } // wxDbTable::Query()
821 /********** wxDbTable::QueryBySqlStmt() **********/
822 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
824 pDb
->WriteSqlLog(pSqlStmt
);
826 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
828 } // wxDbTable::QueryBySqlStmt()
831 /********** wxDbTable::QueryMatching() **********/
832 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
835 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
837 } // wxDbTable::QueryMatching()
840 /********** wxDbTable::QueryOnKeyFields() **********/
841 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
844 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
846 } // wxDbTable::QueryOnKeyFields()
849 /********** wxDbTable::GetPrev() **********/
850 bool wxDbTable::GetPrev(void)
852 if (pDb
->FwdOnlyCursors())
854 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
858 return(getRec(SQL_FETCH_PRIOR
));
860 } // wxDbTable::GetPrev()
863 /********** wxDbTable::operator-- **********/
864 bool wxDbTable::operator--(int)
866 if (pDb
->FwdOnlyCursors())
868 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
872 return(getRec(SQL_FETCH_PRIOR
));
874 } // wxDbTable::operator--
877 /********** wxDbTable::GetFirst() **********/
878 bool wxDbTable::GetFirst(void)
880 if (pDb
->FwdOnlyCursors())
882 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
886 return(getRec(SQL_FETCH_FIRST
));
888 } // wxDbTable::GetFirst()
891 /********** wxDbTable::GetLast() **********/
892 bool wxDbTable::GetLast(void)
894 if (pDb
->FwdOnlyCursors())
896 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
900 return(getRec(SQL_FETCH_LAST
));
902 } // wxDbTable::GetLast()
905 /********** wxDbTable::BuildDeleteStmt() **********/
906 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
908 wxASSERT(!queryOnly
);
912 wxString whereClause
;
916 // Handle the case of DeleteWhere() and the where clause is blank. It should
917 // delete all records from the database in this case.
918 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
920 pSqlStmt
.Printf(wxT("DELETE FROM %s"), tableName
.c_str());
924 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "), tableName
.c_str());
926 // Append the WHERE clause to the SQL DELETE statement
929 case DB_DEL_KEYFIELDS
:
930 // If the datasource supports the ROWID column, build
931 // the where on ROWID for efficiency purposes.
932 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
936 wxChar rowid
[wxDB_ROWID_LEN
+1];
938 // Get the ROWID value. If not successful retreiving the ROWID,
939 // simply fall down through the code and build the WHERE clause
940 // based on the key fields.
941 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
943 pSqlStmt
+= wxT("ROWID = '");
945 pSqlStmt
+= wxT("'");
949 // Unable to delete by ROWID, so build a WHERE
950 // clause based on the keyfields.
951 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
952 pSqlStmt
+= whereClause
;
955 pSqlStmt
+= pWhereClause
;
957 case DB_DEL_MATCHING
:
958 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
959 pSqlStmt
+= whereClause
;
963 } // BuildDeleteStmt()
966 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
967 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
969 wxString tempSqlStmt
;
970 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
971 wxStrcpy(pSqlStmt
, tempSqlStmt
);
972 } // wxDbTable::BuildDeleteStmt()
975 /********** wxDbTable::BuildSelectStmt() **********/
976 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
978 wxString whereClause
;
981 // Build a select statement to query the database
982 pSqlStmt
= wxT("SELECT ");
984 // SELECT DISTINCT values only?
986 pSqlStmt
+= wxT("DISTINCT ");
988 // Was a FROM clause specified to join tables to the base table?
989 // Available for ::Query() only!!!
990 bool appendFromClause
= FALSE
;
991 #if wxODBC_BACKWARD_COMPATABILITY
992 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
993 appendFromClause
= TRUE
;
995 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
996 appendFromClause
= TRUE
;
999 // Add the column list
1001 for (i
= 0; i
< noCols
; i
++)
1003 // If joining tables, the base table column names must be qualified to avoid ambiguity
1004 if (appendFromClause
)
1006 pSqlStmt
+= queryTableName
;
1007 pSqlStmt
+= wxT(".");
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
)
1021 pSqlStmt
+= wxT(",");
1022 pSqlStmt
+= queryTableName
;
1023 pSqlStmt
+= wxT(".ROWID");
1026 pSqlStmt
+= wxT(",ROWID");
1029 // Append the FROM tablename portion
1030 pSqlStmt
+= wxT(" FROM ");
1031 pSqlStmt
+= queryTableName
;
1033 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1034 // The HOLDLOCK keyword follows the table name in the from clause.
1035 // Each table in the from clause must specify HOLDLOCK or
1036 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1037 // is parsed but ignored in SYBASE Transact-SQL.
1038 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1039 pSqlStmt
+= wxT(" HOLDLOCK");
1041 if (appendFromClause
)
1044 // Append the WHERE clause. Either append the where clause for the class
1045 // or build a where clause. The typeOfSelect determines this.
1046 switch(typeOfSelect
)
1048 case DB_SELECT_WHERE
:
1049 #if wxODBC_BACKWARD_COMPATABILITY
1050 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1052 if (where
.Length()) // May not want a where clause!!!
1055 pSqlStmt
+= wxT(" WHERE ");
1059 case DB_SELECT_KEYFIELDS
:
1060 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1061 if (whereClause
.Length())
1063 pSqlStmt
+= wxT(" WHERE ");
1064 pSqlStmt
+= whereClause
;
1067 case DB_SELECT_MATCHING
:
1068 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1069 if (whereClause
.Length())
1071 pSqlStmt
+= wxT(" WHERE ");
1072 pSqlStmt
+= whereClause
;
1077 // Append the ORDER BY clause
1078 #if wxODBC_BACKWARD_COMPATABILITY
1079 if (orderBy
&& wxStrlen(orderBy
))
1081 if (orderBy
.Length())
1084 pSqlStmt
+= wxT(" ORDER BY ");
1085 pSqlStmt
+= orderBy
;
1088 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1089 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1090 // HOLDLOCK for Sybase.
1091 if (selectForUpdate
&& CanSelectForUpdate())
1092 pSqlStmt
+= wxT(" FOR UPDATE");
1094 } // wxDbTable::BuildSelectStmt()
1097 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1098 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1100 wxString tempSqlStmt
;
1101 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1102 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1103 } // wxDbTable::BuildSelectStmt()
1106 /********** wxDbTable::BuildUpdateStmt() **********/
1107 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1109 wxASSERT(!queryOnly
);
1113 wxString whereClause
;
1114 whereClause
.Empty();
1116 bool firstColumn
= TRUE
;
1118 pSqlStmt
.Printf(wxT("UPDATE %s SET "), tableName
.c_str());
1120 // Append a list of columns to be updated
1122 for (i
= 0; i
< noCols
; i
++)
1124 // Only append Updateable columns
1125 if (colDefs
[i
].Updateable
)
1128 pSqlStmt
+= wxT(",");
1130 firstColumn
= FALSE
;
1131 pSqlStmt
+= colDefs
[i
].ColName
;
1132 pSqlStmt
+= wxT(" = ?");
1136 // Append the WHERE clause to the SQL UPDATE statement
1137 pSqlStmt
+= wxT(" WHERE ");
1140 case DB_UPD_KEYFIELDS
:
1141 // If the datasource supports the ROWID column, build
1142 // the where on ROWID for efficiency purposes.
1143 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1144 if (CanUpdByROWID())
1147 wxChar rowid
[wxDB_ROWID_LEN
+1];
1149 // Get the ROWID value. If not successful retreiving the ROWID,
1150 // simply fall down through the code and build the WHERE clause
1151 // based on the key fields.
1152 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1154 pSqlStmt
+= wxT("ROWID = '");
1156 pSqlStmt
+= wxT("'");
1160 // Unable to delete by ROWID, so build a WHERE
1161 // clause based on the keyfields.
1162 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1163 pSqlStmt
+= whereClause
;
1166 pSqlStmt
+= pWhereClause
;
1169 } // BuildUpdateStmt()
1172 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1173 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1175 wxString tempSqlStmt
;
1176 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1177 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1178 } // BuildUpdateStmt()
1181 /********** wxDbTable::BuildWhereClause() **********/
1182 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1183 const wxString
&qualTableName
, bool useLikeComparison
)
1185 * Note: BuildWhereClause() currently ignores timestamp columns.
1186 * They are not included as part of the where clause.
1189 bool moreThanOneColumn
= FALSE
;
1192 // Loop through the columns building a where clause as you go
1194 for (i
= 0; i
< noCols
; i
++)
1196 // Determine if this column should be included in the WHERE clause
1197 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1198 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1200 // Skip over timestamp columns
1201 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1203 // If there is more than 1 column, join them with the keyword "AND"
1204 if (moreThanOneColumn
)
1205 pWhereClause
+= wxT(" AND ");
1207 moreThanOneColumn
= TRUE
;
1208 // Concatenate where phrase for the column
1209 if (qualTableName
.Length())
1211 pWhereClause
+= qualTableName
;
1212 pWhereClause
+= wxT(".");
1214 pWhereClause
+= colDefs
[i
].ColName
;
1215 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1216 pWhereClause
+= wxT(" LIKE ");
1218 pWhereClause
+= wxT(" = ");
1219 switch(colDefs
[i
].SqlCtype
)
1222 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1225 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[i
].PtrDataObj
));
1228 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[i
].PtrDataObj
));
1231 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1234 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1237 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1240 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1243 pWhereClause
+= colValue
;
1246 } // wxDbTable::BuildWhereClause()
1249 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1250 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1251 const wxString
&qualTableName
, bool useLikeComparison
)
1253 wxString tempSqlStmt
;
1254 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1255 wxStrcpy(pWhereClause
, tempSqlStmt
);
1256 } // wxDbTable::BuildWhereClause()
1259 /********** wxDbTable::GetRowNum() **********/
1260 UWORD
wxDbTable::GetRowNum(void)
1264 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1266 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1270 // Completed successfully
1271 return((UWORD
) rowNum
);
1273 } // wxDbTable::GetRowNum()
1276 /********** wxDbTable::CloseCursor() **********/
1277 bool wxDbTable::CloseCursor(HSTMT cursor
)
1279 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1280 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1282 // Completed successfully
1285 } // wxDbTable::CloseCursor()
1288 /********** wxDbTable::CreateTable() **********/
1289 bool wxDbTable::CreateTable(bool attemptDrop
)
1297 #ifdef DBDEBUG_CONSOLE
1298 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1302 if (attemptDrop
&& !DropTable())
1306 #ifdef DBDEBUG_CONSOLE
1307 for (i
= 0; i
< noCols
; i
++)
1309 // Exclude derived columns since they are NOT part of the base table
1310 if (colDefs
[i
].DerivedCol
)
1312 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1313 switch(colDefs
[i
].DbDataType
)
1315 case DB_DATA_TYPE_VARCHAR
:
1316 cout
<< pDb
->typeInfVarchar
.TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1318 case DB_DATA_TYPE_INTEGER
:
1319 cout
<< pDb
->typeInfInteger
.TypeName
;
1321 case DB_DATA_TYPE_FLOAT
:
1322 cout
<< pDb
->typeInfFloat
.TypeName
;
1324 case DB_DATA_TYPE_DATE
:
1325 cout
<< pDb
->typeInfDate
.TypeName
;
1327 case DB_DATA_TYPE_BLOB
:
1328 cout
<< pDb
->typeInfBlob
.TypeName
;
1335 // Build a CREATE TABLE string from the colDefs structure.
1336 bool needComma
= FALSE
;
1337 sqlStmt
.Printf(wxT("CREATE TABLE %s ("), tableName
.c_str());
1339 for (i
= 0; i
< noCols
; i
++)
1341 // Exclude derived columns since they are NOT part of the base table
1342 if (colDefs
[i
].DerivedCol
)
1346 sqlStmt
+= wxT(",");
1348 sqlStmt
+= colDefs
[i
].ColName
;
1349 sqlStmt
+= wxT(" ");
1351 switch(colDefs
[i
].DbDataType
)
1353 case DB_DATA_TYPE_VARCHAR
:
1354 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1356 case DB_DATA_TYPE_INTEGER
:
1357 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1359 case DB_DATA_TYPE_FLOAT
:
1360 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1362 case DB_DATA_TYPE_DATE
:
1363 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1365 case DB_DATA_TYPE_BLOB
:
1366 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1369 // For varchars, append the size of the string
1370 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)// ||
1371 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1374 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1378 if (pDb
->Dbms() == dbmsDB2
||
1379 pDb
->Dbms() == dbmsMY_SQL
||
1380 pDb
->Dbms() == dbmsSYBASE_ASE
||
1381 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1383 if (colDefs
[i
].KeyField
)
1385 sqlStmt
+= wxT(" NOT NULL");
1391 // If there is a primary key defined, include it in the create statement
1392 for (i
= j
= 0; i
< noCols
; i
++)
1394 if (colDefs
[i
].KeyField
)
1400 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1402 switch (pDb
->Dbms())
1404 case dbmsSYBASE_ASA
:
1405 case dbmsSYBASE_ASE
:
1408 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1409 sqlStmt
+= wxT(",PRIMARY KEY (");
1414 sqlStmt
+= wxT(",CONSTRAINT ");
1415 sqlStmt
+= tableName
;
1416 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1421 // List column name(s) of column(s) comprising the primary key
1422 for (i
= j
= 0; i
< noCols
; i
++)
1424 if (colDefs
[i
].KeyField
)
1426 if (j
++) // Multi part key, comma separate names
1427 sqlStmt
+= wxT(",");
1428 sqlStmt
+= colDefs
[i
].ColName
;
1431 sqlStmt
+= wxT(")");
1433 if (pDb
->Dbms() == dbmsSYBASE_ASA
||
1434 pDb
->Dbms() == dbmsSYBASE_ASE
)
1436 sqlStmt
+= wxT(" CONSTRAINT ");
1437 sqlStmt
+= tableName
;
1438 sqlStmt
+= wxT("_PIDX");
1441 // Append the closing parentheses for the create table statement
1442 sqlStmt
+= wxT(")");
1444 pDb
->WriteSqlLog(sqlStmt
);
1446 #ifdef DBDEBUG_CONSOLE
1447 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1450 // Execute the CREATE TABLE statement
1451 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1452 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1454 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1455 pDb
->RollbackTrans();
1460 // Commit the transaction and close the cursor
1461 if (!pDb
->CommitTrans())
1463 if (!CloseCursor(hstmt
))
1466 // Database table created successfully
1469 } // wxDbTable::CreateTable()
1472 /********** wxDbTable::DropTable() **********/
1473 bool wxDbTable::DropTable()
1475 // NOTE: This function returns TRUE if the Table does not exist, but
1476 // only for identified databases. Code will need to be added
1477 // below for any other databases when those databases are defined
1478 // to handle this situation consistently
1482 sqlStmt
.Printf(wxT("DROP TABLE %s"), tableName
.c_str());
1484 pDb
->WriteSqlLog(sqlStmt
);
1486 #ifdef DBDEBUG_CONSOLE
1487 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1490 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1492 // Check for "Base table not found" error and ignore
1493 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1494 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) &&
1495 wxStrcmp(pDb
->sqlState
, wxT("S1000"))) // "Base table not found"
1497 // Check for product specific error codes
1498 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1499 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1500 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1502 pDb
->DispNextError();
1503 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1504 pDb
->RollbackTrans();
1511 // Commit the transaction and close the cursor
1512 if (! pDb
->CommitTrans())
1514 if (! CloseCursor(hstmt
))
1518 } // wxDbTable::DropTable()
1521 /********** wxDbTable::CreateIndex() **********/
1522 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1523 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1527 // Drop the index first
1528 if (attemptDrop
&& !DropIndex(idxName
))
1531 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1532 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1533 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1534 // table was created, then months later you determine that an additional index while
1535 // give better performance, so you want to add an index).
1537 // The following block of code will modify the column definition to make the column be
1538 // defined with the "NOT NULL" qualifier.
1539 if (pDb
->Dbms() == dbmsMY_SQL
)
1544 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1548 // Find the column definition that has the ColName that matches the
1549 // index column name. We need to do this to get the DB_DATA_TYPE of
1550 // the index column, as MySQL's syntax for the ALTER column requires
1552 while (!found
&& (j
< this->noCols
))
1554 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1562 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1563 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1568 wxODBC_ERRORS retcode
;
1569 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1570 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1571 // This line is just here for debug checking of the value
1572 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1582 pDb
->RollbackTrans();
1587 // Build a CREATE INDEX statement
1588 sqlStmt
= wxT("CREATE ");
1590 sqlStmt
+= wxT("UNIQUE ");
1592 sqlStmt
+= wxT("INDEX ");
1594 sqlStmt
+= wxT(" ON ");
1595 sqlStmt
+= tableName
;
1596 sqlStmt
+= wxT(" (");
1598 // Append list of columns making up index
1600 for (i
= 0; i
< noIdxCols
; i
++)
1602 sqlStmt
+= pIdxDefs
[i
].ColName
;
1603 /* Postgres doesn't cope with ASC */
1604 if (pDb
->Dbms() != dbmsPOSTGRES
)
1606 if (pIdxDefs
[i
].Ascending
)
1607 sqlStmt
+= wxT(" ASC");
1609 sqlStmt
+= wxT(" DESC");
1612 if ((i
+ 1) < noIdxCols
)
1613 sqlStmt
+= wxT(",");
1616 // Append closing parentheses
1617 sqlStmt
+= wxT(")");
1619 pDb
->WriteSqlLog(sqlStmt
);
1621 #ifdef DBDEBUG_CONSOLE
1622 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1625 // Execute the CREATE INDEX statement
1626 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1628 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1629 pDb
->RollbackTrans();
1634 // Commit the transaction and close the cursor
1635 if (! pDb
->CommitTrans())
1637 if (! CloseCursor(hstmt
))
1640 // Index Created Successfully
1643 } // wxDbTable::CreateIndex()
1646 /********** wxDbTable::DropIndex() **********/
1647 bool wxDbTable::DropIndex(const wxString
&idxName
)
1649 // NOTE: This function returns TRUE if the Index does not exist, but
1650 // only for identified databases. Code will need to be added
1651 // below for any other databases when those databases are defined
1652 // to handle this situation consistently
1656 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
)
1657 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),idxName
.c_str(), tableName
.c_str());
1658 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1659 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1660 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),tableName
.c_str(), idxName
.c_str());
1662 sqlStmt
.Printf(wxT("DROP INDEX %s"),idxName
.c_str());
1664 pDb
->WriteSqlLog(sqlStmt
);
1666 #ifdef DBDEBUG_CONSOLE
1667 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1670 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1672 // Check for "Index not found" error and ignore
1673 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1674 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1676 // Check for product specific error codes
1677 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1678 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1679 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1680 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1681 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1682 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1685 pDb
->DispNextError();
1686 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1687 pDb
->RollbackTrans();
1694 // Commit the transaction and close the cursor
1695 if (! pDb
->CommitTrans())
1697 if (! CloseCursor(hstmt
))
1701 } // wxDbTable::DropIndex()
1704 /********** wxDbTable::SetOrderByColNums() **********/
1705 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1707 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1713 va_start(argptr
, first
); /* Initialize variable arguments. */
1714 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1716 // Make sure the passed in column number
1717 // is within the valid range of columns
1719 // Valid columns are 0 thru noCols-1
1720 if (colNo
>= noCols
|| colNo
< 0)
1727 tempStr
+= wxT(",");
1729 tempStr
+= colDefs
[colNo
].ColName
;
1730 colNo
= va_arg (argptr
, int);
1732 va_end (argptr
); /* Reset variable arguments. */
1734 SetOrderByClause(tempStr
);
1737 } // wxDbTable::SetOrderByColNums()
1740 /********** wxDbTable::Insert() **********/
1741 int wxDbTable::Insert(void)
1743 wxASSERT(!queryOnly
);
1744 if (queryOnly
|| !insertable
)
1749 // Insert the record by executing the already prepared insert statement
1751 retcode
=SQLExecute(hstmtInsert
);
1752 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1754 // Check to see if integrity constraint was violated
1755 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1756 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1757 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1760 pDb
->DispNextError();
1761 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1766 // Record inserted into the datasource successfully
1769 } // wxDbTable::Insert()
1772 /********** wxDbTable::Update() **********/
1773 bool wxDbTable::Update(void)
1775 wxASSERT(!queryOnly
);
1781 // Build the SQL UPDATE statement
1782 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1784 pDb
->WriteSqlLog(sqlStmt
);
1786 #ifdef DBDEBUG_CONSOLE
1787 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1790 // Execute the SQL UPDATE statement
1791 return(execUpdate(sqlStmt
));
1793 } // wxDbTable::Update()
1796 /********** wxDbTable::Update(pSqlStmt) **********/
1797 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1799 wxASSERT(!queryOnly
);
1803 pDb
->WriteSqlLog(pSqlStmt
);
1805 return(execUpdate(pSqlStmt
));
1807 } // wxDbTable::Update(pSqlStmt)
1810 /********** wxDbTable::UpdateWhere() **********/
1811 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1813 wxASSERT(!queryOnly
);
1819 // Build the SQL UPDATE statement
1820 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1822 pDb
->WriteSqlLog(sqlStmt
);
1824 #ifdef DBDEBUG_CONSOLE
1825 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1828 // Execute the SQL UPDATE statement
1829 return(execUpdate(sqlStmt
));
1831 } // wxDbTable::UpdateWhere()
1834 /********** wxDbTable::Delete() **********/
1835 bool wxDbTable::Delete(void)
1837 wxASSERT(!queryOnly
);
1844 // Build the SQL DELETE statement
1845 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1847 pDb
->WriteSqlLog(sqlStmt
);
1849 // Execute the SQL DELETE statement
1850 return(execDelete(sqlStmt
));
1852 } // wxDbTable::Delete()
1855 /********** wxDbTable::DeleteWhere() **********/
1856 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1858 wxASSERT(!queryOnly
);
1865 // Build the SQL DELETE statement
1866 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1868 pDb
->WriteSqlLog(sqlStmt
);
1870 // Execute the SQL DELETE statement
1871 return(execDelete(sqlStmt
));
1873 } // wxDbTable::DeleteWhere()
1876 /********** wxDbTable::DeleteMatching() **********/
1877 bool wxDbTable::DeleteMatching(void)
1879 wxASSERT(!queryOnly
);
1886 // Build the SQL DELETE statement
1887 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1889 pDb
->WriteSqlLog(sqlStmt
);
1891 // Execute the SQL DELETE statement
1892 return(execDelete(sqlStmt
));
1894 } // wxDbTable::DeleteMatching()
1897 /********** wxDbTable::IsColNull() **********/
1898 bool wxDbTable::IsColNull(UWORD colNo
)
1901 This logic is just not right. It would indicate TRUE
1902 if a numeric field were set to a value of 0.
1904 switch(colDefs[colNo].SqlCtype)
1907 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1909 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1911 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1913 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1915 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1917 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1919 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1920 case SQL_C_TIMESTAMP:
1921 TIMESTAMP_STRUCT *pDt;
1922 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1923 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1931 return (colDefs
[colNo
].Null
);
1932 } // wxDbTable::IsColNull()
1935 /********** wxDbTable::CanSelectForUpdate() **********/
1936 bool wxDbTable::CanSelectForUpdate(void)
1941 if (pDb
->Dbms() == dbmsMY_SQL
)
1944 if ((pDb
->Dbms() == dbmsORACLE
) ||
1945 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
1950 } // wxDbTable::CanSelectForUpdate()
1953 /********** wxDbTable::CanUpdByROWID() **********/
1954 bool wxDbTable::CanUpdByROWID(void)
1957 * NOTE: Returning FALSE for now until this can be debugged,
1958 * as the ROWID is not getting updated correctly
1962 if (pDb->Dbms() == dbmsORACLE)
1967 } // wxDbTable::CanUpdByROWID()
1970 /********** wxDbTable::IsCursorClosedOnCommit() **********/
1971 bool wxDbTable::IsCursorClosedOnCommit(void)
1973 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1978 } // wxDbTable::IsCursorClosedOnCommit()
1982 /********** wxDbTable::ClearMemberVar() **********/
1983 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
1985 wxASSERT(colNo
< noCols
);
1987 switch(colDefs
[colNo
].SqlCtype
)
1990 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
1993 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1996 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1999 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2002 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2005 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2008 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2010 case SQL_C_TIMESTAMP
:
2011 TIMESTAMP_STRUCT
*pDt
;
2012 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2025 } // wxDbTable::ClearMemberVar()
2028 /********** wxDbTable::ClearMemberVars() **********/
2029 void wxDbTable::ClearMemberVars(bool setToNull
)
2033 // Loop through the columns setting each member variable to zero
2034 for (i
=0; i
< noCols
; i
++)
2035 ClearMemberVar(i
,setToNull
);
2037 } // wxDbTable::ClearMemberVars()
2040 /********** wxDbTable::SetQueryTimeout() **********/
2041 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2043 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2044 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2045 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2046 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2047 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2048 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2049 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2050 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2052 // Completed Successfully
2055 } // wxDbTable::SetQueryTimeout()
2058 /********** wxDbTable::SetColDefs() **********/
2059 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2060 SWORD cType
, int size
, bool keyField
, bool upd
,
2061 bool insAllow
, bool derivedCol
)
2063 if (!colDefs
) // May happen if the database connection fails
2066 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2068 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2069 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2072 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2074 colDefs
[index
].DbDataType
= dataType
;
2075 colDefs
[index
].PtrDataObj
= pData
;
2076 colDefs
[index
].SqlCtype
= cType
;
2077 colDefs
[index
].SzDataObj
= size
;
2078 colDefs
[index
].KeyField
= keyField
;
2079 colDefs
[index
].DerivedCol
= derivedCol
;
2080 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2083 colDefs
[index
].Updateable
= FALSE
;
2084 colDefs
[index
].InsertAllowed
= FALSE
;
2088 colDefs
[index
].Updateable
= upd
;
2089 colDefs
[index
].InsertAllowed
= insAllow
;
2092 colDefs
[index
].Null
= FALSE
;
2094 } // wxDbTable::SetColDefs()
2097 /********** wxDbTable::SetColDefs() **********/
2098 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2101 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2107 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2109 for (index
= 0; index
< numCols
; index
++)
2111 // Process the fields
2112 switch (pColInfs
[index
].dbDataType
)
2114 case DB_DATA_TYPE_VARCHAR
:
2115 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2116 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2117 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2119 case DB_DATA_TYPE_INTEGER
:
2120 // Can be long or short
2121 if (pColInfs
[index
].bufferLength
== sizeof(long))
2123 pColDataPtrs
[index
].PtrDataObj
= new long;
2124 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2125 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2129 pColDataPtrs
[index
].PtrDataObj
= new short;
2130 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2131 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2134 case DB_DATA_TYPE_FLOAT
:
2135 // Can be float or double
2136 if (pColInfs
[index
].bufferLength
== sizeof(float))
2138 pColDataPtrs
[index
].PtrDataObj
= new float;
2139 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2140 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2144 pColDataPtrs
[index
].PtrDataObj
= new double;
2145 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2146 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2149 case DB_DATA_TYPE_DATE
:
2150 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2151 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2152 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2154 case DB_DATA_TYPE_BLOB
:
2155 int notSupportedYet
= 0;
2156 wxASSERT_MSG(notSupportedYet
, wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2157 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2158 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2159 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2162 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2163 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2166 // Unable to build all the column definitions, as either one of
2167 // the calls to "new" failed above, or there was a BLOB field
2168 // to have a column definition for. If BLOBs are to be used,
2169 // the other form of ::SetColDefs() must be used, as it is impossible
2170 // to know the maximum size to create the PtrDataObj to be.
2171 delete [] pColDataPtrs
;
2177 return (pColDataPtrs
);
2179 } // wxDbTable::SetColDefs()
2182 /********** wxDbTable::SetCursor() **********/
2183 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2185 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2186 hstmt
= *hstmtDefault
;
2188 hstmt
= *hstmtActivate
;
2190 } // wxDbTable::SetCursor()
2193 /********** wxDbTable::Count(const wxString &) **********/
2194 ULONG
wxDbTable::Count(const wxString
&args
)
2200 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2201 sqlStmt
= wxT("SELECT COUNT(");
2203 sqlStmt
+= wxT(") FROM ");
2204 sqlStmt
+= queryTableName
;
2205 #if wxODBC_BACKWARD_COMPATABILITY
2206 if (from
&& wxStrlen(from
))
2212 // Add the where clause if one is provided
2213 #if wxODBC_BACKWARD_COMPATABILITY
2214 if (where
&& wxStrlen(where
))
2219 sqlStmt
+= wxT(" WHERE ");
2223 pDb
->WriteSqlLog(sqlStmt
);
2225 // Initialize the Count cursor if it's not already initialized
2228 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2229 wxASSERT(hstmtCount
);
2234 // Execute the SQL statement
2235 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2237 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2242 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2244 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2248 // Obtain the result
2249 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2251 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2256 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2257 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2259 // Return the record count
2262 } // wxDbTable::Count()
2265 /********** wxDbTable::Refresh() **********/
2266 bool wxDbTable::Refresh(void)
2270 // Switch to the internal cursor so any active cursors are not corrupted
2271 HSTMT currCursor
= GetCursor();
2272 hstmt
= hstmtInternal
;
2273 #if wxODBC_BACKWARD_COMPATABILITY
2274 // Save the where and order by clauses
2275 char *saveWhere
= where
;
2276 char *saveOrderBy
= orderBy
;
2278 wxString saveWhere
= where
;
2279 wxString saveOrderBy
= orderBy
;
2281 // Build a where clause to refetch the record with. Try and use the
2282 // ROWID if it's available, ow use the key fields.
2283 wxString whereClause
;
2284 whereClause
.Empty();
2286 if (CanUpdByROWID())
2289 wxChar rowid
[wxDB_ROWID_LEN
+1];
2291 // Get the ROWID value. If not successful retreiving the ROWID,
2292 // simply fall down through the code and build the WHERE clause
2293 // based on the key fields.
2294 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2296 whereClause
+= queryTableName
;
2297 whereClause
+= wxT(".ROWID = '");
2298 whereClause
+= rowid
;
2299 whereClause
+= wxT("'");
2303 // If unable to use the ROWID, build a where clause from the keyfields
2304 if (wxStrlen(whereClause
) == 0)
2305 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2307 // Requery the record
2308 where
= whereClause
;
2313 if (result
&& !GetNext())
2316 // Switch back to original cursor
2317 SetCursor(&currCursor
);
2319 // Free the internal cursor
2320 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2321 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2323 // Restore the original where and order by clauses
2325 orderBy
= saveOrderBy
;
2329 } // wxDbTable::Refresh()
2332 /********** wxDbTable::SetColNull() **********/
2333 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2337 colDefs
[colNo
].Null
= set
;
2338 if (set
) // Blank out the values in the member variable
2339 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2345 } // wxDbTable::SetColNull()
2348 /********** wxDbTable::SetColNull() **********/
2349 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2352 for (i
= 0; i
< noCols
; i
++)
2354 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2360 colDefs
[i
].Null
= set
;
2361 if (set
) // Blank out the values in the member variable
2362 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2368 } // wxDbTable::SetColNull()
2371 /********** wxDbTable::GetNewCursor() **********/
2372 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2374 HSTMT
*newHSTMT
= new HSTMT
;
2379 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2381 pDb
->DispAllErrors(henv
, hdbc
);
2386 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2388 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2395 if(!bindCols(*newHSTMT
))
2403 SetCursor(newHSTMT
);
2407 } // wxDbTable::GetNewCursor()
2410 /********** wxDbTable::DeleteCursor() **********/
2411 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2415 if (!hstmtDel
) // Cursor already deleted
2419 ODBC 3.0 says to use this form
2420 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2423 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2425 pDb
->DispAllErrors(henv
, hdbc
);
2433 } // wxDbTable::DeleteCursor()
2435 #endif // wxUSE_ODBC