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());
258 TablesInUse
.DeleteContents(TRUE
);
262 pNode
= TablesInUse
.First();
263 while (pNode
&& !found
)
265 if (((CstructTablesInUse
*)pNode
->Data())->tableID
== tableID
)
268 if (!TablesInUse
.DeleteNode(pNode
))
269 wxMessageBox (s
.GetData(),"Unable to delete node!");
272 pNode
= pNode
->Next();
277 msg
.sprintf("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s
.GetData());
278 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
);
301 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
304 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
305 pDb
->DispAllErrors(henv
, hdbc
);
309 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
310 pDb
->DispAllErrors(henv
, hdbc
);
312 // Delete dynamically allocated cursors
314 DeleteCursor(hstmtDefault
);
317 DeleteCursor(hstmtCount
);
320 } // wxTable::~wxTable()
324 /***************************** PRIVATE FUNCTIONS *****************************/
328 /********** wxTable::bindInsertParams() **********/
329 bool wxTable::bindInsertParams(void)
336 UDWORD precision
= 0;
339 // Bind each column (that can be inserted) of the table to a parameter marker
341 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
343 if (! colDefs
[i
].InsertAllowed
)
345 switch(colDefs
[i
].DbDataType
)
347 case DB_DATA_TYPE_VARCHAR
:
348 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
349 precision
= colDefs
[i
].SzDataObj
;
351 colDefs
[i
].CbValue
= SQL_NTS
;
353 case DB_DATA_TYPE_INTEGER
:
354 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
355 precision
= pDb
->typeInfInteger
.Precision
;
357 colDefs
[i
].CbValue
= 0;
359 case DB_DATA_TYPE_FLOAT
:
360 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
361 precision
= pDb
->typeInfFloat
.Precision
;
362 scale
= pDb
->typeInfFloat
.MaximumScale
;
363 // SQL Sybase Anywhere v5.5 returned a negative number for the
364 // MaxScale. This caused ODBC to kick out an error on ibscale.
365 // I check for this here and set the scale = precision.
367 // scale = (short) precision;
368 colDefs
[i
].CbValue
= 0;
370 case DB_DATA_TYPE_DATE
:
371 fSqlType
= pDb
->typeInfDate
.FsqlType
;
372 precision
= pDb
->typeInfDate
.Precision
;
374 colDefs
[i
].CbValue
= 0;
380 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
381 colDefs
[i
].Null
= FALSE
;
383 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
384 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
385 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
386 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
389 // Completed successfully
392 } // wxTable::bindInsertParams()
395 /********** wxTable::bindUpdateParams() **********/
396 bool wxTable::bindUpdateParams(void)
403 UDWORD precision
= 0;
406 // Bind each UPDATEABLE column of the table to a parameter marker
408 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
410 if (! colDefs
[i
].Updateable
)
412 switch(colDefs
[i
].DbDataType
)
414 case DB_DATA_TYPE_VARCHAR
:
415 fSqlType
= pDb
->typeInfVarchar
.FsqlType
;
416 precision
= colDefs
[i
].SzDataObj
;
418 colDefs
[i
].CbValue
= SQL_NTS
;
420 case DB_DATA_TYPE_INTEGER
:
421 fSqlType
= pDb
->typeInfInteger
.FsqlType
;
422 precision
= pDb
->typeInfInteger
.Precision
;
424 colDefs
[i
].CbValue
= 0;
426 case DB_DATA_TYPE_FLOAT
:
427 fSqlType
= pDb
->typeInfFloat
.FsqlType
;
428 precision
= pDb
->typeInfFloat
.Precision
;
429 scale
= pDb
->typeInfFloat
.MaximumScale
;
430 // SQL Sybase Anywhere v5.5 returned a negative number for the
431 // MaxScale. This caused ODBC to kick out an error on ibscale.
432 // I check for this here and set the scale = precision.
434 // scale = (short) precision;
435 colDefs
[i
].CbValue
= 0;
437 case DB_DATA_TYPE_DATE
:
438 fSqlType
= pDb
->typeInfDate
.FsqlType
;
439 precision
= pDb
->typeInfDate
.Precision
;
441 colDefs
[i
].CbValue
= 0;
444 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
445 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
446 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
447 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
450 // Completed successfully
453 } // wxTable::bindUpdateParams()
456 /********** wxTable::bindCols() **********/
457 bool wxTable::bindCols(HSTMT cursor
)
461 // Bind each column of the table to a memory address for fetching data
463 for (i
= 0; i
< noCols
; i
++)
465 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
466 colDefs
[i
].SzDataObj
, &cb
) != SQL_SUCCESS
)
467 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
470 // Completed successfully
473 } // wxTable::bindCols()
476 /********** wxTable::getRec() **********/
477 bool wxTable::getRec(UWORD fetchType
)
481 if (!pDb
->FwdOnlyCursors())
483 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
487 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
488 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
489 if (retcode
== SQL_NO_DATA_FOUND
)
492 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
496 // Fetch the next record from the record set
497 retcode
= SQLFetch(hstmt
);
498 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
500 if (retcode
== SQL_NO_DATA_FOUND
)
503 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
507 // Completed successfully
510 } // wxTable::getRec()
513 /********** wxTable::execDelete() **********/
514 bool wxTable::execDelete(const char *pSqlStmt
)
516 // Execute the DELETE statement
517 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
518 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
520 // Record deleted successfully
523 } // wxTable::execDelete()
526 /********** wxTable::execUpdate() **********/
527 bool wxTable::execUpdate(const char *pSqlStmt
)
529 // Execute the UPDATE statement
530 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
531 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
533 // Record deleted successfully
536 } // wxTable::execUpdate()
539 /********** wxTable::query() **********/
540 bool wxTable::query(int queryType
, bool forUpdate
, bool distinct
, char *pSqlStmt
)
542 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
544 // Set the selectForUpdate member variable
546 // The user may wish to select for update, but the DBMS may not be capable
547 selectForUpdate
= CanSelectForUpdate();
549 selectForUpdate
= FALSE
;
551 // Set the SQL SELECT string
552 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
553 { // so generate a select statement.
554 GetSelectStmt(sqlStmt
, queryType
, distinct
);
555 pDb
->WriteSqlLog(sqlStmt
);
558 // Make sure the cursor is closed first
559 if (! CloseCursor(hstmt
))
562 // Execute the SQL SELECT statement
565 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
566 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
567 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
569 // Completed successfully
572 } // wxTable::query()
575 /***************************** PUBLIC FUNCTIONS *****************************/
578 /********** wxTable::Open() **********/
579 bool wxTable::Open(void)
585 // char sqlStmt[DB_MAX_STATEMENT_LEN];
588 // Verify that the table exists in the database
589 if (!pDb
->TableExists(tableName
,pDb
->GetUsername(),tablePath
))
592 if (wxStrcmp(tablePath
,""))
593 s
.sprintf("Error opening '%s/%s'.\n",tablePath
,tableName
);
595 s
.sprintf("Error opening '%s'.\n", tableName
);
596 if (!pDb
->TableExists(tableName
,NULL
,tablePath
))
597 s
+= "Table/view does not exist in the database.\n";
599 s
+= "Current logged in user does not have sufficient privileges to access this table.\n";
600 pDb
->LogError(s
.GetData());
604 // Bind the member variables for field exchange between
605 // the wxTable object and the ODBC record.
608 if (!bindInsertParams()) // Inserts
610 if (!bindUpdateParams()) // Updates
613 if (!bindCols(*hstmtDefault
)) // Selects
615 if (!bindCols(hstmtInternal
)) // Internal use only
618 * Do NOT bind the hstmtCount cursor!!!
621 // Build an insert statement using parameter markers
622 if (!queryOnly
&& noCols
> 0)
624 bool needComma
= FALSE
;
625 sqlStmt
.sprintf("INSERT INTO %s (", tableName
);
626 for (i
= 0; i
< noCols
; i
++)
628 if (! colDefs
[i
].InsertAllowed
)
632 sqlStmt
+= colDefs
[i
].ColName
;
636 sqlStmt
+= ") VALUES (";
637 for (i
= 0; i
< noCols
; i
++)
639 if (! colDefs
[i
].InsertAllowed
)
648 // pDb->WriteSqlLog(sqlStmt);
650 // Prepare the insert statement for execution
651 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
652 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
655 // Completed successfully
661 /********** wxTable::Query() **********/
662 bool wxTable::Query(bool forUpdate
, bool distinct
)
665 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
667 } // wxTable::Query()
670 /********** wxTable::QueryBySqlStmt() **********/
671 bool wxTable::QueryBySqlStmt(char *pSqlStmt
)
673 pDb
->WriteSqlLog(pSqlStmt
);
675 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
677 } // wxTable::QueryBySqlStmt()
680 /********** wxTable::QueryMatching() **********/
681 bool wxTable::QueryMatching(bool forUpdate
, bool distinct
)
684 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
686 } // wxTable::QueryMatching()
689 /********** wxTable::QueryOnKeyFields() **********/
690 bool wxTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
693 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
695 } // wxTable::QueryOnKeyFields()
698 /********** wxTable::GetPrev() **********/
699 bool wxTable::GetPrev(void)
701 if (pDb
->FwdOnlyCursors())
703 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxTable"));
707 return(getRec(SQL_FETCH_PRIOR
));
708 } // wxTable::GetPrev()
711 /********** wxTable::operator-- **********/
712 bool wxTable::operator--(int)
714 if (pDb
->FwdOnlyCursors())
716 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxTable"));
720 return(getRec(SQL_FETCH_PRIOR
));
721 } // wxTable::operator--
724 /********** wxTable::GetFirst() **********/
725 bool wxTable::GetFirst(void)
727 if (pDb
->FwdOnlyCursors())
729 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxTable"));
733 return(getRec(SQL_FETCH_FIRST
));
734 } // wxTable::GetFirst()
737 /********** wxTable::GetLast() **********/
738 bool wxTable::GetLast(void)
740 if (pDb
->FwdOnlyCursors())
742 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxTable"));
746 return(getRec(SQL_FETCH_LAST
));
747 } // wxTable::GetLast()
750 /********** wxTable::GetSelectStmt() **********/
751 void wxTable::GetSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
753 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
757 // Build a select statement to query the database
758 wxStrcpy(pSqlStmt
, "SELECT ");
760 // SELECT DISTINCT values only?
762 wxStrcat(pSqlStmt
, "DISTINCT ");
764 // Was a FROM clause specified to join tables to the base table?
765 // Available for ::Query() only!!!
766 bool appendFromClause
= FALSE
;
767 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
768 appendFromClause
= TRUE
;
770 // Add the column list
772 for (i
= 0; i
< noCols
; i
++)
774 // If joining tables, the base table column names must be qualified to avoid ambiguity
775 if (appendFromClause
)
777 wxStrcat(pSqlStmt
, queryTableName
);
778 wxStrcat(pSqlStmt
, ".");
780 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
782 wxStrcat(pSqlStmt
, ",");
785 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
786 // the ROWID if querying distinct records. The rowid will always be unique.
787 if (!distinct
&& CanUpdByROWID())
789 // If joining tables, the base table column names must be qualified to avoid ambiguity
790 if (appendFromClause
)
792 wxStrcat(pSqlStmt
, ",");
793 wxStrcat(pSqlStmt
, queryTableName
);
794 wxStrcat(pSqlStmt
, ".ROWID");
797 wxStrcat(pSqlStmt
, ",ROWID");
800 // Append the FROM tablename portion
801 wxStrcat(pSqlStmt
, " FROM ");
802 wxStrcat(pSqlStmt
, queryTableName
);
804 // Sybase uses the HOLDLOCK keyword to lock a record during query.
805 // The HOLDLOCK keyword follows the table name in the from clause.
806 // Each table in the from clause must specify HOLDLOCK or
807 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
808 // is parsed but ignored in SYBASE Transact-SQL.
809 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
810 wxStrcat(pSqlStmt
, " HOLDLOCK");
812 if (appendFromClause
)
813 wxStrcat(pSqlStmt
, from
);
815 // Append the WHERE clause. Either append the where clause for the class
816 // or build a where clause. The typeOfSelect determines this.
819 case DB_SELECT_WHERE
:
820 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
822 wxStrcat(pSqlStmt
, " WHERE ");
823 wxStrcat(pSqlStmt
, where
);
826 case DB_SELECT_KEYFIELDS
:
827 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
828 if (wxStrlen(whereClause
))
830 wxStrcat(pSqlStmt
, " WHERE ");
831 wxStrcat(pSqlStmt
, whereClause
);
834 case DB_SELECT_MATCHING
:
835 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
836 if (wxStrlen(whereClause
))
838 wxStrcat(pSqlStmt
, " WHERE ");
839 wxStrcat(pSqlStmt
, whereClause
);
844 // Append the ORDER BY clause
845 if (orderBy
&& wxStrlen(orderBy
))
847 wxStrcat(pSqlStmt
, " ORDER BY ");
848 wxStrcat(pSqlStmt
, orderBy
);
851 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
852 // parses the FOR UPDATE clause but ignores it. See the comment above on the
853 // HOLDLOCK for Sybase.
854 if (selectForUpdate
&& CanSelectForUpdate())
855 wxStrcat(pSqlStmt
, " FOR UPDATE");
857 } // wxTable::GetSelectStmt()
860 /********** wxTable::GetRowNum() **********/
861 UWORD
wxTable::GetRowNum(void)
865 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
867 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
871 // Completed successfully
872 return((UWORD
) rowNum
);
874 } // wxTable::GetRowNum()
877 /********** wxTable::CloseCursor() **********/
878 bool wxTable::CloseCursor(HSTMT cursor
)
880 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
881 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
883 // Completed successfully
886 } // wxTable::CloseCursor()
889 /********** wxTable::CreateTable() **********/
890 bool wxTable::CreateTable(bool attemptDrop
)
896 // char sqlStmt[DB_MAX_STATEMENT_LEN];
899 #ifdef DBDEBUG_CONSOLE
900 cout
<< "Creating Table " << tableName
<< "..." << endl
;
904 if (attemptDrop
&& !DropTable())
908 #ifdef DBDEBUG_CONSOLE
909 for (i
= 0; i
< noCols
; i
++)
911 // Exclude derived columns since they are NOT part of the base table
912 if (colDefs
[i
].DerivedCol
)
914 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
915 switch(colDefs
[i
].DbDataType
)
917 case DB_DATA_TYPE_VARCHAR
:
918 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
920 case DB_DATA_TYPE_INTEGER
:
921 cout
<< pDb
->typeInfInteger
.TypeName
;
923 case DB_DATA_TYPE_FLOAT
:
924 cout
<< pDb
->typeInfFloat
.TypeName
;
926 case DB_DATA_TYPE_DATE
:
927 cout
<< pDb
->typeInfDate
.TypeName
;
934 // Build a CREATE TABLE string from the colDefs structure.
935 bool needComma
= FALSE
;
936 sqlStmt
.sprintf("CREATE TABLE %s (", tableName
);
938 for (i
= 0; i
< noCols
; i
++)
940 // Exclude derived columns since they are NOT part of the base table
941 if (colDefs
[i
].DerivedCol
)
947 sqlStmt
+= colDefs
[i
].ColName
;
950 switch(colDefs
[i
].DbDataType
)
952 case DB_DATA_TYPE_VARCHAR
:
953 sqlStmt
+= pDb
->typeInfVarchar
.TypeName
; break;
954 case DB_DATA_TYPE_INTEGER
:
955 sqlStmt
+= pDb
->typeInfInteger
.TypeName
; break;
956 case DB_DATA_TYPE_FLOAT
:
957 sqlStmt
+= pDb
->typeInfFloat
.TypeName
; break;
958 case DB_DATA_TYPE_DATE
:
959 sqlStmt
+= pDb
->typeInfDate
.TypeName
; break;
961 // For varchars, append the size of the string
962 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
965 // wxStrcat(sqlStmt, "(");
966 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
967 // wxStrcat(sqlStmt, ")");
968 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
969 sqlStmt
+= s
.GetData();
972 if (pDb
->Dbms() == dbmsSYBASE_ASE
|| pDb
->Dbms() == dbmsMY_SQL
)
974 if (colDefs
[i
].KeyField
)
976 sqlStmt
+= " NOT NULL";
982 // If there is a primary key defined, include it in the create statement
983 for (i
= j
= 0; i
< noCols
; i
++)
985 if (colDefs
[i
].KeyField
)
991 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
993 if (pDb
->Dbms() != dbmsMY_SQL
)
995 sqlStmt
+= ",CONSTRAINT ";
996 sqlStmt
+= tableName
;
997 sqlStmt
+= "_PIDX PRIMARY KEY (";
1001 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1002 sqlStmt
+= ", PRIMARY KEY (";
1005 // List column name(s) of column(s) comprising the primary key
1006 for (i
= j
= 0; i
< noCols
; i
++)
1008 if (colDefs
[i
].KeyField
)
1010 if (j
++) // Multi part key, comma separate names
1012 sqlStmt
+= colDefs
[i
].ColName
;
1017 // Append the closing parentheses for the create table statement
1020 pDb
->WriteSqlLog(sqlStmt
.GetData());
1022 #ifdef DBDEBUG_CONSOLE
1023 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1026 // Execute the CREATE TABLE statement
1027 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
);
1028 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1030 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1031 pDb
->RollbackTrans();
1036 // Commit the transaction and close the cursor
1037 if (! pDb
->CommitTrans())
1039 if (! CloseCursor(hstmt
))
1042 // Database table created successfully
1045 } // wxTable::CreateTable()
1048 /********** wxTable::DropTable() **********/
1049 bool wxTable::DropTable()
1051 // NOTE: This function returns TRUE if the Table does not exist, but
1052 // only for identified databases. Code will need to be added
1053 // below for any other databases when those databases are defined
1054 // to handle this situation consistently
1056 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1059 sqlStmt
.sprintf("DROP TABLE %s", tableName
);
1061 pDb
->WriteSqlLog(sqlStmt
.GetData());
1063 #ifdef DBDEBUG_CONSOLE
1064 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1067 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1069 // Check for "Base table not found" error and ignore
1070 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1071 if (wxStrcmp(pDb
->sqlState
,"S0002")) // "Base table not found"
1073 // Check for product specific error codes
1074 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
1075 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"S1000")) || // untested
1076 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01")))) // untested
1078 pDb
->DispNextError();
1079 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1080 pDb
->RollbackTrans();
1087 // Commit the transaction and close the cursor
1088 if (! pDb
->CommitTrans())
1090 if (! CloseCursor(hstmt
))
1094 } // wxTable::DropTable()
1097 /********** wxTable::CreateIndex() **********/
1098 bool wxTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, CidxDef
*pIdxDefs
, bool attemptDrop
)
1100 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1103 // Drop the index first
1104 if (attemptDrop
&& !DropIndex(idxName
))
1107 // Build a CREATE INDEX statement
1108 sqlStmt
= "CREATE ";
1110 sqlStmt
+= "UNIQUE ";
1112 sqlStmt
+= "INDEX ";
1115 sqlStmt
+= tableName
;
1118 // Append list of columns making up index
1120 for (i
= 0; i
< noIdxCols
; i
++)
1122 sqlStmt
+= pIdxDefs
[i
].ColName
;
1123 /* Postgres doesn't cope with ASC */
1124 if (pDb
->Dbms() != dbmsPOSTGRES
)
1126 if (pIdxDefs
[i
].Ascending
)
1132 if ((i
+ 1) < noIdxCols
)
1136 // Append closing parentheses
1139 pDb
->WriteSqlLog(sqlStmt
.GetData());
1141 #ifdef DBDEBUG_CONSOLE
1142 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1145 // Execute the CREATE INDEX statement
1146 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1148 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1149 pDb
->RollbackTrans();
1154 // Commit the transaction and close the cursor
1155 if (! pDb
->CommitTrans())
1157 if (! CloseCursor(hstmt
))
1160 // Index Created Successfully
1163 } // wxTable::CreateIndex()
1166 /********** wxTable::DropIndex() **********/
1167 bool wxTable::DropIndex(const char * idxName
)
1169 // NOTE: This function returns TRUE if the Index does not exist, but
1170 // only for identified databases. Code will need to be added
1171 // below for any other databases when those databases are defined
1172 // to handle this situation consistently
1174 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1177 if (pDb
->Dbms() == dbmsACCESS
)
1178 sqlStmt
.sprintf("DROP INDEX %s ON %s",idxName
,tableName
);
1179 else if (pDb
->Dbms() == dbmsSYBASE_ASE
)
1180 sqlStmt
.sprintf("DROP INDEX %s.%s",tableName
,idxName
);
1182 sqlStmt
.sprintf("DROP INDEX %s",idxName
);
1184 pDb
->WriteSqlLog(sqlStmt
.GetData());
1186 #ifdef DBDEBUG_CONSOLE
1187 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1190 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1192 // Check for "Index not found" error and ignore
1193 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1194 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1196 // Check for product specific error codes
1197 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1198 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1199 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S02")) // untested
1202 pDb
->DispNextError();
1203 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1204 pDb
->RollbackTrans();
1211 // Commit the transaction and close the cursor
1212 if (! pDb
->CommitTrans())
1214 if (! CloseCursor(hstmt
))
1218 } // wxTable::DropIndex()
1221 /********** wxTable::Insert() **********/
1222 int wxTable::Insert(void)
1230 // Insert the record by executing the already prepared insert statement
1232 retcode
=SQLExecute(hstmtInsert
);
1233 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1235 // Check to see if integrity constraint was violated
1236 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1237 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1238 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1241 pDb
->DispNextError();
1242 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1247 // Record inserted into the datasource successfully
1250 } // wxTable::Insert()
1253 /********** wxTable::Update() **********/
1254 bool wxTable::Update(void)
1260 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1262 // Build the SQL UPDATE statement
1263 GetUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1265 pDb
->WriteSqlLog(sqlStmt
);
1267 #ifdef DBDEBUG_CONSOLE
1268 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1271 // Execute the SQL UPDATE statement
1272 return(execUpdate(sqlStmt
));
1274 } // wxTable::Update()
1277 /********** wxTable::Update(pSqlStmt) **********/
1278 bool wxTable::Update(const char *pSqlStmt
)
1284 pDb
->WriteSqlLog(pSqlStmt
);
1286 return(execUpdate(pSqlStmt
));
1288 } // wxTable::Update(pSqlStmt)
1291 /********** wxTable::UpdateWhere() **********/
1292 bool wxTable::UpdateWhere(const char *pWhereClause
)
1298 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1300 // Build the SQL UPDATE statement
1301 GetUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1303 pDb
->WriteSqlLog(sqlStmt
);
1305 #ifdef DBDEBUG_CONSOLE
1306 cout
<< endl
<< sqlStmt
.GetData() << endl
<< endl
;
1309 // Execute the SQL UPDATE statement
1310 return(execUpdate(sqlStmt
));
1312 } // wxTable::UpdateWhere()
1315 /********** wxTable::Delete() **********/
1316 bool wxTable::Delete(void)
1322 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1324 // Build the SQL DELETE statement
1325 GetDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1327 pDb
->WriteSqlLog(sqlStmt
);
1329 // Execute the SQL DELETE statement
1330 return(execDelete(sqlStmt
));
1332 } // wxTable::Delete()
1335 /********** wxTable::DeleteWhere() **********/
1336 bool wxTable::DeleteWhere(const char *pWhereClause
)
1342 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1344 // Build the SQL DELETE statement
1345 GetDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1347 pDb
->WriteSqlLog(sqlStmt
);
1349 // Execute the SQL DELETE statement
1350 return(execDelete(sqlStmt
));
1352 } // wxTable::DeleteWhere()
1355 /********** wxTable::DeleteMatching() **********/
1356 bool wxTable::DeleteMatching(void)
1362 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1364 // Build the SQL DELETE statement
1365 GetDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1367 pDb
->WriteSqlLog(sqlStmt
);
1369 // Execute the SQL DELETE statement
1370 return(execDelete(sqlStmt
));
1372 } // wxTable::DeleteMatching()
1375 /********** wxTable::GetUpdateStmt() **********/
1376 void wxTable::GetUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1382 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1383 bool firstColumn
= TRUE
;
1386 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1388 // Append a list of columns to be updated
1390 for (i
= 0; i
< noCols
; i
++)
1392 // Only append Updateable columns
1393 if (colDefs
[i
].Updateable
)
1396 wxStrcat(pSqlStmt
, ",");
1398 firstColumn
= FALSE
;
1399 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1400 wxStrcat(pSqlStmt
, " = ?");
1404 // Append the WHERE clause to the SQL UPDATE statement
1405 wxStrcat(pSqlStmt
, " WHERE ");
1408 case DB_UPD_KEYFIELDS
:
1409 // If the datasource supports the ROWID column, build
1410 // the where on ROWID for efficiency purposes.
1411 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1412 if (CanUpdByROWID())
1415 char rowid
[ROWID_LEN
];
1417 // Get the ROWID value. If not successful retreiving the ROWID,
1418 // simply fall down through the code and build the WHERE clause
1419 // based on the key fields.
1420 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1422 wxStrcat(pSqlStmt
, "ROWID = '");
1423 wxStrcat(pSqlStmt
, rowid
);
1424 wxStrcat(pSqlStmt
, "'");
1428 // Unable to delete by ROWID, so build a WHERE
1429 // clause based on the keyfields.
1430 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1431 wxStrcat(pSqlStmt
, whereClause
);
1434 wxStrcat(pSqlStmt
, pWhereClause
);
1437 } // GetUpdateStmt()
1440 /********** wxTable::GetDeleteStmt() **********/
1441 void wxTable::GetDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1447 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1451 // Handle the case of DeleteWhere() and the where clause is blank. It should
1452 // delete all records from the database in this case.
1453 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1455 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1459 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1461 // Append the WHERE clause to the SQL DELETE statement
1464 case DB_DEL_KEYFIELDS
:
1465 // If the datasource supports the ROWID column, build
1466 // the where on ROWID for efficiency purposes.
1467 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1468 if (CanUpdByROWID())
1471 char rowid
[ROWID_LEN
];
1473 // Get the ROWID value. If not successful retreiving the ROWID,
1474 // simply fall down through the code and build the WHERE clause
1475 // based on the key fields.
1476 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1478 wxStrcat(pSqlStmt
, "ROWID = '");
1479 wxStrcat(pSqlStmt
, rowid
);
1480 wxStrcat(pSqlStmt
, "'");
1484 // Unable to delete by ROWID, so build a WHERE
1485 // clause based on the keyfields.
1486 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1487 wxStrcat(pSqlStmt
, whereClause
);
1490 wxStrcat(pSqlStmt
, pWhereClause
);
1492 case DB_DEL_MATCHING
:
1493 GetWhereClause(whereClause
, DB_WHERE_MATCHING
);
1494 wxStrcat(pSqlStmt
, whereClause
);
1498 } // GetDeleteStmt()
1501 /********** wxTable::GetWhereClause() **********/
1502 void wxTable::GetWhereClause(char *pWhereClause
, int typeOfWhere
, const char *qualTableName
)
1504 * Note: GetWhereClause() currently ignores timestamp columns.
1505 * They are not included as part of the where clause.
1508 bool moreThanOneColumn
= FALSE
;
1511 // Loop through the columns building a where clause as you go
1513 for (i
= 0; i
< noCols
; i
++)
1515 // Determine if this column should be included in the WHERE clause
1516 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1517 (typeOfWhere
== DB_WHERE_MATCHING
&& (! IsColNull(i
))))
1519 // Skip over timestamp columns
1520 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1522 // If there is more than 1 column, join them with the keyword "AND"
1523 if (moreThanOneColumn
)
1524 wxStrcat(pWhereClause
, " AND ");
1526 moreThanOneColumn
= TRUE
;
1527 // Concatenate where phrase for the column
1528 if (qualTableName
&& wxStrlen(qualTableName
))
1530 wxStrcat(pWhereClause
, qualTableName
);
1531 wxStrcat(pWhereClause
, ".");
1533 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1534 wxStrcat(pWhereClause
, " = ");
1535 switch(colDefs
[i
].SqlCtype
)
1538 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1541 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1544 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1547 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1550 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1553 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1556 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1559 wxStrcat(pWhereClause
, colValue
);
1562 } // wxTable::GetWhereClause()
1565 /********** wxTable::IsColNull() **********/
1566 bool wxTable::IsColNull(int colNo
)
1568 switch(colDefs
[colNo
].SqlCtype
)
1571 return(((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] == 0);
1573 return(( *((SWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1575 return(( *((UWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1577 return(( *((SDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1579 return(( *((UDWORD
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1581 return(( *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1583 return((*((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
)) == 0);
1584 case SQL_C_TIMESTAMP
:
1585 TIMESTAMP_STRUCT
*pDt
;
1586 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1587 if (pDt
->year
== 0 && pDt
->month
== 0 && pDt
->day
== 0)
1594 } // wxTable::IsColNull()
1597 /********** wxTable::CanSelectForUpdate() **********/
1598 bool wxTable::CanSelectForUpdate(void)
1600 if (pDb
->Dbms() == dbmsMY_SQL
)
1603 if (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1608 } // wxTable::CanSelectForUpdate()
1611 /********** wxTable::CanUpdByROWID() **********/
1612 bool wxTable::CanUpdByROWID(void)
1615 * NOTE: Returning FALSE for now until this can be debugged,
1616 * as the ROWID is not getting updated correctly
1620 if (pDb
->Dbms() == dbmsORACLE
)
1625 } // wxTable::CanUpdByROWID()
1628 /********** wxTable::IsCursorClosedOnCommit() **********/
1629 bool wxTable::IsCursorClosedOnCommit(void)
1631 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1636 } // wxTable::IsCursorClosedOnCommit()
1639 /********** wxTable::ClearMemberVars() **********/
1640 void wxTable::ClearMemberVars(void)
1642 // Loop through the columns setting each member variable to zero
1644 for (i
= 0; i
< noCols
; i
++)
1646 switch(colDefs
[i
].SqlCtype
)
1649 ((UCHAR FAR
*) colDefs
[i
].PtrDataObj
)[0] = 0;
1652 *((SWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1655 *((UWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1658 *((SDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1661 *((UDWORD
*) colDefs
[i
].PtrDataObj
) = 0;
1664 *((SFLOAT
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1667 *((SDOUBLE
*) colDefs
[i
].PtrDataObj
) = 0.0f
;
1669 case SQL_C_TIMESTAMP
:
1670 TIMESTAMP_STRUCT
*pDt
;
1671 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[i
].PtrDataObj
;
1684 } // wxTable::ClearMemberVars()
1687 /********** wxTable::SetQueryTimeout() **********/
1688 bool wxTable::SetQueryTimeout(UDWORD nSeconds
)
1690 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1691 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1692 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1693 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1694 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1695 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1696 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1697 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1699 // Completed Successfully
1702 } // wxTable::SetQueryTimeout()
1705 /********** wxTable::SetColDefs() **********/
1706 void wxTable::SetColDefs (int index
, const char *fieldName
, int dataType
, void *pData
,
1707 int cType
, int size
, bool keyField
, bool upd
,
1708 bool insAllow
, bool derivedCol
)
1710 if (!colDefs
) // May happen if the database connection fails
1713 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1715 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
1716 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
1719 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
1721 colDefs
[index
].DbDataType
= dataType
;
1722 colDefs
[index
].PtrDataObj
= pData
;
1723 colDefs
[index
].SqlCtype
= cType
;
1724 colDefs
[index
].SzDataObj
= size
;
1725 colDefs
[index
].KeyField
= keyField
;
1726 colDefs
[index
].DerivedCol
= derivedCol
;
1727 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1730 colDefs
[index
].Updateable
= FALSE
;
1731 colDefs
[index
].InsertAllowed
= FALSE
;
1735 colDefs
[index
].Updateable
= upd
;
1736 colDefs
[index
].InsertAllowed
= insAllow
;
1739 colDefs
[index
].Null
= FALSE
;
1741 } // wxTable::SetColDefs()
1744 /********** wxTable::SetColDef() **********/
1745 // BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
1746 //bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
1747 wxColDataPtr
* wxTable::SetColDefs (wxColInf
*pColInfs
, ULONG numCols
)
1750 wxColDataPtr
*pColDataPtrs
= NULL
;
1757 pColDataPtrs
= new wxColDataPtr
[numCols
+1];
1759 for (index
= 0; index
< numCols
; index
++)
1763 title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
1764 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",
1765 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);
1766 msg += " \nDB_DATA_TYPE: ";
1767 switch(pColInfs[index].dbDataType)
1769 case DB_DATA_TYPE_VARCHAR:
1770 msg += pDb->typeInfVarchar.TypeName; break;
1771 case DB_DATA_TYPE_INTEGER:
1772 msg += pDb->typeInfInteger.TypeName; break;
1773 case DB_DATA_TYPE_FLOAT:
1774 msg += pDb->typeInfFloat.TypeName; break;
1775 case DB_DATA_TYPE_DATE:
1776 msg += pDb->typeInfDate.TypeName; break;
1778 wxMessageBox(msg.GetData(),title.GetData());
1780 // Process the fields
1781 switch (pColInfs
[index
].dbDataType
)
1783 case DB_DATA_TYPE_VARCHAR
:
1786 // Tentative fix for Access. Relative to UNICODE?
1787 if (pColInfs
[index
].bufferLength
== 2*pColInfs
[index
].columnSize
)
1789 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].columnSize
+1];
1790 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
1795 // Still needed because iodbc (unix) returns 0 in columnSize
1796 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].bufferLength
+1];
1797 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].bufferLength
;
1799 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
1802 case DB_DATA_TYPE_INTEGER
:
1804 // Can be long or short
1805 if (pColInfs
[index
].bufferLength
== sizeof(long))
1807 pColDataPtrs
[index
].PtrDataObj
= new long;
1808 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
1809 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
1813 pColDataPtrs
[index
].PtrDataObj
= new short;
1814 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
1815 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
1819 case DB_DATA_TYPE_FLOAT
:
1821 // Can be float or double
1822 if (pColInfs
[index
].bufferLength
== sizeof(float))
1824 pColDataPtrs
[index
].PtrDataObj
= new float;
1825 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
1826 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
1830 pColDataPtrs
[index
].PtrDataObj
= new double;
1831 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
1832 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
1836 case DB_DATA_TYPE_DATE
:
1838 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
1839 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
1840 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
1845 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
1848 return (pColDataPtrs
);
1849 } // wxTable::SetColDef()
1852 /********** wxTable::SetCursor() **********/
1853 void wxTable::SetCursor(HSTMT
*hstmtActivate
)
1855 if (hstmtActivate
== DEFAULT_CURSOR
)
1856 hstmt
= *hstmtDefault
;
1858 hstmt
= *hstmtActivate
;
1860 } // wxTable::SetCursor()
1863 /********** wxTable::Count(const char *) **********/
1864 ULONG
wxTable::Count(const char *args
)
1867 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1871 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1872 sqlStmt
= "SELECT COUNT(";
1874 sqlStmt
+= ") FROM ";
1875 sqlStmt
+= queryTableName
;
1877 if (from
&& wxStrlen(from
))
1880 // Add the where clause if one is provided
1881 if (where
&& wxStrlen(where
))
1883 sqlStmt
+= " WHERE ";
1887 pDb
->WriteSqlLog(sqlStmt
.GetData());
1889 // Initialize the Count cursor if it's not already initialized
1892 hstmtCount
= NewCursor(FALSE
,FALSE
);
1898 // Execute the SQL statement
1899 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1901 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1906 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
1908 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1912 // Obtain the result
1913 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &l
, sizeof(l
), &cb
) != SQL_SUCCESS
)
1915 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1920 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
1921 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
1923 // Return the record count
1926 } // wxTable::Count()
1929 /********** wxTable::Refresh() **********/
1930 bool wxTable::Refresh(void)
1934 // Switch to the internal cursor so any active cursors are not corrupted
1935 HSTMT currCursor
= GetCursor();
1936 hstmt
= hstmtInternal
;
1938 // Save the where and order by clauses
1939 char *saveWhere
= where
;
1940 char *saveOrderBy
= orderBy
;
1942 // Build a where clause to refetch the record with. Try and use the
1943 // ROWID if it's available, ow use the key fields.
1944 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
1945 wxStrcpy(whereClause
, "");
1946 if (CanUpdByROWID())
1949 char rowid
[ROWID_LEN
+1];
1951 // Get the ROWID value. If not successful retreiving the ROWID,
1952 // simply fall down through the code and build the WHERE clause
1953 // based on the key fields.
1954 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1956 wxStrcat(whereClause
, queryTableName
);
1957 wxStrcat(whereClause
, ".ROWID = '");
1958 wxStrcat(whereClause
, rowid
);
1959 wxStrcat(whereClause
, "'");
1963 // If unable to use the ROWID, build a where clause from the keyfields
1964 if (wxStrlen(whereClause
) == 0)
1965 GetWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
1967 // Requery the record
1968 where
= whereClause
;
1973 if (result
&& !GetNext())
1976 // Switch back to original cursor
1977 SetCursor(&currCursor
);
1979 // Free the internal cursor
1980 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
1981 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
1983 // Restore the original where and order by clauses
1985 orderBy
= saveOrderBy
;
1989 } // wxTable::Refresh()
1992 /********** wxTable::SetNull(int colNo) **********/
1993 bool wxTable::SetNull(int colNo
)
1996 return(colDefs
[colNo
].Null
= TRUE
);
2000 } // wxTable::SetNull(int colNo)
2003 /********** wxTable::SetNull(char *colName) **********/
2004 bool wxTable::SetNull(const char *colName
)
2007 for (i
= 0; i
< noCols
; i
++)
2009 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2014 return(colDefs
[i
].Null
= TRUE
);
2018 } // wxTable::SetNull(char *colName)
2021 /********** wxTable::NewCursor() **********/
2022 HSTMT
*wxTable::NewCursor(bool setCursor
, bool bindColumns
)
2024 HSTMT
*newHSTMT
= new HSTMT
;
2029 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2031 pDb
->DispAllErrors(henv
, hdbc
);
2036 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2038 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2045 if(!bindCols(*newHSTMT
))
2053 SetCursor(newHSTMT
);
2057 } // wxTable::NewCursor()
2060 /********** wxTable::DeleteCursor() **********/
2061 bool wxTable::DeleteCursor(HSTMT
*hstmtDel
)
2065 if (!hstmtDel
) // Cursor already deleted
2068 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2070 pDb
->DispAllErrors(henv
, hdbc
);
2078 } // wxTable::DeleteCursor()
2080 #endif // wxUSE_ODBC