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 // Use this line for wxWindows v1.x
30 // Use this line for wxWindows v2.x
31 #include "wx/wxprec.h"
32 #include "wx/version.h"
34 #if wxMAJOR_VERSION == 2
36 #pragma implementation "dbtable.h"
40 #ifdef DBDEBUG_CONSOLE
41 #include "wx/ioswrap.h"
49 #if wxMAJOR_VERSION == 2
51 #include "wx/string.h"
52 #include "wx/object.h"
55 #include "wx/msgdlg.h"
58 #include "wx/filefn.h"
61 #if wxMAJOR_VERSION == 1
62 # if defined(wx_msw) || defined(wx_x)
80 #if wxMAJOR_VERSION == 1
82 #elif wxMAJOR_VERSION == 2
83 #include "wx/dbtable.h"
87 // The HPUX preprocessor lines below were commented out on 8/20/97
88 // because macros.h currently redefines DEBUG and is unneeded.
90 // # include <macros.h>
93 # include <sys/minmax.h>
97 ULONG lastTableID
= 0;
105 /********** wxDbTable::wxDbTable() **********/
106 wxDbTable::wxDbTable(wxDb
*pwxDb
, const char *tblName
, const int nCols
,
107 const char *qryTblName
, bool qryOnly
, const char *tblPath
)
109 pDb
= pwxDb
; // Pointer to the wxDb object
113 hstmtDefault
= 0; // Initialized below
114 hstmtCount
= 0; // Initialized first time it is needed
121 noCols
= nCols
; // No. of cols in the table
122 where
= ""; // Where clause
123 orderBy
= ""; // Order By clause
124 from
= ""; // From clause
125 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
131 wxStrcpy(tableName
, tblName
); // Table Name
133 wxStrcpy(tablePath
, tblPath
); // Table Path - used for dBase files
135 if (qryTblName
) // Name of the table/view to query
136 wxStrcpy(queryTableName
, qryTblName
);
138 wxStrcpy(queryTableName
, tblName
);
143 pDb
->incrementTableCount();
146 tableID
= ++lastTableID
;
147 s
.sprintf("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName
,tableID
,pDb
);
150 wxTablesInUse
*tableInUse
;
151 tableInUse
= new wxTablesInUse();
152 tableInUse
->tableName
= tblName
;
153 tableInUse
->tableID
= tableID
;
154 tableInUse
->pDb
= pDb
;
155 TablesInUse
.Append(tableInUse
);
158 pDb
->WriteSqlLog(s
.c_str());
160 // Grab the HENV and HDBC from the wxDb object
161 henv
= pDb
->GetHENV();
162 hdbc
= pDb
->GetHDBC();
164 // Allocate space for column definitions
166 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column defintion
168 // Allocate statement handles for the table
171 // Allocate a separate statement handle for performing inserts
172 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
173 pDb
->DispAllErrors(henv
, hdbc
);
174 // Allocate a separate statement handle for performing deletes
175 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
176 pDb
->DispAllErrors(henv
, hdbc
);
177 // Allocate a separate statement handle for performing updates
178 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
179 pDb
->DispAllErrors(henv
, hdbc
);
181 // Allocate a separate statement handle for internal use
182 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
183 pDb
->DispAllErrors(henv
, hdbc
);
185 // Set the cursor type for the statement handles
186 cursorType
= SQL_CURSOR_STATIC
;
188 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
190 // Check to see if cursor type is supported
191 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
192 if (! wxStrcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
194 // Datasource does not support static cursors. Driver
195 // will substitute a cursor type. Call SQLGetStmtOption()
196 // to determine which cursor type was selected.
197 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
198 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
199 #ifdef DBDEBUG_CONSOLE
200 cout
<< "Static cursor changed to: ";
203 case SQL_CURSOR_FORWARD_ONLY
:
204 cout
<< "Forward Only";
206 case SQL_CURSOR_STATIC
:
209 case SQL_CURSOR_KEYSET_DRIVEN
:
210 cout
<< "Keyset Driven";
212 case SQL_CURSOR_DYNAMIC
:
216 cout
<< endl
<< endl
;
219 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
221 // Force the use of a forward only cursor...
222 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
223 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
225 // Should never happen
226 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
233 pDb
->DispNextError();
234 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
237 #ifdef DBDEBUG_CONSOLE
239 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
244 // Set the cursor type for the INSERT statement handle
245 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
246 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
247 // Set the cursor type for the DELETE statement handle
248 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
249 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
250 // Set the cursor type for the UPDATE statement handle
251 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
252 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
255 // Make the default cursor the active cursor
256 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
257 assert(hstmtDefault
);
258 hstmt
= *hstmtDefault
;
260 } // wxDbTable::wxDbTable()
263 /********** wxDbTable::~wxDbTable() **********/
264 wxDbTable::~wxDbTable()
269 s
.sprintf("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName
,tableID
,pDb
);
270 pDb
->WriteSqlLog(s
.c_str());
276 TablesInUse
.DeleteContents(TRUE
);
280 pNode
= TablesInUse
.First();
281 while (pNode
&& !found
)
283 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
286 if (!TablesInUse
.DeleteNode(pNode
))
287 wxLogDebug (s
.c_str(),wxT("Unable to delete node!"));
290 pNode
= pNode
->Next();
295 msg
.sprintf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
296 wxLogDebug (msg
.c_str(),wxT("NOTICE..."));
301 // Decrement the wxDb table count
303 pDb
->decrementTableCount();
305 // Delete memory allocated for column definitions
309 // Free statement handles
313 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
314 pDb
->DispAllErrors(henv
, hdbc
);
317 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
320 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
321 pDb
->DispAllErrors(henv
, hdbc
);
325 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
326 pDb
->DispAllErrors(henv
, hdbc
);
328 // Delete dynamically allocated cursors
330 DeleteCursor(hstmtDefault
);
333 DeleteCursor(hstmtCount
);
335 } // wxDbTable::~wxDbTable()
339 /***************************** PRIVATE FUNCTIONS *****************************/
343 /********** wxDbTable::bindInsertParams() **********/
344 bool wxDbTable::bindInsertParams(void)
351 UDWORD precision
= 0;
354 // Bind each column (that can be inserted) of the table to a parameter marker
356 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
358 if (! colDefs
[i
].InsertAllowed
)
360 switch(colDefs
[i
].DbDataType
)
362 case DB_DATA_TYPE_VARCHAR
:
363 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
364 precision
= colDefs
[i
].SzDataObj
;
366 colDefs
[i
].CbValue
= SQL_NTS
;
368 case DB_DATA_TYPE_INTEGER
:
369 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
370 precision
= pDb
->GetTypeInfInteger().Precision
;
372 colDefs
[i
].CbValue
= 0;
374 case DB_DATA_TYPE_FLOAT
:
375 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
376 precision
= pDb
->GetTypeInfFloat().Precision
;
377 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
378 // SQL Sybase Anywhere v5.5 returned a negative number for the
379 // MaxScale. This caused ODBC to kick out an error on ibscale.
380 // I check for this here and set the scale = precision.
382 // scale = (short) precision;
383 colDefs
[i
].CbValue
= 0;
385 case DB_DATA_TYPE_DATE
:
386 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
387 precision
= pDb
->GetTypeInfDate().Precision
;
389 colDefs
[i
].CbValue
= 0;
395 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
396 colDefs
[i
].Null
= FALSE
;
399 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
400 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
401 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
403 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
407 // Completed successfully
410 } // wxDbTable::bindInsertParams()
413 /********** wxDbTable::bindUpdateParams() **********/
414 bool wxDbTable::bindUpdateParams(void)
421 UDWORD precision
= 0;
424 // Bind each UPDATEABLE column of the table to a parameter marker
426 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
428 if (! colDefs
[i
].Updateable
)
430 switch(colDefs
[i
].DbDataType
)
432 case DB_DATA_TYPE_VARCHAR
:
433 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
434 precision
= colDefs
[i
].SzDataObj
;
436 colDefs
[i
].CbValue
= SQL_NTS
;
438 case DB_DATA_TYPE_INTEGER
:
439 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
440 precision
= pDb
->GetTypeInfInteger().Precision
;
442 colDefs
[i
].CbValue
= 0;
444 case DB_DATA_TYPE_FLOAT
:
445 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
446 precision
= pDb
->GetTypeInfFloat().Precision
;
447 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
448 // SQL Sybase Anywhere v5.5 returned a negative number for the
449 // MaxScale. This caused ODBC to kick out an error on ibscale.
450 // I check for this here and set the scale = precision.
452 // scale = (short) precision;
453 colDefs
[i
].CbValue
= 0;
455 case DB_DATA_TYPE_DATE
:
456 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
457 precision
= pDb
->GetTypeInfDate().Precision
;
459 colDefs
[i
].CbValue
= 0;
463 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
464 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
465 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
467 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
471 // Completed successfully
474 } // wxDbTable::bindUpdateParams()
477 /********** wxDbTable::bindCols() **********/
478 bool wxDbTable::bindCols(HSTMT cursor
)
482 // Bind each column of the table to a memory address for fetching data
484 for (i
= 0; i
< noCols
; i
++)
486 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
487 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
489 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
493 // Completed successfully
496 } // wxDbTable::bindCols()
499 /********** wxDbTable::getRec() **********/
500 bool wxDbTable::getRec(UWORD fetchType
)
504 if (!pDb
->FwdOnlyCursors())
506 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
510 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
511 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
513 if (retcode
== SQL_NO_DATA_FOUND
)
516 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
521 // Fetch the next record from the record set
522 retcode
= SQLFetch(hstmt
);
523 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
525 if (retcode
== SQL_NO_DATA_FOUND
)
528 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
532 // Completed successfully
535 } // wxDbTable::getRec()
538 /********** wxDbTable::execDelete() **********/
539 bool wxDbTable::execDelete(const char *pSqlStmt
)
541 // Execute the DELETE statement
542 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
543 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
545 // Record deleted successfully
548 } // wxDbTable::execDelete()
551 /********** wxDbTable::execUpdate() **********/
552 bool wxDbTable::execUpdate(const char *pSqlStmt
)
554 // Execute the UPDATE statement
555 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
556 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
558 // Record deleted successfully
561 } // wxDbTable::execUpdate()
564 /********** wxDbTable::query() **********/
565 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const char *pSqlStmt
)
567 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
569 // Set the selectForUpdate member variable
571 // The user may wish to select for update, but the DBMS may not be capable
572 selectForUpdate
= CanSelectForUpdate();
574 selectForUpdate
= FALSE
;
576 // Set the SQL SELECT string
577 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
578 { // so generate a select statement.
579 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
580 pDb
->WriteSqlLog(sqlStmt
);
583 wxStrcpy(sqlStmt
, pSqlStmt
);
585 SQLFreeStmt(hstmt
, SQL_CLOSE
);
586 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
590 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
594 // Make sure the cursor is closed first
595 if (! CloseCursor(hstmt
))
598 // Execute the SQL SELECT statement
600 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
601 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
602 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
604 // Completed successfully
607 } // wxDbTable::query()
610 /***************************** PUBLIC FUNCTIONS *****************************/
613 /********** wxDbTable::Open() **********/
614 bool wxDbTable::Open(void)
622 // Verify that the table exists in the database
623 if (!pDb
->TableExists(tableName
,pDb
->GetUsername(),tablePath
))
626 if (wxStrcmp(tablePath
,""))
627 s
.sprintf(wxT("Error opening '%s/%s'.\n"),tablePath
,tableName
);
629 s
.sprintf(wxT("Error opening '%s'.\n"), tableName
);
630 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
631 s
+= wxT("Table/view does not exist in the database.\n");
633 s
+= wxT("Current logged in user does not have sufficient privileges to access this table.\n");
634 pDb
->LogError(s
.c_str());
638 // Bind the member variables for field exchange between
639 // the wxDbTable object and the ODBC record.
642 if (!bindInsertParams()) // Inserts
645 if (!bindUpdateParams()) // Updates
649 if (!bindCols(*hstmtDefault
)) // Selects
652 if (!bindCols(hstmtInternal
)) // Internal use only
656 * Do NOT bind the hstmtCount cursor!!!
659 // Build an insert statement using parameter markers
660 if (!queryOnly
&& noCols
> 0)
662 bool needComma
= FALSE
;
663 sqlStmt
.sprintf("INSERT INTO %s (", tableName
);
664 for (i
= 0; i
< noCols
; i
++)
666 if (! colDefs
[i
].InsertAllowed
)
670 sqlStmt
+= colDefs
[i
].ColName
;
674 sqlStmt
+= ") VALUES (";
676 int insertableCount
= 0;
678 for (i
= 0; i
< noCols
; i
++)
680 if (! colDefs
[i
].InsertAllowed
)
690 // Prepare the insert statement for execution
693 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
694 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
700 // Completed successfully
703 } // wxDbTable::Open()
706 /********** wxDbTable::Query() **********/
707 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
710 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
712 } // wxDbTable::Query()
715 /********** wxDbTable::QueryBySqlStmt() **********/
716 bool wxDbTable::QueryBySqlStmt(const char *pSqlStmt
)
718 pDb
->WriteSqlLog(pSqlStmt
);
720 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
722 } // wxDbTable::QueryBySqlStmt()
725 /********** wxDbTable::QueryMatching() **********/
726 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
729 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
731 } // wxDbTable::QueryMatching()
734 /********** wxDbTable::QueryOnKeyFields() **********/
735 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
738 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
740 } // wxDbTable::QueryOnKeyFields()
743 /********** wxDbTable::GetPrev() **********/
744 bool wxDbTable::GetPrev(void)
746 if (pDb
->FwdOnlyCursors())
748 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
752 return(getRec(SQL_FETCH_PRIOR
));
754 } // wxDbTable::GetPrev()
757 /********** wxDbTable::operator-- **********/
758 bool wxDbTable::operator--(int)
760 if (pDb
->FwdOnlyCursors())
762 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
766 return(getRec(SQL_FETCH_PRIOR
));
768 } // wxDbTable::operator--
771 /********** wxDbTable::GetFirst() **********/
772 bool wxDbTable::GetFirst(void)
774 if (pDb
->FwdOnlyCursors())
776 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
780 return(getRec(SQL_FETCH_FIRST
));
782 } // wxDbTable::GetFirst()
785 /********** wxDbTable::GetLast() **********/
786 bool wxDbTable::GetLast(void)
788 if (pDb
->FwdOnlyCursors())
790 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
794 return(getRec(SQL_FETCH_LAST
));
796 } // wxDbTable::GetLast()
799 /********** wxDbTable::BuildSelectStmt() **********/
800 void wxDbTable::BuildSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
802 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
806 // Build a select statement to query the database
807 wxStrcpy(pSqlStmt
, "SELECT ");
809 // SELECT DISTINCT values only?
811 wxStrcat(pSqlStmt
, "DISTINCT ");
813 // Was a FROM clause specified to join tables to the base table?
814 // Available for ::Query() only!!!
815 bool appendFromClause
= FALSE
;
816 #if wxODBC_BACKWARD_COMPATABILITY
817 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
818 appendFromClause
= TRUE
;
820 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
821 appendFromClause
= TRUE
;
824 // Add the column list
826 for (i
= 0; i
< noCols
; i
++)
828 // If joining tables, the base table column names must be qualified to avoid ambiguity
829 if (appendFromClause
)
831 wxStrcat(pSqlStmt
, queryTableName
);
832 wxStrcat(pSqlStmt
, ".");
834 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
836 wxStrcat(pSqlStmt
, ",");
839 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
840 // the ROWID if querying distinct records. The rowid will always be unique.
841 if (!distinct
&& CanUpdByROWID())
843 // If joining tables, the base table column names must be qualified to avoid ambiguity
844 if (appendFromClause
)
846 wxStrcat(pSqlStmt
, ",");
847 wxStrcat(pSqlStmt
, queryTableName
);
848 wxStrcat(pSqlStmt
, ".ROWID");
851 wxStrcat(pSqlStmt
, ",ROWID");
854 // Append the FROM tablename portion
855 wxStrcat(pSqlStmt
, " FROM ");
856 wxStrcat(pSqlStmt
, queryTableName
);
858 // Sybase uses the HOLDLOCK keyword to lock a record during query.
859 // The HOLDLOCK keyword follows the table name in the from clause.
860 // Each table in the from clause must specify HOLDLOCK or
861 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
862 // is parsed but ignored in SYBASE Transact-SQL.
863 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
864 wxStrcat(pSqlStmt
, " HOLDLOCK");
866 if (appendFromClause
)
867 wxStrcat(pSqlStmt
, from
);
869 // Append the WHERE clause. Either append the where clause for the class
870 // or build a where clause. The typeOfSelect determines this.
873 case DB_SELECT_WHERE
:
874 #if wxODBC_BACKWARD_COMPATABILITY
875 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
877 if (where
.Length()) // May not want a where clause!!!
880 wxStrcat(pSqlStmt
, " WHERE ");
881 wxStrcat(pSqlStmt
, where
);
884 case DB_SELECT_KEYFIELDS
:
885 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
886 if (wxStrlen(whereClause
))
888 wxStrcat(pSqlStmt
, " WHERE ");
889 wxStrcat(pSqlStmt
, whereClause
);
892 case DB_SELECT_MATCHING
:
893 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
894 if (wxStrlen(whereClause
))
896 wxStrcat(pSqlStmt
, " WHERE ");
897 wxStrcat(pSqlStmt
, whereClause
);
902 // Append the ORDER BY clause
903 #if wxODBC_BACKWARD_COMPATABILITY
904 if (orderBy
&& wxStrlen(orderBy
))
906 if (orderBy
.Length())
909 wxStrcat(pSqlStmt
, " ORDER BY ");
910 wxStrcat(pSqlStmt
, orderBy
);
913 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
914 // parses the FOR UPDATE clause but ignores it. See the comment above on the
915 // HOLDLOCK for Sybase.
916 if (selectForUpdate
&& CanSelectForUpdate())
917 wxStrcat(pSqlStmt
, " FOR UPDATE");
919 } // wxDbTable::BuildSelectStmt()
922 /********** wxDbTable::GetRowNum() **********/
923 UWORD
wxDbTable::GetRowNum(void)
927 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
929 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
933 // Completed successfully
934 return((UWORD
) rowNum
);
936 } // wxDbTable::GetRowNum()
939 /********** wxDbTable::CloseCursor() **********/
940 bool wxDbTable::CloseCursor(HSTMT cursor
)
942 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
943 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
945 // Completed successfully
948 } // wxDbTable::CloseCursor()
951 /********** wxDbTable::CreateTable() **********/
952 bool wxDbTable::CreateTable(bool attemptDrop
)
960 #ifdef DBDEBUG_CONSOLE
961 cout
<< "Creating Table " << tableName
<< "..." << endl
;
965 if (attemptDrop
&& !DropTable())
969 #ifdef DBDEBUG_CONSOLE
970 for (i
= 0; i
< noCols
; i
++)
972 // Exclude derived columns since they are NOT part of the base table
973 if (colDefs
[i
].DerivedCol
)
975 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
976 switch(colDefs
[i
].DbDataType
)
978 case DB_DATA_TYPE_VARCHAR
:
979 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
981 case DB_DATA_TYPE_INTEGER
:
982 cout
<< pDb
->typeInfInteger
.TypeName
;
984 case DB_DATA_TYPE_FLOAT
:
985 cout
<< pDb
->typeInfFloat
.TypeName
;
987 case DB_DATA_TYPE_DATE
:
988 cout
<< pDb
->typeInfDate
.TypeName
;
995 // Build a CREATE TABLE string from the colDefs structure.
996 bool needComma
= FALSE
;
997 sqlStmt
.sprintf("CREATE TABLE %s (", tableName
);
999 for (i
= 0; i
< noCols
; i
++)
1001 // Exclude derived columns since they are NOT part of the base table
1002 if (colDefs
[i
].DerivedCol
)
1008 sqlStmt
+= colDefs
[i
].ColName
;
1011 switch(colDefs
[i
].DbDataType
)
1013 case DB_DATA_TYPE_VARCHAR
:
1014 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1016 case DB_DATA_TYPE_INTEGER
:
1017 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1019 case DB_DATA_TYPE_FLOAT
:
1020 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1022 case DB_DATA_TYPE_DATE
:
1023 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1026 // For varchars, append the size of the string
1027 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1030 // wxStrcat(sqlStmt, "(");
1031 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
1032 // wxStrcat(sqlStmt, ")");
1033 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
1034 sqlStmt
+= s
.c_str();
1037 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
1039 if (colDefs
[i
].KeyField
)
1041 sqlStmt
+= " NOT NULL";
1047 // If there is a primary key defined, include it in the create statement
1048 for (i
= j
= 0; i
< noCols
; i
++)
1050 if (colDefs
[i
].KeyField
)
1056 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1058 if (pDb
->Dbms() != dbmsMY_SQL
)
1060 sqlStmt
+= ",CONSTRAINT ";
1061 sqlStmt
+= tableName
;
1062 sqlStmt
+= "_PIDX PRIMARY KEY (";
1066 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1067 sqlStmt
+= ", PRIMARY KEY (";
1070 // List column name(s) of column(s) comprising the primary key
1071 for (i
= j
= 0; i
< noCols
; i
++)
1073 if (colDefs
[i
].KeyField
)
1075 if (j
++) // Multi part key, comma separate names
1077 sqlStmt
+= colDefs
[i
].ColName
;
1082 // Append the closing parentheses for the create table statement
1085 pDb
->WriteSqlLog(sqlStmt
.c_str());
1087 #ifdef DBDEBUG_CONSOLE
1088 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1091 // Execute the CREATE TABLE statement
1092 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1093 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1095 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1096 pDb
->RollbackTrans();
1101 // Commit the transaction and close the cursor
1102 if (!pDb
->CommitTrans())
1104 if (!CloseCursor(hstmt
))
1107 // Database table created successfully
1110 } // wxDbTable::CreateTable()
1113 /********** wxDbTable::DropTable() **********/
1114 bool wxDbTable::DropTable()
1116 // NOTE: This function returns TRUE if the Table does not exist, but
1117 // only for identified databases. Code will need to be added
1118 // below for any other databases when those databases are defined
1119 // to handle this situation consistently
1123 sqlStmt
.sprintf("DROP TABLE %s", tableName
);
1125 pDb
->WriteSqlLog(sqlStmt
.c_str());
1127 #ifdef DBDEBUG_CONSOLE
1128 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1131 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1133 // Check for "Base table not found" error and ignore
1134 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1135 if (wxStrcmp(pDb
->sqlState
,"S0002") && wxStrcmp(pDb
->sqlState
, "S1000")) // "Base table not found"
1137 // Check for product specific error codes
1138 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
1139 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"37000")) ||
1140 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01"))))
1142 pDb
->DispNextError();
1143 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1144 pDb
->RollbackTrans();
1151 // Commit the transaction and close the cursor
1152 if (! pDb
->CommitTrans())
1154 if (! CloseCursor(hstmt
))
1158 } // wxDbTable::DropTable()
1161 /********** wxDbTable::CreateIndex() **********/
1162 bool wxDbTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1166 // Drop the index first
1167 if (attemptDrop
&& !DropIndex(idxName
))
1170 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1171 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1172 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1173 // table was created, then months later you determine that an additional index while
1174 // give better performance, so you want to add an index).
1176 // The following block of code will modify the column definition to make the column be
1177 // defined with the "NOT NULL" qualifier.
1178 if (pDb
->Dbms() == dbmsMY_SQL
)
1183 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1187 // Find the column definition that has the ColName that matches the
1188 // index column name. We need to do this to get the DB_DATA_TYPE of
1189 // the index column, as MySQL's syntax for the ALTER column requires
1191 while (!found
&& (j
< this->noCols
))
1193 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1201 wxString typeNameAndSize
;
1203 switch(colDefs
[j
].DbDataType
)
1205 case DB_DATA_TYPE_VARCHAR
:
1206 typeNameAndSize
= pDb
->GetTypeInfVarchar().TypeName
;
1208 case DB_DATA_TYPE_INTEGER
:
1209 typeNameAndSize
= pDb
->GetTypeInfInteger().TypeName
;
1211 case DB_DATA_TYPE_FLOAT
:
1212 typeNameAndSize
= pDb
->GetTypeInfFloat().TypeName
;
1214 case DB_DATA_TYPE_DATE
:
1215 typeNameAndSize
= pDb
->GetTypeInfDate().TypeName
;
1219 // For varchars, append the size of the string
1220 if (colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1223 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
1224 typeNameAndSize
+= s
.c_str();
1227 sqlStmt
.sprintf("ALTER TABLE %s MODIFY %s %s NOT NULL",tableName
,pIdxDefs
[i
].ColName
,typeNameAndSize
.c_str());
1228 ok
= pDb
->ExecSql(sqlStmt
.c_str());
1232 wxODBC_ERRORS retcode
;
1233 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1234 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1235 // This line is just here for debug checking of the value
1236 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1246 pDb
->RollbackTrans();
1251 // Build a CREATE INDEX statement
1252 sqlStmt
= "CREATE ";
1254 sqlStmt
+= "UNIQUE ";
1256 sqlStmt
+= "INDEX ";
1259 sqlStmt
+= tableName
;
1262 // Append list of columns making up index
1264 for (i
= 0; i
< noIdxCols
; i
++)
1266 sqlStmt
+= pIdxDefs
[i
].ColName
;
1267 /* Postgres doesn't cope with ASC */
1268 if (pDb
->Dbms() != dbmsPOSTGRES
)
1270 if (pIdxDefs
[i
].Ascending
)
1276 if ((i
+ 1) < noIdxCols
)
1280 // Append closing parentheses
1283 pDb
->WriteSqlLog(sqlStmt
.c_str());
1285 #ifdef DBDEBUG_CONSOLE
1286 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1289 // Execute the CREATE INDEX statement
1290 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1292 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1293 pDb
->RollbackTrans();
1298 // Commit the transaction and close the cursor
1299 if (! pDb
->CommitTrans())
1301 if (! CloseCursor(hstmt
))
1304 // Index Created Successfully
1307 } // wxDbTable::CreateIndex()
1310 /********** wxDbTable::DropIndex() **********/
1311 bool wxDbTable::DropIndex(const char * idxName
)
1313 // NOTE: This function returns TRUE if the Index does not exist, but
1314 // only for identified databases. Code will need to be added
1315 // below for any other databases when those databases are defined
1316 // to handle this situation consistently
1320 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
)
1321 sqlStmt
.sprintf("DROP INDEX %s ON %s",idxName
,tableName
);
1322 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1323 sqlStmt
.sprintf("DROP INDEX %s.%s",tableName
,idxName
);
1325 sqlStmt
.sprintf("DROP INDEX %s",idxName
);
1327 pDb
->WriteSqlLog(sqlStmt
.c_str());
1329 #ifdef DBDEBUG_CONSOLE
1330 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1333 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1335 // Check for "Index not found" error and ignore
1336 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1337 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1339 // Check for product specific error codes
1340 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1341 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"37000")) ||
1342 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,"S1000")) ||
1343 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1344 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S12")) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1345 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01"))
1348 pDb
->DispNextError();
1349 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1350 pDb
->RollbackTrans();
1357 // Commit the transaction and close the cursor
1358 if (! pDb
->CommitTrans())
1360 if (! CloseCursor(hstmt
))
1364 } // wxDbTable::DropIndex()
1367 /********** wxDbTable::Insert() **********/
1368 int wxDbTable::Insert(void)
1371 if (queryOnly
|| !insertable
)
1376 // Insert the record by executing the already prepared insert statement
1378 retcode
=SQLExecute(hstmtInsert
);
1379 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1381 // Check to see if integrity constraint was violated
1382 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1383 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1384 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1387 pDb
->DispNextError();
1388 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1393 // Record inserted into the datasource successfully
1396 } // wxDbTable::Insert()
1399 /********** wxDbTable::Update() **********/
1400 bool wxDbTable::Update(void)
1406 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1408 // Build the SQL UPDATE statement
1409 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1411 pDb
->WriteSqlLog(sqlStmt
);
1413 #ifdef DBDEBUG_CONSOLE
1414 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1417 // Execute the SQL UPDATE statement
1418 return(execUpdate(sqlStmt
));
1420 } // wxDbTable::Update()
1423 /********** wxDbTable::Update(pSqlStmt) **********/
1424 bool wxDbTable::Update(const char *pSqlStmt
)
1430 pDb
->WriteSqlLog(pSqlStmt
);
1432 return(execUpdate(pSqlStmt
));
1434 } // wxDbTable::Update(pSqlStmt)
1437 /********** wxDbTable::UpdateWhere() **********/
1438 bool wxDbTable::UpdateWhere(const char *pWhereClause
)
1444 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1446 // Build the SQL UPDATE statement
1447 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1449 pDb
->WriteSqlLog(sqlStmt
);
1451 #ifdef DBDEBUG_CONSOLE
1452 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1455 // Execute the SQL UPDATE statement
1456 return(execUpdate(sqlStmt
));
1458 } // wxDbTable::UpdateWhere()
1461 /********** wxDbTable::Delete() **********/
1462 bool wxDbTable::Delete(void)
1468 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1470 // Build the SQL DELETE statement
1471 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1473 pDb
->WriteSqlLog(sqlStmt
);
1475 // Execute the SQL DELETE statement
1476 return(execDelete(sqlStmt
));
1478 } // wxDbTable::Delete()
1481 /********** wxDbTable::DeleteWhere() **********/
1482 bool wxDbTable::DeleteWhere(const char *pWhereClause
)
1488 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1490 // Build the SQL DELETE statement
1491 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1493 pDb
->WriteSqlLog(sqlStmt
);
1495 // Execute the SQL DELETE statement
1496 return(execDelete(sqlStmt
));
1498 } // wxDbTable::DeleteWhere()
1501 /********** wxDbTable::DeleteMatching() **********/
1502 bool wxDbTable::DeleteMatching(void)
1508 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1510 // Build the SQL DELETE statement
1511 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1513 pDb
->WriteSqlLog(sqlStmt
);
1515 // Execute the SQL DELETE statement
1516 return(execDelete(sqlStmt
));
1518 } // wxDbTable::DeleteMatching()
1521 /********** wxDbTable::BuildUpdateStmt() **********/
1522 void wxDbTable::BuildUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1528 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1529 bool firstColumn
= TRUE
;
1532 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1534 // Append a list of columns to be updated
1536 for (i
= 0; i
< noCols
; i
++)
1538 // Only append Updateable columns
1539 if (colDefs
[i
].Updateable
)
1542 wxStrcat(pSqlStmt
, ",");
1544 firstColumn
= FALSE
;
1545 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1546 wxStrcat(pSqlStmt
, " = ?");
1550 // Append the WHERE clause to the SQL UPDATE statement
1551 wxStrcat(pSqlStmt
, " WHERE ");
1554 case DB_UPD_KEYFIELDS
:
1555 // If the datasource supports the ROWID column, build
1556 // the where on ROWID for efficiency purposes.
1557 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1558 if (CanUpdByROWID())
1561 char rowid
[wxDB_ROWID_LEN
];
1563 // Get the ROWID value. If not successful retreiving the ROWID,
1564 // simply fall down through the code and build the WHERE clause
1565 // based on the key fields.
1566 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1568 wxStrcat(pSqlStmt
, "ROWID = '");
1569 wxStrcat(pSqlStmt
, rowid
);
1570 wxStrcat(pSqlStmt
, "'");
1574 // Unable to delete by ROWID, so build a WHERE
1575 // clause based on the keyfields.
1576 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1577 wxStrcat(pSqlStmt
, whereClause
);
1580 wxStrcat(pSqlStmt
, pWhereClause
);
1583 } // BuildUpdateStmt()
1586 /********** wxDbTable::BuildDeleteStmt() **********/
1587 void wxDbTable::BuildDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1593 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1597 // Handle the case of DeleteWhere() and the where clause is blank. It should
1598 // delete all records from the database in this case.
1599 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1601 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1605 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1607 // Append the WHERE clause to the SQL DELETE statement
1610 case DB_DEL_KEYFIELDS
:
1611 // If the datasource supports the ROWID column, build
1612 // the where on ROWID for efficiency purposes.
1613 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1614 if (CanUpdByROWID())
1617 char rowid
[wxDB_ROWID_LEN
];
1619 // Get the ROWID value. If not successful retreiving the ROWID,
1620 // simply fall down through the code and build the WHERE clause
1621 // based on the key fields.
1622 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1624 wxStrcat(pSqlStmt
, "ROWID = '");
1625 wxStrcat(pSqlStmt
, rowid
);
1626 wxStrcat(pSqlStmt
, "'");
1630 // Unable to delete by ROWID, so build a WHERE
1631 // clause based on the keyfields.
1632 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1633 wxStrcat(pSqlStmt
, whereClause
);
1636 wxStrcat(pSqlStmt
, pWhereClause
);
1638 case DB_DEL_MATCHING
:
1639 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1640 wxStrcat(pSqlStmt
, whereClause
);
1644 } // BuildDeleteStmt()
1647 /********** wxDbTable::BuildWhereClause() **********/
1648 void wxDbTable::BuildWhereClause(char *pWhereClause
, int typeOfWhere
,
1649 const char *qualTableName
, bool useLikeComparison
)
1651 * Note: BuildWhereClause() currently ignores timestamp columns.
1652 * They are not included as part of the where clause.
1655 bool moreThanOneColumn
= FALSE
;
1658 // Loop through the columns building a where clause as you go
1660 for (i
= 0; i
< noCols
; i
++)
1662 // Determine if this column should be included in the WHERE clause
1663 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1664 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1666 // Skip over timestamp columns
1667 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1669 // If there is more than 1 column, join them with the keyword "AND"
1670 if (moreThanOneColumn
)
1671 wxStrcat(pWhereClause
, " AND ");
1673 moreThanOneColumn
= TRUE
;
1674 // Concatenate where phrase for the column
1675 if (qualTableName
&& wxStrlen(qualTableName
))
1677 wxStrcat(pWhereClause
, qualTableName
);
1678 wxStrcat(pWhereClause
, ".");
1680 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1681 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1682 wxStrcat(pWhereClause
, " LIKE ");
1684 wxStrcat(pWhereClause
, " = ");
1685 switch(colDefs
[i
].SqlCtype
)
1688 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1691 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1694 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1697 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1700 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1703 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1706 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1709 wxStrcat(pWhereClause
, colValue
);
1712 } // wxDbTable::BuildWhereClause()
1715 /********** wxDbTable::IsColNull() **********/
1716 bool wxDbTable::IsColNull(int colNo
)
1718 switch(colDefs
[colNo
].SqlCtype
)
1721 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1723 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1725 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1727 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1729 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1731 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1733 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1734 case SQL_C_TIMESTAMP
:
1735 TIMESTAMP_STRUCT
*pDt
;
1736 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1737 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1744 } // wxDbTable::IsColNull()
1747 /********** wxDbTable::CanSelectForUpdate() **********/
1748 bool wxDbTable::CanSelectForUpdate(void)
1750 if (pDb
->Dbms() == dbmsMY_SQL
)
1753 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1758 } // wxDbTable::CanSelectForUpdate()
1761 /********** wxDbTable::CanUpdByROWID() **********/
1762 bool wxDbTable::CanUpdByROWID(void)
1765 * NOTE: Returning FALSE for now until this can be debugged,
1766 * as the ROWID is not getting updated correctly
1770 if (pDb
->Dbms() == dbmsORACLE
)
1775 } // wxDbTable::CanUpdByROWID()
1778 /********** wxDbTable::IsCursorClosedOnCommit() **********/
1779 bool wxDbTable::IsCursorClosedOnCommit(void)
1781 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1786 } // wxDbTable::IsCursorClosedOnCommit()
1789 /********** wxDbTable::ClearMemberVars() **********/
1790 void wxDbTable::ClearMemberVars(void)
1792 // Loop through the columns setting each member variable to zero
1794 for (i
= 0; i
< noCols
; i
++)
1796 switch(colDefs
[i
].SqlCtype
)
1799 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1802 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1805 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1808 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1811 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1814 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1817 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1819 case SQL_C_TIMESTAMP
:
1820 TIMESTAMP_STRUCT
*pDt
;
1821 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1833 } // wxDbTable::ClearMemberVars()
1836 /********** wxDbTable::SetQueryTimeout() **********/
1837 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
1839 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1840 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1841 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1842 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1843 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1844 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1845 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1846 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1848 // Completed Successfully
1851 } // wxDbTable::SetQueryTimeout()
1854 /********** wxDbTable::SetColDefs() **********/
1855 void wxDbTable::SetColDefs (int index
, const char *fieldName
, int dataType
, void *pData
,
1856 int cType
, int size
, bool keyField
, bool upd
,
1857 bool insAllow
, bool derivedCol
)
1859 if (!colDefs
) // May happen if the database connection fails
1862 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1864 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1865 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1868 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
1870 colDefs
[index
].DbDataType
= dataType
;
1871 colDefs
[index
].PtrDataObj
= pData
;
1872 colDefs
[index
].SqlCtype
= cType
;
1873 colDefs
[index
].SzDataObj
= size
;
1874 colDefs
[index
].KeyField
= keyField
;
1875 colDefs
[index
].DerivedCol
= derivedCol
;
1876 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1879 colDefs
[index
].Updateable
= FALSE
;
1880 colDefs
[index
].InsertAllowed
= FALSE
;
1884 colDefs
[index
].Updateable
= upd
;
1885 colDefs
[index
].InsertAllowed
= insAllow
;
1888 colDefs
[index
].Null
= FALSE
;
1890 } // wxDbTable::SetColDefs()
1893 /********** wxDbTable::SetColDef() **********/
1894 wxDbColDataPtr
* wxDbTable::SetColDefs (wxDbColInf
*pColInfs
, ULONG numCols
)
1897 wxDbColDataPtr
*pColDataPtrs
= NULL
;
1903 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
1905 for (index
= 0; index
< numCols
; index
++)
1907 // Process the fields
1908 switch (pColInfs
[index
].dbDataType
)
1910 case DB_DATA_TYPE_VARCHAR
:
1911 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].bufferLength
+1];
1912 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
1913 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
1915 case DB_DATA_TYPE_INTEGER
:
1916 // Can be long or short
1917 if (pColInfs
[index
].bufferLength
== sizeof(long))
1919 pColDataPtrs
[index
].PtrDataObj
= new long;
1920 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
1921 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
1925 pColDataPtrs
[index
].PtrDataObj
= new short;
1926 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
1927 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
1930 case DB_DATA_TYPE_FLOAT
:
1931 // Can be float or double
1932 if (pColInfs
[index
].bufferLength
== sizeof(float))
1934 pColDataPtrs
[index
].PtrDataObj
= new float;
1935 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
1936 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
1940 pColDataPtrs
[index
].PtrDataObj
= new double;
1941 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
1942 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
1945 case DB_DATA_TYPE_DATE
:
1946 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
1947 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
1948 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
1951 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
1955 return (pColDataPtrs
);
1957 } // wxDbTable::SetColDef()
1960 /********** wxDbTable::SetCursor() **********/
1961 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
1963 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
1964 hstmt
= *hstmtDefault
;
1966 hstmt
= *hstmtActivate
;
1968 } // wxDbTable::SetCursor()
1971 /********** wxDbTable::Count(const char *) **********/
1972 ULONG
wxDbTable::Count(const char *args
)
1978 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1979 sqlStmt
= "SELECT COUNT(";
1981 sqlStmt
+= ") FROM ";
1982 sqlStmt
+= queryTableName
;
1983 #if wxODBC_BACKWARD_COMPATABILITY
1984 if (from
&& wxStrlen(from
))
1990 // Add the where clause if one is provided
1991 #if wxODBC_BACKWARD_COMPATABILITY
1992 if (where
&& wxStrlen(where
))
1997 sqlStmt
+= " WHERE ";
2001 pDb
->WriteSqlLog(sqlStmt
.c_str());
2003 // Initialize the Count cursor if it's not already initialized
2006 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2012 // Execute the SQL statement
2013 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2015 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2020 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2022 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2026 // Obtain the result
2027 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2029 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2034 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2035 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2037 // Return the record count
2040 } // wxDbTable::Count()
2043 /********** wxDbTable::Refresh() **********/
2044 bool wxDbTable::Refresh(void)
2048 // Switch to the internal cursor so any active cursors are not corrupted
2049 HSTMT currCursor
= GetCursor();
2050 hstmt
= hstmtInternal
;
2051 #if wxODBC_BACKWARD_COMPATABILITY
2052 // Save the where and order by clauses
2053 char *saveWhere
= where
;
2054 char *saveOrderBy
= orderBy
;
2056 wxString saveWhere
= where
;
2057 wxString saveOrderBy
= orderBy
;
2059 // Build a where clause to refetch the record with. Try and use the
2060 // ROWID if it's available, ow use the key fields.
2061 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
2062 wxStrcpy(whereClause
, "");
2063 if (CanUpdByROWID())
2066 char rowid
[wxDB_ROWID_LEN
+1];
2068 // Get the ROWID value. If not successful retreiving the ROWID,
2069 // simply fall down through the code and build the WHERE clause
2070 // based on the key fields.
2071 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2073 wxStrcat(whereClause
, queryTableName
);
2074 wxStrcat(whereClause
, ".ROWID = '");
2075 wxStrcat(whereClause
, rowid
);
2076 wxStrcat(whereClause
, "'");
2080 // If unable to use the ROWID, build a where clause from the keyfields
2081 if (wxStrlen(whereClause
) == 0)
2082 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2084 // Requery the record
2085 where
= whereClause
;
2090 if (result
&& !GetNext())
2093 // Switch back to original cursor
2094 SetCursor(&currCursor
);
2096 // Free the internal cursor
2097 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2098 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2100 // Restore the original where and order by clauses
2102 orderBy
= saveOrderBy
;
2106 } // wxDbTable::Refresh()
2109 /********** wxDbTable::SetNull(int colNo) **********/
2110 bool wxDbTable::SetNull(int colNo
)
2113 return(colDefs
[colNo
].Null
= TRUE
);
2117 } // wxDbTable::SetNull(int colNo)
2120 /********** wxDbTable::SetNull(char *colName) **********/
2121 bool wxDbTable::SetNull(const char *colName
)
2124 for (i
= 0; i
< noCols
; i
++)
2126 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2131 return(colDefs
[i
].Null
= TRUE
);
2135 } // wxDbTable::SetNull(char *colName)
2138 /********** wxDbTable::GetNewCursor() **********/
2139 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2141 HSTMT
*newHSTMT
= new HSTMT
;
2146 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2148 pDb
->DispAllErrors(henv
, hdbc
);
2153 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2155 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2162 if(!bindCols(*newHSTMT
))
2170 SetCursor(newHSTMT
);
2174 } // wxDbTable::GetNewCursor()
2177 /********** wxDbTable::DeleteCursor() **********/
2178 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2182 if (!hstmtDel
) // Cursor already deleted
2185 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2187 pDb
->DispAllErrors(henv
, hdbc
);
2195 } // wxDbTable::DeleteCursor()
2197 #endif // wxUSE_ODBC