1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxTable class.
5 // Modified by: George Tasker
7 // -Dynamic cursor support - Only one predefined cursor, as many others as
8 // you need may be created on demand
9 // -Reduced number of active cursors significantly
10 // -Query-Only wxTable objects
13 // Copyright: (c) 1996 Remstar International, Inc.
14 // Licence: wxWindows licence, plus:
15 // Notice: This class library and its intellectual design are free of charge for use,
16 // modification, enhancement, debugging under the following conditions:
17 // 1) These classes may only be used as part of the implementation of a
18 // wxWindows-based application
19 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
20 // user groups free of all charges for use with the wxWindows library.
21 // 3) These classes may not be distributed as part of any other class library,
22 // DLL, text (written or electronic), other than a complete distribution of
23 // the wxWindows GUI development toolkit.
24 ///////////////////////////////////////////////////////////////////////////////
31 // Use this line for wxWindows v1.x
33 // Use this line for wxWindows v2.x
34 #include "wx/version.h"
35 #include "wx/wxprec.h"
37 #if wxMAJOR_VERSION == 2
39 # pragma implementation "dbtable.h"
43 #ifdef DBDEBUG_CONSOLE
51 #if wxMAJOR_VERSION == 2
53 #include "wx/string.h"
54 #include "wx/object.h"
57 #include "wx/msgdlg.h"
59 #include "wx/filefn.h"
62 #if wxMAJOR_VERSION == 1
63 # if defined(wx_msw) || defined(wx_x)
80 #if wxMAJOR_VERSION == 1
82 #elif wxMAJOR_VERSION == 2
83 #include "wx/dbtable.h"
87 // The HPUX preprocessor lines below were commented out on 8/20/97
88 // because macros.h currently redefines DEBUG and is unneeded.
90 // # include <macros.h>
93 # include <sys/minmax.h>
97 ULONG lastTableID
= 0;
105 /********** wxTable::wxTable() **********/
106 wxTable::wxTable(wxDB
*pwxDB
, const char *tblName
, const int nCols
,
107 const char *qryTblName
, bool qryOnly
, const char *tblPath
)
109 pDb
= pwxDB
; // Pointer to the wxDB object
113 hstmtDefault
= 0; // Initialized below
114 hstmtCount
= 0; // Initialized first time it is needed
121 noCols
= nCols
; // No. of cols in the table
122 where
= 0; // Where clause
123 orderBy
= 0; // Order By clause
124 from
= 0; // From clause
125 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
130 wxStrcpy(tableName
, tblName
); // Table Name
132 wxStrcpy(tablePath
, tblPath
); // Table Path - used for dBase files
136 if (qryTblName
) // Name of the table/view to query
137 wxStrcpy(queryTableName
, qryTblName
);
139 wxStrcpy(queryTableName
, tblName
);
147 tableID
= ++lastTableID
;
148 s
.sprintf("wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName
,tableID
,pDb
);
151 CstructTablesInUse
*tableInUse
;
152 tableInUse
= new CstructTablesInUse();
153 tableInUse
->tableName
= tblName
;
154 tableInUse
->tableID
= tableID
;
155 tableInUse
->pDb
= pDb
;
156 TablesInUse
.Append(tableInUse
);
159 pDb
->WriteSqlLog(s
.GetData());
161 // Grab the HENV and HDBC from the wxDB object
165 // Allocate space for column definitions
167 colDefs
= new wxColDef
[noCols
]; // Points to the first column defintion
169 // Allocate statement handles for the table
172 // Allocate a separate statement handle for performing inserts
173 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
174 pDb
->DispAllErrors(henv
, hdbc
);
175 // Allocate a separate statement handle for performing deletes
176 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
177 pDb
->DispAllErrors(henv
, hdbc
);
178 // Allocate a separate statement handle for performing updates
179 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
180 pDb
->DispAllErrors(henv
, hdbc
);
182 // Allocate a separate statement handle for internal use
183 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
184 pDb
->DispAllErrors(henv
, hdbc
);
186 // Set the cursor type for the statement handles
187 cursorType
= SQL_CURSOR_STATIC
;
188 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
190 // Check to see if cursor type is supported
191 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
192 if (! wxStrcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
194 // Datasource does not support static cursors. Driver
195 // will substitute a cursor type. Call SQLGetStmtOption()
196 // to determine which cursor type was selected.
197 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
198 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
199 #ifdef DBDEBUG_CONSOLE
200 cout
<< "Static cursor changed to: ";
203 case SQL_CURSOR_FORWARD_ONLY
:
204 cout
<< "Forward Only"; break;
205 case SQL_CURSOR_STATIC
:
206 cout
<< "Static"; break;
207 case SQL_CURSOR_KEYSET_DRIVEN
:
208 cout
<< "Keyset Driven"; break;
209 case SQL_CURSOR_DYNAMIC
:
210 cout
<< "Dynamic"; break;
212 cout
<< endl
<< endl
;
217 pDb
->DispNextError();
218 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
221 #ifdef DBDEBUG_CONSOLE
223 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
228 // Set the cursor type for the INSERT statement handle
229 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
230 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
231 // Set the cursor type for the DELETE statement handle
232 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
233 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
234 // Set the cursor type for the UPDATE statement handle
235 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
236 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
239 // Make the default cursor the active cursor
240 hstmtDefault
= NewCursor(FALSE
,FALSE
);
241 assert(hstmtDefault
);
242 hstmt
= *hstmtDefault
;
244 } // wxTable::wxTable()
247 /********** wxTable::~wxTable() **********/
253 s
.sprintf("wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName
,tableID
,pDb
);
254 pDb
->WriteSqlLog(s
.GetData());
260 TablesInUse
.DeleteContents(TRUE
);
264 pNode
= TablesInUse
.First();
265 while (pNode
&& !found
)
267 if (((CstructTablesInUse
*)pNode
->Data())->tableID
== tableID
)
270 if (!TablesInUse
.DeleteNode(pNode
))
271 wxMessageBox (s
.GetData(),"Unable to delete node!");
274 pNode
= pNode
->Next();
279 msg
.sprintf("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s
.GetData());
280 wxMessageBox (msg
.GetData(),"NOTICE...");
285 // Decrement the wxDB table count
289 // Delete memory allocated for column definitions
293 // Free statement handles
297 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
298 pDb
->DispAllErrors(henv
, hdbc
);
300 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
301 pDb
->DispAllErrors(henv
, hdbc
);
303 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
304 pDb
->DispAllErrors(henv
, hdbc
);
307 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
308 pDb
->DispAllErrors(henv
, hdbc
);
310 // Delete dynamically allocated cursors
312 DeleteCursor(hstmtDefault
);
314 DeleteCursor(hstmtCount
);
316 } // wxTable::~wxTable()
320 /***************************** PRIVATE FUNCTIONS *****************************/
324 /********** wxTable::bindInsertParams() **********/
325 bool wxTable::bindInsertParams(void)
332 UDWORD precision
= 0;
335 // Bind each column (that can be inserted) of the table to a parameter marker
337 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
339 if (! colDefs
[i
].InsertAllowed
)
341 switch(colDefs
[i
].DbDataType
)
343 case DB_DATA_TYPE_VARCHAR
:
344 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
345 precision
= colDefs
[i
].SzDataObj
;
347 colDefs
[i
].CbValue
= SQL_NTS
;
349 case DB_DATA_TYPE_INTEGER
:
350 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
351 precision
= pDb
->typeInfInteger
.Precision
;
353 colDefs
[i
].CbValue
= 0;
355 case DB_DATA_TYPE_FLOAT
:
356 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
357 precision
= pDb
->typeInfFloat
.Precision
;
358 scale
= pDb
->typeInfFloat
.MaximumScale
;
359 // SQL Sybase Anywhere v5.5 returned a negative number for the
360 // MaxScale. This caused ODBC to kick out an error on ibscale.
361 // I check for this here and set the scale = precision.
363 // scale = (short) precision;
364 colDefs
[i
].CbValue
= 0;
366 case DB_DATA_TYPE_DATE
:
367 fSqlType
= pDb
->typeInfDate
.FsqlType
;
368 precision
= pDb
->typeInfDate
.Precision
;
370 colDefs
[i
].CbValue
= 0;
376 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
377 colDefs
[i
].Null
= FALSE
;
379 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
380 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
381 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
382 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
385 // Completed successfully
388 } // wxTable::bindInsertParams()
391 /********** wxTable::bindUpdateParams() **********/
392 bool wxTable::bindUpdateParams(void)
399 UDWORD precision
= 0;
402 // Bind each UPDATEABLE column of the table to a parameter marker
404 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
406 if (! colDefs
[i
].Updateable
)
408 switch(colDefs
[i
].DbDataType
)
410 case DB_DATA_TYPE_VARCHAR
:
411 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
412 precision
= colDefs
[i
].SzDataObj
;
414 colDefs
[i
].CbValue
= SQL_NTS
;
416 case DB_DATA_TYPE_INTEGER
:
417 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
418 precision
= pDb
->typeInfInteger
.Precision
;
420 colDefs
[i
].CbValue
= 0;
422 case DB_DATA_TYPE_FLOAT
:
423 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
424 precision
= pDb
->typeInfFloat
.Precision
;
425 scale
= pDb
->typeInfFloat
.MaximumScale
;
426 // SQL Sybase Anywhere v5.5 returned a negative number for the
427 // MaxScale. This caused ODBC to kick out an error on ibscale.
428 // I check for this here and set the scale = precision.
430 // scale = (short) precision;
431 colDefs
[i
].CbValue
= 0;
433 case DB_DATA_TYPE_DATE
:
434 fSqlType
= pDb
->typeInfDate
.FsqlType
;
435 precision
= pDb
->typeInfDate
.Precision
;
437 colDefs
[i
].CbValue
= 0;
440 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
441 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
442 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
443 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
446 // Completed successfully
449 } // wxTable::bindUpdateParams()
452 /********** wxTable::bindCols() **********/
453 bool wxTable::bindCols(HSTMT cursor
)
457 // Bind each column of the table to a memory address for fetching data
459 for (i
= 0; i
< noCols
; i
++)
461 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
462 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
463 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
466 // Completed successfully
469 } // wxTable::bindCols()
472 /********** wxTable::getRec() **********/
473 bool wxTable::getRec(UWORD fetchType
)
477 if (!pDb
->FwdOnlyCursors())
479 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
483 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
484 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
485 if (retcode
== SQL_NO_DATA_FOUND
)
488 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
492 // Fetch the next record from the record set
493 retcode
= SQLFetch(hstmt
);
494 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
496 if (retcode
== SQL_NO_DATA_FOUND
)
499 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
503 // Completed successfully
506 } // wxTable::getRec()
509 /********** wxTable::execDelete() **********/
510 bool wxTable::execDelete(const char *pSqlStmt
)
512 // Execute the DELETE statement
513 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
514 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
516 // Record deleted successfully
519 } // wxTable::execDelete()
522 /********** wxTable::execUpdate() **********/
523 bool wxTable::execUpdate(const char *pSqlStmt
)
525 // Execute the UPDATE statement
526 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
527 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
529 // Record deleted successfully
532 } // wxTable::execUpdate()
535 /********** wxTable::query() **********/
536 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
538 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
540 // Set the selectForUpdate member variable
542 // The user may wish to select for update, but the DBMS may not be capable
543 selectForUpdate
= CanSelectForUpdate();
545 selectForUpdate
= FALSE
;
547 // Set the SQL SELECT string
548 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
549 { // so generate a select statement.
550 GetSelectStmt(sqlStmt
, queryType
, distinct
);
551 pDb
->WriteSqlLog(sqlStmt
);
554 // Make sure the cursor is closed first
555 if (! CloseCursor(hstmt
))
558 // Execute the SQL SELECT statement
561 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
562 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
563 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
565 // Completed successfully
568 } // wxTable::query()
571 /***************************** PUBLIC FUNCTIONS *****************************/
574 /********** wxTable::Open() **********/
575 bool wxTable::Open(void)
581 // char sqlStmt[DB_MAX_STATEMENT_LEN];
585 // Verify that the table exists in the database
586 // if (!pDb->TableExists(tableName,pDb->GetUsername(),tablePath))
587 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
589 s
=new wxString("Table/view does not exist in the database");
590 if (*(pDb
->dbInf
.accessibleTables
) == 'Y')
592 (*s
)+=", or you have insufficient permissions.\n";
601 // Verify the user has rights to access the table.
602 // Shortcut boolean evaluation to optimize out call to TablePrivs
603 // Unfortunely this optimization doesn't seem to be reliable!
604 if (/* *(pDb->dbInf.accessibleTables) == 'N' && */
605 !pDb
->TablePrivileges(tableName
,"SELECT",NULL
,tablePath
))
606 s
= new wxString("Current logged in user has insufficient privileges to access this table.\n");
612 if (wxStrcmp(tablePath
,""))
613 p
.sprintf("Error opening '%s/%s'.\n",tablePath
,tableName
);
615 p
.sprintf("Error opening '%s'.\n", tableName
);
618 pDb
->LogError(p
.GetData());
623 // Bind the member variables for field exchange between
624 // the wxTable object and the ODBC record.
627 if (!bindInsertParams()) // Inserts
629 if (!bindUpdateParams()) // Updates
632 if (!bindCols(*hstmtDefault
)) // Selects
634 if (!bindCols(hstmtInternal
)) // Internal use only
637 * Do NOT bind the hstmtCount cursor!!!
640 // Build an insert statement using parameter markers
641 if (!queryOnly
&& noCols
> 0)
643 bool needComma
= FALSE
;
644 sqlStmt
.sprintf("INSERT INTO %s (", tableName
);
645 for (i
= 0; i
< noCols
; i
++)
647 if (! colDefs
[i
].InsertAllowed
)
651 sqlStmt
+= colDefs
[i
].ColName
;
655 sqlStmt
+= ") VALUES (";
656 for (i
= 0; i
< noCols
; i
++)
658 if (! colDefs
[i
].InsertAllowed
)
667 // pDb->WriteSqlLog(sqlStmt);
669 // Prepare the insert statement for execution
670 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
671 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
674 // Completed successfully
680 /********** wxTable::Query() **********/
681 bool wxTable::Query(bool forUpdate
, bool distinct
)
684 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
686 } // wxTable::Query()
689 /********** wxTable::QueryBySqlStmt() **********/
690 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
692 pDb
->WriteSqlLog(pSqlStmt
);
694 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
696 } // wxTable::QueryBySqlStmt()
699 /********** wxTable::QueryMatching() **********/
700 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
703 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
705 } // wxTable::QueryMatching()
708 /********** wxTable::QueryOnKeyFields() **********/
709 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
712 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
714 } // wxTable::QueryOnKeyFields()
717 /********** wxTable::GetPrev() **********/
718 bool wxTable::GetPrev(void)
720 if (pDb
->FwdOnlyCursors())
722 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxTable"));
726 return(getRec(SQL_FETCH_PRIOR
));
727 } // wxTable::GetPrev()
730 /********** wxTable::operator-- **********/
731 bool wxTable::operator--(int)
733 if (pDb
->FwdOnlyCursors())
735 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxTable"));
739 return(getRec(SQL_FETCH_PRIOR
));
740 } // wxTable::operator--
743 /********** wxTable::GetFirst() **********/
744 bool wxTable::GetFirst(void)
746 if (pDb
->FwdOnlyCursors())
748 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxTable"));
752 return(getRec(SQL_FETCH_FIRST
));
753 } // wxTable::GetFirst()
756 /********** wxTable::GetLast() **********/
757 bool wxTable::GetLast(void)
759 if (pDb
->FwdOnlyCursors())
761 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxTable"));
765 return(getRec(SQL_FETCH_LAST
));
766 } // wxTable::GetLast()
769 /********** wxTable::GetSelectStmt() **********/
770 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
772 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
776 // Build a select statement to query the database
777 wxStrcpy(pSqlStmt
, "SELECT ");
779 // SELECT DISTINCT values only?
781 wxStrcat(pSqlStmt
, "DISTINCT ");
783 // Was a FROM clause specified to join tables to the base table?
784 // Available for ::Query() only!!!
785 bool appendFromClause
= FALSE
;
786 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
787 appendFromClause
= TRUE
;
789 // Add the column list
791 for (i
= 0; i
< noCols
; i
++)
793 // If joining tables, the base table column names must be qualified to avoid ambiguity
794 if (appendFromClause
)
796 wxStrcat(pSqlStmt
, queryTableName
);
797 wxStrcat(pSqlStmt
, ".");
799 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
801 wxStrcat(pSqlStmt
, ",");
804 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
805 // the ROWID if querying distinct records. The rowid will always be unique.
806 if (!distinct
&& CanUpdByROWID())
808 // If joining tables, the base table column names must be qualified to avoid ambiguity
809 if (appendFromClause
)
811 wxStrcat(pSqlStmt
, ",");
812 wxStrcat(pSqlStmt
, queryTableName
);
813 wxStrcat(pSqlStmt
, ".ROWID");
816 wxStrcat(pSqlStmt
, ",ROWID");
819 // Append the FROM tablename portion
820 wxStrcat(pSqlStmt
, " FROM ");
821 wxStrcat(pSqlStmt
, queryTableName
);
823 // Sybase uses the HOLDLOCK keyword to lock a record during query.
824 // The HOLDLOCK keyword follows the table name in the from clause.
825 // Each table in the from clause must specify HOLDLOCK or
826 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
827 // is parsed but ignored in SYBASE Transact-SQL.
828 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
829 wxStrcat(pSqlStmt
, " HOLDLOCK");
831 if (appendFromClause
)
832 wxStrcat(pSqlStmt
, from
);
834 // Append the WHERE clause. Either append the where clause for the class
835 // or build a where clause. The typeOfSelect determines this.
838 case DB_SELECT_WHERE
:
839 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
841 wxStrcat(pSqlStmt
, " WHERE ");
842 wxStrcat(pSqlStmt
, where
);
845 case DB_SELECT_KEYFIELDS
:
846 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
847 if (wxStrlen(whereClause
))
849 wxStrcat(pSqlStmt
, " WHERE ");
850 wxStrcat(pSqlStmt
, whereClause
);
853 case DB_SELECT_MATCHING
:
854 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
855 if (wxStrlen(whereClause
))
857 wxStrcat(pSqlStmt
, " WHERE ");
858 wxStrcat(pSqlStmt
, whereClause
);
863 // Append the ORDER BY clause
864 if (orderBy
&& wxStrlen(orderBy
))
866 wxStrcat(pSqlStmt
, " ORDER BY ");
867 wxStrcat(pSqlStmt
, orderBy
);
870 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
871 // parses the FOR UPDATE clause but ignores it. See the comment above on the
872 // HOLDLOCK for Sybase.
873 if (selectForUpdate
&& CanSelectForUpdate())
874 wxStrcat(pSqlStmt
, " FOR UPDATE");
876 } // wxTable::GetSelectStmt()
879 /********** wxTable::GetRowNum() **********/
880 UWORD
wxTable::GetRowNum(void)
884 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
886 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
890 // Completed successfully
891 return((UWORD
) rowNum
);
893 } // wxTable::GetRowNum()
896 /********** wxTable::CloseCursor() **********/
897 bool wxTable::CloseCursor(HSTMT cursor
)
899 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
900 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
902 // Completed successfully
905 } // wxTable::CloseCursor()
908 /********** wxTable::CreateTable() **********/
909 bool wxTable::CreateTable(bool attemptDrop
)
915 // char sqlStmt[DB_MAX_STATEMENT_LEN];
918 #ifdef DBDEBUG_CONSOLE
919 cout
<< "Creating Table " << tableName
<< "..." << endl
;
923 if (attemptDrop
&& !DropTable())
927 #ifdef DBDEBUG_CONSOLE
928 for (i
= 0; i
< noCols
; i
++)
930 // Exclude derived columns since they are NOT part of the base table
931 if (colDefs
[i
].DerivedCol
)
933 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
934 switch(colDefs
[i
].DbDataType
)
936 case DB_DATA_TYPE_VARCHAR
:
937 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
939 case DB_DATA_TYPE_INTEGER
:
940 cout
<< pDb
->typeInfInteger
.TypeName
;
942 case DB_DATA_TYPE_FLOAT
:
943 cout
<< pDb
->typeInfFloat
.TypeName
;
945 case DB_DATA_TYPE_DATE
:
946 cout
<< pDb
->typeInfDate
.TypeName
;
953 // Build a CREATE TABLE string from the colDefs structure.
954 bool needComma
= FALSE
;
955 sqlStmt
.sprintf("CREATE TABLE %s (", tableName
);
957 for (i
= 0; i
< noCols
; i
++)
959 // Exclude derived columns since they are NOT part of the base table
960 if (colDefs
[i
].DerivedCol
)
966 sqlStmt
+= colDefs
[i
].ColName
;
969 switch(colDefs
[i
].DbDataType
)
971 case DB_DATA_TYPE_VARCHAR
:
972 sqlStmt
+= pDb
->typeInfVarchar
.TypeName
; break;
973 case DB_DATA_TYPE_INTEGER
:
974 sqlStmt
+= pDb
->typeInfInteger
.TypeName
; break;
975 case DB_DATA_TYPE_FLOAT
:
976 sqlStmt
+= pDb
->typeInfFloat
.TypeName
; break;
977 case DB_DATA_TYPE_DATE
:
978 sqlStmt
+= pDb
->typeInfDate
.TypeName
; break;
980 // For varchars, append the size of the string
981 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
984 // wxStrcat(sqlStmt, "(");
985 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
986 // wxStrcat(sqlStmt, ")");
987 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
988 sqlStmt
+= s
.GetData();
991 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
993 if (colDefs
[i
].KeyField
)
995 sqlStmt
+= " NOT NULL";
1001 // If there is a primary key defined, include it in the create statement
1002 for (i
= j
= 0; i
< noCols
; i
++)
1004 if (colDefs
[i
].KeyField
)
1010 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1012 if (pDb
->Dbms() != dbmsMY_SQL
)
1014 sqlStmt
+= ",CONSTRAINT ";
1015 sqlStmt
+= tableName
;
1016 sqlStmt
+= "_PIDX PRIMARY KEY (";
1020 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1021 sqlStmt
+= ", PRIMARY KEY (";
1024 // List column name(s) of column(s) comprising the primary key
1025 for (i
= j
= 0; i
< noCols
; i
++)
1027 if (colDefs
[i
].KeyField
)
1029 if (j
++) // Multi part key, comma separate names
1031 sqlStmt
+= colDefs
[i
].ColName
;
1036 // Append the closing parentheses for the create table statement
1039 pDb
->WriteSqlLog(sqlStmt
.GetData());
1041 #ifdef DBDEBUG_CONSOLE
1042 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1045 // Execute the CREATE TABLE statement
1046 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
);
1047 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1049 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1050 pDb
->RollbackTrans();
1055 // Commit the transaction and close the cursor
1056 if (! pDb
->CommitTrans())
1058 if (! CloseCursor(hstmt
))
1061 // Database table created successfully
1064 } // wxTable::CreateTable()
1067 /********** wxTable::DropTable() **********/
1068 bool wxTable::DropTable()
1070 // NOTE: This function returns TRUE if the Table does not exist, but
1071 // only for identified databases. Code will need to be added
1072 // below for any other databases when those databases are defined
1073 // to handle this situation consistently
1075 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1078 sqlStmt
.sprintf("DROP TABLE %s", tableName
);
1080 pDb
->WriteSqlLog(sqlStmt
.GetData());
1082 #ifdef DBDEBUG_CONSOLE
1083 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1086 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1088 // Check for "Base table not found" error and ignore
1089 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1090 if (wxStrcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
1092 // Check for product specific error codes
1093 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
1094 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"S1000")) || // untested
1095 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01")))) // untested
1097 pDb
->DispNextError();
1098 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1099 pDb
->RollbackTrans();
1106 // Commit the transaction and close the cursor
1107 if (! pDb
->CommitTrans())
1109 if (! CloseCursor(hstmt
))
1113 } // wxTable::DropTable()
1116 /********** wxTable::CreateIndex() **********/
1117 bool wxTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
1119 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1122 // Drop the index first
1123 if (attemptDrop
&& !DropIndex(idxName
))
1126 // Build a CREATE INDEX statement
1127 sqlStmt
= "CREATE ";
1129 sqlStmt
+= "UNIQUE ";
1131 sqlStmt
+= "INDEX ";
1134 sqlStmt
+= tableName
;
1137 // Append list of columns making up index
1139 for (i
= 0; i
< noIdxCols
; i
++)
1141 sqlStmt
+= pIdxDefs
[i
].ColName
;
1142 /* Postgres doesn't cope with ASC */
1143 if (pDb
->Dbms() != dbmsPOSTGRES
)
1145 if (pIdxDefs
[i
].Ascending
)
1151 if ((i
+ 1) < noIdxCols
)
1155 // Append closing parentheses
1158 pDb
->WriteSqlLog(sqlStmt
.GetData());
1160 #ifdef DBDEBUG_CONSOLE
1161 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1164 // Execute the CREATE INDEX statement
1165 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1167 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1168 pDb
->RollbackTrans();
1173 // Commit the transaction and close the cursor
1174 if (! pDb
->CommitTrans())
1176 if (! CloseCursor(hstmt
))
1179 // Index Created Successfully
1182 } // wxTable::CreateIndex()
1185 /********** wxTable::DropIndex() **********/
1186 bool wxTable::DropIndex(const char * idxName
)
1188 // NOTE: This function returns TRUE if the Index does not exist, but
1189 // only for identified databases. Code will need to be added
1190 // below for any other databases when those databases are defined
1191 // to handle this situation consistently
1193 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1196 if (pDb
->Dbms() == dbmsACCESS
)
1197 sqlStmt
.sprintf("DROP INDEX %s ON %s",idxName
,tableName
);
1198 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1199 sqlStmt
.sprintf("DROP INDEX %s.%s",tableName
,idxName
);
1201 sqlStmt
.sprintf("DROP INDEX %s",idxName
);
1203 pDb
->WriteSqlLog(sqlStmt
.GetData());
1205 #ifdef DBDEBUG_CONSOLE
1206 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1209 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1211 // Check for "Index not found" error and ignore
1212 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1213 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1215 // Check for product specific error codes
1216 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1217 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1218 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S02")) // untested
1221 pDb
->DispNextError();
1222 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1223 pDb
->RollbackTrans();
1230 // Commit the transaction and close the cursor
1231 if (! pDb
->CommitTrans())
1233 if (! CloseCursor(hstmt
))
1237 } // wxTable::DropIndex()
1240 /********** wxTable::Insert() **********/
1241 int wxTable::Insert(void)
1249 // Insert the record by executing the already prepared insert statement
1251 retcode
=SQLExecute(hstmtInsert
);
1252 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1254 // Check to see if integrity constraint was violated
1255 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1256 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1257 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1260 pDb
->DispNextError();
1261 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1266 // Record inserted into the datasource successfully
1269 } // wxTable::Insert()
1272 /********** wxTable::Update() **********/
1273 bool wxTable::Update(void)
1279 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1281 // Build the SQL UPDATE statement
1282 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1284 pDb
->WriteSqlLog(sqlStmt
);
1286 #ifdef DBDEBUG_CONSOLE
1287 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1290 // Execute the SQL UPDATE statement
1291 return(execUpdate(sqlStmt
));
1293 } // wxTable::Update()
1296 /********** wxTable::Update(pSqlStmt) **********/
1297 bool wxTable::Update(const char *pSqlStmt
)
1303 pDb
->WriteSqlLog(pSqlStmt
);
1305 return(execUpdate(pSqlStmt
));
1307 } // wxTable::Update(pSqlStmt)
1310 /********** wxTable::UpdateWhere() **********/
1311 bool wxTable::UpdateWhere(const char *pWhereClause
)
1317 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1319 // Build the SQL UPDATE statement
1320 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1322 pDb
->WriteSqlLog(sqlStmt
);
1324 #ifdef DBDEBUG_CONSOLE
1325 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1328 // Execute the SQL UPDATE statement
1329 return(execUpdate(sqlStmt
));
1331 } // wxTable::UpdateWhere()
1334 /********** wxTable::Delete() **********/
1335 bool wxTable::Delete(void)
1341 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1343 // Build the SQL DELETE statement
1344 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1346 pDb
->WriteSqlLog(sqlStmt
);
1348 // Execute the SQL DELETE statement
1349 return(execDelete(sqlStmt
));
1351 } // wxTable::Delete()
1354 /********** wxTable::DeleteWhere() **********/
1355 bool wxTable::DeleteWhere(const char *pWhereClause
)
1361 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1363 // Build the SQL DELETE statement
1364 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1366 pDb
->WriteSqlLog(sqlStmt
);
1368 // Execute the SQL DELETE statement
1369 return(execDelete(sqlStmt
));
1371 } // wxTable::DeleteWhere()
1374 /********** wxTable::DeleteMatching() **********/
1375 bool wxTable::DeleteMatching(void)
1381 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1383 // Build the SQL DELETE statement
1384 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1386 pDb
->WriteSqlLog(sqlStmt
);
1388 // Execute the SQL DELETE statement
1389 return(execDelete(sqlStmt
));
1391 } // wxTable::DeleteMatching()
1394 /********** wxTable::GetUpdateStmt() **********/
1395 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1401 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1402 bool firstColumn
= TRUE
;
1405 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1407 // Append a list of columns to be updated
1409 for (i
= 0; i
< noCols
; i
++)
1411 // Only append Updateable columns
1412 if (colDefs
[i
].Updateable
)
1415 wxStrcat(pSqlStmt
, ",");
1417 firstColumn
= FALSE
;
1418 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1419 wxStrcat(pSqlStmt
, " = ?");
1423 // Append the WHERE clause to the SQL UPDATE statement
1424 wxStrcat(pSqlStmt
, " WHERE ");
1427 case DB_UPD_KEYFIELDS
:
1428 // If the datasource supports the ROWID column, build
1429 // the where on ROWID for efficiency purposes.
1430 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1431 if (CanUpdByROWID())
1434 char rowid
[ROWID_LEN
];
1436 // Get the ROWID value. If not successful retreiving the ROWID,
1437 // simply fall down through the code and build the WHERE clause
1438 // based on the key fields.
1439 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1441 wxStrcat(pSqlStmt
, "ROWID = '");
1442 wxStrcat(pSqlStmt
, rowid
);
1443 wxStrcat(pSqlStmt
, "'");
1447 // Unable to delete by ROWID, so build a WHERE
1448 // clause based on the keyfields.
1449 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1450 wxStrcat(pSqlStmt
, whereClause
);
1453 wxStrcat(pSqlStmt
, pWhereClause
);
1456 } // GetUpdateStmt()
1459 /********** wxTable::GetDeleteStmt() **********/
1460 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1466 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1470 // Handle the case of DeleteWhere() and the where clause is blank. It should
1471 // delete all records from the database in this case.
1472 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1474 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1478 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1480 // Append the WHERE clause to the SQL DELETE statement
1483 case DB_DEL_KEYFIELDS
:
1484 // If the datasource supports the ROWID column, build
1485 // the where on ROWID for efficiency purposes.
1486 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1487 if (CanUpdByROWID())
1490 char rowid
[ROWID_LEN
];
1492 // Get the ROWID value. If not successful retreiving the ROWID,
1493 // simply fall down through the code and build the WHERE clause
1494 // based on the key fields.
1495 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1497 wxStrcat(pSqlStmt
, "ROWID = '");
1498 wxStrcat(pSqlStmt
, rowid
);
1499 wxStrcat(pSqlStmt
, "'");
1503 // Unable to delete by ROWID, so build a WHERE
1504 // clause based on the keyfields.
1505 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1506 wxStrcat(pSqlStmt
, whereClause
);
1509 wxStrcat(pSqlStmt
, pWhereClause
);
1511 case DB_DEL_MATCHING
:
1512 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1513 wxStrcat(pSqlStmt
, whereClause
);
1517 } // GetDeleteStmt()
1520 /********** wxTable::GetWhereClause() **********/
1521 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
)
1523 * Note: GetWhereClause() currently ignores timestamp columns.
1524 * They are not included as part of the where clause.
1527 bool moreThanOneColumn
= FALSE
;
1530 // Loop through the columns building a where clause as you go
1532 for (i
= 0; i
< noCols
; i
++)
1534 // Determine if this column should be included in the WHERE clause
1535 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1536 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1538 // Skip over timestamp columns
1539 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1541 // If there is more than 1 column, join them with the keyword "AND"
1542 if (moreThanOneColumn
)
1543 wxStrcat(pWhereClause
, " AND ");
1545 moreThanOneColumn
= TRUE
;
1546 // Concatenate where phrase for the column
1547 if (qualTableName
&& wxStrlen(qualTableName
))
1549 wxStrcat(pWhereClause
, qualTableName
);
1550 wxStrcat(pWhereClause
, ".");
1552 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1553 wxStrcat(pWhereClause
, " = ");
1554 switch(colDefs
[i
].SqlCtype
)
1557 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1560 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1563 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1566 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1569 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1572 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1575 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1578 wxStrcat(pWhereClause
, colValue
);
1581 } // wxTable::GetWhereClause()
1584 /********** wxTable::IsColNull() **********/
1585 bool wxTable::IsColNull(int colNo
)
1587 switch(colDefs
[colNo
].SqlCtype
)
1590 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1592 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1594 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1596 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1598 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1600 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1602 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1603 case SQL_C_TIMESTAMP
:
1604 TIMESTAMP_STRUCT
*pDt
;
1605 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1606 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1613 } // wxTable::IsColNull()
1616 /********** wxTable::CanSelectForUpdate() **********/
1617 bool wxTable::CanSelectForUpdate(void)
1619 if (pDb
->Dbms() == dbmsMY_SQL
)
1622 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1627 } // wxTable::CanSelectForUpdate()
1630 /********** wxTable::CanUpdByROWID() **********/
1631 bool wxTable::CanUpdByROWID(void)
1634 * NOTE: Returning FALSE for now until this can be debugged,
1635 * as the ROWID is not getting updated correctly
1639 if (pDb
->Dbms() == dbmsORACLE
)
1644 } // wxTable::CanUpdByROWID()
1647 /********** wxTable::IsCursorClosedOnCommit() **********/
1648 bool wxTable::IsCursorClosedOnCommit(void)
1650 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1655 } // wxTable::IsCursorClosedOnCommit()
1658 /********** wxTable::ClearMemberVars() **********/
1659 void wxTable::ClearMemberVars(void)
1661 // Loop through the columns setting each member variable to zero
1663 for (i
= 0; i
< noCols
; i
++)
1665 switch(colDefs
[i
].SqlCtype
)
1668 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1671 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1674 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1677 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1680 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1683 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1686 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1688 case SQL_C_TIMESTAMP
:
1689 TIMESTAMP_STRUCT
*pDt
;
1690 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1702 } // wxTable::ClearMemberVars()
1705 /********** wxTable::SetQueryTimeout() **********/
1706 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1708 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1709 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1710 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1711 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1712 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1713 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1714 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1715 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1717 // Completed Successfully
1720 } // wxTable::SetQueryTimeout()
1723 /********** wxTable::SetColDefs() **********/
1724 void wxTable::SetColDefs (int index
, const char *fieldName
, int dataType
, void *pData
,
1725 int cType
, int size
, bool keyField
, bool upd
,
1726 bool insAllow
, bool derivedCol
)
1728 if (!colDefs
) // May happen if the database connection fails
1731 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1733 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1734 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1737 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
1739 colDefs
[index
].DbDataType
= dataType
;
1740 colDefs
[index
].PtrDataObj
= pData
;
1741 colDefs
[index
].SqlCtype
= cType
;
1742 colDefs
[index
].SzDataObj
= size
;
1743 colDefs
[index
].KeyField
= keyField
;
1744 colDefs
[index
].DerivedCol
= derivedCol
;
1745 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1748 colDefs
[index
].Updateable
= FALSE
;
1749 colDefs
[index
].InsertAllowed
= FALSE
;
1753 colDefs
[index
].Updateable
= upd
;
1754 colDefs
[index
].InsertAllowed
= insAllow
;
1757 colDefs
[index
].Null
= FALSE
;
1759 } // wxTable::SetColDefs()
1762 /********** wxTable::SetColDef() **********/
1763 // BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
1764 //bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
1765 wxColDataPtr
* wxTable::SetColDefs (wxColInf
*pColInfs
, ULONG numCols
)
1768 wxColDataPtr
*pColDataPtrs
= NULL
;
1775 pColDataPtrs
= new wxColDataPtr
[numCols
+1];
1777 for (index
= 0; index
< numCols
; index
++)
1781 title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
1782 msg.sprintf("Column name: %s\nData type: %04d\nType name: %s\nColumn size: %d\nBuffer len: %d\nDecimals:%d\nRadix: %d\nNullable: %d\nRemarks: %s",
1783 pColInfs[index].colName,pColInfs[index].sqlDataType,pColInfs[index].typeName,pColInfs[index].columnSize,pColInfs[index].bufferLength,pColInfs[index].decimalDigits,pColInfs[index].numPrecRadix,pColInfs[index].nullable,pColInfs[index].remarks);
1784 msg += " \nDB_DATA_TYPE: ";
1785 switch(pColInfs[index].dbDataType)
1787 case DB_DATA_TYPE_VARCHAR:
1788 msg += pDb->typeInfVarchar.TypeName; break;
1789 case DB_DATA_TYPE_INTEGER:
1790 msg += pDb->typeInfInteger.TypeName; break;
1791 case DB_DATA_TYPE_FLOAT:
1792 msg += pDb->typeInfFloat.TypeName; break;
1793 case DB_DATA_TYPE_DATE:
1794 msg += pDb->typeInfDate.TypeName; break;
1796 wxMessageBox(msg.GetData(),title.GetData());
1798 // Process the fields
1799 switch (pColInfs
[index
].dbDataType
)
1801 case DB_DATA_TYPE_VARCHAR
:
1803 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].bufferLength
+1];
1804 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferLength
;
1805 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
1808 case DB_DATA_TYPE_INTEGER
:
1810 // Can be long or short
1811 if (pColInfs
[index
].bufferLength
== sizeof(long))
1813 pColDataPtrs
[index
].PtrDataObj
= new long;
1814 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
1815 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
1819 pColDataPtrs
[index
].PtrDataObj
= new short;
1820 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
1821 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
1825 case DB_DATA_TYPE_FLOAT
:
1827 // Can be float or double
1828 if (pColInfs
[index
].bufferLength
== sizeof(float))
1830 pColDataPtrs
[index
].PtrDataObj
= new float;
1831 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
1832 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
1836 pColDataPtrs
[index
].PtrDataObj
= new double;
1837 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
1838 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
1842 case DB_DATA_TYPE_DATE
:
1844 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
1845 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
1846 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
1851 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
1854 return (pColDataPtrs
);
1855 } // wxTable::SetColDef()
1858 /********** wxTable::SetCursor() **********/
1859 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1861 if (hstmtActivate
== DEFAULT_CURSOR
)
1862 hstmt
= *hstmtDefault
;
1864 hstmt
= *hstmtActivate
;
1866 } // wxTable::SetCursor()
1869 /********** wxTable::Count(const char *) **********/
1870 ULONG
wxTable::Count(const char *args
)
1873 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1877 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1878 sqlStmt
= "SELECT COUNT(";
1880 sqlStmt
+= ") FROM ";
1881 sqlStmt
+= queryTableName
;
1883 if (from
&& wxStrlen(from
))
1886 // Add the where clause if one is provided
1887 if (where
&& wxStrlen(where
))
1889 sqlStmt
+= " WHERE ";
1893 pDb
->WriteSqlLog(sqlStmt
.GetData());
1895 // Initialize the Count cursor if it's not already initialized
1898 hstmtCount
= NewCursor(FALSE
,FALSE
);
1904 // Execute the SQL statement
1905 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1907 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1912 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1914 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1918 // Obtain the result
1919 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1921 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1926 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1927 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1929 // Return the record count
1932 } // wxTable::Count()
1935 /********** wxTable::Refresh() **********/
1936 bool wxTable::Refresh(void)
1940 // Switch to the internal cursor so any active cursors are not corrupted
1941 HSTMT currCursor
= GetCursor();
1942 hstmt
= hstmtInternal
;
1944 // Save the where and order by clauses
1945 char *saveWhere
= where
;
1946 char *saveOrderBy
= orderBy
;
1948 // Build a where clause to refetch the record with. Try and use the
1949 // ROWID if it's available, ow use the key fields.
1950 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1951 wxStrcpy(whereClause
, "");
1952 if (CanUpdByROWID())
1955 char rowid
[ROWID_LEN
+1];
1957 // Get the ROWID value. If not successful retreiving the ROWID,
1958 // simply fall down through the code and build the WHERE clause
1959 // based on the key fields.
1960 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1962 wxStrcat(whereClause
, queryTableName
);
1963 wxStrcat(whereClause
, ".ROWID = '");
1964 wxStrcat(whereClause
, rowid
);
1965 wxStrcat(whereClause
, "'");
1969 // If unable to use the ROWID, build a where clause from the keyfields
1970 if (wxStrlen(whereClause
) == 0)
1971 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1973 // Requery the record
1974 where
= whereClause
;
1979 if (result
&& !GetNext())
1982 // Switch back to original cursor
1983 SetCursor(&currCursor
);
1985 // Free the internal cursor
1986 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1987 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1989 // Restore the original where and order by clauses
1991 orderBy
= saveOrderBy
;
1995 } // wxTable::Refresh()
1998 /********** wxTable::SetNull(int colNo) **********/
1999 bool wxTable::SetNull(int colNo
)
2002 return(colDefs
[colNo
].Null
= TRUE
);
2006 } // wxTable::SetNull(int colNo)
2009 /********** wxTable::SetNull(char *colName) **********/
2010 bool wxTable::SetNull(const char *colName
)
2013 for (i
= 0; i
< noCols
; i
++)
2015 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2020 return(colDefs
[i
].Null
= TRUE
);
2024 } // wxTable::SetNull(char *colName)
2027 /********** wxTable::NewCursor() **********/
2028 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
2030 HSTMT
*newHSTMT
= new HSTMT
;
2035 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2037 pDb
->DispAllErrors(henv
, hdbc
);
2042 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2044 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2051 if(!bindCols(*newHSTMT
))
2059 SetCursor(newHSTMT
);
2063 } // wxTable::NewCursor()
2066 /********** wxTable::DeleteCursor() **********/
2067 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
2071 if (!hstmtDel
) // Cursor already deleted
2074 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2076 pDb
->DispAllErrors(henv
, hdbc
);
2084 } // wxTable::DeleteCursor()
2086 #endif // wxUSE_ODBC