1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxTable class.
6 // -Dynamic cursor support - Only one predefined cursor, as many others as
7 // you need may be created on demand
8 // -Reduced number of active cursors significantly
9 // -Query-Only wxTable objects
12 // Copyright: (c) 1996 Remstar International, Inc.
13 // Licence: wxWindows licence, plus:
14 // Notice: This class library and its intellectual design are free of charge for use,
15 // modification, enhancement, debugging under the following conditions:
16 // 1) These classes may only be used as part of the implementation of a
17 // wxWindows-based application
18 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
19 // user groups free of all charges for use with the wxWindows library.
20 // 3) These classes may not be distributed as part of any other class library,
21 // DLL, text (written or electronic), other than a complete distribution of
22 // the wxWindows GUI development toolkit.
23 ///////////////////////////////////////////////////////////////////////////////
30 // Use this line for wxWindows v1.x
32 // Use this line for wxWindows v2.x
33 #include "wx/version.h"
34 #include "wx/wxprec.h"
36 #if wxMAJOR_VERSION == 2
38 # pragma implementation "dbtable.h"
42 #ifdef DBDEBUG_CONSOLE
50 #if wxMAJOR_VERSION == 2
52 #include "wx/string.h"
53 #include "wx/object.h"
56 #include "wx/msgdlg.h"
58 #include "wx/filefn.h"
61 #if wxMAJOR_VERSION == 1
62 # if defined(wx_msw) || defined(wx_x)
78 #if wxMAJOR_VERSION == 1
80 #elif wxMAJOR_VERSION == 2
81 #include "wx/dbtable.h"
85 // The HPUX preprocessor lines below were commented out on 8/20/97
86 // because macros.h currently redefines DEBUG and is unneeded.
88 // # include <macros.h>
91 # include <sys/minmax.h>
95 ULONG lastTableID
= 0;
103 /********** wxTable::wxTable() **********/
104 wxTable::wxTable(wxDB
*pwxDB
, const char *tblName
, const int nCols
,
105 const char *qryTblName
, bool qryOnly
, const char *tblPath
)
107 pDb
= pwxDB
; // Pointer to the wxDB object
111 hstmtDefault
= 0; // Initialized below
112 hstmtCount
= 0; // Initialized first time it is needed
119 noCols
= nCols
; // No. of cols in the table
120 where
= 0; // Where clause
121 orderBy
= 0; // Order By clause
122 from
= 0; // From clause
123 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
128 wxStrcpy(tableName
, tblName
); // Table Name
130 wxStrcpy(tablePath
, tblPath
); // Table Path - used for dBase files
132 if (qryTblName
) // Name of the table/view to query
133 wxStrcpy(queryTableName
, qryTblName
);
135 wxStrcpy(queryTableName
, tblName
);
137 // assert(pDb); // Assert is placed after table name is assigned for error reporting reasons
144 tableID
= ++lastTableID
;
145 sprintf(s
, "wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName
,tableID
,pDb
);
148 CstructTablesInUse
*tableInUse
;
149 tableInUse
= new CstructTablesInUse();
150 tableInUse
->tableName
= tblName
;
151 tableInUse
->tableID
= tableID
;
152 tableInUse
->pDb
= pDb
;
153 TablesInUse
.Append(tableInUse
);
158 // Grab the HENV and HDBC from the wxDB object
162 // Allocate space for column definitions
164 colDefs
= new CcolDef
[noCols
]; // Points to the first column defintion
166 // Allocate statement handles for the table
169 // Allocate a separate statement handle for performing inserts
170 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
171 pDb
->DispAllErrors(henv
, hdbc
);
172 // Allocate a separate statement handle for performing deletes
173 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
174 pDb
->DispAllErrors(henv
, hdbc
);
175 // Allocate a separate statement handle for performing updates
176 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
177 pDb
->DispAllErrors(henv
, hdbc
);
179 // Allocate a separate statement handle for internal use
180 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
181 pDb
->DispAllErrors(henv
, hdbc
);
183 // Set the cursor type for the statement handles
184 cursorType
= SQL_CURSOR_STATIC
;
185 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
187 // Check to see if cursor type is supported
188 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
189 if (! wxStrcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
191 // Datasource does not support static cursors. Driver
192 // will substitute a cursor type. Call SQLGetStmtOption()
193 // to determine which cursor type was selected.
194 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
195 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
196 #ifdef DBDEBUG_CONSOLE
197 cout
<< "Static cursor changed to: ";
200 case SQL_CURSOR_FORWARD_ONLY
:
201 cout
<< "Forward Only"; break;
202 case SQL_CURSOR_STATIC
:
203 cout
<< "Static"; break;
204 case SQL_CURSOR_KEYSET_DRIVEN
:
205 cout
<< "Keyset Driven"; break;
206 case SQL_CURSOR_DYNAMIC
:
207 cout
<< "Dynamic"; break;
209 cout
<< endl
<< endl
;
214 pDb
->DispNextError();
215 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
218 #ifdef DBDEBUG_CONSOLE
220 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
225 // Set the cursor type for the INSERT statement handle
226 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
227 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
228 // Set the cursor type for the DELETE statement handle
229 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
230 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
231 // Set the cursor type for the UPDATE statement handle
232 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
233 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
236 // Make the default cursor the active cursor
237 hstmtDefault
= NewCursor(FALSE
,FALSE
);
238 assert(hstmtDefault
);
239 hstmt
= *hstmtDefault
;
241 } // wxTable::wxTable()
243 /********** wxTable::~wxTable() **********/
249 sprintf(s
, "wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName
,tableID
,pDb
);
258 pNode
= TablesInUse
.First();
259 while (pNode
&& !found
)
261 if (((CstructTablesInUse
*)pNode
->Data())->tableID
== tableID
)
264 if (!TablesInUse
.DeleteNode(pNode
))
265 wxMessageBox (s
,"Unable to delete node!");
268 pNode
= pNode
->Next();
273 sprintf(msg
,"Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s
);
274 wxMessageBox (msg
,"NOTICE...");
279 // Decrement the wxDB table count
283 // Delete memory allocated for column definitions
287 // Free statement handles
291 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
292 pDb
->DispAllErrors(henv
, hdbc
);
294 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
295 pDb
->DispAllErrors(henv
, hdbc
);
297 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
298 pDb
->DispAllErrors(henv
, hdbc
);
301 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
302 pDb
->DispAllErrors(henv
, hdbc
);
304 // Delete dynamically allocated cursors
306 DeleteCursor(hstmtDefault
);
308 DeleteCursor(hstmtCount
);
310 } // wxTable::~wxTable()
312 /***************************** PRIVATE FUNCTIONS *****************************/
314 /********** wxTable::bindInsertParams() **********/
315 bool wxTable::bindInsertParams(void)
322 UDWORD precision
= 0;
325 // Bind each column (that can be inserted) of the table to a parameter marker
327 for (i
= 0; i
< noCols
; i
++)
329 if (! colDefs
[i
].InsertAllowed
)
331 switch(colDefs
[i
].DbDataType
)
333 case DB_DATA_TYPE_VARCHAR
:
334 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
335 precision
= colDefs
[i
].SzDataObj
;
337 colDefs
[i
].CbValue
= SQL_NTS
;
339 case DB_DATA_TYPE_INTEGER
:
340 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
341 precision
= pDb
->typeInfInteger
.Precision
;
343 colDefs
[i
].CbValue
= 0;
345 case DB_DATA_TYPE_FLOAT
:
346 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
347 precision
= pDb
->typeInfFloat
.Precision
;
348 scale
= pDb
->typeInfFloat
.MaximumScale
;
349 // SQL Sybase Anywhere v5.5 returned a negative number for the
350 // MaxScale. This caused ODBC to kick out an error on ibscale.
351 // I check for this here and set the scale = precision.
353 // scale = (short) precision;
354 colDefs
[i
].CbValue
= 0;
356 case DB_DATA_TYPE_DATE
:
357 fSqlType
= pDb
->typeInfDate
.FsqlType
;
358 precision
= pDb
->typeInfDate
.Precision
;
360 colDefs
[i
].CbValue
= 0;
366 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
367 colDefs
[i
].Null
= FALSE
;
369 if (SQLBindParameter(hstmtInsert
, i
+1, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
370 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
371 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
372 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
375 // Completed successfully
378 } // wxTable::bindInsertParams()
380 /********** wxTable::bindUpdateParams() **********/
381 bool wxTable::bindUpdateParams(void)
388 UDWORD precision
= 0;
391 // Bind each UPDATEABLE column of the table to a parameter marker
393 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
395 if (! colDefs
[i
].Updateable
)
397 switch(colDefs
[i
].DbDataType
)
399 case DB_DATA_TYPE_VARCHAR
:
400 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
401 precision
= colDefs
[i
].SzDataObj
;
403 colDefs
[i
].CbValue
= SQL_NTS
;
405 case DB_DATA_TYPE_INTEGER
:
406 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
407 precision
= pDb
->typeInfInteger
.Precision
;
409 colDefs
[i
].CbValue
= 0;
411 case DB_DATA_TYPE_FLOAT
:
412 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
413 precision
= pDb
->typeInfFloat
.Precision
;
414 scale
= pDb
->typeInfFloat
.MaximumScale
;
415 // SQL Sybase Anywhere v5.5 returned a negative number for the
416 // MaxScale. This caused ODBC to kick out an error on ibscale.
417 // I check for this here and set the scale = precision.
419 // scale = (short) precision;
420 colDefs
[i
].CbValue
= 0;
422 case DB_DATA_TYPE_DATE
:
423 fSqlType
= pDb
->typeInfDate
.FsqlType
;
424 precision
= pDb
->typeInfDate
.Precision
;
426 colDefs
[i
].CbValue
= 0;
429 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
430 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
431 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
432 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
435 // Completed successfully
438 } // wxTable::bindUpdateParams()
440 /********** wxTable::bindCols() **********/
441 bool wxTable::bindCols(HSTMT cursor
)
445 // Bind each column of the table to a memory address for fetching data
447 for (i
= 0; i
< noCols
; i
++)
449 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
450 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
451 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
454 // Completed successfully
457 } // wxTable::bindCols()
459 /********** wxTable::getRec() **********/
460 bool wxTable::getRec(UWORD fetchType
)
464 #if !wxODBC_FWD_ONLY_CURSORS
466 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
470 // if ((retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus)) != SQL_SUCCESS)
471 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
472 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
473 if (retcode
== SQL_NO_DATA_FOUND
)
476 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
479 // Fetch the next record from the record set
480 retcode
= SQLFetch(hstmt
);
481 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
483 if (retcode
== SQL_NO_DATA_FOUND
)
486 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
490 // Completed successfully
493 } // wxTable::getRec()
495 /********** wxTable::execDelete() **********/
496 bool wxTable::execDelete(const char *pSqlStmt
)
498 // Execute the DELETE statement
499 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
500 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
502 // Record deleted successfully
505 } // wxTable::execDelete()
507 /********** wxTable::execUpdate() **********/
508 bool wxTable::execUpdate(const char *pSqlStmt
)
510 // Execute the UPDATE statement
511 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
512 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
514 // Record deleted successfully
517 } // wxTable::execUpdate()
519 /********** wxTable::query() **********/
520 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
522 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
524 // Set the selectForUpdate member variable
526 // The user may wish to select for update, but the DBMS may not be capable
527 selectForUpdate
= CanSelectForUpdate();
529 selectForUpdate
= FALSE
;
531 // Set the SQL SELECT string
532 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
533 { // so generate a select statement.
534 GetSelectStmt(sqlStmt
, queryType
, distinct
);
535 pDb
->WriteSqlLog(sqlStmt
);
538 // Make sure the cursor is closed first
539 if (! CloseCursor(hstmt
))
542 // Execute the SQL SELECT statement
545 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
546 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
547 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
549 // Completed successfully
552 } // wxTable::query()
556 /********** wxTable::Open() **********/
557 bool wxTable::Open(void)
563 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
565 // Verify that the table exists in the database
566 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
569 sprintf(s
, "Error opening '%s', table/view does not exist in the database.", tableName
);
574 // Bind the member variables for field exchange between
575 // the wxTable object and the ODBC record.
578 if (!bindInsertParams()) // Inserts
580 if (!bindUpdateParams()) // Updates
583 if (!bindCols(*hstmtDefault
)) // Selects
585 if (!bindCols(hstmtInternal
)) // Internal use only
588 * Do NOT bind the hstmtCount cursor!!!
591 // Build an insert statement using parameter markers
592 if (!queryOnly
&& noCols
> 0)
594 bool needComma
= FALSE
;
595 sprintf(sqlStmt
, "INSERT INTO %s (", tableName
);
596 for (i
= 0; i
< noCols
; i
++)
598 if (! colDefs
[i
].InsertAllowed
)
601 wxStrcat(sqlStmt
, ",");
602 wxStrcat(sqlStmt
, colDefs
[i
].ColName
);
606 wxStrcat(sqlStmt
, ") VALUES (");
607 for (i
= 0; i
< noCols
; i
++)
609 if (! colDefs
[i
].InsertAllowed
)
612 wxStrcat(sqlStmt
, ",");
613 wxStrcat(sqlStmt
, "?");
616 wxStrcat(sqlStmt
, ")");
618 // pDb->WriteSqlLog(sqlStmt);
620 // Prepare the insert statement for execution
621 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
622 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
625 // Completed successfully
630 /********** wxTable::Query() **********/
631 bool wxTable::Query(bool forUpdate
, bool distinct
)
634 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
636 } // wxTable::Query()
638 /********** wxTable::QueryBySqlStmt() **********/
639 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
641 pDb
->WriteSqlLog(pSqlStmt
);
643 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
645 } // wxTable::QueryBySqlStmt()
647 /********** wxTable::QueryMatching() **********/
648 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
651 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
653 } // wxTable::QueryMatching()
655 /********** wxTable::QueryOnKeyFields() **********/
656 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
659 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
661 } // wxTable::QueryOnKeyFields()
663 /********** wxTable::GetSelectStmt() **********/
664 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
666 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
670 // Build a select statement to query the database
671 wxStrcpy(pSqlStmt
, "SELECT ");
673 // SELECT DISTINCT values only?
675 wxStrcat(pSqlStmt
, "DISTINCT ");
677 // Was a FROM clause specified to join tables to the base table?
678 // Available for ::Query() only!!!
679 bool appendFromClause
= FALSE
;
680 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
681 appendFromClause
= TRUE
;
683 // Add the column list
685 for (i
= 0; i
< noCols
; i
++)
687 // If joining tables, the base table column names must be qualified to avoid ambiguity
688 if (appendFromClause
)
690 wxStrcat(pSqlStmt
, queryTableName
);
691 wxStrcat(pSqlStmt
, ".");
693 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
695 wxStrcat(pSqlStmt
, ",");
698 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
699 // the ROWID if querying distinct records. The rowid will always be unique.
700 if (!distinct
&& CanUpdByROWID())
702 // If joining tables, the base table column names must be qualified to avoid ambiguity
703 if (appendFromClause
)
705 wxStrcat(pSqlStmt
, ",");
706 wxStrcat(pSqlStmt
, queryTableName
);
707 wxStrcat(pSqlStmt
, ".ROWID");
710 wxStrcat(pSqlStmt
, ",ROWID");
713 // Append the FROM tablename portion
714 wxStrcat(pSqlStmt
, " FROM ");
715 wxStrcat(pSqlStmt
, queryTableName
);
717 // Sybase uses the HOLDLOCK keyword to lock a record during query.
718 // The HOLDLOCK keyword follows the table name in the from clause.
719 // Each table in the from clause must specify HOLDLOCK or
720 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
721 // is parsed but ignored in SYBASE Transact-SQL.
722 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
723 wxStrcat(pSqlStmt
, " HOLDLOCK");
725 if (appendFromClause
)
726 wxStrcat(pSqlStmt
, from
);
728 // Append the WHERE clause. Either append the where clause for the class
729 // or build a where clause. The typeOfSelect determines this.
732 case DB_SELECT_WHERE
:
733 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
735 wxStrcat(pSqlStmt
, " WHERE ");
736 wxStrcat(pSqlStmt
, where
);
739 case DB_SELECT_KEYFIELDS
:
740 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
741 if (wxStrlen(whereClause
))
743 wxStrcat(pSqlStmt
, " WHERE ");
744 wxStrcat(pSqlStmt
, whereClause
);
747 case DB_SELECT_MATCHING
:
748 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
749 if (wxStrlen(whereClause
))
751 wxStrcat(pSqlStmt
, " WHERE ");
752 wxStrcat(pSqlStmt
, whereClause
);
757 // Append the ORDER BY clause
758 if (orderBy
&& wxStrlen(orderBy
))
760 wxStrcat(pSqlStmt
, " ORDER BY ");
761 wxStrcat(pSqlStmt
, orderBy
);
764 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
765 // parses the FOR UPDATE clause but ignores it. See the comment above on the
766 // HOLDLOCK for Sybase.
767 if (selectForUpdate
&& CanSelectForUpdate())
768 wxStrcat(pSqlStmt
, " FOR UPDATE");
770 } // wxTable::GetSelectStmt()
772 /********** wxTable::GetRowNum() **********/
773 UWORD
wxTable::GetRowNum(void)
777 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
779 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
783 // Completed successfully
784 return((UWORD
) rowNum
);
786 } // wxTable::GetRowNum()
788 /********** wxTable::CloseCursor() **********/
789 bool wxTable::CloseCursor(HSTMT cursor
)
791 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
792 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
794 // Completed successfully
797 } // wxTable::CloseCursor()
799 /********** wxTable::CreateTable() **********/
800 bool wxTable::CreateTable(bool attemptDrop
)
806 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
808 #ifdef DBDEBUG_CONSOLE
809 cout
<< "Creating Table " << tableName
<< "..." << endl
;
813 if (attemptDrop
&& !DropTable())
817 #ifdef DBDEBUG_CONSOLE
818 for (i
= 0; i
< noCols
; i
++)
820 // Exclude derived columns since they are NOT part of the base table
821 if (colDefs
[i
].DerivedCol
)
823 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
824 switch(colDefs
[i
].DbDataType
)
826 case DB_DATA_TYPE_VARCHAR
:
827 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
829 case DB_DATA_TYPE_INTEGER
:
830 cout
<< pDb
->typeInfInteger
.TypeName
;
832 case DB_DATA_TYPE_FLOAT
:
833 cout
<< pDb
->typeInfFloat
.TypeName
;
835 case DB_DATA_TYPE_DATE
:
836 cout
<< pDb
->typeInfDate
.TypeName
;
843 // Build a CREATE TABLE string from the colDefs structure.
844 bool needComma
= FALSE
;
845 sprintf(sqlStmt
, "CREATE TABLE %s (", tableName
);
846 for (i
= 0; i
< noCols
; i
++)
848 // Exclude derived columns since they are NOT part of the base table
849 if (colDefs
[i
].DerivedCol
)
853 wxStrcat(sqlStmt
, ",");
855 wxStrcat(sqlStmt
, colDefs
[i
].ColName
);
856 wxStrcat(sqlStmt
, " ");
858 switch(colDefs
[i
].DbDataType
)
860 case DB_DATA_TYPE_VARCHAR
:
861 wxStrcat(sqlStmt
, pDb
->typeInfVarchar
.TypeName
); break;
862 case DB_DATA_TYPE_INTEGER
:
863 wxStrcat(sqlStmt
, pDb
->typeInfInteger
.TypeName
); break;
864 case DB_DATA_TYPE_FLOAT
:
865 wxStrcat(sqlStmt
, pDb
->typeInfFloat
.TypeName
); break;
866 case DB_DATA_TYPE_DATE
:
867 wxStrcat(sqlStmt
, pDb
->typeInfDate
.TypeName
); break;
869 // For varchars, append the size of the string
870 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
873 // wxStrcat(sqlStmt, "(");
874 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
875 // wxStrcat(sqlStmt, ")");
876 sprintf(s
, "(%d)", colDefs
[i
].SzDataObj
);
877 wxStrcat(sqlStmt
, s
);
880 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
882 if (colDefs
[i
].KeyField
)
884 wxStrcat(sqlStmt
, " NOT NULL");
890 // If there is a primary key defined, include it in the create statement
891 for (i
= j
= 0; i
< noCols
; i
++)
893 if (colDefs
[i
].KeyField
)
899 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
901 if (pDb
->Dbms() != dbmsMY_SQL
)
903 wxStrcat(sqlStmt
, ",CONSTRAINT ");
904 wxStrcat(sqlStmt
, tableName
);
905 wxStrcat(sqlStmt
, "_PIDX PRIMARY KEY (");
909 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
910 wxStrcat(sqlStmt
, ", PRIMARY KEY (");
913 // List column name(s) of column(s) comprising the primary key
914 for (i
= j
= 0; i
< noCols
; i
++)
916 if (colDefs
[i
].KeyField
)
918 if (j
++) // Multi part key, comma separate names
919 wxStrcat(sqlStmt
, ",");
920 wxStrcat(sqlStmt
, colDefs
[i
].ColName
);
923 wxStrcat(sqlStmt
, ")");
925 // Append the closing parentheses for the create table statement
926 wxStrcat(sqlStmt
, ")");
928 pDb
->WriteSqlLog(sqlStmt
);
930 #ifdef DBDEBUG_CONSOLE
931 cout
<< endl
<< sqlStmt
<< endl
;
934 // Execute the CREATE TABLE statement
935 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
937 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
938 pDb
->RollbackTrans();
943 // Commit the transaction and close the cursor
944 if (! pDb
->CommitTrans())
946 if (! CloseCursor(hstmt
))
949 // Database table created successfully
952 } // wxTable::CreateTable()
954 /********** wxTable::DropTable() **********/
955 bool wxTable::DropTable()
957 // NOTE: This function returns TRUE if the Table does not exist, but
958 // only for identified databases. Code will need to be added
959 // below for any other databases when those databases are defined
960 // to handle this situation consistently
962 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
964 sprintf(sqlStmt
, "DROP TABLE %s", tableName
);
966 pDb
->WriteSqlLog(sqlStmt
);
968 #ifdef DBDEBUG_CONSOLE
969 cout
<< endl
<< sqlStmt
<< endl
;
972 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
974 // Check for "Base table not found" error and ignore
975 pDb
->GetNextError(henv
, hdbc
, hstmt
);
976 if (wxStrcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
978 // Check for product specific error codes
979 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
980 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"S1000")) || // untested
981 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01")))) // untested
983 pDb
->DispNextError();
984 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
985 pDb
->RollbackTrans();
992 // Commit the transaction and close the cursor
993 if (! pDb
->CommitTrans())
995 if (! CloseCursor(hstmt
))
999 } // wxTable::DropTable()
1001 /********** wxTable::CreateIndex() **********/
1002 bool wxTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
1004 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1006 // Drop the index first
1007 if (attemptDrop
&& !DropIndex(idxName
))
1010 // Build a CREATE INDEX statement
1011 wxStrcpy(sqlStmt
, "CREATE ");
1013 wxStrcat(sqlStmt
, "UNIQUE ");
1015 wxStrcat(sqlStmt
, "INDEX ");
1016 wxStrcat(sqlStmt
, idxName
);
1017 wxStrcat(sqlStmt
, " ON ");
1018 wxStrcat(sqlStmt
, tableName
);
1019 wxStrcat(sqlStmt
, " (");
1021 // Append list of columns making up index
1023 for (i
= 0; i
< noIdxCols
; i
++)
1025 wxStrcat(sqlStmt
, pIdxDefs
[i
].ColName
);
1026 /* Postgres doesn't cope with ASC */
1027 if (pDb
->Dbms() != dbmsPOSTGRES
)
1029 if (pIdxDefs
[i
].Ascending
)
1030 wxStrcat(sqlStmt
, " ASC");
1032 wxStrcat(sqlStmt
, " DESC");
1035 if ((i
+ 1) < noIdxCols
)
1036 wxStrcat(sqlStmt
, ",");
1039 // Append closing parentheses
1040 wxStrcat(sqlStmt
, ")");
1042 pDb
->WriteSqlLog(sqlStmt
);
1044 #ifdef DBDEBUG_CONSOLE
1045 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1048 // Execute the CREATE INDEX statement
1049 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1051 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1052 pDb
->RollbackTrans();
1057 // Commit the transaction and close the cursor
1058 if (! pDb
->CommitTrans())
1060 if (! CloseCursor(hstmt
))
1063 // Index Created Successfully
1066 } // wxTable::CreateIndex()
1068 /********** wxTable::DropIndex() **********/
1069 bool wxTable::DropIndex(const char * idxName
)
1071 // NOTE: This function returns TRUE if the Index does not exist, but
1072 // only for identified databases. Code will need to be added
1073 // below for any other databases when those databases are defined
1074 // to handle this situation consistently
1076 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1078 if (pDb
->Dbms() == dbmsACCESS
)
1079 sprintf(sqlStmt
, "DROP INDEX %s ON %s",idxName
,tableName
);
1080 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1081 sprintf(sqlStmt
, "DROP INDEX %s.%s",tableName
,idxName
);
1083 sprintf(sqlStmt
, "DROP INDEX %s",idxName
);
1085 pDb
->WriteSqlLog(sqlStmt
);
1087 #ifdef DBDEBUG_CONSOLE
1088 cout
<< endl
<< sqlStmt
<< endl
;
1091 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1093 // Check for "Index not found" error and ignore
1094 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1095 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1097 // Check for product specific error codes
1098 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1099 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1100 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S02")) // untested
1103 pDb
->DispNextError();
1104 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1105 pDb
->RollbackTrans();
1112 // Commit the transaction and close the cursor
1113 if (! pDb
->CommitTrans())
1115 if (! CloseCursor(hstmt
))
1119 } // wxTable::DropIndex()
1121 /********** wxTable::Insert() **********/
1122 int wxTable::Insert(void)
1130 // Insert the record by executing the already prepared insert statement
1132 retcode
=SQLExecute(hstmtInsert
);
1133 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1135 // Check to see if integrity constraint was violated
1136 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1137 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1138 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1141 pDb
->DispNextError();
1142 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1147 // Record inserted into the datasource successfully
1150 } // wxTable::Insert()
1152 /********** wxTable::Update() **********/
1153 bool wxTable::Update(void)
1159 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1161 // Build the SQL UPDATE statement
1162 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1164 pDb
->WriteSqlLog(sqlStmt
);
1166 #ifdef DBDEBUG_CONSOLE
1167 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1170 // Execute the SQL UPDATE statement
1171 return(execUpdate(sqlStmt
));
1173 } // wxTable::Update()
1175 /********** wxTable::Update(pSqlStmt) **********/
1176 bool wxTable::Update(const char *pSqlStmt
)
1182 pDb
->WriteSqlLog(pSqlStmt
);
1184 return(execUpdate(pSqlStmt
));
1186 } // wxTable::Update(pSqlStmt)
1188 /********** wxTable::UpdateWhere() **********/
1189 bool wxTable::UpdateWhere(const char *pWhereClause
)
1195 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1197 // Build the SQL UPDATE statement
1198 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1200 pDb
->WriteSqlLog(sqlStmt
);
1202 #ifdef DBDEBUG_CONSOLE
1203 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1206 // Execute the SQL UPDATE statement
1207 return(execUpdate(sqlStmt
));
1209 } // wxTable::UpdateWhere()
1211 /********** wxTable::Delete() **********/
1212 bool wxTable::Delete(void)
1218 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1220 // Build the SQL DELETE statement
1221 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1223 pDb
->WriteSqlLog(sqlStmt
);
1225 // Execute the SQL DELETE statement
1226 return(execDelete(sqlStmt
));
1228 } // wxTable::Delete()
1230 /********** wxTable::DeleteWhere() **********/
1231 bool wxTable::DeleteWhere(const char *pWhereClause
)
1237 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1239 // Build the SQL DELETE statement
1240 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1242 pDb
->WriteSqlLog(sqlStmt
);
1244 // Execute the SQL DELETE statement
1245 return(execDelete(sqlStmt
));
1247 } // wxTable::DeleteWhere()
1249 /********** wxTable::DeleteMatching() **********/
1250 bool wxTable::DeleteMatching(void)
1256 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1258 // Build the SQL DELETE statement
1259 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1261 pDb
->WriteSqlLog(sqlStmt
);
1263 // Execute the SQL DELETE statement
1264 return(execDelete(sqlStmt
));
1266 } // wxTable::DeleteMatching()
1268 /********** wxTable::GetUpdateStmt() **********/
1269 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1275 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1276 bool firstColumn
= TRUE
;
1279 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1281 // Append a list of columns to be updated
1283 for (i
= 0; i
< noCols
; i
++)
1285 // Only append Updateable columns
1286 if (colDefs
[i
].Updateable
)
1289 wxStrcat(pSqlStmt
, ",");
1291 firstColumn
= FALSE
;
1292 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1293 wxStrcat(pSqlStmt
, " = ?");
1297 // Append the WHERE clause to the SQL UPDATE statement
1298 wxStrcat(pSqlStmt
, " WHERE ");
1301 case DB_UPD_KEYFIELDS
:
1302 // If the datasource supports the ROWID column, build
1303 // the where on ROWID for efficiency purposes.
1304 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1305 if (CanUpdByROWID())
1308 char rowid
[ROWID_LEN
];
1310 // Get the ROWID value. If not successful retreiving the ROWID,
1311 // simply fall down through the code and build the WHERE clause
1312 // based on the key fields.
1313 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1315 wxStrcat(pSqlStmt
, "ROWID = '");
1316 wxStrcat(pSqlStmt
, rowid
);
1317 wxStrcat(pSqlStmt
, "'");
1321 // Unable to delete by ROWID, so build a WHERE
1322 // clause based on the keyfields.
1323 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1324 wxStrcat(pSqlStmt
, whereClause
);
1327 wxStrcat(pSqlStmt
, pWhereClause
);
1330 } // GetUpdateStmt()
1332 /********** wxTable::GetDeleteStmt() **********/
1333 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1339 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1343 // Handle the case of DeleteWhere() and the where clause is blank. It should
1344 // delete all records from the database in this case.
1345 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1347 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1351 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1353 // Append the WHERE clause to the SQL DELETE statement
1356 case DB_DEL_KEYFIELDS
:
1357 // If the datasource supports the ROWID column, build
1358 // the where on ROWID for efficiency purposes.
1359 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1360 if (CanUpdByROWID())
1363 char rowid
[ROWID_LEN
];
1365 // Get the ROWID value. If not successful retreiving the ROWID,
1366 // simply fall down through the code and build the WHERE clause
1367 // based on the key fields.
1368 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1370 wxStrcat(pSqlStmt
, "ROWID = '");
1371 wxStrcat(pSqlStmt
, rowid
);
1372 wxStrcat(pSqlStmt
, "'");
1376 // Unable to delete by ROWID, so build a WHERE
1377 // clause based on the keyfields.
1378 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1379 wxStrcat(pSqlStmt
, whereClause
);
1382 wxStrcat(pSqlStmt
, pWhereClause
);
1384 case DB_DEL_MATCHING
:
1385 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1386 wxStrcat(pSqlStmt
, whereClause
);
1390 } // GetDeleteStmt()
1392 /********** wxTable::GetWhereClause() **********/
1394 * Note: GetWhereClause() currently ignores timestamp columns.
1395 * They are not included as part of the where clause.
1398 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
)
1400 bool moreThanOneColumn
= FALSE
;
1403 // Loop through the columns building a where clause as you go
1405 for (i
= 0; i
< noCols
; i
++)
1407 // Determine if this column should be included in the WHERE clause
1408 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1409 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1411 // Skip over timestamp columns
1412 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1414 // If there is more than 1 column, join them with the keyword "AND"
1415 if (moreThanOneColumn
)
1416 wxStrcat(pWhereClause
, " AND ");
1418 moreThanOneColumn
= TRUE
;
1419 // Concatenate where phrase for the column
1420 if (qualTableName
&& wxStrlen(qualTableName
))
1422 wxStrcat(pWhereClause
, qualTableName
);
1423 wxStrcat(pWhereClause
, ".");
1425 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1426 wxStrcat(pWhereClause
, " = ");
1427 switch(colDefs
[i
].SqlCtype
)
1430 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1433 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1436 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1439 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1442 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1445 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1448 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1451 wxStrcat(pWhereClause
, colValue
);
1455 } // wxTable::GetWhereClause()
1457 /********** wxTable::IsColNull() **********/
1458 bool wxTable::IsColNull(int colNo
)
1460 switch(colDefs
[colNo
].SqlCtype
)
1463 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1465 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1467 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1469 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1471 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1473 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1475 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1476 case SQL_C_TIMESTAMP
:
1477 TIMESTAMP_STRUCT
*pDt
;
1478 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1479 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1487 } // wxTable::IsColNull()
1489 /********** wxTable::CanSelectForUpdate() **********/
1490 bool wxTable::CanSelectForUpdate(void)
1492 if (pDb
->Dbms() == dbmsMY_SQL
)
1495 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1500 } // wxTable::CanSelectForUpdate()
1502 /********** wxTable::CanUpdByROWID() **********/
1503 bool wxTable::CanUpdByROWID(void)
1506 //NOTE: Returning FALSE for now until this can be debugged,
1507 // as the ROWID is not getting updated correctly
1510 if (pDb
->Dbms() == dbmsORACLE
)
1515 } // wxTable::CanUpdByROWID()
1517 /********** wxTable::IsCursorClosedOnCommit() **********/
1518 bool wxTable::IsCursorClosedOnCommit(void)
1520 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1525 } // wxTable::IsCursorClosedOnCommit()
1527 /********** wxTable::ClearMemberVars() **********/
1528 void wxTable::ClearMemberVars(void)
1530 // Loop through the columns setting each member variable to zero
1532 for (i
= 0; i
< noCols
; i
++)
1534 switch(colDefs
[i
].SqlCtype
)
1537 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1540 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1543 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1546 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1549 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1552 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1555 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1557 case SQL_C_TIMESTAMP
:
1558 TIMESTAMP_STRUCT
*pDt
;
1559 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1571 } // wxTable::ClearMemberVars()
1573 /********** wxTable::SetQueryTimeout() **********/
1574 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1576 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1577 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1578 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1579 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1580 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1581 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1582 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1583 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1585 // Completed Successfully
1588 } // wxTable::SetQueryTimeout()
1590 /********** wxTable::SetColDefs() **********/
1591 void wxTable::SetColDefs (int index
, const char *fieldName
, int dataType
, void *pData
,
1592 int cType
, int size
, bool keyField
, bool upd
,
1593 bool insAllow
, bool derivedCol
)
1595 if (!colDefs
) // May happen if the database connection fails
1598 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1600 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1601 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1604 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
1606 colDefs
[index
].DbDataType
= dataType
;
1607 colDefs
[index
].PtrDataObj
= pData
;
1608 colDefs
[index
].SqlCtype
= cType
;
1609 colDefs
[index
].SzDataObj
= size
;
1610 colDefs
[index
].KeyField
= keyField
;
1611 colDefs
[index
].DerivedCol
= derivedCol
;
1612 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1615 colDefs
[index
].Updateable
= FALSE
;
1616 colDefs
[index
].InsertAllowed
= FALSE
;
1620 colDefs
[index
].Updateable
= upd
;
1621 colDefs
[index
].InsertAllowed
= insAllow
;
1624 colDefs
[index
].Null
= FALSE
;
1626 } // wxTable::SetColDefs()
1628 /********** wxTable::SetCursor() **********/
1629 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1631 if (hstmtActivate
== DEFAULT_CURSOR
)
1632 hstmt
= *hstmtDefault
;
1634 hstmt
= *hstmtActivate
;
1636 } // wxTable::SetCursor()
1638 /********** wxTable::Count(const char *) **********/
1639 ULONG
wxTable::Count(const char *args
)
1642 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1645 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1646 wxStrcpy(sqlStmt
, "SELECT COUNT(");
1647 wxStrcat(sqlStmt
, args
);
1648 wxStrcat(sqlStmt
, ") FROM ");
1649 wxStrcat(sqlStmt
, queryTableName
);
1651 if (from
&& wxStrlen(from
))
1652 wxStrcat(sqlStmt
, from
);
1654 // Add the where clause if one is provided
1655 if (where
&& wxStrlen(where
))
1657 wxStrcat(sqlStmt
, " WHERE ");
1658 wxStrcat(sqlStmt
, where
);
1661 pDb
->WriteSqlLog(sqlStmt
);
1663 // Initialize the Count cursor if it's not already initialized
1666 hstmtCount
= NewCursor(FALSE
,FALSE
);
1672 // Execute the SQL statement
1673 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1675 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1680 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1682 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1686 // Obtain the result
1687 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1689 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1694 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1695 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1697 // Return the record count
1700 } // wxTable::Count()
1702 /********** wxTable::Refresh() **********/
1703 bool wxTable::Refresh(void)
1707 // Switch to the internal cursor so any active cursors are not corrupted
1708 HSTMT currCursor
= GetCursor();
1709 hstmt
= hstmtInternal
;
1711 // Save the where and order by clauses
1712 char *saveWhere
= where
;
1713 char *saveOrderBy
= orderBy
;
1715 // Build a where clause to refetch the record with. Try and use the
1716 // ROWID if it's available, ow use the key fields.
1717 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1718 wxStrcpy(whereClause
, "");
1719 if (CanUpdByROWID())
1722 char rowid
[ROWID_LEN
+1];
1724 // Get the ROWID value. If not successful retreiving the ROWID,
1725 // simply fall down through the code and build the WHERE clause
1726 // based on the key fields.
1727 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1729 wxStrcat(whereClause
, queryTableName
);
1730 wxStrcat(whereClause
, ".ROWID = '");
1731 wxStrcat(whereClause
, rowid
);
1732 wxStrcat(whereClause
, "'");
1736 // If unable to use the ROWID, build a where clause from the keyfields
1737 if (wxStrlen(whereClause
) == 0)
1738 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1740 // Requery the record
1741 where
= whereClause
;
1746 if (result
&& !GetNext())
1749 // Switch back to original cursor
1750 SetCursor(&currCursor
);
1752 // Free the internal cursor
1753 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1754 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1756 // Restore the original where and order by clauses
1758 orderBy
= saveOrderBy
;
1762 } // wxTable::Refresh()
1764 /********** wxTable::SetNull(UINT colNo) **********/
1765 bool wxTable::SetNull(int colNo
)
1768 return(colDefs
[colNo
].Null
= TRUE
);
1772 } // wxTable::SetNull(UINT colNo)
1774 /********** wxTable::SetNull(char *colName) **********/
1775 bool wxTable::SetNull(const char *colName
)
1778 for (i
= 0; i
< noCols
; i
++)
1780 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
1785 return(colDefs
[i
].Null
= TRUE
);
1789 } // wxTable::SetNull(char *colName)
1791 /********** wxTable::NewCursor() **********/
1792 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
1794 HSTMT
*newHSTMT
= new HSTMT
;
1799 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
1801 pDb
->DispAllErrors(henv
, hdbc
);
1806 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
1808 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
1815 if(!bindCols(*newHSTMT
))
1823 SetCursor(newHSTMT
);
1827 } // wxTable::NewCursor()
1829 /********** wxTable::DeleteCursor() **********/
1830 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
1834 if (!hstmtDel
) // Cursor already deleted
1837 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
1839 pDb
->DispAllErrors(henv
, hdbc
);
1847 } // wxTable::DeleteCursor()
1849 #endif // wxUSE_ODBC