1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxDbTable class.
5 // Modified by: George Tasker
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence, plus:
12 // Notice: This class library and its intellectual design are free of charge for use,
13 // modification, enhancement, debugging under the following conditions:
14 // 1) These classes may only be used as part of the implementation of a
15 // wxWindows-based application
16 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17 // user groups free of all charges for use with the wxWindows library.
18 // 3) These classes may not be distributed as part of any other class library,
19 // DLL, text (written or electronic), other than a complete distribution of
20 // the wxWindows GUI development toolkit.
21 ///////////////////////////////////////////////////////////////////////////////
28 // Use this line for wxWindows v1.x
30 // Use this line for wxWindows v2.x
31 #include "wx/wxprec.h"
32 #include "wx/version.h"
34 #if wxMAJOR_VERSION == 2
36 #pragma implementation "dbtable.h"
40 #ifdef DBDEBUG_CONSOLE
41 #include "wx/ioswrap.h"
49 #if wxMAJOR_VERSION == 2
51 #include "wx/string.h"
52 #include "wx/object.h"
55 #include "wx/msgdlg.h"
58 #include "wx/filefn.h"
61 #if wxMAJOR_VERSION == 1
62 # 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 /********** wxDbTable::wxDbTable() **********/
106 wxDbTable::wxDbTable(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
= ""; // Where clause
123 orderBy
= ""; // Order By clause
124 from
= ""; // From clause
125 selectForUpdate
= FALSE
; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
128 wxStrcpy(tablePath
,"");
129 wxStrcpy(tableName
,"");
130 wxStrcpy(queryTableName
,"");
134 wxStrcpy(tableName
, tblName
); // Table Name
136 wxStrcpy(tablePath
, tblPath
); // Table Path - used for dBase files
140 if (qryTblName
) // Name of the table/view to query
141 wxStrcpy(queryTableName
, qryTblName
);
143 wxStrcpy(queryTableName
, tblName
);
148 pDb
->incrementTableCount();
151 tableID
= ++lastTableID
;
152 s
.sprintf("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName
,tableID
,pDb
);
155 wxTablesInUse
*tableInUse
;
156 tableInUse
= new wxTablesInUse();
157 tableInUse
->tableName
= tblName
;
158 tableInUse
->tableID
= tableID
;
159 tableInUse
->pDb
= pDb
;
160 TablesInUse
.Append(tableInUse
);
163 pDb
->WriteSqlLog(s
.c_str());
165 // Grab the HENV and HDBC from the wxDb object
166 henv
= pDb
->GetHENV();
167 hdbc
= pDb
->GetHDBC();
169 // Allocate space for column definitions
171 colDefs
= new wxDbColDef
[noCols
]; // Points to the first column defintion
173 // Allocate statement handles for the table
176 // Allocate a separate statement handle for performing inserts
177 if (SQLAllocStmt(hdbc
, &hstmtInsert
) != SQL_SUCCESS
)
178 pDb
->DispAllErrors(henv
, hdbc
);
179 // Allocate a separate statement handle for performing deletes
180 if (SQLAllocStmt(hdbc
, &hstmtDelete
) != SQL_SUCCESS
)
181 pDb
->DispAllErrors(henv
, hdbc
);
182 // Allocate a separate statement handle for performing updates
183 if (SQLAllocStmt(hdbc
, &hstmtUpdate
) != SQL_SUCCESS
)
184 pDb
->DispAllErrors(henv
, hdbc
);
186 // Allocate a separate statement handle for internal use
187 if (SQLAllocStmt(hdbc
, &hstmtInternal
) != SQL_SUCCESS
)
188 pDb
->DispAllErrors(henv
, hdbc
);
190 // Set the cursor type for the statement handles
191 cursorType
= SQL_CURSOR_STATIC
;
193 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
195 // Check to see if cursor type is supported
196 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
197 if (! wxStrcmp(pDb
->sqlState
, "01S02")) // Option Value Changed
199 // Datasource does not support static cursors. Driver
200 // will substitute a cursor type. Call SQLGetStmtOption()
201 // to determine which cursor type was selected.
202 if (SQLGetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, &cursorType
) != SQL_SUCCESS
)
203 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
204 #ifdef DBDEBUG_CONSOLE
205 cout
<< "Static cursor changed to: ";
208 case SQL_CURSOR_FORWARD_ONLY
:
209 cout
<< "Forward Only";
211 case SQL_CURSOR_STATIC
:
214 case SQL_CURSOR_KEYSET_DRIVEN
:
215 cout
<< "Keyset Driven";
217 case SQL_CURSOR_DYNAMIC
:
221 cout
<< endl
<< endl
;
224 if (pDb
->FwdOnlyCursors() && cursorType
!= SQL_CURSOR_FORWARD_ONLY
)
226 // Force the use of a forward only cursor...
227 cursorType
= SQL_CURSOR_FORWARD_ONLY
;
228 if (SQLSetStmtOption(hstmtInternal
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
230 // Should never happen
231 pDb
->GetNextError(henv
, hdbc
, hstmtInternal
);
238 pDb
->DispNextError();
239 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
242 #ifdef DBDEBUG_CONSOLE
244 cout
<< "Cursor Type set to STATIC" << endl
<< endl
;
249 // Set the cursor type for the INSERT statement handle
250 if (SQLSetStmtOption(hstmtInsert
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
251 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
252 // Set the cursor type for the DELETE statement handle
253 if (SQLSetStmtOption(hstmtDelete
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
254 pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
);
255 // Set the cursor type for the UPDATE statement handle
256 if (SQLSetStmtOption(hstmtUpdate
, SQL_CURSOR_TYPE
, SQL_CURSOR_FORWARD_ONLY
) != SQL_SUCCESS
)
257 pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
);
260 // Make the default cursor the active cursor
261 hstmtDefault
= GetNewCursor(FALSE
,FALSE
);
262 assert(hstmtDefault
);
263 hstmt
= *hstmtDefault
;
265 } // wxDbTable::wxDbTable()
268 /********** wxDbTable::~wxDbTable() **********/
269 wxDbTable::~wxDbTable()
274 s
.sprintf("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName
,tableID
,pDb
);
275 pDb
->WriteSqlLog(s
.c_str());
281 TablesInUse
.DeleteContents(TRUE
);
285 pNode
= TablesInUse
.First();
286 while (pNode
&& !found
)
288 if (((wxTablesInUse
*)pNode
->Data())->tableID
== tableID
)
291 if (!TablesInUse
.DeleteNode(pNode
))
292 wxLogDebug (s
.c_str(),wxT("Unable to delete node!"));
295 pNode
= pNode
->Next();
300 msg
.sprintf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s
.c_str());
301 wxLogDebug (msg
.c_str(),wxT("NOTICE..."));
306 // Decrement the wxDb table count
308 pDb
->decrementTableCount();
310 // Delete memory allocated for column definitions
314 // Free statement handles
318 if (SQLFreeStmt(hstmtInsert
, SQL_DROP
) != SQL_SUCCESS
)
319 pDb
->DispAllErrors(henv
, hdbc
);
322 if (SQLFreeStmt(hstmtDelete
, SQL_DROP
) != SQL_SUCCESS
)
325 if (SQLFreeStmt(hstmtUpdate
, SQL_DROP
) != SQL_SUCCESS
)
326 pDb
->DispAllErrors(henv
, hdbc
);
330 if (SQLFreeStmt(hstmtInternal
, SQL_DROP
) != SQL_SUCCESS
)
331 pDb
->DispAllErrors(henv
, hdbc
);
333 // Delete dynamically allocated cursors
335 DeleteCursor(hstmtDefault
);
338 DeleteCursor(hstmtCount
);
340 } // wxDbTable::~wxDbTable()
344 /***************************** PRIVATE FUNCTIONS *****************************/
348 /********** wxDbTable::bindInsertParams() **********/
349 bool wxDbTable::bindInsertParams(void)
356 UDWORD precision
= 0;
359 // Bind each column (that can be inserted) of the table to a parameter marker
361 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
363 if (! colDefs
[i
].InsertAllowed
)
365 switch(colDefs
[i
].DbDataType
)
367 case DB_DATA_TYPE_VARCHAR
:
368 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
369 precision
= colDefs
[i
].SzDataObj
;
372 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
374 colDefs
[i
].CbValue
= SQL_NTS
;
376 case DB_DATA_TYPE_INTEGER
:
377 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
378 precision
= pDb
->GetTypeInfInteger().Precision
;
381 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
383 colDefs
[i
].CbValue
= 0;
385 case DB_DATA_TYPE_FLOAT
:
386 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
387 precision
= pDb
->GetTypeInfFloat().Precision
;
388 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
389 // SQL Sybase Anywhere v5.5 returned a negative number for the
390 // MaxScale. This caused ODBC to kick out an error on ibscale.
391 // I check for this here and set the scale = precision.
393 // scale = (short) precision;
395 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
397 colDefs
[i
].CbValue
= 0;
399 case DB_DATA_TYPE_DATE
:
400 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
401 precision
= pDb
->GetTypeInfDate().Precision
;
404 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
406 colDefs
[i
].CbValue
= 0;
411 //RG-NULL if (colDefs[i].Null)
413 //RG-NULL colDefs[i].CbValue = SQL_NULL_DATA;
414 //RG-NULL colDefs[i].Null = FALSE;
417 if (SQLBindParameter(hstmtInsert
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
418 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
419 precision
+1,&colDefs
[i
].CbValue
) != SQL_SUCCESS
)
421 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
425 // Completed successfully
428 } // wxDbTable::bindInsertParams()
431 /********** wxDbTable::bindUpdateParams() **********/
432 bool wxDbTable::bindUpdateParams(void)
439 UDWORD precision
= 0;
442 // Bind each UPDATEABLE column of the table to a parameter marker
444 for (i
= 0, colNo
= 1; i
< noCols
; i
++)
446 if (! colDefs
[i
].Updateable
)
448 switch(colDefs
[i
].DbDataType
)
450 case DB_DATA_TYPE_VARCHAR
:
451 fSqlType
= pDb
->GetTypeInfVarchar().FsqlType
;
452 precision
= colDefs
[i
].SzDataObj
;
455 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
457 colDefs
[i
].CbValue
= SQL_NTS
;
459 case DB_DATA_TYPE_INTEGER
:
460 fSqlType
= pDb
->GetTypeInfInteger().FsqlType
;
461 precision
= pDb
->GetTypeInfInteger().Precision
;
464 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
466 colDefs
[i
].CbValue
= 0;
468 case DB_DATA_TYPE_FLOAT
:
469 fSqlType
= pDb
->GetTypeInfFloat().FsqlType
;
470 precision
= pDb
->GetTypeInfFloat().Precision
;
471 scale
= pDb
->GetTypeInfFloat().MaximumScale
;
472 // SQL Sybase Anywhere v5.5 returned a negative number for the
473 // MaxScale. This caused ODBC to kick out an error on ibscale.
474 // I check for this here and set the scale = precision.
476 // scale = (short) precision;
478 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
480 colDefs
[i
].CbValue
= 0;
482 case DB_DATA_TYPE_DATE
:
483 fSqlType
= pDb
->GetTypeInfDate().FsqlType
;
484 precision
= pDb
->GetTypeInfDate().Precision
;
487 colDefs
[i
].CbValue
= SQL_NULL_DATA
;
489 colDefs
[i
].CbValue
= 0;
493 if (SQLBindParameter(hstmtUpdate
, colNo
++, SQL_PARAM_INPUT
, colDefs
[i
].SqlCtype
,
494 fSqlType
, precision
, scale
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
495 precision
+1, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
497 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
501 // Completed successfully
504 } // wxDbTable::bindUpdateParams()
507 /********** wxDbTable::bindCols() **********/
508 bool wxDbTable::bindCols(HSTMT cursor
)
510 //RG-NULL static SDWORD cb;
512 // Bind each column of the table to a memory address for fetching data
514 for (i
= 0; i
< noCols
; i
++)
516 if (SQLBindCol(cursor
, i
+1, colDefs
[i
].SqlCtype
, (UCHAR
*) colDefs
[i
].PtrDataObj
,
517 //RG-NULL colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
518 colDefs
[i
].SzDataObj
, &colDefs
[i
].CbValue
) != SQL_SUCCESS
)
520 return (pDb
->DispAllErrors(henv
, hdbc
, cursor
));
524 // Completed successfully
527 } // wxDbTable::bindCols()
530 /********** wxDbTable::getRec() **********/
531 bool wxDbTable::getRec(UWORD fetchType
)
535 if (!pDb
->FwdOnlyCursors())
537 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
541 retcode
= SQLExtendedFetch(hstmt
, fetchType
, 0, &cRowsFetched
, &rowStatus
);
542 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
544 if (retcode
== SQL_NO_DATA_FOUND
)
547 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
551 // Set the Null member variable to indicate the Null state
552 // of each column just read in.
554 for (i
= 0; i
< noCols
; i
++)
555 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
560 // Fetch the next record from the record set
561 retcode
= SQLFetch(hstmt
);
562 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
564 if (retcode
== SQL_NO_DATA_FOUND
)
567 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
571 // Set the Null member variable to indicate the Null state
572 // of each column just read in.
574 for (i
= 0; i
< noCols
; i
++)
575 colDefs
[i
].Null
= (colDefs
[i
].CbValue
== SQL_NULL_DATA
);
579 // Completed successfully
582 } // wxDbTable::getRec()
585 /********** wxDbTable::execDelete() **********/
586 bool wxDbTable::execDelete(const char *pSqlStmt
)
588 // Execute the DELETE statement
589 if (SQLExecDirect(hstmtDelete
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
590 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
592 // Record deleted successfully
595 } // wxDbTable::execDelete()
598 /********** wxDbTable::execUpdate() **********/
599 bool wxDbTable::execUpdate(const char *pSqlStmt
)
601 // Execute the UPDATE statement
602 if (SQLExecDirect(hstmtUpdate
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
603 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
605 // Record deleted successfully
608 } // wxDbTable::execUpdate()
611 /********** wxDbTable::query() **********/
612 bool wxDbTable::query(int queryType
, bool forUpdate
, bool distinct
, const char *pSqlStmt
)
614 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
617 // The user may wish to select for update, but the DBMS may not be capable
618 selectForUpdate
= CanSelectForUpdate();
620 selectForUpdate
= FALSE
;
622 // Set the SQL SELECT string
623 if (queryType
!= DB_SELECT_STATEMENT
) // A select statement was not passed in,
624 { // so generate a select statement.
625 BuildSelectStmt(sqlStmt
, queryType
, distinct
);
626 pDb
->WriteSqlLog(sqlStmt
);
629 This is the block of code that got added during the 2.2.1 merge with
630 the 2.2 main branch that somehow got added here when it should not have. - gt
633 wxStrcpy(sqlStmt, pSqlStmt);
635 SQLFreeStmt(hstmt, SQL_CLOSE);
636 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) == SQL_SUCCESS)
640 pDb->DispAllErrors(henv, hdbc, hstmt);
644 // Make sure the cursor is closed first
645 if (!CloseCursor(hstmt
))
648 // Execute the SQL SELECT statement
650 retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) (queryType
== DB_SELECT_STATEMENT
? pSqlStmt
: sqlStmt
), SQL_NTS
);
651 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
652 return(pDb
->DispAllErrors(henv
, hdbc
, hstmt
));
654 // Completed successfully
657 } // wxDbTable::query()
660 /***************************** PUBLIC FUNCTIONS *****************************/
663 /********** wxDbTable::Open() **********/
664 bool wxDbTable::Open(bool checkPrivileges
)
674 // Verify that the table exists in the database
675 if (!pDb
->TableExists(tableName
,/*pDb->GetUsername()*/NULL
,tablePath
))
677 s
= "Table/view does not exist in the database";
678 if ( *(pDb
->dbInf
.accessibleTables
) == 'Y')
679 s
+= ", or you have no permissions.\n";
683 else if (checkPrivileges
)
685 // Verify the user has rights to access the table.
686 // Shortcut boolean evaluation to optimize out call to
689 // Unfortunately this optimization doesn't seem to be
691 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
692 !pDb
->TablePrivileges(tableName
,"SELECT",NULL
,pDb
->GetUsername(),tablePath
))
693 s
= "Current logged in user does not have sufficient privileges to access this table.\n";
700 if (wxStrcmp(tablePath
,""))
701 p
.sprintf("Error opening '%s/%s'.\n",tablePath
,tableName
);
703 p
.sprintf("Error opening '%s'.\n", tableName
);
706 pDb
->LogError(p
.GetData());
711 // Bind the member variables for field exchange between
712 // the wxDbTable object and the ODBC record.
715 if (!bindInsertParams()) // Inserts
718 if (!bindUpdateParams()) // Updates
722 if (!bindCols(*hstmtDefault
)) // Selects
725 if (!bindCols(hstmtInternal
)) // Internal use only
729 * Do NOT bind the hstmtCount cursor!!!
732 // Build an insert statement using parameter markers
733 if (!queryOnly
&& noCols
> 0)
735 bool needComma
= FALSE
;
736 sqlStmt
.sprintf("INSERT INTO %s (", tableName
);
737 for (i
= 0; i
< noCols
; i
++)
739 if (! colDefs
[i
].InsertAllowed
)
743 sqlStmt
+= colDefs
[i
].ColName
;
747 sqlStmt
+= ") VALUES (";
749 int insertableCount
= 0;
751 for (i
= 0; i
< noCols
; i
++)
753 if (! colDefs
[i
].InsertAllowed
)
763 // Prepare the insert statement for execution
766 if (SQLPrepare(hstmtInsert
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
767 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
773 // Completed successfully
776 } // wxDbTable::Open()
779 /********** wxDbTable::Query() **********/
780 bool wxDbTable::Query(bool forUpdate
, bool distinct
)
783 return(query(DB_SELECT_WHERE
, forUpdate
, distinct
));
785 } // wxDbTable::Query()
788 /********** wxDbTable::QueryBySqlStmt() **********/
789 bool wxDbTable::QueryBySqlStmt(const char *pSqlStmt
)
791 pDb
->WriteSqlLog(pSqlStmt
);
793 return(query(DB_SELECT_STATEMENT
, FALSE
, FALSE
, pSqlStmt
));
795 } // wxDbTable::QueryBySqlStmt()
798 /********** wxDbTable::QueryMatching() **********/
799 bool wxDbTable::QueryMatching(bool forUpdate
, bool distinct
)
802 return(query(DB_SELECT_MATCHING
, forUpdate
, distinct
));
804 } // wxDbTable::QueryMatching()
807 /********** wxDbTable::QueryOnKeyFields() **********/
808 bool wxDbTable::QueryOnKeyFields(bool forUpdate
, bool distinct
)
811 return(query(DB_SELECT_KEYFIELDS
, forUpdate
, distinct
));
813 } // wxDbTable::QueryOnKeyFields()
816 /********** wxDbTable::GetPrev() **********/
817 bool wxDbTable::GetPrev(void)
819 if (pDb
->FwdOnlyCursors())
821 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
825 return(getRec(SQL_FETCH_PRIOR
));
827 } // wxDbTable::GetPrev()
830 /********** wxDbTable::operator-- **********/
831 bool wxDbTable::operator--(int)
833 if (pDb
->FwdOnlyCursors())
835 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
839 return(getRec(SQL_FETCH_PRIOR
));
841 } // wxDbTable::operator--
844 /********** wxDbTable::GetFirst() **********/
845 bool wxDbTable::GetFirst(void)
847 if (pDb
->FwdOnlyCursors())
849 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
853 return(getRec(SQL_FETCH_FIRST
));
855 } // wxDbTable::GetFirst()
858 /********** wxDbTable::GetLast() **********/
859 bool wxDbTable::GetLast(void)
861 if (pDb
->FwdOnlyCursors())
863 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
867 return(getRec(SQL_FETCH_LAST
));
869 } // wxDbTable::GetLast()
872 /********** wxDbTable::BuildSelectStmt() **********/
873 void wxDbTable::BuildSelectStmt(char *pSqlStmt
, int typeOfSelect
, bool distinct
)
875 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
879 // Build a select statement to query the database
880 wxStrcpy(pSqlStmt
, "SELECT ");
882 // SELECT DISTINCT values only?
884 wxStrcat(pSqlStmt
, "DISTINCT ");
886 // Was a FROM clause specified to join tables to the base table?
887 // Available for ::Query() only!!!
888 bool appendFromClause
= FALSE
;
889 #if wxODBC_BACKWARD_COMPATABILITY
890 if (typeOfSelect
== DB_SELECT_WHERE
&& from
&& wxStrlen(from
))
891 appendFromClause
= TRUE
;
893 if (typeOfSelect
== DB_SELECT_WHERE
&& from
.Length())
894 appendFromClause
= TRUE
;
897 // Add the column list
899 for (i
= 0; i
< noCols
; i
++)
901 // If joining tables, the base table column names must be qualified to avoid ambiguity
902 if (appendFromClause
)
904 wxStrcat(pSqlStmt
, queryTableName
);
905 wxStrcat(pSqlStmt
, ".");
907 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
909 wxStrcat(pSqlStmt
, ",");
912 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
913 // the ROWID if querying distinct records. The rowid will always be unique.
914 if (!distinct
&& CanUpdByROWID())
916 // If joining tables, the base table column names must be qualified to avoid ambiguity
917 if (appendFromClause
)
919 wxStrcat(pSqlStmt
, ",");
920 wxStrcat(pSqlStmt
, queryTableName
);
921 wxStrcat(pSqlStmt
, ".ROWID");
924 wxStrcat(pSqlStmt
, ",ROWID");
927 // Append the FROM tablename portion
928 wxStrcat(pSqlStmt
, " FROM ");
929 wxStrcat(pSqlStmt
, queryTableName
);
931 // Sybase uses the HOLDLOCK keyword to lock a record during query.
932 // The HOLDLOCK keyword follows the table name in the from clause.
933 // Each table in the from clause must specify HOLDLOCK or
934 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
935 // is parsed but ignored in SYBASE Transact-SQL.
936 if (selectForUpdate
&& (pDb
->Dbms() == dbmsSYBASE_ASA
|| pDb
->Dbms() == dbmsSYBASE_ASE
))
937 wxStrcat(pSqlStmt
, " HOLDLOCK");
939 if (appendFromClause
)
940 wxStrcat(pSqlStmt
, from
);
942 // Append the WHERE clause. Either append the where clause for the class
943 // or build a where clause. The typeOfSelect determines this.
946 case DB_SELECT_WHERE
:
947 #if wxODBC_BACKWARD_COMPATABILITY
948 if (where
&& wxStrlen(where
)) // May not want a where clause!!!
950 if (where
.Length()) // May not want a where clause!!!
953 wxStrcat(pSqlStmt
, " WHERE ");
954 wxStrcat(pSqlStmt
, where
);
957 case DB_SELECT_KEYFIELDS
:
958 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
959 if (wxStrlen(whereClause
))
961 wxStrcat(pSqlStmt
, " WHERE ");
962 wxStrcat(pSqlStmt
, whereClause
);
965 case DB_SELECT_MATCHING
:
966 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
967 if (wxStrlen(whereClause
))
969 wxStrcat(pSqlStmt
, " WHERE ");
970 wxStrcat(pSqlStmt
, whereClause
);
975 // Append the ORDER BY clause
976 #if wxODBC_BACKWARD_COMPATABILITY
977 if (orderBy
&& wxStrlen(orderBy
))
979 if (orderBy
.Length())
982 wxStrcat(pSqlStmt
, " ORDER BY ");
983 wxStrcat(pSqlStmt
, orderBy
);
986 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
987 // parses the FOR UPDATE clause but ignores it. See the comment above on the
988 // HOLDLOCK for Sybase.
989 if (selectForUpdate
&& CanSelectForUpdate())
990 wxStrcat(pSqlStmt
, " FOR UPDATE");
992 } // wxDbTable::BuildSelectStmt()
995 /********** wxDbTable::GetRowNum() **********/
996 UWORD
wxDbTable::GetRowNum(void)
1000 if (SQLGetStmtOption(hstmt
, SQL_ROW_NUMBER
, (UCHAR
*) &rowNum
) != SQL_SUCCESS
)
1002 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1006 // Completed successfully
1007 return((UWORD
) rowNum
);
1009 } // wxDbTable::GetRowNum()
1012 /********** wxDbTable::CloseCursor() **********/
1013 bool wxDbTable::CloseCursor(HSTMT cursor
)
1015 if (SQLFreeStmt(cursor
, SQL_CLOSE
) != SQL_SUCCESS
)
1016 return(pDb
->DispAllErrors(henv
, hdbc
, cursor
));
1018 // Completed successfully
1021 } // wxDbTable::CloseCursor()
1024 /********** wxDbTable::CreateTable() **********/
1025 bool wxDbTable::CreateTable(bool attemptDrop
)
1033 #ifdef DBDEBUG_CONSOLE
1034 cout
<< "Creating Table " << tableName
<< "..." << endl
;
1038 if (attemptDrop
&& !DropTable())
1042 #ifdef DBDEBUG_CONSOLE
1043 for (i
= 0; i
< noCols
; i
++)
1045 // Exclude derived columns since they are NOT part of the base table
1046 if (colDefs
[i
].DerivedCol
)
1048 cout
<< i
+ 1 << ": " << colDefs
[i
].ColName
<< "; ";
1049 switch(colDefs
[i
].DbDataType
)
1051 case DB_DATA_TYPE_VARCHAR
:
1052 cout
<< pDb
->typeInfVarchar
.TypeName
<< "(" << colDefs
[i
].SzDataObj
<< ")";
1054 case DB_DATA_TYPE_INTEGER
:
1055 cout
<< pDb
->typeInfInteger
.TypeName
;
1057 case DB_DATA_TYPE_FLOAT
:
1058 cout
<< pDb
->typeInfFloat
.TypeName
;
1060 case DB_DATA_TYPE_DATE
:
1061 cout
<< pDb
->typeInfDate
.TypeName
;
1068 // Build a CREATE TABLE string from the colDefs structure.
1069 bool needComma
= FALSE
;
1070 sqlStmt
.sprintf("CREATE TABLE %s (", tableName
);
1072 for (i
= 0; i
< noCols
; i
++)
1074 // Exclude derived columns since they are NOT part of the base table
1075 if (colDefs
[i
].DerivedCol
)
1081 sqlStmt
+= colDefs
[i
].ColName
;
1084 switch(colDefs
[i
].DbDataType
)
1086 case DB_DATA_TYPE_VARCHAR
:
1087 sqlStmt
+= pDb
->GetTypeInfVarchar().TypeName
;
1089 case DB_DATA_TYPE_INTEGER
:
1090 sqlStmt
+= pDb
->GetTypeInfInteger().TypeName
;
1092 case DB_DATA_TYPE_FLOAT
:
1093 sqlStmt
+= pDb
->GetTypeInfFloat().TypeName
;
1095 case DB_DATA_TYPE_DATE
:
1096 sqlStmt
+= pDb
->GetTypeInfDate().TypeName
;
1099 // For varchars, append the size of the string
1100 if (colDefs
[i
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1103 // wxStrcat(sqlStmt, "(");
1104 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
1105 // wxStrcat(sqlStmt, ")");
1106 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
1107 sqlStmt
+= s
.c_str();
1110 if (pDb
->Dbms() == dbmsDB2
||
1111 pDb
->Dbms() == dbmsMY_SQL
||
1112 pDb
->Dbms() == dbmsSYBASE_ASE
||
1113 pDb
->Dbms() == dbmsMS_SQL_SERVER
)
1115 if (colDefs
[i
].KeyField
)
1117 sqlStmt
+= " NOT NULL";
1123 // If there is a primary key defined, include it in the create statement
1124 for (i
= j
= 0; i
< noCols
; i
++)
1126 if (colDefs
[i
].KeyField
)
1132 if (j
&& pDb
->Dbms() != dbmsDBASE
) // Found a keyfield
1134 if (pDb
->Dbms() != dbmsMY_SQL
)
1136 sqlStmt
+= ",CONSTRAINT ";
1137 sqlStmt
+= tableName
;
1138 sqlStmt
+= "_PIDX PRIMARY KEY (";
1142 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1143 sqlStmt
+= ", PRIMARY KEY (";
1146 // List column name(s) of column(s) comprising the primary key
1147 for (i
= j
= 0; i
< noCols
; i
++)
1149 if (colDefs
[i
].KeyField
)
1151 if (j
++) // Multi part key, comma separate names
1153 sqlStmt
+= colDefs
[i
].ColName
;
1158 // Append the closing parentheses for the create table statement
1161 pDb
->WriteSqlLog(sqlStmt
.c_str());
1163 #ifdef DBDEBUG_CONSOLE
1164 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1167 // Execute the CREATE TABLE statement
1168 RETCODE retcode
= SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
);
1169 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1171 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1172 pDb
->RollbackTrans();
1177 // Commit the transaction and close the cursor
1178 if (!pDb
->CommitTrans())
1180 if (!CloseCursor(hstmt
))
1183 // Database table created successfully
1186 } // wxDbTable::CreateTable()
1189 /********** wxDbTable::DropTable() **********/
1190 bool wxDbTable::DropTable()
1192 // NOTE: This function returns TRUE if the Table does not exist, but
1193 // only for identified databases. Code will need to be added
1194 // below for any other databases when those databases are defined
1195 // to handle this situation consistently
1199 sqlStmt
.sprintf("DROP TABLE %s", tableName
);
1201 pDb
->WriteSqlLog(sqlStmt
.c_str());
1203 #ifdef DBDEBUG_CONSOLE
1204 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1207 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1209 // Check for "Base table not found" error and ignore
1210 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1211 if (wxStrcmp(pDb
->sqlState
,"S0002") && wxStrcmp(pDb
->sqlState
, "S1000")) // "Base table not found"
1213 // Check for product specific error codes
1214 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // 5.x (and lower?)
1215 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"37000")) ||
1216 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01"))))
1218 pDb
->DispNextError();
1219 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1220 pDb
->RollbackTrans();
1227 // Commit the transaction and close the cursor
1228 if (! pDb
->CommitTrans())
1230 if (! CloseCursor(hstmt
))
1234 } // wxDbTable::DropTable()
1237 /********** wxDbTable::CreateIndex() **********/
1238 bool wxDbTable::CreateIndex(const char * idxName
, bool unique
, int noIdxCols
, wxDbIdxDef
*pIdxDefs
, bool attemptDrop
)
1242 // Drop the index first
1243 if (attemptDrop
&& !DropIndex(idxName
))
1246 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1247 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1248 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1249 // table was created, then months later you determine that an additional index while
1250 // give better performance, so you want to add an index).
1252 // The following block of code will modify the column definition to make the column be
1253 // defined with the "NOT NULL" qualifier.
1254 if (pDb
->Dbms() == dbmsMY_SQL
)
1259 for (i
= 0; i
< noIdxCols
&& ok
; i
++)
1263 // Find the column definition that has the ColName that matches the
1264 // index column name. We need to do this to get the DB_DATA_TYPE of
1265 // the index column, as MySQL's syntax for the ALTER column requires
1267 while (!found
&& (j
< this->noCols
))
1269 if (wxStrcmp(colDefs
[j
].ColName
,pIdxDefs
[i
].ColName
) == 0)
1277 wxString typeNameAndSize
;
1279 switch(colDefs
[j
].DbDataType
)
1281 case DB_DATA_TYPE_VARCHAR
:
1282 typeNameAndSize
= pDb
->GetTypeInfVarchar().TypeName
;
1284 case DB_DATA_TYPE_INTEGER
:
1285 typeNameAndSize
= pDb
->GetTypeInfInteger().TypeName
;
1287 case DB_DATA_TYPE_FLOAT
:
1288 typeNameAndSize
= pDb
->GetTypeInfFloat().TypeName
;
1290 case DB_DATA_TYPE_DATE
:
1291 typeNameAndSize
= pDb
->GetTypeInfDate().TypeName
;
1295 // For varchars, append the size of the string
1296 if (colDefs
[j
].DbDataType
== DB_DATA_TYPE_VARCHAR
)
1299 s
.sprintf("(%d)", colDefs
[i
].SzDataObj
);
1300 typeNameAndSize
+= s
.c_str();
1303 sqlStmt
.sprintf("ALTER TABLE %s MODIFY %s %s NOT NULL",tableName
,pIdxDefs
[i
].ColName
,typeNameAndSize
.c_str());
1304 ok
= pDb
->ExecSql(sqlStmt
.c_str());
1308 wxODBC_ERRORS retcode
;
1309 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1310 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1311 // This line is just here for debug checking of the value
1312 retcode
= (wxODBC_ERRORS
)pDb
->DB_STATUS
;
1322 pDb
->RollbackTrans();
1327 // Build a CREATE INDEX statement
1328 sqlStmt
= "CREATE ";
1330 sqlStmt
+= "UNIQUE ";
1332 sqlStmt
+= "INDEX ";
1335 sqlStmt
+= tableName
;
1338 // Append list of columns making up index
1340 for (i
= 0; i
< noIdxCols
; i
++)
1342 sqlStmt
+= pIdxDefs
[i
].ColName
;
1343 /* Postgres doesn't cope with ASC */
1344 if (pDb
->Dbms() != dbmsPOSTGRES
)
1346 if (pIdxDefs
[i
].Ascending
)
1352 if ((i
+ 1) < noIdxCols
)
1356 // Append closing parentheses
1359 pDb
->WriteSqlLog(sqlStmt
.c_str());
1361 #ifdef DBDEBUG_CONSOLE
1362 cout
<< endl
<< sqlStmt
.c_str() << endl
<< endl
;
1365 // Execute the CREATE INDEX statement
1366 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1368 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1369 pDb
->RollbackTrans();
1374 // Commit the transaction and close the cursor
1375 if (! pDb
->CommitTrans())
1377 if (! CloseCursor(hstmt
))
1380 // Index Created Successfully
1383 } // wxDbTable::CreateIndex()
1386 /********** wxDbTable::DropIndex() **********/
1387 bool wxDbTable::DropIndex(const char * idxName
)
1389 // NOTE: This function returns TRUE if the Index does not exist, but
1390 // only for identified databases. Code will need to be added
1391 // below for any other databases when those databases are defined
1392 // to handle this situation consistently
1396 if (pDb
->Dbms() == dbmsACCESS
|| pDb
->Dbms() == dbmsMY_SQL
)
1397 sqlStmt
.sprintf("DROP INDEX %s ON %s",idxName
,tableName
);
1398 else if ((pDb
->Dbms() == dbmsMS_SQL_SERVER
) ||
1399 (pDb
->Dbms() == dbmsSYBASE_ASE
))
1400 sqlStmt
.sprintf("DROP INDEX %s.%s",tableName
,idxName
);
1402 sqlStmt
.sprintf("DROP INDEX %s",idxName
);
1404 pDb
->WriteSqlLog(sqlStmt
.c_str());
1406 #ifdef DBDEBUG_CONSOLE
1407 cout
<< endl
<< sqlStmt
.c_str() << endl
;
1410 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
1412 // Check for "Index not found" error and ignore
1413 pDb
->GetNextError(henv
, hdbc
, hstmt
);
1414 if (wxStrcmp(pDb
->sqlState
,"S0012")) // "Index not found"
1416 // Check for product specific error codes
1417 if (!((pDb
->Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(pDb
->sqlState
,"42000")) || // v5.x (and lower?)
1418 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"37000")) ||
1419 (pDb
->Dbms() == dbmsMS_SQL_SERVER
&& !wxStrcmp(pDb
->sqlState
,"S1000")) ||
1420 (pDb
->Dbms() == dbmsSYBASE_ASE
&& !wxStrcmp(pDb
->sqlState
,"S0002")) || // Base table not found
1421 (pDb
->Dbms() == dbmsMY_SQL
&& !wxStrcmp(pDb
->sqlState
,"42S12")) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1422 (pDb
->Dbms() == dbmsPOSTGRES
&& !wxStrcmp(pDb
->sqlState
,"08S01"))
1425 pDb
->DispNextError();
1426 pDb
->DispAllErrors(henv
, hdbc
, hstmt
);
1427 pDb
->RollbackTrans();
1434 // Commit the transaction and close the cursor
1435 if (! pDb
->CommitTrans())
1437 if (! CloseCursor(hstmt
))
1441 } // wxDbTable::DropIndex()
1444 /********** wxDbTable::SetOrderByColNums() **********/
1445 bool wxDbTable::SetOrderByColNums(int first
, ... )
1453 va_start(argptr
, first
); /* Initialize variable arguments. */
1454 while (!abort
&& (colNo
!= wxDB_NO_MORE_COLUMN_NUMBERS
))
1456 // Make sure the passed in column number
1457 // is within the valid range of columns
1459 // Valid columns are 0 thru noCols-1
1460 if (colNo
>= noCols
|| colNo
< 0)
1469 tempStr
+= colDefs
[colNo
].ColName
;
1470 colNo
= va_arg (argptr
, int);
1472 va_end (argptr
); /* Reset variable arguments. */
1474 SetOrderByClause(tempStr
.c_str());
1477 } // wxDbTable::SetOrderByColNums()
1480 /********** wxDbTable::Insert() **********/
1481 int wxDbTable::Insert(void)
1484 if (queryOnly
|| !insertable
)
1489 // Insert the record by executing the already prepared insert statement
1491 retcode
=SQLExecute(hstmtInsert
);
1492 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1494 // Check to see if integrity constraint was violated
1495 pDb
->GetNextError(henv
, hdbc
, hstmtInsert
);
1496 if (! wxStrcmp(pDb
->sqlState
, "23000")) // Integrity constraint violated
1497 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1500 pDb
->DispNextError();
1501 pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
);
1506 // Record inserted into the datasource successfully
1509 } // wxDbTable::Insert()
1512 /********** wxDbTable::Update() **********/
1513 bool wxDbTable::Update(void)
1519 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1521 // Build the SQL UPDATE statement
1522 BuildUpdateStmt(sqlStmt
, DB_UPD_KEYFIELDS
);
1524 pDb
->WriteSqlLog(sqlStmt
);
1526 #ifdef DBDEBUG_CONSOLE
1527 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1530 // Execute the SQL UPDATE statement
1531 return(execUpdate(sqlStmt
));
1533 } // wxDbTable::Update()
1536 /********** wxDbTable::Update(pSqlStmt) **********/
1537 bool wxDbTable::Update(const char *pSqlStmt
)
1543 pDb
->WriteSqlLog(pSqlStmt
);
1545 return(execUpdate(pSqlStmt
));
1547 } // wxDbTable::Update(pSqlStmt)
1550 /********** wxDbTable::UpdateWhere() **********/
1551 bool wxDbTable::UpdateWhere(const char *pWhereClause
)
1557 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1559 // Build the SQL UPDATE statement
1560 BuildUpdateStmt(sqlStmt
, DB_UPD_WHERE
, pWhereClause
);
1562 pDb
->WriteSqlLog(sqlStmt
);
1564 #ifdef DBDEBUG_CONSOLE
1565 cout
<< endl
<< sqlStmt
<< endl
<< endl
;
1568 // Execute the SQL UPDATE statement
1569 return(execUpdate(sqlStmt
));
1571 } // wxDbTable::UpdateWhere()
1574 /********** wxDbTable::Delete() **********/
1575 bool wxDbTable::Delete(void)
1581 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1583 // Build the SQL DELETE statement
1584 BuildDeleteStmt(sqlStmt
, DB_DEL_KEYFIELDS
);
1586 pDb
->WriteSqlLog(sqlStmt
);
1588 // Execute the SQL DELETE statement
1589 return(execDelete(sqlStmt
));
1591 } // wxDbTable::Delete()
1594 /********** wxDbTable::DeleteWhere() **********/
1595 bool wxDbTable::DeleteWhere(const char *pWhereClause
)
1601 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1603 // Build the SQL DELETE statement
1604 BuildDeleteStmt(sqlStmt
, DB_DEL_WHERE
, pWhereClause
);
1606 pDb
->WriteSqlLog(sqlStmt
);
1608 // Execute the SQL DELETE statement
1609 return(execDelete(sqlStmt
));
1611 } // wxDbTable::DeleteWhere()
1614 /********** wxDbTable::DeleteMatching() **********/
1615 bool wxDbTable::DeleteMatching(void)
1621 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1623 // Build the SQL DELETE statement
1624 BuildDeleteStmt(sqlStmt
, DB_DEL_MATCHING
);
1626 pDb
->WriteSqlLog(sqlStmt
);
1628 // Execute the SQL DELETE statement
1629 return(execDelete(sqlStmt
));
1631 } // wxDbTable::DeleteMatching()
1634 /********** wxDbTable::BuildUpdateStmt() **********/
1635 void wxDbTable::BuildUpdateStmt(char *pSqlStmt
, int typeOfUpd
, const char *pWhereClause
)
1641 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1642 bool firstColumn
= TRUE
;
1645 sprintf(pSqlStmt
, "UPDATE %s SET ", tableName
);
1647 // Append a list of columns to be updated
1649 for (i
= 0; i
< noCols
; i
++)
1651 // Only append Updateable columns
1652 if (colDefs
[i
].Updateable
)
1655 wxStrcat(pSqlStmt
, ",");
1657 firstColumn
= FALSE
;
1658 wxStrcat(pSqlStmt
, colDefs
[i
].ColName
);
1659 wxStrcat(pSqlStmt
, " = ?");
1663 // Append the WHERE clause to the SQL UPDATE statement
1664 wxStrcat(pSqlStmt
, " WHERE ");
1667 case DB_UPD_KEYFIELDS
:
1668 // If the datasource supports the ROWID column, build
1669 // the where on ROWID for efficiency purposes.
1670 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1671 if (CanUpdByROWID())
1674 char rowid
[wxDB_ROWID_LEN
];
1676 // Get the ROWID value. If not successful retreiving the ROWID,
1677 // simply fall down through the code and build the WHERE clause
1678 // based on the key fields.
1679 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1681 wxStrcat(pSqlStmt
, "ROWID = '");
1682 wxStrcat(pSqlStmt
, rowid
);
1683 wxStrcat(pSqlStmt
, "'");
1687 // Unable to delete by ROWID, so build a WHERE
1688 // clause based on the keyfields.
1689 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1690 wxStrcat(pSqlStmt
, whereClause
);
1693 wxStrcat(pSqlStmt
, pWhereClause
);
1696 } // BuildUpdateStmt()
1699 /********** wxDbTable::BuildDeleteStmt() **********/
1700 void wxDbTable::BuildDeleteStmt(char *pSqlStmt
, int typeOfDel
, const char *pWhereClause
)
1706 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
];
1710 // Handle the case of DeleteWhere() and the where clause is blank. It should
1711 // delete all records from the database in this case.
1712 if (typeOfDel
== DB_DEL_WHERE
&& (pWhereClause
== 0 || wxStrlen(pWhereClause
) == 0))
1714 sprintf(pSqlStmt
, "DELETE FROM %s", tableName
);
1718 sprintf(pSqlStmt
, "DELETE FROM %s WHERE ", tableName
);
1720 // Append the WHERE clause to the SQL DELETE statement
1723 case DB_DEL_KEYFIELDS
:
1724 // If the datasource supports the ROWID column, build
1725 // the where on ROWID for efficiency purposes.
1726 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1727 if (CanUpdByROWID())
1730 char rowid
[wxDB_ROWID_LEN
];
1732 // Get the ROWID value. If not successful retreiving the ROWID,
1733 // simply fall down through the code and build the WHERE clause
1734 // based on the key fields.
1735 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
1737 wxStrcat(pSqlStmt
, "ROWID = '");
1738 wxStrcat(pSqlStmt
, rowid
);
1739 wxStrcat(pSqlStmt
, "'");
1743 // Unable to delete by ROWID, so build a WHERE
1744 // clause based on the keyfields.
1745 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
);
1746 wxStrcat(pSqlStmt
, whereClause
);
1749 wxStrcat(pSqlStmt
, pWhereClause
);
1751 case DB_DEL_MATCHING
:
1752 BuildWhereClause(whereClause
, DB_WHERE_MATCHING
);
1753 wxStrcat(pSqlStmt
, whereClause
);
1757 } // BuildDeleteStmt()
1760 /********** wxDbTable::BuildWhereClause() **********/
1761 void wxDbTable::BuildWhereClause(char *pWhereClause
, int typeOfWhere
,
1762 const char *qualTableName
, bool useLikeComparison
)
1764 * Note: BuildWhereClause() currently ignores timestamp columns.
1765 * They are not included as part of the where clause.
1768 bool moreThanOneColumn
= FALSE
;
1771 // Loop through the columns building a where clause as you go
1773 for (i
= 0; i
< noCols
; i
++)
1775 // Determine if this column should be included in the WHERE clause
1776 if ((typeOfWhere
== DB_WHERE_KEYFIELDS
&& colDefs
[i
].KeyField
) ||
1777 (typeOfWhere
== DB_WHERE_MATCHING
&& (!IsColNull(i
))))
1779 // Skip over timestamp columns
1780 if (colDefs
[i
].SqlCtype
== SQL_C_TIMESTAMP
)
1782 // If there is more than 1 column, join them with the keyword "AND"
1783 if (moreThanOneColumn
)
1784 wxStrcat(pWhereClause
, " AND ");
1786 moreThanOneColumn
= TRUE
;
1787 // Concatenate where phrase for the column
1788 if (qualTableName
&& wxStrlen(qualTableName
))
1790 wxStrcat(pWhereClause
, qualTableName
);
1791 wxStrcat(pWhereClause
, ".");
1793 wxStrcat(pWhereClause
, colDefs
[i
].ColName
);
1794 if (useLikeComparison
&& (colDefs
[i
].SqlCtype
== SQL_C_CHAR
))
1795 wxStrcat(pWhereClause
, " LIKE ");
1797 wxStrcat(pWhereClause
, " = ");
1798 switch(colDefs
[i
].SqlCtype
)
1801 sprintf(colValue
, "'%s'", (UCHAR FAR
*) colDefs
[i
].PtrDataObj
);
1804 sprintf(colValue
, "%hi", *((SWORD
*) colDefs
[i
].PtrDataObj
));
1807 sprintf(colValue
, "%hu", *((UWORD
*) colDefs
[i
].PtrDataObj
));
1810 sprintf(colValue
, "%li", *((SDWORD
*) colDefs
[i
].PtrDataObj
));
1813 sprintf(colValue
, "%lu", *((UDWORD
*) colDefs
[i
].PtrDataObj
));
1816 sprintf(colValue
, "%.6f", *((SFLOAT
*) colDefs
[i
].PtrDataObj
));
1819 sprintf(colValue
, "%.6f", *((SDOUBLE
*) colDefs
[i
].PtrDataObj
));
1822 wxStrcat(pWhereClause
, colValue
);
1825 } // wxDbTable::BuildWhereClause()
1828 /********** wxDbTable::IsColNull() **********/
1829 bool wxDbTable::IsColNull(int colNo
)
1832 This logic is just not right. It would indicate TRUE
1833 if a numeric field were set to a value of 0.
1835 switch(colDefs[colNo].SqlCtype)
1838 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1840 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1842 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1844 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1846 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1848 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1850 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1851 case SQL_C_TIMESTAMP:
1852 TIMESTAMP_STRUCT *pDt;
1853 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1854 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1862 return (colDefs
[colNo
].Null
);
1863 } // wxDbTable::IsColNull()
1866 /********** wxDbTable::CanSelectForUpdate() **********/
1867 bool wxDbTable::CanSelectForUpdate(void)
1872 if (pDb
->Dbms() == dbmsMY_SQL
)
1875 if ((pDb
->Dbms() == dbmsORACLE
) ||
1876 (pDb
->dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
))
1881 } // wxDbTable::CanSelectForUpdate()
1884 /********** wxDbTable::CanUpdByROWID() **********/
1885 bool wxDbTable::CanUpdByROWID(void)
1888 * NOTE: Returning FALSE for now until this can be debugged,
1889 * as the ROWID is not getting updated correctly
1893 if (pDb
->Dbms() == dbmsORACLE
)
1898 } // wxDbTable::CanUpdByROWID()
1901 /********** wxDbTable::IsCursorClosedOnCommit() **********/
1902 bool wxDbTable::IsCursorClosedOnCommit(void)
1904 if (pDb
->dbInf
.cursorCommitBehavior
== SQL_CB_PRESERVE
)
1909 } // wxDbTable::IsCursorClosedOnCommit()
1913 /********** wxDbTable::ClearMemberVar() **********/
1914 void wxDbTable::ClearMemberVar(int colNo
, bool setToNull
)
1916 assert(colNo
< noCols
);
1918 switch(colDefs
[colNo
].SqlCtype
)
1921 ((UCHAR FAR
*) colDefs
[colNo
].PtrDataObj
)[0] = 0;
1924 *((SWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1927 *((UWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1930 *((SDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1933 *((UDWORD
*) colDefs
[colNo
].PtrDataObj
) = 0;
1936 *((SFLOAT
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
1939 *((SDOUBLE
*) colDefs
[colNo
].PtrDataObj
) = 0.0f
;
1941 case SQL_C_TIMESTAMP
:
1942 TIMESTAMP_STRUCT
*pDt
;
1943 pDt
= (TIMESTAMP_STRUCT
*) colDefs
[colNo
].PtrDataObj
;
1956 } // wxDbTable::ClearMemberVar()
1959 /********** wxDbTable::ClearMemberVars() **********/
1960 void wxDbTable::ClearMemberVars(bool setToNull
)
1964 // Loop through the columns setting each member variable to zero
1965 for (i
=0; i
< noCols
; i
++)
1966 ClearMemberVar(i
,setToNull
);
1968 } // wxDbTable::ClearMemberVars()
1971 /********** wxDbTable::SetQueryTimeout() **********/
1972 bool wxDbTable::SetQueryTimeout(UDWORD nSeconds
)
1974 if (SQLSetStmtOption(hstmtInsert
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1975 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInsert
));
1976 if (SQLSetStmtOption(hstmtUpdate
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1977 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtUpdate
));
1978 if (SQLSetStmtOption(hstmtDelete
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1979 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtDelete
));
1980 if (SQLSetStmtOption(hstmtInternal
, SQL_QUERY_TIMEOUT
, nSeconds
) != SQL_SUCCESS
)
1981 return(pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
));
1983 // Completed Successfully
1986 } // wxDbTable::SetQueryTimeout()
1989 /********** wxDbTable::SetColDefs() **********/
1990 void wxDbTable::SetColDefs(int index
, const char *fieldName
, int dataType
, void *pData
,
1991 int cType
, int size
, bool keyField
, bool upd
,
1992 bool insAllow
, bool derivedCol
)
1994 if (!colDefs
) // May happen if the database connection fails
1997 if (wxStrlen(fieldName
) > (unsigned int) DB_MAX_COLUMN_NAME_LEN
)
1999 wxStrncpy (colDefs
[index
].ColName
, fieldName
, DB_MAX_COLUMN_NAME_LEN
);
2000 colDefs
[index
].ColName
[DB_MAX_COLUMN_NAME_LEN
] = 0;
2003 wxStrcpy(colDefs
[index
].ColName
, fieldName
);
2005 colDefs
[index
].DbDataType
= dataType
;
2006 colDefs
[index
].PtrDataObj
= pData
;
2007 colDefs
[index
].SqlCtype
= cType
;
2008 colDefs
[index
].SzDataObj
= size
;
2009 colDefs
[index
].KeyField
= keyField
;
2010 colDefs
[index
].DerivedCol
= derivedCol
;
2011 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2014 colDefs
[index
].Updateable
= FALSE
;
2015 colDefs
[index
].InsertAllowed
= FALSE
;
2019 colDefs
[index
].Updateable
= upd
;
2020 colDefs
[index
].InsertAllowed
= insAllow
;
2023 colDefs
[index
].Null
= FALSE
;
2025 } // wxDbTable::SetColDefs()
2028 /********** wxDbTable::SetColDefs() **********/
2029 wxDbColDataPtr
* wxDbTable::SetColDefs(wxDbColInf
*pColInfs
, ULONG numCols
)
2032 wxDbColDataPtr
*pColDataPtrs
= NULL
;
2038 pColDataPtrs
= new wxDbColDataPtr
[numCols
+1];
2040 for (index
= 0; index
< numCols
; index
++)
2042 // Process the fields
2043 switch (pColInfs
[index
].dbDataType
)
2045 case DB_DATA_TYPE_VARCHAR
:
2046 pColDataPtrs
[index
].PtrDataObj
= new char[pColInfs
[index
].bufferLength
+1];
2047 pColDataPtrs
[index
].SzDataObj
= pColInfs
[index
].columnSize
;
2048 pColDataPtrs
[index
].SqlCtype
= SQL_C_CHAR
;
2050 case DB_DATA_TYPE_INTEGER
:
2051 // Can be long or short
2052 if (pColInfs
[index
].bufferLength
== sizeof(long))
2054 pColDataPtrs
[index
].PtrDataObj
= new long;
2055 pColDataPtrs
[index
].SzDataObj
= sizeof(long);
2056 pColDataPtrs
[index
].SqlCtype
= SQL_C_SLONG
;
2060 pColDataPtrs
[index
].PtrDataObj
= new short;
2061 pColDataPtrs
[index
].SzDataObj
= sizeof(short);
2062 pColDataPtrs
[index
].SqlCtype
= SQL_C_SSHORT
;
2065 case DB_DATA_TYPE_FLOAT
:
2066 // Can be float or double
2067 if (pColInfs
[index
].bufferLength
== sizeof(float))
2069 pColDataPtrs
[index
].PtrDataObj
= new float;
2070 pColDataPtrs
[index
].SzDataObj
= sizeof(float);
2071 pColDataPtrs
[index
].SqlCtype
= SQL_C_FLOAT
;
2075 pColDataPtrs
[index
].PtrDataObj
= new double;
2076 pColDataPtrs
[index
].SzDataObj
= sizeof(double);
2077 pColDataPtrs
[index
].SqlCtype
= SQL_C_DOUBLE
;
2080 case DB_DATA_TYPE_DATE
:
2081 pColDataPtrs
[index
].PtrDataObj
= new TIMESTAMP_STRUCT
;
2082 pColDataPtrs
[index
].SzDataObj
= sizeof(TIMESTAMP_STRUCT
);
2083 pColDataPtrs
[index
].SqlCtype
= SQL_C_TIMESTAMP
;
2086 SetColDefs (index
,pColInfs
[index
].colName
,pColInfs
[index
].dbDataType
, pColDataPtrs
[index
].PtrDataObj
, pColDataPtrs
[index
].SqlCtype
, pColDataPtrs
[index
].SzDataObj
);
2090 return (pColDataPtrs
);
2092 } // wxDbTable::SetColDefs()
2095 /********** wxDbTable::SetCursor() **********/
2096 void wxDbTable::SetCursor(HSTMT
*hstmtActivate
)
2098 if (hstmtActivate
== wxDB_DEFAULT_CURSOR
)
2099 hstmt
= *hstmtDefault
;
2101 hstmt
= *hstmtActivate
;
2103 } // wxDbTable::SetCursor()
2106 /********** wxDbTable::Count(const char *) **********/
2107 ULONG
wxDbTable::Count(const char *args
)
2113 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
2114 sqlStmt
= "SELECT COUNT(";
2116 sqlStmt
+= ") FROM ";
2117 sqlStmt
+= queryTableName
;
2118 #if wxODBC_BACKWARD_COMPATABILITY
2119 if (from
&& wxStrlen(from
))
2125 // Add the where clause if one is provided
2126 #if wxODBC_BACKWARD_COMPATABILITY
2127 if (where
&& wxStrlen(where
))
2132 sqlStmt
+= " WHERE ";
2136 pDb
->WriteSqlLog(sqlStmt
.c_str());
2138 // Initialize the Count cursor if it's not already initialized
2141 hstmtCount
= GetNewCursor(FALSE
,FALSE
);
2147 // Execute the SQL statement
2148 if (SQLExecDirect(*hstmtCount
, (UCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2150 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2155 if (SQLFetch(*hstmtCount
) != SQL_SUCCESS
)
2157 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2161 // Obtain the result
2162 if (SQLGetData(*hstmtCount
, 1, SQL_C_ULONG
, &count
, sizeof(count
), &cb
) != SQL_SUCCESS
)
2164 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2169 if (SQLFreeStmt(*hstmtCount
, SQL_CLOSE
) != SQL_SUCCESS
)
2170 pDb
->DispAllErrors(henv
, hdbc
, *hstmtCount
);
2172 // Return the record count
2175 } // wxDbTable::Count()
2178 /********** wxDbTable::Refresh() **********/
2179 bool wxDbTable::Refresh(void)
2183 // Switch to the internal cursor so any active cursors are not corrupted
2184 HSTMT currCursor
= GetCursor();
2185 hstmt
= hstmtInternal
;
2186 #if wxODBC_BACKWARD_COMPATABILITY
2187 // Save the where and order by clauses
2188 char *saveWhere
= where
;
2189 char *saveOrderBy
= orderBy
;
2191 wxString saveWhere
= where
;
2192 wxString saveOrderBy
= orderBy
;
2194 // Build a where clause to refetch the record with. Try and use the
2195 // ROWID if it's available, ow use the key fields.
2196 char whereClause
[DB_MAX_WHERE_CLAUSE_LEN
+1];
2197 wxStrcpy(whereClause
, "");
2198 if (CanUpdByROWID())
2201 char rowid
[wxDB_ROWID_LEN
+1];
2203 // Get the ROWID value. If not successful retreiving the ROWID,
2204 // simply fall down through the code and build the WHERE clause
2205 // based on the key fields.
2206 if (SQLGetData(hstmt
, noCols
+1, SQL_C_CHAR
, (UCHAR
*) rowid
, wxDB_ROWID_LEN
, &cb
) == SQL_SUCCESS
)
2208 wxStrcat(whereClause
, queryTableName
);
2209 wxStrcat(whereClause
, ".ROWID = '");
2210 wxStrcat(whereClause
, rowid
);
2211 wxStrcat(whereClause
, "'");
2215 // If unable to use the ROWID, build a where clause from the keyfields
2216 if (wxStrlen(whereClause
) == 0)
2217 BuildWhereClause(whereClause
, DB_WHERE_KEYFIELDS
, queryTableName
);
2219 // Requery the record
2220 where
= whereClause
;
2225 if (result
&& !GetNext())
2228 // Switch back to original cursor
2229 SetCursor(&currCursor
);
2231 // Free the internal cursor
2232 if (SQLFreeStmt(hstmtInternal
, SQL_CLOSE
) != SQL_SUCCESS
)
2233 pDb
->DispAllErrors(henv
, hdbc
, hstmtInternal
);
2235 // Restore the original where and order by clauses
2237 orderBy
= saveOrderBy
;
2241 } // wxDbTable::Refresh()
2244 /********** wxDbTable::SetColNull(int colNo, bool set) **********/
2245 bool wxDbTable::SetColNull(int colNo
, bool set
)
2249 colDefs
[colNo
].Null
= set
;
2250 if (set
) // Blank out the values in the member variable
2251 ClearMemberVar(colNo
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2257 } // wxDbTable::SetColNull(int colNo)
2260 /********** wxDbTable::SetColNull(char *colName, bool set) **********/
2261 bool wxDbTable::SetColNull(const char *colName
, bool set
)
2264 for (i
= 0; i
< noCols
; i
++)
2266 if (!wxStricmp(colName
, colDefs
[i
].ColName
))
2272 colDefs
[i
].Null
= set
;
2273 if (set
) // Blank out the values in the member variable
2274 ClearMemberVar(i
,FALSE
); // Must call with FALSE, or infinite recursion will happen
2280 } // wxDbTable::SetColNull(char *colName)
2283 /********** wxDbTable::GetNewCursor() **********/
2284 HSTMT
*wxDbTable::GetNewCursor(bool setCursor
, bool bindColumns
)
2286 HSTMT
*newHSTMT
= new HSTMT
;
2291 if (SQLAllocStmt(hdbc
, newHSTMT
) != SQL_SUCCESS
)
2293 pDb
->DispAllErrors(henv
, hdbc
);
2298 if (SQLSetStmtOption(*newHSTMT
, SQL_CURSOR_TYPE
, cursorType
) != SQL_SUCCESS
)
2300 pDb
->DispAllErrors(henv
, hdbc
, *newHSTMT
);
2307 if(!bindCols(*newHSTMT
))
2315 SetCursor(newHSTMT
);
2319 } // wxDbTable::GetNewCursor()
2322 /********** wxDbTable::DeleteCursor() **********/
2323 bool wxDbTable::DeleteCursor(HSTMT
*hstmtDel
)
2327 if (!hstmtDel
) // Cursor already deleted
2330 if (SQLFreeStmt(*hstmtDel
, SQL_DROP
) != SQL_SUCCESS
)
2332 pDb
->DispAllErrors(henv
, hdbc
);
2340 } // wxDbTable::DeleteCursor()
2342 #endif // wxUSE_ODBC