1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dbtable.cpp
3 // Purpose: Implementation of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence
12 ///////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
23 #include "wx/object.h"
25 #include "wx/string.h"
30 #ifdef DBDEBUG_CONSOLE
31 #include "wx/ioswrap.h"
34 #include "wx/filefn.h"
40 #include "wx/dbtable.h"
42 ULONG lastTableID
= 0;
46 #include "wx/thread.h"
49 wxCriticalSection csTablesInUse
;
53 void csstrncpyt(wxChar
*target
, const wxChar
*source
, int n
)
55 while ( (*target
++ = *source
++) != '\0' && --n
!= 0 )
63 /********** wxDbColDef::wxDbColDef() Constructor **********/
64 wxDbColDef::wxDbColDef()
70 bool wxDbColDef::Initialize()
73 DbDataType
= DB_DATA_TYPE_INTEGER
;
74 SqlCtype
= SQL_C_LONG
;
79 InsertAllowed
= false;
85 } // wxDbColDef::Initialize()
88 /********** wxDbTable::wxDbTable() Constructor **********/
89 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
90 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
92 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
94 } // wxDbTable::wxDbTable()
97 /********** wxDbTable::~wxDbTable() **********/
98 wxDbTable::~wxDbTable()
101 } // wxDbTable::~wxDbTable()
104 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
105 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
107 // Initializing member variables
108 pDb
= pwxDb
; // Pointer to the wxDb object
112 m_hstmtGridQuery
= 0;
113 hstmtDefault
= 0; // Initialized below
114 hstmtCount
= 0; // Initialized first time it is needed
121 m_numCols
= numColumns
; // Number of columns in the table
122 where
.Empty(); // Where clause
123 orderBy
.Empty(); // Order By clause
124 from
.Empty(); // From clause
125 selectForUpdate
= false; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
130 queryTableName
.Empty();
132 wxASSERT(tblName
.length());
138 tableName
= tblName
; // Table Name
139 if ((pDb
->Dbms() == dbmsORACLE
) ||
140 (pDb
->Dbms() == dbmsFIREBIRD
) ||
141 (pDb
->Dbms() == dbmsINTERBASE
))
142 tableName
= tableName
.Upper();
144 if (tblPath
.length())
145 tablePath
= tblPath
; // Table Path - used for dBase files
149 if (qryTblName
.length()) // Name of the table/view to query
150 queryTableName
= qryTblName
;
152 queryTableName
= tblName
;
154 if ((pDb
->Dbms() == dbmsORACLE
) ||
155 (pDb
->Dbms() == dbmsFIREBIRD
) ||
156 (pDb
->Dbms() == dbmsINTERBASE
))
157 queryTableName
= queryTableName
.Upper();
159 pDb
->incrementTableCount();
162 tableID
= ++lastTableID
;
163 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"),
164 tblName
.c_str(), tableID
, wx_static_cast(void*, pDb
));
167 wxTablesInUse
*tableInUse
;
168 tableInUse
= new wxTablesInUse();
169 tableInUse
->tableName
= tblName
;
170 tableInUse
->tableID
= tableID
;
171 tableInUse
->pDb
= pDb
;
173 wxCriticalSectionLocker
lock(csTablesInUse
);
174 TablesInUse
.Append(tableInUse
);
180 // Grab the HENV and HDBC from the wxDb object
181 henv
= pDb
->GetHENV();
182 hdbc
= pDb
->GetHDBC();
184 // Allocate space for column definitions
186 colDefs
= new wxDbColDef
[m_numCols
]; // Points to the first column definition
188 // Allocate statement handles for the table
191 // Allocate a separate statement handle for performing inserts
192 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
193 pDb
->DispAllErrors(henv
, hdbc
);
194 // Allocate a separate statement handle for performing deletes
195 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
196 pDb
->DispAllErrors(henv
, hdbc
);
197 // Allocate a separate statement handle for performing updates
198 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
199 pDb
->DispAllErrors(henv
, hdbc
);
201 // Allocate a separate statement handle for internal use
202 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
203 pDb
->DispAllErrors(henv
, hdbc
);
205 // Set the cursor type for the statement handles
206 cursorType
= SQL_CURSOR_STATIC
;
208 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
210 // Check to see if cursor type is supported
211 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
212 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
214 // Datasource does not support static cursors. Driver
215 // will substitute a cursor type. Call SQLGetStmtOption()
216 // to determine which cursor type was selected.
217 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
218 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
219 #ifdef DBDEBUG_CONSOLE
220 cout
<< wxT("Static cursor changed to: ");
223 case SQL_CURSOR_FORWARD_ONLY
:
224 cout
<< wxT("Forward Only");
226 case SQL_CURSOR_STATIC
:
227 cout
<< wxT("Static");
229 case SQL_CURSOR_KEYSET_DRIVEN
:
230 cout
<< wxT("Keyset Driven");
232 case SQL_CURSOR_DYNAMIC
:
233 cout
<< wxT("Dynamic");
236 cout
<< endl
<< endl
;
239 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
241 // Force the use of a forward only cursor...
242 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
243 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
245 // Should never happen
246 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
253 pDb
->DispNextError();
254 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
257 #ifdef DBDEBUG_CONSOLE
259 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
264 // Set the cursor type for the INSERT statement handle
265 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
266 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
267 // Set the cursor type for the DELETE statement handle
268 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
269 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
270 // Set the cursor type for the UPDATE statement handle
271 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
272 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
275 // Make the default cursor the active cursor
276 hstmtDefault
= GetNewCursor(false,false);
277 wxASSERT(hstmtDefault
);
278 hstmt
= *hstmtDefault
;
282 } // wxDbTable::initialize()
285 void wxDbTable::cleanup()
290 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"),
291 tableName
.c_str(), tableID
, wx_static_cast(void*, pDb
));
300 wxList::compatibility_iterator pNode
;
302 wxCriticalSectionLocker
lock(csTablesInUse
);
303 pNode
= TablesInUse
.GetFirst();
304 while (!found
&& pNode
)
306 if (((wxTablesInUse
*)pNode
->GetData())->tableID
== tableID
)
309 delete (wxTablesInUse
*)pNode
->GetData();
310 TablesInUse
.Erase(pNode
);
313 pNode
= pNode
->GetNext();
319 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
320 wxLogDebug (msg
,wxT("NOTICE..."));
325 // Decrement the wxDb table count
327 pDb
->decrementTableCount();
329 // Delete memory allocated for column definitions
333 // Free statement handles
339 ODBC 3.0 says to use this form
340 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
342 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
343 pDb
->DispAllErrors(henv
, hdbc
);
349 ODBC 3.0 says to use this form
350 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
352 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
353 pDb
->DispAllErrors(henv
, hdbc
);
359 ODBC 3.0 says to use this form
360 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
362 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
363 pDb
->DispAllErrors(henv
, hdbc
);
369 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
370 pDb
->DispAllErrors(henv
, hdbc
);
373 // Delete dynamically allocated cursors
375 DeleteCursor(hstmtDefault
);
378 DeleteCursor(hstmtCount
);
380 if (m_hstmtGridQuery
)
381 DeleteCursor(m_hstmtGridQuery
);
383 } // wxDbTable::cleanup()
386 /***************************** PRIVATE FUNCTIONS *****************************/
389 void wxDbTable::setCbValueForColumn(int columnIndex
)
391 switch(colDefs
[columnIndex
].DbDataType
)
393 case DB_DATA_TYPE_VARCHAR
:
394 case DB_DATA_TYPE_MEMO
:
395 if (colDefs
[columnIndex
].Null
)
396 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
398 colDefs
[columnIndex
].CbValue
= SQL_NTS
;
400 case DB_DATA_TYPE_INTEGER
:
401 if (colDefs
[columnIndex
].Null
)
402 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
404 colDefs
[columnIndex
].CbValue
= 0;
406 case DB_DATA_TYPE_FLOAT
:
407 if (colDefs
[columnIndex
].Null
)
408 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
410 colDefs
[columnIndex
].CbValue
= 0;
412 case DB_DATA_TYPE_DATE
:
413 if (colDefs
[columnIndex
].Null
)
414 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
416 colDefs
[columnIndex
].CbValue
= 0;
418 case DB_DATA_TYPE_BLOB
:
419 if (colDefs
[columnIndex
].Null
)
420 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
422 if (colDefs
[columnIndex
].SqlCtype
== SQL_C_WXCHAR
)
423 colDefs
[columnIndex
].CbValue
= SQL_NTS
;
425 colDefs
[columnIndex
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[columnIndex
].SzDataObj
);
430 /********** wxDbTable::bindParams() **********/
431 bool wxDbTable::bindParams(bool forUpdate
)
433 wxASSERT(!queryOnly
);
438 SDWORD precision
= 0;
441 // Bind each column of the table that should be bound
442 // to a parameter marker
446 for (i
=0, colNumber
=1; i
< m_numCols
; i
++)
450 if (!colDefs
[i
].Updateable
)
455 if (!colDefs
[i
].InsertAllowed
)
459 switch(colDefs
[i
].DbDataType
)
461 case DB_DATA_TYPE_VARCHAR
:
462 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
463 precision
= colDefs
[i
].SzDataObj
;
466 case DB_DATA_TYPE_MEMO
:
467 fSqlType
= pDb
->GetTypeInfMemo().FsqlType
;
468 precision
= colDefs
[i
].SzDataObj
;
471 case DB_DATA_TYPE_INTEGER
:
472 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
473 precision
= pDb
->GetTypeInfInteger().Precision
;
476 case DB_DATA_TYPE_FLOAT
:
477 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
478 precision
= pDb
->GetTypeInfFloat().Precision
;
479 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
480 // SQL Sybase Anywhere v5.5 returned a negative number for the
481 // MaxScale. This caused ODBC to kick out an error on ibscale.
482 // I check for this here and set the scale = precision.
484 // scale = (short) precision;
486 case DB_DATA_TYPE_DATE
:
487 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
488 precision
= pDb
->GetTypeInfDate().Precision
;
491 case DB_DATA_TYPE_BLOB
:
492 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
493 precision
= colDefs
[i
].SzDataObj
;
498 setCbValueForColumn(i
);
502 if (SQLBindParameter(hstmtUpdate
, colNumber
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
503 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
504 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
506 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
511 if (SQLBindParameter(hstmtInsert
, colNumber
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
512 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
513 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
515 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
520 // Completed successfully
523 } // wxDbTable::bindParams()
526 /********** wxDbTable::bindInsertParams() **********/
527 bool wxDbTable::bindInsertParams(void)
529 return bindParams(false);
530 } // wxDbTable::bindInsertParams()
533 /********** wxDbTable::bindUpdateParams() **********/
534 bool wxDbTable::bindUpdateParams(void)
536 return bindParams(true);
537 } // wxDbTable::bindUpdateParams()
540 /********** wxDbTable::bindCols() **********/
541 bool wxDbTable::bindCols(HSTMT cursor
)
543 // Bind each column of the table to a memory address for fetching data
545 for (i
= 0; i
< m_numCols
; i
++)
547 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
548 colDefs
[i
].SzDataObj
, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
549 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
552 // Completed successfully
554 } // wxDbTable::bindCols()
557 /********** wxDbTable::getRec() **********/
558 bool wxDbTable::getRec(UWORD fetchType
)
562 if (!pDb
->FwdOnlyCursors())
564 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
565 SQLULEN cRowsFetched
;
568 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
569 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
571 if (retcode
== SQL_NO_DATA_FOUND
)
574 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
578 // Set the Null member variable to indicate the Null state
579 // of each column just read in.
581 for (i
= 0; i
< m_numCols
; i
++)
582 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
587 // Fetch the next record from the record set
588 retcode
= SQLFetch(hstmt
);
589 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
591 if (retcode
== SQL_NO_DATA_FOUND
)
594 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
598 // Set the Null member variable to indicate the Null state
599 // of each column just read in.
601 for (i
= 0; i
< m_numCols
; i
++)
602 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
606 // Completed successfully
609 } // wxDbTable::getRec()
612 /********** wxDbTable::execDelete() **********/
613 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
617 // Execute the DELETE statement
618 retcode
= SQLExecDirect(hstmtDelete
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
620 if (retcode
== SQL_SUCCESS
||
621 retcode
== SQL_NO_DATA_FOUND
||
622 retcode
== SQL_SUCCESS_WITH_INFO
)
624 // Record deleted successfully
628 // Problem deleting record
629 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
631 } // wxDbTable::execDelete()
634 /********** wxDbTable::execUpdate() **********/
635 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
639 // Execute the UPDATE statement
640 retcode
= SQLExecDirect(hstmtUpdate
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
642 if (retcode
== SQL_SUCCESS
||
643 retcode
== SQL_NO_DATA_FOUND
||
644 retcode
== SQL_SUCCESS_WITH_INFO
)
646 // Record updated successfully
649 else if (retcode
== SQL_NEED_DATA
)
652 retcode
= SQLParamData(hstmtUpdate
, &pParmID
);
653 while (retcode
== SQL_NEED_DATA
)
655 // Find the parameter
657 for (i
=0; i
< m_numCols
; i
++)
659 if (colDefs
[i
].PtrDataObj
== pParmID
)
661 // We found it. Store the parameter.
662 retcode
= SQLPutData(hstmtUpdate
, pParmID
, colDefs
[i
].SzDataObj
);
663 if (retcode
!= SQL_SUCCESS
)
665 pDb
->DispNextError();
666 return pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
671 retcode
= SQLParamData(hstmtUpdate
, &pParmID
);
673 if (retcode
== SQL_SUCCESS
||
674 retcode
== SQL_NO_DATA_FOUND
||
675 retcode
== SQL_SUCCESS_WITH_INFO
)
677 // Record updated successfully
682 // Problem updating record
683 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
685 } // wxDbTable::execUpdate()
688 /********** wxDbTable::query() **********/
689 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
694 // The user may wish to select for update, but the DBMS may not be capable
695 selectForUpdate
= CanSelectForUpdate();
697 selectForUpdate
= false;
699 // Set the SQL SELECT string
700 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
701 { // so generate a select statement.
702 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
703 pDb
->WriteSqlLog(sqlStmt
);
706 // Make sure the cursor is closed first
707 if (!CloseCursor(hstmt
))
710 // Execute the SQL SELECT statement
712 retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
713 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
714 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
716 // Completed successfully
719 } // wxDbTable::query()
722 /***************************** PUBLIC FUNCTIONS *****************************/
725 /********** wxDbTable::Open() **********/
726 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
735 // Calculate the maximum size of the concatenated
736 // keys for use with wxDbGrid
738 for (i
=0; i
< m_numCols
; i
++)
740 if (colDefs
[i
].KeyField
)
742 m_keysize
+= colDefs
[i
].SzDataObj
;
749 if (checkTableExists
)
751 if (pDb
->Dbms() == dbmsPOSTGRES
)
752 exists
= pDb
->TableExists(tableName
, NULL
, tablePath
);
754 exists
= pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
);
757 // Verify that the table exists in the database
760 s
= wxT("Table/view does not exist in the database");
761 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
762 s
+= wxT(", or you have no permissions.\n");
766 else if (checkPrivileges
)
768 // Verify the user has rights to access the table.
769 bool hasPrivs
wxDUMMY_INITIALIZE(true);
771 if (pDb
->Dbms() == dbmsPOSTGRES
)
772 hasPrivs
= pDb
->TablePrivileges(tableName
, wxT("SELECT"), pDb
->GetUsername(), NULL
, tablePath
);
774 hasPrivs
= pDb
->TablePrivileges(tableName
, wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
);
777 s
= wxT("Connecting user does not have sufficient privileges to access this table.\n");
784 if (!tablePath
.empty())
785 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
787 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
790 pDb
->LogError(p
.GetData());
795 // Bind the member variables for field exchange between
796 // the wxDbTable object and the ODBC record.
799 if (!bindInsertParams()) // Inserts
802 if (!bindUpdateParams()) // Updates
806 if (!bindCols(*hstmtDefault
)) // Selects
809 if (!bindCols(hstmtInternal
)) // Internal use only
813 * Do NOT bind the hstmtCount cursor!!!
816 // Build an insert statement using parameter markers
817 if (!queryOnly
&& m_numCols
> 0)
819 bool needComma
= false;
820 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
821 pDb
->SQLTableName(tableName
.c_str()).c_str());
822 for (i
= 0; i
< m_numCols
; i
++)
824 if (! colDefs
[i
].InsertAllowed
)
828 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
832 sqlStmt
+= wxT(") VALUES (");
834 int insertableCount
= 0;
836 for (i
= 0; i
< m_numCols
; i
++)
838 if (! colDefs
[i
].InsertAllowed
)
848 // Prepare the insert statement for execution
851 if (SQLPrepare(hstmtInsert
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
852 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
858 // Completed successfully
861 } // wxDbTable::Open()
864 /********** wxDbTable::Query() **********/
865 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
868 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
870 } // wxDbTable::Query()
873 /********** wxDbTable::QueryBySqlStmt() **********/
874 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
876 pDb
->WriteSqlLog(pSqlStmt
);
878 return(query(DB_SELECT_STATEMENT
, false, false, pSqlStmt
));
880 } // wxDbTable::QueryBySqlStmt()
883 /********** wxDbTable::QueryMatching() **********/
884 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
887 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
889 } // wxDbTable::QueryMatching()
892 /********** wxDbTable::QueryOnKeyFields() **********/
893 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
896 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
898 } // wxDbTable::QueryOnKeyFields()
901 /********** wxDbTable::GetPrev() **********/
902 bool wxDbTable::GetPrev(void)
904 if (pDb
->FwdOnlyCursors())
906 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
910 return(getRec(SQL_FETCH_PRIOR
));
912 } // wxDbTable::GetPrev()
915 /********** wxDbTable::operator-- **********/
916 bool wxDbTable::operator--(int)
918 if (pDb
->FwdOnlyCursors())
920 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
924 return(getRec(SQL_FETCH_PRIOR
));
926 } // wxDbTable::operator--
929 /********** wxDbTable::GetFirst() **********/
930 bool wxDbTable::GetFirst(void)
932 if (pDb
->FwdOnlyCursors())
934 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
938 return(getRec(SQL_FETCH_FIRST
));
940 } // wxDbTable::GetFirst()
943 /********** wxDbTable::GetLast() **********/
944 bool wxDbTable::GetLast(void)
946 if (pDb
->FwdOnlyCursors())
948 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
952 return(getRec(SQL_FETCH_LAST
));
954 } // wxDbTable::GetLast()
957 /********** wxDbTable::BuildDeleteStmt() **********/
958 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
960 wxASSERT(!queryOnly
);
964 wxString whereClause
;
968 // Handle the case of DeleteWhere() and the where clause is blank. It should
969 // delete all records from the database in this case.
970 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.length() == 0))
972 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
973 pDb
->SQLTableName(tableName
.c_str()).c_str());
977 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
978 pDb
->SQLTableName(tableName
.c_str()).c_str());
980 // Append the WHERE clause to the SQL DELETE statement
983 case DB_DEL_KEYFIELDS
:
984 // If the datasource supports the ROWID column, build
985 // the where on ROWID for efficiency purposes.
986 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
987 if (CanUpdateByROWID())
990 wxChar rowid
[wxDB_ROWID_LEN
+1];
992 // Get the ROWID value. If not successful retreiving the ROWID,
993 // simply fall down through the code and build the WHERE clause
994 // based on the key fields.
995 if (SQLGetData(hstmt
, (UWORD
)(m_numCols
+1), SQL_C_WXCHAR
, (UCHAR
*) rowid
, sizeof(rowid
), &cb
) == SQL_SUCCESS
)
997 pSqlStmt
+= wxT("ROWID = '");
999 pSqlStmt
+= wxT("'");
1003 // Unable to delete by ROWID, so build a WHERE
1004 // clause based on the keyfields.
1005 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1006 pSqlStmt
+= whereClause
;
1009 pSqlStmt
+= pWhereClause
;
1011 case DB_DEL_MATCHING
:
1012 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1013 pSqlStmt
+= whereClause
;
1017 } // BuildDeleteStmt()
1020 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
1021 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
1023 wxString tempSqlStmt
;
1024 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
1025 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1026 } // wxDbTable::BuildDeleteStmt()
1029 /********** wxDbTable::BuildSelectStmt() **********/
1030 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
1032 wxString whereClause
;
1033 whereClause
.Empty();
1035 // Build a select statement to query the database
1036 pSqlStmt
= wxT("SELECT ");
1038 // SELECT DISTINCT values only?
1040 pSqlStmt
+= wxT("DISTINCT ");
1042 // Was a FROM clause specified to join tables to the base table?
1043 // Available for ::Query() only!!!
1044 bool appendFromClause
= false;
1045 #if wxODBC_BACKWARD_COMPATABILITY
1046 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
1047 appendFromClause
= true;
1049 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.length())
1050 appendFromClause
= true;
1053 // Add the column list
1056 for (i
= 0; i
< m_numCols
; i
++)
1058 tStr
= colDefs
[i
].ColName
;
1059 // If joining tables, the base table column names must be qualified to avoid ambiguity
1060 if ((appendFromClause
|| pDb
->Dbms() == dbmsACCESS
) && tStr
.Find(wxT('.')) == wxNOT_FOUND
)
1062 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1063 pSqlStmt
+= wxT(".");
1065 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1066 if (i
+ 1 < m_numCols
)
1067 pSqlStmt
+= wxT(",");
1070 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1071 // the ROWID if querying distinct records. The rowid will always be unique.
1072 if (!distinct
&& CanUpdateByROWID())
1074 // If joining tables, the base table column names must be qualified to avoid ambiguity
1075 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1077 pSqlStmt
+= wxT(",");
1078 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1079 pSqlStmt
+= wxT(".ROWID");
1082 pSqlStmt
+= wxT(",ROWID");
1085 // Append the FROM tablename portion
1086 pSqlStmt
+= wxT(" FROM ");
1087 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1088 // pSqlStmt += queryTableName;
1090 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1091 // The HOLDLOCK keyword follows the table name in the from clause.
1092 // Each table in the from clause must specify HOLDLOCK or
1093 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1094 // is parsed but ignored in SYBASE Transact-SQL.
1095 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1096 pSqlStmt
+= wxT(" HOLDLOCK");
1098 if (appendFromClause
)
1101 // Append the WHERE clause. Either append the where clause for the class
1102 // or build a where clause. The typeOfSelect determines this.
1103 switch(typeOfSelect
)
1105 case DB_SELECT_WHERE
:
1106 #if wxODBC_BACKWARD_COMPATABILITY
1107 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1109 if (where
.length()) // May not want a where clause!!!
1112 pSqlStmt
+= wxT(" WHERE ");
1116 case DB_SELECT_KEYFIELDS
:
1117 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1118 if (whereClause
.length())
1120 pSqlStmt
+= wxT(" WHERE ");
1121 pSqlStmt
+= whereClause
;
1124 case DB_SELECT_MATCHING
:
1125 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1126 if (whereClause
.length())
1128 pSqlStmt
+= wxT(" WHERE ");
1129 pSqlStmt
+= whereClause
;
1134 // Append the ORDER BY clause
1135 #if wxODBC_BACKWARD_COMPATABILITY
1136 if (orderBy
&& wxStrlen(orderBy
))
1138 if (orderBy
.length())
1141 pSqlStmt
+= wxT(" ORDER BY ");
1142 pSqlStmt
+= orderBy
;
1145 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1146 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1147 // HOLDLOCK for Sybase.
1148 if (selectForUpdate
&& CanSelectForUpdate())
1149 pSqlStmt
+= wxT(" FOR UPDATE");
1151 } // wxDbTable::BuildSelectStmt()
1154 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1155 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1157 wxString tempSqlStmt
;
1158 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1159 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1160 } // wxDbTable::BuildSelectStmt()
1163 /********** wxDbTable::BuildUpdateStmt() **********/
1164 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpdate
, const wxString
&pWhereClause
)
1166 wxASSERT(!queryOnly
);
1170 wxString whereClause
;
1171 whereClause
.Empty();
1173 bool firstColumn
= true;
1175 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1176 pDb
->SQLTableName(tableName
.c_str()).c_str());
1178 // Append a list of columns to be updated
1180 for (i
= 0; i
< m_numCols
; i
++)
1182 // Only append Updateable columns
1183 if (colDefs
[i
].Updateable
)
1186 pSqlStmt
+= wxT(",");
1188 firstColumn
= false;
1190 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1191 // pSqlStmt += colDefs[i].ColName;
1192 pSqlStmt
+= wxT(" = ?");
1196 // Append the WHERE clause to the SQL UPDATE statement
1197 pSqlStmt
+= wxT(" WHERE ");
1198 switch(typeOfUpdate
)
1200 case DB_UPD_KEYFIELDS
:
1201 // If the datasource supports the ROWID column, build
1202 // the where on ROWID for efficiency purposes.
1203 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1204 if (CanUpdateByROWID())
1207 wxChar rowid
[wxDB_ROWID_LEN
+1];
1209 // Get the ROWID value. If not successful retreiving the ROWID,
1210 // simply fall down through the code and build the WHERE clause
1211 // based on the key fields.
1212 if (SQLGetData(hstmt
, (UWORD
)(m_numCols
+1), SQL_C_WXCHAR
, (UCHAR
*) rowid
, sizeof(rowid
), &cb
) == SQL_SUCCESS
)
1214 pSqlStmt
+= wxT("ROWID = '");
1216 pSqlStmt
+= wxT("'");
1220 // Unable to delete by ROWID, so build a WHERE
1221 // clause based on the keyfields.
1222 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1223 pSqlStmt
+= whereClause
;
1226 pSqlStmt
+= pWhereClause
;
1229 } // BuildUpdateStmt()
1232 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1233 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpdate
, const wxString
&pWhereClause
)
1235 wxString tempSqlStmt
;
1236 BuildUpdateStmt(tempSqlStmt
, typeOfUpdate
, pWhereClause
);
1237 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1238 } // BuildUpdateStmt()
1241 /********** wxDbTable::BuildWhereClause() **********/
1242 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1243 const wxString
&qualTableName
, bool useLikeComparison
)
1245 * Note: BuildWhereClause() currently ignores timestamp columns.
1246 * They are not included as part of the where clause.
1249 bool moreThanOneColumn
= false;
1252 // Loop through the columns building a where clause as you go
1254 for (colNumber
= 0; colNumber
< m_numCols
; colNumber
++)
1256 // Determine if this column should be included in the WHERE clause
1257 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[colNumber
].KeyField
) ||
1258 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull((UWORD
)colNumber
))))
1260 // Skip over timestamp columns
1261 if (colDefs
[colNumber
].SqlCtype
== SQL_C_TIMESTAMP
)
1263 // If there is more than 1 column, join them with the keyword "AND"
1264 if (moreThanOneColumn
)
1265 pWhereClause
+= wxT(" AND ");
1267 moreThanOneColumn
= true;
1269 // Concatenate where phrase for the column
1270 wxString tStr
= colDefs
[colNumber
].ColName
;
1272 if (qualTableName
.length() && tStr
.Find(wxT('.')) == wxNOT_FOUND
)
1274 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1275 pWhereClause
+= wxT(".");
1277 pWhereClause
+= pDb
->SQLColumnName(colDefs
[colNumber
].ColName
);
1279 if (useLikeComparison
&& (colDefs
[colNumber
].SqlCtype
== SQL_C_WXCHAR
))
1280 pWhereClause
+= wxT(" LIKE ");
1282 pWhereClause
+= wxT(" = ");
1284 switch(colDefs
[colNumber
].SqlCtype
)
1290 //case SQL_C_WXCHAR: SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
1291 colValue
.Printf(wxT("'%s'"), GetDb()->EscapeSqlChars((wxChar
*)colDefs
[colNumber
].PtrDataObj
).c_str());
1295 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[colNumber
].PtrDataObj
));
1298 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[colNumber
].PtrDataObj
));
1302 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[colNumber
].PtrDataObj
));
1305 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[colNumber
].PtrDataObj
));
1308 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[colNumber
].PtrDataObj
));
1311 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[colNumber
].PtrDataObj
));
1316 strMsg
.Printf(wxT("wxDbTable::bindParams(): Unknown column type for colDefs %d colName %s"),
1317 colNumber
,colDefs
[colNumber
].ColName
);
1318 wxFAIL_MSG(strMsg
.c_str());
1322 pWhereClause
+= colValue
;
1325 } // wxDbTable::BuildWhereClause()
1328 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1329 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1330 const wxString
&qualTableName
, bool useLikeComparison
)
1332 wxString tempSqlStmt
;
1333 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1334 wxStrcpy(pWhereClause
, tempSqlStmt
);
1335 } // wxDbTable::BuildWhereClause()
1338 /********** wxDbTable::GetRowNum() **********/
1339 UWORD
wxDbTable::GetRowNum(void)
1343 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1345 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1349 // Completed successfully
1350 return((UWORD
) rowNum
);
1352 } // wxDbTable::GetRowNum()
1355 /********** wxDbTable::CloseCursor() **********/
1356 bool wxDbTable::CloseCursor(HSTMT cursor
)
1358 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1359 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1361 // Completed successfully
1364 } // wxDbTable::CloseCursor()
1367 /********** wxDbTable::CreateTable() **********/
1368 bool wxDbTable::CreateTable(bool attemptDrop
)
1376 #ifdef DBDEBUG_CONSOLE
1377 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1381 if (attemptDrop
&& !DropTable())
1385 #ifdef DBDEBUG_CONSOLE
1386 for (i
= 0; i
< m_numCols
; i
++)
1388 // Exclude derived columns since they are NOT part of the base table
1389 if (colDefs
[i
].DerivedCol
)
1391 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1392 switch(colDefs
[i
].DbDataType
)
1394 case DB_DATA_TYPE_VARCHAR
:
1395 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)) << wxT(")");
1397 case DB_DATA_TYPE_MEMO
:
1398 cout
<< pDb
->GetTypeInfMemo().TypeName
;
1400 case DB_DATA_TYPE_INTEGER
:
1401 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1403 case DB_DATA_TYPE_FLOAT
:
1404 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1406 case DB_DATA_TYPE_DATE
:
1407 cout
<< pDb
->GetTypeInfDate().TypeName
;
1409 case DB_DATA_TYPE_BLOB
:
1410 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1417 // Build a CREATE TABLE string from the colDefs structure.
1418 bool needComma
= false;
1420 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1421 pDb
->SQLTableName(tableName
.c_str()).c_str());
1423 for (i
= 0; i
< m_numCols
; i
++)
1425 // Exclude derived columns since they are NOT part of the base table
1426 if (colDefs
[i
].DerivedCol
)
1430 sqlStmt
+= wxT(",");
1432 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1433 // sqlStmt += colDefs[i].ColName;
1434 sqlStmt
+= wxT(" ");
1436 switch(colDefs
[i
].DbDataType
)
1438 case DB_DATA_TYPE_VARCHAR
:
1439 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1441 case DB_DATA_TYPE_MEMO
:
1442 sqlStmt
+= pDb
->GetTypeInfMemo().TypeName
;
1444 case DB_DATA_TYPE_INTEGER
:
1445 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1447 case DB_DATA_TYPE_FLOAT
:
1448 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1450 case DB_DATA_TYPE_DATE
:
1451 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1453 case DB_DATA_TYPE_BLOB
:
1454 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1457 // For varchars, append the size of the string
1458 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
&&
1459 (pDb
->Dbms() != dbmsMY_SQL
|| pDb
->GetTypeInfVarchar().TypeName
!= _T("text")))// ||
1460 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1463 s
.Printf(wxT("(%d)"), (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)));
1467 if (pDb
->Dbms() == dbmsDB2
||
1468 pDb
->Dbms() == dbmsMY_SQL
||
1469 pDb
->Dbms() == dbmsSYBASE_ASE
||
1470 pDb
->Dbms() == dbmsINTERBASE
||
1471 pDb
->Dbms() == dbmsFIREBIRD
||
1472 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1474 if (colDefs
[i
].KeyField
)
1476 sqlStmt
+= wxT(" NOT NULL");
1482 // If there is a primary key defined, include it in the create statement
1483 for (i
= j
= 0; i
< m_numCols
; i
++)
1485 if (colDefs
[i
].KeyField
)
1491 if ( j
&& (pDb
->Dbms() != dbmsDBASE
)
1492 && (pDb
->Dbms() != dbmsXBASE_SEQUITER
) ) // Found a keyfield
1494 switch (pDb
->Dbms())
1498 case dbmsSYBASE_ASA
:
1499 case dbmsSYBASE_ASE
:
1503 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1504 sqlStmt
+= wxT(",PRIMARY KEY (");
1509 sqlStmt
+= wxT(",CONSTRAINT ");
1510 // DB2 is limited to 18 characters for index names
1511 if (pDb
->Dbms() == dbmsDB2
)
1513 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."));
1514 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1515 // sqlStmt += tableName.substr(0, 13);
1518 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1519 // sqlStmt += tableName;
1521 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1526 // List column name(s) of column(s) comprising the primary key
1527 for (i
= j
= 0; i
< m_numCols
; i
++)
1529 if (colDefs
[i
].KeyField
)
1531 if (j
++) // Multi part key, comma separate names
1532 sqlStmt
+= wxT(",");
1533 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1535 if (pDb
->Dbms() == dbmsMY_SQL
&&
1536 colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1539 s
.Printf(wxT("(%d)"), (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)));
1544 sqlStmt
+= wxT(")");
1546 if (pDb
->Dbms() == dbmsINFORMIX
||
1547 pDb
->Dbms() == dbmsSYBASE_ASA
||
1548 pDb
->Dbms() == dbmsSYBASE_ASE
)
1550 sqlStmt
+= wxT(" CONSTRAINT ");
1551 sqlStmt
+= pDb
->SQLTableName(tableName
);
1552 // sqlStmt += tableName;
1553 sqlStmt
+= wxT("_PIDX");
1556 // Append the closing parentheses for the create table statement
1557 sqlStmt
+= wxT(")");
1559 pDb
->WriteSqlLog(sqlStmt
);
1561 #ifdef DBDEBUG_CONSOLE
1562 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1565 // Execute the CREATE TABLE statement
1566 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1567 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1569 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1570 pDb
->RollbackTrans();
1575 // Commit the transaction and close the cursor
1576 if (!pDb
->CommitTrans())
1578 if (!CloseCursor(hstmt
))
1581 // Database table created successfully
1584 } // wxDbTable::CreateTable()
1587 /********** wxDbTable::DropTable() **********/
1588 bool wxDbTable::DropTable()
1590 // NOTE: This function returns true if the Table does not exist, but
1591 // only for identified databases. Code will need to be added
1592 // below for any other databases when those databases are defined
1593 // to handle this situation consistently
1597 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1598 pDb
->SQLTableName(tableName
.c_str()).c_str());
1600 pDb
->WriteSqlLog(sqlStmt
);
1602 #ifdef DBDEBUG_CONSOLE
1603 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1606 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1607 if (retcode
!= SQL_SUCCESS
)
1609 // Check for "Base table not found" error and ignore
1610 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1611 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1612 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1614 // Check for product specific error codes
1615 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1616 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1617 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1618 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1620 pDb
->DispNextError();
1621 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1622 pDb
->RollbackTrans();
1623 // CloseCursor(hstmt);
1629 // Commit the transaction and close the cursor
1630 if (! pDb
->CommitTrans())
1632 if (! CloseCursor(hstmt
))
1636 } // wxDbTable::DropTable()
1639 /********** wxDbTable::CreateIndex() **********/
1640 bool wxDbTable::CreateIndex(const wxString
&indexName
, bool unique
, UWORD numIndexColumns
,
1641 wxDbIdxDef
*pIndexDefs
, bool attemptDrop
)
1645 // Drop the index first
1646 if (attemptDrop
&& !DropIndex(indexName
))
1649 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1650 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1651 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1652 // table was created, then months later you determine that an additional index while
1653 // give better performance, so you want to add an index).
1655 // The following block of code will modify the column definition to make the column be
1656 // defined with the "NOT NULL" qualifier.
1657 if (pDb
->Dbms() == dbmsMY_SQL
)
1662 for (i
= 0; i
< numIndexColumns
&& ok
; i
++)
1666 // Find the column definition that has the ColName that matches the
1667 // index column name. We need to do this to get the DB_DATA_TYPE of
1668 // the index column, as MySQL's syntax for the ALTER column requires
1670 while (!found
&& (j
< this->m_numCols
))
1672 if (wxStrcmp(colDefs
[j
].ColName
,pIndexDefs
[i
].ColName
) == 0)
1680 ok
= pDb
->ModifyColumn(tableName
, pIndexDefs
[i
].ColName
,
1681 colDefs
[j
].DbDataType
, (int)(colDefs
[j
].SzDataObj
/ sizeof(wxChar
)),
1687 // retcode is not used
1688 wxODBC_ERRORS retcode
;
1689 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1690 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1691 // This line is just here for debug checking of the value
1692 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1703 pDb
->RollbackTrans();
1708 // Build a CREATE INDEX statement
1709 sqlStmt
= wxT("CREATE ");
1711 sqlStmt
+= wxT("UNIQUE ");
1713 sqlStmt
+= wxT("INDEX ");
1714 sqlStmt
+= pDb
->SQLTableName(indexName
);
1715 sqlStmt
+= wxT(" ON ");
1717 sqlStmt
+= pDb
->SQLTableName(tableName
);
1718 // sqlStmt += tableName;
1719 sqlStmt
+= wxT(" (");
1721 // Append list of columns making up index
1723 for (i
= 0; i
< numIndexColumns
; i
++)
1725 sqlStmt
+= pDb
->SQLColumnName(pIndexDefs
[i
].ColName
);
1726 // sqlStmt += pIndexDefs[i].ColName;
1728 // MySQL requires a key length on VARCHAR keys
1729 if ( pDb
->Dbms() == dbmsMY_SQL
)
1731 // Find the details on this column
1733 for ( j
= 0; j
< m_numCols
; ++j
)
1735 if ( wxStrcmp( pIndexDefs
[i
].ColName
, colDefs
[j
].ColName
) == 0 )
1740 if ( colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1743 s
.Printf(wxT("(%d)"), (int)(colDefs
[i
].SzDataObj
/ sizeof(wxChar
)));
1748 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1749 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (wxStrncmp(pDb
->dbInf
.dbmsVer
,_T("07"),2)==0)) &&
1750 !(pDb
->Dbms() == dbmsFIREBIRD
) &&
1751 !(pDb
->Dbms() == dbmsPOSTGRES
))
1753 if (pIndexDefs
[i
].Ascending
)
1754 sqlStmt
+= wxT(" ASC");
1756 sqlStmt
+= wxT(" DESC");
1759 wxASSERT_MSG(pIndexDefs
[i
].Ascending
, _T("Datasource does not support DESCending index columns"));
1761 if ((i
+ 1) < numIndexColumns
)
1762 sqlStmt
+= wxT(",");
1765 // Append closing parentheses
1766 sqlStmt
+= wxT(")");
1768 pDb
->WriteSqlLog(sqlStmt
);
1770 #ifdef DBDEBUG_CONSOLE
1771 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1774 // Execute the CREATE INDEX statement
1775 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1776 if (retcode
!= SQL_SUCCESS
)
1778 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1779 pDb
->RollbackTrans();
1784 // Commit the transaction and close the cursor
1785 if (! pDb
->CommitTrans())
1787 if (! CloseCursor(hstmt
))
1790 // Index Created Successfully
1793 } // wxDbTable::CreateIndex()
1796 /********** wxDbTable::DropIndex() **********/
1797 bool wxDbTable::DropIndex(const wxString
&indexName
)
1799 // NOTE: This function returns true if the Index does not exist, but
1800 // only for identified databases. Code will need to be added
1801 // below for any other databases when those databases are defined
1802 // to handle this situation consistently
1806 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1807 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1808 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1809 pDb
->SQLTableName(indexName
.c_str()).c_str(),
1810 pDb
->SQLTableName(tableName
.c_str()).c_str());
1811 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1812 (pDb
->Dbms() == dbmsSYBASE_ASE
) ||
1813 (pDb
->Dbms() == dbmsXBASE_SEQUITER
))
1814 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1815 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1816 pDb
->SQLTableName(indexName
.c_str()).c_str());
1818 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1819 pDb
->SQLTableName(indexName
.c_str()).c_str());
1821 pDb
->WriteSqlLog(sqlStmt
);
1823 #ifdef DBDEBUG_CONSOLE
1824 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1826 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1827 if (retcode
!= SQL_SUCCESS
)
1829 // Check for "Index not found" error and ignore
1830 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1831 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1833 // Check for product specific error codes
1834 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1835 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1836 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1837 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1838 (pDb
->Dbms() == dbmsMAXDB
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1839 (pDb
->Dbms() == dbmsFIREBIRD
&& !wxStrcmp(pDb
->sqlState
,wxT("HY000"))) ||
1840 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S0002"))) || // Base table not found
1841 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1842 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))
1845 pDb
->DispNextError();
1846 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1847 pDb
->RollbackTrans();
1854 // Commit the transaction and close the cursor
1855 if (! pDb
->CommitTrans())
1857 if (! CloseCursor(hstmt
))
1861 } // wxDbTable::DropIndex()
1864 /********** wxDbTable::SetOrderByColNums() **********/
1865 bool wxDbTable::SetOrderByColNums(UWORD first
, ... )
1867 int colNumber
= first
; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
1873 va_start(argptr
, first
); /* Initialize variable arguments. */
1874 while (!abort
&& (colNumber
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1876 // Make sure the passed in column number
1877 // is within the valid range of columns
1879 // Valid columns are 0 thru m_numCols-1
1880 if (colNumber
>= m_numCols
|| colNumber
< 0)
1886 if (colNumber
!= first
)
1887 tempStr
+= wxT(",");
1889 tempStr
+= colDefs
[colNumber
].ColName
;
1890 colNumber
= va_arg (argptr
, int);
1892 va_end (argptr
); /* Reset variable arguments. */
1894 SetOrderByClause(tempStr
);
1897 } // wxDbTable::SetOrderByColNums()
1900 /********** wxDbTable::Insert() **********/
1901 int wxDbTable::Insert(void)
1903 wxASSERT(!queryOnly
);
1904 if (queryOnly
|| !insertable
)
1909 // Insert the record by executing the already prepared insert statement
1911 retcode
= SQLExecute(hstmtInsert
);
1912 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
&&
1913 retcode
!= SQL_NEED_DATA
)
1915 // Check to see if integrity constraint was violated
1916 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1917 if (! wxStrcmp(pDb
->sqlState
, wxT("23000"))) // Integrity constraint violated
1918 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1921 pDb
->DispNextError();
1922 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1926 if (retcode
== SQL_NEED_DATA
)
1929 retcode
= SQLParamData(hstmtInsert
, &pParmID
);
1930 while (retcode
== SQL_NEED_DATA
)
1932 // Find the parameter
1934 for (i
=0; i
< m_numCols
; i
++)
1936 if (colDefs
[i
].PtrDataObj
== pParmID
)
1938 // We found it. Store the parameter.
1939 retcode
= SQLPutData(hstmtInsert
, pParmID
, colDefs
[i
].SzDataObj
);
1940 if (retcode
!= SQL_SUCCESS
)
1942 pDb
->DispNextError();
1943 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1949 retcode
= SQLParamData(hstmtInsert
, &pParmID
);
1950 if (retcode
!= SQL_SUCCESS
&&
1951 retcode
!= SQL_SUCCESS_WITH_INFO
)
1953 // record was not inserted
1954 pDb
->DispNextError();
1955 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1961 // Record inserted into the datasource successfully
1964 } // wxDbTable::Insert()
1967 /********** wxDbTable::Update() **********/
1968 bool wxDbTable::Update(void)
1970 wxASSERT(!queryOnly
);
1976 // Build the SQL UPDATE statement
1977 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1979 pDb
->WriteSqlLog(sqlStmt
);
1981 #ifdef DBDEBUG_CONSOLE
1982 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1985 // Execute the SQL UPDATE statement
1986 return(execUpdate(sqlStmt
));
1988 } // wxDbTable::Update()
1991 /********** wxDbTable::Update(pSqlStmt) **********/
1992 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1994 wxASSERT(!queryOnly
);
1998 pDb
->WriteSqlLog(pSqlStmt
);
2000 return(execUpdate(pSqlStmt
));
2002 } // wxDbTable::Update(pSqlStmt)
2005 /********** wxDbTable::UpdateWhere() **********/
2006 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
2008 wxASSERT(!queryOnly
);
2014 // Build the SQL UPDATE statement
2015 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
2017 pDb
->WriteSqlLog(sqlStmt
);
2019 #ifdef DBDEBUG_CONSOLE
2020 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
2023 // Execute the SQL UPDATE statement
2024 return(execUpdate(sqlStmt
));
2026 } // wxDbTable::UpdateWhere()
2029 /********** wxDbTable::Delete() **********/
2030 bool wxDbTable::Delete(void)
2032 wxASSERT(!queryOnly
);
2039 // Build the SQL DELETE statement
2040 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
2042 pDb
->WriteSqlLog(sqlStmt
);
2044 // Execute the SQL DELETE statement
2045 return(execDelete(sqlStmt
));
2047 } // wxDbTable::Delete()
2050 /********** wxDbTable::DeleteWhere() **********/
2051 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
2053 wxASSERT(!queryOnly
);
2060 // Build the SQL DELETE statement
2061 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
2063 pDb
->WriteSqlLog(sqlStmt
);
2065 // Execute the SQL DELETE statement
2066 return(execDelete(sqlStmt
));
2068 } // wxDbTable::DeleteWhere()
2071 /********** wxDbTable::DeleteMatching() **********/
2072 bool wxDbTable::DeleteMatching(void)
2074 wxASSERT(!queryOnly
);
2081 // Build the SQL DELETE statement
2082 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
2084 pDb
->WriteSqlLog(sqlStmt
);
2086 // Execute the SQL DELETE statement
2087 return(execDelete(sqlStmt
));
2089 } // wxDbTable::DeleteMatching()
2092 /********** wxDbTable::IsColNull() **********/
2093 bool wxDbTable::IsColNull(UWORD colNumber
) const
2096 This logic is just not right. It would indicate true
2097 if a numeric field were set to a value of 0.
2099 switch(colDefs[colNumber].SqlCtype)
2103 //case SQL_C_WXCHAR: SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
2104 return(((UCHAR FAR *) colDefs[colNumber].PtrDataObj)[0] == 0);
2106 return(( *((SWORD *) colDefs[colNumber].PtrDataObj)) == 0);
2108 return(( *((UWORD*) colDefs[colNumber].PtrDataObj)) == 0);
2110 return(( *((SDWORD *) colDefs[colNumber].PtrDataObj)) == 0);
2112 return(( *((UDWORD *) colDefs[colNumber].PtrDataObj)) == 0);
2114 return(( *((SFLOAT *) colDefs[colNumber].PtrDataObj)) == 0);
2116 return((*((SDOUBLE *) colDefs[colNumber].PtrDataObj)) == 0);
2117 case SQL_C_TIMESTAMP:
2118 TIMESTAMP_STRUCT *pDt;
2119 pDt = (TIMESTAMP_STRUCT *) colDefs[colNumber].PtrDataObj;
2120 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
2128 return (colDefs
[colNumber
].Null
);
2129 } // wxDbTable::IsColNull()
2132 /********** wxDbTable::CanSelectForUpdate() **********/
2133 bool wxDbTable::CanSelectForUpdate(void)
2138 if (pDb
->Dbms() == dbmsMY_SQL
)
2141 if ((pDb
->Dbms() == dbmsORACLE
) ||
2142 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
2147 } // wxDbTable::CanSelectForUpdate()
2150 /********** wxDbTable::CanUpdateByROWID() **********/
2151 bool wxDbTable::CanUpdateByROWID(void)
2154 * NOTE: Returning false for now until this can be debugged,
2155 * as the ROWID is not getting updated correctly
2159 if (pDb->Dbms() == dbmsORACLE)
2164 } // wxDbTable::CanUpdateByROWID()
2167 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2168 bool wxDbTable::IsCursorClosedOnCommit(void)
2170 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2175 } // wxDbTable::IsCursorClosedOnCommit()
2179 /********** wxDbTable::ClearMemberVar() **********/
2180 void wxDbTable::ClearMemberVar(UWORD colNumber
, bool setToNull
)
2182 wxASSERT(colNumber
< m_numCols
);
2184 switch(colDefs
[colNumber
].SqlCtype
)
2190 //case SQL_C_WXCHAR: SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
2191 ((UCHAR FAR
*) colDefs
[colNumber
].PtrDataObj
)[0] = 0;
2194 *((SWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2197 *((UWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2201 *((SDWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2204 *((UDWORD
*) colDefs
[colNumber
].PtrDataObj
) = 0;
2207 *((SFLOAT
*) colDefs
[colNumber
].PtrDataObj
) = 0.0f
;
2210 *((SDOUBLE
*) colDefs
[colNumber
].PtrDataObj
) = 0.0f
;
2212 case SQL_C_TIMESTAMP
:
2213 TIMESTAMP_STRUCT
*pDt
;
2214 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNumber
].PtrDataObj
;
2225 pDtd
= (DATE_STRUCT
*) colDefs
[colNumber
].PtrDataObj
;
2232 pDtt
= (TIME_STRUCT
*) colDefs
[colNumber
].PtrDataObj
;
2240 SetColNull(colNumber
);
2241 } // wxDbTable::ClearMemberVar()
2244 /********** wxDbTable::ClearMemberVars() **********/
2245 void wxDbTable::ClearMemberVars(bool setToNull
)
2249 // Loop through the columns setting each member variable to zero
2250 for (i
=0; i
< m_numCols
; i
++)
2251 ClearMemberVar((UWORD
)i
,setToNull
);
2253 } // wxDbTable::ClearMemberVars()
2256 /********** wxDbTable::SetQueryTimeout() **********/
2257 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2259 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2260 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2261 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2262 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2263 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2264 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2265 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2266 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2268 // Completed Successfully
2271 } // wxDbTable::SetQueryTimeout()
2274 /********** wxDbTable::SetColDefs() **********/
2275 bool wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2276 SWORD cType
, int size
, bool keyField
, bool updateable
,
2277 bool insertAllowed
, bool derivedColumn
)
2281 if (index
>= m_numCols
) // Columns numbers are zero based....
2283 tmpStr
.Printf(wxT("Specified column index (%d) exceeds the maximum number of columns (%d) registered for this table definition. Column definition not added."), index
, m_numCols
);
2289 if (!colDefs
) // May happen if the database connection fails
2292 if (fieldName
.length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2294 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2295 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0; // Prevent buffer overrun
2297 tmpStr
.Printf(wxT("Column name '%s' is too long. Truncated to '%s'."),
2298 fieldName
.c_str(),colDefs
[index
].ColName
);
2303 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2305 colDefs
[index
].DbDataType
= dataType
;
2306 colDefs
[index
].PtrDataObj
= pData
;
2307 colDefs
[index
].SqlCtype
= cType
;
2308 colDefs
[index
].SzDataObj
= size
; //TODO: glt ??? * sizeof(wxChar) ???
2309 colDefs
[index
].KeyField
= keyField
;
2310 colDefs
[index
].DerivedCol
= derivedColumn
;
2311 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2314 colDefs
[index
].Updateable
= false;
2315 colDefs
[index
].InsertAllowed
= false;
2319 colDefs
[index
].Updateable
= updateable
;
2320 colDefs
[index
].InsertAllowed
= insertAllowed
;
2323 colDefs
[index
].Null
= false;
2327 } // wxDbTable::SetColDefs()
2330 /********** wxDbTable::SetColDefs() **********/
2331 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2334 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2340 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2342 for (index
= 0; index
< numCols
; index
++)
2344 // Process the fields
2345 switch (pColInfs
[index
].dbDataType
)
2347 case DB_DATA_TYPE_VARCHAR
:
2348 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferSize
+(1*sizeof(wxChar
))];
2349 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferSize
+(1*sizeof(wxChar
));
2350 pColDataPtrs
[index
].SqlCtype
= SQL_C_WXCHAR
;
2352 case DB_DATA_TYPE_MEMO
:
2353 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferSize
+(1*sizeof(wxChar
))];
2354 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferSize
+(1*sizeof(wxChar
));
2355 pColDataPtrs
[index
].SqlCtype
= SQL_C_WXCHAR
;
2357 case DB_DATA_TYPE_INTEGER
:
2358 // Can be long or short
2359 if (pColInfs
[index
].bufferSize
== sizeof(long))
2361 pColDataPtrs
[index
].PtrDataObj
= new long;
2362 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2363 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2367 pColDataPtrs
[index
].PtrDataObj
= new short;
2368 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2369 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2372 case DB_DATA_TYPE_FLOAT
:
2373 // Can be float or double
2374 if (pColInfs
[index
].bufferSize
== sizeof(float))
2376 pColDataPtrs
[index
].PtrDataObj
= new float;
2377 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2378 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2382 pColDataPtrs
[index
].PtrDataObj
= new double;
2383 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2384 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2387 case DB_DATA_TYPE_DATE
:
2388 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2389 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2390 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2392 case DB_DATA_TYPE_BLOB
:
2393 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2394 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2395 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2396 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2399 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2400 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2403 // Unable to build all the column definitions, as either one of
2404 // the calls to "new" failed above, or there was a BLOB field
2405 // to have a column definition for. If BLOBs are to be used,
2406 // the other form of ::SetColDefs() must be used, as it is impossible
2407 // to know the maximum size to create the PtrDataObj to be.
2408 delete [] pColDataPtrs
;
2414 return (pColDataPtrs
);
2416 } // wxDbTable::SetColDefs()
2419 /********** wxDbTable::SetCursor() **********/
2420 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2422 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2423 hstmt
= *hstmtDefault
;
2425 hstmt
= *hstmtActivate
;
2427 } // wxDbTable::SetCursor()
2430 /********** wxDbTable::Count(const wxString &) **********/
2431 ULONG
wxDbTable::Count(const wxString
&args
)
2437 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2438 sqlStmt
= wxT("SELECT COUNT(");
2440 sqlStmt
+= wxT(") FROM ");
2441 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2442 // sqlStmt += queryTableName;
2443 #if wxODBC_BACKWARD_COMPATABILITY
2444 if (from
&& wxStrlen(from
))
2450 // Add the where clause if one is provided
2451 #if wxODBC_BACKWARD_COMPATABILITY
2452 if (where
&& wxStrlen(where
))
2457 sqlStmt
+= wxT(" WHERE ");
2461 pDb
->WriteSqlLog(sqlStmt
);
2463 // Initialize the Count cursor if it's not already initialized
2466 hstmtCount
= GetNewCursor(false,false);
2467 wxASSERT(hstmtCount
);
2472 // Execute the SQL statement
2473 if (SQLExecDirect(*hstmtCount
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2475 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2480 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2482 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2486 // Obtain the result
2487 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2489 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2494 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2495 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2497 // Return the record count
2500 } // wxDbTable::Count()
2503 /********** wxDbTable::Refresh() **********/
2504 bool wxDbTable::Refresh(void)
2508 // Switch to the internal cursor so any active cursors are not corrupted
2509 HSTMT currCursor
= GetCursor();
2510 hstmt
= hstmtInternal
;
2511 #if wxODBC_BACKWARD_COMPATABILITY
2512 // Save the where and order by clauses
2513 wxChar
*saveWhere
= where
;
2514 wxChar
*saveOrderBy
= orderBy
;
2516 wxString saveWhere
= where
;
2517 wxString saveOrderBy
= orderBy
;
2519 // Build a where clause to refetch the record with. Try and use the
2520 // ROWID if it's available, ow use the key fields.
2521 wxString whereClause
;
2522 whereClause
.Empty();
2524 if (CanUpdateByROWID())
2527 wxChar rowid
[wxDB_ROWID_LEN
+1];
2529 // Get the ROWID value. If not successful retreiving the ROWID,
2530 // simply fall down through the code and build the WHERE clause
2531 // based on the key fields.
2532 if (SQLGetData(hstmt
, (UWORD
)(m_numCols
+1), SQL_C_WXCHAR
, (UCHAR
*) rowid
, sizeof(rowid
), &cb
) == SQL_SUCCESS
)
2534 whereClause
+= pDb
->SQLTableName(queryTableName
);
2535 // whereClause += queryTableName;
2536 whereClause
+= wxT(".ROWID = '");
2537 whereClause
+= rowid
;
2538 whereClause
+= wxT("'");
2542 // If unable to use the ROWID, build a where clause from the keyfields
2543 if (wxStrlen(whereClause
) == 0)
2544 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2546 // Requery the record
2547 where
= whereClause
;
2552 if (result
&& !GetNext())
2555 // Switch back to original cursor
2556 SetCursor(&currCursor
);
2558 // Free the internal cursor
2559 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2560 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2562 // Restore the original where and order by clauses
2564 orderBy
= saveOrderBy
;
2568 } // wxDbTable::Refresh()
2571 /********** wxDbTable::SetColNull() **********/
2572 bool wxDbTable::SetColNull(UWORD colNumber
, bool set
)
2574 if (colNumber
< m_numCols
)
2576 colDefs
[colNumber
].Null
= set
;
2577 if (set
) // Blank out the values in the member variable
2578 ClearMemberVar(colNumber
, false); // Must call with false here, or infinite recursion will happen
2580 setCbValueForColumn(colNumber
);
2587 } // wxDbTable::SetColNull()
2590 /********** wxDbTable::SetColNull() **********/
2591 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2594 for (colNumber
= 0; colNumber
< m_numCols
; colNumber
++)
2596 if (!wxStricmp(colName
, colDefs
[colNumber
].ColName
))
2600 if (colNumber
< m_numCols
)
2602 colDefs
[colNumber
].Null
= set
;
2603 if (set
) // Blank out the values in the member variable
2604 ClearMemberVar((UWORD
)colNumber
,false); // Must call with false here, or infinite recursion will happen
2606 setCbValueForColumn(colNumber
);
2613 } // wxDbTable::SetColNull()
2616 /********** wxDbTable::GetNewCursor() **********/
2617 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2619 HSTMT
*newHSTMT
= new HSTMT
;
2624 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2626 pDb
->DispAllErrors(henv
, hdbc
);
2631 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2633 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2640 if (!bindCols(*newHSTMT
))
2648 SetCursor(newHSTMT
);
2652 } // wxDbTable::GetNewCursor()
2655 /********** wxDbTable::DeleteCursor() **********/
2656 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2660 if (!hstmtDel
) // Cursor already deleted
2664 ODBC 3.0 says to use this form
2665 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2668 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2670 pDb
->DispAllErrors(henv
, hdbc
);
2678 } // wxDbTable::DeleteCursor()
2680 //////////////////////////////////////////////////////////////
2681 // wxDbGrid support functions
2682 //////////////////////////////////////////////////////////////
2684 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2686 if (!m_hstmtGridQuery
)
2688 m_hstmtGridQuery
= GetNewCursor(false,false);
2689 if (!bindCols(*m_hstmtGridQuery
))
2693 m_rowmode
= rowmode
;
2696 case WX_ROW_MODE_QUERY
:
2697 SetCursor(m_hstmtGridQuery
);
2699 case WX_ROW_MODE_INDIVIDUAL
:
2700 SetCursor(hstmtDefault
);
2705 } // wxDbTable::SetRowMode()
2708 wxVariant
wxDbTable::GetColumn(const int colNumber
) const
2711 if ((colNumber
< m_numCols
) && (!IsColNull((UWORD
)colNumber
)))
2713 switch (colDefs
[colNumber
].SqlCtype
)
2716 #if defined(SQL_WCHAR)
2719 #if defined(SQL_WVARCHAR)
2725 val
= (wxChar
*)(colDefs
[colNumber
].PtrDataObj
);
2729 val
= *(long *)(colDefs
[colNumber
].PtrDataObj
);
2733 val
= (long int )(*(short *)(colDefs
[colNumber
].PtrDataObj
));
2736 val
= (long)(*(unsigned long *)(colDefs
[colNumber
].PtrDataObj
));
2739 val
= (long)(*(wxChar
*)(colDefs
[colNumber
].PtrDataObj
));
2741 case SQL_C_UTINYINT
:
2742 val
= (long)(*(wxChar
*)(colDefs
[colNumber
].PtrDataObj
));
2745 val
= (long)(*(UWORD
*)(colDefs
[colNumber
].PtrDataObj
));
2748 val
= (DATE_STRUCT
*)(colDefs
[colNumber
].PtrDataObj
);
2751 val
= (TIME_STRUCT
*)(colDefs
[colNumber
].PtrDataObj
);
2753 case SQL_C_TIMESTAMP
:
2754 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNumber
].PtrDataObj
);
2757 val
= *(double *)(colDefs
[colNumber
].PtrDataObj
);
2764 } // wxDbTable::GetCol()
2767 void wxDbTable::SetColumn(const int colNumber
, const wxVariant val
)
2769 //FIXME: Add proper wxDateTime support to wxVariant..
2772 SetColNull((UWORD
)colNumber
, val
.IsNull());
2776 if ((colDefs
[colNumber
].SqlCtype
== SQL_C_DATE
)
2777 || (colDefs
[colNumber
].SqlCtype
== SQL_C_TIME
)
2778 || (colDefs
[colNumber
].SqlCtype
== SQL_C_TIMESTAMP
))
2780 //Returns null if invalid!
2781 if (!dateval
.ParseDate(val
.GetString()))
2782 SetColNull((UWORD
)colNumber
, true);
2785 switch (colDefs
[colNumber
].SqlCtype
)
2788 #if defined(SQL_WCHAR)
2791 #if defined(SQL_WVARCHAR)
2797 csstrncpyt((wxChar
*)(colDefs
[colNumber
].PtrDataObj
),
2798 val
.GetString().c_str(),
2799 colDefs
[colNumber
].SzDataObj
-1); //TODO: glt ??? * sizeof(wxChar) ???
2803 *(long *)(colDefs
[colNumber
].PtrDataObj
) = val
;
2807 *(short *)(colDefs
[colNumber
].PtrDataObj
) = (short)val
.GetLong();
2810 *(unsigned long *)(colDefs
[colNumber
].PtrDataObj
) = val
.GetLong();
2813 *(wxChar
*)(colDefs
[colNumber
].PtrDataObj
) = val
.GetChar();
2815 case SQL_C_UTINYINT
:
2816 *(wxChar
*)(colDefs
[colNumber
].PtrDataObj
) = val
.GetChar();
2819 *(unsigned short *)(colDefs
[colNumber
].PtrDataObj
) = (unsigned short)val
.GetLong();
2821 //FIXME: Add proper wxDateTime support to wxVariant..
2824 DATE_STRUCT
*dataptr
=
2825 (DATE_STRUCT
*)colDefs
[colNumber
].PtrDataObj
;
2827 dataptr
->year
= (SWORD
)dateval
.GetYear();
2828 dataptr
->month
= (UWORD
)(dateval
.GetMonth()+1);
2829 dataptr
->day
= (UWORD
)dateval
.GetDay();
2834 TIME_STRUCT
*dataptr
=
2835 (TIME_STRUCT
*)colDefs
[colNumber
].PtrDataObj
;
2837 dataptr
->hour
= dateval
.GetHour();
2838 dataptr
->minute
= dateval
.GetMinute();
2839 dataptr
->second
= dateval
.GetSecond();
2842 case SQL_C_TIMESTAMP
:
2844 TIMESTAMP_STRUCT
*dataptr
=
2845 (TIMESTAMP_STRUCT
*)colDefs
[colNumber
].PtrDataObj
;
2846 dataptr
->year
= (SWORD
)dateval
.GetYear();
2847 dataptr
->month
= (UWORD
)(dateval
.GetMonth()+1);
2848 dataptr
->day
= (UWORD
)dateval
.GetDay();
2850 dataptr
->hour
= dateval
.GetHour();
2851 dataptr
->minute
= dateval
.GetMinute();
2852 dataptr
->second
= dateval
.GetSecond();
2856 *(double *)(colDefs
[colNumber
].PtrDataObj
) = val
;
2861 } // if (!val.IsNull())
2862 } // wxDbTable::SetCol()
2865 GenericKey
wxDbTable::GetKey()
2870 blk
= malloc(m_keysize
);
2871 blkptr
= (wxChar
*) blk
;
2874 for (i
=0; i
< m_numCols
; i
++)
2876 if (colDefs
[i
].KeyField
)
2878 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2879 blkptr
+= colDefs
[i
].SzDataObj
;
2883 GenericKey k
= GenericKey(blk
, m_keysize
);
2887 } // wxDbTable::GetKey()
2890 void wxDbTable::SetKey(const GenericKey
& k
)
2896 blkptr
= (wxChar
*)blk
;
2899 for (i
=0; i
< m_numCols
; i
++)
2901 if (colDefs
[i
].KeyField
)
2903 SetColNull((UWORD
)i
, false);
2904 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2905 blkptr
+= colDefs
[i
].SzDataObj
;
2908 } // wxDbTable::SetKey()
2911 #endif // wxUSE_ODBC