1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence, plus:
12 // Notice: This class library and its intellectual design are free of charge for use,
13 // modification, enhancement, debugging under the following conditions:
14 // 1) These classes may only be used as part of the implementation of a
15 // wxWindows-based application
16 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17 // user groups free of all charges for use with the wxWindows library.
18 // 3) These classes may not be distributed as part of any other class library,
19 // DLL, text (written or electronic), other than a complete distribution of
20 // the wxWindows GUI development toolkit.
21 ///////////////////////////////////////////////////////////////////////////////
28 #pragma implementation "dbtable.h"
31 #include "wx/wxprec.h"
37 #ifdef DBDEBUG_CONSOLE
39 #include "wx/ioswrap.h"
43 #include "wx/string.h"
44 #include "wx/object.h"
48 #include "wx/msgdlg.h"
52 #include "wx/filefn.h"
61 #include "wx/dbtable.h"
64 // The HPUX preprocessor lines below were commented out on 8/20/97
65 // because macros.h currently redefines DEBUG and is unneeded.
67 // # include <macros.h>
70 # include <sys/minmax.h>
74 ULONG lastTableID
= 0;
82 /********** wxDbColDef::wxDbColDef() Constructor **********/
83 wxDbColDef::wxDbColDef()
89 bool wxDbColDef::Initialize()
92 DbDataType
= DB_DATA_TYPE_INTEGER
;
93 SqlCtype
= SQL_C_LONG
;
98 InsertAllowed
= FALSE
;
104 } // wxDbColDef::Initialize()
107 /********** wxDbTable::wxDbTable() Constructor **********/
108 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
109 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
111 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
113 } // wxDbTable::wxDbTable()
116 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
117 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
118 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
120 wxString tempQryTblName
;
121 tempQryTblName
= qryTblName
;
122 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
124 } // wxDbTable::wxDbTable()
127 /********** wxDbTable::~wxDbTable() **********/
128 wxDbTable::~wxDbTable()
131 } // wxDbTable::~wxDbTable()
134 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
135 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
137 // Initializing member variables
138 pDb
= pwxDb
; // Pointer to the wxDb object
142 m_hstmtGridQuery
= 0;
143 hstmtDefault
= 0; // Initialized below
144 hstmtCount
= 0; // Initialized first time it is needed
151 noCols
= numColumns
; // Number of cols in the table
152 where
.Empty(); // Where clause
153 orderBy
.Empty(); // Order By clause
154 from
.Empty(); // From clause
155 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
160 queryTableName
.Empty();
162 wxASSERT(tblName
.Length());
168 tableName
= tblName
; // Table Name
169 if (tblPath
.Length())
170 tablePath
= tblPath
; // Table Path - used for dBase files
174 if (qryTblName
.Length()) // Name of the table/view to query
175 queryTableName
= qryTblName
;
177 queryTableName
= tblName
;
179 pDb
->incrementTableCount();
182 tableID
= ++lastTableID
;
183 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
186 wxTablesInUse
*tableInUse
;
187 tableInUse
= new wxTablesInUse();
188 tableInUse
->tableName
= tblName
;
189 tableInUse
->tableID
= tableID
;
190 tableInUse
->pDb
= pDb
;
191 TablesInUse
.Append(tableInUse
);
196 // Grab the HENV and HDBC from the wxDb object
197 henv
= pDb
->GetHENV();
198 hdbc
= pDb
->GetHDBC();
200 // Allocate space for column definitions
202 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
204 // Allocate statement handles for the table
207 // Allocate a separate statement handle for performing inserts
208 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
209 pDb
->DispAllErrors(henv
, hdbc
);
210 // Allocate a separate statement handle for performing deletes
211 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
212 pDb
->DispAllErrors(henv
, hdbc
);
213 // Allocate a separate statement handle for performing updates
214 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
215 pDb
->DispAllErrors(henv
, hdbc
);
217 // Allocate a separate statement handle for internal use
218 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
219 pDb
->DispAllErrors(henv
, hdbc
);
221 // Set the cursor type for the statement handles
222 cursorType
= SQL_CURSOR_STATIC
;
224 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
226 // Check to see if cursor type is supported
227 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
228 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
230 // Datasource does not support static cursors. Driver
231 // will substitute a cursor type. Call SQLGetStmtOption()
232 // to determine which cursor type was selected.
233 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
234 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
235 #ifdef DBDEBUG_CONSOLE
236 cout
<< wxT("Static cursor changed to: ");
239 case SQL_CURSOR_FORWARD_ONLY
:
240 cout
<< wxT("Forward Only");
242 case SQL_CURSOR_STATIC
:
243 cout
<< wxT("Static");
245 case SQL_CURSOR_KEYSET_DRIVEN
:
246 cout
<< wxT("Keyset Driven");
248 case SQL_CURSOR_DYNAMIC
:
249 cout
<< wxT("Dynamic");
252 cout
<< endl
<< endl
;
255 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
257 // Force the use of a forward only cursor...
258 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
259 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
261 // Should never happen
262 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
269 pDb
->DispNextError();
270 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
273 #ifdef DBDEBUG_CONSOLE
275 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
280 // Set the cursor type for the INSERT statement handle
281 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
282 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
283 // Set the cursor type for the DELETE statement handle
284 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
285 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
286 // Set the cursor type for the UPDATE statement handle
287 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
288 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
291 // Make the default cursor the active cursor
292 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
293 wxASSERT(hstmtDefault
);
294 hstmt
= *hstmtDefault
;
298 } // wxDbTable::initialize()
301 void wxDbTable::cleanup()
306 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
313 TablesInUse
.DeleteContents(TRUE
);
317 pNode
= TablesInUse
.First();
318 while (pNode
&& !found
)
320 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
323 if (!TablesInUse
.DeleteNode(pNode
))
324 wxLogDebug (s
,wxT("Unable to delete node!"));
327 pNode
= pNode
->Next();
332 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
333 wxLogDebug (msg
,wxT("NOTICE..."));
338 // Decrement the wxDb table count
340 pDb
->decrementTableCount();
342 // Delete memory allocated for column definitions
346 // Free statement handles
352 ODBC 3.0 says to use this form
353 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
355 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
356 pDb
->DispAllErrors(henv
, hdbc
);
362 ODBC 3.0 says to use this form
363 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
365 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
366 pDb
->DispAllErrors(henv
, hdbc
);
372 ODBC 3.0 says to use this form
373 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
375 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
376 pDb
->DispAllErrors(henv
, hdbc
);
382 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
383 pDb
->DispAllErrors(henv
, hdbc
);
386 // Delete dynamically allocated cursors
388 DeleteCursor(hstmtDefault
);
391 DeleteCursor(hstmtCount
);
393 if (m_hstmtGridQuery
)
394 DeleteCursor(m_hstmtGridQuery
);
396 } // wxDbTable::cleanup()
399 /***************************** PRIVATE FUNCTIONS *****************************/
402 /********** wxDbTable::bindParams() **********/
403 bool wxDbTable::bindParams(bool forUpdate
)
405 wxASSERT(!queryOnly
);
410 UDWORD precision
= 0;
413 // Bind each column of the table that should be bound
414 // to a parameter marker
418 for (i
=0, colNo
=1; i
< noCols
; i
++)
422 if (!colDefs
[i
].Updateable
)
427 if (!colDefs
[i
].InsertAllowed
)
431 switch(colDefs
[i
].DbDataType
)
433 case DB_DATA_TYPE_VARCHAR
:
434 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
435 precision
= colDefs
[i
].SzDataObj
;
438 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
440 colDefs
[i
].CbValue
= SQL_NTS
;
442 case DB_DATA_TYPE_INTEGER
:
443 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
444 precision
= pDb
->GetTypeInfInteger().Precision
;
447 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
449 colDefs
[i
].CbValue
= 0;
451 case DB_DATA_TYPE_FLOAT
:
452 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
453 precision
= pDb
->GetTypeInfFloat().Precision
;
454 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
455 // SQL Sybase Anywhere v5.5 returned a negative number for the
456 // MaxScale. This caused ODBC to kick out an error on ibscale.
457 // I check for this here and set the scale = precision.
459 // scale = (short) precision;
461 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
463 colDefs
[i
].CbValue
= 0;
465 case DB_DATA_TYPE_DATE
:
466 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
467 precision
= pDb
->GetTypeInfDate().Precision
;
470 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
472 colDefs
[i
].CbValue
= 0;
474 case DB_DATA_TYPE_BLOB
:
475 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
479 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
481 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
486 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
487 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
488 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
490 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
495 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
496 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
497 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
499 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
504 // Completed successfully
507 } // wxDbTable::bindParams()
510 /********** wxDbTable::bindInsertParams() **********/
511 bool wxDbTable::bindInsertParams(void)
513 return bindParams(FALSE
);
514 } // wxDbTable::bindInsertParams()
517 /********** wxDbTable::bindUpdateParams() **********/
518 bool wxDbTable::bindUpdateParams(void)
520 return bindParams(TRUE
);
521 } // wxDbTable::bindUpdateParams()
524 /********** wxDbTable::bindCols() **********/
525 bool wxDbTable::bindCols(HSTMT cursor
)
527 // Bind each column of the table to a memory address for fetching data
529 for (i
= 0; i
< noCols
; i
++)
531 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
532 colDefs
[i
].SzDataObj
, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
534 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
538 // Completed successfully
541 } // wxDbTable::bindCols()
544 /********** wxDbTable::getRec() **********/
545 bool wxDbTable::getRec(UWORD fetchType
)
549 if (!pDb
->FwdOnlyCursors())
551 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
555 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
556 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
558 if (retcode
== SQL_NO_DATA_FOUND
)
561 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
565 // Set the Null member variable to indicate the Null state
566 // of each column just read in.
568 for (i
= 0; i
< noCols
; i
++)
569 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
574 // Fetch the next record from the record set
575 retcode
= SQLFetch(hstmt
);
576 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
578 if (retcode
== SQL_NO_DATA_FOUND
)
581 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
585 // Set the Null member variable to indicate the Null state
586 // of each column just read in.
588 for (i
= 0; i
< noCols
; i
++)
589 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
593 // Completed successfully
596 } // wxDbTable::getRec()
599 /********** wxDbTable::execDelete() **********/
600 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
604 // Execute the DELETE statement
605 retcode
= SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
607 if (retcode
== SQL_SUCCESS
||
608 retcode
== SQL_NO_DATA_FOUND
||
609 retcode
== SQL_SUCCESS_WITH_INFO
)
611 // Record deleted successfully
615 // Problem deleting record
616 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
618 } // wxDbTable::execDelete()
621 /********** wxDbTable::execUpdate() **********/
622 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
626 // Execute the UPDATE statement
627 retcode
= SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
629 if (retcode
== SQL_SUCCESS
||
630 retcode
== SQL_NO_DATA_FOUND
||
631 retcode
== SQL_SUCCESS_WITH_INFO
)
633 // Record updated successfully
637 // Problem updating record
638 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
640 } // wxDbTable::execUpdate()
643 /********** wxDbTable::query() **********/
644 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
649 // The user may wish to select for update, but the DBMS may not be capable
650 selectForUpdate
= CanSelectForUpdate();
652 selectForUpdate
= FALSE
;
654 // Set the SQL SELECT string
655 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
656 { // so generate a select statement.
657 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
658 pDb
->WriteSqlLog(sqlStmt
);
661 // Make sure the cursor is closed first
662 if (!CloseCursor(hstmt
))
665 // Execute the SQL SELECT statement
667 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
668 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
669 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
671 // Completed successfully
674 } // wxDbTable::query()
677 /***************************** PUBLIC FUNCTIONS *****************************/
680 /********** wxDbTable::Open() **********/
681 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
691 // Calculate the maximum size of the concatenated
692 // keys for use with wxDbGrid
694 for (i
=0; i
< noCols
; i
++)
696 if (colDefs
[i
].KeyField
)
699 m_keysize
+= colDefs
[i
].SzDataObj
;
704 // Verify that the table exists in the database
705 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
707 s
= wxT("Table/view does not exist in the database");
708 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
709 s
+= wxT(", or you have no permissions.\n");
713 else if (checkPrivileges
)
715 // Verify the user has rights to access the table.
716 // Shortcut boolean evaluation to optimize out call to
719 // Unfortunately this optimization doesn't seem to be
721 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
722 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
723 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
730 if (!tablePath
.IsEmpty())
731 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
733 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
736 pDb
->LogError(p
.GetData());
741 // Bind the member variables for field exchange between
742 // the wxDbTable object and the ODBC record.
745 if (!bindInsertParams()) // Inserts
748 if (!bindUpdateParams()) // Updates
752 if (!bindCols(*hstmtDefault
)) // Selects
755 if (!bindCols(hstmtInternal
)) // Internal use only
759 * Do NOT bind the hstmtCount cursor!!!
762 // Build an insert statement using parameter markers
763 if (!queryOnly
&& noCols
> 0)
765 bool needComma
= FALSE
;
766 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
767 pDb
->SQLTableName(tableName
.c_str()).c_str());
768 for (i
= 0; i
< noCols
; i
++)
770 if (! colDefs
[i
].InsertAllowed
)
774 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
775 // sqlStmt += colDefs[i].ColName;
779 sqlStmt
+= wxT(") VALUES (");
781 int insertableCount
= 0;
783 for (i
= 0; i
< noCols
; i
++)
785 if (! colDefs
[i
].InsertAllowed
)
795 // Prepare the insert statement for execution
798 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
799 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
805 // Completed successfully
808 } // wxDbTable::Open()
811 /********** wxDbTable::Query() **********/
812 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
815 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
817 } // wxDbTable::Query()
820 /********** wxDbTable::QueryBySqlStmt() **********/
821 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
823 pDb
->WriteSqlLog(pSqlStmt
);
825 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
827 } // wxDbTable::QueryBySqlStmt()
830 /********** wxDbTable::QueryMatching() **********/
831 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
834 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
836 } // wxDbTable::QueryMatching()
839 /********** wxDbTable::QueryOnKeyFields() **********/
840 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
843 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
845 } // wxDbTable::QueryOnKeyFields()
848 /********** wxDbTable::GetPrev() **********/
849 bool wxDbTable::GetPrev(void)
851 if (pDb
->FwdOnlyCursors())
853 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
857 return(getRec(SQL_FETCH_PRIOR
));
859 } // wxDbTable::GetPrev()
862 /********** wxDbTable::operator-- **********/
863 bool wxDbTable::operator--(int)
865 if (pDb
->FwdOnlyCursors())
867 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
871 return(getRec(SQL_FETCH_PRIOR
));
873 } // wxDbTable::operator--
876 /********** wxDbTable::GetFirst() **********/
877 bool wxDbTable::GetFirst(void)
879 if (pDb
->FwdOnlyCursors())
881 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
885 return(getRec(SQL_FETCH_FIRST
));
887 } // wxDbTable::GetFirst()
890 /********** wxDbTable::GetLast() **********/
891 bool wxDbTable::GetLast(void)
893 if (pDb
->FwdOnlyCursors())
895 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
899 return(getRec(SQL_FETCH_LAST
));
901 } // wxDbTable::GetLast()
904 /********** wxDbTable::BuildDeleteStmt() **********/
905 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
907 wxASSERT(!queryOnly
);
911 wxString whereClause
;
915 // Handle the case of DeleteWhere() and the where clause is blank. It should
916 // delete all records from the database in this case.
917 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
919 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
920 pDb
->SQLTableName(tableName
.c_str()).c_str());
924 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
925 pDb
->SQLTableName(tableName
.c_str()).c_str());
927 // Append the WHERE clause to the SQL DELETE statement
930 case DB_DEL_KEYFIELDS
:
931 // If the datasource supports the ROWID column, build
932 // the where on ROWID for efficiency purposes.
933 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
937 wxChar rowid
[wxDB_ROWID_LEN
+1];
939 // Get the ROWID value. If not successful retreiving the ROWID,
940 // simply fall down through the code and build the WHERE clause
941 // based on the key fields.
942 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
944 pSqlStmt
+= wxT("ROWID = '");
946 pSqlStmt
+= wxT("'");
950 // Unable to delete by ROWID, so build a WHERE
951 // clause based on the keyfields.
952 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
953 pSqlStmt
+= whereClause
;
956 pSqlStmt
+= pWhereClause
;
958 case DB_DEL_MATCHING
:
959 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
960 pSqlStmt
+= whereClause
;
964 } // BuildDeleteStmt()
967 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
968 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
970 wxString tempSqlStmt
;
971 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
972 wxStrcpy(pSqlStmt
, tempSqlStmt
);
973 } // wxDbTable::BuildDeleteStmt()
976 /********** wxDbTable::BuildSelectStmt() **********/
977 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
979 wxString whereClause
;
982 // Build a select statement to query the database
983 pSqlStmt
= wxT("SELECT ");
985 // SELECT DISTINCT values only?
987 pSqlStmt
+= wxT("DISTINCT ");
989 // Was a FROM clause specified to join tables to the base table?
990 // Available for ::Query() only!!!
991 bool appendFromClause
= FALSE
;
992 #if wxODBC_BACKWARD_COMPATABILITY
993 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
994 appendFromClause
= TRUE
;
996 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
997 appendFromClause
= TRUE
;
1000 // Add the column list
1002 for (i
= 0; i
< noCols
; i
++)
1004 // If joining tables, the base table column names must be qualified to avoid ambiguity
1005 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1007 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1008 // pSqlStmt += queryTableName;
1009 pSqlStmt
+= wxT(".");
1011 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1012 // pSqlStmt += colDefs[i].ColName;
1014 pSqlStmt
+= wxT(",");
1017 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1018 // the ROWID if querying distinct records. The rowid will always be unique.
1019 if (!distinct
&& CanUpdByROWID())
1021 // If joining tables, the base table column names must be qualified to avoid ambiguity
1022 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1024 pSqlStmt
+= wxT(",");
1025 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1026 // pSqlStmt += queryTableName;
1027 pSqlStmt
+= wxT(".ROWID");
1030 pSqlStmt
+= wxT(",ROWID");
1033 // Append the FROM tablename portion
1034 pSqlStmt
+= wxT(" FROM ");
1035 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1036 // pSqlStmt += queryTableName;
1038 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1039 // The HOLDLOCK keyword follows the table name in the from clause.
1040 // Each table in the from clause must specify HOLDLOCK or
1041 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1042 // is parsed but ignored in SYBASE Transact-SQL.
1043 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1044 pSqlStmt
+= wxT(" HOLDLOCK");
1046 if (appendFromClause
)
1049 // Append the WHERE clause. Either append the where clause for the class
1050 // or build a where clause. The typeOfSelect determines this.
1051 switch(typeOfSelect
)
1053 case DB_SELECT_WHERE
:
1054 #if wxODBC_BACKWARD_COMPATABILITY
1055 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1057 if (where
.Length()) // May not want a where clause!!!
1060 pSqlStmt
+= wxT(" WHERE ");
1064 case DB_SELECT_KEYFIELDS
:
1065 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1066 if (whereClause
.Length())
1068 pSqlStmt
+= wxT(" WHERE ");
1069 pSqlStmt
+= whereClause
;
1072 case DB_SELECT_MATCHING
:
1073 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1074 if (whereClause
.Length())
1076 pSqlStmt
+= wxT(" WHERE ");
1077 pSqlStmt
+= whereClause
;
1082 // Append the ORDER BY clause
1083 #if wxODBC_BACKWARD_COMPATABILITY
1084 if (orderBy
&& wxStrlen(orderBy
))
1086 if (orderBy
.Length())
1089 pSqlStmt
+= wxT(" ORDER BY ");
1090 pSqlStmt
+= orderBy
;
1093 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1094 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1095 // HOLDLOCK for Sybase.
1096 if (selectForUpdate
&& CanSelectForUpdate())
1097 pSqlStmt
+= wxT(" FOR UPDATE");
1099 } // wxDbTable::BuildSelectStmt()
1102 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1103 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1105 wxString tempSqlStmt
;
1106 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1107 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1108 } // wxDbTable::BuildSelectStmt()
1111 /********** wxDbTable::BuildUpdateStmt() **********/
1112 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1114 wxASSERT(!queryOnly
);
1118 wxString whereClause
;
1119 whereClause
.Empty();
1121 bool firstColumn
= TRUE
;
1123 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1124 pDb
->SQLTableName(tableName
.c_str()).c_str());
1126 // Append a list of columns to be updated
1128 for (i
= 0; i
< noCols
; i
++)
1130 // Only append Updateable columns
1131 if (colDefs
[i
].Updateable
)
1134 pSqlStmt
+= wxT(",");
1136 firstColumn
= FALSE
;
1138 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1139 // pSqlStmt += colDefs[i].ColName;
1140 pSqlStmt
+= wxT(" = ?");
1144 // Append the WHERE clause to the SQL UPDATE statement
1145 pSqlStmt
+= wxT(" WHERE ");
1148 case DB_UPD_KEYFIELDS
:
1149 // If the datasource supports the ROWID column, build
1150 // the where on ROWID for efficiency purposes.
1151 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1152 if (CanUpdByROWID())
1155 wxChar rowid
[wxDB_ROWID_LEN
+1];
1157 // Get the ROWID value. If not successful retreiving the ROWID,
1158 // simply fall down through the code and build the WHERE clause
1159 // based on the key fields.
1160 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1162 pSqlStmt
+= wxT("ROWID = '");
1164 pSqlStmt
+= wxT("'");
1168 // Unable to delete by ROWID, so build a WHERE
1169 // clause based on the keyfields.
1170 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1171 pSqlStmt
+= whereClause
;
1174 pSqlStmt
+= pWhereClause
;
1177 } // BuildUpdateStmt()
1180 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1181 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1183 wxString tempSqlStmt
;
1184 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1185 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1186 } // BuildUpdateStmt()
1189 /********** wxDbTable::BuildWhereClause() **********/
1190 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1191 const wxString
&qualTableName
, bool useLikeComparison
)
1193 * Note: BuildWhereClause() currently ignores timestamp columns.
1194 * They are not included as part of the where clause.
1197 bool moreThanOneColumn
= FALSE
;
1200 // Loop through the columns building a where clause as you go
1202 for (i
= 0; i
< noCols
; i
++)
1204 // Determine if this column should be included in the WHERE clause
1205 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1206 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1208 // Skip over timestamp columns
1209 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1211 // If there is more than 1 column, join them with the keyword "AND"
1212 if (moreThanOneColumn
)
1213 pWhereClause
+= wxT(" AND ");
1215 moreThanOneColumn
= TRUE
;
1216 // Concatenate where phrase for the column
1217 if (qualTableName
.Length())
1219 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1220 // pWhereClause += qualTableName;
1221 pWhereClause
+= wxT(".");
1223 pWhereClause
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1224 // pWhereClause += colDefs[i].ColName;
1225 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1226 pWhereClause
+= wxT(" LIKE ");
1228 pWhereClause
+= wxT(" = ");
1229 switch(colDefs
[i
].SqlCtype
)
1232 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1235 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[i
].PtrDataObj
));
1238 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[i
].PtrDataObj
));
1241 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1244 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1247 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1250 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1253 pWhereClause
+= colValue
;
1256 } // wxDbTable::BuildWhereClause()
1259 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1260 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1261 const wxString
&qualTableName
, bool useLikeComparison
)
1263 wxString tempSqlStmt
;
1264 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1265 wxStrcpy(pWhereClause
, tempSqlStmt
);
1266 } // wxDbTable::BuildWhereClause()
1269 /********** wxDbTable::GetRowNum() **********/
1270 UWORD
wxDbTable::GetRowNum(void)
1274 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1276 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1280 // Completed successfully
1281 return((UWORD
) rowNum
);
1283 } // wxDbTable::GetRowNum()
1286 /********** wxDbTable::CloseCursor() **********/
1287 bool wxDbTable::CloseCursor(HSTMT cursor
)
1289 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1290 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1292 // Completed successfully
1295 } // wxDbTable::CloseCursor()
1298 /********** wxDbTable::CreateTable() **********/
1299 bool wxDbTable::CreateTable(bool attemptDrop
)
1307 #ifdef DBDEBUG_CONSOLE
1308 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1312 if (attemptDrop
&& !DropTable())
1316 #ifdef DBDEBUG_CONSOLE
1317 for (i
= 0; i
< noCols
; i
++)
1319 // Exclude derived columns since they are NOT part of the base table
1320 if (colDefs
[i
].DerivedCol
)
1322 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1323 switch(colDefs
[i
].DbDataType
)
1325 case DB_DATA_TYPE_VARCHAR
:
1326 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1328 case DB_DATA_TYPE_INTEGER
:
1329 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1331 case DB_DATA_TYPE_FLOAT
:
1332 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1334 case DB_DATA_TYPE_DATE
:
1335 cout
<< pDb
->GetTypeInfDate().TypeName
;
1337 case DB_DATA_TYPE_BLOB
:
1338 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1345 // Build a CREATE TABLE string from the colDefs structure.
1346 bool needComma
= FALSE
;
1348 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1349 pDb
->SQLTableName(tableName
.c_str()).c_str());
1351 for (i
= 0; i
< noCols
; i
++)
1353 // Exclude derived columns since they are NOT part of the base table
1354 if (colDefs
[i
].DerivedCol
)
1358 sqlStmt
+= wxT(",");
1360 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1361 // sqlStmt += colDefs[i].ColName;
1362 sqlStmt
+= wxT(" ");
1364 switch(colDefs
[i
].DbDataType
)
1366 case DB_DATA_TYPE_VARCHAR
:
1367 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1369 case DB_DATA_TYPE_INTEGER
:
1370 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1372 case DB_DATA_TYPE_FLOAT
:
1373 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1375 case DB_DATA_TYPE_DATE
:
1376 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1378 case DB_DATA_TYPE_BLOB
:
1379 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1382 // For varchars, append the size of the string
1383 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)// ||
1384 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1387 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1391 if (pDb
->Dbms() == dbmsDB2
||
1392 pDb
->Dbms() == dbmsMY_SQL
||
1393 pDb
->Dbms() == dbmsSYBASE_ASE
||
1394 pDb
->Dbms() == dbmsINTERBASE
||
1395 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1397 if (colDefs
[i
].KeyField
)
1399 sqlStmt
+= wxT(" NOT NULL");
1405 // If there is a primary key defined, include it in the create statement
1406 for (i
= j
= 0; i
< noCols
; i
++)
1408 if (colDefs
[i
].KeyField
)
1414 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1416 switch (pDb
->Dbms())
1420 case dbmsSYBASE_ASA
:
1421 case dbmsSYBASE_ASE
:
1424 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1425 sqlStmt
+= wxT(",PRIMARY KEY (");
1430 sqlStmt
+= wxT(",CONSTRAINT ");
1431 // DB2 is limited to 18 characters for index names
1432 if (pDb
->Dbms() == dbmsDB2
)
1434 wxASSERT_MSG((tableName
&& wxStrlen(tableName
) <= 13), wxT("DB2 table/index names must be no longer than 13 characters in length.\n\nTruncating table name to 13 characters."));
1435 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1436 // sqlStmt += tableName.substr(0, 13);
1439 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1440 // sqlStmt += tableName;
1442 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1447 // List column name(s) of column(s) comprising the primary key
1448 for (i
= j
= 0; i
< noCols
; i
++)
1450 if (colDefs
[i
].KeyField
)
1452 if (j
++) // Multi part key, comma separate names
1453 sqlStmt
+= wxT(",");
1454 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1455 // sqlStmt += colDefs[i].ColName;
1458 sqlStmt
+= wxT(")");
1460 if (pDb
->Dbms() == dbmsINFORMIX
||
1461 pDb
->Dbms() == dbmsSYBASE_ASA
||
1462 pDb
->Dbms() == dbmsSYBASE_ASE
)
1464 sqlStmt
+= wxT(" CONSTRAINT ");
1465 sqlStmt
+= pDb
->SQLTableName(tableName
);
1466 // sqlStmt += tableName;
1467 sqlStmt
+= wxT("_PIDX");
1470 // Append the closing parentheses for the create table statement
1471 sqlStmt
+= wxT(")");
1473 pDb
->WriteSqlLog(sqlStmt
);
1475 #ifdef DBDEBUG_CONSOLE
1476 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1479 // Execute the CREATE TABLE statement
1480 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1481 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1483 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1484 pDb
->RollbackTrans();
1489 // Commit the transaction and close the cursor
1490 if (!pDb
->CommitTrans())
1492 if (!CloseCursor(hstmt
))
1495 // Database table created successfully
1498 } // wxDbTable::CreateTable()
1501 /********** wxDbTable::DropTable() **********/
1502 bool wxDbTable::DropTable()
1504 // NOTE: This function returns TRUE if the Table does not exist, but
1505 // only for identified databases. Code will need to be added
1506 // below for any other databases when those databases are defined
1507 // to handle this situation consistently
1511 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1512 pDb
->SQLTableName(tableName
.c_str()).c_str());
1514 pDb
->WriteSqlLog(sqlStmt
);
1516 #ifdef DBDEBUG_CONSOLE
1517 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1520 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1521 if (retcode
!= SQL_SUCCESS
)
1523 // Check for "Base table not found" error and ignore
1524 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1525 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1526 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1528 // Check for product specific error codes
1529 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1530 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1531 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1532 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1534 pDb
->DispNextError();
1535 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1536 pDb
->RollbackTrans();
1537 // CloseCursor(hstmt);
1543 // Commit the transaction and close the cursor
1544 if (! pDb
->CommitTrans())
1546 if (! CloseCursor(hstmt
))
1550 } // wxDbTable::DropTable()
1553 /********** wxDbTable::CreateIndex() **********/
1554 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1555 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1559 // Drop the index first
1560 if (attemptDrop
&& !DropIndex(idxName
))
1563 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1564 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1565 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1566 // table was created, then months later you determine that an additional index while
1567 // give better performance, so you want to add an index).
1569 // The following block of code will modify the column definition to make the column be
1570 // defined with the "NOT NULL" qualifier.
1571 if (pDb
->Dbms() == dbmsMY_SQL
)
1576 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1580 // Find the column definition that has the ColName that matches the
1581 // index column name. We need to do this to get the DB_DATA_TYPE of
1582 // the index column, as MySQL's syntax for the ALTER column requires
1584 while (!found
&& (j
< this->noCols
))
1586 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1594 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1595 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1600 wxODBC_ERRORS retcode
;
1601 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1602 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1603 // This line is just here for debug checking of the value
1604 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1614 pDb
->RollbackTrans();
1619 // Build a CREATE INDEX statement
1620 sqlStmt
= wxT("CREATE ");
1622 sqlStmt
+= wxT("UNIQUE ");
1624 sqlStmt
+= wxT("INDEX ");
1625 sqlStmt
+= pDb
->SQLTableName(idxName
);
1626 sqlStmt
+= wxT(" ON ");
1628 sqlStmt
+= pDb
->SQLTableName(tableName
);
1629 // sqlStmt += tableName;
1630 sqlStmt
+= wxT(" (");
1632 // Append list of columns making up index
1634 for (i
= 0; i
< noIdxCols
; i
++)
1636 sqlStmt
+= pDb
->SQLColumnName(pIdxDefs
[i
].ColName
);
1637 // sqlStmt += pIdxDefs[i].ColName;
1639 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1640 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (strncmp(pDb
->dbInf
.dbmsVer
,"07",2)==0)) &&
1641 !(pDb
->Dbms() == dbmsPOSTGRES
))
1643 if (pIdxDefs
[i
].Ascending
)
1644 sqlStmt
+= wxT(" ASC");
1646 sqlStmt
+= wxT(" DESC");
1649 wxASSERT_MSG(pIdxDefs
[i
].Ascending
, "Datasource does not support DESCending index columns");
1651 if ((i
+ 1) < noIdxCols
)
1652 sqlStmt
+= wxT(",");
1655 // Append closing parentheses
1656 sqlStmt
+= wxT(")");
1658 pDb
->WriteSqlLog(sqlStmt
);
1660 #ifdef DBDEBUG_CONSOLE
1661 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1664 // Execute the CREATE INDEX statement
1665 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1667 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1668 pDb
->RollbackTrans();
1673 // Commit the transaction and close the cursor
1674 if (! pDb
->CommitTrans())
1676 if (! CloseCursor(hstmt
))
1679 // Index Created Successfully
1682 } // wxDbTable::CreateIndex()
1685 /********** wxDbTable::DropIndex() **********/
1686 bool wxDbTable::DropIndex(const wxString
&idxName
)
1688 // NOTE: This function returns TRUE if the Index does not exist, but
1689 // only for identified databases. Code will need to be added
1690 // below for any other databases when those databases are defined
1691 // to handle this situation consistently
1695 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1696 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1697 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1698 pDb
->SQLTableName(idxName
.c_str()).c_str(),
1699 pDb
->SQLTableName(tableName
.c_str()).c_str());
1700 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1701 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1702 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1703 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1704 pDb
->SQLTableName(idxName
.c_str()).c_str());
1706 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1707 pDb
->SQLTableName(idxName
.c_str()).c_str());
1709 pDb
->WriteSqlLog(sqlStmt
);
1711 #ifdef DBDEBUG_CONSOLE
1712 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1715 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1717 // Check for "Index not found" error and ignore
1718 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1719 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1721 // Check for product specific error codes
1722 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1723 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1724 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1725 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1726 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1727 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1728 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1731 pDb
->DispNextError();
1732 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1733 pDb
->RollbackTrans();
1740 // Commit the transaction and close the cursor
1741 if (! pDb
->CommitTrans())
1743 if (! CloseCursor(hstmt
))
1747 } // wxDbTable::DropIndex()
1750 /********** wxDbTable::SetOrderByColNums() **********/
1751 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1753 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1759 va_start(argptr
, first
); /* Initialize variable arguments. */
1760 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1762 // Make sure the passed in column number
1763 // is within the valid range of columns
1765 // Valid columns are 0 thru noCols-1
1766 if (colNo
>= noCols
|| colNo
< 0)
1773 tempStr
+= wxT(",");
1775 tempStr
+= colDefs
[colNo
].ColName
;
1776 colNo
= va_arg (argptr
, int);
1778 va_end (argptr
); /* Reset variable arguments. */
1780 SetOrderByClause(tempStr
);
1783 } // wxDbTable::SetOrderByColNums()
1786 /********** wxDbTable::Insert() **********/
1787 int wxDbTable::Insert(void)
1789 wxASSERT(!queryOnly
);
1790 if (queryOnly
|| !insertable
)
1795 // Insert the record by executing the already prepared insert statement
1797 retcode
=SQLExecute(hstmtInsert
);
1798 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1800 // Check to see if integrity constraint was violated
1801 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1802 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1803 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1806 pDb
->DispNextError();
1807 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1812 // Record inserted into the datasource successfully
1815 } // wxDbTable::Insert()
1818 /********** wxDbTable::Update() **********/
1819 bool wxDbTable::Update(void)
1821 wxASSERT(!queryOnly
);
1827 // Build the SQL UPDATE statement
1828 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1830 pDb
->WriteSqlLog(sqlStmt
);
1832 #ifdef DBDEBUG_CONSOLE
1833 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1836 // Execute the SQL UPDATE statement
1837 return(execUpdate(sqlStmt
));
1839 } // wxDbTable::Update()
1842 /********** wxDbTable::Update(pSqlStmt) **********/
1843 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1845 wxASSERT(!queryOnly
);
1849 pDb
->WriteSqlLog(pSqlStmt
);
1851 return(execUpdate(pSqlStmt
));
1853 } // wxDbTable::Update(pSqlStmt)
1856 /********** wxDbTable::UpdateWhere() **********/
1857 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1859 wxASSERT(!queryOnly
);
1865 // Build the SQL UPDATE statement
1866 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1868 pDb
->WriteSqlLog(sqlStmt
);
1870 #ifdef DBDEBUG_CONSOLE
1871 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1874 // Execute the SQL UPDATE statement
1875 return(execUpdate(sqlStmt
));
1877 } // wxDbTable::UpdateWhere()
1880 /********** wxDbTable::Delete() **********/
1881 bool wxDbTable::Delete(void)
1883 wxASSERT(!queryOnly
);
1890 // Build the SQL DELETE statement
1891 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1893 pDb
->WriteSqlLog(sqlStmt
);
1895 // Execute the SQL DELETE statement
1896 return(execDelete(sqlStmt
));
1898 } // wxDbTable::Delete()
1901 /********** wxDbTable::DeleteWhere() **********/
1902 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1904 wxASSERT(!queryOnly
);
1911 // Build the SQL DELETE statement
1912 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1914 pDb
->WriteSqlLog(sqlStmt
);
1916 // Execute the SQL DELETE statement
1917 return(execDelete(sqlStmt
));
1919 } // wxDbTable::DeleteWhere()
1922 /********** wxDbTable::DeleteMatching() **********/
1923 bool wxDbTable::DeleteMatching(void)
1925 wxASSERT(!queryOnly
);
1932 // Build the SQL DELETE statement
1933 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1935 pDb
->WriteSqlLog(sqlStmt
);
1937 // Execute the SQL DELETE statement
1938 return(execDelete(sqlStmt
));
1940 } // wxDbTable::DeleteMatching()
1943 /********** wxDbTable::IsColNull() **********/
1944 bool wxDbTable::IsColNull(UWORD colNo
) const
1947 This logic is just not right. It would indicate TRUE
1948 if a numeric field were set to a value of 0.
1950 switch(colDefs[colNo].SqlCtype)
1953 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1955 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1957 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1959 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1961 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1963 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1965 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1966 case SQL_C_TIMESTAMP:
1967 TIMESTAMP_STRUCT *pDt;
1968 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1969 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1977 return (colDefs
[colNo
].Null
);
1978 } // wxDbTable::IsColNull()
1981 /********** wxDbTable::CanSelectForUpdate() **********/
1982 bool wxDbTable::CanSelectForUpdate(void)
1987 if (pDb
->Dbms() == dbmsMY_SQL
)
1990 if ((pDb
->Dbms() == dbmsORACLE
) ||
1991 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
1996 } // wxDbTable::CanSelectForUpdate()
1999 /********** wxDbTable::CanUpdByROWID() **********/
2000 bool wxDbTable::CanUpdByROWID(void)
2003 * NOTE: Returning FALSE for now until this can be debugged,
2004 * as the ROWID is not getting updated correctly
2008 if (pDb->Dbms() == dbmsORACLE)
2013 } // wxDbTable::CanUpdByROWID()
2016 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2017 bool wxDbTable::IsCursorClosedOnCommit(void)
2019 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2024 } // wxDbTable::IsCursorClosedOnCommit()
2028 /********** wxDbTable::ClearMemberVar() **********/
2029 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2031 wxASSERT(colNo
< noCols
);
2033 switch(colDefs
[colNo
].SqlCtype
)
2036 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2039 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2042 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2045 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2048 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2051 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2054 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2056 case SQL_C_TIMESTAMP
:
2057 TIMESTAMP_STRUCT
*pDt
;
2058 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2071 } // wxDbTable::ClearMemberVar()
2074 /********** wxDbTable::ClearMemberVars() **********/
2075 void wxDbTable::ClearMemberVars(bool setToNull
)
2079 // Loop through the columns setting each member variable to zero
2080 for (i
=0; i
< noCols
; i
++)
2081 ClearMemberVar(i
,setToNull
);
2083 } // wxDbTable::ClearMemberVars()
2086 /********** wxDbTable::SetQueryTimeout() **********/
2087 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2089 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2090 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2091 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2092 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2093 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2094 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2095 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2096 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2098 // Completed Successfully
2101 } // wxDbTable::SetQueryTimeout()
2104 /********** wxDbTable::SetColDefs() **********/
2105 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2106 SWORD cType
, int size
, bool keyField
, bool upd
,
2107 bool insAllow
, bool derivedCol
)
2109 if (!colDefs
) // May happen if the database connection fails
2112 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2114 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2115 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2119 tmpMsg
.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2120 fieldName
.c_str(),colDefs
[index
].ColName
);
2122 #endif // __WXDEBUG__
2125 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2127 colDefs
[index
].DbDataType
= dataType
;
2128 colDefs
[index
].PtrDataObj
= pData
;
2129 colDefs
[index
].SqlCtype
= cType
;
2130 colDefs
[index
].SzDataObj
= size
;
2131 colDefs
[index
].KeyField
= keyField
;
2132 colDefs
[index
].DerivedCol
= derivedCol
;
2133 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2136 colDefs
[index
].Updateable
= FALSE
;
2137 colDefs
[index
].InsertAllowed
= FALSE
;
2141 colDefs
[index
].Updateable
= upd
;
2142 colDefs
[index
].InsertAllowed
= insAllow
;
2145 colDefs
[index
].Null
= FALSE
;
2147 } // wxDbTable::SetColDefs()
2150 /********** wxDbTable::SetColDefs() **********/
2151 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2154 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2160 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2162 for (index
= 0; index
< numCols
; index
++)
2164 // Process the fields
2165 switch (pColInfs
[index
].dbDataType
)
2167 case DB_DATA_TYPE_VARCHAR
:
2168 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2169 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2170 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2172 case DB_DATA_TYPE_INTEGER
:
2173 // Can be long or short
2174 if (pColInfs
[index
].bufferLength
== sizeof(long))
2176 pColDataPtrs
[index
].PtrDataObj
= new long;
2177 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2178 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2182 pColDataPtrs
[index
].PtrDataObj
= new short;
2183 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2184 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2187 case DB_DATA_TYPE_FLOAT
:
2188 // Can be float or double
2189 if (pColInfs
[index
].bufferLength
== sizeof(float))
2191 pColDataPtrs
[index
].PtrDataObj
= new float;
2192 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2193 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2197 pColDataPtrs
[index
].PtrDataObj
= new double;
2198 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2199 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2202 case DB_DATA_TYPE_DATE
:
2203 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2204 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2205 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2207 case DB_DATA_TYPE_BLOB
:
2208 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2209 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2210 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2211 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2214 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2215 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2218 // Unable to build all the column definitions, as either one of
2219 // the calls to "new" failed above, or there was a BLOB field
2220 // to have a column definition for. If BLOBs are to be used,
2221 // the other form of ::SetColDefs() must be used, as it is impossible
2222 // to know the maximum size to create the PtrDataObj to be.
2223 delete [] pColDataPtrs
;
2229 return (pColDataPtrs
);
2231 } // wxDbTable::SetColDefs()
2234 /********** wxDbTable::SetCursor() **********/
2235 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2237 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2238 hstmt
= *hstmtDefault
;
2240 hstmt
= *hstmtActivate
;
2242 } // wxDbTable::SetCursor()
2245 /********** wxDbTable::Count(const wxString &) **********/
2246 ULONG
wxDbTable::Count(const wxString
&args
)
2252 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2253 sqlStmt
= wxT("SELECT COUNT(");
2255 sqlStmt
+= wxT(") FROM ");
2256 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2257 // sqlStmt += queryTableName;
2258 #if wxODBC_BACKWARD_COMPATABILITY
2259 if (from
&& wxStrlen(from
))
2265 // Add the where clause if one is provided
2266 #if wxODBC_BACKWARD_COMPATABILITY
2267 if (where
&& wxStrlen(where
))
2272 sqlStmt
+= wxT(" WHERE ");
2276 pDb
->WriteSqlLog(sqlStmt
);
2278 // Initialize the Count cursor if it's not already initialized
2281 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2282 wxASSERT(hstmtCount
);
2287 // Execute the SQL statement
2288 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2290 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2295 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2297 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2301 // Obtain the result
2302 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2304 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2309 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2310 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2312 // Return the record count
2315 } // wxDbTable::Count()
2318 /********** wxDbTable::Refresh() **********/
2319 bool wxDbTable::Refresh(void)
2323 // Switch to the internal cursor so any active cursors are not corrupted
2324 HSTMT currCursor
= GetCursor();
2325 hstmt
= hstmtInternal
;
2326 #if wxODBC_BACKWARD_COMPATABILITY
2327 // Save the where and order by clauses
2328 char *saveWhere
= where
;
2329 char *saveOrderBy
= orderBy
;
2331 wxString saveWhere
= where
;
2332 wxString saveOrderBy
= orderBy
;
2334 // Build a where clause to refetch the record with. Try and use the
2335 // ROWID if it's available, ow use the key fields.
2336 wxString whereClause
;
2337 whereClause
.Empty();
2339 if (CanUpdByROWID())
2342 wxChar rowid
[wxDB_ROWID_LEN
+1];
2344 // Get the ROWID value. If not successful retreiving the ROWID,
2345 // simply fall down through the code and build the WHERE clause
2346 // based on the key fields.
2347 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2349 whereClause
+= pDb
->SQLTableName(queryTableName
);
2350 // whereClause += queryTableName;
2351 whereClause
+= wxT(".ROWID = '");
2352 whereClause
+= rowid
;
2353 whereClause
+= wxT("'");
2357 // If unable to use the ROWID, build a where clause from the keyfields
2358 if (wxStrlen(whereClause
) == 0)
2359 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2361 // Requery the record
2362 where
= whereClause
;
2367 if (result
&& !GetNext())
2370 // Switch back to original cursor
2371 SetCursor(&currCursor
);
2373 // Free the internal cursor
2374 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2375 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2377 // Restore the original where and order by clauses
2379 orderBy
= saveOrderBy
;
2383 } // wxDbTable::Refresh()
2386 /********** wxDbTable::SetColNull() **********/
2387 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2391 colDefs
[colNo
].Null
= set
;
2392 if (set
) // Blank out the values in the member variable
2393 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2399 } // wxDbTable::SetColNull()
2402 /********** wxDbTable::SetColNull() **********/
2403 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2406 for (i
= 0; i
< noCols
; i
++)
2408 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2414 colDefs
[i
].Null
= set
;
2415 if (set
) // Blank out the values in the member variable
2416 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2422 } // wxDbTable::SetColNull()
2425 /********** wxDbTable::GetNewCursor() **********/
2426 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2428 HSTMT
*newHSTMT
= new HSTMT
;
2433 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2435 pDb
->DispAllErrors(henv
, hdbc
);
2440 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2442 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2449 if (!bindCols(*newHSTMT
))
2457 SetCursor(newHSTMT
);
2461 } // wxDbTable::GetNewCursor()
2464 /********** wxDbTable::DeleteCursor() **********/
2465 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2469 if (!hstmtDel
) // Cursor already deleted
2473 ODBC 3.0 says to use this form
2474 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2477 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2479 pDb
->DispAllErrors(henv
, hdbc
);
2487 } // wxDbTable::DeleteCursor()
2489 //////////////////////////////////////////////////////////////
2490 // wxDbGrid support functions
2491 //////////////////////////////////////////////////////////////
2493 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2495 if (!m_hstmtGridQuery
)
2497 m_hstmtGridQuery
= GetNewCursor(FALSE
,FALSE
);
2498 if (!bindCols(*m_hstmtGridQuery
))
2502 m_rowmode
= rowmode
;
2505 case WX_ROW_MODE_QUERY
:
2506 SetCursor(m_hstmtGridQuery
);
2508 case WX_ROW_MODE_INDIVIDUAL
:
2509 SetCursor(hstmtDefault
);
2514 } // wxDbTable::SetRowMode()
2517 wxVariant
wxDbTable::GetCol(const int colNo
) const
2520 if ((colNo
< noCols
) && (!IsColNull(colNo
)))
2522 switch (colDefs
[colNo
].SqlCtype
)
2526 val
= (wxChar
*)(colDefs
[colNo
].PtrDataObj
);
2530 val
= *(long *)(colDefs
[colNo
].PtrDataObj
);
2534 val
= (long int )(*(short *)(colDefs
[colNo
].PtrDataObj
));
2537 val
= (long)(*(unsigned long *)(colDefs
[colNo
].PtrDataObj
));
2540 val
= (long)(*(char *)(colDefs
[colNo
].PtrDataObj
));
2542 case SQL_C_UTINYINT
:
2543 val
= (long)(*(unsigned char *)(colDefs
[colNo
].PtrDataObj
));
2546 val
= (long)(*(UWORD
*)(colDefs
[colNo
].PtrDataObj
));
2549 val
= (DATE_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2552 val
= (TIME_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2554 case SQL_C_TIMESTAMP
:
2555 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2558 val
= *(double *)(colDefs
[colNo
].PtrDataObj
);
2565 } // wxDbTable::GetCol()
2568 void csstrncpyt(char *s
, const char *t
, int n
)
2570 while ((*s
++ = *t
++) && --n
)
2576 void wxDbTable::SetCol(const int colNo
, const wxVariant val
)
2578 //FIXME: Add proper wxDateTime support to wxVariant..
2581 SetColNull(colNo
, val
.IsNull());
2585 if ((colDefs
[colNo
].SqlCtype
== SQL_C_DATE
)
2586 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIME
)
2587 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
))
2589 //Returns null if invalid!
2590 if (!dateval
.ParseDate(val
.GetString()))
2591 SetColNull(colNo
, TRUE
);
2594 switch (colDefs
[colNo
].SqlCtype
)
2598 csstrncpyt((char *)(colDefs
[colNo
].PtrDataObj
),
2599 val
.GetString().c_str(),
2600 colDefs
[colNo
].SzDataObj
-1);
2604 *(long *)(colDefs
[colNo
].PtrDataObj
) = val
;
2608 *(short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2611 *(unsigned long *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2614 *(char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2616 case SQL_C_UTINYINT
:
2617 *(unsigned char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2620 *(unsigned short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2622 //FIXME: Add proper wxDateTime support to wxVariant..
2625 DATE_STRUCT
*dataptr
=
2626 (DATE_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2628 dataptr
->year
= dateval
.GetYear();
2629 dataptr
->month
= dateval
.GetMonth()+1;
2630 dataptr
->day
= dateval
.GetDay();
2635 TIME_STRUCT
*dataptr
=
2636 (TIME_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2638 dataptr
->hour
= dateval
.GetHour();
2639 dataptr
->minute
= dateval
.GetMinute();
2640 dataptr
->second
= dateval
.GetSecond();
2643 case SQL_C_TIMESTAMP
:
2645 TIMESTAMP_STRUCT
*dataptr
=
2646 (TIMESTAMP_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2647 dataptr
->year
= dateval
.GetYear();
2648 dataptr
->month
= dateval
.GetMonth()+1;
2649 dataptr
->day
= dateval
.GetDay();
2651 dataptr
->hour
= dateval
.GetHour();
2652 dataptr
->minute
= dateval
.GetMinute();
2653 dataptr
->second
= dateval
.GetSecond();
2657 *(double *)(colDefs
[colNo
].PtrDataObj
) = val
;
2662 } // if (!val.IsNull())
2663 } // wxDbTable::SetCol()
2666 GenericKey
wxDbTable::GetKey()
2671 blk
= malloc(m_keysize
);
2672 blkptr
= (wxChar
*) blk
;
2675 for (i
=0; i
< noCols
; i
++)
2677 if (colDefs
[i
].KeyField
)
2679 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2680 blkptr
+= colDefs
[i
].SzDataObj
;
2684 GenericKey k
= GenericKey(blk
, m_keysize
);
2688 } // wxDbTable::GetKey()
2691 void wxDbTable::SetKey(const GenericKey
& k
)
2697 blkptr
= (wxChar
*)blk
;
2700 for (i
=0; i
< noCols
; i
++)
2702 if (colDefs
[i
].KeyField
)
2704 SetColNull(i
, FALSE
);
2705 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2706 blkptr
+= colDefs
[i
].SzDataObj
;
2709 } // wxDbTable::SetKey()
2712 #endif // wxUSE_ODBC