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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
29 #pragma implementation "dbtable.h"
32 #include "wx/wxprec.h"
38 #ifdef DBDEBUG_CONSOLE
44 #include "wx/ioswrap.h"
48 #include "wx/string.h"
49 #include "wx/object.h"
54 #include "wx/filefn.h"
62 #include "wx/dbtable.h"
65 // The HPUX preprocessor lines below were commented out on 8/20/97
66 // because macros.h currently redefines DEBUG and is unneeded.
68 // # include <macros.h>
71 # include <sys/minmax.h>
75 ULONG lastTableID
= 0;
83 void csstrncpyt(wxChar
*target
, const wxChar
*source
, int n
)
85 while ( (*target
++ = *source
++) != '\0' && --n
)
93 /********** wxDbColDef::wxDbColDef() Constructor **********/
94 wxDbColDef::wxDbColDef()
100 bool wxDbColDef::Initialize()
103 DbDataType
= DB_DATA_TYPE_INTEGER
;
104 SqlCtype
= SQL_C_LONG
;
109 InsertAllowed
= FALSE
;
115 } // wxDbColDef::Initialize()
118 /********** wxDbTable::wxDbTable() Constructor **********/
119 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
120 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
122 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
124 } // wxDbTable::wxDbTable()
127 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
128 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
129 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
131 wxString tempQryTblName
;
132 tempQryTblName
= qryTblName
;
133 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
135 } // wxDbTable::wxDbTable()
138 /********** wxDbTable::~wxDbTable() **********/
139 wxDbTable::~wxDbTable()
142 } // wxDbTable::~wxDbTable()
145 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
146 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
148 // Initializing member variables
149 pDb
= pwxDb
; // Pointer to the wxDb object
153 m_hstmtGridQuery
= 0;
154 hstmtDefault
= 0; // Initialized below
155 hstmtCount
= 0; // Initialized first time it is needed
162 noCols
= numColumns
; // Number of cols in the table
163 where
.Empty(); // Where clause
164 orderBy
.Empty(); // Order By clause
165 from
.Empty(); // From clause
166 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
171 queryTableName
.Empty();
173 wxASSERT(tblName
.Length());
179 tableName
= tblName
; // Table Name
180 if (tblPath
.Length())
181 tablePath
= tblPath
; // Table Path - used for dBase files
185 if (qryTblName
.Length()) // Name of the table/view to query
186 queryTableName
= qryTblName
;
188 queryTableName
= tblName
;
190 pDb
->incrementTableCount();
193 tableID
= ++lastTableID
;
194 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
197 wxTablesInUse
*tableInUse
;
198 tableInUse
= new wxTablesInUse();
199 tableInUse
->tableName
= tblName
;
200 tableInUse
->tableID
= tableID
;
201 tableInUse
->pDb
= pDb
;
202 TablesInUse
.Append(tableInUse
);
207 // Grab the HENV and HDBC from the wxDb object
208 henv
= pDb
->GetHENV();
209 hdbc
= pDb
->GetHDBC();
211 // Allocate space for column definitions
213 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
215 // Allocate statement handles for the table
218 // Allocate a separate statement handle for performing inserts
219 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
220 pDb
->DispAllErrors(henv
, hdbc
);
221 // Allocate a separate statement handle for performing deletes
222 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
223 pDb
->DispAllErrors(henv
, hdbc
);
224 // Allocate a separate statement handle for performing updates
225 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
226 pDb
->DispAllErrors(henv
, hdbc
);
228 // Allocate a separate statement handle for internal use
229 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
230 pDb
->DispAllErrors(henv
, hdbc
);
232 // Set the cursor type for the statement handles
233 cursorType
= SQL_CURSOR_STATIC
;
235 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
237 // Check to see if cursor type is supported
238 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
239 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
241 // Datasource does not support static cursors. Driver
242 // will substitute a cursor type. Call SQLGetStmtOption()
243 // to determine which cursor type was selected.
244 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
245 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
246 #ifdef DBDEBUG_CONSOLE
247 cout
<< wxT("Static cursor changed to: ");
250 case SQL_CURSOR_FORWARD_ONLY
:
251 cout
<< wxT("Forward Only");
253 case SQL_CURSOR_STATIC
:
254 cout
<< wxT("Static");
256 case SQL_CURSOR_KEYSET_DRIVEN
:
257 cout
<< wxT("Keyset Driven");
259 case SQL_CURSOR_DYNAMIC
:
260 cout
<< wxT("Dynamic");
263 cout
<< endl
<< endl
;
266 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
268 // Force the use of a forward only cursor...
269 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
270 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
272 // Should never happen
273 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
280 pDb
->DispNextError();
281 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
284 #ifdef DBDEBUG_CONSOLE
286 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
291 // Set the cursor type for the INSERT statement handle
292 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
293 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
294 // Set the cursor type for the DELETE statement handle
295 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
296 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
297 // Set the cursor type for the UPDATE statement handle
298 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
299 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
302 // Make the default cursor the active cursor
303 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
304 wxASSERT(hstmtDefault
);
305 hstmt
= *hstmtDefault
;
309 } // wxDbTable::initialize()
312 void wxDbTable::cleanup()
317 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
324 TablesInUse
.DeleteContents(TRUE
);
328 pNode
= TablesInUse
.GetFirst();
329 while (pNode
&& !found
)
331 if (((wxTablesInUse
*)pNode
->GetData())->tableID
== tableID
)
334 if (!TablesInUse
.DeleteNode(pNode
))
335 wxLogDebug (s
,wxT("Unable to delete node!"));
338 pNode
= pNode
->GetNext();
343 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
344 wxLogDebug (msg
,wxT("NOTICE..."));
349 // Decrement the wxDb table count
351 pDb
->decrementTableCount();
353 // Delete memory allocated for column definitions
357 // Free statement handles
363 ODBC 3.0 says to use this form
364 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
366 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
367 pDb
->DispAllErrors(henv
, hdbc
);
373 ODBC 3.0 says to use this form
374 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
376 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
377 pDb
->DispAllErrors(henv
, hdbc
);
383 ODBC 3.0 says to use this form
384 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
386 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
387 pDb
->DispAllErrors(henv
, hdbc
);
393 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
394 pDb
->DispAllErrors(henv
, hdbc
);
397 // Delete dynamically allocated cursors
399 DeleteCursor(hstmtDefault
);
402 DeleteCursor(hstmtCount
);
404 if (m_hstmtGridQuery
)
405 DeleteCursor(m_hstmtGridQuery
);
407 } // wxDbTable::cleanup()
410 /***************************** PRIVATE FUNCTIONS *****************************/
413 /********** wxDbTable::bindParams() **********/
414 bool wxDbTable::bindParams(bool forUpdate
)
416 wxASSERT(!queryOnly
);
421 SDWORD precision
= 0;
424 // Bind each column of the table that should be bound
425 // to a parameter marker
429 for (i
=0, colNo
=1; i
< noCols
; i
++)
433 if (!colDefs
[i
].Updateable
)
438 if (!colDefs
[i
].InsertAllowed
)
442 switch(colDefs
[i
].DbDataType
)
444 case DB_DATA_TYPE_VARCHAR
:
445 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
446 precision
= colDefs
[i
].SzDataObj
;
449 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
451 colDefs
[i
].CbValue
= SQL_NTS
;
453 case DB_DATA_TYPE_INTEGER
:
454 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
455 precision
= pDb
->GetTypeInfInteger().Precision
;
458 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
460 colDefs
[i
].CbValue
= 0;
462 case DB_DATA_TYPE_FLOAT
:
463 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
464 precision
= pDb
->GetTypeInfFloat().Precision
;
465 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
466 // SQL Sybase Anywhere v5.5 returned a negative number for the
467 // MaxScale. This caused ODBC to kick out an error on ibscale.
468 // I check for this here and set the scale = precision.
470 // scale = (short) precision;
472 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
474 colDefs
[i
].CbValue
= 0;
476 case DB_DATA_TYPE_DATE
:
477 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
478 precision
= pDb
->GetTypeInfDate().Precision
;
481 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
483 colDefs
[i
].CbValue
= 0;
485 case DB_DATA_TYPE_BLOB
:
486 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
490 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
492 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
497 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
498 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
499 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
501 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
506 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
507 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
508 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
510 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
515 // Completed successfully
518 } // wxDbTable::bindParams()
521 /********** wxDbTable::bindInsertParams() **********/
522 bool wxDbTable::bindInsertParams(void)
524 return bindParams(FALSE
);
525 } // wxDbTable::bindInsertParams()
528 /********** wxDbTable::bindUpdateParams() **********/
529 bool wxDbTable::bindUpdateParams(void)
531 return bindParams(TRUE
);
532 } // wxDbTable::bindUpdateParams()
535 /********** wxDbTable::bindCols() **********/
536 bool wxDbTable::bindCols(HSTMT cursor
)
540 // Bind each column of the table to a memory address for fetching data
542 for (i
= 0; i
< noCols
; i
++)
544 cb
= colDefs
[i
].CbValue
;
545 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
546 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
547 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
550 // Completed successfully
553 } // wxDbTable::bindCols()
556 /********** wxDbTable::getRec() **********/
557 bool wxDbTable::getRec(UWORD fetchType
)
561 if (!pDb
->FwdOnlyCursors())
563 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
567 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
568 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
570 if (retcode
== SQL_NO_DATA_FOUND
)
573 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
577 // Set the Null member variable to indicate the Null state
578 // of each column just read in.
580 for (i
= 0; i
< noCols
; i
++)
581 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
586 // Fetch the next record from the record set
587 retcode
= SQLFetch(hstmt
);
588 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
590 if (retcode
== SQL_NO_DATA_FOUND
)
593 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
597 // Set the Null member variable to indicate the Null state
598 // of each column just read in.
600 for (i
= 0; i
< noCols
; i
++)
601 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
605 // Completed successfully
608 } // wxDbTable::getRec()
611 /********** wxDbTable::execDelete() **********/
612 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
616 // Execute the DELETE statement
617 retcode
= SQLExecDirect(hstmtDelete
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
619 if (retcode
== SQL_SUCCESS
||
620 retcode
== SQL_NO_DATA_FOUND
||
621 retcode
== SQL_SUCCESS_WITH_INFO
)
623 // Record deleted successfully
627 // Problem deleting record
628 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
630 } // wxDbTable::execDelete()
633 /********** wxDbTable::execUpdate() **********/
634 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
638 // Execute the UPDATE statement
639 retcode
= SQLExecDirect(hstmtUpdate
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
641 if (retcode
== SQL_SUCCESS
||
642 retcode
== SQL_NO_DATA_FOUND
||
643 retcode
== SQL_SUCCESS_WITH_INFO
)
645 // Record updated successfully
648 else if (retcode
== SQL_NEED_DATA
)
651 retcode
= SQLParamData(hstmtUpdate
, &pParmID
);
652 while (retcode
== SQL_NEED_DATA
)
654 // Find the parameter
656 for (i
=0; i
< noCols
; i
++)
658 if (colDefs
[i
].PtrDataObj
== pParmID
)
660 // We found it. Store the parameter.
661 retcode
= SQLPutData(hstmtUpdate
, pParmID
, colDefs
[i
].SzDataObj
);
662 if (retcode
!= SQL_SUCCESS
)
664 pDb
->DispNextError();
665 return pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
671 if (retcode
== SQL_SUCCESS
||
672 retcode
== SQL_NO_DATA_FOUND
||
673 retcode
== SQL_SUCCESS_WITH_INFO
)
675 // Record updated successfully
680 // Problem updating record
681 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
683 } // wxDbTable::execUpdate()
686 /********** wxDbTable::query() **********/
687 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
692 // The user may wish to select for update, but the DBMS may not be capable
693 selectForUpdate
= CanSelectForUpdate();
695 selectForUpdate
= FALSE
;
697 // Set the SQL SELECT string
698 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
699 { // so generate a select statement.
700 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
701 pDb
->WriteSqlLog(sqlStmt
);
704 // Make sure the cursor is closed first
705 if (!CloseCursor(hstmt
))
708 // Execute the SQL SELECT statement
710 retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
711 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
712 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
714 // Completed successfully
717 } // wxDbTable::query()
720 /***************************** PUBLIC FUNCTIONS *****************************/
723 /********** wxDbTable::Open() **********/
724 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
734 // Calculate the maximum size of the concatenated
735 // keys for use with wxDbGrid
737 for (i
=0; i
< noCols
; i
++)
739 if (colDefs
[i
].KeyField
)
742 m_keysize
+= colDefs
[i
].SzDataObj
;
747 // Verify that the table exists in the database
748 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
750 s
= wxT("Table/view does not exist in the database");
751 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
752 s
+= wxT(", or you have no permissions.\n");
756 else if (checkPrivileges
)
758 // Verify the user has rights to access the table.
759 // Shortcut boolean evaluation to optimize out call to
762 // Unfortunately this optimization doesn't seem to be
764 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
765 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
766 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
773 if (!tablePath
.IsEmpty())
774 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
776 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
779 pDb
->LogError(p
.GetData());
784 // Bind the member variables for field exchange between
785 // the wxDbTable object and the ODBC record.
788 if (!bindInsertParams()) // Inserts
791 if (!bindUpdateParams()) // Updates
795 if (!bindCols(*hstmtDefault
)) // Selects
798 if (!bindCols(hstmtInternal
)) // Internal use only
802 * Do NOT bind the hstmtCount cursor!!!
805 // Build an insert statement using parameter markers
806 if (!queryOnly
&& noCols
> 0)
808 bool needComma
= FALSE
;
809 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
810 pDb
->SQLTableName(tableName
.c_str()).c_str());
811 for (i
= 0; i
< noCols
; i
++)
813 if (! colDefs
[i
].InsertAllowed
)
817 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
818 // sqlStmt += colDefs[i].ColName;
822 sqlStmt
+= wxT(") VALUES (");
824 int insertableCount
= 0;
826 for (i
= 0; i
< noCols
; i
++)
828 if (! colDefs
[i
].InsertAllowed
)
838 // Prepare the insert statement for execution
841 if (SQLPrepare(hstmtInsert
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
842 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
848 // Completed successfully
851 } // wxDbTable::Open()
854 /********** wxDbTable::Query() **********/
855 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
858 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
860 } // wxDbTable::Query()
863 /********** wxDbTable::QueryBySqlStmt() **********/
864 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
866 pDb
->WriteSqlLog(pSqlStmt
);
868 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
870 } // wxDbTable::QueryBySqlStmt()
873 /********** wxDbTable::QueryMatching() **********/
874 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
877 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
879 } // wxDbTable::QueryMatching()
882 /********** wxDbTable::QueryOnKeyFields() **********/
883 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
886 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
888 } // wxDbTable::QueryOnKeyFields()
891 /********** wxDbTable::GetPrev() **********/
892 bool wxDbTable::GetPrev(void)
894 if (pDb
->FwdOnlyCursors())
896 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
900 return(getRec(SQL_FETCH_PRIOR
));
902 } // wxDbTable::GetPrev()
905 /********** wxDbTable::operator-- **********/
906 bool wxDbTable::operator--(int)
908 if (pDb
->FwdOnlyCursors())
910 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
914 return(getRec(SQL_FETCH_PRIOR
));
916 } // wxDbTable::operator--
919 /********** wxDbTable::GetFirst() **********/
920 bool wxDbTable::GetFirst(void)
922 if (pDb
->FwdOnlyCursors())
924 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
928 return(getRec(SQL_FETCH_FIRST
));
930 } // wxDbTable::GetFirst()
933 /********** wxDbTable::GetLast() **********/
934 bool wxDbTable::GetLast(void)
936 if (pDb
->FwdOnlyCursors())
938 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
942 return(getRec(SQL_FETCH_LAST
));
944 } // wxDbTable::GetLast()
947 /********** wxDbTable::BuildDeleteStmt() **********/
948 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
950 wxASSERT(!queryOnly
);
954 wxString whereClause
;
958 // Handle the case of DeleteWhere() and the where clause is blank. It should
959 // delete all records from the database in this case.
960 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
962 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
963 pDb
->SQLTableName(tableName
.c_str()).c_str());
967 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
968 pDb
->SQLTableName(tableName
.c_str()).c_str());
970 // Append the WHERE clause to the SQL DELETE statement
973 case DB_DEL_KEYFIELDS
:
974 // If the datasource supports the ROWID column, build
975 // the where on ROWID for efficiency purposes.
976 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
980 wxChar rowid
[wxDB_ROWID_LEN
+1];
982 // Get the ROWID value. If not successful retreiving the ROWID,
983 // simply fall down through the code and build the WHERE clause
984 // based on the key fields.
985 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
987 pSqlStmt
+= wxT("ROWID = '");
989 pSqlStmt
+= wxT("'");
993 // Unable to delete by ROWID, so build a WHERE
994 // clause based on the keyfields.
995 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
996 pSqlStmt
+= whereClause
;
999 pSqlStmt
+= pWhereClause
;
1001 case DB_DEL_MATCHING
:
1002 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1003 pSqlStmt
+= whereClause
;
1007 } // BuildDeleteStmt()
1010 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
1011 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
1013 wxString tempSqlStmt
;
1014 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
1015 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1016 } // wxDbTable::BuildDeleteStmt()
1019 /********** wxDbTable::BuildSelectStmt() **********/
1020 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
1022 wxString whereClause
;
1023 whereClause
.Empty();
1025 // Build a select statement to query the database
1026 pSqlStmt
= wxT("SELECT ");
1028 // SELECT DISTINCT values only?
1030 pSqlStmt
+= wxT("DISTINCT ");
1032 // Was a FROM clause specified to join tables to the base table?
1033 // Available for ::Query() only!!!
1034 bool appendFromClause
= FALSE
;
1035 #if wxODBC_BACKWARD_COMPATABILITY
1036 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
1037 appendFromClause
= TRUE
;
1039 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
1040 appendFromClause
= TRUE
;
1043 // Add the column list
1046 for (i
= 0; i
< noCols
; i
++)
1048 tStr
= colDefs
[i
].ColName
;
1049 // If joining tables, the base table column names must be qualified to avoid ambiguity
1050 if ((appendFromClause
|| pDb
->Dbms() == dbmsACCESS
) && !tStr
.Find(wxT('.')))
1052 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1053 pSqlStmt
+= wxT(".");
1055 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1057 pSqlStmt
+= wxT(",");
1060 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1061 // the ROWID if querying distinct records. The rowid will always be unique.
1062 if (!distinct
&& CanUpdByROWID())
1064 // If joining tables, the base table column names must be qualified to avoid ambiguity
1065 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1067 pSqlStmt
+= wxT(",");
1068 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1069 // pSqlStmt += queryTableName;
1070 pSqlStmt
+= wxT(".ROWID");
1073 pSqlStmt
+= wxT(",ROWID");
1076 // Append the FROM tablename portion
1077 pSqlStmt
+= wxT(" FROM ");
1078 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1079 // pSqlStmt += queryTableName;
1081 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1082 // The HOLDLOCK keyword follows the table name in the from clause.
1083 // Each table in the from clause must specify HOLDLOCK or
1084 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1085 // is parsed but ignored in SYBASE Transact-SQL.
1086 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1087 pSqlStmt
+= wxT(" HOLDLOCK");
1089 if (appendFromClause
)
1092 // Append the WHERE clause. Either append the where clause for the class
1093 // or build a where clause. The typeOfSelect determines this.
1094 switch(typeOfSelect
)
1096 case DB_SELECT_WHERE
:
1097 #if wxODBC_BACKWARD_COMPATABILITY
1098 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1100 if (where
.Length()) // May not want a where clause!!!
1103 pSqlStmt
+= wxT(" WHERE ");
1107 case DB_SELECT_KEYFIELDS
:
1108 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1109 if (whereClause
.Length())
1111 pSqlStmt
+= wxT(" WHERE ");
1112 pSqlStmt
+= whereClause
;
1115 case DB_SELECT_MATCHING
:
1116 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1117 if (whereClause
.Length())
1119 pSqlStmt
+= wxT(" WHERE ");
1120 pSqlStmt
+= whereClause
;
1125 // Append the ORDER BY clause
1126 #if wxODBC_BACKWARD_COMPATABILITY
1127 if (orderBy
&& wxStrlen(orderBy
))
1129 if (orderBy
.Length())
1132 pSqlStmt
+= wxT(" ORDER BY ");
1133 pSqlStmt
+= orderBy
;
1136 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1137 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1138 // HOLDLOCK for Sybase.
1139 if (selectForUpdate
&& CanSelectForUpdate())
1140 pSqlStmt
+= wxT(" FOR UPDATE");
1142 } // wxDbTable::BuildSelectStmt()
1145 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1146 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1148 wxString tempSqlStmt
;
1149 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1150 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1151 } // wxDbTable::BuildSelectStmt()
1154 /********** wxDbTable::BuildUpdateStmt() **********/
1155 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1157 wxASSERT(!queryOnly
);
1161 wxString whereClause
;
1162 whereClause
.Empty();
1164 bool firstColumn
= TRUE
;
1166 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1167 pDb
->SQLTableName(tableName
.c_str()).c_str());
1169 // Append a list of columns to be updated
1171 for (i
= 0; i
< noCols
; i
++)
1173 // Only append Updateable columns
1174 if (colDefs
[i
].Updateable
)
1177 pSqlStmt
+= wxT(",");
1179 firstColumn
= FALSE
;
1181 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1182 // pSqlStmt += colDefs[i].ColName;
1183 pSqlStmt
+= wxT(" = ?");
1187 // Append the WHERE clause to the SQL UPDATE statement
1188 pSqlStmt
+= wxT(" WHERE ");
1191 case DB_UPD_KEYFIELDS
:
1192 // If the datasource supports the ROWID column, build
1193 // the where on ROWID for efficiency purposes.
1194 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1195 if (CanUpdByROWID())
1198 wxChar rowid
[wxDB_ROWID_LEN
+1];
1200 // Get the ROWID value. If not successful retreiving the ROWID,
1201 // simply fall down through the code and build the WHERE clause
1202 // based on the key fields.
1203 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1205 pSqlStmt
+= wxT("ROWID = '");
1207 pSqlStmt
+= wxT("'");
1211 // Unable to delete by ROWID, so build a WHERE
1212 // clause based on the keyfields.
1213 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1214 pSqlStmt
+= whereClause
;
1217 pSqlStmt
+= pWhereClause
;
1220 } // BuildUpdateStmt()
1223 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1224 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1226 wxString tempSqlStmt
;
1227 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1228 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1229 } // BuildUpdateStmt()
1232 /********** wxDbTable::BuildWhereClause() **********/
1233 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1234 const wxString
&qualTableName
, bool useLikeComparison
)
1236 * Note: BuildWhereClause() currently ignores timestamp columns.
1237 * They are not included as part of the where clause.
1240 bool moreThanOneColumn
= FALSE
;
1243 // Loop through the columns building a where clause as you go
1245 for (colNo
= 0; colNo
< noCols
; colNo
++)
1247 // Determine if this column should be included in the WHERE clause
1248 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[colNo
].KeyField
) ||
1249 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(colNo
))))
1251 // Skip over timestamp columns
1252 if (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
)
1254 // If there is more than 1 column, join them with the keyword "AND"
1255 if (moreThanOneColumn
)
1256 pWhereClause
+= wxT(" AND ");
1258 moreThanOneColumn
= TRUE
;
1260 // Concatenate where phrase for the column
1261 wxString tStr
= colDefs
[colNo
].ColName
;
1263 if (qualTableName
.Length() && !tStr
.Find(wxT('.')))
1265 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1266 pWhereClause
+= wxT(".");
1268 pWhereClause
+= pDb
->SQLColumnName(colDefs
[colNo
].ColName
);
1270 if (useLikeComparison
&& (colDefs
[colNo
].SqlCtype
== SQL_C_CHAR
))
1271 pWhereClause
+= wxT(" LIKE ");
1273 pWhereClause
+= wxT(" = ");
1275 switch(colDefs
[colNo
].SqlCtype
)
1278 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
);
1281 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[colNo
].PtrDataObj
));
1284 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[colNo
].PtrDataObj
));
1287 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[colNo
].PtrDataObj
));
1290 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[colNo
].PtrDataObj
));
1293 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
));
1296 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
));
1299 pWhereClause
+= colValue
;
1302 } // wxDbTable::BuildWhereClause()
1305 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1306 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1307 const wxString
&qualTableName
, bool useLikeComparison
)
1309 wxString tempSqlStmt
;
1310 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1311 wxStrcpy(pWhereClause
, tempSqlStmt
);
1312 } // wxDbTable::BuildWhereClause()
1315 /********** wxDbTable::GetRowNum() **********/
1316 UWORD
wxDbTable::GetRowNum(void)
1320 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1322 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1326 // Completed successfully
1327 return((UWORD
) rowNum
);
1329 } // wxDbTable::GetRowNum()
1332 /********** wxDbTable::CloseCursor() **********/
1333 bool wxDbTable::CloseCursor(HSTMT cursor
)
1335 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1336 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1338 // Completed successfully
1341 } // wxDbTable::CloseCursor()
1344 /********** wxDbTable::CreateTable() **********/
1345 bool wxDbTable::CreateTable(bool attemptDrop
)
1353 #ifdef DBDEBUG_CONSOLE
1354 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1358 if (attemptDrop
&& !DropTable())
1362 #ifdef DBDEBUG_CONSOLE
1363 for (i
= 0; i
< noCols
; i
++)
1365 // Exclude derived columns since they are NOT part of the base table
1366 if (colDefs
[i
].DerivedCol
)
1368 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1369 switch(colDefs
[i
].DbDataType
)
1371 case DB_DATA_TYPE_VARCHAR
:
1372 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1374 case DB_DATA_TYPE_INTEGER
:
1375 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1377 case DB_DATA_TYPE_FLOAT
:
1378 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1380 case DB_DATA_TYPE_DATE
:
1381 cout
<< pDb
->GetTypeInfDate().TypeName
;
1383 case DB_DATA_TYPE_BLOB
:
1384 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1391 // Build a CREATE TABLE string from the colDefs structure.
1392 bool needComma
= FALSE
;
1394 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1395 pDb
->SQLTableName(tableName
.c_str()).c_str());
1397 for (i
= 0; i
< noCols
; i
++)
1399 // Exclude derived columns since they are NOT part of the base table
1400 if (colDefs
[i
].DerivedCol
)
1404 sqlStmt
+= wxT(",");
1406 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1407 // sqlStmt += colDefs[i].ColName;
1408 sqlStmt
+= wxT(" ");
1410 switch(colDefs
[i
].DbDataType
)
1412 case DB_DATA_TYPE_VARCHAR
:
1413 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1415 case DB_DATA_TYPE_INTEGER
:
1416 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1418 case DB_DATA_TYPE_FLOAT
:
1419 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1421 case DB_DATA_TYPE_DATE
:
1422 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1424 case DB_DATA_TYPE_BLOB
:
1425 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1428 // For varchars, append the size of the string
1429 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
&&
1430 (pDb
->Dbms() != dbmsMY_SQL
|| pDb
->GetTypeInfVarchar().TypeName
!= _T("text")))// ||
1431 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1434 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1438 if (pDb
->Dbms() == dbmsDB2
||
1439 pDb
->Dbms() == dbmsMY_SQL
||
1440 pDb
->Dbms() == dbmsSYBASE_ASE
||
1441 pDb
->Dbms() == dbmsINTERBASE
||
1442 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1444 if (colDefs
[i
].KeyField
)
1446 sqlStmt
+= wxT(" NOT NULL");
1452 // If there is a primary key defined, include it in the create statement
1453 for (i
= j
= 0; i
< noCols
; i
++)
1455 if (colDefs
[i
].KeyField
)
1461 if ( j
&& (pDb
->Dbms() != dbmsDBASE
)
1462 && (pDb
->Dbms() != dbmsXBASE_SEQUITER
) ) // Found a keyfield
1464 switch (pDb
->Dbms())
1468 case dbmsSYBASE_ASA
:
1469 case dbmsSYBASE_ASE
:
1472 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1473 sqlStmt
+= wxT(",PRIMARY KEY (");
1478 sqlStmt
+= wxT(",CONSTRAINT ");
1479 // DB2 is limited to 18 characters for index names
1480 if (pDb
->Dbms() == dbmsDB2
)
1482 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."));
1483 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1484 // sqlStmt += tableName.substr(0, 13);
1487 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1488 // sqlStmt += tableName;
1490 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1495 // List column name(s) of column(s) comprising the primary key
1496 for (i
= j
= 0; i
< noCols
; i
++)
1498 if (colDefs
[i
].KeyField
)
1500 if (j
++) // Multi part key, comma separate names
1501 sqlStmt
+= wxT(",");
1502 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1504 if (pDb
->Dbms() == dbmsMY_SQL
&&
1505 colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1508 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1513 sqlStmt
+= wxT(")");
1515 if (pDb
->Dbms() == dbmsINFORMIX
||
1516 pDb
->Dbms() == dbmsSYBASE_ASA
||
1517 pDb
->Dbms() == dbmsSYBASE_ASE
)
1519 sqlStmt
+= wxT(" CONSTRAINT ");
1520 sqlStmt
+= pDb
->SQLTableName(tableName
);
1521 // sqlStmt += tableName;
1522 sqlStmt
+= wxT("_PIDX");
1525 // Append the closing parentheses for the create table statement
1526 sqlStmt
+= wxT(")");
1528 pDb
->WriteSqlLog(sqlStmt
);
1530 #ifdef DBDEBUG_CONSOLE
1531 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1534 // Execute the CREATE TABLE statement
1535 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1536 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1538 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1539 pDb
->RollbackTrans();
1544 // Commit the transaction and close the cursor
1545 if (!pDb
->CommitTrans())
1547 if (!CloseCursor(hstmt
))
1550 // Database table created successfully
1553 } // wxDbTable::CreateTable()
1556 /********** wxDbTable::DropTable() **********/
1557 bool wxDbTable::DropTable()
1559 // NOTE: This function returns TRUE if the Table does not exist, but
1560 // only for identified databases. Code will need to be added
1561 // below for any other databases when those databases are defined
1562 // to handle this situation consistently
1566 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1567 pDb
->SQLTableName(tableName
.c_str()).c_str());
1569 pDb
->WriteSqlLog(sqlStmt
);
1571 #ifdef DBDEBUG_CONSOLE
1572 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1575 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1576 if (retcode
!= SQL_SUCCESS
)
1578 // Check for "Base table not found" error and ignore
1579 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1580 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1581 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1583 // Check for product specific error codes
1584 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1585 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1586 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1587 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1589 pDb
->DispNextError();
1590 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1591 pDb
->RollbackTrans();
1592 // CloseCursor(hstmt);
1598 // Commit the transaction and close the cursor
1599 if (! pDb
->CommitTrans())
1601 if (! CloseCursor(hstmt
))
1605 } // wxDbTable::DropTable()
1608 /********** wxDbTable::CreateIndex() **********/
1609 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1610 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1614 // Drop the index first
1615 if (attemptDrop
&& !DropIndex(idxName
))
1618 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1619 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1620 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1621 // table was created, then months later you determine that an additional index while
1622 // give better performance, so you want to add an index).
1624 // The following block of code will modify the column definition to make the column be
1625 // defined with the "NOT NULL" qualifier.
1626 if (pDb
->Dbms() == dbmsMY_SQL
)
1631 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1635 // Find the column definition that has the ColName that matches the
1636 // index column name. We need to do this to get the DB_DATA_TYPE of
1637 // the index column, as MySQL's syntax for the ALTER column requires
1639 while (!found
&& (j
< this->noCols
))
1641 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1649 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1650 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1656 // retcode is not used
1657 wxODBC_ERRORS retcode
;
1658 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1659 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1660 // This line is just here for debug checking of the value
1661 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1672 pDb
->RollbackTrans();
1677 // Build a CREATE INDEX statement
1678 sqlStmt
= wxT("CREATE ");
1680 sqlStmt
+= wxT("UNIQUE ");
1682 sqlStmt
+= wxT("INDEX ");
1683 sqlStmt
+= pDb
->SQLTableName(idxName
);
1684 sqlStmt
+= wxT(" ON ");
1686 sqlStmt
+= pDb
->SQLTableName(tableName
);
1687 // sqlStmt += tableName;
1688 sqlStmt
+= wxT(" (");
1690 // Append list of columns making up index
1692 for (i
= 0; i
< noIdxCols
; i
++)
1694 sqlStmt
+= pDb
->SQLColumnName(pIdxDefs
[i
].ColName
);
1695 // sqlStmt += pIdxDefs[i].ColName;
1697 // MySQL requires a key length on VARCHAR keys
1698 if ( pDb
->Dbms() == dbmsMY_SQL
)
1700 // Find the details on this column
1702 for ( j
= 0; j
< noCols
; ++j
)
1704 if ( wxStrcmp( pIdxDefs
[i
].ColName
, colDefs
[j
].ColName
) == 0 )
1709 if ( colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1712 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1717 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1718 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (wxStrncmp(pDb
->dbInf
.dbmsVer
,_T("07"),2)==0)) &&
1719 !(pDb
->Dbms() == dbmsPOSTGRES
))
1721 if (pIdxDefs
[i
].Ascending
)
1722 sqlStmt
+= wxT(" ASC");
1724 sqlStmt
+= wxT(" DESC");
1727 wxASSERT_MSG(pIdxDefs
[i
].Ascending
, _T("Datasource does not support DESCending index columns"));
1729 if ((i
+ 1) < noIdxCols
)
1730 sqlStmt
+= wxT(",");
1733 // Append closing parentheses
1734 sqlStmt
+= wxT(")");
1736 pDb
->WriteSqlLog(sqlStmt
);
1738 #ifdef DBDEBUG_CONSOLE
1739 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1742 // Execute the CREATE INDEX statement
1743 if (SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1745 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1746 pDb
->RollbackTrans();
1751 // Commit the transaction and close the cursor
1752 if (! pDb
->CommitTrans())
1754 if (! CloseCursor(hstmt
))
1757 // Index Created Successfully
1760 } // wxDbTable::CreateIndex()
1763 /********** wxDbTable::DropIndex() **********/
1764 bool wxDbTable::DropIndex(const wxString
&idxName
)
1766 // NOTE: This function returns TRUE if the Index does not exist, but
1767 // only for identified databases. Code will need to be added
1768 // below for any other databases when those databases are defined
1769 // to handle this situation consistently
1773 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1774 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1775 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1776 pDb
->SQLTableName(idxName
.c_str()).c_str(),
1777 pDb
->SQLTableName(tableName
.c_str()).c_str());
1778 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1779 (pDb
->Dbms() == dbmsSYBASE_ASE
) ||
1780 (pDb
->Dbms() == dbmsXBASE_SEQUITER
))
1781 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1782 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1783 pDb
->SQLTableName(idxName
.c_str()).c_str());
1785 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1786 pDb
->SQLTableName(idxName
.c_str()).c_str());
1788 pDb
->WriteSqlLog(sqlStmt
);
1790 #ifdef DBDEBUG_CONSOLE
1791 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1794 if (SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1796 // Check for "Index not found" error and ignore
1797 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1798 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1800 // Check for product specific error codes
1801 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1802 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1803 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1804 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1805 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1806 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1807 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1810 pDb
->DispNextError();
1811 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1812 pDb
->RollbackTrans();
1819 // Commit the transaction and close the cursor
1820 if (! pDb
->CommitTrans())
1822 if (! CloseCursor(hstmt
))
1826 } // wxDbTable::DropIndex()
1829 /********** wxDbTable::SetOrderByColNums() **********/
1830 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1832 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1838 va_start(argptr
, first
); /* Initialize variable arguments. */
1839 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1841 // Make sure the passed in column number
1842 // is within the valid range of columns
1844 // Valid columns are 0 thru noCols-1
1845 if (colNo
>= noCols
|| colNo
< 0)
1852 tempStr
+= wxT(",");
1854 tempStr
+= colDefs
[colNo
].ColName
;
1855 colNo
= va_arg (argptr
, int);
1857 va_end (argptr
); /* Reset variable arguments. */
1859 SetOrderByClause(tempStr
);
1862 } // wxDbTable::SetOrderByColNums()
1865 /********** wxDbTable::Insert() **********/
1866 int wxDbTable::Insert(void)
1868 wxASSERT(!queryOnly
);
1869 if (queryOnly
|| !insertable
)
1874 // Insert the record by executing the already prepared insert statement
1876 retcode
=SQLExecute(hstmtInsert
);
1877 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
&&
1878 retcode
!= SQL_NEED_DATA
)
1880 // Check to see if integrity constraint was violated
1881 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1882 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1883 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1886 pDb
->DispNextError();
1887 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1891 if (retcode
== SQL_NEED_DATA
)
1894 retcode
= SQLParamData(hstmtInsert
, &pParmID
);
1895 while (retcode
== SQL_NEED_DATA
)
1897 // Find the parameter
1899 for (i
=0; i
< noCols
; i
++)
1901 if (colDefs
[i
].PtrDataObj
== pParmID
)
1903 // We found it. Store the parameter.
1904 retcode
= SQLPutData(hstmtInsert
, pParmID
, colDefs
[i
].SzDataObj
);
1905 if (retcode
!= SQL_SUCCESS
)
1907 pDb
->DispNextError();
1908 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1917 // Record inserted into the datasource successfully
1920 } // wxDbTable::Insert()
1923 /********** wxDbTable::Update() **********/
1924 bool wxDbTable::Update(void)
1926 wxASSERT(!queryOnly
);
1932 // Build the SQL UPDATE statement
1933 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1935 pDb
->WriteSqlLog(sqlStmt
);
1937 #ifdef DBDEBUG_CONSOLE
1938 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1941 // Execute the SQL UPDATE statement
1942 return(execUpdate(sqlStmt
));
1944 } // wxDbTable::Update()
1947 /********** wxDbTable::Update(pSqlStmt) **********/
1948 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1950 wxASSERT(!queryOnly
);
1954 pDb
->WriteSqlLog(pSqlStmt
);
1956 return(execUpdate(pSqlStmt
));
1958 } // wxDbTable::Update(pSqlStmt)
1961 /********** wxDbTable::UpdateWhere() **********/
1962 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1964 wxASSERT(!queryOnly
);
1970 // Build the SQL UPDATE statement
1971 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1973 pDb
->WriteSqlLog(sqlStmt
);
1975 #ifdef DBDEBUG_CONSOLE
1976 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1979 // Execute the SQL UPDATE statement
1980 return(execUpdate(sqlStmt
));
1982 } // wxDbTable::UpdateWhere()
1985 /********** wxDbTable::Delete() **********/
1986 bool wxDbTable::Delete(void)
1988 wxASSERT(!queryOnly
);
1995 // Build the SQL DELETE statement
1996 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1998 pDb
->WriteSqlLog(sqlStmt
);
2000 // Execute the SQL DELETE statement
2001 return(execDelete(sqlStmt
));
2003 } // wxDbTable::Delete()
2006 /********** wxDbTable::DeleteWhere() **********/
2007 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
2009 wxASSERT(!queryOnly
);
2016 // Build the SQL DELETE statement
2017 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
2019 pDb
->WriteSqlLog(sqlStmt
);
2021 // Execute the SQL DELETE statement
2022 return(execDelete(sqlStmt
));
2024 } // wxDbTable::DeleteWhere()
2027 /********** wxDbTable::DeleteMatching() **********/
2028 bool wxDbTable::DeleteMatching(void)
2030 wxASSERT(!queryOnly
);
2037 // Build the SQL DELETE statement
2038 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
2040 pDb
->WriteSqlLog(sqlStmt
);
2042 // Execute the SQL DELETE statement
2043 return(execDelete(sqlStmt
));
2045 } // wxDbTable::DeleteMatching()
2048 /********** wxDbTable::IsColNull() **********/
2049 bool wxDbTable::IsColNull(UWORD colNo
) const
2052 This logic is just not right. It would indicate TRUE
2053 if a numeric field were set to a value of 0.
2055 switch(colDefs[colNo].SqlCtype)
2058 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
2060 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
2062 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
2064 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
2066 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
2068 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
2070 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
2071 case SQL_C_TIMESTAMP:
2072 TIMESTAMP_STRUCT *pDt;
2073 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
2074 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
2082 return (colDefs
[colNo
].Null
);
2083 } // wxDbTable::IsColNull()
2086 /********** wxDbTable::CanSelectForUpdate() **********/
2087 bool wxDbTable::CanSelectForUpdate(void)
2092 if (pDb
->Dbms() == dbmsMY_SQL
)
2095 if ((pDb
->Dbms() == dbmsORACLE
) ||
2096 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
2101 } // wxDbTable::CanSelectForUpdate()
2104 /********** wxDbTable::CanUpdByROWID() **********/
2105 bool wxDbTable::CanUpdByROWID(void)
2108 * NOTE: Returning FALSE for now until this can be debugged,
2109 * as the ROWID is not getting updated correctly
2113 if (pDb->Dbms() == dbmsORACLE)
2118 } // wxDbTable::CanUpdByROWID()
2121 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2122 bool wxDbTable::IsCursorClosedOnCommit(void)
2124 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2129 } // wxDbTable::IsCursorClosedOnCommit()
2133 /********** wxDbTable::ClearMemberVar() **********/
2134 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2136 wxASSERT(colNo
< noCols
);
2138 switch(colDefs
[colNo
].SqlCtype
)
2141 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2144 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2147 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2150 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2153 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2156 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2159 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2161 case SQL_C_TIMESTAMP
:
2162 TIMESTAMP_STRUCT
*pDt
;
2163 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2176 } // wxDbTable::ClearMemberVar()
2179 /********** wxDbTable::ClearMemberVars() **********/
2180 void wxDbTable::ClearMemberVars(bool setToNull
)
2184 // Loop through the columns setting each member variable to zero
2185 for (i
=0; i
< noCols
; i
++)
2186 ClearMemberVar(i
,setToNull
);
2188 } // wxDbTable::ClearMemberVars()
2191 /********** wxDbTable::SetQueryTimeout() **********/
2192 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2194 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2195 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2196 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2197 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2198 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2199 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2200 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2201 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2203 // Completed Successfully
2206 } // wxDbTable::SetQueryTimeout()
2209 /********** wxDbTable::SetColDefs() **********/
2210 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2211 SWORD cType
, int size
, bool keyField
, bool upd
,
2212 bool insAllow
, bool derivedCol
)
2214 wxASSERT_MSG( index
< noCols
,
2215 _T("Specified column index exceeds the maximum number of columns for this table.") );
2217 if (!colDefs
) // May happen if the database connection fails
2220 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2222 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2223 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2227 tmpMsg
.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2228 fieldName
.c_str(),colDefs
[index
].ColName
);
2230 #endif // __WXDEBUG__
2233 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2235 colDefs
[index
].DbDataType
= dataType
;
2236 colDefs
[index
].PtrDataObj
= pData
;
2237 colDefs
[index
].SqlCtype
= cType
;
2238 colDefs
[index
].SzDataObj
= size
;
2239 colDefs
[index
].KeyField
= keyField
;
2240 colDefs
[index
].DerivedCol
= derivedCol
;
2241 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2244 colDefs
[index
].Updateable
= FALSE
;
2245 colDefs
[index
].InsertAllowed
= FALSE
;
2249 colDefs
[index
].Updateable
= upd
;
2250 colDefs
[index
].InsertAllowed
= insAllow
;
2253 colDefs
[index
].Null
= FALSE
;
2255 } // wxDbTable::SetColDefs()
2258 /********** wxDbTable::SetColDefs() **********/
2259 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2262 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2268 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2270 for (index
= 0; index
< numCols
; index
++)
2272 // Process the fields
2273 switch (pColInfs
[index
].dbDataType
)
2275 case DB_DATA_TYPE_VARCHAR
:
2276 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2277 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2278 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2280 case DB_DATA_TYPE_INTEGER
:
2281 // Can be long or short
2282 if (pColInfs
[index
].bufferLength
== sizeof(long))
2284 pColDataPtrs
[index
].PtrDataObj
= new long;
2285 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2286 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2290 pColDataPtrs
[index
].PtrDataObj
= new short;
2291 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2292 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2295 case DB_DATA_TYPE_FLOAT
:
2296 // Can be float or double
2297 if (pColInfs
[index
].bufferLength
== sizeof(float))
2299 pColDataPtrs
[index
].PtrDataObj
= new float;
2300 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2301 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2305 pColDataPtrs
[index
].PtrDataObj
= new double;
2306 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2307 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2310 case DB_DATA_TYPE_DATE
:
2311 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2312 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2313 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2315 case DB_DATA_TYPE_BLOB
:
2316 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2317 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2318 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2319 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2322 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2323 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2326 // Unable to build all the column definitions, as either one of
2327 // the calls to "new" failed above, or there was a BLOB field
2328 // to have a column definition for. If BLOBs are to be used,
2329 // the other form of ::SetColDefs() must be used, as it is impossible
2330 // to know the maximum size to create the PtrDataObj to be.
2331 delete [] pColDataPtrs
;
2337 return (pColDataPtrs
);
2339 } // wxDbTable::SetColDefs()
2342 /********** wxDbTable::SetCursor() **********/
2343 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2345 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2346 hstmt
= *hstmtDefault
;
2348 hstmt
= *hstmtActivate
;
2350 } // wxDbTable::SetCursor()
2353 /********** wxDbTable::Count(const wxString &) **********/
2354 ULONG
wxDbTable::Count(const wxString
&args
)
2360 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2361 sqlStmt
= wxT("SELECT COUNT(");
2363 sqlStmt
+= wxT(") FROM ");
2364 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2365 // sqlStmt += queryTableName;
2366 #if wxODBC_BACKWARD_COMPATABILITY
2367 if (from
&& wxStrlen(from
))
2373 // Add the where clause if one is provided
2374 #if wxODBC_BACKWARD_COMPATABILITY
2375 if (where
&& wxStrlen(where
))
2380 sqlStmt
+= wxT(" WHERE ");
2384 pDb
->WriteSqlLog(sqlStmt
);
2386 // Initialize the Count cursor if it's not already initialized
2389 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2390 wxASSERT(hstmtCount
);
2395 // Execute the SQL statement
2396 if (SQLExecDirect(*hstmtCount
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2398 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2403 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2405 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2409 // Obtain the result
2410 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2412 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2417 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2418 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2420 // Return the record count
2423 } // wxDbTable::Count()
2426 /********** wxDbTable::Refresh() **********/
2427 bool wxDbTable::Refresh(void)
2431 // Switch to the internal cursor so any active cursors are not corrupted
2432 HSTMT currCursor
= GetCursor();
2433 hstmt
= hstmtInternal
;
2434 #if wxODBC_BACKWARD_COMPATABILITY
2435 // Save the where and order by clauses
2436 wxChar
*saveWhere
= where
;
2437 wxChar
*saveOrderBy
= orderBy
;
2439 wxString saveWhere
= where
;
2440 wxString saveOrderBy
= orderBy
;
2442 // Build a where clause to refetch the record with. Try and use the
2443 // ROWID if it's available, ow use the key fields.
2444 wxString whereClause
;
2445 whereClause
.Empty();
2447 if (CanUpdByROWID())
2450 wxChar rowid
[wxDB_ROWID_LEN
+1];
2452 // Get the ROWID value. If not successful retreiving the ROWID,
2453 // simply fall down through the code and build the WHERE clause
2454 // based on the key fields.
2455 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2457 whereClause
+= pDb
->SQLTableName(queryTableName
);
2458 // whereClause += queryTableName;
2459 whereClause
+= wxT(".ROWID = '");
2460 whereClause
+= rowid
;
2461 whereClause
+= wxT("'");
2465 // If unable to use the ROWID, build a where clause from the keyfields
2466 if (wxStrlen(whereClause
) == 0)
2467 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2469 // Requery the record
2470 where
= whereClause
;
2475 if (result
&& !GetNext())
2478 // Switch back to original cursor
2479 SetCursor(&currCursor
);
2481 // Free the internal cursor
2482 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2483 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2485 // Restore the original where and order by clauses
2487 orderBy
= saveOrderBy
;
2491 } // wxDbTable::Refresh()
2494 /********** wxDbTable::SetColNull() **********/
2495 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2499 colDefs
[colNo
].Null
= set
;
2500 if (set
) // Blank out the values in the member variable
2502 colDefs
[colNo
].CbValue
= SQL_NULL_DATA
; // SF PATCH#766404
2503 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2510 } // wxDbTable::SetColNull()
2513 /********** wxDbTable::SetColNull() **********/
2514 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2517 for (colNo
= 0; colNo
< noCols
; colNo
++)
2519 if (!wxStricmp(colName
, colDefs
[colNo
].ColName
))
2525 colDefs
[colNo
].Null
= set
;
2526 if (set
) // Blank out the values in the member variable
2528 colDefs
[colNo
].CbValue
= SQL_NULL_DATA
; // SF PATCH#766404
2529 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2536 } // wxDbTable::SetColNull()
2539 /********** wxDbTable::GetNewCursor() **********/
2540 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2542 HSTMT
*newHSTMT
= new HSTMT
;
2547 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2549 pDb
->DispAllErrors(henv
, hdbc
);
2554 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2556 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2563 if (!bindCols(*newHSTMT
))
2571 SetCursor(newHSTMT
);
2575 } // wxDbTable::GetNewCursor()
2578 /********** wxDbTable::DeleteCursor() **********/
2579 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2583 if (!hstmtDel
) // Cursor already deleted
2587 ODBC 3.0 says to use this form
2588 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2591 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2593 pDb
->DispAllErrors(henv
, hdbc
);
2601 } // wxDbTable::DeleteCursor()
2603 //////////////////////////////////////////////////////////////
2604 // wxDbGrid support functions
2605 //////////////////////////////////////////////////////////////
2607 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2609 if (!m_hstmtGridQuery
)
2611 m_hstmtGridQuery
= GetNewCursor(FALSE
,FALSE
);
2612 if (!bindCols(*m_hstmtGridQuery
))
2616 m_rowmode
= rowmode
;
2619 case WX_ROW_MODE_QUERY
:
2620 SetCursor(m_hstmtGridQuery
);
2622 case WX_ROW_MODE_INDIVIDUAL
:
2623 SetCursor(hstmtDefault
);
2628 } // wxDbTable::SetRowMode()
2631 wxVariant
wxDbTable::GetCol(const int colNo
) const
2634 if ((colNo
< noCols
) && (!IsColNull(colNo
)))
2636 switch (colDefs
[colNo
].SqlCtype
)
2640 val
= (wxChar
*)(colDefs
[colNo
].PtrDataObj
);
2644 val
= *(long *)(colDefs
[colNo
].PtrDataObj
);
2648 val
= (long int )(*(short *)(colDefs
[colNo
].PtrDataObj
));
2651 val
= (long)(*(unsigned long *)(colDefs
[colNo
].PtrDataObj
));
2654 val
= (long)(*(wxChar
*)(colDefs
[colNo
].PtrDataObj
));
2656 case SQL_C_UTINYINT
:
2657 val
= (long)(*(wxChar
*)(colDefs
[colNo
].PtrDataObj
));
2660 val
= (long)(*(UWORD
*)(colDefs
[colNo
].PtrDataObj
));
2663 val
= (DATE_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2666 val
= (TIME_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2668 case SQL_C_TIMESTAMP
:
2669 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2672 val
= *(double *)(colDefs
[colNo
].PtrDataObj
);
2679 } // wxDbTable::GetCol()
2682 void wxDbTable::SetCol(const int colNo
, const wxVariant val
)
2684 //FIXME: Add proper wxDateTime support to wxVariant..
2687 SetColNull(colNo
, val
.IsNull());
2691 if ((colDefs
[colNo
].SqlCtype
== SQL_C_DATE
)
2692 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIME
)
2693 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
))
2695 //Returns null if invalid!
2696 if (!dateval
.ParseDate(val
.GetString()))
2697 SetColNull(colNo
, TRUE
);
2700 switch (colDefs
[colNo
].SqlCtype
)
2704 csstrncpyt((wxChar
*)(colDefs
[colNo
].PtrDataObj
),
2705 val
.GetString().c_str(),
2706 colDefs
[colNo
].SzDataObj
-1);
2710 *(long *)(colDefs
[colNo
].PtrDataObj
) = val
;
2714 *(short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2717 *(unsigned long *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2720 *(wxChar
*)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2722 case SQL_C_UTINYINT
:
2723 *(wxChar
*)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2726 *(unsigned short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2728 //FIXME: Add proper wxDateTime support to wxVariant..
2731 DATE_STRUCT
*dataptr
=
2732 (DATE_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2734 dataptr
->year
= dateval
.GetYear();
2735 dataptr
->month
= dateval
.GetMonth()+1;
2736 dataptr
->day
= dateval
.GetDay();
2741 TIME_STRUCT
*dataptr
=
2742 (TIME_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2744 dataptr
->hour
= dateval
.GetHour();
2745 dataptr
->minute
= dateval
.GetMinute();
2746 dataptr
->second
= dateval
.GetSecond();
2749 case SQL_C_TIMESTAMP
:
2751 TIMESTAMP_STRUCT
*dataptr
=
2752 (TIMESTAMP_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2753 dataptr
->year
= dateval
.GetYear();
2754 dataptr
->month
= dateval
.GetMonth()+1;
2755 dataptr
->day
= dateval
.GetDay();
2757 dataptr
->hour
= dateval
.GetHour();
2758 dataptr
->minute
= dateval
.GetMinute();
2759 dataptr
->second
= dateval
.GetSecond();
2763 *(double *)(colDefs
[colNo
].PtrDataObj
) = val
;
2768 } // if (!val.IsNull())
2769 } // wxDbTable::SetCol()
2772 GenericKey
wxDbTable::GetKey()
2777 blk
= malloc(m_keysize
);
2778 blkptr
= (wxChar
*) blk
;
2781 for (i
=0; i
< noCols
; i
++)
2783 if (colDefs
[i
].KeyField
)
2785 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2786 blkptr
+= colDefs
[i
].SzDataObj
;
2790 GenericKey k
= GenericKey(blk
, m_keysize
);
2794 } // wxDbTable::GetKey()
2797 void wxDbTable::SetKey(const GenericKey
& k
)
2803 blkptr
= (wxChar
*)blk
;
2806 for (i
=0; i
< noCols
; i
++)
2808 if (colDefs
[i
].KeyField
)
2810 SetColNull(i
, FALSE
);
2811 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2812 blkptr
+= colDefs
[i
].SzDataObj
;
2815 } // wxDbTable::SetKey()
2818 #endif // wxUSE_ODBC