1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxTable class.
5 // Modified by: George Tasker
7 // -Dynamic cursor support - Only one predefined cursor, as many others as
8 // you need may be created on demand
9 // -Reduced number of active cursors significantly
10 // -Query-Only wxTable objects
13 // Copyright: (c) 1996 Remstar International, Inc.
14 // Licence: wxWindows licence, plus:
15 // Notice: This class library and its intellectual design are free of charge for use,
16 // modification, enhancement, debugging under the following conditions:
17 // 1) These classes may only be used as part of the implementation of a
18 // wxWindows-based application
19 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
20 // user groups free of all charges for use with the wxWindows library.
21 // 3) These classes may not be distributed as part of any other class library,
22 // DLL, text (written or electronic), other than a complete distribution of
23 // the wxWindows GUI development toolkit.
24 ///////////////////////////////////////////////////////////////////////////////
31 // Use this line for wxWindows v1.x
33 // Use this line for wxWindows v2.x
34 #include "wx/version.h"
35 #include "wx/wxprec.h"
37 #if wxMAJOR_VERSION == 2
39 # pragma implementation "dbtable.h"
43 #ifdef DBDEBUG_CONSOLE
51 #if wxMAJOR_VERSION == 2
53 #include "wx/string.h"
54 #include "wx/object.h"
57 #include "wx/msgdlg.h"
59 #include "wx/filefn.h"
62 #if wxMAJOR_VERSION == 1
63 # 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 /********** wxTable::wxTable() **********/
106 wxTable::wxTable(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
= 0; // Where clause
123 orderBy
= 0; // Order By clause
124 from
= 0; // From clause
125 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
130 wxStrcpy(tableName
, tblName
); // Table Name
132 wxStrcpy(tablePath
, tblPath
); // Table Path - used for dBase files
134 if (qryTblName
) // Name of the table/view to query
135 wxStrcpy(queryTableName
, qryTblName
);
137 wxStrcpy(queryTableName
, tblName
);
145 tableID
= ++lastTableID
;
146 sprintf(s
, "wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName
,tableID
,pDb
);
149 CstructTablesInUse
*tableInUse
;
150 tableInUse
= new CstructTablesInUse();
151 tableInUse
->tableName
= tblName
;
152 tableInUse
->tableID
= tableID
;
153 tableInUse
->pDb
= pDb
;
154 TablesInUse
.Append(tableInUse
);
159 // Grab the HENV and HDBC from the wxDB object
163 // Allocate space for column definitions
165 colDefs
= new wxColDef
[noCols
]; // Points to the first column defintion
167 // Allocate statement handles for the table
170 // Allocate a separate statement handle for performing inserts
171 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
172 pDb
->DispAllErrors(henv
, hdbc
);
173 // Allocate a separate statement handle for performing deletes
174 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
175 pDb
->DispAllErrors(henv
, hdbc
);
176 // Allocate a separate statement handle for performing updates
177 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
178 pDb
->DispAllErrors(henv
, hdbc
);
180 // Allocate a separate statement handle for internal use
181 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
182 pDb
->DispAllErrors(henv
, hdbc
);
184 // Set the cursor type for the statement handles
185 cursorType
= SQL_CURSOR_STATIC
;
186 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
188 // Check to see if cursor type is supported
189 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
190 if (! wxStrcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
192 // Datasource does not support static cursors. Driver
193 // will substitute a cursor type. Call SQLGetStmtOption()
194 // to determine which cursor type was selected.
195 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
196 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
197 #ifdef DBDEBUG_CONSOLE
198 cout
<< "Static cursor changed to: ";
201 case SQL_CURSOR_FORWARD_ONLY
:
202 cout
<< "Forward Only"; break;
203 case SQL_CURSOR_STATIC
:
204 cout
<< "Static"; break;
205 case SQL_CURSOR_KEYSET_DRIVEN
:
206 cout
<< "Keyset Driven"; break;
207 case SQL_CURSOR_DYNAMIC
:
208 cout
<< "Dynamic"; break;
210 cout
<< endl
<< endl
;
215 pDb
->DispNextError();
216 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
219 #ifdef DBDEBUG_CONSOLE
221 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
226 // Set the cursor type for the INSERT statement handle
227 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
228 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
229 // Set the cursor type for the DELETE statement handle
230 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
231 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
232 // Set the cursor type for the UPDATE statement handle
233 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
234 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
237 // Make the default cursor the active cursor
238 hstmtDefault
= NewCursor(FALSE
,FALSE
);
239 assert(hstmtDefault
);
240 hstmt
= *hstmtDefault
;
242 } // wxTable::wxTable()
245 /********** wxTable::~wxTable() **********/
251 sprintf(s
, "wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName
,tableID
,pDb
);
260 pNode
= TablesInUse
.First();
261 while (pNode
&& !found
)
263 if (((CstructTablesInUse
*)pNode
->Data())->tableID
== tableID
)
266 if (!TablesInUse
.DeleteNode(pNode
))
267 wxMessageBox (s
,"Unable to delete node!");
270 pNode
= pNode
->Next();
275 sprintf(msg
,"Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s
);
276 wxMessageBox (msg
,"NOTICE...");
281 // Decrement the wxDB table count
285 // Delete memory allocated for column definitions
289 // Free statement handles
293 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
294 pDb
->DispAllErrors(henv
, hdbc
);
296 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
297 pDb
->DispAllErrors(henv
, hdbc
);
299 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
300 pDb
->DispAllErrors(henv
, hdbc
);
303 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
304 pDb
->DispAllErrors(henv
, hdbc
);
306 // Delete dynamically allocated cursors
308 DeleteCursor(hstmtDefault
);
310 DeleteCursor(hstmtCount
);
312 } // wxTable::~wxTable()
316 /***************************** PRIVATE FUNCTIONS *****************************/
320 /********** wxTable::bindInsertParams() **********/
321 bool wxTable::bindInsertParams(void)
328 UDWORD precision
= 0;
331 // Bind each column (that can be inserted) of the table to a parameter marker
333 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
335 if (! colDefs
[i
].InsertAllowed
)
337 switch(colDefs
[i
].DbDataType
)
339 case DB_DATA_TYPE_VARCHAR
:
340 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
341 precision
= colDefs
[i
].SzDataObj
;
343 colDefs
[i
].CbValue
= SQL_NTS
;
345 case DB_DATA_TYPE_INTEGER
:
346 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
347 precision
= pDb
->typeInfInteger
.Precision
;
349 colDefs
[i
].CbValue
= 0;
351 case DB_DATA_TYPE_FLOAT
:
352 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
353 precision
= pDb
->typeInfFloat
.Precision
;
354 scale
= pDb
->typeInfFloat
.MaximumScale
;
355 // SQL Sybase Anywhere v5.5 returned a negative number for the
356 // MaxScale. This caused ODBC to kick out an error on ibscale.
357 // I check for this here and set the scale = precision.
359 // scale = (short) precision;
360 colDefs
[i
].CbValue
= 0;
362 case DB_DATA_TYPE_DATE
:
363 fSqlType
= pDb
->typeInfDate
.FsqlType
;
364 precision
= pDb
->typeInfDate
.Precision
;
366 colDefs
[i
].CbValue
= 0;
372 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
373 colDefs
[i
].Null
= FALSE
;
375 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
376 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
377 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
378 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
381 // Completed successfully
384 } // wxTable::bindInsertParams()
387 /********** wxTable::bindUpdateParams() **********/
388 bool wxTable::bindUpdateParams(void)
395 UDWORD precision
= 0;
398 // Bind each UPDATEABLE column of the table to a parameter marker
400 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
402 if (! colDefs
[i
].Updateable
)
404 switch(colDefs
[i
].DbDataType
)
406 case DB_DATA_TYPE_VARCHAR
:
407 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
408 precision
= colDefs
[i
].SzDataObj
;
410 colDefs
[i
].CbValue
= SQL_NTS
;
412 case DB_DATA_TYPE_INTEGER
:
413 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
414 precision
= pDb
->typeInfInteger
.Precision
;
416 colDefs
[i
].CbValue
= 0;
418 case DB_DATA_TYPE_FLOAT
:
419 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
420 precision
= pDb
->typeInfFloat
.Precision
;
421 scale
= pDb
->typeInfFloat
.MaximumScale
;
422 // SQL Sybase Anywhere v5.5 returned a negative number for the
423 // MaxScale. This caused ODBC to kick out an error on ibscale.
424 // I check for this here and set the scale = precision.
426 // scale = (short) precision;
427 colDefs
[i
].CbValue
= 0;
429 case DB_DATA_TYPE_DATE
:
430 fSqlType
= pDb
->typeInfDate
.FsqlType
;
431 precision
= pDb
->typeInfDate
.Precision
;
433 colDefs
[i
].CbValue
= 0;
436 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
437 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
438 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
439 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
442 // Completed successfully
445 } // wxTable::bindUpdateParams()
448 /********** wxTable::bindCols() **********/
449 bool wxTable::bindCols(HSTMT cursor
)
453 // Bind each column of the table to a memory address for fetching data
455 for (i
= 0; i
< noCols
; i
++)
457 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
458 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
459 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
462 // Completed successfully
465 } // wxTable::bindCols()
468 /********** wxTable::getRec() **********/
469 bool wxTable::getRec(UWORD fetchType
)
473 if (!pDb
->FwdOnlyCursors())
475 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
479 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
480 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
481 if (retcode
== SQL_NO_DATA_FOUND
)
484 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
488 // Fetch the next record from the record set
489 retcode
= SQLFetch(hstmt
);
490 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
492 if (retcode
== SQL_NO_DATA_FOUND
)
495 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
499 // Completed successfully
502 } // wxTable::getRec()
505 /********** wxTable::execDelete() **********/
506 bool wxTable::execDelete(const char *pSqlStmt
)
508 // Execute the DELETE statement
509 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
510 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
512 // Record deleted successfully
515 } // wxTable::execDelete()
518 /********** wxTable::execUpdate() **********/
519 bool wxTable::execUpdate(const char *pSqlStmt
)
521 // Execute the UPDATE statement
522 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
523 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
525 // Record deleted successfully
528 } // wxTable::execUpdate()
531 /********** wxTable::query() **********/
532 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
534 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
536 // Set the selectForUpdate member variable
538 // The user may wish to select for update, but the DBMS may not be capable
539 selectForUpdate
= CanSelectForUpdate();
541 selectForUpdate
= FALSE
;
543 // Set the SQL SELECT string
544 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
545 { // so generate a select statement.
546 GetSelectStmt(sqlStmt
, queryType
, distinct
);
547 pDb
->WriteSqlLog(sqlStmt
);
550 // Make sure the cursor is closed first
551 if (! CloseCursor(hstmt
))
554 // Execute the SQL SELECT statement
557 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
558 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
559 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
561 // Completed successfully
564 } // wxTable::query()
567 /***************************** PUBLIC FUNCTIONS *****************************/
570 /********** wxTable::Open() **********/
571 bool wxTable::Open(void)
577 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
579 // Verify that the table exists in the database
580 if (!pDb
->TableExists(tableName
,pDb
->GetUsername(),tablePath
))
583 if (wxStrcmp(tablePath
,""))
584 sprintf(s
, "Error opening '%s/%s'.\n",tablePath
,tableName
);
586 sprintf(s
, "Error opening '%s'.\n", tableName
);
587 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
588 wxStrcat(s
,"Table/view does not exist in the database.\n");
590 wxStrcat(s
,"Current logged in user does not have sufficient privileges to access this table.\n");
595 // Bind the member variables for field exchange between
596 // the wxTable object and the ODBC record.
599 if (!bindInsertParams()) // Inserts
601 if (!bindUpdateParams()) // Updates
604 if (!bindCols(*hstmtDefault
)) // Selects
606 if (!bindCols(hstmtInternal
)) // Internal use only
609 * Do NOT bind the hstmtCount cursor!!!
612 // Build an insert statement using parameter markers
613 if (!queryOnly
&& noCols
> 0)
615 bool needComma
= FALSE
;
616 sprintf(sqlStmt
, "INSERT INTO %s (", tableName
);
617 for (i
= 0; i
< noCols
; i
++)
619 if (! colDefs
[i
].InsertAllowed
)
622 wxStrcat(sqlStmt
, ",");
623 wxStrcat(sqlStmt
, colDefs
[i
].ColName
);
627 wxStrcat(sqlStmt
, ") VALUES (");
628 for (i
= 0; i
< noCols
; i
++)
630 if (! colDefs
[i
].InsertAllowed
)
633 wxStrcat(sqlStmt
, ",");
634 wxStrcat(sqlStmt
, "?");
637 wxStrcat(sqlStmt
, ")");
639 // pDb->WriteSqlLog(sqlStmt);
641 // Prepare the insert statement for execution
642 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
643 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
646 // Completed successfully
652 /********** wxTable::Query() **********/
653 bool wxTable::Query(bool forUpdate
, bool distinct
)
656 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
658 } // wxTable::Query()
661 /********** wxTable::QueryBySqlStmt() **********/
662 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
664 pDb
->WriteSqlLog(pSqlStmt
);
666 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
668 } // wxTable::QueryBySqlStmt()
671 /********** wxTable::QueryMatching() **********/
672 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
675 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
677 } // wxTable::QueryMatching()
680 /********** wxTable::QueryOnKeyFields() **********/
681 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
684 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
686 } // wxTable::QueryOnKeyFields()
689 /********** wxTable::GetPrev() **********/
690 bool wxTable::GetPrev(void)
692 if (pDb
->FwdOnlyCursors())
694 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxTable"));
698 return(getRec(SQL_FETCH_PRIOR
));
699 } // wxTable::GetPrev()
702 /********** wxTable::operator-- **********/
703 bool wxTable::operator--(int)
705 if (pDb
->FwdOnlyCursors())
707 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxTable"));
711 return(getRec(SQL_FETCH_PRIOR
));
712 } // wxTable::operator--
715 /********** wxTable::GetFirst() **********/
716 bool wxTable::GetFirst(void)
718 if (pDb
->FwdOnlyCursors())
720 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxTable"));
724 return(getRec(SQL_FETCH_FIRST
));
725 } // wxTable::GetFirst()
728 /********** wxTable::GetLast() **********/
729 bool wxTable::GetLast(void)
731 if (pDb
->FwdOnlyCursors())
733 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxTable"));
737 return(getRec(SQL_FETCH_LAST
));
738 } // wxTable::GetLast()
741 /********** wxTable::GetSelectStmt() **********/
742 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
744 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
748 // Build a select statement to query the database
749 wxStrcpy(pSqlStmt
, "SELECT ");
751 // SELECT DISTINCT values only?
753 wxStrcat(pSqlStmt
, "DISTINCT ");
755 // Was a FROM clause specified to join tables to the base table?
756 // Available for ::Query() only!!!
757 bool appendFromClause
= FALSE
;
758 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
759 appendFromClause
= TRUE
;
761 // Add the column list
763 for (i
= 0; i
< noCols
; i
++)
765 // If joining tables, the base table column names must be qualified to avoid ambiguity
766 if (appendFromClause
)
768 wxStrcat(pSqlStmt
, queryTableName
);
769 wxStrcat(pSqlStmt
, ".");
771 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
773 wxStrcat(pSqlStmt
, ",");
776 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
777 // the ROWID if querying distinct records. The rowid will always be unique.
778 if (!distinct
&& CanUpdByROWID())
780 // If joining tables, the base table column names must be qualified to avoid ambiguity
781 if (appendFromClause
)
783 wxStrcat(pSqlStmt
, ",");
784 wxStrcat(pSqlStmt
, queryTableName
);
785 wxStrcat(pSqlStmt
, ".ROWID");
788 wxStrcat(pSqlStmt
, ",ROWID");
791 // Append the FROM tablename portion
792 wxStrcat(pSqlStmt
, " FROM ");
793 wxStrcat(pSqlStmt
, queryTableName
);
795 // Sybase uses the HOLDLOCK keyword to lock a record during query.
796 // The HOLDLOCK keyword follows the table name in the from clause.
797 // Each table in the from clause must specify HOLDLOCK or
798 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
799 // is parsed but ignored in SYBASE Transact-SQL.
800 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
801 wxStrcat(pSqlStmt
, " HOLDLOCK");
803 if (appendFromClause
)
804 wxStrcat(pSqlStmt
, from
);
806 // Append the WHERE clause. Either append the where clause for the class
807 // or build a where clause. The typeOfSelect determines this.
810 case DB_SELECT_WHERE
:
811 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
813 wxStrcat(pSqlStmt
, " WHERE ");
814 wxStrcat(pSqlStmt
, where
);
817 case DB_SELECT_KEYFIELDS
:
818 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
819 if (wxStrlen(whereClause
))
821 wxStrcat(pSqlStmt
, " WHERE ");
822 wxStrcat(pSqlStmt
, whereClause
);
825 case DB_SELECT_MATCHING
:
826 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
827 if (wxStrlen(whereClause
))
829 wxStrcat(pSqlStmt
, " WHERE ");
830 wxStrcat(pSqlStmt
, whereClause
);
835 // Append the ORDER BY clause
836 if (orderBy
&& wxStrlen(orderBy
))
838 wxStrcat(pSqlStmt
, " ORDER BY ");
839 wxStrcat(pSqlStmt
, orderBy
);
842 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
843 // parses the FOR UPDATE clause but ignores it. See the comment above on the
844 // HOLDLOCK for Sybase.
845 if (selectForUpdate
&& CanSelectForUpdate())
846 wxStrcat(pSqlStmt
, " FOR UPDATE");
848 } // wxTable::GetSelectStmt()
851 /********** wxTable::GetRowNum() **********/
852 UWORD
wxTable::GetRowNum(void)
856 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
858 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
862 // Completed successfully
863 return((UWORD
) rowNum
);
865 } // wxTable::GetRowNum()
868 /********** wxTable::CloseCursor() **********/
869 bool wxTable::CloseCursor(HSTMT cursor
)
871 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
872 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
874 // Completed successfully
877 } // wxTable::CloseCursor()
880 /********** wxTable::CreateTable() **********/
881 bool wxTable::CreateTable(bool attemptDrop
)
887 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
889 #ifdef DBDEBUG_CONSOLE
890 cout
<< "Creating Table " << tableName
<< "..." << endl
;
894 if (attemptDrop
&& !DropTable())
898 #ifdef DBDEBUG_CONSOLE
899 for (i
= 0; i
< noCols
; i
++)
901 // Exclude derived columns since they are NOT part of the base table
902 if (colDefs
[i
].DerivedCol
)
904 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
905 switch(colDefs
[i
].DbDataType
)
907 case DB_DATA_TYPE_VARCHAR
:
908 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
910 case DB_DATA_TYPE_INTEGER
:
911 cout
<< pDb
->typeInfInteger
.TypeName
;
913 case DB_DATA_TYPE_FLOAT
:
914 cout
<< pDb
->typeInfFloat
.TypeName
;
916 case DB_DATA_TYPE_DATE
:
917 cout
<< pDb
->typeInfDate
.TypeName
;
924 // Build a CREATE TABLE string from the colDefs structure.
925 bool needComma
= FALSE
;
926 sprintf(sqlStmt
, "CREATE TABLE %s (", tableName
);
927 for (i
= 0; i
< noCols
; i
++)
929 // Exclude derived columns since they are NOT part of the base table
930 if (colDefs
[i
].DerivedCol
)
934 wxStrcat(sqlStmt
, ",");
936 wxStrcat(sqlStmt
, colDefs
[i
].ColName
);
937 wxStrcat(sqlStmt
, " ");
939 switch(colDefs
[i
].DbDataType
)
941 case DB_DATA_TYPE_VARCHAR
:
942 wxStrcat(sqlStmt
, pDb
->typeInfVarchar
.TypeName
); break;
943 case DB_DATA_TYPE_INTEGER
:
944 wxStrcat(sqlStmt
, pDb
->typeInfInteger
.TypeName
); break;
945 case DB_DATA_TYPE_FLOAT
:
946 wxStrcat(sqlStmt
, pDb
->typeInfFloat
.TypeName
); break;
947 case DB_DATA_TYPE_DATE
:
948 wxStrcat(sqlStmt
, pDb
->typeInfDate
.TypeName
); break;
950 // For varchars, append the size of the string
951 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
954 // wxStrcat(sqlStmt, "(");
955 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
956 // wxStrcat(sqlStmt, ")");
957 sprintf(s
, "(%d)", colDefs
[i
].SzDataObj
);
958 wxStrcat(sqlStmt
, s
);
961 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
963 if (colDefs
[i
].KeyField
)
965 wxStrcat(sqlStmt
, " NOT NULL");
971 // If there is a primary key defined, include it in the create statement
972 for (i
= j
= 0; i
< noCols
; i
++)
974 if (colDefs
[i
].KeyField
)
980 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
982 if (pDb
->Dbms() != dbmsMY_SQL
)
984 wxStrcat(sqlStmt
, ",CONSTRAINT ");
985 wxStrcat(sqlStmt
, tableName
);
986 wxStrcat(sqlStmt
, "_PIDX PRIMARY KEY (");
990 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
991 wxStrcat(sqlStmt
, ", PRIMARY KEY (");
994 // List column name(s) of column(s) comprising the primary key
995 for (i
= j
= 0; i
< noCols
; i
++)
997 if (colDefs
[i
].KeyField
)
999 if (j
++) // Multi part key, comma separate names
1000 wxStrcat(sqlStmt
, ",");
1001 wxStrcat(sqlStmt
, colDefs
[i
].ColName
);
1004 wxStrcat(sqlStmt
, ")");
1006 // Append the closing parentheses for the create table statement
1007 wxStrcat(sqlStmt
, ")");
1009 pDb
->WriteSqlLog(sqlStmt
);
1011 #ifdef DBDEBUG_CONSOLE
1012 cout
<< endl
<< sqlStmt
<< endl
;
1015 // Execute the CREATE TABLE statement
1016 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
);
1017 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1019 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1020 pDb
->RollbackTrans();
1025 // Commit the transaction and close the cursor
1026 if (! pDb
->CommitTrans())
1028 if (! CloseCursor(hstmt
))
1031 // Database table created successfully
1034 } // wxTable::CreateTable()
1037 /********** wxTable::DropTable() **********/
1038 bool wxTable::DropTable()
1040 // NOTE: This function returns TRUE if the Table does not exist, but
1041 // only for identified databases. Code will need to be added
1042 // below for any other databases when those databases are defined
1043 // to handle this situation consistently
1045 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1047 sprintf(sqlStmt
, "DROP TABLE %s", tableName
);
1049 pDb
->WriteSqlLog(sqlStmt
);
1051 #ifdef DBDEBUG_CONSOLE
1052 cout
<< endl
<< sqlStmt
<< endl
;
1055 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1057 // Check for "Base table not found" error and ignore
1058 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1059 if (wxStrcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
1061 // Check for product specific error codes
1062 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
1063 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"S1000")) || // untested
1064 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01")))) // untested
1066 pDb
->DispNextError();
1067 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1068 pDb
->RollbackTrans();
1075 // Commit the transaction and close the cursor
1076 if (! pDb
->CommitTrans())
1078 if (! CloseCursor(hstmt
))
1082 } // wxTable::DropTable()
1085 /********** wxTable::CreateIndex() **********/
1086 bool wxTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
1088 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1090 // Drop the index first
1091 if (attemptDrop
&& !DropIndex(idxName
))
1094 // Build a CREATE INDEX statement
1095 wxStrcpy(sqlStmt
, "CREATE ");
1097 wxStrcat(sqlStmt
, "UNIQUE ");
1099 wxStrcat(sqlStmt
, "INDEX ");
1100 wxStrcat(sqlStmt
, idxName
);
1101 wxStrcat(sqlStmt
, " ON ");
1102 wxStrcat(sqlStmt
, tableName
);
1103 wxStrcat(sqlStmt
, " (");
1105 // Append list of columns making up index
1107 for (i
= 0; i
< noIdxCols
; i
++)
1109 wxStrcat(sqlStmt
, pIdxDefs
[i
].ColName
);
1110 /* Postgres doesn't cope with ASC */
1111 if (pDb
->Dbms() != dbmsPOSTGRES
)
1113 if (pIdxDefs
[i
].Ascending
)
1114 wxStrcat(sqlStmt
, " ASC");
1116 wxStrcat(sqlStmt
, " DESC");
1119 if ((i
+ 1) < noIdxCols
)
1120 wxStrcat(sqlStmt
, ",");
1123 // Append closing parentheses
1124 wxStrcat(sqlStmt
, ")");
1126 pDb
->WriteSqlLog(sqlStmt
);
1128 #ifdef DBDEBUG_CONSOLE
1129 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1132 // Execute the CREATE INDEX statement
1133 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1135 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1136 pDb
->RollbackTrans();
1141 // Commit the transaction and close the cursor
1142 if (! pDb
->CommitTrans())
1144 if (! CloseCursor(hstmt
))
1147 // Index Created Successfully
1150 } // wxTable::CreateIndex()
1153 /********** wxTable::DropIndex() **********/
1154 bool wxTable::DropIndex(const char * idxName
)
1156 // NOTE: This function returns TRUE if the Index does not exist, but
1157 // only for identified databases. Code will need to be added
1158 // below for any other databases when those databases are defined
1159 // to handle this situation consistently
1161 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1163 if (pDb
->Dbms() == dbmsACCESS
)
1164 sprintf(sqlStmt
, "DROP INDEX %s ON %s",idxName
,tableName
);
1165 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1166 sprintf(sqlStmt
, "DROP INDEX %s.%s",tableName
,idxName
);
1168 sprintf(sqlStmt
, "DROP INDEX %s",idxName
);
1170 pDb
->WriteSqlLog(sqlStmt
);
1172 #ifdef DBDEBUG_CONSOLE
1173 cout
<< endl
<< sqlStmt
<< endl
;
1176 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1178 // Check for "Index not found" error and ignore
1179 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1180 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1182 // Check for product specific error codes
1183 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1184 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1185 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S02")) // untested
1188 pDb
->DispNextError();
1189 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1190 pDb
->RollbackTrans();
1197 // Commit the transaction and close the cursor
1198 if (! pDb
->CommitTrans())
1200 if (! CloseCursor(hstmt
))
1204 } // wxTable::DropIndex()
1207 /********** wxTable::Insert() **********/
1208 int wxTable::Insert(void)
1216 // Insert the record by executing the already prepared insert statement
1218 retcode
=SQLExecute(hstmtInsert
);
1219 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1221 // Check to see if integrity constraint was violated
1222 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1223 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1224 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1227 pDb
->DispNextError();
1228 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1233 // Record inserted into the datasource successfully
1236 } // wxTable::Insert()
1239 /********** wxTable::Update() **********/
1240 bool wxTable::Update(void)
1246 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1248 // Build the SQL UPDATE statement
1249 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1251 pDb
->WriteSqlLog(sqlStmt
);
1253 #ifdef DBDEBUG_CONSOLE
1254 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1257 // Execute the SQL UPDATE statement
1258 return(execUpdate(sqlStmt
));
1260 } // wxTable::Update()
1263 /********** wxTable::Update(pSqlStmt) **********/
1264 bool wxTable::Update(const char *pSqlStmt
)
1270 pDb
->WriteSqlLog(pSqlStmt
);
1272 return(execUpdate(pSqlStmt
));
1274 } // wxTable::Update(pSqlStmt)
1277 /********** wxTable::UpdateWhere() **********/
1278 bool wxTable::UpdateWhere(const char *pWhereClause
)
1284 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1286 // Build the SQL UPDATE statement
1287 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1289 pDb
->WriteSqlLog(sqlStmt
);
1291 #ifdef DBDEBUG_CONSOLE
1292 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1295 // Execute the SQL UPDATE statement
1296 return(execUpdate(sqlStmt
));
1298 } // wxTable::UpdateWhere()
1301 /********** wxTable::Delete() **********/
1302 bool wxTable::Delete(void)
1308 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1310 // Build the SQL DELETE statement
1311 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1313 pDb
->WriteSqlLog(sqlStmt
);
1315 // Execute the SQL DELETE statement
1316 return(execDelete(sqlStmt
));
1318 } // wxTable::Delete()
1321 /********** wxTable::DeleteWhere() **********/
1322 bool wxTable::DeleteWhere(const char *pWhereClause
)
1328 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1330 // Build the SQL DELETE statement
1331 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1333 pDb
->WriteSqlLog(sqlStmt
);
1335 // Execute the SQL DELETE statement
1336 return(execDelete(sqlStmt
));
1338 } // wxTable::DeleteWhere()
1341 /********** wxTable::DeleteMatching() **********/
1342 bool wxTable::DeleteMatching(void)
1348 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1350 // Build the SQL DELETE statement
1351 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1353 pDb
->WriteSqlLog(sqlStmt
);
1355 // Execute the SQL DELETE statement
1356 return(execDelete(sqlStmt
));
1358 } // wxTable::DeleteMatching()
1361 /********** wxTable::GetUpdateStmt() **********/
1362 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1368 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1369 bool firstColumn
= TRUE
;
1372 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1374 // Append a list of columns to be updated
1376 for (i
= 0; i
< noCols
; i
++)
1378 // Only append Updateable columns
1379 if (colDefs
[i
].Updateable
)
1382 wxStrcat(pSqlStmt
, ",");
1384 firstColumn
= FALSE
;
1385 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1386 wxStrcat(pSqlStmt
, " = ?");
1390 // Append the WHERE clause to the SQL UPDATE statement
1391 wxStrcat(pSqlStmt
, " WHERE ");
1394 case DB_UPD_KEYFIELDS
:
1395 // If the datasource supports the ROWID column, build
1396 // the where on ROWID for efficiency purposes.
1397 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1398 if (CanUpdByROWID())
1401 char rowid
[ROWID_LEN
];
1403 // Get the ROWID value. If not successful retreiving the ROWID,
1404 // simply fall down through the code and build the WHERE clause
1405 // based on the key fields.
1406 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1408 wxStrcat(pSqlStmt
, "ROWID = '");
1409 wxStrcat(pSqlStmt
, rowid
);
1410 wxStrcat(pSqlStmt
, "'");
1414 // Unable to delete by ROWID, so build a WHERE
1415 // clause based on the keyfields.
1416 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1417 wxStrcat(pSqlStmt
, whereClause
);
1420 wxStrcat(pSqlStmt
, pWhereClause
);
1423 } // GetUpdateStmt()
1426 /********** wxTable::GetDeleteStmt() **********/
1427 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1433 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1437 // Handle the case of DeleteWhere() and the where clause is blank. It should
1438 // delete all records from the database in this case.
1439 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1441 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1445 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1447 // Append the WHERE clause to the SQL DELETE statement
1450 case DB_DEL_KEYFIELDS
:
1451 // If the datasource supports the ROWID column, build
1452 // the where on ROWID for efficiency purposes.
1453 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1454 if (CanUpdByROWID())
1457 char rowid
[ROWID_LEN
];
1459 // Get the ROWID value. If not successful retreiving the ROWID,
1460 // simply fall down through the code and build the WHERE clause
1461 // based on the key fields.
1462 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1464 wxStrcat(pSqlStmt
, "ROWID = '");
1465 wxStrcat(pSqlStmt
, rowid
);
1466 wxStrcat(pSqlStmt
, "'");
1470 // Unable to delete by ROWID, so build a WHERE
1471 // clause based on the keyfields.
1472 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1473 wxStrcat(pSqlStmt
, whereClause
);
1476 wxStrcat(pSqlStmt
, pWhereClause
);
1478 case DB_DEL_MATCHING
:
1479 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1480 wxStrcat(pSqlStmt
, whereClause
);
1484 } // GetDeleteStmt()
1487 /********** wxTable::GetWhereClause() **********/
1488 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
)
1490 * Note: GetWhereClause() currently ignores timestamp columns.
1491 * They are not included as part of the where clause.
1494 bool moreThanOneColumn
= FALSE
;
1497 // Loop through the columns building a where clause as you go
1499 for (i
= 0; i
< noCols
; i
++)
1501 // Determine if this column should be included in the WHERE clause
1502 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1503 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1505 // Skip over timestamp columns
1506 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1508 // If there is more than 1 column, join them with the keyword "AND"
1509 if (moreThanOneColumn
)
1510 wxStrcat(pWhereClause
, " AND ");
1512 moreThanOneColumn
= TRUE
;
1513 // Concatenate where phrase for the column
1514 if (qualTableName
&& wxStrlen(qualTableName
))
1516 wxStrcat(pWhereClause
, qualTableName
);
1517 wxStrcat(pWhereClause
, ".");
1519 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1520 wxStrcat(pWhereClause
, " = ");
1521 switch(colDefs
[i
].SqlCtype
)
1524 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1527 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1530 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1533 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1536 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1539 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1542 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1545 wxStrcat(pWhereClause
, colValue
);
1548 } // wxTable::GetWhereClause()
1551 /********** wxTable::IsColNull() **********/
1552 bool wxTable::IsColNull(int colNo
)
1554 switch(colDefs
[colNo
].SqlCtype
)
1557 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1559 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1561 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1563 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1565 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1567 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1569 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1570 case SQL_C_TIMESTAMP
:
1571 TIMESTAMP_STRUCT
*pDt
;
1572 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1573 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1580 } // wxTable::IsColNull()
1583 /********** wxTable::CanSelectForUpdate() **********/
1584 bool wxTable::CanSelectForUpdate(void)
1586 if (pDb
->Dbms() == dbmsMY_SQL
)
1589 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1594 } // wxTable::CanSelectForUpdate()
1597 /********** wxTable::CanUpdByROWID() **********/
1598 bool wxTable::CanUpdByROWID(void)
1601 * NOTE: Returning FALSE for now until this can be debugged,
1602 * as the ROWID is not getting updated correctly
1606 if (pDb
->Dbms() == dbmsORACLE
)
1611 } // wxTable::CanUpdByROWID()
1614 /********** wxTable::IsCursorClosedOnCommit() **********/
1615 bool wxTable::IsCursorClosedOnCommit(void)
1617 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1622 } // wxTable::IsCursorClosedOnCommit()
1625 /********** wxTable::ClearMemberVars() **********/
1626 void wxTable::ClearMemberVars(void)
1628 // Loop through the columns setting each member variable to zero
1630 for (i
= 0; i
< noCols
; i
++)
1632 switch(colDefs
[i
].SqlCtype
)
1635 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1638 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1641 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1644 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1647 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1650 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1653 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1655 case SQL_C_TIMESTAMP
:
1656 TIMESTAMP_STRUCT
*pDt
;
1657 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1669 } // wxTable::ClearMemberVars()
1672 /********** wxTable::SetQueryTimeout() **********/
1673 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1675 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1676 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1677 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1678 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1679 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1680 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1681 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1682 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1684 // Completed Successfully
1687 } // wxTable::SetQueryTimeout()
1690 /********** wxTable::SetColDefs() **********/
1691 void wxTable::SetColDefs (int index
, const char *fieldName
, int dataType
, void *pData
,
1692 int cType
, int size
, bool keyField
, bool upd
,
1693 bool insAllow
, bool derivedCol
)
1695 if (!colDefs
) // May happen if the database connection fails
1698 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1700 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1701 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1704 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
1706 colDefs
[index
].DbDataType
= dataType
;
1707 colDefs
[index
].PtrDataObj
= pData
;
1708 colDefs
[index
].SqlCtype
= cType
;
1709 colDefs
[index
].SzDataObj
= size
;
1710 colDefs
[index
].KeyField
= keyField
;
1711 colDefs
[index
].DerivedCol
= derivedCol
;
1712 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1715 colDefs
[index
].Updateable
= FALSE
;
1716 colDefs
[index
].InsertAllowed
= FALSE
;
1720 colDefs
[index
].Updateable
= upd
;
1721 colDefs
[index
].InsertAllowed
= insAllow
;
1724 colDefs
[index
].Null
= FALSE
;
1726 } // wxTable::SetColDefs()
1729 /********** wxTable::SetColDef() **********/
1730 // BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
1731 //bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
1732 wxColDataPtr
* wxTable::SetColDefs (wxColInf
*pColInfs
, ULONG numCols
)
1735 wxColDataPtr
*pColDataPtrs
= NULL
;
1742 pColDataPtrs
= new wxColDataPtr
[numCols
+1];
1744 for (index
= 0; index
< numCols
; index
++)
1748 title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
1749 msg.sprintf("Column name: %s\nData type: %04d\nType name: %s\nColumn size: %d\nBuffer len: %d\nDecimals:%d\nRadix: %d\nNullable: %d\nRemarks: %s",
1750 pColInfs[index].colName,pColInfs[index].sqlDataType,pColInfs[index].typeName,pColInfs[index].columnSize,pColInfs[index].bufferLength,pColInfs[index].decimalDigits,pColInfs[index].numPrecRadix,pColInfs[index].nullable,pColInfs[index].remarks);
1751 msg += " \nDB_DATA_TYPE: ";
1752 switch(pColInfs[index].dbDataType)
1754 case DB_DATA_TYPE_VARCHAR:
1755 msg += pDb->typeInfVarchar.TypeName; break;
1756 case DB_DATA_TYPE_INTEGER:
1757 msg += pDb->typeInfInteger.TypeName; break;
1758 case DB_DATA_TYPE_FLOAT:
1759 msg += pDb->typeInfFloat.TypeName; break;
1760 case DB_DATA_TYPE_DATE:
1761 msg += pDb->typeInfDate.TypeName; break;
1763 wxMessageBox(msg.GetData(),title.GetData());
1765 // Process the fields
1766 switch (pColInfs
[index
].dbDataType
)
1768 case DB_DATA_TYPE_VARCHAR
:
1770 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].bufferLength
+1];
1771 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferLength
;
1772 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
1775 case DB_DATA_TYPE_INTEGER
:
1777 // Can be long or short
1778 if (pColInfs
[index
].bufferLength
== sizeof(long))
1780 pColDataPtrs
[index
].PtrDataObj
= new long;
1781 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
1782 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
1786 pColDataPtrs
[index
].PtrDataObj
= new short;
1787 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
1788 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
1792 case DB_DATA_TYPE_FLOAT
:
1794 // Can be float or double
1795 if (pColInfs
[index
].bufferLength
== sizeof(float))
1797 pColDataPtrs
[index
].PtrDataObj
= new float;
1798 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
1799 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
1803 pColDataPtrs
[index
].PtrDataObj
= new double;
1804 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
1805 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
1809 case DB_DATA_TYPE_DATE
:
1811 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
1812 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
1813 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
1818 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
1821 return (pColDataPtrs
);
1822 } // wxTable::SetColDef()
1825 /********** wxTable::SetCursor() **********/
1826 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1828 if (hstmtActivate
== DEFAULT_CURSOR
)
1829 hstmt
= *hstmtDefault
;
1831 hstmt
= *hstmtActivate
;
1833 } // wxTable::SetCursor()
1836 /********** wxTable::Count(const char *) **********/
1837 ULONG
wxTable::Count(const char *args
)
1840 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1843 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1844 wxStrcpy(sqlStmt
, "SELECT COUNT(");
1845 wxStrcat(sqlStmt
, args
);
1846 wxStrcat(sqlStmt
, ") FROM ");
1847 wxStrcat(sqlStmt
, queryTableName
);
1849 if (from
&& wxStrlen(from
))
1850 wxStrcat(sqlStmt
, from
);
1852 // Add the where clause if one is provided
1853 if (where
&& wxStrlen(where
))
1855 wxStrcat(sqlStmt
, " WHERE ");
1856 wxStrcat(sqlStmt
, where
);
1859 pDb
->WriteSqlLog(sqlStmt
);
1861 // Initialize the Count cursor if it's not already initialized
1864 hstmtCount
= NewCursor(FALSE
,FALSE
);
1870 // Execute the SQL statement
1871 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1873 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1878 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1880 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1884 // Obtain the result
1885 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1887 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1892 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1893 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1895 // Return the record count
1898 } // wxTable::Count()
1901 /********** wxTable::Refresh() **********/
1902 bool wxTable::Refresh(void)
1906 // Switch to the internal cursor so any active cursors are not corrupted
1907 HSTMT currCursor
= GetCursor();
1908 hstmt
= hstmtInternal
;
1910 // Save the where and order by clauses
1911 char *saveWhere
= where
;
1912 char *saveOrderBy
= orderBy
;
1914 // Build a where clause to refetch the record with. Try and use the
1915 // ROWID if it's available, ow use the key fields.
1916 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1917 wxStrcpy(whereClause
, "");
1918 if (CanUpdByROWID())
1921 char rowid
[ROWID_LEN
+1];
1923 // Get the ROWID value. If not successful retreiving the ROWID,
1924 // simply fall down through the code and build the WHERE clause
1925 // based on the key fields.
1926 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1928 wxStrcat(whereClause
, queryTableName
);
1929 wxStrcat(whereClause
, ".ROWID = '");
1930 wxStrcat(whereClause
, rowid
);
1931 wxStrcat(whereClause
, "'");
1935 // If unable to use the ROWID, build a where clause from the keyfields
1936 if (wxStrlen(whereClause
) == 0)
1937 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1939 // Requery the record
1940 where
= whereClause
;
1945 if (result
&& !GetNext())
1948 // Switch back to original cursor
1949 SetCursor(&currCursor
);
1951 // Free the internal cursor
1952 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1953 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1955 // Restore the original where and order by clauses
1957 orderBy
= saveOrderBy
;
1961 } // wxTable::Refresh()
1964 /********** wxTable::SetNull(int colNo) **********/
1965 bool wxTable::SetNull(int colNo
)
1968 return(colDefs
[colNo
].Null
= TRUE
);
1972 } // wxTable::SetNull(int colNo)
1975 /********** wxTable::SetNull(char *colName) **********/
1976 bool wxTable::SetNull(const char *colName
)
1979 for (i
= 0; i
< noCols
; i
++)
1981 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
1986 return(colDefs
[i
].Null
= TRUE
);
1990 } // wxTable::SetNull(char *colName)
1993 /********** wxTable::NewCursor() **********/
1994 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
1996 HSTMT
*newHSTMT
= new HSTMT
;
2001 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2003 pDb
->DispAllErrors(henv
, hdbc
);
2008 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2010 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2017 if(!bindCols(*newHSTMT
))
2025 SetCursor(newHSTMT
);
2029 } // wxTable::NewCursor()
2032 /********** wxTable::DeleteCursor() **********/
2033 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
2037 if (!hstmtDel
) // Cursor already deleted
2040 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2042 pDb
->DispAllErrors(henv
, hdbc
);
2050 } // wxTable::DeleteCursor()
2052 #endif // wxUSE_ODBC