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
, 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 strcpy(tableName
, tblName
); // Table Name
130 strcpy(tablePath
, tblPath
); // Table Path - used for dBase files
132 if (qryTblName
) // Name of the table/view to query
133 strcpy(queryTableName
, qryTblName
);
135 strcpy(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
);
253 #ifndef PROGRAM_FP4UPG
259 pNode
= TablesInUse
.First();
260 while (pNode
&& !found
)
262 if (((CstructTablesInUse
*)pNode
->Data())->tableID
== tableID
)
265 if (!TablesInUse
.DeleteNode(pNode
))
266 wxMessageBox (s
,"Unable to delete node!");
269 pNode
= pNode
->Next();
274 sprintf(msg
,"Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s
);
275 wxMessageBox (msg
,"NOTICE...");
280 // Decrement the wxDB table count
284 // Delete memory allocated for column definitions
288 // Free statement handles
292 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
293 pDb
->DispAllErrors(henv
, hdbc
);
295 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
296 pDb
->DispAllErrors(henv
, hdbc
);
298 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
299 pDb
->DispAllErrors(henv
, hdbc
);
302 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
303 pDb
->DispAllErrors(henv
, hdbc
);
305 // Delete dynamically allocated cursors
307 DeleteCursor(hstmtDefault
);
309 DeleteCursor(hstmtCount
);
311 } // wxTable::~wxTable()
313 /********** wxTable::Open() **********/
314 bool wxTable::Open(void)
320 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
322 // Verify that the table exists in the database
323 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
326 sprintf(s
, "Error opening '%s', table/view does not exist in the database.", tableName
);
331 // Bind the member variables for field exchange between
332 // the wxTable object and the ODBC record.
335 if (!bindInsertParams()) // Inserts
337 if (!bindUpdateParams()) // Updates
340 if (!bindCols(*hstmtDefault
)) // Selects
342 if (!bindCols(hstmtInternal
)) // Internal use only
345 * Do NOT bind the hstmtCount cursor!!!
348 // Build an insert statement using parameter markers
349 if (!queryOnly
&& noCols
> 0)
351 bool needComma
= FALSE
;
352 sprintf(sqlStmt
, "INSERT INTO %s (", tableName
);
353 for (i
= 0; i
< noCols
; i
++)
355 if (! colDefs
[i
].InsertAllowed
)
358 strcat(sqlStmt
, ",");
359 strcat(sqlStmt
, colDefs
[i
].ColName
);
363 strcat(sqlStmt
, ") VALUES (");
364 for (i
= 0; i
< noCols
; i
++)
366 if (! colDefs
[i
].InsertAllowed
)
369 strcat(sqlStmt
, ",");
370 strcat(sqlStmt
, "?");
373 strcat(sqlStmt
, ")");
375 // pDb->WriteSqlLog(sqlStmt);
377 // Prepare the insert statement for execution
378 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
379 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
382 // Completed successfully
387 /********** wxTable::Query() **********/
388 bool wxTable::Query(bool forUpdate
, bool distinct
)
391 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
393 } // wxTable::Query()
395 /********** wxTable::QueryBySqlStmt() **********/
396 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
398 pDb
->WriteSqlLog(pSqlStmt
);
400 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
402 } // wxTable::QueryBySqlStmt()
404 /********** wxTable::QueryMatching() **********/
405 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
408 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
410 } // wxTable::QueryMatching()
412 /********** wxTable::QueryOnKeyFields() **********/
413 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
416 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
418 } // wxTable::QueryOnKeyFields()
420 /********** wxTable::query() **********/
421 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
423 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
425 // Set the selectForUpdate member variable
427 // The user may wish to select for update, but the DBMS may not be capable
428 selectForUpdate
= CanSelectForUpdate();
430 selectForUpdate
= FALSE
;
432 // Set the SQL SELECT string
433 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
434 { // so generate a select statement.
435 GetSelectStmt(sqlStmt
, queryType
, distinct
);
436 pDb
->WriteSqlLog(sqlStmt
);
439 // Make sure the cursor is closed first
440 if (! CloseCursor(hstmt
))
443 // Execute the SQL SELECT statement
446 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
447 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
448 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
450 // Completed successfully
453 } // wxTable::query()
455 /********** wxTable::GetSelectStmt() **********/
456 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
458 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
462 // Build a select statement to query the database
463 strcpy(pSqlStmt
, "SELECT ");
465 // SELECT DISTINCT values only?
467 strcat(pSqlStmt
, "DISTINCT ");
469 // Was a FROM clause specified to join tables to the base table?
470 // Available for ::Query() only!!!
471 bool appendFromClause
= FALSE
;
472 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& strlen(from
))
473 appendFromClause
= TRUE
;
475 // Add the column list
477 for (i
= 0; i
< noCols
; i
++)
479 // If joining tables, the base table column names must be qualified to avoid ambiguity
480 if (appendFromClause
)
482 strcat(pSqlStmt
, queryTableName
);
483 strcat(pSqlStmt
, ".");
485 strcat(pSqlStmt
, colDefs
[i
].ColName
);
487 strcat(pSqlStmt
, ",");
490 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
491 // the ROWID if querying distinct records. The rowid will always be unique.
492 if (!distinct
&& CanUpdByROWID())
494 // If joining tables, the base table column names must be qualified to avoid ambiguity
495 if (appendFromClause
)
497 strcat(pSqlStmt
, ",");
498 strcat(pSqlStmt
, queryTableName
);
499 strcat(pSqlStmt
, ".ROWID");
502 strcat(pSqlStmt
, ",ROWID");
505 // Append the FROM tablename portion
506 strcat(pSqlStmt
, " FROM ");
507 strcat(pSqlStmt
, queryTableName
);
509 // Sybase uses the HOLDLOCK keyword to lock a record during query.
510 // The HOLDLOCK keyword follows the table name in the from clause.
511 // Each table in the from clause must specify HOLDLOCK or
512 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
513 // is parsed but ignored in SYBASE Transact-SQL.
514 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
515 strcat(pSqlStmt
, " HOLDLOCK");
517 if (appendFromClause
)
518 strcat(pSqlStmt
, from
);
520 // Append the WHERE clause. Either append the where clause for the class
521 // or build a where clause. The typeOfSelect determines this.
524 case DB_SELECT_WHERE
:
525 if (where
&& strlen(where
)) // May not want a where clause!!!
527 strcat(pSqlStmt
, " WHERE ");
528 strcat(pSqlStmt
, where
);
531 case DB_SELECT_KEYFIELDS
:
532 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
533 if (strlen(whereClause
))
535 strcat(pSqlStmt
, " WHERE ");
536 strcat(pSqlStmt
, whereClause
);
539 case DB_SELECT_MATCHING
:
540 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
541 if (strlen(whereClause
))
543 strcat(pSqlStmt
, " WHERE ");
544 strcat(pSqlStmt
, whereClause
);
549 // Append the ORDER BY clause
550 if (orderBy
&& strlen(orderBy
))
552 strcat(pSqlStmt
, " ORDER BY ");
553 strcat(pSqlStmt
, orderBy
);
556 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
557 // parses the FOR UPDATE clause but ignores it. See the comment above on the
558 // HOLDLOCK for Sybase.
559 if (selectForUpdate
&& CanSelectForUpdate())
560 strcat(pSqlStmt
, " FOR UPDATE");
562 } // wxTable::GetSelectStmt()
564 /********** wxTable::getRec() **********/
565 bool wxTable::getRec(UWORD fetchType
)
569 #if !wxODBC_FWD_ONLY_CURSORS
571 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
575 // if ((retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus)) != SQL_SUCCESS)
576 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
577 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
578 if (retcode
== SQL_NO_DATA_FOUND
)
581 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
584 // Fetch the next record from the record set
585 retcode
= SQLFetch(hstmt
);
586 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
588 if (retcode
== SQL_NO_DATA_FOUND
)
591 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
595 // Completed successfully
598 } // wxTable::getRec()
600 /********** wxTable::GetRowNum() **********/
601 UWORD
wxTable::GetRowNum(void)
605 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
607 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
611 // Completed successfully
612 return((UWORD
) rowNum
);
614 } // wxTable::GetRowNum()
616 /********** wxTable::bindInsertParams() **********/
617 bool wxTable::bindInsertParams(void)
624 UDWORD precision
= 0;
627 // Bind each column (that can be inserted) of the table to a parameter marker
629 for (i
= 0; i
< noCols
; i
++)
631 if (! colDefs
[i
].InsertAllowed
)
633 switch(colDefs
[i
].DbDataType
)
635 case DB_DATA_TYPE_VARCHAR
:
636 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
637 precision
= colDefs
[i
].SzDataObj
;
639 colDefs
[i
].CbValue
= SQL_NTS
;
641 case DB_DATA_TYPE_INTEGER
:
642 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
643 precision
= pDb
->typeInfInteger
.Precision
;
645 colDefs
[i
].CbValue
= 0;
647 case DB_DATA_TYPE_FLOAT
:
648 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
649 precision
= pDb
->typeInfFloat
.Precision
;
650 scale
= pDb
->typeInfFloat
.MaximumScale
;
651 // SQL Sybase Anywhere v5.5 returned a negative number for the
652 // MaxScale. This caused ODBC to kick out an error on ibscale.
653 // I check for this here and set the scale = precision.
655 // scale = (short) precision;
656 colDefs
[i
].CbValue
= 0;
658 case DB_DATA_TYPE_DATE
:
659 fSqlType
= pDb
->typeInfDate
.FsqlType
;
660 precision
= pDb
->typeInfDate
.Precision
;
662 colDefs
[i
].CbValue
= 0;
668 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
669 colDefs
[i
].Null
= FALSE
;
671 if (SQLBindParameter(hstmtInsert
, i
+1, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
672 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
673 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
674 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
677 // Completed successfully
680 } // wxTable::bindInsertParams()
682 /********** wxTable::bindUpdateParams() **********/
683 bool wxTable::bindUpdateParams(void)
690 UDWORD precision
= 0;
693 // Bind each UPDATEABLE column of the table to a parameter marker
695 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
697 if (! colDefs
[i
].Updateable
)
699 switch(colDefs
[i
].DbDataType
)
701 case DB_DATA_TYPE_VARCHAR
:
702 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
703 precision
= colDefs
[i
].SzDataObj
;
705 colDefs
[i
].CbValue
= SQL_NTS
;
707 case DB_DATA_TYPE_INTEGER
:
708 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
709 precision
= pDb
->typeInfInteger
.Precision
;
711 colDefs
[i
].CbValue
= 0;
713 case DB_DATA_TYPE_FLOAT
:
714 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
715 precision
= pDb
->typeInfFloat
.Precision
;
716 scale
= pDb
->typeInfFloat
.MaximumScale
;
717 // SQL Sybase Anywhere v5.5 returned a negative number for the
718 // MaxScale. This caused ODBC to kick out an error on ibscale.
719 // I check for this here and set the scale = precision.
721 // scale = (short) precision;
722 colDefs
[i
].CbValue
= 0;
724 case DB_DATA_TYPE_DATE
:
725 fSqlType
= pDb
->typeInfDate
.FsqlType
;
726 precision
= pDb
->typeInfDate
.Precision
;
728 colDefs
[i
].CbValue
= 0;
731 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
732 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
733 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
734 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
737 // Completed successfully
740 } // wxTable::bindUpdateParams()
742 /********** wxTable::bindCols() **********/
743 bool wxTable::bindCols(HSTMT cursor
)
747 // Bind each column of the table to a memory address for fetching data
749 for (i
= 0; i
< noCols
; i
++)
751 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
752 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
753 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
756 // Completed successfully
759 } // wxTable::bindCols()
761 /********** wxTable::CloseCursor() **********/
762 bool wxTable::CloseCursor(HSTMT cursor
)
764 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
765 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
767 // Completed successfully
770 } // wxTable::CloseCursor()
772 /********** wxTable::CreateTable() **********/
773 bool wxTable::CreateTable(bool attemptDrop
)
779 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
781 #ifdef DBDEBUG_CONSOLE
782 cout
<< "Creating Table " << tableName
<< "..." << endl
;
786 if (attemptDrop
&& !DropTable())
790 #ifdef DBDEBUG_CONSOLE
791 for (i
= 0; i
< noCols
; i
++)
793 // Exclude derived columns since they are NOT part of the base table
794 if (colDefs
[i
].DerivedCol
)
796 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
797 switch(colDefs
[i
].DbDataType
)
799 case DB_DATA_TYPE_VARCHAR
:
800 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
802 case DB_DATA_TYPE_INTEGER
:
803 cout
<< pDb
->typeInfInteger
.TypeName
;
805 case DB_DATA_TYPE_FLOAT
:
806 cout
<< pDb
->typeInfFloat
.TypeName
;
808 case DB_DATA_TYPE_DATE
:
809 cout
<< pDb
->typeInfDate
.TypeName
;
816 // Build a CREATE TABLE string from the colDefs structure.
817 bool needComma
= FALSE
;
818 sprintf(sqlStmt
, "CREATE TABLE %s (", tableName
);
819 for (i
= 0; i
< noCols
; i
++)
821 // Exclude derived columns since they are NOT part of the base table
822 if (colDefs
[i
].DerivedCol
)
826 strcat(sqlStmt
, ",");
828 strcat(sqlStmt
, colDefs
[i
].ColName
);
829 strcat(sqlStmt
, " ");
831 switch(colDefs
[i
].DbDataType
)
833 case DB_DATA_TYPE_VARCHAR
:
834 strcat(sqlStmt
, pDb
->typeInfVarchar
.TypeName
); break;
835 case DB_DATA_TYPE_INTEGER
:
836 strcat(sqlStmt
, pDb
->typeInfInteger
.TypeName
); break;
837 case DB_DATA_TYPE_FLOAT
:
838 strcat(sqlStmt
, pDb
->typeInfFloat
.TypeName
); break;
839 case DB_DATA_TYPE_DATE
:
840 strcat(sqlStmt
, pDb
->typeInfDate
.TypeName
); break;
842 // For varchars, append the size of the string
843 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
846 // strcat(sqlStmt, "(");
847 // strcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
848 // strcat(sqlStmt, ")");
849 sprintf(s
, "(%d)", colDefs
[i
].SzDataObj
);
853 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
855 if (colDefs
[i
].KeyField
)
857 strcat(sqlStmt
, " NOT NULL");
863 // If there is a primary key defined, include it in the create statement
864 for (i
= j
= 0; i
< noCols
; i
++)
866 if (colDefs
[i
].KeyField
)
872 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
874 if (pDb
->Dbms() != dbmsMY_SQL
)
876 strcat(sqlStmt
, ",CONSTRAINT ");
877 strcat(sqlStmt
, tableName
);
878 strcat(sqlStmt
, "_PIDX PRIMARY KEY (");
882 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
883 strcat(sqlStmt
, ", PRIMARY KEY (");
886 // List column name(s) of column(s) comprising the primary key
887 for (i
= j
= 0; i
< noCols
; i
++)
889 if (colDefs
[i
].KeyField
)
891 if (j
++) // Multi part key, comma separate names
892 strcat(sqlStmt
, ",");
893 strcat(sqlStmt
, colDefs
[i
].ColName
);
896 strcat(sqlStmt
, ")");
898 // Append the closing parentheses for the create table statement
899 strcat(sqlStmt
, ")");
901 pDb
->WriteSqlLog(sqlStmt
);
903 #ifdef DBDEBUG_CONSOLE
904 cout
<< endl
<< sqlStmt
<< endl
;
907 // Execute the CREATE TABLE statement
908 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
910 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
911 pDb
->RollbackTrans();
916 // Commit the transaction and close the cursor
917 if (! pDb
->CommitTrans())
919 if (! CloseCursor(hstmt
))
922 // Database table created successfully
925 } // wxTable::CreateTable()
927 /********** wxTable::DropTable() **********/
928 bool wxTable::DropTable()
930 // NOTE: This function returns TRUE if the Table does not exist, but
931 // only for identified databases. Code will need to be added
932 // below for any other databases when those databases are defined
933 // to handle this situation consistently
935 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
937 sprintf(sqlStmt
, "DROP TABLE %s", tableName
);
939 pDb
->WriteSqlLog(sqlStmt
);
941 #ifdef DBDEBUG_CONSOLE
942 cout
<< endl
<< sqlStmt
<< endl
;
945 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
947 // Check for "Base table not found" error and ignore
948 pDb
->GetNextError(henv
, hdbc
, hstmt
);
949 if (wxStrcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
951 // Check for product specific error codes
952 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
953 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"S1000")) || // untested
954 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01")))) // untested
956 pDb
->DispNextError();
957 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
958 pDb
->RollbackTrans();
965 // Commit the transaction and close the cursor
966 if (! pDb
->CommitTrans())
968 if (! CloseCursor(hstmt
))
972 } // wxTable::DropTable()
974 /********** wxTable::CreateIndex() **********/
975 bool wxTable::CreateIndex(char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
977 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
979 // Drop the index first
980 if (attemptDrop
&& !DropIndex(idxName
))
983 // Build a CREATE INDEX statement
984 strcpy(sqlStmt
, "CREATE ");
986 strcat(sqlStmt
, "UNIQUE ");
988 strcat(sqlStmt
, "INDEX ");
989 strcat(sqlStmt
, idxName
);
990 strcat(sqlStmt
, " ON ");
991 strcat(sqlStmt
, tableName
);
992 strcat(sqlStmt
, " (");
994 // Append list of columns making up index
996 for (i
= 0; i
< noIdxCols
; i
++)
998 strcat(sqlStmt
, pIdxDefs
[i
].ColName
);
999 /* Postgres doesn't cope with ASC */
1000 if (pDb
->Dbms() != dbmsPOSTGRES
)
1002 if (pIdxDefs
[i
].Ascending
)
1003 strcat(sqlStmt
, " ASC");
1005 strcat(sqlStmt
, " DESC");
1008 if ((i
+ 1) < noIdxCols
)
1009 strcat(sqlStmt
, ",");
1012 // Append closing parentheses
1013 strcat(sqlStmt
, ")");
1015 pDb
->WriteSqlLog(sqlStmt
);
1017 #ifdef DBDEBUG_CONSOLE
1018 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1021 // Execute the CREATE INDEX statement
1022 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1024 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1025 pDb
->RollbackTrans();
1030 // Commit the transaction and close the cursor
1031 if (! pDb
->CommitTrans())
1033 if (! CloseCursor(hstmt
))
1036 // Index Created Successfully
1039 } // wxTable::CreateIndex()
1041 /********** wxTable::DropIndex() **********/
1042 bool wxTable::DropIndex(char * idxName
)
1044 // NOTE: This function returns TRUE if the Index does not exist, but
1045 // only for identified databases. Code will need to be added
1046 // below for any other databases when those databases are defined
1047 // to handle this situation consistently
1049 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1051 if (pDb
->Dbms() == dbmsACCESS
)
1052 sprintf(sqlStmt
, "DROP INDEX %s ON %s",idxName
,tableName
);
1053 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1054 sprintf(sqlStmt
, "DROP INDEX %s.%s",tableName
,idxName
);
1056 sprintf(sqlStmt
, "DROP INDEX %s",idxName
);
1058 pDb
->WriteSqlLog(sqlStmt
);
1060 #ifdef DBDEBUG_CONSOLE
1061 cout
<< endl
<< sqlStmt
<< endl
;
1064 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1066 // Check for "Index not found" error and ignore
1067 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1068 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1070 // Check for product specific error codes
1071 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1072 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1073 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S02")) // untested
1076 pDb
->DispNextError();
1077 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1078 pDb
->RollbackTrans();
1085 // Commit the transaction and close the cursor
1086 if (! pDb
->CommitTrans())
1088 if (! CloseCursor(hstmt
))
1092 } // wxTable::DropIndex()
1094 /********** wxTable::Insert() **********/
1095 int wxTable::Insert(void)
1103 // Insert the record by executing the already prepared insert statement
1105 retcode
=SQLExecute(hstmtInsert
);
1106 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1108 // Check to see if integrity constraint was violated
1109 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1110 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1111 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1114 pDb
->DispNextError();
1115 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1120 // Record inserted into the datasource successfully
1123 } // wxTable::Insert()
1125 /********** wxTable::Update(pSqlStmt) **********/
1126 bool wxTable::Update(char *pSqlStmt
)
1132 pDb
->WriteSqlLog(pSqlStmt
);
1134 return(execUpdate(pSqlStmt
));
1136 } // wxTable::Update(pSqlStmt)
1138 /********** wxTable::Update() **********/
1139 bool wxTable::Update(void)
1145 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1147 // Build the SQL UPDATE statement
1148 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1150 pDb
->WriteSqlLog(sqlStmt
);
1152 #ifdef DBDEBUG_CONSOLE
1153 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1156 // Execute the SQL UPDATE statement
1157 return(execUpdate(sqlStmt
));
1159 } // wxTable::Update()
1161 /********** wxTable::UpdateWhere() **********/
1162 bool wxTable::UpdateWhere(char *pWhereClause
)
1168 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1170 // Build the SQL UPDATE statement
1171 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1173 pDb
->WriteSqlLog(sqlStmt
);
1175 #ifdef DBDEBUG_CONSOLE
1176 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1179 // Execute the SQL UPDATE statement
1180 return(execUpdate(sqlStmt
));
1182 } // wxTable::UpdateWhere()
1184 /********** wxTable::Delete() **********/
1185 bool wxTable::Delete(void)
1191 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1193 // Build the SQL DELETE statement
1194 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1196 pDb
->WriteSqlLog(sqlStmt
);
1198 // Execute the SQL DELETE statement
1199 return(execDelete(sqlStmt
));
1201 } // wxTable::Delete()
1203 /********** wxTable::DeleteWhere() **********/
1204 bool wxTable::DeleteWhere(char *pWhereClause
)
1210 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1212 // Build the SQL DELETE statement
1213 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1215 pDb
->WriteSqlLog(sqlStmt
);
1217 // Execute the SQL DELETE statement
1218 return(execDelete(sqlStmt
));
1220 } // wxTable::DeleteWhere()
1222 /********** wxTable::DeleteMatching() **********/
1223 bool wxTable::DeleteMatching(void)
1229 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1231 // Build the SQL DELETE statement
1232 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1234 pDb
->WriteSqlLog(sqlStmt
);
1236 // Execute the SQL DELETE statement
1237 return(execDelete(sqlStmt
));
1239 } // wxTable::DeleteMatching()
1241 /********** wxTable::execDelete() **********/
1242 bool wxTable::execDelete(char *pSqlStmt
)
1244 // Execute the DELETE statement
1245 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1246 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1248 // Record deleted successfully
1251 } // wxTable::execDelete()
1253 /********** wxTable::execUpdate() **********/
1254 bool wxTable::execUpdate(char *pSqlStmt
)
1256 // Execute the UPDATE statement
1257 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1258 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1260 // Record deleted successfully
1263 } // wxTable::execUpdate()
1265 /********** wxTable::GetUpdateStmt() **********/
1266 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, char *pWhereClause
)
1272 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1273 bool firstColumn
= TRUE
;
1276 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1278 // Append a list of columns to be updated
1280 for (i
= 0; i
< noCols
; i
++)
1282 // Only append Updateable columns
1283 if (colDefs
[i
].Updateable
)
1286 strcat(pSqlStmt
, ",");
1288 firstColumn
= FALSE
;
1289 strcat(pSqlStmt
, colDefs
[i
].ColName
);
1290 strcat(pSqlStmt
, " = ?");
1294 // Append the WHERE clause to the SQL UPDATE statement
1295 strcat(pSqlStmt
, " WHERE ");
1298 case DB_UPD_KEYFIELDS
:
1299 // If the datasource supports the ROWID column, build
1300 // the where on ROWID for efficiency purposes.
1301 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1302 if (CanUpdByROWID())
1305 char rowid
[ROWID_LEN
];
1307 // Get the ROWID value. If not successful retreiving the ROWID,
1308 // simply fall down through the code and build the WHERE clause
1309 // based on the key fields.
1310 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1312 strcat(pSqlStmt
, "ROWID = '");
1313 strcat(pSqlStmt
, rowid
);
1314 strcat(pSqlStmt
, "'");
1318 // Unable to delete by ROWID, so build a WHERE
1319 // clause based on the keyfields.
1320 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1321 strcat(pSqlStmt
, whereClause
);
1324 strcat(pSqlStmt
, pWhereClause
);
1328 } // GetUpdateStmt()
1330 /********** wxTable::GetDeleteStmt() **********/
1331 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, char *pWhereClause
)
1337 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1341 // Handle the case of DeleteWhere() and the where clause is blank. It should
1342 // delete all records from the database in this case.
1343 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || strlen(pWhereClause
) == 0))
1345 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1349 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1351 // Append the WHERE clause to the SQL DELETE statement
1354 case DB_DEL_KEYFIELDS
:
1355 // If the datasource supports the ROWID column, build
1356 // the where on ROWID for efficiency purposes.
1357 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1358 if (CanUpdByROWID())
1361 char rowid
[ROWID_LEN
];
1363 // Get the ROWID value. If not successful retreiving the ROWID,
1364 // simply fall down through the code and build the WHERE clause
1365 // based on the key fields.
1366 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1368 strcat(pSqlStmt
, "ROWID = '");
1369 strcat(pSqlStmt
, rowid
);
1370 strcat(pSqlStmt
, "'");
1374 // Unable to delete by ROWID, so build a WHERE
1375 // clause based on the keyfields.
1376 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1377 strcat(pSqlStmt
, whereClause
);
1380 strcat(pSqlStmt
, pWhereClause
);
1382 case DB_DEL_MATCHING
:
1383 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1384 strcat(pSqlStmt
, whereClause
);
1388 } // GetDeleteStmt()
1390 /********** wxTable::GetWhereClause() **********/
1392 * Note: GetWhereClause() currently ignores timestamp columns.
1393 * They are not included as part of the where clause.
1396 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, char *qualTableName
)
1398 bool moreThanOneColumn
= FALSE
;
1401 // Loop through the columns building a where clause as you go
1403 for (i
= 0; i
< noCols
; i
++)
1405 // Determine if this column should be included in the WHERE clause
1406 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1407 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1409 // Skip over timestamp columns
1410 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1412 // If there is more than 1 column, join them with the keyword "AND"
1413 if (moreThanOneColumn
)
1414 strcat(pWhereClause
, " AND ");
1416 moreThanOneColumn
= TRUE
;
1417 // Concatenate where phrase for the column
1418 if (qualTableName
&& strlen(qualTableName
))
1420 strcat(pWhereClause
, qualTableName
);
1421 strcat(pWhereClause
, ".");
1423 strcat(pWhereClause
, colDefs
[i
].ColName
);
1424 strcat(pWhereClause
, " = ");
1425 switch(colDefs
[i
].SqlCtype
)
1428 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1431 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1434 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1437 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1440 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1443 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1446 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1449 strcat(pWhereClause
, colValue
);
1453 } // wxTable::GetWhereClause()
1455 /********** wxTable::IsColNull() **********/
1456 bool wxTable::IsColNull(int colNo
)
1458 switch(colDefs
[colNo
].SqlCtype
)
1461 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1463 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1465 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1467 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1469 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1471 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1473 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1474 case SQL_C_TIMESTAMP
:
1475 TIMESTAMP_STRUCT
*pDt
;
1476 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1477 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1485 } // wxTable::IsColNull()
1487 /********** wxTable::CanSelectForUpdate() **********/
1488 bool wxTable::CanSelectForUpdate(void)
1490 if (pDb
->Dbms() == dbmsMY_SQL
)
1493 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1498 } // wxTable::CanSelectForUpdate()
1500 /********** wxTable::CanUpdByROWID() **********/
1501 bool wxTable::CanUpdByROWID(void)
1504 //NOTE: Returning FALSE for now until this can be debugged,
1505 // as the ROWID is not getting updated correctly
1508 if (pDb
->Dbms() == dbmsORACLE
)
1513 } // wxTable::CanUpdByROWID()
1515 /********** wxTable::IsCursorClosedOnCommit() **********/
1516 bool wxTable::IsCursorClosedOnCommit(void)
1518 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1523 } // wxTable::IsCursorClosedOnCommit()
1525 /********** wxTable::ClearMemberVars() **********/
1526 void wxTable::ClearMemberVars(void)
1528 // Loop through the columns setting each member variable to zero
1530 for (i
= 0; i
< noCols
; i
++)
1532 switch(colDefs
[i
].SqlCtype
)
1535 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1538 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1541 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1544 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1547 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1550 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1553 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1555 case SQL_C_TIMESTAMP
:
1556 TIMESTAMP_STRUCT
*pDt
;
1557 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1569 } // wxTable::ClearMemberVars()
1571 /********** wxTable::SetQueryTimeout() **********/
1572 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1574 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1575 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1576 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1577 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1578 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1579 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1580 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1581 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1583 // Completed Successfully
1586 } // wxTable::SetQueryTimeout()
1588 /********** wxTable::SetColDefs() **********/
1589 void wxTable::SetColDefs (int index
, char *fieldName
, int dataType
, void *pData
,
1590 int cType
, int size
, bool keyField
, bool upd
,
1591 bool insAllow
, bool derivedCol
)
1593 if (!colDefs
) // May happen if the database connection fails
1596 if (strlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1598 strncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1599 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1602 strcpy(colDefs
[index
].ColName
, fieldName
);
1604 colDefs
[index
].DbDataType
= dataType
;
1605 colDefs
[index
].PtrDataObj
= pData
;
1606 colDefs
[index
].SqlCtype
= cType
;
1607 colDefs
[index
].SzDataObj
= size
;
1608 colDefs
[index
].KeyField
= keyField
;
1609 colDefs
[index
].DerivedCol
= derivedCol
;
1610 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1613 colDefs
[index
].Updateable
= FALSE
;
1614 colDefs
[index
].InsertAllowed
= FALSE
;
1618 colDefs
[index
].Updateable
= upd
;
1619 colDefs
[index
].InsertAllowed
= insAllow
;
1622 colDefs
[index
].Null
= FALSE
;
1624 } // wxTable::SetColDefs()
1626 /********** wxTable::SetCursor() **********/
1627 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1629 if (hstmtActivate
== DEFAULT_CURSOR
)
1630 hstmt
= *hstmtDefault
;
1632 hstmt
= *hstmtActivate
;
1634 } // wxTable::SetCursor()
1636 /********** wxTable::Count() **********/
1637 ULONG
wxTable::Count(void)
1640 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1643 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1644 strcpy(sqlStmt
, "SELECT COUNT(*) FROM ");
1645 strcat(sqlStmt
, queryTableName
);
1647 if (from
&& strlen(from
))
1648 strcat(sqlStmt
, from
);
1650 // Add the where clause if one is provided
1651 if (where
&& strlen(where
))
1653 strcat(sqlStmt
, " WHERE ");
1654 strcat(sqlStmt
, where
);
1657 pDb
->WriteSqlLog(sqlStmt
);
1659 // Initialize the Count cursor if it's not already initialized
1662 hstmtCount
= NewCursor(FALSE
,FALSE
);
1668 // Execute the SQL statement
1669 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1671 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1676 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1678 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1682 // Obtain the result
1683 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1685 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1690 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1691 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1693 // Return the record count
1696 } // wxTable::Count()
1698 /********** wxTable::Refresh() **********/
1699 bool wxTable::Refresh(void)
1703 // Switch to the internal cursor so any active cursors are not corrupted
1704 HSTMT currCursor
= GetCursor();
1705 hstmt
= hstmtInternal
;
1707 // Save the where and order by clauses
1708 char *saveWhere
= where
;
1709 char *saveOrderBy
= orderBy
;
1711 // Build a where clause to refetch the record with. Try and use the
1712 // ROWID if it's available, ow use the key fields.
1713 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1714 strcpy(whereClause
, "");
1715 if (CanUpdByROWID())
1718 char rowid
[ROWID_LEN
+1];
1720 // Get the ROWID value. If not successful retreiving the ROWID,
1721 // simply fall down through the code and build the WHERE clause
1722 // based on the key fields.
1723 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1725 strcat(whereClause
, queryTableName
);
1726 strcat(whereClause
, ".ROWID = '");
1727 strcat(whereClause
, rowid
);
1728 strcat(whereClause
, "'");
1732 // If unable to use the ROWID, build a where clause from the keyfields
1733 if (strlen(whereClause
) == 0)
1734 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1736 // Requery the record
1737 where
= whereClause
;
1742 if (result
&& !GetNext())
1745 // Switch back to original cursor
1746 SetCursor(&currCursor
);
1748 // Free the internal cursor
1749 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1750 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1752 // Restore the original where and order by clauses
1754 orderBy
= saveOrderBy
;
1758 } // wxTable::Refresh()
1760 /********** wxTable::SetNull(UINT colNo) **********/
1761 bool wxTable::SetNull(int colNo
)
1764 return(colDefs
[colNo
].Null
= TRUE
);
1768 } // wxTable::SetNull(UINT colNo)
1770 /********** wxTable::SetNull(char *colName) **********/
1771 bool wxTable::SetNull(char *colName
)
1774 for (i
= 0; i
< noCols
; i
++)
1776 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
1781 return(colDefs
[i
].Null
= TRUE
);
1785 } // wxTable::SetNull(char *colName)
1787 /********** wxTable::NewCursor() **********/
1788 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
1790 HSTMT
*newHSTMT
= new HSTMT
;
1795 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
1797 pDb
->DispAllErrors(henv
, hdbc
);
1802 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
1804 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
1811 if(!bindCols(*newHSTMT
))
1819 SetCursor(newHSTMT
);
1823 } // wxTable::NewCursor()
1825 /********** wxTable::DeleteCursor() **********/
1826 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
1830 if (!hstmtDel
) // Cursor already deleted
1833 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
1835 pDb
->DispAllErrors(henv
, hdbc
);
1843 } // wxTable::DeleteCursor()
1845 #endif // wxUSE_ODBC