1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence, plus:
12 // Notice: This class library and its intellectual design are free of charge for use,
13 // modification, enhancement, debugging under the following conditions:
14 // 1) These classes may only be used as part of the implementation of a
15 // wxWindows-based application
16 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17 // user groups free of all charges for use with the wxWindows library.
18 // 3) These classes may not be distributed as part of any other class library,
19 // DLL, text (written or electronic), other than a complete distribution of
20 // the wxWindows GUI development toolkit.
21 ///////////////////////////////////////////////////////////////////////////////
28 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
29 #pragma implementation "dbtable.h"
32 #include "wx/wxprec.h"
38 #ifdef DBDEBUG_CONSOLE
44 #include "wx/ioswrap.h"
48 #include "wx/string.h"
49 #include "wx/object.h"
54 #include "wx/filefn.h"
62 #include "wx/dbtable.h"
65 // The HPUX preprocessor lines below were commented out on 8/20/97
66 // because macros.h currently redefines DEBUG and is unneeded.
68 // # include <macros.h>
71 # include <sys/minmax.h>
75 ULONG lastTableID
= 0;
83 void csstrncpyt(wxChar
*target
, const wxChar
*source
, int n
)
85 while ( (*target
++ = *source
++) != '\0' && --n
)
93 /********** wxDbColDef::wxDbColDef() Constructor **********/
94 wxDbColDef::wxDbColDef()
100 bool wxDbColDef::Initialize()
103 DbDataType
= DB_DATA_TYPE_INTEGER
;
104 SqlCtype
= SQL_C_LONG
;
109 InsertAllowed
= FALSE
;
115 } // wxDbColDef::Initialize()
118 /********** wxDbTable::wxDbTable() Constructor **********/
119 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
120 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
122 if (!initialize(pwxDb
, tblName
, numColumns
, qryTblName
, qryOnly
, tblPath
))
124 } // wxDbTable::wxDbTable()
127 /***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
128 wxDbTable::wxDbTable(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
129 const wxChar
*qryTblName
, bool qryOnly
, const wxString
&tblPath
)
131 wxString tempQryTblName
;
132 tempQryTblName
= qryTblName
;
133 if (!initialize(pwxDb
, tblName
, numColumns
, tempQryTblName
, qryOnly
, tblPath
))
135 } // wxDbTable::wxDbTable()
138 /********** wxDbTable::~wxDbTable() **********/
139 wxDbTable::~wxDbTable()
142 } // wxDbTable::~wxDbTable()
145 bool wxDbTable::initialize(wxDb
*pwxDb
, const wxString
&tblName
, const UWORD numColumns
,
146 const wxString
&qryTblName
, bool qryOnly
, const wxString
&tblPath
)
148 // Initializing member variables
149 pDb
= pwxDb
; // Pointer to the wxDb object
153 m_hstmtGridQuery
= 0;
154 hstmtDefault
= 0; // Initialized below
155 hstmtCount
= 0; // Initialized first time it is needed
162 noCols
= numColumns
; // Number of cols in the table
163 where
.Empty(); // Where clause
164 orderBy
.Empty(); // Order By clause
165 from
.Empty(); // From clause
166 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
171 queryTableName
.Empty();
173 wxASSERT(tblName
.Length());
179 tableName
= tblName
; // Table Name
180 if (tblPath
.Length())
181 tablePath
= tblPath
; // Table Path - used for dBase files
185 if (qryTblName
.Length()) // Name of the table/view to query
186 queryTableName
= qryTblName
;
188 queryTableName
= tblName
;
190 pDb
->incrementTableCount();
193 tableID
= ++lastTableID
;
194 s
.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName
.c_str(), tableID
, pDb
);
197 wxTablesInUse
*tableInUse
;
198 tableInUse
= new wxTablesInUse();
199 tableInUse
->tableName
= tblName
;
200 tableInUse
->tableID
= tableID
;
201 tableInUse
->pDb
= pDb
;
202 TablesInUse
.Append(tableInUse
);
207 // Grab the HENV and HDBC from the wxDb object
208 henv
= pDb
->GetHENV();
209 hdbc
= pDb
->GetHDBC();
211 // Allocate space for column definitions
213 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column definition
215 // Allocate statement handles for the table
218 // Allocate a separate statement handle for performing inserts
219 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
220 pDb
->DispAllErrors(henv
, hdbc
);
221 // Allocate a separate statement handle for performing deletes
222 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
223 pDb
->DispAllErrors(henv
, hdbc
);
224 // Allocate a separate statement handle for performing updates
225 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
226 pDb
->DispAllErrors(henv
, hdbc
);
228 // Allocate a separate statement handle for internal use
229 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
230 pDb
->DispAllErrors(henv
, hdbc
);
232 // Set the cursor type for the statement handles
233 cursorType
= SQL_CURSOR_STATIC
;
235 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
237 // Check to see if cursor type is supported
238 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
239 if (! wxStrcmp(pDb
->sqlState
, wxT("01S02"))) // Option Value Changed
241 // Datasource does not support static cursors. Driver
242 // will substitute a cursor type. Call SQLGetStmtOption()
243 // to determine which cursor type was selected.
244 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
245 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
246 #ifdef DBDEBUG_CONSOLE
247 cout
<< wxT("Static cursor changed to: ");
250 case SQL_CURSOR_FORWARD_ONLY
:
251 cout
<< wxT("Forward Only");
253 case SQL_CURSOR_STATIC
:
254 cout
<< wxT("Static");
256 case SQL_CURSOR_KEYSET_DRIVEN
:
257 cout
<< wxT("Keyset Driven");
259 case SQL_CURSOR_DYNAMIC
:
260 cout
<< wxT("Dynamic");
263 cout
<< endl
<< endl
;
266 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
268 // Force the use of a forward only cursor...
269 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
270 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
272 // Should never happen
273 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
280 pDb
->DispNextError();
281 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
284 #ifdef DBDEBUG_CONSOLE
286 cout
<< wxT("Cursor Type set to STATIC") << endl
<< endl
;
291 // Set the cursor type for the INSERT statement handle
292 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
293 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
294 // Set the cursor type for the DELETE statement handle
295 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
296 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
297 // Set the cursor type for the UPDATE statement handle
298 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
299 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
302 // Make the default cursor the active cursor
303 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
304 wxASSERT(hstmtDefault
);
305 hstmt
= *hstmtDefault
;
309 } // wxDbTable::initialize()
312 void wxDbTable::cleanup()
317 s
.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName
.c_str(), tableID
, pDb
);
324 TablesInUse
.DeleteContents(TRUE
);
328 pNode
= TablesInUse
.GetFirst();
329 while (pNode
&& !found
)
331 if (((wxTablesInUse
*)pNode
->GetData())->tableID
== tableID
)
334 if (!TablesInUse
.DeleteNode(pNode
))
335 wxLogDebug (s
,wxT("Unable to delete node!"));
338 pNode
= pNode
->GetNext();
343 msg
.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
344 wxLogDebug (msg
,wxT("NOTICE..."));
349 // Decrement the wxDb table count
351 pDb
->decrementTableCount();
353 // Delete memory allocated for column definitions
357 // Free statement handles
363 ODBC 3.0 says to use this form
364 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
366 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
367 pDb
->DispAllErrors(henv
, hdbc
);
373 ODBC 3.0 says to use this form
374 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
376 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
377 pDb
->DispAllErrors(henv
, hdbc
);
383 ODBC 3.0 says to use this form
384 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
386 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
387 pDb
->DispAllErrors(henv
, hdbc
);
393 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
394 pDb
->DispAllErrors(henv
, hdbc
);
397 // Delete dynamically allocated cursors
399 DeleteCursor(hstmtDefault
);
402 DeleteCursor(hstmtCount
);
404 if (m_hstmtGridQuery
)
405 DeleteCursor(m_hstmtGridQuery
);
407 } // wxDbTable::cleanup()
410 /***************************** PRIVATE FUNCTIONS *****************************/
413 void wxDbTable::setCbValueForColumn(int columnIndex
)
415 switch(colDefs
[columnIndex
].DbDataType
)
417 case DB_DATA_TYPE_VARCHAR
:
418 if (colDefs
[columnIndex
].Null
)
419 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
421 colDefs
[columnIndex
].CbValue
= SQL_NTS
;
423 case DB_DATA_TYPE_INTEGER
:
424 if (colDefs
[columnIndex
].Null
)
425 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
427 colDefs
[columnIndex
].CbValue
= 0;
429 case DB_DATA_TYPE_FLOAT
:
430 if (colDefs
[columnIndex
].Null
)
431 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
433 colDefs
[columnIndex
].CbValue
= 0;
435 case DB_DATA_TYPE_DATE
:
436 if (colDefs
[columnIndex
].Null
)
437 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
439 colDefs
[columnIndex
].CbValue
= 0;
441 case DB_DATA_TYPE_BLOB
:
442 if (colDefs
[columnIndex
].Null
)
443 colDefs
[columnIndex
].CbValue
= SQL_NULL_DATA
;
445 if (colDefs
[columnIndex
].SqlCtype
== SQL_C_BINARY
)
446 colDefs
[columnIndex
].CbValue
= 0;
447 else if (colDefs
[columnIndex
].SqlCtype
== SQL_C_CHAR
)
448 colDefs
[columnIndex
].CbValue
= SQL_LEN_DATA_AT_EXEC(0);
450 colDefs
[columnIndex
].CbValue
= SQL_LEN_DATA_AT_EXEC(colDefs
[columnIndex
].SzDataObj
);
455 /********** wxDbTable::bindParams() **********/
456 bool wxDbTable::bindParams(bool forUpdate
)
458 wxASSERT(!queryOnly
);
463 SDWORD precision
= 0;
466 // Bind each column of the table that should be bound
467 // to a parameter marker
471 for (i
=0, colNo
=1; i
< noCols
; i
++)
475 if (!colDefs
[i
].Updateable
)
480 if (!colDefs
[i
].InsertAllowed
)
484 switch(colDefs
[i
].DbDataType
)
486 case DB_DATA_TYPE_VARCHAR
:
487 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
488 precision
= colDefs
[i
].SzDataObj
;
491 case DB_DATA_TYPE_INTEGER
:
492 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
493 precision
= pDb
->GetTypeInfInteger().Precision
;
496 case DB_DATA_TYPE_FLOAT
:
497 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
498 precision
= pDb
->GetTypeInfFloat().Precision
;
499 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
500 // SQL Sybase Anywhere v5.5 returned a negative number for the
501 // MaxScale. This caused ODBC to kick out an error on ibscale.
502 // I check for this here and set the scale = precision.
504 // scale = (short) precision;
506 case DB_DATA_TYPE_DATE
:
507 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
508 precision
= pDb
->GetTypeInfDate().Precision
;
511 case DB_DATA_TYPE_BLOB
:
512 fSqlType
= pDb
->GetTypeInfBlob().FsqlType
;
518 setCbValueForColumn(i
);
522 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
523 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
524 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
526 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
531 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
532 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
533 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
535 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
540 // Completed successfully
543 } // wxDbTable::bindParams()
546 /********** wxDbTable::bindInsertParams() **********/
547 bool wxDbTable::bindInsertParams(void)
549 return bindParams(FALSE
);
550 } // wxDbTable::bindInsertParams()
553 /********** wxDbTable::bindUpdateParams() **********/
554 bool wxDbTable::bindUpdateParams(void)
556 return bindParams(TRUE
);
557 } // wxDbTable::bindUpdateParams()
560 /********** wxDbTable::bindCols() **********/
561 bool wxDbTable::bindCols(HSTMT cursor
)
565 // Bind each column of the table to a memory address for fetching data
567 for (i
= 0; i
< noCols
; i
++)
569 cb
= colDefs
[i
].CbValue
;
570 if (SQLBindCol(cursor
, (UWORD
)(i
+1), colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
571 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
572 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
575 // Completed successfully
578 } // wxDbTable::bindCols()
581 /********** wxDbTable::getRec() **********/
582 bool wxDbTable::getRec(UWORD fetchType
)
586 if (!pDb
->FwdOnlyCursors())
588 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
592 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
593 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
595 if (retcode
== SQL_NO_DATA_FOUND
)
598 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
602 // Set the Null member variable to indicate the Null state
603 // of each column just read in.
605 for (i
= 0; i
< noCols
; i
++)
606 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
611 // Fetch the next record from the record set
612 retcode
= SQLFetch(hstmt
);
613 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
615 if (retcode
== SQL_NO_DATA_FOUND
)
618 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
622 // Set the Null member variable to indicate the Null state
623 // of each column just read in.
625 for (i
= 0; i
< noCols
; i
++)
626 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
630 // Completed successfully
633 } // wxDbTable::getRec()
636 /********** wxDbTable::execDelete() **********/
637 bool wxDbTable::execDelete(const wxString
&pSqlStmt
)
641 // Execute the DELETE statement
642 retcode
= SQLExecDirect(hstmtDelete
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
644 if (retcode
== SQL_SUCCESS
||
645 retcode
== SQL_NO_DATA_FOUND
||
646 retcode
== SQL_SUCCESS_WITH_INFO
)
648 // Record deleted successfully
652 // Problem deleting record
653 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
655 } // wxDbTable::execDelete()
658 /********** wxDbTable::execUpdate() **********/
659 bool wxDbTable::execUpdate(const wxString
&pSqlStmt
)
663 // Execute the UPDATE statement
664 retcode
= SQLExecDirect(hstmtUpdate
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
666 if (retcode
== SQL_SUCCESS
||
667 retcode
== SQL_NO_DATA_FOUND
||
668 retcode
== SQL_SUCCESS_WITH_INFO
)
670 // Record updated successfully
673 else if (retcode
== SQL_NEED_DATA
)
676 retcode
= SQLParamData(hstmtUpdate
, &pParmID
);
677 while (retcode
== SQL_NEED_DATA
)
679 // Find the parameter
681 for (i
=0; i
< noCols
; i
++)
683 if (colDefs
[i
].PtrDataObj
== pParmID
)
685 // We found it. Store the parameter.
686 retcode
= SQLPutData(hstmtUpdate
, pParmID
, colDefs
[i
].SzDataObj
);
687 if (retcode
!= SQL_SUCCESS
)
689 pDb
->DispNextError();
690 return pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
696 if (retcode
== SQL_SUCCESS
||
697 retcode
== SQL_NO_DATA_FOUND
||
698 retcode
== SQL_SUCCESS_WITH_INFO
)
700 // Record updated successfully
705 // Problem updating record
706 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
708 } // wxDbTable::execUpdate()
711 /********** wxDbTable::query() **********/
712 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const wxString
&pSqlStmt
)
717 // The user may wish to select for update, but the DBMS may not be capable
718 selectForUpdate
= CanSelectForUpdate();
720 selectForUpdate
= FALSE
;
722 // Set the SQL SELECT string
723 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
724 { // so generate a select statement.
725 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
726 pDb
->WriteSqlLog(sqlStmt
);
729 // Make sure the cursor is closed first
730 if (!CloseCursor(hstmt
))
733 // Execute the SQL SELECT statement
735 retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
.c_str() : sqlStmt
.c_str()), SQL_NTS
);
736 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
737 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
739 // Completed successfully
742 } // wxDbTable::query()
745 /***************************** PUBLIC FUNCTIONS *****************************/
748 /********** wxDbTable::Open() **********/
749 bool wxDbTable::Open(bool checkPrivileges
, bool checkTableExists
)
759 // Calculate the maximum size of the concatenated
760 // keys for use with wxDbGrid
762 for (i
=0; i
< noCols
; i
++)
764 if (colDefs
[i
].KeyField
)
767 m_keysize
+= colDefs
[i
].SzDataObj
;
772 // Verify that the table exists in the database
773 if (checkTableExists
&& !pDb
->TableExists(tableName
, pDb
->GetUsername(), tablePath
))
775 s
= wxT("Table/view does not exist in the database");
776 if ( *(pDb
->dbInf
.accessibleTables
) == wxT('Y'))
777 s
+= wxT(", or you have no permissions.\n");
781 else if (checkPrivileges
)
783 // Verify the user has rights to access the table.
784 // Shortcut boolean evaluation to optimize out call to
787 // Unfortunately this optimization doesn't seem to be
789 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
790 !pDb
->TablePrivileges(tableName
,wxT("SELECT"), pDb
->GetUsername(), pDb
->GetUsername(), tablePath
))
791 s
= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
798 if (!tablePath
.IsEmpty())
799 p
.Printf(wxT("Error opening '%s/%s'.\n"),tablePath
.c_str(),tableName
.c_str());
801 p
.Printf(wxT("Error opening '%s'.\n"), tableName
.c_str());
804 pDb
->LogError(p
.GetData());
809 // Bind the member variables for field exchange between
810 // the wxDbTable object and the ODBC record.
813 if (!bindInsertParams()) // Inserts
816 if (!bindUpdateParams()) // Updates
820 if (!bindCols(*hstmtDefault
)) // Selects
823 if (!bindCols(hstmtInternal
)) // Internal use only
827 * Do NOT bind the hstmtCount cursor!!!
830 // Build an insert statement using parameter markers
831 if (!queryOnly
&& noCols
> 0)
833 bool needComma
= FALSE
;
834 sqlStmt
.Printf(wxT("INSERT INTO %s ("),
835 pDb
->SQLTableName(tableName
.c_str()).c_str());
836 for (i
= 0; i
< noCols
; i
++)
838 if (! colDefs
[i
].InsertAllowed
)
842 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
843 // sqlStmt += colDefs[i].ColName;
847 sqlStmt
+= wxT(") VALUES (");
849 int insertableCount
= 0;
851 for (i
= 0; i
< noCols
; i
++)
853 if (! colDefs
[i
].InsertAllowed
)
863 // Prepare the insert statement for execution
866 if (SQLPrepare(hstmtInsert
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
867 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
873 // Completed successfully
876 } // wxDbTable::Open()
879 /********** wxDbTable::Query() **********/
880 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
883 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
885 } // wxDbTable::Query()
888 /********** wxDbTable::QueryBySqlStmt() **********/
889 bool wxDbTable::QueryBySqlStmt(const wxString
&pSqlStmt
)
891 pDb
->WriteSqlLog(pSqlStmt
);
893 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
895 } // wxDbTable::QueryBySqlStmt()
898 /********** wxDbTable::QueryMatching() **********/
899 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
902 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
904 } // wxDbTable::QueryMatching()
907 /********** wxDbTable::QueryOnKeyFields() **********/
908 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
911 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
913 } // wxDbTable::QueryOnKeyFields()
916 /********** wxDbTable::GetPrev() **********/
917 bool wxDbTable::GetPrev(void)
919 if (pDb
->FwdOnlyCursors())
921 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
925 return(getRec(SQL_FETCH_PRIOR
));
927 } // wxDbTable::GetPrev()
930 /********** wxDbTable::operator-- **********/
931 bool wxDbTable::operator--(int)
933 if (pDb
->FwdOnlyCursors())
935 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
939 return(getRec(SQL_FETCH_PRIOR
));
941 } // wxDbTable::operator--
944 /********** wxDbTable::GetFirst() **********/
945 bool wxDbTable::GetFirst(void)
947 if (pDb
->FwdOnlyCursors())
949 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
953 return(getRec(SQL_FETCH_FIRST
));
955 } // wxDbTable::GetFirst()
958 /********** wxDbTable::GetLast() **********/
959 bool wxDbTable::GetLast(void)
961 if (pDb
->FwdOnlyCursors())
963 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
967 return(getRec(SQL_FETCH_LAST
));
969 } // wxDbTable::GetLast()
972 /********** wxDbTable::BuildDeleteStmt() **********/
973 void wxDbTable::BuildDeleteStmt(wxString
&pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
975 wxASSERT(!queryOnly
);
979 wxString whereClause
;
983 // Handle the case of DeleteWhere() and the where clause is blank. It should
984 // delete all records from the database in this case.
985 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
.Length() == 0))
987 pSqlStmt
.Printf(wxT("DELETE FROM %s"),
988 pDb
->SQLTableName(tableName
.c_str()).c_str());
992 pSqlStmt
.Printf(wxT("DELETE FROM %s WHERE "),
993 pDb
->SQLTableName(tableName
.c_str()).c_str());
995 // Append the WHERE clause to the SQL DELETE statement
998 case DB_DEL_KEYFIELDS
:
999 // If the datasource supports the ROWID column, build
1000 // the where on ROWID for efficiency purposes.
1001 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1002 if (CanUpdByROWID())
1005 wxChar rowid
[wxDB_ROWID_LEN
+1];
1007 // Get the ROWID value. If not successful retreiving the ROWID,
1008 // simply fall down through the code and build the WHERE clause
1009 // based on the key fields.
1010 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1012 pSqlStmt
+= wxT("ROWID = '");
1014 pSqlStmt
+= wxT("'");
1018 // Unable to delete by ROWID, so build a WHERE
1019 // clause based on the keyfields.
1020 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1021 pSqlStmt
+= whereClause
;
1024 pSqlStmt
+= pWhereClause
;
1026 case DB_DEL_MATCHING
:
1027 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1028 pSqlStmt
+= whereClause
;
1032 } // BuildDeleteStmt()
1035 /***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
1036 void wxDbTable::BuildDeleteStmt(wxChar
*pSqlStmt
, int typeOfDel
, const wxString
&pWhereClause
)
1038 wxString tempSqlStmt
;
1039 BuildDeleteStmt(tempSqlStmt
, typeOfDel
, pWhereClause
);
1040 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1041 } // wxDbTable::BuildDeleteStmt()
1044 /********** wxDbTable::BuildSelectStmt() **********/
1045 void wxDbTable::BuildSelectStmt(wxString
&pSqlStmt
, int typeOfSelect
, bool distinct
)
1047 wxString whereClause
;
1048 whereClause
.Empty();
1050 // Build a select statement to query the database
1051 pSqlStmt
= wxT("SELECT ");
1053 // SELECT DISTINCT values only?
1055 pSqlStmt
+= wxT("DISTINCT ");
1057 // Was a FROM clause specified to join tables to the base table?
1058 // Available for ::Query() only!!!
1059 bool appendFromClause
= FALSE
;
1060 #if wxODBC_BACKWARD_COMPATABILITY
1061 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
1062 appendFromClause
= TRUE
;
1064 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
1065 appendFromClause
= TRUE
;
1068 // Add the column list
1071 for (i
= 0; i
< noCols
; i
++)
1073 tStr
= colDefs
[i
].ColName
;
1074 // If joining tables, the base table column names must be qualified to avoid ambiguity
1075 if ((appendFromClause
|| pDb
->Dbms() == dbmsACCESS
) && !tStr
.Find(wxT('.')))
1077 pSqlStmt
+= pDb
->SQLTableName(queryTableName
.c_str());
1078 pSqlStmt
+= wxT(".");
1080 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1082 pSqlStmt
+= wxT(",");
1085 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1086 // the ROWID if querying distinct records. The rowid will always be unique.
1087 if (!distinct
&& CanUpdByROWID())
1089 // If joining tables, the base table column names must be qualified to avoid ambiguity
1090 if (appendFromClause
|| pDb
->Dbms() == dbmsACCESS
)
1092 pSqlStmt
+= wxT(",");
1093 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1094 // pSqlStmt += queryTableName;
1095 pSqlStmt
+= wxT(".ROWID");
1098 pSqlStmt
+= wxT(",ROWID");
1101 // Append the FROM tablename portion
1102 pSqlStmt
+= wxT(" FROM ");
1103 pSqlStmt
+= pDb
->SQLTableName(queryTableName
);
1104 // pSqlStmt += queryTableName;
1106 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1107 // The HOLDLOCK keyword follows the table name in the from clause.
1108 // Each table in the from clause must specify HOLDLOCK or
1109 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1110 // is parsed but ignored in SYBASE Transact-SQL.
1111 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
1112 pSqlStmt
+= wxT(" HOLDLOCK");
1114 if (appendFromClause
)
1117 // Append the WHERE clause. Either append the where clause for the class
1118 // or build a where clause. The typeOfSelect determines this.
1119 switch(typeOfSelect
)
1121 case DB_SELECT_WHERE
:
1122 #if wxODBC_BACKWARD_COMPATABILITY
1123 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
1125 if (where
.Length()) // May not want a where clause!!!
1128 pSqlStmt
+= wxT(" WHERE ");
1132 case DB_SELECT_KEYFIELDS
:
1133 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1134 if (whereClause
.Length())
1136 pSqlStmt
+= wxT(" WHERE ");
1137 pSqlStmt
+= whereClause
;
1140 case DB_SELECT_MATCHING
:
1141 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1142 if (whereClause
.Length())
1144 pSqlStmt
+= wxT(" WHERE ");
1145 pSqlStmt
+= whereClause
;
1150 // Append the ORDER BY clause
1151 #if wxODBC_BACKWARD_COMPATABILITY
1152 if (orderBy
&& wxStrlen(orderBy
))
1154 if (orderBy
.Length())
1157 pSqlStmt
+= wxT(" ORDER BY ");
1158 pSqlStmt
+= orderBy
;
1161 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1162 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1163 // HOLDLOCK for Sybase.
1164 if (selectForUpdate
&& CanSelectForUpdate())
1165 pSqlStmt
+= wxT(" FOR UPDATE");
1167 } // wxDbTable::BuildSelectStmt()
1170 /***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1171 void wxDbTable::BuildSelectStmt(wxChar
*pSqlStmt
, int typeOfSelect
, bool distinct
)
1173 wxString tempSqlStmt
;
1174 BuildSelectStmt(tempSqlStmt
, typeOfSelect
, distinct
);
1175 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1176 } // wxDbTable::BuildSelectStmt()
1179 /********** wxDbTable::BuildUpdateStmt() **********/
1180 void wxDbTable::BuildUpdateStmt(wxString
&pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1182 wxASSERT(!queryOnly
);
1186 wxString whereClause
;
1187 whereClause
.Empty();
1189 bool firstColumn
= TRUE
;
1191 pSqlStmt
.Printf(wxT("UPDATE %s SET "),
1192 pDb
->SQLTableName(tableName
.c_str()).c_str());
1194 // Append a list of columns to be updated
1196 for (i
= 0; i
< noCols
; i
++)
1198 // Only append Updateable columns
1199 if (colDefs
[i
].Updateable
)
1202 pSqlStmt
+= wxT(",");
1204 firstColumn
= FALSE
;
1206 pSqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1207 // pSqlStmt += colDefs[i].ColName;
1208 pSqlStmt
+= wxT(" = ?");
1212 // Append the WHERE clause to the SQL UPDATE statement
1213 pSqlStmt
+= wxT(" WHERE ");
1216 case DB_UPD_KEYFIELDS
:
1217 // If the datasource supports the ROWID column, build
1218 // the where on ROWID for efficiency purposes.
1219 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1220 if (CanUpdByROWID())
1223 wxChar rowid
[wxDB_ROWID_LEN
+1];
1225 // Get the ROWID value. If not successful retreiving the ROWID,
1226 // simply fall down through the code and build the WHERE clause
1227 // based on the key fields.
1228 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1230 pSqlStmt
+= wxT("ROWID = '");
1232 pSqlStmt
+= wxT("'");
1236 // Unable to delete by ROWID, so build a WHERE
1237 // clause based on the keyfields.
1238 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1239 pSqlStmt
+= whereClause
;
1242 pSqlStmt
+= pWhereClause
;
1245 } // BuildUpdateStmt()
1248 /***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1249 void wxDbTable::BuildUpdateStmt(wxChar
*pSqlStmt
, int typeOfUpd
, const wxString
&pWhereClause
)
1251 wxString tempSqlStmt
;
1252 BuildUpdateStmt(tempSqlStmt
, typeOfUpd
, pWhereClause
);
1253 wxStrcpy(pSqlStmt
, tempSqlStmt
);
1254 } // BuildUpdateStmt()
1257 /********** wxDbTable::BuildWhereClause() **********/
1258 void wxDbTable::BuildWhereClause(wxString
&pWhereClause
, int typeOfWhere
,
1259 const wxString
&qualTableName
, bool useLikeComparison
)
1261 * Note: BuildWhereClause() currently ignores timestamp columns.
1262 * They are not included as part of the where clause.
1265 bool moreThanOneColumn
= FALSE
;
1268 // Loop through the columns building a where clause as you go
1270 for (colNo
= 0; colNo
< noCols
; colNo
++)
1272 // Determine if this column should be included in the WHERE clause
1273 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[colNo
].KeyField
) ||
1274 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(colNo
))))
1276 // Skip over timestamp columns
1277 if (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
)
1279 // If there is more than 1 column, join them with the keyword "AND"
1280 if (moreThanOneColumn
)
1281 pWhereClause
+= wxT(" AND ");
1283 moreThanOneColumn
= TRUE
;
1285 // Concatenate where phrase for the column
1286 wxString tStr
= colDefs
[colNo
].ColName
;
1288 if (qualTableName
.Length() && !tStr
.Find(wxT('.')))
1290 pWhereClause
+= pDb
->SQLTableName(qualTableName
);
1291 pWhereClause
+= wxT(".");
1293 pWhereClause
+= pDb
->SQLColumnName(colDefs
[colNo
].ColName
);
1295 if (useLikeComparison
&& (colDefs
[colNo
].SqlCtype
== SQL_C_CHAR
))
1296 pWhereClause
+= wxT(" LIKE ");
1298 pWhereClause
+= wxT(" = ");
1300 switch(colDefs
[colNo
].SqlCtype
)
1303 colValue
.Printf(wxT("'%s'"), (UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
);
1307 colValue
.Printf(wxT("%hi"), *((SWORD
*) colDefs
[colNo
].PtrDataObj
));
1310 colValue
.Printf(wxT("%hu"), *((UWORD
*) colDefs
[colNo
].PtrDataObj
));
1314 colValue
.Printf(wxT("%li"), *((SDWORD
*) colDefs
[colNo
].PtrDataObj
));
1317 colValue
.Printf(wxT("%lu"), *((UDWORD
*) colDefs
[colNo
].PtrDataObj
));
1320 colValue
.Printf(wxT("%.6f"), *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
));
1323 colValue
.Printf(wxT("%.6f"), *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
));
1328 strMsg
.Printf(wxT("wxDbTable::bindParams(): Unknown column type for colDefs %d colName %s"),
1329 colNo
,colDefs
[colNo
].ColName
);
1330 wxFAIL_MSG(wxT(strMsg
));
1334 pWhereClause
+= colValue
;
1337 } // wxDbTable::BuildWhereClause()
1340 /***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1341 void wxDbTable::BuildWhereClause(wxChar
*pWhereClause
, int typeOfWhere
,
1342 const wxString
&qualTableName
, bool useLikeComparison
)
1344 wxString tempSqlStmt
;
1345 BuildWhereClause(tempSqlStmt
, typeOfWhere
, qualTableName
, useLikeComparison
);
1346 wxStrcpy(pWhereClause
, tempSqlStmt
);
1347 } // wxDbTable::BuildWhereClause()
1350 /********** wxDbTable::GetRowNum() **********/
1351 UWORD
wxDbTable::GetRowNum(void)
1355 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1357 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1361 // Completed successfully
1362 return((UWORD
) rowNum
);
1364 } // wxDbTable::GetRowNum()
1367 /********** wxDbTable::CloseCursor() **********/
1368 bool wxDbTable::CloseCursor(HSTMT cursor
)
1370 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1371 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1373 // Completed successfully
1376 } // wxDbTable::CloseCursor()
1379 /********** wxDbTable::CreateTable() **********/
1380 bool wxDbTable::CreateTable(bool attemptDrop
)
1388 #ifdef DBDEBUG_CONSOLE
1389 cout
<< wxT("Creating Table ") << tableName
<< wxT("...") << endl
;
1393 if (attemptDrop
&& !DropTable())
1397 #ifdef DBDEBUG_CONSOLE
1398 for (i
= 0; i
< noCols
; i
++)
1400 // Exclude derived columns since they are NOT part of the base table
1401 if (colDefs
[i
].DerivedCol
)
1403 cout
<< i
+ 1 << wxT(": ") << colDefs
[i
].ColName
<< wxT("; ");
1404 switch(colDefs
[i
].DbDataType
)
1406 case DB_DATA_TYPE_VARCHAR
:
1407 cout
<< pDb
->GetTypeInfVarchar().TypeName
<< wxT("(") << colDefs
[i
].SzDataObj
<< wxT(")");
1409 case DB_DATA_TYPE_INTEGER
:
1410 cout
<< pDb
->GetTypeInfInteger().TypeName
;
1412 case DB_DATA_TYPE_FLOAT
:
1413 cout
<< pDb
->GetTypeInfFloat().TypeName
;
1415 case DB_DATA_TYPE_DATE
:
1416 cout
<< pDb
->GetTypeInfDate().TypeName
;
1418 case DB_DATA_TYPE_BLOB
:
1419 cout
<< pDb
->GetTypeInfBlob().TypeName
;
1426 // Build a CREATE TABLE string from the colDefs structure.
1427 bool needComma
= FALSE
;
1429 sqlStmt
.Printf(wxT("CREATE TABLE %s ("),
1430 pDb
->SQLTableName(tableName
.c_str()).c_str());
1432 for (i
= 0; i
< noCols
; i
++)
1434 // Exclude derived columns since they are NOT part of the base table
1435 if (colDefs
[i
].DerivedCol
)
1439 sqlStmt
+= wxT(",");
1441 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1442 // sqlStmt += colDefs[i].ColName;
1443 sqlStmt
+= wxT(" ");
1445 switch(colDefs
[i
].DbDataType
)
1447 case DB_DATA_TYPE_VARCHAR
:
1448 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1450 case DB_DATA_TYPE_INTEGER
:
1451 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1453 case DB_DATA_TYPE_FLOAT
:
1454 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1456 case DB_DATA_TYPE_DATE
:
1457 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1459 case DB_DATA_TYPE_BLOB
:
1460 sqlStmt
+= pDb
->GetTypeInfBlob().TypeName
;
1463 // For varchars, append the size of the string
1464 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
&&
1465 (pDb
->Dbms() != dbmsMY_SQL
|| pDb
->GetTypeInfVarchar().TypeName
!= _T("text")))// ||
1466 // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
1469 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1473 if (pDb
->Dbms() == dbmsDB2
||
1474 pDb
->Dbms() == dbmsMY_SQL
||
1475 pDb
->Dbms() == dbmsSYBASE_ASE
||
1476 pDb
->Dbms() == dbmsINTERBASE
||
1477 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1479 if (colDefs
[i
].KeyField
)
1481 sqlStmt
+= wxT(" NOT NULL");
1487 // If there is a primary key defined, include it in the create statement
1488 for (i
= j
= 0; i
< noCols
; i
++)
1490 if (colDefs
[i
].KeyField
)
1496 if ( j
&& (pDb
->Dbms() != dbmsDBASE
)
1497 && (pDb
->Dbms() != dbmsXBASE_SEQUITER
) ) // Found a keyfield
1499 switch (pDb
->Dbms())
1503 case dbmsSYBASE_ASA
:
1504 case dbmsSYBASE_ASE
:
1507 // MySQL goes out on this one. We also declare the relevant key NON NULL above
1508 sqlStmt
+= wxT(",PRIMARY KEY (");
1513 sqlStmt
+= wxT(",CONSTRAINT ");
1514 // DB2 is limited to 18 characters for index names
1515 if (pDb
->Dbms() == dbmsDB2
)
1517 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."));
1518 sqlStmt
+= pDb
->SQLTableName(tableName
.substr(0, 13).c_str());
1519 // sqlStmt += tableName.substr(0, 13);
1522 sqlStmt
+= pDb
->SQLTableName(tableName
.c_str());
1523 // sqlStmt += tableName;
1525 sqlStmt
+= wxT("_PIDX PRIMARY KEY (");
1530 // List column name(s) of column(s) comprising the primary key
1531 for (i
= j
= 0; i
< noCols
; i
++)
1533 if (colDefs
[i
].KeyField
)
1535 if (j
++) // Multi part key, comma separate names
1536 sqlStmt
+= wxT(",");
1537 sqlStmt
+= pDb
->SQLColumnName(colDefs
[i
].ColName
);
1539 if (pDb
->Dbms() == dbmsMY_SQL
&&
1540 colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1543 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1548 sqlStmt
+= wxT(")");
1550 if (pDb
->Dbms() == dbmsINFORMIX
||
1551 pDb
->Dbms() == dbmsSYBASE_ASA
||
1552 pDb
->Dbms() == dbmsSYBASE_ASE
)
1554 sqlStmt
+= wxT(" CONSTRAINT ");
1555 sqlStmt
+= pDb
->SQLTableName(tableName
);
1556 // sqlStmt += tableName;
1557 sqlStmt
+= wxT("_PIDX");
1560 // Append the closing parentheses for the create table statement
1561 sqlStmt
+= wxT(")");
1563 pDb
->WriteSqlLog(sqlStmt
);
1565 #ifdef DBDEBUG_CONSOLE
1566 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1569 // Execute the CREATE TABLE statement
1570 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1571 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1573 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1574 pDb
->RollbackTrans();
1579 // Commit the transaction and close the cursor
1580 if (!pDb
->CommitTrans())
1582 if (!CloseCursor(hstmt
))
1585 // Database table created successfully
1588 } // wxDbTable::CreateTable()
1591 /********** wxDbTable::DropTable() **********/
1592 bool wxDbTable::DropTable()
1594 // NOTE: This function returns TRUE if the Table does not exist, but
1595 // only for identified databases. Code will need to be added
1596 // below for any other databases when those databases are defined
1597 // to handle this situation consistently
1601 sqlStmt
.Printf(wxT("DROP TABLE %s"),
1602 pDb
->SQLTableName(tableName
.c_str()).c_str());
1604 pDb
->WriteSqlLog(sqlStmt
);
1606 #ifdef DBDEBUG_CONSOLE
1607 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1610 RETCODE retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1611 if (retcode
!= SQL_SUCCESS
)
1613 // Check for "Base table not found" error and ignore
1614 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1615 if (wxStrcmp(pDb
->sqlState
, wxT("S0002")) /*&&
1616 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
1618 // Check for product specific error codes
1619 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // 5.x (and lower?)
1620 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1621 (pDb
->Dbms() == dbmsPERVASIVE_SQL
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) || // Returns an S1000 then an S0002
1622 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,wxT("08S01")))))
1624 pDb
->DispNextError();
1625 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1626 pDb
->RollbackTrans();
1627 // CloseCursor(hstmt);
1633 // Commit the transaction and close the cursor
1634 if (! pDb
->CommitTrans())
1636 if (! CloseCursor(hstmt
))
1640 } // wxDbTable::DropTable()
1643 /********** wxDbTable::CreateIndex() **********/
1644 bool wxDbTable::CreateIndex(const wxString
&idxName
, bool unique
, UWORD noIdxCols
,
1645 wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1649 // Drop the index first
1650 if (attemptDrop
&& !DropIndex(idxName
))
1653 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1654 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1655 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1656 // table was created, then months later you determine that an additional index while
1657 // give better performance, so you want to add an index).
1659 // The following block of code will modify the column definition to make the column be
1660 // defined with the "NOT NULL" qualifier.
1661 if (pDb
->Dbms() == dbmsMY_SQL
)
1666 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1670 // Find the column definition that has the ColName that matches the
1671 // index column name. We need to do this to get the DB_DATA_TYPE of
1672 // the index column, as MySQL's syntax for the ALTER column requires
1674 while (!found
&& (j
< this->noCols
))
1676 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1684 ok
= pDb
->ModifyColumn(tableName
, pIdxDefs
[i
].ColName
,
1685 colDefs
[j
].DbDataType
, colDefs
[j
].SzDataObj
,
1691 // retcode is not used
1692 wxODBC_ERRORS retcode
;
1693 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1694 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1695 // This line is just here for debug checking of the value
1696 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1707 pDb
->RollbackTrans();
1712 // Build a CREATE INDEX statement
1713 sqlStmt
= wxT("CREATE ");
1715 sqlStmt
+= wxT("UNIQUE ");
1717 sqlStmt
+= wxT("INDEX ");
1718 sqlStmt
+= pDb
->SQLTableName(idxName
);
1719 sqlStmt
+= wxT(" ON ");
1721 sqlStmt
+= pDb
->SQLTableName(tableName
);
1722 // sqlStmt += tableName;
1723 sqlStmt
+= wxT(" (");
1725 // Append list of columns making up index
1727 for (i
= 0; i
< noIdxCols
; i
++)
1729 sqlStmt
+= pDb
->SQLColumnName(pIdxDefs
[i
].ColName
);
1730 // sqlStmt += pIdxDefs[i].ColName;
1732 // MySQL requires a key length on VARCHAR keys
1733 if ( pDb
->Dbms() == dbmsMY_SQL
)
1735 // Find the details on this column
1737 for ( j
= 0; j
< noCols
; ++j
)
1739 if ( wxStrcmp( pIdxDefs
[i
].ColName
, colDefs
[j
].ColName
) == 0 )
1744 if ( colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1747 s
.Printf(wxT("(%d)"), colDefs
[i
].SzDataObj
);
1752 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1753 if (!((pDb
->Dbms() == dbmsMS_SQL_SERVER
) && (wxStrncmp(pDb
->dbInf
.dbmsVer
,_T("07"),2)==0)) &&
1754 !(pDb
->Dbms() == dbmsPOSTGRES
))
1756 if (pIdxDefs
[i
].Ascending
)
1757 sqlStmt
+= wxT(" ASC");
1759 sqlStmt
+= wxT(" DESC");
1762 wxASSERT_MSG(pIdxDefs
[i
].Ascending
, _T("Datasource does not support DESCending index columns"));
1764 if ((i
+ 1) < noIdxCols
)
1765 sqlStmt
+= wxT(",");
1768 // Append closing parentheses
1769 sqlStmt
+= wxT(")");
1771 pDb
->WriteSqlLog(sqlStmt
);
1773 #ifdef DBDEBUG_CONSOLE
1774 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1777 // Execute the CREATE INDEX statement
1778 if (SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1780 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1781 pDb
->RollbackTrans();
1786 // Commit the transaction and close the cursor
1787 if (! pDb
->CommitTrans())
1789 if (! CloseCursor(hstmt
))
1792 // Index Created Successfully
1795 } // wxDbTable::CreateIndex()
1798 /********** wxDbTable::DropIndex() **********/
1799 bool wxDbTable::DropIndex(const wxString
&idxName
)
1801 // NOTE: This function returns TRUE if the Index does not exist, but
1802 // only for identified databases. Code will need to be added
1803 // below for any other databases when those databases are defined
1804 // to handle this situation consistently
1808 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
||
1809 pDb
->Dbms() == dbmsDBASE
/*|| Paradox needs this syntax too when we add support*/)
1810 sqlStmt
.Printf(wxT("DROP INDEX %s ON %s"),
1811 pDb
->SQLTableName(idxName
.c_str()).c_str(),
1812 pDb
->SQLTableName(tableName
.c_str()).c_str());
1813 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1814 (pDb
->Dbms() == dbmsSYBASE_ASE
) ||
1815 (pDb
->Dbms() == dbmsXBASE_SEQUITER
))
1816 sqlStmt
.Printf(wxT("DROP INDEX %s.%s"),
1817 pDb
->SQLTableName(tableName
.c_str()).c_str(),
1818 pDb
->SQLTableName(idxName
.c_str()).c_str());
1820 sqlStmt
.Printf(wxT("DROP INDEX %s"),
1821 pDb
->SQLTableName(idxName
.c_str()).c_str());
1823 pDb
->WriteSqlLog(sqlStmt
);
1825 #ifdef DBDEBUG_CONSOLE
1826 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1829 if (SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1831 // Check for "Index not found" error and ignore
1832 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1833 if (wxStrcmp(pDb
->sqlState
,wxT("S0012"))) // "Index not found"
1835 // Check for product specific error codes
1836 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,wxT("42000"))) || // v5.x (and lower?)
1837 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,wxT("37000"))) ||
1838 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
1839 (pDb
->Dbms() == dbmsINTERBASE
&& !wxStrcmp(pDb
->sqlState
,wxT("S1000"))) ||
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 colNo
= 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
&& (colNo
!= 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 noCols-1
1880 if (colNo
>= noCols
|| colNo
< 0)
1887 tempStr
+= wxT(",");
1889 tempStr
+= colDefs
[colNo
].ColName
;
1890 colNo
= 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
< noCols
; 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
);
1952 // Record inserted into the datasource successfully
1955 } // wxDbTable::Insert()
1958 /********** wxDbTable::Update() **********/
1959 bool wxDbTable::Update(void)
1961 wxASSERT(!queryOnly
);
1967 // Build the SQL UPDATE statement
1968 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1970 pDb
->WriteSqlLog(sqlStmt
);
1972 #ifdef DBDEBUG_CONSOLE
1973 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1976 // Execute the SQL UPDATE statement
1977 return(execUpdate(sqlStmt
));
1979 } // wxDbTable::Update()
1982 /********** wxDbTable::Update(pSqlStmt) **********/
1983 bool wxDbTable::Update(const wxString
&pSqlStmt
)
1985 wxASSERT(!queryOnly
);
1989 pDb
->WriteSqlLog(pSqlStmt
);
1991 return(execUpdate(pSqlStmt
));
1993 } // wxDbTable::Update(pSqlStmt)
1996 /********** wxDbTable::UpdateWhere() **********/
1997 bool wxDbTable::UpdateWhere(const wxString
&pWhereClause
)
1999 wxASSERT(!queryOnly
);
2005 // Build the SQL UPDATE statement
2006 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
2008 pDb
->WriteSqlLog(sqlStmt
);
2010 #ifdef DBDEBUG_CONSOLE
2011 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
2014 // Execute the SQL UPDATE statement
2015 return(execUpdate(sqlStmt
));
2017 } // wxDbTable::UpdateWhere()
2020 /********** wxDbTable::Delete() **********/
2021 bool wxDbTable::Delete(void)
2023 wxASSERT(!queryOnly
);
2030 // Build the SQL DELETE statement
2031 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
2033 pDb
->WriteSqlLog(sqlStmt
);
2035 // Execute the SQL DELETE statement
2036 return(execDelete(sqlStmt
));
2038 } // wxDbTable::Delete()
2041 /********** wxDbTable::DeleteWhere() **********/
2042 bool wxDbTable::DeleteWhere(const wxString
&pWhereClause
)
2044 wxASSERT(!queryOnly
);
2051 // Build the SQL DELETE statement
2052 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
2054 pDb
->WriteSqlLog(sqlStmt
);
2056 // Execute the SQL DELETE statement
2057 return(execDelete(sqlStmt
));
2059 } // wxDbTable::DeleteWhere()
2062 /********** wxDbTable::DeleteMatching() **********/
2063 bool wxDbTable::DeleteMatching(void)
2065 wxASSERT(!queryOnly
);
2072 // Build the SQL DELETE statement
2073 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
2075 pDb
->WriteSqlLog(sqlStmt
);
2077 // Execute the SQL DELETE statement
2078 return(execDelete(sqlStmt
));
2080 } // wxDbTable::DeleteMatching()
2083 /********** wxDbTable::IsColNull() **********/
2084 bool wxDbTable::IsColNull(UWORD colNo
) const
2087 This logic is just not right. It would indicate TRUE
2088 if a numeric field were set to a value of 0.
2090 switch(colDefs[colNo].SqlCtype)
2093 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
2095 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
2097 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
2099 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
2101 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
2103 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
2105 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
2106 case SQL_C_TIMESTAMP:
2107 TIMESTAMP_STRUCT *pDt;
2108 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
2109 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
2117 return (colDefs
[colNo
].Null
);
2118 } // wxDbTable::IsColNull()
2121 /********** wxDbTable::CanSelectForUpdate() **********/
2122 bool wxDbTable::CanSelectForUpdate(void)
2127 if (pDb
->Dbms() == dbmsMY_SQL
)
2130 if ((pDb
->Dbms() == dbmsORACLE
) ||
2131 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
2136 } // wxDbTable::CanSelectForUpdate()
2139 /********** wxDbTable::CanUpdByROWID() **********/
2140 bool wxDbTable::CanUpdByROWID(void)
2143 * NOTE: Returning FALSE for now until this can be debugged,
2144 * as the ROWID is not getting updated correctly
2148 if (pDb->Dbms() == dbmsORACLE)
2153 } // wxDbTable::CanUpdByROWID()
2156 /********** wxDbTable::IsCursorClosedOnCommit() **********/
2157 bool wxDbTable::IsCursorClosedOnCommit(void)
2159 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
2164 } // wxDbTable::IsCursorClosedOnCommit()
2168 /********** wxDbTable::ClearMemberVar() **********/
2169 void wxDbTable::ClearMemberVar(UWORD colNo
, bool setToNull
)
2171 wxASSERT(colNo
< noCols
);
2173 switch(colDefs
[colNo
].SqlCtype
)
2176 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
2179 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2182 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2185 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2188 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
2191 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2194 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
2196 case SQL_C_TIMESTAMP
:
2197 TIMESTAMP_STRUCT
*pDt
;
2198 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
2211 } // wxDbTable::ClearMemberVar()
2214 /********** wxDbTable::ClearMemberVars() **********/
2215 void wxDbTable::ClearMemberVars(bool setToNull
)
2219 // Loop through the columns setting each member variable to zero
2220 for (i
=0; i
< noCols
; i
++)
2221 ClearMemberVar(i
,setToNull
);
2223 } // wxDbTable::ClearMemberVars()
2226 /********** wxDbTable::SetQueryTimeout() **********/
2227 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
2229 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2230 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
2231 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2232 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
2233 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2234 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
2235 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
2236 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
2238 // Completed Successfully
2241 } // wxDbTable::SetQueryTimeout()
2244 /********** wxDbTable::SetColDefs() **********/
2245 void wxDbTable::SetColDefs(UWORD index
, const wxString
&fieldName
, int dataType
, void *pData
,
2246 SWORD cType
, int size
, bool keyField
, bool upd
,
2247 bool insAllow
, bool derivedCol
)
2249 wxASSERT_MSG( index
< noCols
,
2250 _T("Specified column index exceeds the maximum number of columns for this table.") );
2252 if (!colDefs
) // May happen if the database connection fails
2255 if (fieldName
.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
2257 wxStrncpy(colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2258 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2262 tmpMsg
.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2263 fieldName
.c_str(),colDefs
[index
].ColName
);
2265 #endif // __WXDEBUG__
2268 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2270 colDefs
[index
].DbDataType
= dataType
;
2271 colDefs
[index
].PtrDataObj
= pData
;
2272 colDefs
[index
].SqlCtype
= cType
;
2273 colDefs
[index
].SzDataObj
= size
;
2274 colDefs
[index
].KeyField
= keyField
;
2275 colDefs
[index
].DerivedCol
= derivedCol
;
2276 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2279 colDefs
[index
].Updateable
= FALSE
;
2280 colDefs
[index
].InsertAllowed
= FALSE
;
2284 colDefs
[index
].Updateable
= upd
;
2285 colDefs
[index
].InsertAllowed
= insAllow
;
2288 colDefs
[index
].Null
= FALSE
;
2290 } // wxDbTable::SetColDefs()
2293 /********** wxDbTable::SetColDefs() **********/
2294 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, UWORD numCols
)
2297 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2303 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2305 for (index
= 0; index
< numCols
; index
++)
2307 // Process the fields
2308 switch (pColInfs
[index
].dbDataType
)
2310 case DB_DATA_TYPE_VARCHAR
:
2311 pColDataPtrs
[index
].PtrDataObj
= new wxChar
[pColInfs
[index
].bufferLength
+1];
2312 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2313 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2315 case DB_DATA_TYPE_INTEGER
:
2316 // Can be long or short
2317 if (pColInfs
[index
].bufferLength
== sizeof(long))
2319 pColDataPtrs
[index
].PtrDataObj
= new long;
2320 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2321 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2325 pColDataPtrs
[index
].PtrDataObj
= new short;
2326 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2327 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2330 case DB_DATA_TYPE_FLOAT
:
2331 // Can be float or double
2332 if (pColInfs
[index
].bufferLength
== sizeof(float))
2334 pColDataPtrs
[index
].PtrDataObj
= new float;
2335 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2336 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2340 pColDataPtrs
[index
].PtrDataObj
= new double;
2341 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2342 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2345 case DB_DATA_TYPE_DATE
:
2346 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2347 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2348 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2350 case DB_DATA_TYPE_BLOB
:
2351 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2352 pColDataPtrs
[index
].PtrDataObj
= /*BLOB ADDITION NEEDED*/NULL
;
2353 pColDataPtrs
[index
].SzDataObj
= /*BLOB ADDITION NEEDED*/sizeof(void *);
2354 pColDataPtrs
[index
].SqlCtype
= SQL_VARBINARY
;
2357 if (pColDataPtrs
[index
].PtrDataObj
!= NULL
)
2358 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2361 // Unable to build all the column definitions, as either one of
2362 // the calls to "new" failed above, or there was a BLOB field
2363 // to have a column definition for. If BLOBs are to be used,
2364 // the other form of ::SetColDefs() must be used, as it is impossible
2365 // to know the maximum size to create the PtrDataObj to be.
2366 delete [] pColDataPtrs
;
2372 return (pColDataPtrs
);
2374 } // wxDbTable::SetColDefs()
2377 /********** wxDbTable::SetCursor() **********/
2378 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2380 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2381 hstmt
= *hstmtDefault
;
2383 hstmt
= *hstmtActivate
;
2385 } // wxDbTable::SetCursor()
2388 /********** wxDbTable::Count(const wxString &) **********/
2389 ULONG
wxDbTable::Count(const wxString
&args
)
2395 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2396 sqlStmt
= wxT("SELECT COUNT(");
2398 sqlStmt
+= wxT(") FROM ");
2399 sqlStmt
+= pDb
->SQLTableName(queryTableName
);
2400 // sqlStmt += queryTableName;
2401 #if wxODBC_BACKWARD_COMPATABILITY
2402 if (from
&& wxStrlen(from
))
2408 // Add the where clause if one is provided
2409 #if wxODBC_BACKWARD_COMPATABILITY
2410 if (where
&& wxStrlen(where
))
2415 sqlStmt
+= wxT(" WHERE ");
2419 pDb
->WriteSqlLog(sqlStmt
);
2421 // Initialize the Count cursor if it's not already initialized
2424 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2425 wxASSERT(hstmtCount
);
2430 // Execute the SQL statement
2431 if (SQLExecDirect(*hstmtCount
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2433 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2438 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2440 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2444 // Obtain the result
2445 if (SQLGetData(*hstmtCount
, (UWORD
)1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2447 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2452 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2453 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2455 // Return the record count
2458 } // wxDbTable::Count()
2461 /********** wxDbTable::Refresh() **********/
2462 bool wxDbTable::Refresh(void)
2466 // Switch to the internal cursor so any active cursors are not corrupted
2467 HSTMT currCursor
= GetCursor();
2468 hstmt
= hstmtInternal
;
2469 #if wxODBC_BACKWARD_COMPATABILITY
2470 // Save the where and order by clauses
2471 wxChar
*saveWhere
= where
;
2472 wxChar
*saveOrderBy
= orderBy
;
2474 wxString saveWhere
= where
;
2475 wxString saveOrderBy
= orderBy
;
2477 // Build a where clause to refetch the record with. Try and use the
2478 // ROWID if it's available, ow use the key fields.
2479 wxString whereClause
;
2480 whereClause
.Empty();
2482 if (CanUpdByROWID())
2485 wxChar rowid
[wxDB_ROWID_LEN
+1];
2487 // Get the ROWID value. If not successful retreiving the ROWID,
2488 // simply fall down through the code and build the WHERE clause
2489 // based on the key fields.
2490 if (SQLGetData(hstmt
, (UWORD
)(noCols
+1), SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2492 whereClause
+= pDb
->SQLTableName(queryTableName
);
2493 // whereClause += queryTableName;
2494 whereClause
+= wxT(".ROWID = '");
2495 whereClause
+= rowid
;
2496 whereClause
+= wxT("'");
2500 // If unable to use the ROWID, build a where clause from the keyfields
2501 if (wxStrlen(whereClause
) == 0)
2502 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2504 // Requery the record
2505 where
= whereClause
;
2510 if (result
&& !GetNext())
2513 // Switch back to original cursor
2514 SetCursor(&currCursor
);
2516 // Free the internal cursor
2517 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2518 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2520 // Restore the original where and order by clauses
2522 orderBy
= saveOrderBy
;
2526 } // wxDbTable::Refresh()
2529 /********** wxDbTable::SetColNull() **********/
2530 bool wxDbTable::SetColNull(UWORD colNo
, bool set
)
2534 colDefs
[colNo
].Null
= set
;
2535 if (set
) // Blank out the values in the member variable
2536 ClearMemberVar(colNo
, FALSE
); // Must call with FALSE here, or infinite recursion will happen
2538 setCbValueForColumn(colNo
);
2545 } // wxDbTable::SetColNull()
2548 /********** wxDbTable::SetColNull() **********/
2549 bool wxDbTable::SetColNull(const wxString
&colName
, bool set
)
2552 for (colNo
= 0; colNo
< noCols
; colNo
++)
2554 if (!wxStricmp(colName
, colDefs
[colNo
].ColName
))
2560 colDefs
[colNo
].Null
= set
;
2561 if (set
) // Blank out the values in the member variable
2562 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE here, or infinite recursion will happen
2564 setCbValueForColumn(colNo
);
2571 } // wxDbTable::SetColNull()
2574 /********** wxDbTable::GetNewCursor() **********/
2575 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2577 HSTMT
*newHSTMT
= new HSTMT
;
2582 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2584 pDb
->DispAllErrors(henv
, hdbc
);
2589 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2591 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2598 if (!bindCols(*newHSTMT
))
2606 SetCursor(newHSTMT
);
2610 } // wxDbTable::GetNewCursor()
2613 /********** wxDbTable::DeleteCursor() **********/
2614 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2618 if (!hstmtDel
) // Cursor already deleted
2622 ODBC 3.0 says to use this form
2623 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2626 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2628 pDb
->DispAllErrors(henv
, hdbc
);
2636 } // wxDbTable::DeleteCursor()
2638 //////////////////////////////////////////////////////////////
2639 // wxDbGrid support functions
2640 //////////////////////////////////////////////////////////////
2642 void wxDbTable::SetRowMode(const rowmode_t rowmode
)
2644 if (!m_hstmtGridQuery
)
2646 m_hstmtGridQuery
= GetNewCursor(FALSE
,FALSE
);
2647 if (!bindCols(*m_hstmtGridQuery
))
2651 m_rowmode
= rowmode
;
2654 case WX_ROW_MODE_QUERY
:
2655 SetCursor(m_hstmtGridQuery
);
2657 case WX_ROW_MODE_INDIVIDUAL
:
2658 SetCursor(hstmtDefault
);
2663 } // wxDbTable::SetRowMode()
2666 wxVariant
wxDbTable::GetCol(const int colNo
) const
2669 if ((colNo
< noCols
) && (!IsColNull(colNo
)))
2671 switch (colDefs
[colNo
].SqlCtype
)
2675 val
= (wxChar
*)(colDefs
[colNo
].PtrDataObj
);
2679 val
= *(long *)(colDefs
[colNo
].PtrDataObj
);
2683 val
= (long int )(*(short *)(colDefs
[colNo
].PtrDataObj
));
2686 val
= (long)(*(unsigned long *)(colDefs
[colNo
].PtrDataObj
));
2689 val
= (long)(*(wxChar
*)(colDefs
[colNo
].PtrDataObj
));
2691 case SQL_C_UTINYINT
:
2692 val
= (long)(*(wxChar
*)(colDefs
[colNo
].PtrDataObj
));
2695 val
= (long)(*(UWORD
*)(colDefs
[colNo
].PtrDataObj
));
2698 val
= (DATE_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2701 val
= (TIME_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2703 case SQL_C_TIMESTAMP
:
2704 val
= (TIMESTAMP_STRUCT
*)(colDefs
[colNo
].PtrDataObj
);
2707 val
= *(double *)(colDefs
[colNo
].PtrDataObj
);
2714 } // wxDbTable::GetCol()
2717 void wxDbTable::SetCol(const int colNo
, const wxVariant val
)
2719 //FIXME: Add proper wxDateTime support to wxVariant..
2722 SetColNull(colNo
, val
.IsNull());
2726 if ((colDefs
[colNo
].SqlCtype
== SQL_C_DATE
)
2727 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIME
)
2728 || (colDefs
[colNo
].SqlCtype
== SQL_C_TIMESTAMP
))
2730 //Returns null if invalid!
2731 if (!dateval
.ParseDate(val
.GetString()))
2732 SetColNull(colNo
, TRUE
);
2735 switch (colDefs
[colNo
].SqlCtype
)
2739 csstrncpyt((wxChar
*)(colDefs
[colNo
].PtrDataObj
),
2740 val
.GetString().c_str(),
2741 colDefs
[colNo
].SzDataObj
-1);
2745 *(long *)(colDefs
[colNo
].PtrDataObj
) = val
;
2749 *(short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2752 *(unsigned long *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2755 *(wxChar
*)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2757 case SQL_C_UTINYINT
:
2758 *(wxChar
*)(colDefs
[colNo
].PtrDataObj
) = val
.GetChar();
2761 *(unsigned short *)(colDefs
[colNo
].PtrDataObj
) = val
.GetLong();
2763 //FIXME: Add proper wxDateTime support to wxVariant..
2766 DATE_STRUCT
*dataptr
=
2767 (DATE_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2769 dataptr
->year
= dateval
.GetYear();
2770 dataptr
->month
= dateval
.GetMonth()+1;
2771 dataptr
->day
= dateval
.GetDay();
2776 TIME_STRUCT
*dataptr
=
2777 (TIME_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2779 dataptr
->hour
= dateval
.GetHour();
2780 dataptr
->minute
= dateval
.GetMinute();
2781 dataptr
->second
= dateval
.GetSecond();
2784 case SQL_C_TIMESTAMP
:
2786 TIMESTAMP_STRUCT
*dataptr
=
2787 (TIMESTAMP_STRUCT
*)colDefs
[colNo
].PtrDataObj
;
2788 dataptr
->year
= dateval
.GetYear();
2789 dataptr
->month
= dateval
.GetMonth()+1;
2790 dataptr
->day
= dateval
.GetDay();
2792 dataptr
->hour
= dateval
.GetHour();
2793 dataptr
->minute
= dateval
.GetMinute();
2794 dataptr
->second
= dateval
.GetSecond();
2798 *(double *)(colDefs
[colNo
].PtrDataObj
) = val
;
2803 } // if (!val.IsNull())
2804 } // wxDbTable::SetCol()
2807 GenericKey
wxDbTable::GetKey()
2812 blk
= malloc(m_keysize
);
2813 blkptr
= (wxChar
*) blk
;
2816 for (i
=0; i
< noCols
; i
++)
2818 if (colDefs
[i
].KeyField
)
2820 memcpy(blkptr
,colDefs
[i
].PtrDataObj
, colDefs
[i
].SzDataObj
);
2821 blkptr
+= colDefs
[i
].SzDataObj
;
2825 GenericKey k
= GenericKey(blk
, m_keysize
);
2829 } // wxDbTable::GetKey()
2832 void wxDbTable::SetKey(const GenericKey
& k
)
2838 blkptr
= (wxChar
*)blk
;
2841 for (i
=0; i
< noCols
; i
++)
2843 if (colDefs
[i
].KeyField
)
2845 SetColNull(i
, FALSE
);
2846 memcpy(colDefs
[i
].PtrDataObj
, blkptr
, colDefs
[i
].SzDataObj
);
2847 blkptr
+= colDefs
[i
].SzDataObj
;
2850 } // wxDbTable::SetKey()
2853 #endif // wxUSE_ODBC