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
134 if (qryTblName
) // Name of the table/view to query
135 wxStrcpy(queryTableName
, qryTblName
);
137 wxStrcpy(queryTableName
, tblName
);
145 tableID
= ++lastTableID
;
146 s
.sprintf("wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName
,tableID
,pDb
);
149 CstructTablesInUse
*tableInUse
;
150 tableInUse
= new CstructTablesInUse();
151 tableInUse
->tableName
= tblName
;
152 tableInUse
->tableID
= tableID
;
153 tableInUse
->pDb
= pDb
;
154 TablesInUse
.Append(tableInUse
);
157 pDb
->WriteSqlLog(s
.GetData());
159 // Grab the HENV and HDBC from the wxDB object
163 // Allocate space for column definitions
165 colDefs
= new wxColDef
[noCols
]; // Points to the first column defintion
167 // Allocate statement handles for the table
170 // Allocate a separate statement handle for performing inserts
171 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
172 pDb
->DispAllErrors(henv
, hdbc
);
173 // Allocate a separate statement handle for performing deletes
174 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
175 pDb
->DispAllErrors(henv
, hdbc
);
176 // Allocate a separate statement handle for performing updates
177 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
178 pDb
->DispAllErrors(henv
, hdbc
);
180 // Allocate a separate statement handle for internal use
181 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
182 pDb
->DispAllErrors(henv
, hdbc
);
184 // Set the cursor type for the statement handles
185 cursorType
= SQL_CURSOR_STATIC
;
186 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
188 // Check to see if cursor type is supported
189 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
190 if (! wxStrcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
192 // Datasource does not support static cursors. Driver
193 // will substitute a cursor type. Call SQLGetStmtOption()
194 // to determine which cursor type was selected.
195 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
196 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
197 #ifdef DBDEBUG_CONSOLE
198 cout
<< "Static cursor changed to: ";
201 case SQL_CURSOR_FORWARD_ONLY
:
202 cout
<< "Forward Only"; break;
203 case SQL_CURSOR_STATIC
:
204 cout
<< "Static"; break;
205 case SQL_CURSOR_KEYSET_DRIVEN
:
206 cout
<< "Keyset Driven"; break;
207 case SQL_CURSOR_DYNAMIC
:
208 cout
<< "Dynamic"; break;
210 cout
<< endl
<< endl
;
215 pDb
->DispNextError();
216 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
219 #ifdef DBDEBUG_CONSOLE
221 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
226 // Set the cursor type for the INSERT statement handle
227 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
228 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
229 // Set the cursor type for the DELETE statement handle
230 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
231 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
232 // Set the cursor type for the UPDATE statement handle
233 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
234 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
237 // Make the default cursor the active cursor
238 hstmtDefault
= NewCursor(FALSE
,FALSE
);
239 assert(hstmtDefault
);
240 hstmt
= *hstmtDefault
;
242 } // wxTable::wxTable()
245 /********** wxTable::~wxTable() **********/
251 s
.sprintf("wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName
,tableID
,pDb
);
252 pDb
->WriteSqlLog(s
.GetData());
261 pNode
= TablesInUse
.First();
262 while (pNode
&& !found
)
264 if (((CstructTablesInUse
*)pNode
->Data())->tableID
== tableID
)
267 if (!TablesInUse
.DeleteNode(pNode
))
268 wxMessageBox (s
.GetData(),"Unable to delete node!");
271 pNode
= pNode
->Next();
276 msg
.sprintf("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s
.GetData());
277 wxMessageBox (msg
.GetData(),"NOTICE...");
282 // Decrement the wxDB table count
286 // Delete memory allocated for column definitions
290 // Free statement handles
294 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
295 pDb
->DispAllErrors(henv
, hdbc
);
297 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
298 pDb
->DispAllErrors(henv
, hdbc
);
300 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
301 pDb
->DispAllErrors(henv
, hdbc
);
304 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
305 pDb
->DispAllErrors(henv
, hdbc
);
307 // Delete dynamically allocated cursors
309 DeleteCursor(hstmtDefault
);
311 DeleteCursor(hstmtCount
);
313 } // wxTable::~wxTable()
317 /***************************** PRIVATE FUNCTIONS *****************************/
321 /********** wxTable::bindInsertParams() **********/
322 bool wxTable::bindInsertParams(void)
329 UDWORD precision
= 0;
332 // Bind each column (that can be inserted) of the table to a parameter marker
334 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
336 if (! colDefs
[i
].InsertAllowed
)
338 switch(colDefs
[i
].DbDataType
)
340 case DB_DATA_TYPE_VARCHAR
:
341 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
342 precision
= colDefs
[i
].SzDataObj
;
344 colDefs
[i
].CbValue
= SQL_NTS
;
346 case DB_DATA_TYPE_INTEGER
:
347 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
348 precision
= pDb
->typeInfInteger
.Precision
;
350 colDefs
[i
].CbValue
= 0;
352 case DB_DATA_TYPE_FLOAT
:
353 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
354 precision
= pDb
->typeInfFloat
.Precision
;
355 scale
= pDb
->typeInfFloat
.MaximumScale
;
356 // SQL Sybase Anywhere v5.5 returned a negative number for the
357 // MaxScale. This caused ODBC to kick out an error on ibscale.
358 // I check for this here and set the scale = precision.
360 // scale = (short) precision;
361 colDefs
[i
].CbValue
= 0;
363 case DB_DATA_TYPE_DATE
:
364 fSqlType
= pDb
->typeInfDate
.FsqlType
;
365 precision
= pDb
->typeInfDate
.Precision
;
367 colDefs
[i
].CbValue
= 0;
373 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
374 colDefs
[i
].Null
= FALSE
;
376 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
377 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
378 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
379 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
382 // Completed successfully
385 } // wxTable::bindInsertParams()
388 /********** wxTable::bindUpdateParams() **********/
389 bool wxTable::bindUpdateParams(void)
396 UDWORD precision
= 0;
399 // Bind each UPDATEABLE column of the table to a parameter marker
401 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
403 if (! colDefs
[i
].Updateable
)
405 switch(colDefs
[i
].DbDataType
)
407 case DB_DATA_TYPE_VARCHAR
:
408 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
409 precision
= colDefs
[i
].SzDataObj
;
411 colDefs
[i
].CbValue
= SQL_NTS
;
413 case DB_DATA_TYPE_INTEGER
:
414 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
415 precision
= pDb
->typeInfInteger
.Precision
;
417 colDefs
[i
].CbValue
= 0;
419 case DB_DATA_TYPE_FLOAT
:
420 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
421 precision
= pDb
->typeInfFloat
.Precision
;
422 scale
= pDb
->typeInfFloat
.MaximumScale
;
423 // SQL Sybase Anywhere v5.5 returned a negative number for the
424 // MaxScale. This caused ODBC to kick out an error on ibscale.
425 // I check for this here and set the scale = precision.
427 // scale = (short) precision;
428 colDefs
[i
].CbValue
= 0;
430 case DB_DATA_TYPE_DATE
:
431 fSqlType
= pDb
->typeInfDate
.FsqlType
;
432 precision
= pDb
->typeInfDate
.Precision
;
434 colDefs
[i
].CbValue
= 0;
437 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
438 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
439 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
440 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
443 // Completed successfully
446 } // wxTable::bindUpdateParams()
449 /********** wxTable::bindCols() **********/
450 bool wxTable::bindCols(HSTMT cursor
)
454 // Bind each column of the table to a memory address for fetching data
456 for (i
= 0; i
< noCols
; i
++)
458 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
459 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
460 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
463 // Completed successfully
466 } // wxTable::bindCols()
469 /********** wxTable::getRec() **********/
470 bool wxTable::getRec(UWORD fetchType
)
474 if (!pDb
->FwdOnlyCursors())
476 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
480 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
481 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
482 if (retcode
== SQL_NO_DATA_FOUND
)
485 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
489 // Fetch the next record from the record set
490 retcode
= SQLFetch(hstmt
);
491 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
493 if (retcode
== SQL_NO_DATA_FOUND
)
496 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
500 // Completed successfully
503 } // wxTable::getRec()
506 /********** wxTable::execDelete() **********/
507 bool wxTable::execDelete(const char *pSqlStmt
)
509 // Execute the DELETE statement
510 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
511 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
513 // Record deleted successfully
516 } // wxTable::execDelete()
519 /********** wxTable::execUpdate() **********/
520 bool wxTable::execUpdate(const char *pSqlStmt
)
522 // Execute the UPDATE statement
523 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
524 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
526 // Record deleted successfully
529 } // wxTable::execUpdate()
532 /********** wxTable::query() **********/
533 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
535 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
537 // Set the selectForUpdate member variable
539 // The user may wish to select for update, but the DBMS may not be capable
540 selectForUpdate
= CanSelectForUpdate();
542 selectForUpdate
= FALSE
;
544 // Set the SQL SELECT string
545 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
546 { // so generate a select statement.
547 GetSelectStmt(sqlStmt
, queryType
, distinct
);
548 pDb
->WriteSqlLog(sqlStmt
);
551 // Make sure the cursor is closed first
552 if (! CloseCursor(hstmt
))
555 // Execute the SQL SELECT statement
558 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
559 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
560 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
562 // Completed successfully
565 } // wxTable::query()
568 /***************************** PUBLIC FUNCTIONS *****************************/
571 /********** wxTable::Open() **********/
572 bool wxTable::Open(void)
578 // char sqlStmt[DB_MAX_STATEMENT_LEN];
581 // Verify that the table exists in the database
582 if (!pDb
->TableExists(tableName
,pDb
->GetUsername(),tablePath
))
585 if (wxStrcmp(tablePath
,""))
586 s
.sprintf("Error opening '%s/%s'.\n",tablePath
,tableName
);
588 s
.sprintf("Error opening '%s'.\n", tableName
);
589 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
590 s
+= "Table/view does not exist in the database.\n";
592 s
+= "Current logged in user does not have sufficient privileges to access this table.\n";
593 pDb
->LogError(s
.GetData());
597 // Bind the member variables for field exchange between
598 // the wxTable object and the ODBC record.
601 if (!bindInsertParams()) // Inserts
603 if (!bindUpdateParams()) // Updates
606 if (!bindCols(*hstmtDefault
)) // Selects
608 if (!bindCols(hstmtInternal
)) // Internal use only
611 * Do NOT bind the hstmtCount cursor!!!
614 // Build an insert statement using parameter markers
615 if (!queryOnly
&& noCols
> 0)
617 bool needComma
= FALSE
;
618 sqlStmt
.sprintf("INSERT INTO %s (", tableName
);
619 for (i
= 0; i
< noCols
; i
++)
621 if (! colDefs
[i
].InsertAllowed
)
625 sqlStmt
+= colDefs
[i
].ColName
;
629 sqlStmt
+= ") VALUES (";
630 for (i
= 0; i
< noCols
; i
++)
632 if (! colDefs
[i
].InsertAllowed
)
641 // pDb->WriteSqlLog(sqlStmt);
643 // Prepare the insert statement for execution
644 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
645 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
648 // Completed successfully
654 /********** wxTable::Query() **********/
655 bool wxTable::Query(bool forUpdate
, bool distinct
)
658 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
660 } // wxTable::Query()
663 /********** wxTable::QueryBySqlStmt() **********/
664 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
666 pDb
->WriteSqlLog(pSqlStmt
);
668 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
670 } // wxTable::QueryBySqlStmt()
673 /********** wxTable::QueryMatching() **********/
674 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
677 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
679 } // wxTable::QueryMatching()
682 /********** wxTable::QueryOnKeyFields() **********/
683 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
686 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
688 } // wxTable::QueryOnKeyFields()
691 /********** wxTable::GetPrev() **********/
692 bool wxTable::GetPrev(void)
694 if (pDb
->FwdOnlyCursors())
696 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxTable"));
700 return(getRec(SQL_FETCH_PRIOR
));
701 } // wxTable::GetPrev()
704 /********** wxTable::operator-- **********/
705 bool wxTable::operator--(int)
707 if (pDb
->FwdOnlyCursors())
709 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxTable"));
713 return(getRec(SQL_FETCH_PRIOR
));
714 } // wxTable::operator--
717 /********** wxTable::GetFirst() **********/
718 bool wxTable::GetFirst(void)
720 if (pDb
->FwdOnlyCursors())
722 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxTable"));
726 return(getRec(SQL_FETCH_FIRST
));
727 } // wxTable::GetFirst()
730 /********** wxTable::GetLast() **********/
731 bool wxTable::GetLast(void)
733 if (pDb
->FwdOnlyCursors())
735 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxTable"));
739 return(getRec(SQL_FETCH_LAST
));
740 } // wxTable::GetLast()
743 /********** wxTable::GetSelectStmt() **********/
744 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
746 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
750 // Build a select statement to query the database
751 wxStrcpy(pSqlStmt
, "SELECT ");
753 // SELECT DISTINCT values only?
755 wxStrcat(pSqlStmt
, "DISTINCT ");
757 // Was a FROM clause specified to join tables to the base table?
758 // Available for ::Query() only!!!
759 bool appendFromClause
= FALSE
;
760 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
761 appendFromClause
= TRUE
;
763 // Add the column list
765 for (i
= 0; i
< noCols
; i
++)
767 // If joining tables, the base table column names must be qualified to avoid ambiguity
768 if (appendFromClause
)
770 wxStrcat(pSqlStmt
, queryTableName
);
771 wxStrcat(pSqlStmt
, ".");
773 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
775 wxStrcat(pSqlStmt
, ",");
778 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
779 // the ROWID if querying distinct records. The rowid will always be unique.
780 if (!distinct
&& CanUpdByROWID())
782 // If joining tables, the base table column names must be qualified to avoid ambiguity
783 if (appendFromClause
)
785 wxStrcat(pSqlStmt
, ",");
786 wxStrcat(pSqlStmt
, queryTableName
);
787 wxStrcat(pSqlStmt
, ".ROWID");
790 wxStrcat(pSqlStmt
, ",ROWID");
793 // Append the FROM tablename portion
794 wxStrcat(pSqlStmt
, " FROM ");
795 wxStrcat(pSqlStmt
, queryTableName
);
797 // Sybase uses the HOLDLOCK keyword to lock a record during query.
798 // The HOLDLOCK keyword follows the table name in the from clause.
799 // Each table in the from clause must specify HOLDLOCK or
800 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
801 // is parsed but ignored in SYBASE Transact-SQL.
802 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
803 wxStrcat(pSqlStmt
, " HOLDLOCK");
805 if (appendFromClause
)
806 wxStrcat(pSqlStmt
, from
);
808 // Append the WHERE clause. Either append the where clause for the class
809 // or build a where clause. The typeOfSelect determines this.
812 case DB_SELECT_WHERE
:
813 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
815 wxStrcat(pSqlStmt
, " WHERE ");
816 wxStrcat(pSqlStmt
, where
);
819 case DB_SELECT_KEYFIELDS
:
820 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
821 if (wxStrlen(whereClause
))
823 wxStrcat(pSqlStmt
, " WHERE ");
824 wxStrcat(pSqlStmt
, whereClause
);
827 case DB_SELECT_MATCHING
:
828 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
829 if (wxStrlen(whereClause
))
831 wxStrcat(pSqlStmt
, " WHERE ");
832 wxStrcat(pSqlStmt
, whereClause
);
837 // Append the ORDER BY clause
838 if (orderBy
&& wxStrlen(orderBy
))
840 wxStrcat(pSqlStmt
, " ORDER BY ");
841 wxStrcat(pSqlStmt
, orderBy
);
844 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
845 // parses the FOR UPDATE clause but ignores it. See the comment above on the
846 // HOLDLOCK for Sybase.
847 if (selectForUpdate
&& CanSelectForUpdate())
848 wxStrcat(pSqlStmt
, " FOR UPDATE");
850 } // wxTable::GetSelectStmt()
853 /********** wxTable::GetRowNum() **********/
854 UWORD
wxTable::GetRowNum(void)
858 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
860 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
864 // Completed successfully
865 return((UWORD
) rowNum
);
867 } // wxTable::GetRowNum()
870 /********** wxTable::CloseCursor() **********/
871 bool wxTable::CloseCursor(HSTMT cursor
)
873 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
874 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
876 // Completed successfully
879 } // wxTable::CloseCursor()
882 /********** wxTable::CreateTable() **********/
883 bool wxTable::CreateTable(bool attemptDrop
)
889 // char sqlStmt[DB_MAX_STATEMENT_LEN];
892 #ifdef DBDEBUG_CONSOLE
893 cout
<< "Creating Table " << tableName
<< "..." << endl
;
897 if (attemptDrop
&& !DropTable())
901 #ifdef DBDEBUG_CONSOLE
902 for (i
= 0; i
< noCols
; i
++)
904 // Exclude derived columns since they are NOT part of the base table
905 if (colDefs
[i
].DerivedCol
)
907 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
908 switch(colDefs
[i
].DbDataType
)
910 case DB_DATA_TYPE_VARCHAR
:
911 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
913 case DB_DATA_TYPE_INTEGER
:
914 cout
<< pDb
->typeInfInteger
.TypeName
;
916 case DB_DATA_TYPE_FLOAT
:
917 cout
<< pDb
->typeInfFloat
.TypeName
;
919 case DB_DATA_TYPE_DATE
:
920 cout
<< pDb
->typeInfDate
.TypeName
;
927 // Build a CREATE TABLE string from the colDefs structure.
928 bool needComma
= FALSE
;
929 sqlStmt
.sprintf("CREATE TABLE %s (", tableName
);
931 for (i
= 0; i
< noCols
; i
++)
933 // Exclude derived columns since they are NOT part of the base table
934 if (colDefs
[i
].DerivedCol
)
940 sqlStmt
+= colDefs
[i
].ColName
;
943 switch(colDefs
[i
].DbDataType
)
945 case DB_DATA_TYPE_VARCHAR
:
946 sqlStmt
+= pDb
->typeInfVarchar
.TypeName
; break;
947 case DB_DATA_TYPE_INTEGER
:
948 sqlStmt
+= pDb
->typeInfInteger
.TypeName
; break;
949 case DB_DATA_TYPE_FLOAT
:
950 sqlStmt
+= pDb
->typeInfFloat
.TypeName
; break;
951 case DB_DATA_TYPE_DATE
:
952 sqlStmt
+= pDb
->typeInfDate
.TypeName
; break;
954 // For varchars, append the size of the string
955 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
958 // wxStrcat(sqlStmt, "(");
959 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
960 // wxStrcat(sqlStmt, ")");
961 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
962 sqlStmt
+= s
.GetData();
965 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
967 if (colDefs
[i
].KeyField
)
969 sqlStmt
+= " NOT NULL";
975 // If there is a primary key defined, include it in the create statement
976 for (i
= j
= 0; i
< noCols
; i
++)
978 if (colDefs
[i
].KeyField
)
984 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
986 if (pDb
->Dbms() != dbmsMY_SQL
)
988 sqlStmt
+= ",CONSTRAINT ";
989 sqlStmt
+= tableName
;
990 sqlStmt
+= "_PIDX PRIMARY KEY (";
994 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
995 sqlStmt
+= ", PRIMARY KEY (";
998 // List column name(s) of column(s) comprising the primary key
999 for (i
= j
= 0; i
< noCols
; i
++)
1001 if (colDefs
[i
].KeyField
)
1003 if (j
++) // Multi part key, comma separate names
1005 sqlStmt
+= colDefs
[i
].ColName
;
1010 // Append the closing parentheses for the create table statement
1013 pDb
->WriteSqlLog(sqlStmt
.GetData());
1015 #ifdef DBDEBUG_CONSOLE
1016 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1019 // Execute the CREATE TABLE statement
1020 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
);
1021 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1023 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1024 pDb
->RollbackTrans();
1029 // Commit the transaction and close the cursor
1030 if (! pDb
->CommitTrans())
1032 if (! CloseCursor(hstmt
))
1035 // Database table created successfully
1038 } // wxTable::CreateTable()
1041 /********** wxTable::DropTable() **********/
1042 bool wxTable::DropTable()
1044 // NOTE: This function returns TRUE if the Table does not exist, but
1045 // only for identified databases. Code will need to be added
1046 // below for any other databases when those databases are defined
1047 // to handle this situation consistently
1049 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1052 sqlStmt
.sprintf("DROP TABLE %s", tableName
);
1054 pDb
->WriteSqlLog(sqlStmt
.GetData());
1056 #ifdef DBDEBUG_CONSOLE
1057 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1060 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1062 // Check for "Base table not found" error and ignore
1063 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1064 if (wxStrcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
1066 // Check for product specific error codes
1067 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
1068 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"S1000")) || // untested
1069 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01")))) // untested
1071 pDb
->DispNextError();
1072 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1073 pDb
->RollbackTrans();
1080 // Commit the transaction and close the cursor
1081 if (! pDb
->CommitTrans())
1083 if (! CloseCursor(hstmt
))
1087 } // wxTable::DropTable()
1090 /********** wxTable::CreateIndex() **********/
1091 bool wxTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
1093 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1096 // Drop the index first
1097 if (attemptDrop
&& !DropIndex(idxName
))
1100 // Build a CREATE INDEX statement
1101 sqlStmt
= "CREATE ";
1103 sqlStmt
+= "UNIQUE ";
1105 sqlStmt
+= "INDEX ";
1108 sqlStmt
+= tableName
;
1111 // Append list of columns making up index
1113 for (i
= 0; i
< noIdxCols
; i
++)
1115 sqlStmt
+= pIdxDefs
[i
].ColName
;
1116 /* Postgres doesn't cope with ASC */
1117 if (pDb
->Dbms() != dbmsPOSTGRES
)
1119 if (pIdxDefs
[i
].Ascending
)
1125 if ((i
+ 1) < noIdxCols
)
1129 // Append closing parentheses
1132 pDb
->WriteSqlLog(sqlStmt
.GetData());
1134 #ifdef DBDEBUG_CONSOLE
1135 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1138 // Execute the CREATE INDEX statement
1139 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1141 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1142 pDb
->RollbackTrans();
1147 // Commit the transaction and close the cursor
1148 if (! pDb
->CommitTrans())
1150 if (! CloseCursor(hstmt
))
1153 // Index Created Successfully
1156 } // wxTable::CreateIndex()
1159 /********** wxTable::DropIndex() **********/
1160 bool wxTable::DropIndex(const char * idxName
)
1162 // NOTE: This function returns TRUE if the Index does not exist, but
1163 // only for identified databases. Code will need to be added
1164 // below for any other databases when those databases are defined
1165 // to handle this situation consistently
1167 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1170 if (pDb
->Dbms() == dbmsACCESS
)
1171 sqlStmt
.sprintf("DROP INDEX %s ON %s",idxName
,tableName
);
1172 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1173 sqlStmt
.sprintf("DROP INDEX %s.%s",tableName
,idxName
);
1175 sqlStmt
.sprintf("DROP INDEX %s",idxName
);
1177 pDb
->WriteSqlLog(sqlStmt
.GetData());
1179 #ifdef DBDEBUG_CONSOLE
1180 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1183 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1185 // Check for "Index not found" error and ignore
1186 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1187 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1189 // Check for product specific error codes
1190 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1191 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1192 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S02")) // untested
1195 pDb
->DispNextError();
1196 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1197 pDb
->RollbackTrans();
1204 // Commit the transaction and close the cursor
1205 if (! pDb
->CommitTrans())
1207 if (! CloseCursor(hstmt
))
1211 } // wxTable::DropIndex()
1214 /********** wxTable::Insert() **********/
1215 int wxTable::Insert(void)
1223 // Insert the record by executing the already prepared insert statement
1225 retcode
=SQLExecute(hstmtInsert
);
1226 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1228 // Check to see if integrity constraint was violated
1229 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1230 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1231 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1234 pDb
->DispNextError();
1235 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1240 // Record inserted into the datasource successfully
1243 } // wxTable::Insert()
1246 /********** wxTable::Update() **********/
1247 bool wxTable::Update(void)
1253 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1255 // Build the SQL UPDATE statement
1256 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1258 pDb
->WriteSqlLog(sqlStmt
);
1260 #ifdef DBDEBUG_CONSOLE
1261 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1264 // Execute the SQL UPDATE statement
1265 return(execUpdate(sqlStmt
));
1267 } // wxTable::Update()
1270 /********** wxTable::Update(pSqlStmt) **********/
1271 bool wxTable::Update(const char *pSqlStmt
)
1277 pDb
->WriteSqlLog(pSqlStmt
);
1279 return(execUpdate(pSqlStmt
));
1281 } // wxTable::Update(pSqlStmt)
1284 /********** wxTable::UpdateWhere() **********/
1285 bool wxTable::UpdateWhere(const char *pWhereClause
)
1291 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1293 // Build the SQL UPDATE statement
1294 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1296 pDb
->WriteSqlLog(sqlStmt
);
1298 #ifdef DBDEBUG_CONSOLE
1299 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1302 // Execute the SQL UPDATE statement
1303 return(execUpdate(sqlStmt
));
1305 } // wxTable::UpdateWhere()
1308 /********** wxTable::Delete() **********/
1309 bool wxTable::Delete(void)
1315 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1317 // Build the SQL DELETE statement
1318 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1320 pDb
->WriteSqlLog(sqlStmt
);
1322 // Execute the SQL DELETE statement
1323 return(execDelete(sqlStmt
));
1325 } // wxTable::Delete()
1328 /********** wxTable::DeleteWhere() **********/
1329 bool wxTable::DeleteWhere(const char *pWhereClause
)
1335 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1337 // Build the SQL DELETE statement
1338 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1340 pDb
->WriteSqlLog(sqlStmt
);
1342 // Execute the SQL DELETE statement
1343 return(execDelete(sqlStmt
));
1345 } // wxTable::DeleteWhere()
1348 /********** wxTable::DeleteMatching() **********/
1349 bool wxTable::DeleteMatching(void)
1355 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1357 // Build the SQL DELETE statement
1358 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1360 pDb
->WriteSqlLog(sqlStmt
);
1362 // Execute the SQL DELETE statement
1363 return(execDelete(sqlStmt
));
1365 } // wxTable::DeleteMatching()
1368 /********** wxTable::GetUpdateStmt() **********/
1369 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1375 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1376 bool firstColumn
= TRUE
;
1379 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1381 // Append a list of columns to be updated
1383 for (i
= 0; i
< noCols
; i
++)
1385 // Only append Updateable columns
1386 if (colDefs
[i
].Updateable
)
1389 wxStrcat(pSqlStmt
, ",");
1391 firstColumn
= FALSE
;
1392 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1393 wxStrcat(pSqlStmt
, " = ?");
1397 // Append the WHERE clause to the SQL UPDATE statement
1398 wxStrcat(pSqlStmt
, " WHERE ");
1401 case DB_UPD_KEYFIELDS
:
1402 // If the datasource supports the ROWID column, build
1403 // the where on ROWID for efficiency purposes.
1404 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1405 if (CanUpdByROWID())
1408 char rowid
[ROWID_LEN
];
1410 // Get the ROWID value. If not successful retreiving the ROWID,
1411 // simply fall down through the code and build the WHERE clause
1412 // based on the key fields.
1413 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1415 wxStrcat(pSqlStmt
, "ROWID = '");
1416 wxStrcat(pSqlStmt
, rowid
);
1417 wxStrcat(pSqlStmt
, "'");
1421 // Unable to delete by ROWID, so build a WHERE
1422 // clause based on the keyfields.
1423 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1424 wxStrcat(pSqlStmt
, whereClause
);
1427 wxStrcat(pSqlStmt
, pWhereClause
);
1430 } // GetUpdateStmt()
1433 /********** wxTable::GetDeleteStmt() **********/
1434 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1440 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1444 // Handle the case of DeleteWhere() and the where clause is blank. It should
1445 // delete all records from the database in this case.
1446 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1448 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1452 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1454 // Append the WHERE clause to the SQL DELETE statement
1457 case DB_DEL_KEYFIELDS
:
1458 // If the datasource supports the ROWID column, build
1459 // the where on ROWID for efficiency purposes.
1460 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1461 if (CanUpdByROWID())
1464 char rowid
[ROWID_LEN
];
1466 // Get the ROWID value. If not successful retreiving the ROWID,
1467 // simply fall down through the code and build the WHERE clause
1468 // based on the key fields.
1469 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1471 wxStrcat(pSqlStmt
, "ROWID = '");
1472 wxStrcat(pSqlStmt
, rowid
);
1473 wxStrcat(pSqlStmt
, "'");
1477 // Unable to delete by ROWID, so build a WHERE
1478 // clause based on the keyfields.
1479 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1480 wxStrcat(pSqlStmt
, whereClause
);
1483 wxStrcat(pSqlStmt
, pWhereClause
);
1485 case DB_DEL_MATCHING
:
1486 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1487 wxStrcat(pSqlStmt
, whereClause
);
1491 } // GetDeleteStmt()
1494 /********** wxTable::GetWhereClause() **********/
1495 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
)
1497 * Note: GetWhereClause() currently ignores timestamp columns.
1498 * They are not included as part of the where clause.
1501 bool moreThanOneColumn
= FALSE
;
1504 // Loop through the columns building a where clause as you go
1506 for (i
= 0; i
< noCols
; i
++)
1508 // Determine if this column should be included in the WHERE clause
1509 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1510 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1512 // Skip over timestamp columns
1513 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1515 // If there is more than 1 column, join them with the keyword "AND"
1516 if (moreThanOneColumn
)
1517 wxStrcat(pWhereClause
, " AND ");
1519 moreThanOneColumn
= TRUE
;
1520 // Concatenate where phrase for the column
1521 if (qualTableName
&& wxStrlen(qualTableName
))
1523 wxStrcat(pWhereClause
, qualTableName
);
1524 wxStrcat(pWhereClause
, ".");
1526 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1527 wxStrcat(pWhereClause
, " = ");
1528 switch(colDefs
[i
].SqlCtype
)
1531 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1534 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1537 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1540 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1543 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1546 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1549 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1552 wxStrcat(pWhereClause
, colValue
);
1555 } // wxTable::GetWhereClause()
1558 /********** wxTable::IsColNull() **********/
1559 bool wxTable::IsColNull(int colNo
)
1561 switch(colDefs
[colNo
].SqlCtype
)
1564 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1566 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1568 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1570 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1572 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1574 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1576 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1577 case SQL_C_TIMESTAMP
:
1578 TIMESTAMP_STRUCT
*pDt
;
1579 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1580 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1587 } // wxTable::IsColNull()
1590 /********** wxTable::CanSelectForUpdate() **********/
1591 bool wxTable::CanSelectForUpdate(void)
1593 if (pDb
->Dbms() == dbmsMY_SQL
)
1596 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1601 } // wxTable::CanSelectForUpdate()
1604 /********** wxTable::CanUpdByROWID() **********/
1605 bool wxTable::CanUpdByROWID(void)
1608 * NOTE: Returning FALSE for now until this can be debugged,
1609 * as the ROWID is not getting updated correctly
1613 if (pDb
->Dbms() == dbmsORACLE
)
1618 } // wxTable::CanUpdByROWID()
1621 /********** wxTable::IsCursorClosedOnCommit() **********/
1622 bool wxTable::IsCursorClosedOnCommit(void)
1624 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1629 } // wxTable::IsCursorClosedOnCommit()
1632 /********** wxTable::ClearMemberVars() **********/
1633 void wxTable::ClearMemberVars(void)
1635 // Loop through the columns setting each member variable to zero
1637 for (i
= 0; i
< noCols
; i
++)
1639 switch(colDefs
[i
].SqlCtype
)
1642 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1645 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1648 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1651 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1654 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1657 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1660 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1662 case SQL_C_TIMESTAMP
:
1663 TIMESTAMP_STRUCT
*pDt
;
1664 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1676 } // wxTable::ClearMemberVars()
1679 /********** wxTable::SetQueryTimeout() **********/
1680 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1682 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1683 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1684 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1685 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1686 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1687 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1688 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1689 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1691 // Completed Successfully
1694 } // wxTable::SetQueryTimeout()
1697 /********** wxTable::SetColDefs() **********/
1698 void wxTable::SetColDefs (int index
, const char *fieldName
, int dataType
, void *pData
,
1699 int cType
, int size
, bool keyField
, bool upd
,
1700 bool insAllow
, bool derivedCol
)
1702 if (!colDefs
) // May happen if the database connection fails
1705 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1707 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1708 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1711 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
1713 colDefs
[index
].DbDataType
= dataType
;
1714 colDefs
[index
].PtrDataObj
= pData
;
1715 colDefs
[index
].SqlCtype
= cType
;
1716 colDefs
[index
].SzDataObj
= size
;
1717 colDefs
[index
].KeyField
= keyField
;
1718 colDefs
[index
].DerivedCol
= derivedCol
;
1719 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1722 colDefs
[index
].Updateable
= FALSE
;
1723 colDefs
[index
].InsertAllowed
= FALSE
;
1727 colDefs
[index
].Updateable
= upd
;
1728 colDefs
[index
].InsertAllowed
= insAllow
;
1731 colDefs
[index
].Null
= FALSE
;
1733 } // wxTable::SetColDefs()
1736 /********** wxTable::SetColDef() **********/
1737 // BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
1738 //bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
1739 wxColDataPtr
* wxTable::SetColDefs (wxColInf
*pColInfs
, ULONG numCols
)
1742 wxColDataPtr
*pColDataPtrs
= NULL
;
1749 pColDataPtrs
= new wxColDataPtr
[numCols
+1];
1751 for (index
= 0; index
< numCols
; index
++)
1755 title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
1756 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",
1757 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);
1758 msg += " \nDB_DATA_TYPE: ";
1759 switch(pColInfs[index].dbDataType)
1761 case DB_DATA_TYPE_VARCHAR:
1762 msg += pDb->typeInfVarchar.TypeName; break;
1763 case DB_DATA_TYPE_INTEGER:
1764 msg += pDb->typeInfInteger.TypeName; break;
1765 case DB_DATA_TYPE_FLOAT:
1766 msg += pDb->typeInfFloat.TypeName; break;
1767 case DB_DATA_TYPE_DATE:
1768 msg += pDb->typeInfDate.TypeName; break;
1770 wxMessageBox(msg.GetData(),title.GetData());
1772 // Process the fields
1773 switch (pColInfs
[index
].dbDataType
)
1775 case DB_DATA_TYPE_VARCHAR
:
1777 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].bufferLength
+1];
1778 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferLength
;
1779 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
1782 case DB_DATA_TYPE_INTEGER
:
1784 // Can be long or short
1785 if (pColInfs
[index
].bufferLength
== sizeof(long))
1787 pColDataPtrs
[index
].PtrDataObj
= new long;
1788 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
1789 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
1793 pColDataPtrs
[index
].PtrDataObj
= new short;
1794 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
1795 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
1799 case DB_DATA_TYPE_FLOAT
:
1801 // Can be float or double
1802 if (pColInfs
[index
].bufferLength
== sizeof(float))
1804 pColDataPtrs
[index
].PtrDataObj
= new float;
1805 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
1806 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
1810 pColDataPtrs
[index
].PtrDataObj
= new double;
1811 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
1812 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
1816 case DB_DATA_TYPE_DATE
:
1818 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
1819 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
1820 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
1825 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
1828 return (pColDataPtrs
);
1829 } // wxTable::SetColDef()
1832 /********** wxTable::SetCursor() **********/
1833 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1835 if (hstmtActivate
== DEFAULT_CURSOR
)
1836 hstmt
= *hstmtDefault
;
1838 hstmt
= *hstmtActivate
;
1840 } // wxTable::SetCursor()
1843 /********** wxTable::Count(const char *) **********/
1844 ULONG
wxTable::Count(const char *args
)
1847 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1851 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1852 sqlStmt
= "SELECT COUNT(";
1854 sqlStmt
+= ") FROM ";
1855 sqlStmt
+= queryTableName
;
1857 if (from
&& wxStrlen(from
))
1860 // Add the where clause if one is provided
1861 if (where
&& wxStrlen(where
))
1863 sqlStmt
+= " WHERE ";
1867 pDb
->WriteSqlLog(sqlStmt
.GetData());
1869 // Initialize the Count cursor if it's not already initialized
1872 hstmtCount
= NewCursor(FALSE
,FALSE
);
1878 // Execute the SQL statement
1879 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1881 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1886 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1888 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1892 // Obtain the result
1893 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1895 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1900 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1901 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1903 // Return the record count
1906 } // wxTable::Count()
1909 /********** wxTable::Refresh() **********/
1910 bool wxTable::Refresh(void)
1914 // Switch to the internal cursor so any active cursors are not corrupted
1915 HSTMT currCursor
= GetCursor();
1916 hstmt
= hstmtInternal
;
1918 // Save the where and order by clauses
1919 char *saveWhere
= where
;
1920 char *saveOrderBy
= orderBy
;
1922 // Build a where clause to refetch the record with. Try and use the
1923 // ROWID if it's available, ow use the key fields.
1924 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1925 wxStrcpy(whereClause
, "");
1926 if (CanUpdByROWID())
1929 char rowid
[ROWID_LEN
+1];
1931 // Get the ROWID value. If not successful retreiving the ROWID,
1932 // simply fall down through the code and build the WHERE clause
1933 // based on the key fields.
1934 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1936 wxStrcat(whereClause
, queryTableName
);
1937 wxStrcat(whereClause
, ".ROWID = '");
1938 wxStrcat(whereClause
, rowid
);
1939 wxStrcat(whereClause
, "'");
1943 // If unable to use the ROWID, build a where clause from the keyfields
1944 if (wxStrlen(whereClause
) == 0)
1945 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1947 // Requery the record
1948 where
= whereClause
;
1953 if (result
&& !GetNext())
1956 // Switch back to original cursor
1957 SetCursor(&currCursor
);
1959 // Free the internal cursor
1960 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1961 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1963 // Restore the original where and order by clauses
1965 orderBy
= saveOrderBy
;
1969 } // wxTable::Refresh()
1972 /********** wxTable::SetNull(int colNo) **********/
1973 bool wxTable::SetNull(int colNo
)
1976 return(colDefs
[colNo
].Null
= TRUE
);
1980 } // wxTable::SetNull(int colNo)
1983 /********** wxTable::SetNull(char *colName) **********/
1984 bool wxTable::SetNull(const char *colName
)
1987 for (i
= 0; i
< noCols
; i
++)
1989 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
1994 return(colDefs
[i
].Null
= TRUE
);
1998 } // wxTable::SetNull(char *colName)
2001 /********** wxTable::NewCursor() **********/
2002 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
2004 HSTMT
*newHSTMT
= new HSTMT
;
2009 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2011 pDb
->DispAllErrors(henv
, hdbc
);
2016 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2018 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2025 if(!bindCols(*newHSTMT
))
2033 SetCursor(newHSTMT
);
2037 } // wxTable::NewCursor()
2040 /********** wxTable::DeleteCursor() **********/
2041 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
2045 if (!hstmtDel
) // Cursor already deleted
2048 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2050 pDb
->DispAllErrors(henv
, hdbc
);
2058 } // wxTable::DeleteCursor()
2060 #endif // wxUSE_ODBC