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 int nCols
,
132 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
134 if (!initialize(pwxDb
, tblName
, nCols
, qryTblName
, qryOnly
, tblPath
))
136 } // wxDbTable::wxDbTable()
139 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
140 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const int nCols
,
141 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
143 wxString tempQryTblName
;
144 tempQryTblName
= qryTblName
;
145 if (!initialize(pwxDb
, tblName
, nCols
, 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 int nCols
,
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
= nCols
; // No. 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
434 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
438 if (! colDefs
[i
].Updateable
)
443 if (! colDefs
[i
].InsertAllowed
)
447 switch(colDefs
[i
].DbDataType
)
449 case DB_DATA_TYPE_VARCHAR
:
450 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
451 precision
= colDefs
[i
].SzDataObj
;
454 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
456 colDefs
[i
].CbValue
= SQL_NTS
;
458 case DB_DATA_TYPE_INTEGER
:
459 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
460 precision
= pDb
->GetTypeInfInteger().Precision
;
463 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
465 colDefs
[i
].CbValue
= 0;
467 case DB_DATA_TYPE_FLOAT
:
468 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
469 precision
= pDb
->GetTypeInfFloat().Precision
;
470 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
471 // SQL Sybase Anywhere v5.5 returned a negative number for the
472 // MaxScale. This caused ODBC to kick out an error on ibscale.
473 // I check for this here and set the scale = precision.
475 // scale = (short) precision;
477 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
479 colDefs
[i
].CbValue
= 0;
481 case DB_DATA_TYPE_DATE
:
482 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
483 precision
= pDb
->GetTypeInfDate().Precision
;
486 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
488 colDefs
[i
].CbValue
= 0;
490 case DB_DATA_TYPE_BLOB
:
491 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
495 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
497 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
502 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
503 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
504 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
506 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
511 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
512 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
513 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
515 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
520 // Completed successfully
523 } // wxDbTable::bindParams()
526 /********** wxDbTable::bindInsertParams() **********/
527 bool wxDbTable::bindInsertParams(void)
529 return bindParams(FALSE
);
530 } // wxDbTable::bindInsertParams()
533 /********** wxDbTable::bindUpdateParams() **********/
534 bool wxDbTable::bindUpdateParams(void)
536 return bindParams(TRUE
);
537 } // wxDbTable::bindUpdateParams()
540 /********** wxDbTable::bindCols() **********/
541 bool wxDbTable::bindCols(HSTMT cursor
)
543 //RG-NULL static SDWORD cb;
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
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
550 //RG-NULL colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
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
)
707 // Verify that the table exists in the database
708 if (!pDb
->TableExists(tableName
,/*pDb->GetUsername()*/NULL
,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"),NULL
,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
, 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
, 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 if (pDb
->Dbms() != dbmsMY_SQL
)
1404 sqlStmt
+= wxT(",CONSTRAINT ");
1405 sqlStmt
+= tableName
;
1406 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1410 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1411 sqlStmt
+= wxT(", PRIMARY KEY (");
1414 // List column name(s) of column(s) comprising the primary key
1415 for (i
= j
= 0; i
< noCols
; i
++)
1417 if (colDefs
[i
].KeyField
)
1419 if (j
++) // Multi part key, comma separate names
1420 sqlStmt
+= wxT(",");
1421 sqlStmt
+= colDefs
[i
].ColName
;
1424 sqlStmt
+= wxT(")");
1426 // Append the closing parentheses for the create table statement
1427 sqlStmt
+= wxT(")");
1429 pDb
->WriteSqlLog(sqlStmt
);
1431 #ifdef DBDEBUG_CONSOLE
1432 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1435 // Execute the CREATE TABLE statement
1436 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1437 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1439 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1440 pDb
->RollbackTrans();
1445 // Commit the transaction and close the cursor
1446 if (!pDb
->CommitTrans())
1448 if (!CloseCursor(hstmt
))
1451 // Database table created successfully
1454 } // wxDbTable::CreateTable()
1457 /********** wxDbTable::DropTable() **********/
1458 bool wxDbTable::DropTable()
1460 // NOTE: This function returns TRUE if the Table does not exist, but
1461 // only for identified databases. Code will need to be added
1462 // below for any other databases when those databases are defined
1463 // to handle this situation consistently
1467 sqlStmt
.Printf(wxT("DROP TABLE %s"), tableName
.c_str());
1469 pDb
->WriteSqlLog(sqlStmt
);
1471 #ifdef DBDEBUG_CONSOLE
1472 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1475 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1477 // Check for "Base table not found" error and ignore
1478 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1479 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) &&
1480 wxStrcmp(pDb
->sqlState
, wxT("S1000"))) // "Base table not found"
1482 // Check for product specific error codes
1483 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1484 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1485 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1487 pDb
->DispNextError();
1488 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1489 pDb
->RollbackTrans();
1496 // Commit the transaction and close the cursor
1497 if (! pDb
->CommitTrans())
1499 if (! CloseCursor(hstmt
))
1503 } // wxDbTable::DropTable()
1506 /********** wxDbTable::CreateIndex() **********/
1507 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, int noIdxCols
, wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1511 // Drop the index first
1512 if (attemptDrop
&& !DropIndex(idxName
))
1515 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1516 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1517 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1518 // table was created, then months later you determine that an additional index while
1519 // give better performance, so you want to add an index).
1521 // The following block of code will modify the column definition to make the column be
1522 // defined with the "NOT NULL" qualifier.
1523 if (pDb
->Dbms() == dbmsMY_SQL
)
1528 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1532 // Find the column definition that has the ColName that matches the
1533 // index column name. We need to do this to get the DB_DATA_TYPE of
1534 // the index column, as MySQL's syntax for the ALTER column requires
1536 while (!found
&& (j
< this->noCols
))
1538 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1546 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1547 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1552 wxODBC_ERRORS retcode
;
1553 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1554 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1555 // This line is just here for debug checking of the value
1556 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1566 pDb
->RollbackTrans();
1571 // Build a CREATE INDEX statement
1572 sqlStmt
= wxT("CREATE ");
1574 sqlStmt
+= wxT("UNIQUE ");
1576 sqlStmt
+= wxT("INDEX ");
1578 sqlStmt
+= wxT(" ON ");
1579 sqlStmt
+= tableName
;
1580 sqlStmt
+= wxT(" (");
1582 // Append list of columns making up index
1584 for (i
= 0; i
< noIdxCols
; i
++)
1586 sqlStmt
+= pIdxDefs
[i
].ColName
;
1587 /* Postgres doesn't cope with ASC */
1588 if (pDb
->Dbms() != dbmsPOSTGRES
)
1590 if (pIdxDefs
[i
].Ascending
)
1591 sqlStmt
+= wxT(" ASC");
1593 sqlStmt
+= wxT(" DESC");
1596 if ((i
+ 1) < noIdxCols
)
1597 sqlStmt
+= wxT(",");
1600 // Append closing parentheses
1601 sqlStmt
+= wxT(")");
1603 pDb
->WriteSqlLog(sqlStmt
);
1605 #ifdef DBDEBUG_CONSOLE
1606 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1609 // Execute the CREATE INDEX statement
1610 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1612 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1613 pDb
->RollbackTrans();
1618 // Commit the transaction and close the cursor
1619 if (! pDb
->CommitTrans())
1621 if (! CloseCursor(hstmt
))
1624 // Index Created Successfully
1627 } // wxDbTable::CreateIndex()
1630 /********** wxDbTable::DropIndex() **********/
1631 bool wxDbTable::DropIndex(const wxString
&idxName
)
1633 // NOTE: This function returns TRUE if the Index does not exist, but
1634 // only for identified databases. Code will need to be added
1635 // below for any other databases when those databases are defined
1636 // to handle this situation consistently
1640 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
)
1641 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),idxName
.c_str(), tableName
.c_str());
1642 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1643 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1644 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),tableName
.c_str(), idxName
.c_str());
1646 sqlStmt
.Printf(wxT("DROP INDEX %s"),idxName
.c_str());
1648 pDb
->WriteSqlLog(sqlStmt
);
1650 #ifdef DBDEBUG_CONSOLE
1651 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1654 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1656 // Check for "Index not found" error and ignore
1657 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1658 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1660 // Check for product specific error codes
1661 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1662 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1663 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1664 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1665 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1666 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1669 pDb
->DispNextError();
1670 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1671 pDb
->RollbackTrans();
1678 // Commit the transaction and close the cursor
1679 if (! pDb
->CommitTrans())
1681 if (! CloseCursor(hstmt
))
1685 } // wxDbTable::DropIndex()
1688 /********** wxDbTable::SetOrderByColNums() **********/
1689 bool wxDbTable::SetOrderByColNums(int first
, ... )
1697 va_start(argptr
, first
); /* Initialize variable arguments. */
1698 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1700 // Make sure the passed in column number
1701 // is within the valid range of columns
1703 // Valid columns are 0 thru noCols-1
1704 if (colNo
>= noCols
|| colNo
< 0)
1711 tempStr
+= wxT(",");
1713 tempStr
+= colDefs
[colNo
].ColName
;
1714 colNo
= va_arg (argptr
, int);
1716 va_end (argptr
); /* Reset variable arguments. */
1718 SetOrderByClause(tempStr
);
1721 } // wxDbTable::SetOrderByColNums()
1724 /********** wxDbTable::Insert() **********/
1725 int wxDbTable::Insert(void)
1727 wxASSERT(!queryOnly
);
1728 if (queryOnly
|| !insertable
)
1733 // Insert the record by executing the already prepared insert statement
1735 retcode
=SQLExecute(hstmtInsert
);
1736 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1738 // Check to see if integrity constraint was violated
1739 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1740 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1741 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1744 pDb
->DispNextError();
1745 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1750 // Record inserted into the datasource successfully
1753 } // wxDbTable::Insert()
1756 /********** wxDbTable::Update() **********/
1757 bool wxDbTable::Update(void)
1759 wxASSERT(!queryOnly
);
1765 // Build the SQL UPDATE statement
1766 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1768 pDb
->WriteSqlLog(sqlStmt
);
1770 #ifdef DBDEBUG_CONSOLE
1771 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1774 // Execute the SQL UPDATE statement
1775 return(execUpdate(sqlStmt
));
1777 } // wxDbTable::Update()
1780 /********** wxDbTable::Update(pSqlStmt) **********/
1781 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1783 wxASSERT(!queryOnly
);
1787 pDb
->WriteSqlLog(pSqlStmt
);
1789 return(execUpdate(pSqlStmt
));
1791 } // wxDbTable::Update(pSqlStmt)
1794 /********** wxDbTable::UpdateWhere() **********/
1795 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1797 wxASSERT(!queryOnly
);
1803 // Build the SQL UPDATE statement
1804 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1806 pDb
->WriteSqlLog(sqlStmt
);
1808 #ifdef DBDEBUG_CONSOLE
1809 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1812 // Execute the SQL UPDATE statement
1813 return(execUpdate(sqlStmt
));
1815 } // wxDbTable::UpdateWhere()
1818 /********** wxDbTable::Delete() **********/
1819 bool wxDbTable::Delete(void)
1821 wxASSERT(!queryOnly
);
1828 // Build the SQL DELETE statement
1829 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1831 pDb
->WriteSqlLog(sqlStmt
);
1833 // Execute the SQL DELETE statement
1834 return(execDelete(sqlStmt
));
1836 } // wxDbTable::Delete()
1839 /********** wxDbTable::DeleteWhere() **********/
1840 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1842 wxASSERT(!queryOnly
);
1849 // Build the SQL DELETE statement
1850 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1852 pDb
->WriteSqlLog(sqlStmt
);
1854 // Execute the SQL DELETE statement
1855 return(execDelete(sqlStmt
));
1857 } // wxDbTable::DeleteWhere()
1860 /********** wxDbTable::DeleteMatching() **********/
1861 bool wxDbTable::DeleteMatching(void)
1863 wxASSERT(!queryOnly
);
1870 // Build the SQL DELETE statement
1871 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1873 pDb
->WriteSqlLog(sqlStmt
);
1875 // Execute the SQL DELETE statement
1876 return(execDelete(sqlStmt
));
1878 } // wxDbTable::DeleteMatching()
1881 /********** wxDbTable::IsColNull() **********/
1882 bool wxDbTable::IsColNull(int colNo
)
1885 This logic is just not right. It would indicate TRUE
1886 if a numeric field were set to a value of 0.
1888 switch(colDefs[colNo].SqlCtype)
1891 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1893 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1895 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1897 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1899 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1901 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1903 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1904 case SQL_C_TIMESTAMP:
1905 TIMESTAMP_STRUCT *pDt;
1906 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1907 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1915 return (colDefs
[colNo
].Null
);
1916 } // wxDbTable::IsColNull()
1919 /********** wxDbTable::CanSelectForUpdate() **********/
1920 bool wxDbTable::CanSelectForUpdate(void)
1925 if (pDb
->Dbms() == dbmsMY_SQL
)
1928 if ((pDb
->Dbms() == dbmsORACLE
) ||
1929 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
1934 } // wxDbTable::CanSelectForUpdate()
1937 /********** wxDbTable::CanUpdByROWID() **********/
1938 bool wxDbTable::CanUpdByROWID(void)
1941 * NOTE: Returning FALSE for now until this can be debugged,
1942 * as the ROWID is not getting updated correctly
1946 if (pDb
->Dbms() == dbmsORACLE
)
1951 } // wxDbTable::CanUpdByROWID()
1954 /********** wxDbTable::IsCursorClosedOnCommit() **********/
1955 bool wxDbTable::IsCursorClosedOnCommit(void)
1957 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1962 } // wxDbTable::IsCursorClosedOnCommit()
1966 /********** wxDbTable::ClearMemberVar() **********/
1967 void wxDbTable::ClearMemberVar(int colNo
, bool setToNull
)
1969 wxASSERT(colNo
< noCols
);
1971 switch(colDefs
[colNo
].SqlCtype
)
1974 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
1977 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1980 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1983 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1986 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1989 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
1992 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
1994 case SQL_C_TIMESTAMP
:
1995 TIMESTAMP_STRUCT
*pDt
;
1996 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2009 } // wxDbTable::ClearMemberVar()
2012 /********** wxDbTable::ClearMemberVars() **********/
2013 void wxDbTable::ClearMemberVars(bool setToNull
)
2017 // Loop through the columns setting each member variable to zero
2018 for (i
=0; i
< noCols
; i
++)
2019 ClearMemberVar(i
,setToNull
);
2021 } // wxDbTable::ClearMemberVars()
2024 /********** wxDbTable::SetQueryTimeout() **********/
2025 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2027 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2028 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2029 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2030 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2031 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2032 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2033 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2034 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2036 // Completed Successfully
2039 } // wxDbTable::SetQueryTimeout()
2042 /********** wxDbTable::SetColDefs() **********/
2043 void wxDbTable::SetColDefs(int index
, const wxString
&fieldName
, int dataType
, void *pData
,
2044 int cType
, int size
, bool keyField
, bool upd
,
2045 bool insAllow
, bool derivedCol
)
2047 if (!colDefs
) // May happen if the database connection fails
2050 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2052 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2053 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2056 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2058 colDefs
[index
].DbDataType
= dataType
;
2059 colDefs
[index
].PtrDataObj
= pData
;
2060 colDefs
[index
].SqlCtype
= cType
;
2061 colDefs
[index
].SzDataObj
= size
;
2062 colDefs
[index
].KeyField
= keyField
;
2063 colDefs
[index
].DerivedCol
= derivedCol
;
2064 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2067 colDefs
[index
].Updateable
= FALSE
;
2068 colDefs
[index
].InsertAllowed
= FALSE
;
2072 colDefs
[index
].Updateable
= upd
;
2073 colDefs
[index
].InsertAllowed
= insAllow
;
2076 colDefs
[index
].Null
= FALSE
;
2078 } // wxDbTable::SetColDefs()
2081 /********** wxDbTable::SetColDefs() **********/
2082 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, ULONG numCols
)
2085 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2091 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2093 for (index
= 0; index
< numCols
; index
++)
2095 // Process the fields
2096 switch (pColInfs
[index
].dbDataType
)
2098 case DB_DATA_TYPE_VARCHAR
:
2099 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2100 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2101 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2103 case DB_DATA_TYPE_INTEGER
:
2104 // Can be long or short
2105 if (pColInfs
[index
].bufferLength
== sizeof(long))
2107 pColDataPtrs
[index
].PtrDataObj
= new long;
2108 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2109 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2113 pColDataPtrs
[index
].PtrDataObj
= new short;
2114 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2115 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2118 case DB_DATA_TYPE_FLOAT
:
2119 // Can be float or double
2120 if (pColInfs
[index
].bufferLength
== sizeof(float))
2122 pColDataPtrs
[index
].PtrDataObj
= new float;
2123 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2124 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2128 pColDataPtrs
[index
].PtrDataObj
= new double;
2129 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2130 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2133 case DB_DATA_TYPE_DATE
:
2134 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2135 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2136 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2138 case DB_DATA_TYPE_BLOB
:
2139 int notSupportedYet
= 0;
2140 wxASSERT_MSG(notSupportedYet
, wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2141 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2142 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2143 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2146 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2147 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2150 // Unable to build all the column definitions, as either one of
2151 // the calls to "new" failed above, or there was a BLOB field
2152 // to have a column definition for. If BLOBs are to be used,
2153 // the other form of ::SetColDefs() must be used, as it is impossible
2154 // to know the maximum size to create the PtrDataObj to be.
2155 delete [] pColDataPtrs
;
2161 return (pColDataPtrs
);
2163 } // wxDbTable::SetColDefs()
2166 /********** wxDbTable::SetCursor() **********/
2167 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2169 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2170 hstmt
= *hstmtDefault
;
2172 hstmt
= *hstmtActivate
;
2174 } // wxDbTable::SetCursor()
2177 /********** wxDbTable::Count(const wxString &) **********/
2178 ULONG
wxDbTable::Count(const wxString
&args
)
2184 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2185 sqlStmt
= wxT("SELECT COUNT(");
2187 sqlStmt
+= wxT(") FROM ");
2188 sqlStmt
+= queryTableName
;
2189 #if wxODBC_BACKWARD_COMPATABILITY
2190 if (from
&& wxStrlen(from
))
2196 // Add the where clause if one is provided
2197 #if wxODBC_BACKWARD_COMPATABILITY
2198 if (where
&& wxStrlen(where
))
2203 sqlStmt
+= wxT(" WHERE ");
2207 pDb
->WriteSqlLog(sqlStmt
);
2209 // Initialize the Count cursor if it's not already initialized
2212 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2213 wxASSERT(hstmtCount
);
2218 // Execute the SQL statement
2219 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2221 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2226 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2228 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2232 // Obtain the result
2233 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2235 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2240 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2241 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2243 // Return the record count
2246 } // wxDbTable::Count()
2249 /********** wxDbTable::Refresh() **********/
2250 bool wxDbTable::Refresh(void)
2254 // Switch to the internal cursor so any active cursors are not corrupted
2255 HSTMT currCursor
= GetCursor();
2256 hstmt
= hstmtInternal
;
2257 #if wxODBC_BACKWARD_COMPATABILITY
2258 // Save the where and order by clauses
2259 char *saveWhere
= where
;
2260 char *saveOrderBy
= orderBy
;
2262 wxString saveWhere
= where
;
2263 wxString saveOrderBy
= orderBy
;
2265 // Build a where clause to refetch the record with. Try and use the
2266 // ROWID if it's available, ow use the key fields.
2267 wxString whereClause
;
2268 whereClause
.Empty();
2270 if (CanUpdByROWID())
2273 wxChar rowid
[wxDB_ROWID_LEN
+1];
2275 // Get the ROWID value. If not successful retreiving the ROWID,
2276 // simply fall down through the code and build the WHERE clause
2277 // based on the key fields.
2278 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2280 whereClause
+= queryTableName
;
2281 whereClause
+= wxT(".ROWID = '");
2282 whereClause
+= rowid
;
2283 whereClause
+= wxT("'");
2287 // If unable to use the ROWID, build a where clause from the keyfields
2288 if (wxStrlen(whereClause
) == 0)
2289 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2291 // Requery the record
2292 where
= whereClause
;
2297 if (result
&& !GetNext())
2300 // Switch back to original cursor
2301 SetCursor(&currCursor
);
2303 // Free the internal cursor
2304 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2305 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2307 // Restore the original where and order by clauses
2309 orderBy
= saveOrderBy
;
2313 } // wxDbTable::Refresh()
2316 /********** wxDbTable::SetColNull(int colNo, bool set) **********/
2317 bool wxDbTable::SetColNull(int colNo
, bool set
)
2321 colDefs
[colNo
].Null
= set
;
2322 if (set
) // Blank out the values in the member variable
2323 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2329 } // wxDbTable::SetColNull()
2332 /********** wxDbTable::SetColNull(const wxString &colName, bool set) **********/
2333 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2336 for (i
= 0; i
< noCols
; i
++)
2338 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2344 colDefs
[i
].Null
= set
;
2345 if (set
) // Blank out the values in the member variable
2346 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2352 } // wxDbTable::SetColNull()
2355 /********** wxDbTable::GetNewCursor() **********/
2356 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2358 HSTMT
*newHSTMT
= new HSTMT
;
2363 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2365 pDb
->DispAllErrors(henv
, hdbc
);
2370 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2372 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2379 if(!bindCols(*newHSTMT
))
2387 SetCursor(newHSTMT
);
2391 } // wxDbTable::GetNewCursor()
2394 /********** wxDbTable::DeleteCursor() **********/
2395 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2399 if (!hstmtDel
) // Cursor already deleted
2403 ODBC 3.0 says to use this form
2404 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2407 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2409 pDb
->DispAllErrors(henv
, hdbc
);
2417 } // wxDbTable::DeleteCursor()
2419 #endif // wxUSE_ODBC