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
43 # include <iostream.h>
50 #if wxMAJOR_VERSION == 2
56 #if wxMAJOR_VERSION == 1
57 # if defined(wx_msw) || defined(wx_x)
73 #if wxMAJOR_VERSION == 1
75 #elif wxMAJOR_VERSION == 2
76 #include "wx/dbtable.h"
80 #define stricmp _stricmp
81 #define strnicmp _strnicmp
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 (! strcmp(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 #ifndef FWD_ONLY_CURSORS
570 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
573 if ((retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
)) != SQL_SUCCESS
)
574 if (retcode
== SQL_NO_DATA_FOUND
)
577 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
579 // Fetch the next record from the record set
581 retcode
= SQLFetch(hstmt
);
582 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
584 if (retcode
== SQL_NO_DATA_FOUND
)
587 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
591 // Completed successfully
594 } // wxTable::getRec()
596 /********** wxTable::GetRowNum() **********/
597 UWORD
wxTable::GetRowNum(void)
601 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
603 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
607 // Completed successfully
608 return((UWORD
) rowNum
);
610 } // wxTable::GetRowNum()
612 /********** wxTable::bindInsertParams() **********/
613 bool wxTable::bindInsertParams(void)
620 UDWORD precision
= 0;
623 // Bind each column (that can be inserted) of the table to a parameter marker
625 for (i
= 0; i
< noCols
; i
++)
627 if (! colDefs
[i
].InsertAllowed
)
629 switch(colDefs
[i
].DbDataType
)
631 case DB_DATA_TYPE_VARCHAR
:
632 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
633 precision
= colDefs
[i
].SzDataObj
;
635 colDefs
[i
].CbValue
= SQL_NTS
;
637 case DB_DATA_TYPE_INTEGER
:
638 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
639 precision
= pDb
->typeInfInteger
.Precision
;
641 colDefs
[i
].CbValue
= 0;
643 case DB_DATA_TYPE_FLOAT
:
644 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
645 precision
= pDb
->typeInfFloat
.Precision
;
646 scale
= pDb
->typeInfFloat
.MaximumScale
;
647 // SQL Sybase Anywhere v5.5 returned a negative number for the
648 // MaxScale. This caused ODBC to kick out an error on ibscale.
649 // I check for this here and set the scale = precision.
651 // scale = (short) precision;
652 colDefs
[i
].CbValue
= 0;
654 case DB_DATA_TYPE_DATE
:
655 fSqlType
= pDb
->typeInfDate
.FsqlType
;
656 precision
= pDb
->typeInfDate
.Precision
;
658 colDefs
[i
].CbValue
= 0;
664 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
665 colDefs
[i
].Null
= FALSE
;
667 if (SQLBindParameter(hstmtInsert
, i
+1, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
668 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
669 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
670 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
673 // Completed successfully
676 } // wxTable::bindInsertParams()
678 /********** wxTable::bindUpdateParams() **********/
679 bool wxTable::bindUpdateParams(void)
686 UDWORD precision
= 0;
689 // Bind each UPDATEABLE column of the table to a parameter marker
691 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
693 if (! colDefs
[i
].Updateable
)
695 switch(colDefs
[i
].DbDataType
)
697 case DB_DATA_TYPE_VARCHAR
:
698 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
699 precision
= colDefs
[i
].SzDataObj
;
701 colDefs
[i
].CbValue
= SQL_NTS
;
703 case DB_DATA_TYPE_INTEGER
:
704 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
705 precision
= pDb
->typeInfInteger
.Precision
;
707 colDefs
[i
].CbValue
= 0;
709 case DB_DATA_TYPE_FLOAT
:
710 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
711 precision
= pDb
->typeInfFloat
.Precision
;
712 scale
= pDb
->typeInfFloat
.MaximumScale
;
713 // SQL Sybase Anywhere v5.5 returned a negative number for the
714 // MaxScale. This caused ODBC to kick out an error on ibscale.
715 // I check for this here and set the scale = precision.
717 // scale = (short) precision;
718 colDefs
[i
].CbValue
= 0;
720 case DB_DATA_TYPE_DATE
:
721 fSqlType
= pDb
->typeInfDate
.FsqlType
;
722 precision
= pDb
->typeInfDate
.Precision
;
724 colDefs
[i
].CbValue
= 0;
727 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
728 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
729 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
730 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
733 // Completed successfully
736 } // wxTable::bindUpdateParams()
738 /********** wxTable::bindCols() **********/
739 bool wxTable::bindCols(HSTMT cursor
)
743 // Bind each column of the table to a memory address for fetching data
745 for (i
= 0; i
< noCols
; i
++)
747 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
748 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
749 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
752 // Completed successfully
755 } // wxTable::bindCols()
757 /********** wxTable::CloseCursor() **********/
758 bool wxTable::CloseCursor(HSTMT cursor
)
760 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
761 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
763 // Completed successfully
766 } // wxTable::CloseCursor()
768 /********** wxTable::CreateTable() **********/
769 bool wxTable::CreateTable(bool attemptDrop
)
775 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
777 #ifdef DBDEBUG_CONSOLE
778 cout
<< "Creating Table " << tableName
<< "..." << endl
;
782 if (attemptDrop
&& !DropTable())
786 #ifdef DBDEBUG_CONSOLE
787 for (i
= 0; i
< noCols
; i
++)
789 // Exclude derived columns since they are NOT part of the base table
790 if (colDefs
[i
].DerivedCol
)
792 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
793 switch(colDefs
[i
].DbDataType
)
795 case DB_DATA_TYPE_VARCHAR
:
796 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
798 case DB_DATA_TYPE_INTEGER
:
799 cout
<< pDb
->typeInfInteger
.TypeName
;
801 case DB_DATA_TYPE_FLOAT
:
802 cout
<< pDb
->typeInfFloat
.TypeName
;
804 case DB_DATA_TYPE_DATE
:
805 cout
<< pDb
->typeInfDate
.TypeName
;
812 // Build a CREATE TABLE string from the colDefs structure.
813 bool needComma
= FALSE
;
814 sprintf(sqlStmt
, "CREATE TABLE %s (", tableName
);
815 for (i
= 0; i
< noCols
; i
++)
817 // Exclude derived columns since they are NOT part of the base table
818 if (colDefs
[i
].DerivedCol
)
822 strcat(sqlStmt
, ",");
824 strcat(sqlStmt
, colDefs
[i
].ColName
);
825 strcat(sqlStmt
, " ");
827 switch(colDefs
[i
].DbDataType
)
829 case DB_DATA_TYPE_VARCHAR
:
830 strcat(sqlStmt
, pDb
->typeInfVarchar
.TypeName
); break;
831 case DB_DATA_TYPE_INTEGER
:
832 strcat(sqlStmt
, pDb
->typeInfInteger
.TypeName
); break;
833 case DB_DATA_TYPE_FLOAT
:
834 strcat(sqlStmt
, pDb
->typeInfFloat
.TypeName
); break;
835 case DB_DATA_TYPE_DATE
:
836 strcat(sqlStmt
, pDb
->typeInfDate
.TypeName
); break;
838 // For varchars, append the size of the string
839 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
842 // strcat(sqlStmt, "(");
843 // strcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
844 // strcat(sqlStmt, ")");
845 sprintf(s
, "(%d)", colDefs
[i
].SzDataObj
);
849 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
851 if (colDefs
[i
].KeyField
)
853 strcat(sqlStmt
, " NOT NULL");
859 // If there is a primary key defined, include it in the create statement
860 for (i
= j
= 0; i
< noCols
; i
++)
862 if (colDefs
[i
].KeyField
)
868 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
870 if (pDb
->Dbms() != dbmsMY_SQL
)
872 strcat(sqlStmt
, ",CONSTRAINT ");
873 strcat(sqlStmt
, tableName
);
874 strcat(sqlStmt
, "_PIDX PRIMARY KEY (");
878 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
879 strcat(sqlStmt
, ", PRIMARY KEY (");
882 // List column name(s) of column(s) comprising the primary key
883 for (i
= j
= 0; i
< noCols
; i
++)
885 if (colDefs
[i
].KeyField
)
887 if (j
++) // Multi part key, comma separate names
888 strcat(sqlStmt
, ",");
889 strcat(sqlStmt
, colDefs
[i
].ColName
);
892 strcat(sqlStmt
, ")");
894 // Append the closing parentheses for the create table statement
895 strcat(sqlStmt
, ")");
897 pDb
->WriteSqlLog(sqlStmt
);
899 #ifdef DBDEBUG_CONSOLE
900 cout
<< endl
<< sqlStmt
<< endl
;
903 // Execute the CREATE TABLE statement
904 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
906 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
907 pDb
->RollbackTrans();
912 // Commit the transaction and close the cursor
913 if (! pDb
->CommitTrans())
915 if (! CloseCursor(hstmt
))
918 // Database table created successfully
921 } // wxTable::CreateTable()
923 /********** wxTable::DropTable() **********/
924 bool wxTable::DropTable()
926 // NOTE: This function returns TRUE if the Table does not exist, but
927 // only for identified databases. Code will need to be added
928 // below for any other databases when those databases are defined
929 // to handle this situation consistently
931 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
933 sprintf(sqlStmt
, "DROP TABLE %s", tableName
);
935 pDb
->WriteSqlLog(sqlStmt
);
937 #ifdef DBDEBUG_CONSOLE
938 cout
<< endl
<< sqlStmt
<< endl
;
941 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
943 // Check for "Base table not found" error and ignore
944 pDb
->GetNextError(henv
, hdbc
, hstmt
);
945 if (strcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
947 // Check for product specific error codes
948 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !strcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
949 (pDb
->Dbms() == dbmsMY_SQL
&& !strcmp(pDb
->sqlState
,"S1000")) || // untested
950 (pDb
->Dbms() == dbmsPOSTGRES
&& !strcmp(pDb
->sqlState
,"08S01")))) // untested
952 pDb
->DispNextError();
953 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
954 pDb
->RollbackTrans();
961 // Commit the transaction and close the cursor
962 if (! pDb
->CommitTrans())
964 if (! CloseCursor(hstmt
))
968 } // wxTable::DropTable()
970 /********** wxTable::CreateIndex() **********/
971 bool wxTable::CreateIndex(char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
973 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
975 // Drop the index first
976 if (attemptDrop
&& !DropIndex(idxName
))
979 // Build a CREATE INDEX statement
980 strcpy(sqlStmt
, "CREATE ");
982 strcat(sqlStmt
, "UNIQUE ");
984 strcat(sqlStmt
, "INDEX ");
985 strcat(sqlStmt
, idxName
);
986 strcat(sqlStmt
, " ON ");
987 strcat(sqlStmt
, tableName
);
988 strcat(sqlStmt
, " (");
990 // Append list of columns making up index
992 for (i
= 0; i
< noIdxCols
; i
++)
994 strcat(sqlStmt
, pIdxDefs
[i
].ColName
);
995 /* Postgres doesn't cope with ASC */
996 if (pDb
->Dbms() != dbmsPOSTGRES
)
998 if (pIdxDefs
[i
].Ascending
)
999 strcat(sqlStmt
, " ASC");
1001 strcat(sqlStmt
, " DESC");
1004 if ((i
+ 1) < noIdxCols
)
1005 strcat(sqlStmt
, ",");
1008 // Append closing parentheses
1009 strcat(sqlStmt
, ")");
1011 pDb
->WriteSqlLog(sqlStmt
);
1013 #ifdef DBDEBUG_CONSOLE
1014 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1017 // Execute the CREATE INDEX statement
1018 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1020 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1021 pDb
->RollbackTrans();
1026 // Commit the transaction and close the cursor
1027 if (! pDb
->CommitTrans())
1029 if (! CloseCursor(hstmt
))
1032 // Index Created Successfully
1035 } // wxTable::CreateIndex()
1037 /********** wxTable::DropIndex() **********/
1038 bool wxTable::DropIndex(char * idxName
)
1040 // NOTE: This function returns TRUE if the Index 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 if (pDb
->Dbms() == dbmsACCESS
)
1048 sprintf(sqlStmt
, "DROP INDEX %s ON %s",idxName
,tableName
);
1049 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1050 sprintf(sqlStmt
, "DROP INDEX %s.%s",tableName
,idxName
);
1052 sprintf(sqlStmt
, "DROP INDEX %s",idxName
);
1054 pDb
->WriteSqlLog(sqlStmt
);
1056 #ifdef DBDEBUG_CONSOLE
1057 cout
<< endl
<< sqlStmt
<< endl
;
1060 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1062 // Check for "Index not found" error and ignore
1063 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1064 if (strcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1066 // Check for product specific error codes
1067 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !strcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1068 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !strcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1069 (pDb
->Dbms() == dbmsMY_SQL
&& !strcmp(pDb
->sqlState
,"42S02")) // untested
1072 pDb
->DispNextError();
1073 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1074 pDb
->RollbackTrans();
1081 // Commit the transaction and close the cursor
1082 if (! pDb
->CommitTrans())
1084 if (! CloseCursor(hstmt
))
1088 } // wxTable::DropIndex()
1090 /********** wxTable::Insert() **********/
1091 int wxTable::Insert(void)
1099 // Insert the record by executing the already prepared insert statement
1101 retcode
=SQLExecute(hstmtInsert
);
1102 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1104 // Check to see if integrity constraint was violated
1105 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1106 if (! strcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1107 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1110 pDb
->DispNextError();
1111 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1116 // Record inserted into the datasource successfully
1119 } // wxTable::Insert()
1121 /********** wxTable::Update(pSqlStmt) **********/
1122 bool wxTable::Update(char *pSqlStmt
)
1128 pDb
->WriteSqlLog(pSqlStmt
);
1130 return(execUpdate(pSqlStmt
));
1132 } // wxTable::Update(pSqlStmt)
1134 /********** wxTable::Update() **********/
1135 bool wxTable::Update(void)
1141 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1143 // Build the SQL UPDATE statement
1144 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1146 pDb
->WriteSqlLog(sqlStmt
);
1148 #ifdef DBDEBUG_CONSOLE
1149 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1152 // Execute the SQL UPDATE statement
1153 return(execUpdate(sqlStmt
));
1155 } // wxTable::Update()
1157 /********** wxTable::UpdateWhere() **********/
1158 bool wxTable::UpdateWhere(char *pWhereClause
)
1164 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1166 // Build the SQL UPDATE statement
1167 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1169 pDb
->WriteSqlLog(sqlStmt
);
1171 #ifdef DBDEBUG_CONSOLE
1172 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1175 // Execute the SQL UPDATE statement
1176 return(execUpdate(sqlStmt
));
1178 } // wxTable::UpdateWhere()
1180 /********** wxTable::Delete() **********/
1181 bool wxTable::Delete(void)
1187 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1189 // Build the SQL DELETE statement
1190 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1192 pDb
->WriteSqlLog(sqlStmt
);
1194 // Execute the SQL DELETE statement
1195 return(execDelete(sqlStmt
));
1197 } // wxTable::Delete()
1199 /********** wxTable::DeleteWhere() **********/
1200 bool wxTable::DeleteWhere(char *pWhereClause
)
1206 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1208 // Build the SQL DELETE statement
1209 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1211 pDb
->WriteSqlLog(sqlStmt
);
1213 // Execute the SQL DELETE statement
1214 return(execDelete(sqlStmt
));
1216 } // wxTable::DeleteWhere()
1218 /********** wxTable::DeleteMatching() **********/
1219 bool wxTable::DeleteMatching(void)
1225 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1227 // Build the SQL DELETE statement
1228 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1230 pDb
->WriteSqlLog(sqlStmt
);
1232 // Execute the SQL DELETE statement
1233 return(execDelete(sqlStmt
));
1235 } // wxTable::DeleteMatching()
1237 /********** wxTable::execDelete() **********/
1238 bool wxTable::execDelete(char *pSqlStmt
)
1240 // Execute the DELETE statement
1241 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1242 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1244 // Record deleted successfully
1247 } // wxTable::execDelete()
1249 /********** wxTable::execUpdate() **********/
1250 bool wxTable::execUpdate(char *pSqlStmt
)
1252 // Execute the UPDATE statement
1253 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1254 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1256 // Record deleted successfully
1259 } // wxTable::execUpdate()
1261 /********** wxTable::GetUpdateStmt() **********/
1262 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, char *pWhereClause
)
1268 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1269 bool firstColumn
= TRUE
;
1272 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1274 // Append a list of columns to be updated
1276 for (i
= 0; i
< noCols
; i
++)
1278 // Only append Updateable columns
1279 if (colDefs
[i
].Updateable
)
1282 strcat(pSqlStmt
, ",");
1284 firstColumn
= FALSE
;
1285 strcat(pSqlStmt
, colDefs
[i
].ColName
);
1286 strcat(pSqlStmt
, " = ?");
1290 // Append the WHERE clause to the SQL UPDATE statement
1291 strcat(pSqlStmt
, " WHERE ");
1294 case DB_UPD_KEYFIELDS
:
1295 // If the datasource supports the ROWID column, build
1296 // the where on ROWID for efficiency purposes.
1297 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1298 if (CanUpdByROWID())
1301 char rowid
[ROWID_LEN
];
1303 // Get the ROWID value. If not successful retreiving the ROWID,
1304 // simply fall down through the code and build the WHERE clause
1305 // based on the key fields.
1306 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1308 strcat(pSqlStmt
, "ROWID = '");
1309 strcat(pSqlStmt
, rowid
);
1310 strcat(pSqlStmt
, "'");
1314 // Unable to delete by ROWID, so build a WHERE
1315 // clause based on the keyfields.
1316 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1317 strcat(pSqlStmt
, whereClause
);
1320 strcat(pSqlStmt
, pWhereClause
);
1324 } // GetUpdateStmt()
1326 /********** wxTable::GetDeleteStmt() **********/
1327 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, char *pWhereClause
)
1333 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1337 // Handle the case of DeleteWhere() and the where clause is blank. It should
1338 // delete all records from the database in this case.
1339 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || strlen(pWhereClause
) == 0))
1341 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1345 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1347 // Append the WHERE clause to the SQL DELETE statement
1350 case DB_DEL_KEYFIELDS
:
1351 // If the datasource supports the ROWID column, build
1352 // the where on ROWID for efficiency purposes.
1353 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1354 if (CanUpdByROWID())
1357 char rowid
[ROWID_LEN
];
1359 // Get the ROWID value. If not successful retreiving the ROWID,
1360 // simply fall down through the code and build the WHERE clause
1361 // based on the key fields.
1362 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1364 strcat(pSqlStmt
, "ROWID = '");
1365 strcat(pSqlStmt
, rowid
);
1366 strcat(pSqlStmt
, "'");
1370 // Unable to delete by ROWID, so build a WHERE
1371 // clause based on the keyfields.
1372 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1373 strcat(pSqlStmt
, whereClause
);
1376 strcat(pSqlStmt
, pWhereClause
);
1378 case DB_DEL_MATCHING
:
1379 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1380 strcat(pSqlStmt
, whereClause
);
1384 } // GetDeleteStmt()
1386 /********** wxTable::GetWhereClause() **********/
1388 * Note: GetWhereClause() currently ignores timestamp columns.
1389 * They are not included as part of the where clause.
1392 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, char *qualTableName
)
1394 bool moreThanOneColumn
= FALSE
;
1397 // Loop through the columns building a where clause as you go
1399 for (i
= 0; i
< noCols
; i
++)
1401 // Determine if this column should be included in the WHERE clause
1402 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1403 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1405 // Skip over timestamp columns
1406 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1408 // If there is more than 1 column, join them with the keyword "AND"
1409 if (moreThanOneColumn
)
1410 strcat(pWhereClause
, " AND ");
1412 moreThanOneColumn
= TRUE
;
1413 // Concatenate where phrase for the column
1414 if (qualTableName
&& strlen(qualTableName
))
1416 strcat(pWhereClause
, qualTableName
);
1417 strcat(pWhereClause
, ".");
1419 strcat(pWhereClause
, colDefs
[i
].ColName
);
1420 strcat(pWhereClause
, " = ");
1421 switch(colDefs
[i
].SqlCtype
)
1424 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1427 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1430 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1433 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1436 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1439 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1442 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1445 strcat(pWhereClause
, colValue
);
1449 } // wxTable::GetWhereClause()
1451 /********** wxTable::IsColNull() **********/
1452 bool wxTable::IsColNull(int colNo
)
1454 switch(colDefs
[colNo
].SqlCtype
)
1457 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1459 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1461 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1463 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1465 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1467 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1469 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1470 case SQL_C_TIMESTAMP
:
1471 TIMESTAMP_STRUCT
*pDt
;
1472 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1473 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1481 } // wxTable::IsColNull()
1483 /********** wxTable::CanSelectForUpdate() **********/
1484 bool wxTable::CanSelectForUpdate(void)
1486 if (pDb
->Dbms() == dbmsMY_SQL
)
1489 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1494 } // wxTable::CanSelectForUpdate()
1496 /********** wxTable::CanUpdByROWID() **********/
1497 bool wxTable::CanUpdByROWID(void)
1500 //NOTE: Returning FALSE for now until this can be debugged,
1501 // as the ROWID is not getting updated correctly
1504 if (pDb
->Dbms() == dbmsORACLE
)
1509 } // wxTable::CanUpdByROWID()
1511 /********** wxTable::IsCursorClosedOnCommit() **********/
1512 bool wxTable::IsCursorClosedOnCommit(void)
1514 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1519 } // wxTable::IsCursorClosedOnCommit()
1521 /********** wxTable::ClearMemberVars() **********/
1522 void wxTable::ClearMemberVars(void)
1524 // Loop through the columns setting each member variable to zero
1526 for (i
= 0; i
< noCols
; i
++)
1528 switch(colDefs
[i
].SqlCtype
)
1531 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1534 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1537 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1540 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1543 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1546 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1549 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1551 case SQL_C_TIMESTAMP
:
1552 TIMESTAMP_STRUCT
*pDt
;
1553 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1565 } // wxTable::ClearMemberVars()
1567 /********** wxTable::SetQueryTimeout() **********/
1568 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1570 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1571 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1572 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1573 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1574 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1575 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1576 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1577 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1579 // Completed Successfully
1582 } // wxTable::SetQueryTimeout()
1584 /********** wxTable::SetColDefs() **********/
1585 void wxTable::SetColDefs (int index
, char *fieldName
, int dataType
, void *pData
,
1586 int cType
, int size
, bool keyField
, bool upd
,
1587 bool insAllow
, bool derivedCol
)
1589 if (!colDefs
) // May happen if the database connection fails
1592 if (strlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1594 strncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1595 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1598 strcpy(colDefs
[index
].ColName
, fieldName
);
1600 colDefs
[index
].DbDataType
= dataType
;
1601 colDefs
[index
].PtrDataObj
= pData
;
1602 colDefs
[index
].SqlCtype
= cType
;
1603 colDefs
[index
].SzDataObj
= size
;
1604 colDefs
[index
].KeyField
= keyField
;
1605 colDefs
[index
].DerivedCol
= derivedCol
;
1606 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1609 colDefs
[index
].Updateable
= FALSE
;
1610 colDefs
[index
].InsertAllowed
= FALSE
;
1614 colDefs
[index
].Updateable
= upd
;
1615 colDefs
[index
].InsertAllowed
= insAllow
;
1618 colDefs
[index
].Null
= FALSE
;
1620 } // wxTable::SetColDefs()
1622 /********** wxTable::SetCursor() **********/
1623 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1625 if (hstmtActivate
== DEFAULT_CURSOR
)
1626 hstmt
= *hstmtDefault
;
1628 hstmt
= *hstmtActivate
;
1630 } // wxTable::SetCursor()
1632 /********** wxTable::Count() **********/
1633 ULONG
wxTable::Count(void)
1636 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1639 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1640 strcpy(sqlStmt
, "SELECT COUNT(*) FROM ");
1641 strcat(sqlStmt
, queryTableName
);
1643 if (from
&& strlen(from
))
1644 strcat(sqlStmt
, from
);
1646 // Add the where clause if one is provided
1647 if (where
&& strlen(where
))
1649 strcat(sqlStmt
, " WHERE ");
1650 strcat(sqlStmt
, where
);
1653 pDb
->WriteSqlLog(sqlStmt
);
1655 // Initialize the Count cursor if it's not already initialized
1658 hstmtCount
= NewCursor(FALSE
,FALSE
);
1664 // Execute the SQL statement
1665 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1667 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1672 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1674 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1678 // Obtain the result
1679 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1681 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1686 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1687 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1689 // Return the record count
1692 } // wxTable::Count()
1694 /********** wxTable::Refresh() **********/
1695 bool wxTable::Refresh(void)
1699 // Switch to the internal cursor so any active cursors are not corrupted
1700 HSTMT currCursor
= GetCursor();
1701 hstmt
= hstmtInternal
;
1703 // Save the where and order by clauses
1704 char *saveWhere
= where
;
1705 char *saveOrderBy
= orderBy
;
1707 // Build a where clause to refetch the record with. Try and use the
1708 // ROWID if it's available, ow use the key fields.
1709 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1710 strcpy(whereClause
, "");
1711 if (CanUpdByROWID())
1714 char rowid
[ROWID_LEN
+1];
1716 // Get the ROWID value. If not successful retreiving the ROWID,
1717 // simply fall down through the code and build the WHERE clause
1718 // based on the key fields.
1719 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1721 strcat(whereClause
, queryTableName
);
1722 strcat(whereClause
, ".ROWID = '");
1723 strcat(whereClause
, rowid
);
1724 strcat(whereClause
, "'");
1728 // If unable to use the ROWID, build a where clause from the keyfields
1729 if (strlen(whereClause
) == 0)
1730 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1732 // Requery the record
1733 where
= whereClause
;
1738 if (result
&& !GetNext())
1741 // Switch back to original cursor
1742 SetCursor(&currCursor
);
1744 // Free the internal cursor
1745 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1746 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1748 // Restore the original where and order by clauses
1750 orderBy
= saveOrderBy
;
1754 } // wxTable::Refresh()
1756 /********** wxTable::SetNull(UINT colNo) **********/
1757 bool wxTable::SetNull(int colNo
)
1760 return(colDefs
[colNo
].Null
= TRUE
);
1764 } // wxTable::SetNull(UINT colNo)
1766 /********** wxTable::SetNull(char *colName) **********/
1767 bool wxTable::SetNull(char *colName
)
1770 for (i
= 0; i
< noCols
; i
++)
1772 if (!stricmp(colName
, colDefs
[i
].ColName
))
1777 return(colDefs
[i
].Null
= TRUE
);
1781 } // wxTable::SetNull(char *colName)
1783 /********** wxTable::NewCursor() **********/
1784 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
1786 HSTMT
*newHSTMT
= new HSTMT
;
1791 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
1793 pDb
->DispAllErrors(henv
, hdbc
);
1798 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
1800 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
1807 if(!bindCols(*newHSTMT
))
1815 SetCursor(newHSTMT
);
1819 } // wxTable::NewCursor()
1821 /********** wxTable::DeleteCursor() **********/
1822 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
1826 if (!hstmtDel
) // Cursor already deleted
1829 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
1831 pDb
->DispAllErrors(henv
, hdbc
);
1839 } // wxTable::DeleteCursor()
1841 #endif // wxUSE_ODBC