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"
52 #include "wx/msgdlg.h"
56 #include "wx/filefn.h"
65 #include "wx/dbtable.h"
68 // The HPUX preprocessor lines below were commented out on 8/20/97
69 // because macros.h currently redefines DEBUG and is unneeded.
71 // # include <macros.h>
74 # include <sys/minmax.h>
78 ULONG lastTableID
= 0;
86 /********** wxDbColDef::wxDbColDef() Constructor **********/
87 wxDbColDef::wxDbColDef()
93 bool wxDbColDef::Initialize()
96 DbDataType
= DB_DATA_TYPE_INTEGER
;
97 SqlCtype
= SQL_C_LONG
;
102 InsertAllowed
= FALSE
;
108 } // wxDbColDef::Initialize()
111 /********** wxDbTable::wxDbTable() Constructor **********/
112 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
113 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
115 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
117 } // wxDbTable::wxDbTable()
120 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
121 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
122 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
124 wxString tempQryTblName
;
125 tempQryTblName
= qryTblName
;
126 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
128 } // wxDbTable::wxDbTable()
131 /********** wxDbTable::~wxDbTable() **********/
132 wxDbTable::~wxDbTable()
135 } // wxDbTable::~wxDbTable()
138 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
139 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
141 // Initializing member variables
142 pDb
= pwxDb
; // Pointer to the wxDb object
146 m_hstmtGridQuery
= 0;
147 hstmtDefault
= 0; // Initialized below
148 hstmtCount
= 0; // Initialized first time it is needed
155 noCols
= numColumns
; // Number of cols in the table
156 where
.Empty(); // Where clause
157 orderBy
.Empty(); // Order By clause
158 from
.Empty(); // From clause
159 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
164 queryTableName
.Empty();
166 wxASSERT(tblName
.Length());
172 tableName
= tblName
; // Table Name
173 if (tblPath
.Length())
174 tablePath
= tblPath
; // Table Path - used for dBase files
178 if (qryTblName
.Length()) // Name of the table/view to query
179 queryTableName
= qryTblName
;
181 queryTableName
= tblName
;
183 pDb
->incrementTableCount();
186 tableID
= ++lastTableID
;
187 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
190 wxTablesInUse
*tableInUse
;
191 tableInUse
= new wxTablesInUse();
192 tableInUse
->tableName
= tblName
;
193 tableInUse
->tableID
= tableID
;
194 tableInUse
->pDb
= pDb
;
195 TablesInUse
.Append(tableInUse
);
200 // Grab the HENV and HDBC from the wxDb object
201 henv
= pDb
->GetHENV();
202 hdbc
= pDb
->GetHDBC();
204 // Allocate space for column definitions
206 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
208 // Allocate statement handles for the table
211 // Allocate a separate statement handle for performing inserts
212 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
213 pDb
->DispAllErrors(henv
, hdbc
);
214 // Allocate a separate statement handle for performing deletes
215 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
216 pDb
->DispAllErrors(henv
, hdbc
);
217 // Allocate a separate statement handle for performing updates
218 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
219 pDb
->DispAllErrors(henv
, hdbc
);
221 // Allocate a separate statement handle for internal use
222 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
223 pDb
->DispAllErrors(henv
, hdbc
);
225 // Set the cursor type for the statement handles
226 cursorType
= SQL_CURSOR_STATIC
;
228 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
230 // Check to see if cursor type is supported
231 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
232 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
234 // Datasource does not support static cursors. Driver
235 // will substitute a cursor type. Call SQLGetStmtOption()
236 // to determine which cursor type was selected.
237 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
238 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
239 #ifdef DBDEBUG_CONSOLE
240 cout
<< wxT("Static cursor changed to: ");
243 case SQL_CURSOR_FORWARD_ONLY
:
244 cout
<< wxT("Forward Only");
246 case SQL_CURSOR_STATIC
:
247 cout
<< wxT("Static");
249 case SQL_CURSOR_KEYSET_DRIVEN
:
250 cout
<< wxT("Keyset Driven");
252 case SQL_CURSOR_DYNAMIC
:
253 cout
<< wxT("Dynamic");
256 cout
<< endl
<< endl
;
259 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
261 // Force the use of a forward only cursor...
262 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
263 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
265 // Should never happen
266 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
273 pDb
->DispNextError();
274 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
277 #ifdef DBDEBUG_CONSOLE
279 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
284 // Set the cursor type for the INSERT statement handle
285 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
286 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
287 // Set the cursor type for the DELETE statement handle
288 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
289 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
290 // Set the cursor type for the UPDATE statement handle
291 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
292 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
295 // Make the default cursor the active cursor
296 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
297 wxASSERT(hstmtDefault
);
298 hstmt
= *hstmtDefault
;
302 } // wxDbTable::initialize()
305 void wxDbTable::cleanup()
310 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
317 TablesInUse
.DeleteContents(TRUE
);
321 pNode
= TablesInUse
.First();
322 while (pNode
&& !found
)
324 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
327 if (!TablesInUse
.DeleteNode(pNode
))
328 wxLogDebug (s
,wxT("Unable to delete node!"));
331 pNode
= pNode
->Next();
336 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
337 wxLogDebug (msg
,wxT("NOTICE..."));
342 // Decrement the wxDb table count
344 pDb
->decrementTableCount();
346 // Delete memory allocated for column definitions
350 // Free statement handles
356 ODBC 3.0 says to use this form
357 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
359 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
360 pDb
->DispAllErrors(henv
, hdbc
);
366 ODBC 3.0 says to use this form
367 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
369 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
370 pDb
->DispAllErrors(henv
, hdbc
);
376 ODBC 3.0 says to use this form
377 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
379 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
380 pDb
->DispAllErrors(henv
, hdbc
);
386 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
387 pDb
->DispAllErrors(henv
, hdbc
);
390 // Delete dynamically allocated cursors
392 DeleteCursor(hstmtDefault
);
395 DeleteCursor(hstmtCount
);
397 if (m_hstmtGridQuery
)
398 DeleteCursor(m_hstmtGridQuery
);
400 } // wxDbTable::cleanup()
403 /***************************** PRIVATE FUNCTIONS *****************************/
406 /********** wxDbTable::bindParams() **********/
407 bool wxDbTable::bindParams(bool forUpdate
)
409 wxASSERT(!queryOnly
);
414 UDWORD precision
= 0;
417 // Bind each column of the table that should be bound
418 // to a parameter marker
422 for (i
=0, colNo
=1; i
< noCols
; i
++)
426 if (!colDefs
[i
].Updateable
)
431 if (!colDefs
[i
].InsertAllowed
)
435 switch(colDefs
[i
].DbDataType
)
437 case DB_DATA_TYPE_VARCHAR
:
438 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
439 precision
= colDefs
[i
].SzDataObj
;
442 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
444 colDefs
[i
].CbValue
= SQL_NTS
;
446 case DB_DATA_TYPE_INTEGER
:
447 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
448 precision
= pDb
->GetTypeInfInteger().Precision
;
451 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
453 colDefs
[i
].CbValue
= 0;
455 case DB_DATA_TYPE_FLOAT
:
456 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
457 precision
= pDb
->GetTypeInfFloat().Precision
;
458 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
459 // SQL Sybase Anywhere v5.5 returned a negative number for the
460 // MaxScale. This caused ODBC to kick out an error on ibscale.
461 // I check for this here and set the scale = precision.
463 // scale = (short) precision;
465 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
467 colDefs
[i
].CbValue
= 0;
469 case DB_DATA_TYPE_DATE
:
470 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
471 precision
= pDb
->GetTypeInfDate().Precision
;
474 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
476 colDefs
[i
].CbValue
= 0;
478 case DB_DATA_TYPE_BLOB
:
479 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
483 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
485 colDefs
[i
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[i
].SzDataObj
);
490 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
491 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
492 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
494 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
499 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
500 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
501 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
503 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
508 // Completed successfully
511 } // wxDbTable::bindParams()
514 /********** wxDbTable::bindInsertParams() **********/
515 bool wxDbTable::bindInsertParams(void)
517 return bindParams(FALSE
);
518 } // wxDbTable::bindInsertParams()
521 /********** wxDbTable::bindUpdateParams() **********/
522 bool wxDbTable::bindUpdateParams(void)
524 return bindParams(TRUE
);
525 } // wxDbTable::bindUpdateParams()
528 /********** wxDbTable::bindCols() **********/
529 bool wxDbTable::bindCols(HSTMT cursor
)
533 // Bind each column of the table to a memory address for fetching data
535 for (i
= 0; i
< noCols
; i
++)
537 cb
= colDefs
[i
].CbValue
;
538 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
539 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
540 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
543 // Completed successfully
546 } // wxDbTable::bindCols()
549 /********** wxDbTable::getRec() **********/
550 bool wxDbTable::getRec(UWORD fetchType
)
554 if (!pDb
->FwdOnlyCursors())
556 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
560 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
561 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
563 if (retcode
== SQL_NO_DATA_FOUND
)
566 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
570 // Set the Null member variable to indicate the Null state
571 // of each column just read in.
573 for (i
= 0; i
< noCols
; i
++)
574 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
579 // Fetch the next record from the record set
580 retcode
= SQLFetch(hstmt
);
581 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
583 if (retcode
== SQL_NO_DATA_FOUND
)
586 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
590 // Set the Null member variable to indicate the Null state
591 // of each column just read in.
593 for (i
= 0; i
< noCols
; i
++)
594 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
598 // Completed successfully
601 } // wxDbTable::getRec()
604 /********** wxDbTable::execDelete() **********/
605 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
609 // Execute the DELETE statement
610 retcode
= SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
612 if (retcode
== SQL_SUCCESS
||
613 retcode
== SQL_NO_DATA_FOUND
||
614 retcode
== SQL_SUCCESS_WITH_INFO
)
616 // Record deleted successfully
620 // Problem deleting record
621 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
623 } // wxDbTable::execDelete()
626 /********** wxDbTable::execUpdate() **********/
627 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
631 // Execute the UPDATE statement
632 retcode
= SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
634 if (retcode
== SQL_SUCCESS
||
635 retcode
== SQL_NO_DATA_FOUND
||
636 retcode
== SQL_SUCCESS_WITH_INFO
)
638 // Record updated successfully
642 // Problem updating record
643 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
645 } // wxDbTable::execUpdate()
648 /********** wxDbTable::query() **********/
649 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
654 // The user may wish to select for update, but the DBMS may not be capable
655 selectForUpdate
= CanSelectForUpdate();
657 selectForUpdate
= FALSE
;
659 // Set the SQL SELECT string
660 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
661 { // so generate a select statement.
662 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
663 pDb
->WriteSqlLog(sqlStmt
);
666 // Make sure the cursor is closed first
667 if (!CloseCursor(hstmt
))
670 // Execute the SQL SELECT statement
672 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
673 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
674 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
676 // Completed successfully
679 } // wxDbTable::query()
682 /***************************** PUBLIC FUNCTIONS *****************************/
685 /********** wxDbTable::Open() **********/
686 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
696 // Calculate the maximum size of the concatenated
697 // keys for use with wxDbGrid
699 for (i
=0; i
< noCols
; i
++)
701 if (colDefs
[i
].KeyField
)
704 m_keysize
+= colDefs
[i
].SzDataObj
;
709 // Verify that the table exists in the database
710 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
712 s
= wxT("Table/view does not exist in the database");
713 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
714 s
+= wxT(", or you have no permissions.\n");
718 else if (checkPrivileges
)
720 // Verify the user has rights to access the table.
721 // Shortcut boolean evaluation to optimize out call to
724 // Unfortunately this optimization doesn't seem to be
726 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
727 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
728 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
735 if (!tablePath
.IsEmpty())
736 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
738 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
741 pDb
->LogError(p
.GetData());
746 // Bind the member variables for field exchange between
747 // the wxDbTable object and the ODBC record.
750 if (!bindInsertParams()) // Inserts
753 if (!bindUpdateParams()) // Updates
757 if (!bindCols(*hstmtDefault
)) // Selects
760 if (!bindCols(hstmtInternal
)) // Internal use only
764 * Do NOT bind the hstmtCount cursor!!!
767 // Build an insert statement using parameter markers
768 if (!queryOnly
&& noCols
> 0)
770 bool needComma
= FALSE
;
771 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
772 pDb
->SQLTableName(tableName
.c_str()).c_str());
773 for (i
= 0; i
< noCols
; i
++)
775 if (! colDefs
[i
].InsertAllowed
)
779 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
780 // sqlStmt += colDefs[i].ColName;
784 sqlStmt
+= wxT(") VALUES (");
786 int insertableCount
= 0;
788 for (i
= 0; i
< noCols
; i
++)
790 if (! colDefs
[i
].InsertAllowed
)
800 // Prepare the insert statement for execution
803 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
804 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
810 // Completed successfully
813 } // wxDbTable::Open()
816 /********** wxDbTable::Query() **********/
817 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
820 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
822 } // wxDbTable::Query()
825 /********** wxDbTable::QueryBySqlStmt() **********/
826 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
828 pDb
->WriteSqlLog(pSqlStmt
);
830 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
832 } // wxDbTable::QueryBySqlStmt()
835 /********** wxDbTable::QueryMatching() **********/
836 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
839 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
841 } // wxDbTable::QueryMatching()
844 /********** wxDbTable::QueryOnKeyFields() **********/
845 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
848 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
850 } // wxDbTable::QueryOnKeyFields()
853 /********** wxDbTable::GetPrev() **********/
854 bool wxDbTable::GetPrev(void)
856 if (pDb
->FwdOnlyCursors())
858 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
862 return(getRec(SQL_FETCH_PRIOR
));
864 } // wxDbTable::GetPrev()
867 /********** wxDbTable::operator-- **********/
868 bool wxDbTable::operator--(int)
870 if (pDb
->FwdOnlyCursors())
872 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
876 return(getRec(SQL_FETCH_PRIOR
));
878 } // wxDbTable::operator--
881 /********** wxDbTable::GetFirst() **********/
882 bool wxDbTable::GetFirst(void)
884 if (pDb
->FwdOnlyCursors())
886 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
890 return(getRec(SQL_FETCH_FIRST
));
892 } // wxDbTable::GetFirst()
895 /********** wxDbTable::GetLast() **********/
896 bool wxDbTable::GetLast(void)
898 if (pDb
->FwdOnlyCursors())
900 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
904 return(getRec(SQL_FETCH_LAST
));
906 } // wxDbTable::GetLast()
909 /********** wxDbTable::BuildDeleteStmt() **********/
910 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
912 wxASSERT(!queryOnly
);
916 wxString whereClause
;
920 // Handle the case of DeleteWhere() and the where clause is blank. It should
921 // delete all records from the database in this case.
922 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
924 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
925 pDb
->SQLTableName(tableName
.c_str()).c_str());
929 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
930 pDb
->SQLTableName(tableName
.c_str()).c_str());
932 // Append the WHERE clause to the SQL DELETE statement
935 case DB_DEL_KEYFIELDS
:
936 // If the datasource supports the ROWID column, build
937 // the where on ROWID for efficiency purposes.
938 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
942 wxChar rowid
[wxDB_ROWID_LEN
+1];
944 // Get the ROWID value. If not successful retreiving the ROWID,
945 // simply fall down through the code and build the WHERE clause
946 // based on the key fields.
947 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
949 pSqlStmt
+= wxT("ROWID = '");
951 pSqlStmt
+= wxT("'");
955 // Unable to delete by ROWID, so build a WHERE
956 // clause based on the keyfields.
957 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
958 pSqlStmt
+= whereClause
;
961 pSqlStmt
+= pWhereClause
;
963 case DB_DEL_MATCHING
:
964 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
965 pSqlStmt
+= whereClause
;
969 } // BuildDeleteStmt()
972 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
973 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
975 wxString tempSqlStmt
;
976 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
977 wxStrcpy(pSqlStmt
, tempSqlStmt
);
978 } // wxDbTable::BuildDeleteStmt()
981 /********** wxDbTable::BuildSelectStmt() **********/
982 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
984 wxString whereClause
;
987 // Build a select statement to query the database
988 pSqlStmt
= wxT("SELECT ");
990 // SELECT DISTINCT values only?
992 pSqlStmt
+= wxT("DISTINCT ");
994 // Was a FROM clause specified to join tables to the base table?
995 // Available for ::Query() only!!!
996 bool appendFromClause
= FALSE
;
997 #if wxODBC_BACKWARD_COMPATABILITY
998 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
999 appendFromClause
= TRUE
;
1001 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
1002 appendFromClause
= TRUE
;
1005 // Add the column list
1007 for (i
= 0; i
< noCols
; i
++)
1009 // If joining tables, the base table column names must be qualified to avoid ambiguity
1010 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1012 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1013 // pSqlStmt += queryTableName;
1014 pSqlStmt
+= wxT(".");
1016 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1017 // pSqlStmt += colDefs[i].ColName;
1019 pSqlStmt
+= wxT(",");
1022 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1023 // the ROWID if querying distinct records. The rowid will always be unique.
1024 if (!distinct
&& CanUpdByROWID())
1026 // If joining tables, the base table column names must be qualified to avoid ambiguity
1027 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1029 pSqlStmt
+= wxT(",");
1030 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1031 // pSqlStmt += queryTableName;
1032 pSqlStmt
+= wxT(".ROWID");
1035 pSqlStmt
+= wxT(",ROWID");
1038 // Append the FROM tablename portion
1039 pSqlStmt
+= wxT(" FROM ");
1040 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1041 // pSqlStmt += queryTableName;
1043 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1044 // The HOLDLOCK keyword follows the table name in the from clause.
1045 // Each table in the from clause must specify HOLDLOCK or
1046 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1047 // is parsed but ignored in SYBASE Transact-SQL.
1048 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1049 pSqlStmt
+= wxT(" HOLDLOCK");
1051 if (appendFromClause
)
1054 // Append the WHERE clause. Either append the where clause for the class
1055 // or build a where clause. The typeOfSelect determines this.
1056 switch(typeOfSelect
)
1058 case DB_SELECT_WHERE
:
1059 #if wxODBC_BACKWARD_COMPATABILITY
1060 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1062 if (where
.Length()) // May not want a where clause!!!
1065 pSqlStmt
+= wxT(" WHERE ");
1069 case DB_SELECT_KEYFIELDS
:
1070 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1071 if (whereClause
.Length())
1073 pSqlStmt
+= wxT(" WHERE ");
1074 pSqlStmt
+= whereClause
;
1077 case DB_SELECT_MATCHING
:
1078 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1079 if (whereClause
.Length())
1081 pSqlStmt
+= wxT(" WHERE ");
1082 pSqlStmt
+= whereClause
;
1087 // Append the ORDER BY clause
1088 #if wxODBC_BACKWARD_COMPATABILITY
1089 if (orderBy
&& wxStrlen(orderBy
))
1091 if (orderBy
.Length())
1094 pSqlStmt
+= wxT(" ORDER BY ");
1095 pSqlStmt
+= orderBy
;
1098 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1099 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1100 // HOLDLOCK for Sybase.
1101 if (selectForUpdate
&& CanSelectForUpdate())
1102 pSqlStmt
+= wxT(" FOR UPDATE");
1104 } // wxDbTable::BuildSelectStmt()
1107 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1108 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1110 wxString tempSqlStmt
;
1111 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1112 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1113 } // wxDbTable::BuildSelectStmt()
1116 /********** wxDbTable::BuildUpdateStmt() **********/
1117 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1119 wxASSERT(!queryOnly
);
1123 wxString whereClause
;
1124 whereClause
.Empty();
1126 bool firstColumn
= TRUE
;
1128 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1129 pDb
->SQLTableName(tableName
.c_str()).c_str());
1131 // Append a list of columns to be updated
1133 for (i
= 0; i
< noCols
; i
++)
1135 // Only append Updateable columns
1136 if (colDefs
[i
].Updateable
)
1139 pSqlStmt
+= wxT(",");
1141 firstColumn
= FALSE
;
1143 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1144 // pSqlStmt += colDefs[i].ColName;
1145 pSqlStmt
+= wxT(" = ?");
1149 // Append the WHERE clause to the SQL UPDATE statement
1150 pSqlStmt
+= wxT(" WHERE ");
1153 case DB_UPD_KEYFIELDS
:
1154 // If the datasource supports the ROWID column, build
1155 // the where on ROWID for efficiency purposes.
1156 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1157 if (CanUpdByROWID())
1160 wxChar rowid
[wxDB_ROWID_LEN
+1];
1162 // Get the ROWID value. If not successful retreiving the ROWID,
1163 // simply fall down through the code and build the WHERE clause
1164 // based on the key fields.
1165 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1167 pSqlStmt
+= wxT("ROWID = '");
1169 pSqlStmt
+= wxT("'");
1173 // Unable to delete by ROWID, so build a WHERE
1174 // clause based on the keyfields.
1175 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1176 pSqlStmt
+= whereClause
;
1179 pSqlStmt
+= pWhereClause
;
1182 } // BuildUpdateStmt()
1185 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1186 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1188 wxString tempSqlStmt
;
1189 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1190 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1191 } // BuildUpdateStmt()
1194 /********** wxDbTable::BuildWhereClause() **********/
1195 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1196 const wxString
&qualTableName
, bool useLikeComparison
)
1198 * Note: BuildWhereClause() currently ignores timestamp columns.
1199 * They are not included as part of the where clause.
1202 bool moreThanOneColumn
= FALSE
;
1205 // Loop through the columns building a where clause as you go
1207 for (i
= 0; i
< noCols
; i
++)
1209 // Determine if this column should be included in the WHERE clause
1210 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1211 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1213 // Skip over timestamp columns
1214 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1216 // If there is more than 1 column, join them with the keyword "AND"
1217 if (moreThanOneColumn
)
1218 pWhereClause
+= wxT(" AND ");
1220 moreThanOneColumn
= TRUE
;
1221 // Concatenate where phrase for the column
1222 if (qualTableName
.Length())
1224 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1225 // pWhereClause += qualTableName;
1226 pWhereClause
+= wxT(".");
1228 pWhereClause
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1229 // pWhereClause += colDefs[i].ColName;
1230 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1231 pWhereClause
+= wxT(" LIKE ");
1233 pWhereClause
+= wxT(" = ");
1234 switch(colDefs
[i
].SqlCtype
)
1237 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1240 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[i
].PtrDataObj
));
1243 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[i
].PtrDataObj
));
1246 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1249 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1252 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1255 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1258 pWhereClause
+= colValue
;
1261 } // wxDbTable::BuildWhereClause()
1264 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1265 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1266 const wxString
&qualTableName
, bool useLikeComparison
)
1268 wxString tempSqlStmt
;
1269 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1270 wxStrcpy(pWhereClause
, tempSqlStmt
);
1271 } // wxDbTable::BuildWhereClause()
1274 /********** wxDbTable::GetRowNum() **********/
1275 UWORD
wxDbTable::GetRowNum(void)
1279 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1281 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1285 // Completed successfully
1286 return((UWORD
) rowNum
);
1288 } // wxDbTable::GetRowNum()
1291 /********** wxDbTable::CloseCursor() **********/
1292 bool wxDbTable::CloseCursor(HSTMT cursor
)
1294 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1295 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1297 // Completed successfully
1300 } // wxDbTable::CloseCursor()
1303 /********** wxDbTable::CreateTable() **********/
1304 bool wxDbTable::CreateTable(bool attemptDrop
)
1312 #ifdef DBDEBUG_CONSOLE
1313 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1317 if (attemptDrop
&& !DropTable())
1321 #ifdef DBDEBUG_CONSOLE
1322 for (i
= 0; i
< noCols
; i
++)
1324 // Exclude derived columns since they are NOT part of the base table
1325 if (colDefs
[i
].DerivedCol
)
1327 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1328 switch(colDefs
[i
].DbDataType
)
1330 case DB_DATA_TYPE_VARCHAR
:
1331 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1333 case DB_DATA_TYPE_INTEGER
:
1334 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1336 case DB_DATA_TYPE_FLOAT
:
1337 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1339 case DB_DATA_TYPE_DATE
:
1340 cout
<< pDb
->GetTypeInfDate().TypeName
;
1342 case DB_DATA_TYPE_BLOB
:
1343 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1350 // Build a CREATE TABLE string from the colDefs structure.
1351 bool needComma
= FALSE
;
1353 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1354 pDb
->SQLTableName(tableName
.c_str()).c_str());
1356 for (i
= 0; i
< noCols
; i
++)
1358 // Exclude derived columns since they are NOT part of the base table
1359 if (colDefs
[i
].DerivedCol
)
1363 sqlStmt
+= wxT(",");
1365 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1366 // sqlStmt += colDefs[i].ColName;
1367 sqlStmt
+= wxT(" ");
1369 switch(colDefs
[i
].DbDataType
)
1371 case DB_DATA_TYPE_VARCHAR
:
1372 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1374 case DB_DATA_TYPE_INTEGER
:
1375 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1377 case DB_DATA_TYPE_FLOAT
:
1378 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1380 case DB_DATA_TYPE_DATE
:
1381 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1383 case DB_DATA_TYPE_BLOB
:
1384 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1387 // For varchars, append the size of the string
1388 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
&&
1389 (pDb
->Dbms() != dbmsMY_SQL
|| pDb
->GetTypeInfVarchar().TypeName
!= "text"))// ||
1390 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1393 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1397 if (pDb
->Dbms() == dbmsDB2
||
1398 pDb
->Dbms() == dbmsMY_SQL
||
1399 pDb
->Dbms() == dbmsSYBASE_ASE
||
1400 pDb
->Dbms() == dbmsINTERBASE
||
1401 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1403 if (colDefs
[i
].KeyField
)
1405 sqlStmt
+= wxT(" NOT NULL");
1411 // If there is a primary key defined, include it in the create statement
1412 for (i
= j
= 0; i
< noCols
; i
++)
1414 if (colDefs
[i
].KeyField
)
1420 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1422 switch (pDb
->Dbms())
1426 case dbmsSYBASE_ASA
:
1427 case dbmsSYBASE_ASE
:
1430 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1431 sqlStmt
+= wxT(",PRIMARY KEY (");
1436 sqlStmt
+= wxT(",CONSTRAINT ");
1437 // DB2 is limited to 18 characters for index names
1438 if (pDb
->Dbms() == dbmsDB2
)
1440 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."));
1441 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1442 // sqlStmt += tableName.substr(0, 13);
1445 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1446 // sqlStmt += tableName;
1448 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1453 // List column name(s) of column(s) comprising the primary key
1454 for (i
= j
= 0; i
< noCols
; i
++)
1456 if (colDefs
[i
].KeyField
)
1458 if (j
++) // Multi part key, comma separate names
1459 sqlStmt
+= wxT(",");
1460 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1462 if (pDb
->Dbms() == dbmsMY_SQL
&&
1463 colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1466 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1471 sqlStmt
+= wxT(")");
1473 if (pDb
->Dbms() == dbmsINFORMIX
||
1474 pDb
->Dbms() == dbmsSYBASE_ASA
||
1475 pDb
->Dbms() == dbmsSYBASE_ASE
)
1477 sqlStmt
+= wxT(" CONSTRAINT ");
1478 sqlStmt
+= pDb
->SQLTableName(tableName
);
1479 // sqlStmt += tableName;
1480 sqlStmt
+= wxT("_PIDX");
1483 // Append the closing parentheses for the create table statement
1484 sqlStmt
+= wxT(")");
1486 pDb
->WriteSqlLog(sqlStmt
);
1488 #ifdef DBDEBUG_CONSOLE
1489 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1492 // Execute the CREATE TABLE statement
1493 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1494 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1496 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1497 pDb
->RollbackTrans();
1502 // Commit the transaction and close the cursor
1503 if (!pDb
->CommitTrans())
1505 if (!CloseCursor(hstmt
))
1508 // Database table created successfully
1511 } // wxDbTable::CreateTable()
1514 /********** wxDbTable::DropTable() **********/
1515 bool wxDbTable::DropTable()
1517 // NOTE: This function returns TRUE if the Table does not exist, but
1518 // only for identified databases. Code will need to be added
1519 // below for any other databases when those databases are defined
1520 // to handle this situation consistently
1524 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1525 pDb
->SQLTableName(tableName
.c_str()).c_str());
1527 pDb
->WriteSqlLog(sqlStmt
);
1529 #ifdef DBDEBUG_CONSOLE
1530 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1533 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1534 if (retcode
!= SQL_SUCCESS
)
1536 // Check for "Base table not found" error and ignore
1537 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1538 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1539 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1541 // Check for product specific error codes
1542 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1543 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1544 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1545 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1547 pDb
->DispNextError();
1548 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1549 pDb
->RollbackTrans();
1550 // CloseCursor(hstmt);
1556 // Commit the transaction and close the cursor
1557 if (! pDb
->CommitTrans())
1559 if (! CloseCursor(hstmt
))
1563 } // wxDbTable::DropTable()
1566 /********** wxDbTable::CreateIndex() **********/
1567 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1568 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1572 // Drop the index first
1573 if (attemptDrop
&& !DropIndex(idxName
))
1576 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1577 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1578 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1579 // table was created, then months later you determine that an additional index while
1580 // give better performance, so you want to add an index).
1582 // The following block of code will modify the column definition to make the column be
1583 // defined with the "NOT NULL" qualifier.
1584 if (pDb
->Dbms() == dbmsMY_SQL
)
1589 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1593 // Find the column definition that has the ColName that matches the
1594 // index column name. We need to do this to get the DB_DATA_TYPE of
1595 // the index column, as MySQL's syntax for the ALTER column requires
1597 while (!found
&& (j
< this->noCols
))
1599 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1607 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1608 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1613 wxODBC_ERRORS retcode
;
1614 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1615 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1616 // This line is just here for debug checking of the value
1617 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1627 pDb
->RollbackTrans();
1632 // Build a CREATE INDEX statement
1633 sqlStmt
= wxT("CREATE ");
1635 sqlStmt
+= wxT("UNIQUE ");
1637 sqlStmt
+= wxT("INDEX ");
1638 sqlStmt
+= pDb
->SQLTableName(idxName
);
1639 sqlStmt
+= wxT(" ON ");
1641 sqlStmt
+= pDb
->SQLTableName(tableName
);
1642 // sqlStmt += tableName;
1643 sqlStmt
+= wxT(" (");
1645 // Append list of columns making up index
1647 for (i
= 0; i
< noIdxCols
; i
++)
1649 sqlStmt
+= pDb
->SQLColumnName(pIdxDefs
[i
].ColName
);
1650 // sqlStmt += pIdxDefs[i].ColName;
1652 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1653 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (strncmp(pDb
->dbInf
.dbmsVer
,"07",2)==0)) &&
1654 !(pDb
->Dbms() == dbmsPOSTGRES
))
1656 if (pIdxDefs
[i
].Ascending
)
1657 sqlStmt
+= wxT(" ASC");
1659 sqlStmt
+= wxT(" DESC");
1662 wxASSERT_MSG(pIdxDefs
[i
].Ascending
, "Datasource does not support DESCending index columns");
1664 if ((i
+ 1) < noIdxCols
)
1665 sqlStmt
+= wxT(",");
1668 // Append closing parentheses
1669 sqlStmt
+= wxT(")");
1671 pDb
->WriteSqlLog(sqlStmt
);
1673 #ifdef DBDEBUG_CONSOLE
1674 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1677 // Execute the CREATE INDEX statement
1678 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1680 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1681 pDb
->RollbackTrans();
1686 // Commit the transaction and close the cursor
1687 if (! pDb
->CommitTrans())
1689 if (! CloseCursor(hstmt
))
1692 // Index Created Successfully
1695 } // wxDbTable::CreateIndex()
1698 /********** wxDbTable::DropIndex() **********/
1699 bool wxDbTable::DropIndex(const wxString
&idxName
)
1701 // NOTE: This function returns TRUE if the Index does not exist, but
1702 // only for identified databases. Code will need to be added
1703 // below for any other databases when those databases are defined
1704 // to handle this situation consistently
1708 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1709 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1710 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1711 pDb
->SQLTableName(idxName
.c_str()).c_str(),
1712 pDb
->SQLTableName(tableName
.c_str()).c_str());
1713 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1714 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1715 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1716 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1717 pDb
->SQLTableName(idxName
.c_str()).c_str());
1719 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1720 pDb
->SQLTableName(idxName
.c_str()).c_str());
1722 pDb
->WriteSqlLog(sqlStmt
);
1724 #ifdef DBDEBUG_CONSOLE
1725 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1728 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1730 // Check for "Index not found" error and ignore
1731 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1732 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1734 // Check for product specific error codes
1735 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1736 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1737 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1738 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1739 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1740 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1741 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1744 pDb
->DispNextError();
1745 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1746 pDb
->RollbackTrans();
1753 // Commit the transaction and close the cursor
1754 if (! pDb
->CommitTrans())
1756 if (! CloseCursor(hstmt
))
1760 } // wxDbTable::DropIndex()
1763 /********** wxDbTable::SetOrderByColNums() **********/
1764 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1766 int colNo
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1772 va_start(argptr
, first
); /* Initialize variable arguments. */
1773 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1775 // Make sure the passed in column number
1776 // is within the valid range of columns
1778 // Valid columns are 0 thru noCols-1
1779 if (colNo
>= noCols
|| colNo
< 0)
1786 tempStr
+= wxT(",");
1788 tempStr
+= colDefs
[colNo
].ColName
;
1789 colNo
= va_arg (argptr
, int);
1791 va_end (argptr
); /* Reset variable arguments. */
1793 SetOrderByClause(tempStr
);
1796 } // wxDbTable::SetOrderByColNums()
1799 /********** wxDbTable::Insert() **********/
1800 int wxDbTable::Insert(void)
1802 wxASSERT(!queryOnly
);
1803 if (queryOnly
|| !insertable
)
1808 // Insert the record by executing the already prepared insert statement
1810 retcode
=SQLExecute(hstmtInsert
);
1811 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1813 // Check to see if integrity constraint was violated
1814 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1815 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1816 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1819 pDb
->DispNextError();
1820 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1825 // Record inserted into the datasource successfully
1828 } // wxDbTable::Insert()
1831 /********** wxDbTable::Update() **********/
1832 bool wxDbTable::Update(void)
1834 wxASSERT(!queryOnly
);
1840 // Build the SQL UPDATE statement
1841 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1843 pDb
->WriteSqlLog(sqlStmt
);
1845 #ifdef DBDEBUG_CONSOLE
1846 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1849 // Execute the SQL UPDATE statement
1850 return(execUpdate(sqlStmt
));
1852 } // wxDbTable::Update()
1855 /********** wxDbTable::Update(pSqlStmt) **********/
1856 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1858 wxASSERT(!queryOnly
);
1862 pDb
->WriteSqlLog(pSqlStmt
);
1864 return(execUpdate(pSqlStmt
));
1866 } // wxDbTable::Update(pSqlStmt)
1869 /********** wxDbTable::UpdateWhere() **********/
1870 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1872 wxASSERT(!queryOnly
);
1878 // Build the SQL UPDATE statement
1879 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1881 pDb
->WriteSqlLog(sqlStmt
);
1883 #ifdef DBDEBUG_CONSOLE
1884 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1887 // Execute the SQL UPDATE statement
1888 return(execUpdate(sqlStmt
));
1890 } // wxDbTable::UpdateWhere()
1893 /********** wxDbTable::Delete() **********/
1894 bool wxDbTable::Delete(void)
1896 wxASSERT(!queryOnly
);
1903 // Build the SQL DELETE statement
1904 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1906 pDb
->WriteSqlLog(sqlStmt
);
1908 // Execute the SQL DELETE statement
1909 return(execDelete(sqlStmt
));
1911 } // wxDbTable::Delete()
1914 /********** wxDbTable::DeleteWhere() **********/
1915 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
1917 wxASSERT(!queryOnly
);
1924 // Build the SQL DELETE statement
1925 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1927 pDb
->WriteSqlLog(sqlStmt
);
1929 // Execute the SQL DELETE statement
1930 return(execDelete(sqlStmt
));
1932 } // wxDbTable::DeleteWhere()
1935 /********** wxDbTable::DeleteMatching() **********/
1936 bool wxDbTable::DeleteMatching(void)
1938 wxASSERT(!queryOnly
);
1945 // Build the SQL DELETE statement
1946 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1948 pDb
->WriteSqlLog(sqlStmt
);
1950 // Execute the SQL DELETE statement
1951 return(execDelete(sqlStmt
));
1953 } // wxDbTable::DeleteMatching()
1956 /********** wxDbTable::IsColNull() **********/
1957 bool wxDbTable::IsColNull(UWORD colNo
) const
1960 This logic is just not right. It would indicate TRUE
1961 if a numeric field were set to a value of 0.
1963 switch(colDefs[colNo].SqlCtype)
1966 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1968 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1970 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1972 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1974 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1976 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1978 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1979 case SQL_C_TIMESTAMP:
1980 TIMESTAMP_STRUCT *pDt;
1981 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1982 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1990 return (colDefs
[colNo
].Null
);
1991 } // wxDbTable::IsColNull()
1994 /********** wxDbTable::CanSelectForUpdate() **********/
1995 bool wxDbTable::CanSelectForUpdate(void)
2000 if (pDb
->Dbms() == dbmsMY_SQL
)
2003 if ((pDb
->Dbms() == dbmsORACLE
) ||
2004 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
2009 } // wxDbTable::CanSelectForUpdate()
2012 /********** wxDbTable::CanUpdByROWID() **********/
2013 bool wxDbTable::CanUpdByROWID(void)
2016 * NOTE: Returning FALSE for now until this can be debugged,
2017 * as the ROWID is not getting updated correctly
2021 if (pDb->Dbms() == dbmsORACLE)
2026 } // wxDbTable::CanUpdByROWID()
2029 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2030 bool wxDbTable::IsCursorClosedOnCommit(void)
2032 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2037 } // wxDbTable::IsCursorClosedOnCommit()
2041 /********** wxDbTable::ClearMemberVar() **********/
2042 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2044 wxASSERT(colNo
< noCols
);
2046 switch(colDefs
[colNo
].SqlCtype
)
2049 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2052 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2055 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2058 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2061 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2064 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2067 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2069 case SQL_C_TIMESTAMP
:
2070 TIMESTAMP_STRUCT
*pDt
;
2071 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2084 } // wxDbTable::ClearMemberVar()
2087 /********** wxDbTable::ClearMemberVars() **********/
2088 void wxDbTable::ClearMemberVars(bool setToNull
)
2092 // Loop through the columns setting each member variable to zero
2093 for (i
=0; i
< noCols
; i
++)
2094 ClearMemberVar(i
,setToNull
);
2096 } // wxDbTable::ClearMemberVars()
2099 /********** wxDbTable::SetQueryTimeout() **********/
2100 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2102 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2103 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2104 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2105 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2106 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2107 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2108 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2109 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2111 // Completed Successfully
2114 } // wxDbTable::SetQueryTimeout()
2117 /********** wxDbTable::SetColDefs() **********/
2118 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2119 SWORD cType
, int size
, bool keyField
, bool upd
,
2120 bool insAllow
, bool derivedCol
)
2122 wxASSERT_MSG( index
< noCols
,
2123 _T("Specified column index exceeds the maximum number of columns for this table.") );
2125 if (!colDefs
) // May happen if the database connection fails
2128 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2130 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2131 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2135 tmpMsg
.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2136 fieldName
.c_str(),colDefs
[index
].ColName
);
2138 #endif // __WXDEBUG__
2141 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2143 colDefs
[index
].DbDataType
= dataType
;
2144 colDefs
[index
].PtrDataObj
= pData
;
2145 colDefs
[index
].SqlCtype
= cType
;
2146 colDefs
[index
].SzDataObj
= size
;
2147 colDefs
[index
].KeyField
= keyField
;
2148 colDefs
[index
].DerivedCol
= derivedCol
;
2149 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2152 colDefs
[index
].Updateable
= FALSE
;
2153 colDefs
[index
].InsertAllowed
= FALSE
;
2157 colDefs
[index
].Updateable
= upd
;
2158 colDefs
[index
].InsertAllowed
= insAllow
;
2161 colDefs
[index
].Null
= FALSE
;
2163 } // wxDbTable::SetColDefs()
2166 /********** wxDbTable::SetColDefs() **********/
2167 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2170 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2176 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2178 for (index
= 0; index
< numCols
; index
++)
2180 // Process the fields
2181 switch (pColInfs
[index
].dbDataType
)
2183 case DB_DATA_TYPE_VARCHAR
:
2184 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2185 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2186 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2188 case DB_DATA_TYPE_INTEGER
:
2189 // Can be long or short
2190 if (pColInfs
[index
].bufferLength
== sizeof(long))
2192 pColDataPtrs
[index
].PtrDataObj
= new long;
2193 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2194 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2198 pColDataPtrs
[index
].PtrDataObj
= new short;
2199 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2200 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2203 case DB_DATA_TYPE_FLOAT
:
2204 // Can be float or double
2205 if (pColInfs
[index
].bufferLength
== sizeof(float))
2207 pColDataPtrs
[index
].PtrDataObj
= new float;
2208 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2209 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2213 pColDataPtrs
[index
].PtrDataObj
= new double;
2214 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2215 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2218 case DB_DATA_TYPE_DATE
:
2219 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2220 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2221 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2223 case DB_DATA_TYPE_BLOB
:
2224 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2225 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2226 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2227 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2230 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2231 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2234 // Unable to build all the column definitions, as either one of
2235 // the calls to "new" failed above, or there was a BLOB field
2236 // to have a column definition for. If BLOBs are to be used,
2237 // the other form of ::SetColDefs() must be used, as it is impossible
2238 // to know the maximum size to create the PtrDataObj to be.
2239 delete [] pColDataPtrs
;
2245 return (pColDataPtrs
);
2247 } // wxDbTable::SetColDefs()
2250 /********** wxDbTable::SetCursor() **********/
2251 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2253 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2254 hstmt
= *hstmtDefault
;
2256 hstmt
= *hstmtActivate
;
2258 } // wxDbTable::SetCursor()
2261 /********** wxDbTable::Count(const wxString &) **********/
2262 ULONG
wxDbTable::Count(const wxString
&args
)
2268 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2269 sqlStmt
= wxT("SELECT COUNT(");
2271 sqlStmt
+= wxT(") FROM ");
2272 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2273 // sqlStmt += queryTableName;
2274 #if wxODBC_BACKWARD_COMPATABILITY
2275 if (from
&& wxStrlen(from
))
2281 // Add the where clause if one is provided
2282 #if wxODBC_BACKWARD_COMPATABILITY
2283 if (where
&& wxStrlen(where
))
2288 sqlStmt
+= wxT(" WHERE ");
2292 pDb
->WriteSqlLog(sqlStmt
);
2294 // Initialize the Count cursor if it's not already initialized
2297 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2298 wxASSERT(hstmtCount
);
2303 // Execute the SQL statement
2304 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2306 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2311 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2313 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2317 // Obtain the result
2318 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2320 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2325 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2326 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2328 // Return the record count
2331 } // wxDbTable::Count()
2334 /********** wxDbTable::Refresh() **********/
2335 bool wxDbTable::Refresh(void)
2339 // Switch to the internal cursor so any active cursors are not corrupted
2340 HSTMT currCursor
= GetCursor();
2341 hstmt
= hstmtInternal
;
2342 #if wxODBC_BACKWARD_COMPATABILITY
2343 // Save the where and order by clauses
2344 char *saveWhere
= where
;
2345 char *saveOrderBy
= orderBy
;
2347 wxString saveWhere
= where
;
2348 wxString saveOrderBy
= orderBy
;
2350 // Build a where clause to refetch the record with. Try and use the
2351 // ROWID if it's available, ow use the key fields.
2352 wxString whereClause
;
2353 whereClause
.Empty();
2355 if (CanUpdByROWID())
2358 wxChar rowid
[wxDB_ROWID_LEN
+1];
2360 // Get the ROWID value. If not successful retreiving the ROWID,
2361 // simply fall down through the code and build the WHERE clause
2362 // based on the key fields.
2363 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2365 whereClause
+= pDb
->SQLTableName(queryTableName
);
2366 // whereClause += queryTableName;
2367 whereClause
+= wxT(".ROWID = '");
2368 whereClause
+= rowid
;
2369 whereClause
+= wxT("'");
2373 // If unable to use the ROWID, build a where clause from the keyfields
2374 if (wxStrlen(whereClause
) == 0)
2375 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2377 // Requery the record
2378 where
= whereClause
;
2383 if (result
&& !GetNext())
2386 // Switch back to original cursor
2387 SetCursor(&currCursor
);
2389 // Free the internal cursor
2390 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2391 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2393 // Restore the original where and order by clauses
2395 orderBy
= saveOrderBy
;
2399 } // wxDbTable::Refresh()
2402 /********** wxDbTable::SetColNull() **********/
2403 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2407 colDefs
[colNo
].Null
= set
;
2408 if (set
) // Blank out the values in the member variable
2409 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2415 } // wxDbTable::SetColNull()
2418 /********** wxDbTable::SetColNull() **********/
2419 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2422 for (i
= 0; i
< noCols
; i
++)
2424 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2430 colDefs
[i
].Null
= set
;
2431 if (set
) // Blank out the values in the member variable
2432 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2438 } // wxDbTable::SetColNull()
2441 /********** wxDbTable::GetNewCursor() **********/
2442 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2444 HSTMT
*newHSTMT
= new HSTMT
;
2449 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2451 pDb
->DispAllErrors(henv
, hdbc
);
2456 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2458 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2465 if (!bindCols(*newHSTMT
))
2473 SetCursor(newHSTMT
);
2477 } // wxDbTable::GetNewCursor()
2480 /********** wxDbTable::DeleteCursor() **********/
2481 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2485 if (!hstmtDel
) // Cursor already deleted
2489 ODBC 3.0 says to use this form
2490 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2493 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2495 pDb
->DispAllErrors(henv
, hdbc
);
2503 } // wxDbTable::DeleteCursor()
2505 //////////////////////////////////////////////////////////////
2506 // wxDbGrid support functions
2507 //////////////////////////////////////////////////////////////
2509 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2511 if (!m_hstmtGridQuery
)
2513 m_hstmtGridQuery
= GetNewCursor(FALSE
,FALSE
);
2514 if (!bindCols(*m_hstmtGridQuery
))
2518 m_rowmode
= rowmode
;
2521 case WX_ROW_MODE_QUERY
:
2522 SetCursor(m_hstmtGridQuery
);
2524 case WX_ROW_MODE_INDIVIDUAL
:
2525 SetCursor(hstmtDefault
);
2530 } // wxDbTable::SetRowMode()
2533 wxVariant
wxDbTable::GetCol(const int colNo
) const
2536 if ((colNo
< noCols
) && (!IsColNull(colNo
)))
2538 switch (colDefs
[colNo
].SqlCtype
)
2542 val
= (wxChar
*)(colDefs
[colNo
].PtrDataObj
);
2546 val
= *(long *)(colDefs
[colNo
].PtrDataObj
);
2550 val
= (long int )(*(short *)(colDefs
[colNo
].PtrDataObj
));
2553 val
= (long)(*(unsigned long *)(colDefs
[colNo
].PtrDataObj
));
2556 val
= (long)(*(char *)(colDefs
[colNo
].PtrDataObj
));
2558 case SQL_C_UTINYINT
:
2559 val
= (long)(*(unsigned char *)(colDefs
[colNo
].PtrDataObj
));
2562 val
= (long)(*(UWORD
*)(colDefs
[colNo
].PtrDataObj
));
2565 val
= (DATE_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2568 val
= (TIME_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2570 case SQL_C_TIMESTAMP
:
2571 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2574 val
= *(double *)(colDefs
[colNo
].PtrDataObj
);
2581 } // wxDbTable::GetCol()
2584 void csstrncpyt(char *s
, const char *t
, int n
)
2586 while ((*s
++ = *t
++) && --n
)
2592 void wxDbTable::SetCol(const int colNo
, const wxVariant val
)
2594 //FIXME: Add proper wxDateTime support to wxVariant..
2597 SetColNull(colNo
, val
.IsNull());
2601 if ((colDefs
[colNo
].SqlCtype
== SQL_C_DATE
)
2602 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIME
)
2603 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
))
2605 //Returns null if invalid!
2606 if (!dateval
.ParseDate(val
.GetString()))
2607 SetColNull(colNo
, TRUE
);
2610 switch (colDefs
[colNo
].SqlCtype
)
2614 csstrncpyt((char *)(colDefs
[colNo
].PtrDataObj
),
2615 val
.GetString().c_str(),
2616 colDefs
[colNo
].SzDataObj
-1);
2620 *(long *)(colDefs
[colNo
].PtrDataObj
) = val
;
2624 *(short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2627 *(unsigned long *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2630 *(char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2632 case SQL_C_UTINYINT
:
2633 *(unsigned char *)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2636 *(unsigned short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2638 //FIXME: Add proper wxDateTime support to wxVariant..
2641 DATE_STRUCT
*dataptr
=
2642 (DATE_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2644 dataptr
->year
= dateval
.GetYear();
2645 dataptr
->month
= dateval
.GetMonth()+1;
2646 dataptr
->day
= dateval
.GetDay();
2651 TIME_STRUCT
*dataptr
=
2652 (TIME_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2654 dataptr
->hour
= dateval
.GetHour();
2655 dataptr
->minute
= dateval
.GetMinute();
2656 dataptr
->second
= dateval
.GetSecond();
2659 case SQL_C_TIMESTAMP
:
2661 TIMESTAMP_STRUCT
*dataptr
=
2662 (TIMESTAMP_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2663 dataptr
->year
= dateval
.GetYear();
2664 dataptr
->month
= dateval
.GetMonth()+1;
2665 dataptr
->day
= dateval
.GetDay();
2667 dataptr
->hour
= dateval
.GetHour();
2668 dataptr
->minute
= dateval
.GetMinute();
2669 dataptr
->second
= dateval
.GetSecond();
2673 *(double *)(colDefs
[colNo
].PtrDataObj
) = val
;
2678 } // if (!val.IsNull())
2679 } // wxDbTable::SetCol()
2682 GenericKey
wxDbTable::GetKey()
2687 blk
= malloc(m_keysize
);
2688 blkptr
= (wxChar
*) blk
;
2691 for (i
=0; i
< noCols
; i
++)
2693 if (colDefs
[i
].KeyField
)
2695 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2696 blkptr
+= colDefs
[i
].SzDataObj
;
2700 GenericKey k
= GenericKey(blk
, m_keysize
);
2704 } // wxDbTable::GetKey()
2707 void wxDbTable::SetKey(const GenericKey
& k
)
2713 blkptr
= (wxChar
*)blk
;
2716 for (i
=0; i
< noCols
; i
++)
2718 if (colDefs
[i
].KeyField
)
2720 SetColNull(i
, FALSE
);
2721 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2722 blkptr
+= colDefs
[i
].SzDataObj
;
2725 } // wxDbTable::SetKey()
2728 #endif // wxUSE_ODBC