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
43 #include "wx/ioswrap.h"
47 #include "wx/string.h"
48 #include "wx/object.h"
53 #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 /********** wxDbColDef::wxDbColDef() Constructor **********/
84 wxDbColDef::wxDbColDef()
90 bool wxDbColDef::Initialize()
93 DbDataType
= DB_DATA_TYPE_INTEGER
;
94 SqlCtype
= SQL_C_LONG
;
99 InsertAllowed
= FALSE
;
105 } // wxDbColDef::Initialize()
108 /********** wxDbTable::wxDbTable() Constructor **********/
109 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
110 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
112 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
114 } // wxDbTable::wxDbTable()
117 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
118 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
119 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
121 wxString tempQryTblName
;
122 tempQryTblName
= qryTblName
;
123 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
125 } // wxDbTable::wxDbTable()
128 /********** wxDbTable::~wxDbTable() **********/
129 wxDbTable::~wxDbTable()
132 } // wxDbTable::~wxDbTable()
135 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
136 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
138 // Initializing member variables
139 pDb
= pwxDb
; // Pointer to the wxDb object
143 m_hstmtGridQuery
= 0;
144 hstmtDefault
= 0; // Initialized below
145 hstmtCount
= 0; // Initialized first time it is needed
152 noCols
= numColumns
; // Number of cols in the table
153 where
.Empty(); // Where clause
154 orderBy
.Empty(); // Order By clause
155 from
.Empty(); // From clause
156 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
161 queryTableName
.Empty();
163 wxASSERT(tblName
.Length());
169 tableName
= tblName
; // Table Name
170 if (tblPath
.Length())
171 tablePath
= tblPath
; // Table Path - used for dBase files
175 if (qryTblName
.Length()) // Name of the table/view to query
176 queryTableName
= qryTblName
;
178 queryTableName
= tblName
;
180 pDb
->incrementTableCount();
183 tableID
= ++lastTableID
;
184 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
187 wxTablesInUse
*tableInUse
;
188 tableInUse
= new wxTablesInUse();
189 tableInUse
->tableName
= tblName
;
190 tableInUse
->tableID
= tableID
;
191 tableInUse
->pDb
= pDb
;
192 TablesInUse
.Append(tableInUse
);
197 // Grab the HENV and HDBC from the wxDb object
198 henv
= pDb
->GetHENV();
199 hdbc
= pDb
->GetHDBC();
201 // Allocate space for column definitions
203 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
205 // Allocate statement handles for the table
208 // Allocate a separate statement handle for performing inserts
209 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
210 pDb
->DispAllErrors(henv
, hdbc
);
211 // Allocate a separate statement handle for performing deletes
212 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
213 pDb
->DispAllErrors(henv
, hdbc
);
214 // Allocate a separate statement handle for performing updates
215 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
216 pDb
->DispAllErrors(henv
, hdbc
);
218 // Allocate a separate statement handle for internal use
219 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
220 pDb
->DispAllErrors(henv
, hdbc
);
222 // Set the cursor type for the statement handles
223 cursorType
= SQL_CURSOR_STATIC
;
225 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
227 // Check to see if cursor type is supported
228 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
229 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
231 // Datasource does not support static cursors. Driver
232 // will substitute a cursor type. Call SQLGetStmtOption()
233 // to determine which cursor type was selected.
234 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
235 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
236 #ifdef DBDEBUG_CONSOLE
237 cout
<< wxT("Static cursor changed to: ");
240 case SQL_CURSOR_FORWARD_ONLY
:
241 cout
<< wxT("Forward Only");
243 case SQL_CURSOR_STATIC
:
244 cout
<< wxT("Static");
246 case SQL_CURSOR_KEYSET_DRIVEN
:
247 cout
<< wxT("Keyset Driven");
249 case SQL_CURSOR_DYNAMIC
:
250 cout
<< wxT("Dynamic");
253 cout
<< endl
<< endl
;
256 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
258 // Force the use of a forward only cursor...
259 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
260 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
262 // Should never happen
263 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
270 pDb
->DispNextError();
271 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
274 #ifdef DBDEBUG_CONSOLE
276 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
281 // Set the cursor type for the INSERT statement handle
282 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
283 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
284 // Set the cursor type for the DELETE statement handle
285 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
286 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
287 // Set the cursor type for the UPDATE statement handle
288 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
289 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
292 // Make the default cursor the active cursor
293 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
294 wxASSERT(hstmtDefault
);
295 hstmt
= *hstmtDefault
;
299 } // wxDbTable::initialize()
302 void wxDbTable::cleanup()
307 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
314 TablesInUse
.DeleteContents(TRUE
);
318 pNode
= TablesInUse
.First();
319 while (pNode
&& !found
)
321 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
324 if (!TablesInUse
.DeleteNode(pNode
))
325 wxLogDebug (s
,wxT("Unable to delete node!"));
328 pNode
= pNode
->Next();
333 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
334 wxLogDebug (msg
,wxT("NOTICE..."));
339 // Decrement the wxDb table count
341 pDb
->decrementTableCount();
343 // Delete memory allocated for column definitions
347 // Free statement handles
353 ODBC 3.0 says to use this form
354 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
356 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
357 pDb
->DispAllErrors(henv
, hdbc
);
363 ODBC 3.0 says to use this form
364 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
366 if (SQLFreeStmt(hstmtDelete
, 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(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
377 pDb
->DispAllErrors(henv
, hdbc
);
383 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
384 pDb
->DispAllErrors(henv
, hdbc
);
387 // Delete dynamically allocated cursors
389 DeleteCursor(hstmtDefault
);
392 DeleteCursor(hstmtCount
);
394 if (m_hstmtGridQuery
)
395 DeleteCursor(m_hstmtGridQuery
);
397 } // wxDbTable::cleanup()
400 /***************************** PRIVATE FUNCTIONS *****************************/
403 /********** wxDbTable::bindParams() **********/
404 bool wxDbTable::bindParams(bool forUpdate
)
406 wxASSERT(!queryOnly
);
411 SDWORD precision
= 0;
414 // Bind each column of the table that should be bound
415 // to a parameter marker
419 for (i
=0, colNo
=1; i
< noCols
; i
++)
423 if (!colDefs
[i
].Updateable
)
428 if (!colDefs
[i
].InsertAllowed
)
432 switch(colDefs
[i
].DbDataType
)
434 case DB_DATA_TYPE_VARCHAR
:
435 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
436 precision
= colDefs
[i
].SzDataObj
;
439 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
441 colDefs
[i
].CbValue
= SQL_NTS
;
443 case DB_DATA_TYPE_INTEGER
:
444 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
445 precision
= pDb
->GetTypeInfInteger().Precision
;
448 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
450 colDefs
[i
].CbValue
= 0;
452 case DB_DATA_TYPE_FLOAT
:
453 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
454 precision
= pDb
->GetTypeInfFloat().Precision
;
455 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
456 // SQL Sybase Anywhere v5.5 returned a negative number for the
457 // MaxScale. This caused ODBC to kick out an error on ibscale.
458 // I check for this here and set the scale = precision.
460 // scale = (short) precision;
462 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
464 colDefs
[i
].CbValue
= 0;
466 case DB_DATA_TYPE_DATE
:
467 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
468 precision
= pDb
->GetTypeInfDate().Precision
;
471 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
473 colDefs
[i
].CbValue
= 0;
475 case DB_DATA_TYPE_BLOB
:
476 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
480 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
482 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
487 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
488 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
489 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
491 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
496 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
497 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
498 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
500 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
505 // Completed successfully
508 } // wxDbTable::bindParams()
511 /********** wxDbTable::bindInsertParams() **********/
512 bool wxDbTable::bindInsertParams(void)
514 return bindParams(FALSE
);
515 } // wxDbTable::bindInsertParams()
518 /********** wxDbTable::bindUpdateParams() **********/
519 bool wxDbTable::bindUpdateParams(void)
521 return bindParams(TRUE
);
522 } // wxDbTable::bindUpdateParams()
525 /********** wxDbTable::bindCols() **********/
526 bool wxDbTable::bindCols(HSTMT cursor
)
530 // Bind each column of the table to a memory address for fetching data
532 for (i
= 0; i
< noCols
; i
++)
534 cb
= colDefs
[i
].CbValue
;
535 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
536 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
537 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
540 // Completed successfully
543 } // wxDbTable::bindCols()
546 /********** wxDbTable::getRec() **********/
547 bool wxDbTable::getRec(UWORD fetchType
)
551 if (!pDb
->FwdOnlyCursors())
553 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
557 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
558 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
560 if (retcode
== SQL_NO_DATA_FOUND
)
563 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
567 // Set the Null member variable to indicate the Null state
568 // of each column just read in.
570 for (i
= 0; i
< noCols
; i
++)
571 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
576 // Fetch the next record from the record set
577 retcode
= SQLFetch(hstmt
);
578 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
580 if (retcode
== SQL_NO_DATA_FOUND
)
583 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
587 // Set the Null member variable to indicate the Null state
588 // of each column just read in.
590 for (i
= 0; i
< noCols
; i
++)
591 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
595 // Completed successfully
598 } // wxDbTable::getRec()
601 /********** wxDbTable::execDelete() **********/
602 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
606 // Execute the DELETE statement
607 retcode
= SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
609 if (retcode
== SQL_SUCCESS
||
610 retcode
== SQL_NO_DATA_FOUND
||
611 retcode
== SQL_SUCCESS_WITH_INFO
)
613 // Record deleted successfully
617 // Problem deleting record
618 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
620 } // wxDbTable::execDelete()
623 /********** wxDbTable::execUpdate() **********/
624 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
628 // Execute the UPDATE statement
629 retcode
= SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
631 if (retcode
== SQL_SUCCESS
||
632 retcode
== SQL_NO_DATA_FOUND
||
633 retcode
== SQL_SUCCESS_WITH_INFO
)
635 // Record updated successfully
638 else if (retcode
== SQL_NEED_DATA
)
641 while ((retcode
= SQLParamData(hstmtUpdate
, &pParmID
) == SQL_NEED_DATA
))
643 // Find the parameter
645 for (i
=0; i
< noCols
; i
++)
647 if (colDefs
[i
].PtrDataObj
== pParmID
)
649 // We found it. Store the parameter.
650 retcode
= SQLPutData(hstmtUpdate
, pParmID
, colDefs
[i
].SzDataObj
);
651 if (retcode
!= SQL_SUCCESS
)
653 pDb
->DispNextError();
654 return pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
660 if (retcode
== SQL_SUCCESS
||
661 retcode
== SQL_NO_DATA_FOUND
||
662 retcode
== SQL_SUCCESS_WITH_INFO
)
664 // Record updated successfully
669 // Problem updating record
670 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
672 } // wxDbTable::execUpdate()
675 /********** wxDbTable::query() **********/
676 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
681 // The user may wish to select for update, but the DBMS may not be capable
682 selectForUpdate
= CanSelectForUpdate();
684 selectForUpdate
= FALSE
;
686 // Set the SQL SELECT string
687 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
688 { // so generate a select statement.
689 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
690 pDb
->WriteSqlLog(sqlStmt
);
693 // Make sure the cursor is closed first
694 if (!CloseCursor(hstmt
))
697 // Execute the SQL SELECT statement
699 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
700 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
701 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
703 // Completed successfully
706 } // wxDbTable::query()
709 /***************************** PUBLIC FUNCTIONS *****************************/
712 /********** wxDbTable::Open() **********/
713 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
723 // Calculate the maximum size of the concatenated
724 // keys for use with wxDbGrid
726 for (i
=0; i
< noCols
; i
++)
728 if (colDefs
[i
].KeyField
)
731 m_keysize
+= colDefs
[i
].SzDataObj
;
736 // Verify that the table exists in the database
737 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
739 s
= wxT("Table/view does not exist in the database");
740 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
741 s
+= wxT(", or you have no permissions.\n");
745 else if (checkPrivileges
)
747 // Verify the user has rights to access the table.
748 // Shortcut boolean evaluation to optimize out call to
751 // Unfortunately this optimization doesn't seem to be
753 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
754 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
755 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
762 if (!tablePath
.IsEmpty())
763 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
765 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
768 pDb
->LogError(p
.GetData());
773 // Bind the member variables for field exchange between
774 // the wxDbTable object and the ODBC record.
777 if (!bindInsertParams()) // Inserts
780 if (!bindUpdateParams()) // Updates
784 if (!bindCols(*hstmtDefault
)) // Selects
787 if (!bindCols(hstmtInternal
)) // Internal use only
791 * Do NOT bind the hstmtCount cursor!!!
794 // Build an insert statement using parameter markers
795 if (!queryOnly
&& noCols
> 0)
797 bool needComma
= FALSE
;
798 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
799 pDb
->SQLTableName(tableName
.c_str()).c_str());
800 for (i
= 0; i
< noCols
; i
++)
802 if (! colDefs
[i
].InsertAllowed
)
806 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
807 // sqlStmt += colDefs[i].ColName;
811 sqlStmt
+= wxT(") VALUES (");
813 int insertableCount
= 0;
815 for (i
= 0; i
< noCols
; i
++)
817 if (! colDefs
[i
].InsertAllowed
)
827 // Prepare the insert statement for execution
830 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
831 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
837 // Completed successfully
840 } // wxDbTable::Open()
843 /********** wxDbTable::Query() **********/
844 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
847 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
849 } // wxDbTable::Query()
852 /********** wxDbTable::QueryBySqlStmt() **********/
853 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
855 pDb
->WriteSqlLog(pSqlStmt
);
857 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
859 } // wxDbTable::QueryBySqlStmt()
862 /********** wxDbTable::QueryMatching() **********/
863 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
866 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
868 } // wxDbTable::QueryMatching()
871 /********** wxDbTable::QueryOnKeyFields() **********/
872 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
875 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
877 } // wxDbTable::QueryOnKeyFields()
880 /********** wxDbTable::GetPrev() **********/
881 bool wxDbTable::GetPrev(void)
883 if (pDb
->FwdOnlyCursors())
885 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
889 return(getRec(SQL_FETCH_PRIOR
));
891 } // wxDbTable::GetPrev()
894 /********** wxDbTable::operator-- **********/
895 bool wxDbTable::operator--(int)
897 if (pDb
->FwdOnlyCursors())
899 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
903 return(getRec(SQL_FETCH_PRIOR
));
905 } // wxDbTable::operator--
908 /********** wxDbTable::GetFirst() **********/
909 bool wxDbTable::GetFirst(void)
911 if (pDb
->FwdOnlyCursors())
913 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
917 return(getRec(SQL_FETCH_FIRST
));
919 } // wxDbTable::GetFirst()
922 /********** wxDbTable::GetLast() **********/
923 bool wxDbTable::GetLast(void)
925 if (pDb
->FwdOnlyCursors())
927 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
931 return(getRec(SQL_FETCH_LAST
));
933 } // wxDbTable::GetLast()
936 /********** wxDbTable::BuildDeleteStmt() **********/
937 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
939 wxASSERT(!queryOnly
);
943 wxString whereClause
;
947 // Handle the case of DeleteWhere() and the where clause is blank. It should
948 // delete all records from the database in this case.
949 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
951 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
952 pDb
->SQLTableName(tableName
.c_str()).c_str());
956 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
957 pDb
->SQLTableName(tableName
.c_str()).c_str());
959 // Append the WHERE clause to the SQL DELETE statement
962 case DB_DEL_KEYFIELDS
:
963 // If the datasource supports the ROWID column, build
964 // the where on ROWID for efficiency purposes.
965 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
969 wxChar rowid
[wxDB_ROWID_LEN
+1];
971 // Get the ROWID value. If not successful retreiving the ROWID,
972 // simply fall down through the code and build the WHERE clause
973 // based on the key fields.
974 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
976 pSqlStmt
+= wxT("ROWID = '");
978 pSqlStmt
+= wxT("'");
982 // Unable to delete by ROWID, so build a WHERE
983 // clause based on the keyfields.
984 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
985 pSqlStmt
+= whereClause
;
988 pSqlStmt
+= pWhereClause
;
990 case DB_DEL_MATCHING
:
991 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
992 pSqlStmt
+= whereClause
;
996 } // BuildDeleteStmt()
999 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
1000 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
1002 wxString tempSqlStmt
;
1003 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
1004 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1005 } // wxDbTable::BuildDeleteStmt()
1008 /********** wxDbTable::BuildSelectStmt() **********/
1009 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
1011 wxString whereClause
;
1012 whereClause
.Empty();
1014 // Build a select statement to query the database
1015 pSqlStmt
= wxT("SELECT ");
1017 // SELECT DISTINCT values only?
1019 pSqlStmt
+= wxT("DISTINCT ");
1021 // Was a FROM clause specified to join tables to the base table?
1022 // Available for ::Query() only!!!
1023 bool appendFromClause
= FALSE
;
1024 #if wxODBC_BACKWARD_COMPATABILITY
1025 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
1026 appendFromClause
= TRUE
;
1028 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
1029 appendFromClause
= TRUE
;
1032 // Add the column list
1034 for (i
= 0; i
< noCols
; i
++)
1036 // If joining tables, the base table column names must be qualified to avoid ambiguity
1037 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1039 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1040 // pSqlStmt += queryTableName;
1041 pSqlStmt
+= wxT(".");
1043 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1044 // pSqlStmt += colDefs[i].ColName;
1046 pSqlStmt
+= wxT(",");
1049 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1050 // the ROWID if querying distinct records. The rowid will always be unique.
1051 if (!distinct
&& CanUpdByROWID())
1053 // If joining tables, the base table column names must be qualified to avoid ambiguity
1054 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1056 pSqlStmt
+= wxT(",");
1057 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1058 // pSqlStmt += queryTableName;
1059 pSqlStmt
+= wxT(".ROWID");
1062 pSqlStmt
+= wxT(",ROWID");
1065 // Append the FROM tablename portion
1066 pSqlStmt
+= wxT(" FROM ");
1067 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1068 // pSqlStmt += queryTableName;
1070 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1071 // The HOLDLOCK keyword follows the table name in the from clause.
1072 // Each table in the from clause must specify HOLDLOCK or
1073 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1074 // is parsed but ignored in SYBASE Transact-SQL.
1075 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1076 pSqlStmt
+= wxT(" HOLDLOCK");
1078 if (appendFromClause
)
1081 // Append the WHERE clause. Either append the where clause for the class
1082 // or build a where clause. The typeOfSelect determines this.
1083 switch(typeOfSelect
)
1085 case DB_SELECT_WHERE
:
1086 #if wxODBC_BACKWARD_COMPATABILITY
1087 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1089 if (where
.Length()) // May not want a where clause!!!
1092 pSqlStmt
+= wxT(" WHERE ");
1096 case DB_SELECT_KEYFIELDS
:
1097 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1098 if (whereClause
.Length())
1100 pSqlStmt
+= wxT(" WHERE ");
1101 pSqlStmt
+= whereClause
;
1104 case DB_SELECT_MATCHING
:
1105 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1106 if (whereClause
.Length())
1108 pSqlStmt
+= wxT(" WHERE ");
1109 pSqlStmt
+= whereClause
;
1114 // Append the ORDER BY clause
1115 #if wxODBC_BACKWARD_COMPATABILITY
1116 if (orderBy
&& wxStrlen(orderBy
))
1118 if (orderBy
.Length())
1121 pSqlStmt
+= wxT(" ORDER BY ");
1122 pSqlStmt
+= orderBy
;
1125 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1126 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1127 // HOLDLOCK for Sybase.
1128 if (selectForUpdate
&& CanSelectForUpdate())
1129 pSqlStmt
+= wxT(" FOR UPDATE");
1131 } // wxDbTable::BuildSelectStmt()
1134 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1135 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1137 wxString tempSqlStmt
;
1138 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1139 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1140 } // wxDbTable::BuildSelectStmt()
1143 /********** wxDbTable::BuildUpdateStmt() **********/
1144 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1146 wxASSERT(!queryOnly
);
1150 wxString whereClause
;
1151 whereClause
.Empty();
1153 bool firstColumn
= TRUE
;
1155 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1156 pDb
->SQLTableName(tableName
.c_str()).c_str());
1158 // Append a list of columns to be updated
1160 for (i
= 0; i
< noCols
; i
++)
1162 // Only append Updateable columns
1163 if (colDefs
[i
].Updateable
)
1166 pSqlStmt
+= wxT(",");
1168 firstColumn
= FALSE
;
1170 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1171 // pSqlStmt += colDefs[i].ColName;
1172 pSqlStmt
+= wxT(" = ?");
1176 // Append the WHERE clause to the SQL UPDATE statement
1177 pSqlStmt
+= wxT(" WHERE ");
1180 case DB_UPD_KEYFIELDS
:
1181 // If the datasource supports the ROWID column, build
1182 // the where on ROWID for efficiency purposes.
1183 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1184 if (CanUpdByROWID())
1187 wxChar rowid
[wxDB_ROWID_LEN
+1];
1189 // Get the ROWID value. If not successful retreiving the ROWID,
1190 // simply fall down through the code and build the WHERE clause
1191 // based on the key fields.
1192 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1194 pSqlStmt
+= wxT("ROWID = '");
1196 pSqlStmt
+= wxT("'");
1200 // Unable to delete by ROWID, so build a WHERE
1201 // clause based on the keyfields.
1202 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1203 pSqlStmt
+= whereClause
;
1206 pSqlStmt
+= pWhereClause
;
1209 } // BuildUpdateStmt()
1212 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1213 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1215 wxString tempSqlStmt
;
1216 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1217 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1218 } // BuildUpdateStmt()
1221 /********** wxDbTable::BuildWhereClause() **********/
1222 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1223 const wxString
&qualTableName
, bool useLikeComparison
)
1225 * Note: BuildWhereClause() currently ignores timestamp columns.
1226 * They are not included as part of the where clause.
1229 bool moreThanOneColumn
= FALSE
;
1232 // Loop through the columns building a where clause as you go
1234 for (i
= 0; i
< noCols
; i
++)
1236 // Determine if this column should be included in the WHERE clause
1237 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1238 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1240 // Skip over timestamp columns
1241 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1243 // If there is more than 1 column, join them with the keyword "AND"
1244 if (moreThanOneColumn
)
1245 pWhereClause
+= wxT(" AND ");
1247 moreThanOneColumn
= TRUE
;
1248 // Concatenate where phrase for the column
1249 if (qualTableName
.Length())
1251 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1252 // pWhereClause += qualTableName;
1253 pWhereClause
+= wxT(".");
1255 pWhereClause
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1256 // pWhereClause += colDefs[i].ColName;
1257 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1258 pWhereClause
+= wxT(" LIKE ");
1260 pWhereClause
+= wxT(" = ");
1261 switch(colDefs
[i
].SqlCtype
)
1264 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1267 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[i
].PtrDataObj
));
1270 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[i
].PtrDataObj
));
1273 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1276 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1279 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1282 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1285 pWhereClause
+= colValue
;
1288 } // wxDbTable::BuildWhereClause()
1291 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1292 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1293 const wxString
&qualTableName
, bool useLikeComparison
)
1295 wxString tempSqlStmt
;
1296 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1297 wxStrcpy(pWhereClause
, tempSqlStmt
);
1298 } // wxDbTable::BuildWhereClause()
1301 /********** wxDbTable::GetRowNum() **********/
1302 UWORD
wxDbTable::GetRowNum(void)
1306 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1308 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1312 // Completed successfully
1313 return((UWORD
) rowNum
);
1315 } // wxDbTable::GetRowNum()
1318 /********** wxDbTable::CloseCursor() **********/
1319 bool wxDbTable::CloseCursor(HSTMT cursor
)
1321 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1322 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1324 // Completed successfully
1327 } // wxDbTable::CloseCursor()
1330 /********** wxDbTable::CreateTable() **********/
1331 bool wxDbTable::CreateTable(bool attemptDrop
)
1339 #ifdef DBDEBUG_CONSOLE
1340 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1344 if (attemptDrop
&& !DropTable())
1348 #ifdef DBDEBUG_CONSOLE
1349 for (i
= 0; i
< noCols
; i
++)
1351 // Exclude derived columns since they are NOT part of the base table
1352 if (colDefs
[i
].DerivedCol
)
1354 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1355 switch(colDefs
[i
].DbDataType
)
1357 case DB_DATA_TYPE_VARCHAR
:
1358 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1360 case DB_DATA_TYPE_INTEGER
:
1361 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1363 case DB_DATA_TYPE_FLOAT
:
1364 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1366 case DB_DATA_TYPE_DATE
:
1367 cout
<< pDb
->GetTypeInfDate().TypeName
;
1369 case DB_DATA_TYPE_BLOB
:
1370 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1377 // Build a CREATE TABLE string from the colDefs structure.
1378 bool needComma
= FALSE
;
1380 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1381 pDb
->SQLTableName(tableName
.c_str()).c_str());
1383 for (i
= 0; i
< noCols
; i
++)
1385 // Exclude derived columns since they are NOT part of the base table
1386 if (colDefs
[i
].DerivedCol
)
1390 sqlStmt
+= wxT(",");
1392 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1393 // sqlStmt += colDefs[i].ColName;
1394 sqlStmt
+= wxT(" ");
1396 switch(colDefs
[i
].DbDataType
)
1398 case DB_DATA_TYPE_VARCHAR
:
1399 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1401 case DB_DATA_TYPE_INTEGER
:
1402 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1404 case DB_DATA_TYPE_FLOAT
:
1405 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1407 case DB_DATA_TYPE_DATE
:
1408 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1410 case DB_DATA_TYPE_BLOB
:
1411 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1414 // For varchars, append the size of the string
1415 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
&&
1416 (pDb
->Dbms() != dbmsMY_SQL
|| pDb
->GetTypeInfVarchar().TypeName
!= "text"))// ||
1417 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1420 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1424 if (pDb
->Dbms() == dbmsDB2
||
1425 pDb
->Dbms() == dbmsMY_SQL
||
1426 pDb
->Dbms() == dbmsSYBASE_ASE
||
1427 pDb
->Dbms() == dbmsINTERBASE
||
1428 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1430 if (colDefs
[i
].KeyField
)
1432 sqlStmt
+= wxT(" NOT NULL");
1438 // If there is a primary key defined, include it in the create statement
1439 for (i
= j
= 0; i
< noCols
; i
++)
1441 if (colDefs
[i
].KeyField
)
1447 if (j
&& (pDb
->Dbms() != dbmsDBASE
)
1448 && (pDb
->Dbms() != dbmsXBASE_SEQUITER
)
1449 ) // Found a keyfield
1451 switch (pDb
->Dbms())
1455 case dbmsSYBASE_ASA
:
1456 case dbmsSYBASE_ASE
:
1459 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1460 sqlStmt
+= wxT(",PRIMARY KEY (");
1465 sqlStmt
+= wxT(",CONSTRAINT ");
1466 // DB2 is limited to 18 characters for index names
1467 if (pDb
->Dbms() == dbmsDB2
)
1469 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."));
1470 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1471 // sqlStmt += tableName.substr(0, 13);
1474 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1475 // sqlStmt += tableName;
1477 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1482 // List column name(s) of column(s) comprising the primary key
1483 for (i
= j
= 0; i
< noCols
; i
++)
1485 if (colDefs
[i
].KeyField
)
1487 if (j
++) // Multi part key, comma separate names
1488 sqlStmt
+= wxT(",");
1489 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1491 if (pDb
->Dbms() == dbmsMY_SQL
&&
1492 colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1495 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1500 sqlStmt
+= wxT(")");
1502 if (pDb
->Dbms() == dbmsINFORMIX
||
1503 pDb
->Dbms() == dbmsSYBASE_ASA
||
1504 pDb
->Dbms() == dbmsSYBASE_ASE
)
1506 sqlStmt
+= wxT(" CONSTRAINT ");
1507 sqlStmt
+= pDb
->SQLTableName(tableName
);
1508 // sqlStmt += tableName;
1509 sqlStmt
+= wxT("_PIDX");
1512 // Append the closing parentheses for the create table statement
1513 sqlStmt
+= wxT(")");
1515 pDb
->WriteSqlLog(sqlStmt
);
1517 #ifdef DBDEBUG_CONSOLE
1518 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1521 // Execute the CREATE TABLE statement
1522 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1523 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1525 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1526 pDb
->RollbackTrans();
1531 // Commit the transaction and close the cursor
1532 if (!pDb
->CommitTrans())
1534 if (!CloseCursor(hstmt
))
1537 // Database table created successfully
1540 } // wxDbTable::CreateTable()
1543 /********** wxDbTable::DropTable() **********/
1544 bool wxDbTable::DropTable()
1546 // NOTE: This function returns TRUE if the Table does not exist, but
1547 // only for identified databases. Code will need to be added
1548 // below for any other databases when those databases are defined
1549 // to handle this situation consistently
1553 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1554 pDb
->SQLTableName(tableName
.c_str()).c_str());
1556 pDb
->WriteSqlLog(sqlStmt
);
1558 #ifdef DBDEBUG_CONSOLE
1559 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1562 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1563 if (retcode
!= SQL_SUCCESS
)
1565 // Check for "Base table not found" error and ignore
1566 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1567 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1568 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1570 // Check for product specific error codes
1571 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1572 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1573 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1574 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1576 pDb
->DispNextError();
1577 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1578 pDb
->RollbackTrans();
1579 // CloseCursor(hstmt);
1585 // Commit the transaction and close the cursor
1586 if (! pDb
->CommitTrans())
1588 if (! CloseCursor(hstmt
))
1592 } // wxDbTable::DropTable()
1595 /********** wxDbTable::CreateIndex() **********/
1596 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1597 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1601 // Drop the index first
1602 if (attemptDrop
&& !DropIndex(idxName
))
1605 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1606 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1607 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1608 // table was created, then months later you determine that an additional index while
1609 // give better performance, so you want to add an index).
1611 // The following block of code will modify the column definition to make the column be
1612 // defined with the "NOT NULL" qualifier.
1613 if (pDb
->Dbms() == dbmsMY_SQL
)
1618 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1622 // Find the column definition that has the ColName that matches the
1623 // index column name. We need to do this to get the DB_DATA_TYPE of
1624 // the index column, as MySQL's syntax for the ALTER column requires
1626 while (!found
&& (j
< this->noCols
))
1628 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1636 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1637 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1642 wxODBC_ERRORS retcode
;
1643 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1644 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1645 // This line is just here for debug checking of the value
1646 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1656 pDb
->RollbackTrans();
1661 // Build a CREATE INDEX statement
1662 sqlStmt
= wxT("CREATE ");
1664 sqlStmt
+= wxT("UNIQUE ");
1666 sqlStmt
+= wxT("INDEX ");
1667 sqlStmt
+= pDb
->SQLTableName(idxName
);
1668 sqlStmt
+= wxT(" ON ");
1670 sqlStmt
+= pDb
->SQLTableName(tableName
);
1671 // sqlStmt += tableName;
1672 sqlStmt
+= wxT(" (");
1674 // Append list of columns making up index
1676 for (i
= 0; i
< noIdxCols
; i
++)
1678 sqlStmt
+= pDb
->SQLColumnName(pIdxDefs
[i
].ColName
);
1679 // sqlStmt += pIdxDefs[i].ColName;
1681 // MySQL requires a key length on VARCHAR keys
1682 if ( pDb
->Dbms() == dbmsMY_SQL
)
1684 // Find the details on this column
1686 for ( j
= 0; j
< noCols
; ++j
)
1688 if ( wxStrcmp( pIdxDefs
[i
].ColName
, colDefs
[j
].ColName
) == 0 )
1693 if ( colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1696 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1701 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1702 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (strncmp(pDb
->dbInf
.dbmsVer
,"07",2)==0)) &&
1703 !(pDb
->Dbms() == dbmsPOSTGRES
))
1705 if (pIdxDefs
[i
].Ascending
)
1706 sqlStmt
+= wxT(" ASC");
1708 sqlStmt
+= wxT(" DESC");
1711 wxASSERT_MSG(pIdxDefs
[i
].Ascending
, "Datasource does not support DESCending index columns");
1713 if ((i
+ 1) < noIdxCols
)
1714 sqlStmt
+= wxT(",");
1717 // Append closing parentheses
1718 sqlStmt
+= wxT(")");
1720 pDb
->WriteSqlLog(sqlStmt
);
1722 #ifdef DBDEBUG_CONSOLE
1723 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1726 // Execute the CREATE INDEX statement
1727 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1729 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1730 pDb
->RollbackTrans();
1735 // Commit the transaction and close the cursor
1736 if (! pDb
->CommitTrans())
1738 if (! CloseCursor(hstmt
))
1741 // Index Created Successfully
1744 } // wxDbTable::CreateIndex()
1747 /********** wxDbTable::DropIndex() **********/
1748 bool wxDbTable::DropIndex(const wxString
&idxName
)
1750 // NOTE: This function returns TRUE if the Index does not exist, but
1751 // only for identified databases. Code will need to be added
1752 // below for any other databases when those databases are defined
1753 // to handle this situation consistently
1757 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1758 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1759 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1760 pDb
->SQLTableName(idxName
.c_str()).c_str(),
1761 pDb
->SQLTableName(tableName
.c_str()).c_str());
1762 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1763 (pDb
->Dbms() == dbmsSYBASE_ASE
) ||
1764 (pDb
->Dbms() == dbmsXBASE_SEQUITER
))
1765 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1766 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1767 pDb
->SQLTableName(idxName
.c_str()).c_str());
1769 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1770 pDb
->SQLTableName(idxName
.c_str()).c_str());
1772 pDb
->WriteSqlLog(sqlStmt
);
1774 #ifdef DBDEBUG_CONSOLE
1775 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1778 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1780 // Check for "Index not found" error and ignore
1781 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1782 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1784 // Check for product specific error codes
1785 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1786 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1787 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1788 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1789 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1790 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1791 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1794 pDb
->DispNextError();
1795 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1796 pDb
->RollbackTrans();
1803 // Commit the transaction and close the cursor
1804 if (! pDb
->CommitTrans())
1806 if (! CloseCursor(hstmt
))
1810 } // wxDbTable::DropIndex()
1813 /********** wxDbTable::SetOrderByColNums() **********/
1814 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1816 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1822 va_start(argptr
, first
); /* Initialize variable arguments. */
1823 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1825 // Make sure the passed in column number
1826 // is within the valid range of columns
1828 // Valid columns are 0 thru noCols-1
1829 if (colNo
>= noCols
|| colNo
< 0)
1836 tempStr
+= wxT(",");
1838 tempStr
+= colDefs
[colNo
].ColName
;
1839 colNo
= va_arg (argptr
, int);
1841 va_end (argptr
); /* Reset variable arguments. */
1843 SetOrderByClause(tempStr
);
1846 } // wxDbTable::SetOrderByColNums()
1849 /********** wxDbTable::Insert() **********/
1850 int wxDbTable::Insert(void)
1852 wxASSERT(!queryOnly
);
1853 if (queryOnly
|| !insertable
)
1858 // Insert the record by executing the already prepared insert statement
1860 retcode
=SQLExecute(hstmtInsert
);
1861 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
&&
1862 retcode
!= SQL_NEED_DATA
)
1864 // Check to see if integrity constraint was violated
1865 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1866 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1867 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1870 pDb
->DispNextError();
1871 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1875 if (retcode
== SQL_NEED_DATA
)
1878 while ((retcode
= SQLParamData(hstmtInsert
, &pParmID
) == SQL_NEED_DATA
))
1880 // Find the parameter
1882 for (i
=0; i
< noCols
; i
++)
1884 if (colDefs
[i
].PtrDataObj
== pParmID
)
1886 // We found it. Store the parameter.
1887 retcode
= SQLPutData(hstmtInsert
, pParmID
, colDefs
[i
].SzDataObj
);
1888 if (retcode
!= SQL_SUCCESS
)
1890 pDb
->DispNextError();
1891 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1900 // Record inserted into the datasource successfully
1903 } // wxDbTable::Insert()
1906 /********** wxDbTable::Update() **********/
1907 bool wxDbTable::Update(void)
1909 wxASSERT(!queryOnly
);
1915 // Build the SQL UPDATE statement
1916 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1918 pDb
->WriteSqlLog(sqlStmt
);
1920 #ifdef DBDEBUG_CONSOLE
1921 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1924 // Execute the SQL UPDATE statement
1925 return(execUpdate(sqlStmt
));
1927 } // wxDbTable::Update()
1930 /********** wxDbTable::Update(pSqlStmt) **********/
1931 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1933 wxASSERT(!queryOnly
);
1937 pDb
->WriteSqlLog(pSqlStmt
);
1939 return(execUpdate(pSqlStmt
));
1941 } // wxDbTable::Update(pSqlStmt)
1944 /********** wxDbTable::UpdateWhere() **********/
1945 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1947 wxASSERT(!queryOnly
);
1953 // Build the SQL UPDATE statement
1954 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1956 pDb
->WriteSqlLog(sqlStmt
);
1958 #ifdef DBDEBUG_CONSOLE
1959 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1962 // Execute the SQL UPDATE statement
1963 return(execUpdate(sqlStmt
));
1965 } // wxDbTable::UpdateWhere()
1968 /********** wxDbTable::Delete() **********/
1969 bool wxDbTable::Delete(void)
1971 wxASSERT(!queryOnly
);
1978 // Build the SQL DELETE statement
1979 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1981 pDb
->WriteSqlLog(sqlStmt
);
1983 // Execute the SQL DELETE statement
1984 return(execDelete(sqlStmt
));
1986 } // wxDbTable::Delete()
1989 /********** wxDbTable::DeleteWhere() **********/
1990 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1992 wxASSERT(!queryOnly
);
1999 // Build the SQL DELETE statement
2000 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
2002 pDb
->WriteSqlLog(sqlStmt
);
2004 // Execute the SQL DELETE statement
2005 return(execDelete(sqlStmt
));
2007 } // wxDbTable::DeleteWhere()
2010 /********** wxDbTable::DeleteMatching() **********/
2011 bool wxDbTable::DeleteMatching(void)
2013 wxASSERT(!queryOnly
);
2020 // Build the SQL DELETE statement
2021 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
2023 pDb
->WriteSqlLog(sqlStmt
);
2025 // Execute the SQL DELETE statement
2026 return(execDelete(sqlStmt
));
2028 } // wxDbTable::DeleteMatching()
2031 /********** wxDbTable::IsColNull() **********/
2032 bool wxDbTable::IsColNull(UWORD colNo
) const
2035 This logic is just not right. It would indicate TRUE
2036 if a numeric field were set to a value of 0.
2038 switch(colDefs[colNo].SqlCtype)
2041 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
2043 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
2045 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
2047 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
2049 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
2051 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
2053 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
2054 case SQL_C_TIMESTAMP:
2055 TIMESTAMP_STRUCT *pDt;
2056 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
2057 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
2065 return (colDefs
[colNo
].Null
);
2066 } // wxDbTable::IsColNull()
2069 /********** wxDbTable::CanSelectForUpdate() **********/
2070 bool wxDbTable::CanSelectForUpdate(void)
2075 if (pDb
->Dbms() == dbmsMY_SQL
)
2078 if ((pDb
->Dbms() == dbmsORACLE
) ||
2079 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
2084 } // wxDbTable::CanSelectForUpdate()
2087 /********** wxDbTable::CanUpdByROWID() **********/
2088 bool wxDbTable::CanUpdByROWID(void)
2091 * NOTE: Returning FALSE for now until this can be debugged,
2092 * as the ROWID is not getting updated correctly
2096 if (pDb->Dbms() == dbmsORACLE)
2101 } // wxDbTable::CanUpdByROWID()
2104 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2105 bool wxDbTable::IsCursorClosedOnCommit(void)
2107 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2112 } // wxDbTable::IsCursorClosedOnCommit()
2116 /********** wxDbTable::ClearMemberVar() **********/
2117 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2119 wxASSERT(colNo
< noCols
);
2121 switch(colDefs
[colNo
].SqlCtype
)
2124 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2127 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2130 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2133 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2136 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2139 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2142 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2144 case SQL_C_TIMESTAMP
:
2145 TIMESTAMP_STRUCT
*pDt
;
2146 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2159 } // wxDbTable::ClearMemberVar()
2162 /********** wxDbTable::ClearMemberVars() **********/
2163 void wxDbTable::ClearMemberVars(bool setToNull
)
2167 // Loop through the columns setting each member variable to zero
2168 for (i
=0; i
< noCols
; i
++)
2169 ClearMemberVar(i
,setToNull
);
2171 } // wxDbTable::ClearMemberVars()
2174 /********** wxDbTable::SetQueryTimeout() **********/
2175 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2177 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2178 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2179 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2180 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2181 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2182 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2183 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2184 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2186 // Completed Successfully
2189 } // wxDbTable::SetQueryTimeout()
2192 /********** wxDbTable::SetColDefs() **********/
2193 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2194 SWORD cType
, int size
, bool keyField
, bool upd
,
2195 bool insAllow
, bool derivedCol
)
2197 wxASSERT_MSG( index
< noCols
,
2198 _T("Specified column index exceeds the maximum number of columns for this table.") );
2200 if (!colDefs
) // May happen if the database connection fails
2203 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2205 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2206 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2210 tmpMsg
.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2211 fieldName
.c_str(),colDefs
[index
].ColName
);
2213 #endif // __WXDEBUG__
2216 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2218 colDefs
[index
].DbDataType
= dataType
;
2219 colDefs
[index
].PtrDataObj
= pData
;
2220 colDefs
[index
].SqlCtype
= cType
;
2221 colDefs
[index
].SzDataObj
= size
;
2222 colDefs
[index
].KeyField
= keyField
;
2223 colDefs
[index
].DerivedCol
= derivedCol
;
2224 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2227 colDefs
[index
].Updateable
= FALSE
;
2228 colDefs
[index
].InsertAllowed
= FALSE
;
2232 colDefs
[index
].Updateable
= upd
;
2233 colDefs
[index
].InsertAllowed
= insAllow
;
2236 colDefs
[index
].Null
= FALSE
;
2238 } // wxDbTable::SetColDefs()
2241 /********** wxDbTable::SetColDefs() **********/
2242 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2245 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2251 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2253 for (index
= 0; index
< numCols
; index
++)
2255 // Process the fields
2256 switch (pColInfs
[index
].dbDataType
)
2258 case DB_DATA_TYPE_VARCHAR
:
2259 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2260 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2261 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2263 case DB_DATA_TYPE_INTEGER
:
2264 // Can be long or short
2265 if (pColInfs
[index
].bufferLength
== sizeof(long))
2267 pColDataPtrs
[index
].PtrDataObj
= new long;
2268 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2269 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2273 pColDataPtrs
[index
].PtrDataObj
= new short;
2274 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2275 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2278 case DB_DATA_TYPE_FLOAT
:
2279 // Can be float or double
2280 if (pColInfs
[index
].bufferLength
== sizeof(float))
2282 pColDataPtrs
[index
].PtrDataObj
= new float;
2283 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2284 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2288 pColDataPtrs
[index
].PtrDataObj
= new double;
2289 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2290 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2293 case DB_DATA_TYPE_DATE
:
2294 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2295 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2296 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2298 case DB_DATA_TYPE_BLOB
:
2299 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2300 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2301 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2302 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2305 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2306 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2309 // Unable to build all the column definitions, as either one of
2310 // the calls to "new" failed above, or there was a BLOB field
2311 // to have a column definition for. If BLOBs are to be used,
2312 // the other form of ::SetColDefs() must be used, as it is impossible
2313 // to know the maximum size to create the PtrDataObj to be.
2314 delete [] pColDataPtrs
;
2320 return (pColDataPtrs
);
2322 } // wxDbTable::SetColDefs()
2325 /********** wxDbTable::SetCursor() **********/
2326 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2328 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2329 hstmt
= *hstmtDefault
;
2331 hstmt
= *hstmtActivate
;
2333 } // wxDbTable::SetCursor()
2336 /********** wxDbTable::Count(const wxString &) **********/
2337 ULONG
wxDbTable::Count(const wxString
&args
)
2343 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2344 sqlStmt
= wxT("SELECT COUNT(");
2346 sqlStmt
+= wxT(") FROM ");
2347 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2348 // sqlStmt += queryTableName;
2349 #if wxODBC_BACKWARD_COMPATABILITY
2350 if (from
&& wxStrlen(from
))
2356 // Add the where clause if one is provided
2357 #if wxODBC_BACKWARD_COMPATABILITY
2358 if (where
&& wxStrlen(where
))
2363 sqlStmt
+= wxT(" WHERE ");
2367 pDb
->WriteSqlLog(sqlStmt
);
2369 // Initialize the Count cursor if it's not already initialized
2372 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2373 wxASSERT(hstmtCount
);
2378 // Execute the SQL statement
2379 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2381 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2386 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2388 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2392 // Obtain the result
2393 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2395 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2400 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2401 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2403 // Return the record count
2406 } // wxDbTable::Count()
2409 /********** wxDbTable::Refresh() **********/
2410 bool wxDbTable::Refresh(void)
2414 // Switch to the internal cursor so any active cursors are not corrupted
2415 HSTMT currCursor
= GetCursor();
2416 hstmt
= hstmtInternal
;
2417 #if wxODBC_BACKWARD_COMPATABILITY
2418 // Save the where and order by clauses
2419 char *saveWhere
= where
;
2420 char *saveOrderBy
= orderBy
;
2422 wxString saveWhere
= where
;
2423 wxString saveOrderBy
= orderBy
;
2425 // Build a where clause to refetch the record with. Try and use the
2426 // ROWID if it's available, ow use the key fields.
2427 wxString whereClause
;
2428 whereClause
.Empty();
2430 if (CanUpdByROWID())
2433 wxChar rowid
[wxDB_ROWID_LEN
+1];
2435 // Get the ROWID value. If not successful retreiving the ROWID,
2436 // simply fall down through the code and build the WHERE clause
2437 // based on the key fields.
2438 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2440 whereClause
+= pDb
->SQLTableName(queryTableName
);
2441 // whereClause += queryTableName;
2442 whereClause
+= wxT(".ROWID = '");
2443 whereClause
+= rowid
;
2444 whereClause
+= wxT("'");
2448 // If unable to use the ROWID, build a where clause from the keyfields
2449 if (wxStrlen(whereClause
) == 0)
2450 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2452 // Requery the record
2453 where
= whereClause
;
2458 if (result
&& !GetNext())
2461 // Switch back to original cursor
2462 SetCursor(&currCursor
);
2464 // Free the internal cursor
2465 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2466 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2468 // Restore the original where and order by clauses
2470 orderBy
= saveOrderBy
;
2474 } // wxDbTable::Refresh()
2477 /********** wxDbTable::SetColNull() **********/
2478 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2482 colDefs
[colNo
].Null
= set
;
2483 if (set
) // Blank out the values in the member variable
2484 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2490 } // wxDbTable::SetColNull()
2493 /********** wxDbTable::SetColNull() **********/
2494 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2497 for (i
= 0; i
< noCols
; i
++)
2499 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2505 colDefs
[i
].Null
= set
;
2506 if (set
) // Blank out the values in the member variable
2507 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2513 } // wxDbTable::SetColNull()
2516 /********** wxDbTable::GetNewCursor() **********/
2517 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2519 HSTMT
*newHSTMT
= new HSTMT
;
2524 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2526 pDb
->DispAllErrors(henv
, hdbc
);
2531 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2533 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2540 if (!bindCols(*newHSTMT
))
2548 SetCursor(newHSTMT
);
2552 } // wxDbTable::GetNewCursor()
2555 /********** wxDbTable::DeleteCursor() **********/
2556 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2560 if (!hstmtDel
) // Cursor already deleted
2564 ODBC 3.0 says to use this form
2565 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2568 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2570 pDb
->DispAllErrors(henv
, hdbc
);
2578 } // wxDbTable::DeleteCursor()
2580 //////////////////////////////////////////////////////////////
2581 // wxDbGrid support functions
2582 //////////////////////////////////////////////////////////////
2584 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2586 if (!m_hstmtGridQuery
)
2588 m_hstmtGridQuery
= GetNewCursor(FALSE
,FALSE
);
2589 if (!bindCols(*m_hstmtGridQuery
))
2593 m_rowmode
= rowmode
;
2596 case WX_ROW_MODE_QUERY
:
2597 SetCursor(m_hstmtGridQuery
);
2599 case WX_ROW_MODE_INDIVIDUAL
:
2600 SetCursor(hstmtDefault
);
2605 } // wxDbTable::SetRowMode()
2608 wxVariant
wxDbTable::GetCol(const int colNo
) const
2611 if ((colNo
< noCols
) && (!IsColNull(colNo
)))
2613 switch (colDefs
[colNo
].SqlCtype
)
2617 val
= (wxChar
*)(colDefs
[colNo
].PtrDataObj
);
2621 val
= *(long *)(colDefs
[colNo
].PtrDataObj
);
2625 val
= (long int )(*(short *)(colDefs
[colNo
].PtrDataObj
));
2628 val
= (long)(*(unsigned long *)(colDefs
[colNo
].PtrDataObj
));
2631 val
= (long)(*(char *)(colDefs
[colNo
].PtrDataObj
));
2633 case SQL_C_UTINYINT
:
2634 val
= (long)(*(unsigned char *)(colDefs
[colNo
].PtrDataObj
));
2637 val
= (long)(*(UWORD
*)(colDefs
[colNo
].PtrDataObj
));
2640 val
= (DATE_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2643 val
= (TIME_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2645 case SQL_C_TIMESTAMP
:
2646 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2649 val
= *(double *)(colDefs
[colNo
].PtrDataObj
);
2656 } // wxDbTable::GetCol()
2659 void csstrncpyt(char *s
, const char *t
, int n
)
2661 while ( (*s
++ = *t
++) != '\0' && --n
)
2667 void wxDbTable::SetCol(const int colNo
, const wxVariant val
)
2669 //FIXME: Add proper wxDateTime support to wxVariant..
2672 SetColNull(colNo
, val
.IsNull());
2676 if ((colDefs
[colNo
].SqlCtype
== SQL_C_DATE
)
2677 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIME
)
2678 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
))
2680 //Returns null if invalid!
2681 if (!dateval
.ParseDate(val
.GetString()))
2682 SetColNull(colNo
, TRUE
);
2685 switch (colDefs
[colNo
].SqlCtype
)
2689 csstrncpyt((char *)(colDefs
[colNo
].PtrDataObj
),
2690 val
.GetString().c_str(),
2691 colDefs
[colNo
].SzDataObj
-1);
2695 *(long *)(colDefs
[colNo
].PtrDataObj
) = val
;
2699 *(short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2702 *(unsigned long *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2705 *(char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2707 case SQL_C_UTINYINT
:
2708 *(unsigned char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2711 *(unsigned short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2713 //FIXME: Add proper wxDateTime support to wxVariant..
2716 DATE_STRUCT
*dataptr
=
2717 (DATE_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2719 dataptr
->year
= dateval
.GetYear();
2720 dataptr
->month
= dateval
.GetMonth()+1;
2721 dataptr
->day
= dateval
.GetDay();
2726 TIME_STRUCT
*dataptr
=
2727 (TIME_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2729 dataptr
->hour
= dateval
.GetHour();
2730 dataptr
->minute
= dateval
.GetMinute();
2731 dataptr
->second
= dateval
.GetSecond();
2734 case SQL_C_TIMESTAMP
:
2736 TIMESTAMP_STRUCT
*dataptr
=
2737 (TIMESTAMP_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2738 dataptr
->year
= dateval
.GetYear();
2739 dataptr
->month
= dateval
.GetMonth()+1;
2740 dataptr
->day
= dateval
.GetDay();
2742 dataptr
->hour
= dateval
.GetHour();
2743 dataptr
->minute
= dateval
.GetMinute();
2744 dataptr
->second
= dateval
.GetSecond();
2748 *(double *)(colDefs
[colNo
].PtrDataObj
) = val
;
2753 } // if (!val.IsNull())
2754 } // wxDbTable::SetCol()
2757 GenericKey
wxDbTable::GetKey()
2762 blk
= malloc(m_keysize
);
2763 blkptr
= (wxChar
*) blk
;
2766 for (i
=0; i
< noCols
; i
++)
2768 if (colDefs
[i
].KeyField
)
2770 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2771 blkptr
+= colDefs
[i
].SzDataObj
;
2775 GenericKey k
= GenericKey(blk
, m_keysize
);
2779 } // wxDbTable::GetKey()
2782 void wxDbTable::SetKey(const GenericKey
& k
)
2788 blkptr
= (wxChar
*)blk
;
2791 for (i
=0; i
< noCols
; i
++)
2793 if (colDefs
[i
].KeyField
)
2795 SetColNull(i
, FALSE
);
2796 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2797 blkptr
+= colDefs
[i
].SzDataObj
;
2800 } // wxDbTable::SetKey()
2803 #endif // wxUSE_ODBC