1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxTable class.
8 // Copyright: (c) 1996 Remstar International, Inc.
9 // Licence: wxWindows licence, plus:
10 // Notice: This class library and its intellectual design are free of charge for use,
11 // modification, enhancement, debugging under the following conditions:
12 // 1) These classes may only be used as part of the implementation of a
13 // wxWindows-based application
14 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
15 // user groups free of all charges for use with the wxWindows library.
16 // 3) These classes may not be distributed as part of any other class library,
17 // DLL, text (written or electronic), other than a complete distribution of
18 // the wxWindows GUI development toolkit.
19 ///////////////////////////////////////////////////////////////////////////////
22 #pragma implementation "dbtable.h"
36 #include "wx/wxprec.h"
48 #include <wx/dbtable.h>
56 // The HPUX preprocessor lines below were commented out on 8/20/97
57 // because macros.h currently redefines DEBUG and is unneeded.
59 // # include <macros.h>
62 # include <sys/minmax.h>
66 /********** wxTable::wxTable() **********/
67 wxTable::wxTable(wxDB
*pwxDB
, const char *tblName
, const int nCols
, const char *qryTblName
)
69 // Assign member variables
70 pDb
= pwxDB
; // Pointer to the wxDB object
72 strcpy(tableName
, tblName
); // Table Name
73 if (qryTblName
) // Name of the table/view to query
74 strcpy(queryTableName
, qryTblName
);
76 strcpy(queryTableName
, tblName
);
78 assert(pDb
); // Assert is placed after table name is assigned for error reporting reasons
82 noCols
= nCols
; // No. of cols in the table
83 where
= 0; // Where clause
84 orderBy
= 0; // Order By clause
85 from
= 0; // From clause
86 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
88 // Grab the HENV and HDBC from the wxDB object
92 // Allocate space for column definitions
94 colDefs
= new CcolDef
[noCols
]; // Points to the first column defintion
98 // Allocate statement handles for the table
99 if (SQLAllocStmt(hdbc
, &c0
) != SQL_SUCCESS
)
100 pDb
->DispAllErrors(henv
, hdbc
);
101 if (SQLAllocStmt(hdbc
, &c1
) != SQL_SUCCESS
)
102 pDb
->DispAllErrors(henv
, hdbc
);
103 if (SQLAllocStmt(hdbc
, &c2
) != SQL_SUCCESS
)
104 pDb
->DispAllErrors(henv
, hdbc
);
105 // if (SQLAllocStmt(hdbc, &c3) != SQL_SUCCESS)
106 // pDb->DispAllErrors(henv, hdbc);
107 // if (SQLAllocStmt(hdbc, &c4) != SQL_SUCCESS)
108 // pDb->DispAllErrors(henv, hdbc);
109 // if (SQLAllocStmt(hdbc, &c5) != SQL_SUCCESS)
110 // pDb->DispAllErrors(henv, hdbc);
111 // Allocate a separate statement handle for performing inserts
112 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
113 pDb
->DispAllErrors(henv
, hdbc
);
114 // Allocate a separate statement handle for performing deletes
115 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
116 pDb
->DispAllErrors(henv
, hdbc
);
117 // Allocate a separate statement handle for performing updates
118 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
119 pDb
->DispAllErrors(henv
, hdbc
);
120 // Allocate a separate statement handle for performing count(*) function
121 if (SQLAllocStmt(hdbc
, &hstmtCount
) != SQL_SUCCESS
)
122 pDb
->DispAllErrors(henv
, hdbc
);
124 // Set the cursor type for the statement handles
125 UDWORD cursorType
= SQL_CURSOR_STATIC
;
126 if (SQLSetStmtOption(c1
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
128 // Check to see if cursor type is supported
129 pDb
->GetNextError(henv
, hdbc
, c1
);
130 if (! strcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
132 // Datasource does not support static cursors. Driver
133 // will substitute a cursor type. Call SQLGetStmtOption()
134 // to determine which cursor type was selected.
135 if (SQLGetStmtOption(c1
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
136 pDb
->DispAllErrors(henv
, hdbc
, c1
);
138 cout
<< "Static cursor changed to: ";
141 case SQL_CURSOR_FORWARD_ONLY
:
142 cout
<< "Forward Only"; break;
143 case SQL_CURSOR_STATIC
:
144 cout
<< "Static"; break;
145 case SQL_CURSOR_KEYSET_DRIVEN
:
146 cout
<< "Keyset Driven"; break;
147 case SQL_CURSOR_DYNAMIC
:
148 cout
<< "Dynamic"; break;
150 cout
<< endl
<< endl
;
155 pDb
->DispNextError();
156 pDb
->DispAllErrors(henv
, hdbc
, c1
);
161 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
164 if (SQLSetStmtOption(c0
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
165 pDb
->DispAllErrors(henv
, hdbc
, c0
);
166 if (SQLSetStmtOption(c2
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
167 pDb
->DispAllErrors(henv
, hdbc
, c2
);
168 // if (SQLSetStmtOption(c3, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
169 // pDb->DispAllErrors(henv, hdbc, c3);
170 // if (SQLSetStmtOption(c4, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
171 // pDb->DispAllErrors(henv, hdbc, c4);
172 // if (SQLSetStmtOption(c5, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
173 // pDb->DispAllErrors(henv, hdbc, c5);
175 // Set the cursor type for the INSERT statement handle
176 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
177 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
178 // Set the cursor type for the DELETE statement handle
179 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
180 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
181 // Set the cursor type for the UPDATE statement handle
182 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
183 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
184 // Set the cursor type for the COUNT(*) statement handle
185 if (SQLSetStmtOption(hstmtCount
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
186 pDb
->DispAllErrors(henv
, hdbc
, hstmtCount
);
188 // Copy cursor 1 to the default cursor
190 currCursorNo
= DB_CURSOR1
;
192 } // wxTable::wxTable()
194 /********** wxTable::~wxTable() **********/
197 // Delete memory allocated for column definitions
201 // Free statement handles
202 if (SQLFreeStmt(c0
, SQL_DROP
) != SQL_SUCCESS
)
203 pDb
->DispAllErrors(henv
, hdbc
);
204 if (SQLFreeStmt(c1
, SQL_DROP
) != SQL_SUCCESS
)
205 pDb
->DispAllErrors(henv
, hdbc
);
206 if (SQLFreeStmt(c2
, SQL_DROP
) != SQL_SUCCESS
)
207 pDb
->DispAllErrors(henv
, hdbc
);
208 // if (SQLFreeStmt(c3, SQL_DROP) != SQL_SUCCESS)
209 // pDb->DispAllErrors(henv, hdbc);
210 // if (SQLFreeStmt(c4, SQL_DROP) != SQL_SUCCESS)
211 // pDb->DispAllErrors(henv, hdbc);
212 // if (SQLFreeStmt(c5, SQL_DROP) != SQL_SUCCESS)
213 // pDb->DispAllErrors(henv, hdbc);
214 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
215 pDb
->DispAllErrors(henv
, hdbc
);
216 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
217 pDb
->DispAllErrors(henv
, hdbc
);
218 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
219 pDb
->DispAllErrors(henv
, hdbc
);
220 if (SQLFreeStmt(hstmtCount
, SQL_DROP
) != SQL_SUCCESS
)
221 pDb
->DispAllErrors(henv
, hdbc
);
223 } // wxTable::~wxTable()
225 /********** wxTable::Open() **********/
226 bool wxTable::Open(void)
232 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
234 // Verify that the table exists in the database
235 if (!pDb
->TableExists(tableName
))
238 sprintf(s
, "Error opening '%s', table/view does not exist in the database.", tableName
);
243 // Bind the member variables for field exchange between
244 // the wxTable object and the ODBC record.
245 if(! bindInsertParams()) // Inserts
247 if(! bindUpdateParams()) // Updates
249 if(! bindCols(c0
)) // Selects
255 // if(! bindCols(c3))
257 // if(! bindCols(c4))
259 // if(! bindCols(c5))
262 // Build an insert statement using parameter markers
265 bool needComma
= FALSE
;
266 sprintf(sqlStmt
, "INSERT INTO %s (", tableName
);
267 for (i
= 0; i
< noCols
; i
++)
269 if (! colDefs
[i
].InsertAllowed
)
272 strcat(sqlStmt
, ",");
273 strcat(sqlStmt
, colDefs
[i
].ColName
);
277 strcat(sqlStmt
, ") VALUES (");
278 for (i
= 0; i
< noCols
; i
++)
280 if (! colDefs
[i
].InsertAllowed
)
283 strcat(sqlStmt
, ",");
284 strcat(sqlStmt
, "?");
287 strcat(sqlStmt
, ")");
289 pDb
->WriteSqlLog(sqlStmt
);
291 // Prepare the insert statement for execution
292 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
293 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
296 // Completed successfully
301 /********** wxTable::Query() **********/
302 bool wxTable::Query(bool forUpdate
, bool distinct
)
305 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
307 } // wxTable::Query()
309 /********** wxTable::QueryBySqlStmt() **********/
310 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
312 pDb
->WriteSqlLog(pSqlStmt
);
314 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
316 } // wxTable::QueryBySqlStmt()
318 /********** wxTable::QueryMatching() **********/
319 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
322 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
324 } // wxTable::QueryMatching()
326 /********** wxTable::QueryOnKeyFields() **********/
327 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
330 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
332 } // wxTable::QueryOnKeyFields()
334 /********** wxTable::query() **********/
335 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
337 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
339 // Set the selectForUpdate member variable
341 // The user may wish to select for update, but the DBMS may not be capable
342 selectForUpdate
= CanSelectForUpdate();
344 selectForUpdate
= FALSE
;
346 // Set the SQL SELECT string
347 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
348 { // so generate a select statement.
349 GetSelectStmt(sqlStmt
, queryType
, distinct
);
350 pDb
->WriteSqlLog(sqlStmt
);
353 // Make sure the cursor is closed first
354 if (! CloseCursor(hstmt
))
357 // Execute the SQL SELECT statement
358 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
),
359 SQL_NTS
) != SQL_SUCCESS
)
360 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
362 // Completed successfully
365 } // wxTable::query()
367 /********** wxTable::GetSelectStmt() **********/
368 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
370 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
374 // Build a select statement to query the database
375 strcpy(pSqlStmt
, "SELECT ");
377 // SELECT DISTINCT values only?
379 strcat(pSqlStmt
, "DISTINCT ");
381 // Was a FROM clause specified to join tables to the base table?
382 // Available for ::Query() only!!!
383 bool appendFromClause
= FALSE
;
384 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& strlen(from
))
385 appendFromClause
= TRUE
;
387 // Add the column list
388 for (int i
= 0; i
< noCols
; i
++)
390 // If joining tables, the base table column names must be qualified to avoid ambiguity
391 if (appendFromClause
)
393 strcat(pSqlStmt
, queryTableName
);
394 strcat(pSqlStmt
, ".");
396 strcat(pSqlStmt
, colDefs
[i
].ColName
);
398 strcat(pSqlStmt
, ",");
401 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
402 // the ROWID if querying distinct records. The rowid will always be unique.
403 if (!distinct
&& CanUpdByROWID())
405 // If joining tables, the base table column names must be qualified to avoid ambiguity
406 if (appendFromClause
)
408 strcat(pSqlStmt
, ",");
409 strcat(pSqlStmt
, queryTableName
);
410 strcat(pSqlStmt
, ".ROWID");
413 strcat(pSqlStmt
, ",ROWID");
416 // Append the FROM tablename portion
417 strcat(pSqlStmt
, " FROM ");
418 strcat(pSqlStmt
, queryTableName
);
419 if (appendFromClause
)
420 strcat(pSqlStmt
, from
);
422 // Append the WHERE clause. Either append the where clause for the class
423 // or build a where clause. The typeOfSelect determines this.
426 case DB_SELECT_WHERE
:
427 if (where
&& strlen(where
)) // May not want a where clause!!!
429 strcat(pSqlStmt
, " WHERE ");
430 strcat(pSqlStmt
, where
);
433 case DB_SELECT_KEYFIELDS
:
434 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
435 if (strlen(whereClause
))
437 strcat(pSqlStmt
, " WHERE ");
438 strcat(pSqlStmt
, whereClause
);
441 case DB_SELECT_MATCHING
:
442 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
443 if (strlen(whereClause
))
445 strcat(pSqlStmt
, " WHERE ");
446 strcat(pSqlStmt
, whereClause
);
451 // Append the ORDER BY clause
452 if (orderBy
&& strlen(orderBy
))
454 strcat(pSqlStmt
, " ORDER BY ");
455 strcat(pSqlStmt
, orderBy
);
458 // SELECT FOR UPDATE if told to do so and the datasource is capable
459 if (selectForUpdate
&& CanSelectForUpdate())
460 strcat(pSqlStmt
, " FOR UPDATE");
462 } // wxTable::GetSelectStmt()
464 /********** wxTable::getRec() **********/
465 bool wxTable::getRec(UWORD fetchType
)
469 #ifndef FWD_ONLY_CURSORS
470 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
473 if ((retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
)) != SQL_SUCCESS
)
474 if (retcode
== SQL_NO_DATA_FOUND
)
477 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
479 // Fetch the next record from the record set
480 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
481 if (retcode
== SQL_NO_DATA_FOUND
)
484 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
487 // Completed successfully
490 } // wxTable::getRec()
492 /********** wxTable::GetRowNum() **********/
493 UWORD
wxTable::GetRowNum(void)
497 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, &rowNum
) != SQL_SUCCESS
)
499 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
503 // Completed successfully
504 return((UWORD
) rowNum
);
506 } // wxTable::GetRowNum()
508 /********** wxTable::bindInsertParams() **********/
509 bool wxTable::bindInsertParams(void)
515 // Bind each column (that can be inserted) of the table to a parameter marker
516 for (int i
= 0; i
< noCols
; i
++)
518 if (! colDefs
[i
].InsertAllowed
)
520 switch(colDefs
[i
].DbDataType
)
522 case DB_DATA_TYPE_VARCHAR
:
523 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
524 precision
= colDefs
[i
].SzDataObj
;
526 colDefs
[i
].CbValue
= SQL_NTS
;
528 case DB_DATA_TYPE_INTEGER
:
529 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
530 precision
= pDb
->typeInfInteger
.Precision
;
532 colDefs
[i
].CbValue
= 0;
534 case DB_DATA_TYPE_FLOAT
:
535 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
536 precision
= pDb
->typeInfFloat
.Precision
;
537 scale
= pDb
->typeInfFloat
.MaximumScale
;
538 // SQL Sybase Anywhere v5.5 returned a negative number for the
539 // MaxScale. This caused ODBC to kick out an error on ibscale.
540 // I check for this here and set the scale = precision.
542 // scale = (short) precision;
543 colDefs
[i
].CbValue
= 0;
545 case DB_DATA_TYPE_DATE
:
546 fSqlType
= pDb
->typeInfDate
.FsqlType
;
547 precision
= pDb
->typeInfDate
.Precision
;
549 colDefs
[i
].CbValue
= 0;
552 if (SQLBindParameter(hstmtInsert
, i
+1, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
553 fSqlType
, precision
, scale
, colDefs
[i
].PtrDataObj
,
554 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
555 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
558 // Completed successfully
561 } // wxTable::bindInsertParams()
563 /********** wxTable::bindUpdateParams() **********/
564 bool wxTable::bindUpdateParams(void)
570 // Bind each UPDATEABLE column of the table to a parameter marker
571 for (int i
= 0, colNo
= 1; i
< noCols
; i
++)
573 if (! colDefs
[i
].Updateable
)
575 switch(colDefs
[i
].DbDataType
)
577 case DB_DATA_TYPE_VARCHAR
:
578 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
579 precision
= colDefs
[i
].SzDataObj
;
581 colDefs
[i
].CbValue
= SQL_NTS
;
583 case DB_DATA_TYPE_INTEGER
:
584 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
585 precision
= pDb
->typeInfInteger
.Precision
;
587 colDefs
[i
].CbValue
= 0;
589 case DB_DATA_TYPE_FLOAT
:
590 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
591 precision
= pDb
->typeInfFloat
.Precision
;
592 scale
= pDb
->typeInfFloat
.MaximumScale
;
593 // SQL Sybase Anywhere v5.5 returned a negative number for the
594 // MaxScale. This caused ODBC to kick out an error on ibscale.
595 // I check for this here and set the scale = precision.
597 // scale = (short) precision;
598 colDefs
[i
].CbValue
= 0;
600 case DB_DATA_TYPE_DATE
:
601 fSqlType
= pDb
->typeInfDate
.FsqlType
;
602 precision
= pDb
->typeInfDate
.Precision
;
604 colDefs
[i
].CbValue
= 0;
607 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
608 fSqlType
, precision
, scale
, colDefs
[i
].PtrDataObj
,
609 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
610 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
613 // Completed successfully
616 } // wxTable::bindUpdateParams()
618 /********** wxTable::bindCols() **********/
619 bool wxTable::bindCols(HSTMT cursor
)
623 // Bind each column of the table to a memory address for fetching data
624 for (int i
= 0; i
< noCols
; i
++)
626 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, colDefs
[i
].PtrDataObj
,
627 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
628 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
631 // Completed successfully
634 } // wxTable::bindCols()
636 /********** wxTable::CloseCursor() **********/
637 bool wxTable::CloseCursor(HSTMT cursor
)
639 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
640 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
642 // Completed successfully
645 } // wxTable::CloseCursor()
647 /********** wxTable::CreateTable() **********/
648 bool wxTable::CreateTable(void)
654 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
657 cout
<< "Creating Table " << tableName
<< "..." << endl
;
660 // Drop the table first
661 sprintf(sqlStmt
, "DROP TABLE %s", tableName
);
662 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
664 // Check for sqlState = S0002, "Table or view not found".
665 // Ignore this error, bomb out on any other error.
666 // SQL Sybase Anwhere v5.5 returns an access violation error here
667 // (sqlstate = 42000) rather than an S0002.
668 pDb
->GetNextError(henv
, hdbc
, hstmt
);
669 if (strcmp(pDb
->sqlState
, "S0002") && strcmp(pDb
->sqlState
, "42000"))
671 pDb
->DispNextError();
672 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
673 pDb
->RollbackTrans();
679 pDb
->WriteSqlLog(sqlStmt
);
681 // Commit the transaction and close the cursor
682 if (! pDb
->CommitTrans())
684 if (! CloseCursor(hstmt
))
689 for (i
= 0; i
< noCols
; i
++)
691 // Exclude derived columns since they are NOT part of the base table
692 if (colDefs
[i
].DerivedCol
)
694 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
695 switch(colDefs
[i
].DbDataType
)
697 case DB_DATA_TYPE_VARCHAR
:
698 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
700 case DB_DATA_TYPE_INTEGER
:
701 cout
<< pDb
->typeInfInteger
.TypeName
;
703 case DB_DATA_TYPE_FLOAT
:
704 cout
<< pDb
->typeInfFloat
.TypeName
;
706 case DB_DATA_TYPE_DATE
:
707 cout
<< pDb
->typeInfDate
.TypeName
;
714 // Build a CREATE TABLE string from the colDefs structure.
715 bool needComma
= FALSE
;
716 sprintf(sqlStmt
, "CREATE TABLE %s (", tableName
);
717 for (i
= 0; i
< noCols
; i
++)
719 // Exclude derived columns since they are NOT part of the base table
720 if (colDefs
[i
].DerivedCol
)
724 strcat(sqlStmt
, ",");
726 strcat(sqlStmt
, colDefs
[i
].ColName
);
727 strcat(sqlStmt
, " ");
729 switch(colDefs
[i
].DbDataType
)
731 case DB_DATA_TYPE_VARCHAR
:
732 strcat(sqlStmt
, pDb
->typeInfVarchar
.TypeName
); break;
733 case DB_DATA_TYPE_INTEGER
:
734 strcat(sqlStmt
, pDb
->typeInfInteger
.TypeName
); break;
735 case DB_DATA_TYPE_FLOAT
:
736 strcat(sqlStmt
, pDb
->typeInfFloat
.TypeName
); break;
737 case DB_DATA_TYPE_DATE
:
738 strcat(sqlStmt
, pDb
->typeInfDate
.TypeName
); break;
740 // For varchars, append the size of the string
741 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
744 // strcat(sqlStmt, "(");
745 // strcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
746 // strcat(sqlStmt, ")");
747 sprintf(s
, "(%d)", colDefs
[i
].SzDataObj
);
752 // If there is a primary key defined, include it in the create statement
753 for (i
= j
= 0; i
< noCols
; i
++)
755 if (colDefs
[i
].KeyField
)
761 if (j
) // Found a keyfield
763 strcat(sqlStmt
, ",CONSTRAINT ");
764 strcat(sqlStmt
, tableName
);
765 strcat(sqlStmt
, "_PIDX PRIMARY KEY (");
766 // List column name(s) of column(s) comprising the primary key
767 for (i
= j
= 0; i
< noCols
; i
++)
769 if (colDefs
[i
].KeyField
)
771 if (j
++) // Multi part key, comma separate names
772 strcat(sqlStmt
, ",");
773 strcat(sqlStmt
, colDefs
[i
].ColName
);
776 strcat(sqlStmt
, ")");
778 // Append the closing parentheses for the create table statement
779 strcat(sqlStmt
, ")");
781 pDb
->WriteSqlLog(sqlStmt
);
784 cout
<< endl
<< sqlStmt
<< endl
;
787 // Execute the CREATE TABLE statement
788 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
790 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
791 pDb
->RollbackTrans();
796 // Commit the transaction and close the cursor
797 if (! pDb
->CommitTrans())
799 if (! CloseCursor(hstmt
))
802 // Database table created successfully
805 } // wxTable::CreateTable()
807 /********** wxTable::CreateIndex() **********/
808 bool wxTable::CreateIndex(char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
)
810 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
812 // Build a CREATE INDEX statement
813 strcpy(sqlStmt
, "CREATE ");
815 strcat(sqlStmt
, "UNIQUE ");
817 strcat(sqlStmt
, "INDEX ");
818 strcat(sqlStmt
, idxName
);
819 strcat(sqlStmt
, " ON ");
820 strcat(sqlStmt
, tableName
);
821 strcat(sqlStmt
, " (");
823 // Append list of columns making up index
824 for (int i
= 0; i
< noIdxCols
; i
++)
826 strcat(sqlStmt
, pIdxDefs
[i
].ColName
);
827 if (pIdxDefs
[i
].Ascending
)
828 strcat(sqlStmt
, " ASC");
830 strcat(sqlStmt
, " DESC");
831 if ((i
+ 1) < noIdxCols
)
832 strcat(sqlStmt
, ",");
835 // Append closing parentheses
836 strcat(sqlStmt
, ")");
838 pDb
->WriteSqlLog(sqlStmt
);
841 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
844 // Execute the CREATE INDEX statement
845 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
847 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
848 pDb
->RollbackTrans();
853 // Commit the transaction and close the cursor
854 if (! pDb
->CommitTrans())
856 if (! CloseCursor(hstmt
))
859 // Index Created Successfully
862 } // wxTable::CreateIndex()
864 /********** wxTable::Insert() **********/
865 int wxTable::Insert(void)
867 // Insert the record by executing the already prepared insert statement
868 if (SQLExecute(hstmtInsert
) != SQL_SUCCESS
)
870 // Check to see if integrity constraint was violated
871 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
872 if (! strcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
873 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
876 pDb
->DispNextError();
877 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
882 // Record inserted into the datasource successfully
885 } // wxTable::Insert()
887 /********** wxTable::Update(pSqlStmt) **********/
888 bool wxTable::Update(char *pSqlStmt
)
890 pDb
->WriteSqlLog(pSqlStmt
);
892 return(execUpdate(pSqlStmt
));
894 } // wxTable::Update(pSqlStmt)
896 /********** wxTable::Update() **********/
897 bool wxTable::Update(void)
899 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
901 // Build the SQL UPDATE statement
902 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
904 pDb
->WriteSqlLog(sqlStmt
);
907 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
910 // Execute the SQL UPDATE statement
911 return(execUpdate(sqlStmt
));
913 } // wxTable::Update()
915 /********** wxTable::UpdateWhere() **********/
916 bool wxTable::UpdateWhere(char *pWhereClause
)
918 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
920 // Build the SQL UPDATE statement
921 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
923 pDb
->WriteSqlLog(sqlStmt
);
926 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
929 // Execute the SQL UPDATE statement
930 return(execUpdate(sqlStmt
));
932 } // wxTable::UpdateWhere()
934 /********** wxTable::Delete() **********/
935 bool wxTable::Delete(void)
937 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
939 // Build the SQL DELETE statement
940 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
942 pDb
->WriteSqlLog(sqlStmt
);
944 // Execute the SQL DELETE statement
945 return(execDelete(sqlStmt
));
947 } // wxTable::Delete()
949 /********** wxTable::DeleteWhere() **********/
950 bool wxTable::DeleteWhere(char *pWhereClause
)
952 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
954 // Build the SQL DELETE statement
955 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
957 pDb
->WriteSqlLog(sqlStmt
);
959 // Execute the SQL DELETE statement
960 return(execDelete(sqlStmt
));
962 } // wxTable::DeleteWhere()
964 /********** wxTable::DeleteMatching() **********/
965 bool wxTable::DeleteMatching(void)
967 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
969 // Build the SQL DELETE statement
970 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
972 pDb
->WriteSqlLog(sqlStmt
);
974 // Execute the SQL DELETE statement
975 return(execDelete(sqlStmt
));
977 } // wxTable::DeleteMatching()
979 /********** wxTable::execDelete() **********/
980 bool wxTable::execDelete(char *pSqlStmt
)
982 // Execute the DELETE statement
983 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
984 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
986 // Record deleted successfully
989 } // wxTable::execDelete()
991 /********** wxTable::execUpdate() **********/
992 bool wxTable::execUpdate(char *pSqlStmt
)
994 // Execute the UPDATE statement
995 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
996 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
998 // Record deleted successfully
1001 } // wxTable::execUpdate()
1003 /********** wxTable::GetUpdateStmt() **********/
1004 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, char *pWhereClause
)
1006 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1007 bool firstColumn
= TRUE
;
1010 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1012 // Append a list of columns to be updated
1013 for (int i
= 0; i
< noCols
; i
++)
1015 // Only append Updateable columns
1016 if (colDefs
[i
].Updateable
)
1019 strcat(pSqlStmt
, ",");
1021 firstColumn
= FALSE
;
1022 strcat(pSqlStmt
, colDefs
[i
].ColName
);
1023 strcat(pSqlStmt
, " = ?");
1027 // Append the WHERE clause to the SQL UPDATE statement
1028 strcat(pSqlStmt
, " WHERE ");
1031 case DB_UPD_KEYFIELDS
:
1032 // If the datasource supports the ROWID column, build
1033 // the where on ROWID for efficiency purposes.
1034 // e.g. UPDATE PARTS SET C1 = ?, C2 = ? WHERE ROWID = '111.222.333'
1035 if (CanUpdByROWID())
1038 char rowid
[ROWID_LEN
];
1040 // Get the ROWID value. If not successful retreiving the ROWID,
1041 // simply fall down through the code and build the WHERE clause
1042 // based on the key fields.
1043 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1045 strcat(pSqlStmt
, "ROWID = '");
1046 strcat(pSqlStmt
, rowid
);
1047 strcat(pSqlStmt
, "'");
1051 // Unable to delete by ROWID, so build a WHERE
1052 // clause based on the keyfields.
1053 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1054 strcat(pSqlStmt
, whereClause
);
1057 strcat(pSqlStmt
, pWhereClause
);
1061 } // GetUpdateStmt()
1063 /********** wxTable::GetDeleteStmt() **********/
1064 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, char *pWhereClause
)
1066 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1070 // Handle the case of DeleteWhere() and the where clause is blank. It should
1071 // delete all records from the database in this case.
1072 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || strlen(pWhereClause
) == 0))
1074 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1078 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1080 // Append the WHERE clause to the SQL DELETE statement
1083 case DB_DEL_KEYFIELDS
:
1084 // If the datasource supports the ROWID column, build
1085 // the where on ROWID for efficiency purposes.
1086 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1087 if (CanUpdByROWID())
1090 char rowid
[ROWID_LEN
];
1092 // Get the ROWID value. If not successful retreiving the ROWID,
1093 // simply fall down through the code and build the WHERE clause
1094 // based on the key fields.
1095 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1097 strcat(pSqlStmt
, "ROWID = '");
1098 strcat(pSqlStmt
, rowid
);
1099 strcat(pSqlStmt
, "'");
1103 // Unable to delete by ROWID, so build a WHERE
1104 // clause based on the keyfields.
1105 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1106 strcat(pSqlStmt
, whereClause
);
1109 strcat(pSqlStmt
, pWhereClause
);
1111 case DB_DEL_MATCHING
:
1112 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1113 strcat(pSqlStmt
, whereClause
);
1117 } // GetDeleteStmt()
1119 /********** wxTable::GetWhereClause() **********/
1121 * Note: GetWhereClause() currently ignores timestamp columns.
1122 * They are not included as part of the where clause.
1125 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, char *qualTableName
)
1127 bool moreThanOneColumn
= FALSE
;
1130 // Loop through the columns building a where clause as you go
1131 for (int i
= 0; i
< noCols
; i
++)
1133 // Determine if this column should be included in the WHERE clause
1134 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1135 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1137 // Skip over timestamp columns
1138 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1140 // If there is more than 1 column, join them with the keyword "AND"
1141 if (moreThanOneColumn
)
1142 strcat(pWhereClause
, " AND ");
1144 moreThanOneColumn
= TRUE
;
1145 // Concatenate where phrase for the column
1146 if (qualTableName
&& strlen(qualTableName
))
1148 strcat(pWhereClause
, qualTableName
);
1149 strcat(pWhereClause
, ".");
1151 strcat(pWhereClause
, colDefs
[i
].ColName
);
1152 strcat(pWhereClause
, " = ");
1153 switch(colDefs
[i
].SqlCtype
)
1156 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1159 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1162 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1165 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1168 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1171 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1174 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1177 strcat(pWhereClause
, colValue
);
1181 } // wxTable::GetWhereClause()
1183 /********** wxTable::IsColNull() **********/
1184 bool wxTable::IsColNull(int colNo
)
1186 switch(colDefs
[colNo
].SqlCtype
)
1189 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1191 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1193 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1195 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1197 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1199 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1201 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1202 case SQL_C_TIMESTAMP
:
1203 TIMESTAMP_STRUCT
*pDt
;
1204 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1205 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1213 } // wxTable::IsColNull()
1215 /********** wxTable::CanSelectForUpdate() **********/
1217 bool wxTable::CanSelectForUpdate(void)
1219 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1224 } // wxTable::CanSelectForUpdate()
1226 /********** wxTable::CanUpdByROWID() **********/
1227 bool wxTable::CanUpdByROWID(void)
1230 //NOTE: Returning FALSE for now until this can be debugged,
1231 // as the ROWID is not getting updated correctly
1234 if ((! strcmp(pDb
->dbInf
.dbmsName
, "Oracle")) || (! strcmp(pDb
->dbInf
.dbmsName
, "ORACLE")))
1239 } // wxTable::CanUpdByROWID()
1241 /********** wxTable::IsCursorClosedOnCommit() **********/
1242 bool wxTable::IsCursorClosedOnCommit(void)
1244 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1249 } // wxTable::IsCursorClosedOnCommit()
1251 /********** wxTable::ClearMemberVars() **********/
1252 void wxTable::ClearMemberVars(void)
1254 // Loop through the columns setting each member variable to zero
1255 for (int i
= 0; i
< noCols
; i
++)
1257 switch(colDefs
[i
].SqlCtype
)
1260 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1263 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1266 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1269 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1272 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1275 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1278 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1280 case SQL_C_TIMESTAMP
:
1281 TIMESTAMP_STRUCT
*pDt
;
1282 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1294 } // wxTable::ClearMemberVars()
1296 /********** wxTable::SetQueryTimeout() **********/
1297 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1299 if (SQLSetStmtOption(c0
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1300 return(pDb
->DispAllErrors(henv
, hdbc
, c0
));
1301 if (SQLSetStmtOption(c1
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1302 return(pDb
->DispAllErrors(henv
, hdbc
, c1
));
1303 if (SQLSetStmtOption(c2
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1304 return(pDb
->DispAllErrors(henv
, hdbc
, c2
));
1305 // if (SQLSetStmtOption(c3, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1306 // return(pDb->DispAllErrors(henv, hdbc, c3));
1307 // if (SQLSetStmtOption(c4, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1308 // return(pDb->DispAllErrors(henv, hdbc, c4));
1309 // if (SQLSetStmtOption(c5, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1310 // return(pDb->DispAllErrors(henv, hdbc, c5));
1311 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1312 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1313 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1314 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1315 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1316 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1317 if (SQLSetStmtOption(hstmtCount
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1318 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtCount
));
1320 // Completed Successfully
1323 } // wxTable::SetQueryTimeout()
1325 /********** wxTable::SetColDefs() **********/
1326 void wxTable::SetColDefs (int index
, char *fieldName
, int dataType
, void *pData
,
1327 int cType
, int size
, bool keyField
, bool upd
,
1328 bool insAllow
, bool derivedCol
)
1330 if (strlen(fieldName
) > DB_MAX_COLUMN_NAME_LEN
) // glt 4/21/97
1332 strncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1333 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0; // glt 10/23/97
1336 strcpy(colDefs
[index
].ColName
, fieldName
);
1338 colDefs
[index
].DbDataType
= dataType
;
1339 colDefs
[index
].PtrDataObj
= pData
;
1340 colDefs
[index
].SqlCtype
= cType
;
1341 colDefs
[index
].SzDataObj
= size
;
1342 colDefs
[index
].KeyField
= keyField
;
1343 colDefs
[index
].DerivedCol
= derivedCol
;
1344 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1347 colDefs
[index
].Updateable
= FALSE
;
1348 colDefs
[index
].InsertAllowed
= FALSE
;
1352 colDefs
[index
].Updateable
= upd
;
1353 colDefs
[index
].InsertAllowed
= insAllow
;
1356 } // wxTable::SetColDefs()
1358 /********** wxTable::SetCursor() **********/
1359 bool wxTable::SetCursor(int cursorNo
)
1365 // currCursorNo doesn't change since Cursor0 is a temp cursor
1369 currCursorNo
= DB_CURSOR1
;
1373 currCursorNo
= DB_CURSOR2
;
1377 // currCursorNo = DB_CURSOR3;
1381 // currCursorNo = DB_CURSOR4;
1385 // currCursorNo = DB_CURSOR5;
1391 // Refresh the current record
1392 #ifndef FWD_ONLY_CURSORS
1393 UDWORD cRowsFetched
;
1395 SQLExtendedFetch(hstmt
, SQL_FETCH_NEXT
, 0, &cRowsFetched
, &rowStatus
);
1396 SQLExtendedFetch(hstmt
, SQL_FETCH_PRIOR
, 0, &cRowsFetched
, &rowStatus
);
1399 // Completed successfully
1402 } // wxTable::SetCursor()
1404 /********** wxTable::Count() **********/
1405 ULONG
wxTable::Count(void)
1408 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1411 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1412 strcpy(sqlStmt
, "SELECT COUNT(*) FROM ");
1413 strcat(sqlStmt
, queryTableName
);
1415 if (from
&& strlen(from
))
1416 strcat(sqlStmt
, from
);
1418 // Add the where clause if one is provided
1419 if (where
&& strlen(where
))
1421 strcat(sqlStmt
, " WHERE ");
1422 strcat(sqlStmt
, where
);
1425 pDb
->WriteSqlLog(sqlStmt
);
1427 // Execute the SQL statement
1428 if (SQLExecDirect(hstmtCount
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1430 pDb
->DispAllErrors(henv
, hdbc
, hstmtCount
);
1435 if (SQLFetch(hstmtCount
) != SQL_SUCCESS
)
1437 pDb
->DispAllErrors(henv
, hdbc
, hstmtCount
);
1441 // Obtain the result
1442 if (SQLGetData(hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1444 pDb
->DispAllErrors(henv
, hdbc
, hstmtCount
);
1449 if (SQLFreeStmt(hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1450 pDb
->DispAllErrors(henv
, hdbc
, hstmtCount
);
1452 // Return the record count
1455 } // wxTable::Count()
1457 /********** wxTable::Refresh() **********/
1458 bool wxTable::Refresh(void)
1462 // Switch to cursor 0
1463 int cursorNo
= GetCursor();
1467 // Save the where and order by clauses
1468 char *saveWhere
= where
;
1469 char *saveOrderBy
= orderBy
;
1471 // Build a where clause to refetch the record with. Try and use the
1472 // ROWID if it's available, ow use the key fields.
1473 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1474 strcpy(whereClause
, "");
1475 if (CanUpdByROWID())
1478 char rowid
[ROWID_LEN
+1];
1480 // Get the ROWID value. If not successful retreiving the ROWID,
1481 // simply fall down through the code and build the WHERE clause
1482 // based on the key fields.
1483 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1485 strcat(whereClause
, queryTableName
);
1486 strcat(whereClause
, ".ROWID = '");
1487 strcat(whereClause
, rowid
);
1488 strcat(whereClause
, "'");
1492 // If unable to use the ROWID, build a where clause from the keyfields
1493 if (strlen(whereClause
) == 0)
1494 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1496 // Requery the record
1497 where
= whereClause
;
1502 if (result
&& !GetNext())
1505 // Switch back to original cursor
1506 if (!SetCursor(cursorNo
))
1509 // Restore the original where and order by clauses
1511 orderBy
= saveOrderBy
;
1515 } // wxTable::Refresh()