]> git.saurik.com Git - wxWidgets.git/blob - src/common/dbtable.cpp
assert in GetNextItem() fixed (?)
[wxWidgets.git] / src / common / dbtable.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dbtable.cpp
3 // Purpose: Implementation of the wxTable class.
4 // Author: Doug Card
5 // Modified by: George Tasker
6 // Mods: April 1999
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
11 // Created: 9.96
12 // RCS-ID: $Id$
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 ///////////////////////////////////////////////////////////////////////////////
25
26 /*
27 // SYNOPSIS START
28 // SYNOPSIS STOP
29 */
30
31 // Use this line for wxWindows v1.x
32 //#include "wx_ver.h"
33 // Use this line for wxWindows v2.x
34 #include "wx/version.h"
35 #include "wx/wxprec.h"
36
37 #if wxMAJOR_VERSION == 2
38 # ifdef __GNUG__
39 # pragma implementation "dbtable.h"
40 # endif
41 #endif
42
43 #ifdef DBDEBUG_CONSOLE
44 #include <iostream.h>
45 #endif
46
47 #ifdef __BORLANDC__
48 #pragma hdrstop
49 #endif //__BORLANDC__
50
51 #if wxMAJOR_VERSION == 2
52 #ifndef WX_PRECOMP
53 #include "wx/string.h"
54 #include "wx/object.h"
55 #include "wx/list.h"
56 #include "wx/utils.h"
57 #include "wx/msgdlg.h"
58 #endif
59 #include "wx/filefn.h"
60 #endif
61
62 #if wxMAJOR_VERSION == 1
63 # if defined(wx_msw) || defined(wx_x)
64 # ifdef WX_PRECOMP
65 # include "wx_prec.h"
66 # else
67 # include "wx.h"
68 # endif
69 # endif
70 # define wxUSE_ODBC 1
71 #endif
72
73 #if wxUSE_ODBC
74
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <assert.h>
79
80 #if wxMAJOR_VERSION == 1
81 #include "table.h"
82 #elif wxMAJOR_VERSION == 2
83 #include "wx/dbtable.h"
84 #endif
85
86 #ifdef __UNIX__
87 // The HPUX preprocessor lines below were commented out on 8/20/97
88 // because macros.h currently redefines DEBUG and is unneeded.
89 // # ifdef HPUX
90 // # include <macros.h>
91 // # endif
92 # ifdef LINUX
93 # include <sys/minmax.h>
94 # endif
95 #endif
96
97 ULONG lastTableID = 0;
98
99
100 #ifdef __WXDEBUG__
101 wxList TablesInUse;
102 #endif
103
104
105 /********** wxTable::wxTable() **********/
106 wxTable::wxTable(wxDB *pwxDB, const char *tblName, const int nCols,
107 const char *qryTblName, bool qryOnly, const char *tblPath)
108 {
109 pDb = pwxDB; // Pointer to the wxDB object
110 henv = 0;
111 hdbc = 0;
112 hstmt = 0;
113 hstmtDefault = 0; // Initialized below
114 hstmtCount = 0; // Initialized first time it is needed
115 hstmtInsert = 0;
116 hstmtDelete = 0;
117 hstmtUpdate = 0;
118 hstmtInternal = 0;
119 colDefs = 0;
120 tableID = 0;
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
126 queryOnly = qryOnly;
127
128 assert (tblName);
129
130 wxStrcpy(tableName, tblName); // Table Name
131 if (tblPath)
132 wxStrcpy(tablePath, tblPath); // Table Path - used for dBase files
133
134 if (qryTblName) // Name of the table/view to query
135 wxStrcpy(queryTableName, qryTblName);
136 else
137 wxStrcpy(queryTableName, tblName);
138
139 if (!pDb)
140 return;
141
142 pDb->nTables++;
143
144 char s[200];
145 tableID = ++lastTableID;
146 sprintf(s, "wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName,tableID,pDb);
147
148 #ifdef __WXDEBUG__
149 CstructTablesInUse *tableInUse;
150 tableInUse = new CstructTablesInUse();
151 tableInUse->tableName = tblName;
152 tableInUse->tableID = tableID;
153 tableInUse->pDb = pDb;
154 TablesInUse.Append(tableInUse);
155 #endif
156
157 pDb->WriteSqlLog(s);
158
159 // Grab the HENV and HDBC from the wxDB object
160 henv = pDb->henv;
161 hdbc = pDb->hdbc;
162
163 // Allocate space for column definitions
164 if (noCols)
165 colDefs = new wxColDef[noCols]; // Points to the first column defintion
166
167 // Allocate statement handles for the table
168 if (!queryOnly)
169 {
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);
179 }
180 // Allocate a separate statement handle for internal use
181 if (SQLAllocStmt(hdbc, &hstmtInternal) != SQL_SUCCESS)
182 pDb->DispAllErrors(henv, hdbc);
183
184 // Set the cursor type for the statement handles
185 cursorType = SQL_CURSOR_STATIC;
186 if (SQLSetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
187 {
188 // Check to see if cursor type is supported
189 pDb->GetNextError(henv, hdbc, hstmtInternal);
190 if (! wxStrcmp(pDb->sqlState, "01S02")) // Option Value Changed
191 {
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: ";
199 switch(cursorType)
200 {
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;
209 }
210 cout << endl << endl;
211 #endif
212 }
213 else
214 {
215 pDb->DispNextError();
216 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
217 }
218 }
219 #ifdef DBDEBUG_CONSOLE
220 else
221 cout << "Cursor Type set to STATIC" << endl << endl;
222 #endif
223
224 if (!queryOnly)
225 {
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);
235 }
236
237 // Make the default cursor the active cursor
238 hstmtDefault = NewCursor(FALSE,FALSE);
239 assert(hstmtDefault);
240 hstmt = *hstmtDefault;
241
242 } // wxTable::wxTable()
243
244
245 /********** wxTable::~wxTable() **********/
246 wxTable::~wxTable()
247 {
248 char s[80];
249 if (pDb)
250 {
251 sprintf(s, "wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName,tableID,pDb);
252 pDb->WriteSqlLog(s);
253 }
254
255 #ifdef __WXDEBUG__
256 if (tableID)
257 {
258 bool found = FALSE;
259 wxNode *pNode;
260 pNode = TablesInUse.First();
261 while (pNode && !found)
262 {
263 if (((CstructTablesInUse *)pNode->Data())->tableID == tableID)
264 {
265 found = TRUE;
266 if (!TablesInUse.DeleteNode(pNode))
267 wxMessageBox (s,"Unable to delete node!");
268 }
269 else
270 pNode = pNode->Next();
271 }
272 if (!found)
273 {
274 char msg[250];
275 sprintf(msg,"Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s);
276 wxMessageBox (msg,"NOTICE...");
277 }
278 }
279 #endif
280
281 // Decrement the wxDB table count
282 if (pDb)
283 pDb->nTables--;
284
285 // Delete memory allocated for column definitions
286 if (colDefs)
287 delete [] colDefs;
288
289 // Free statement handles
290 if (!queryOnly)
291 {
292 if (hstmtInsert)
293 if (SQLFreeStmt(hstmtInsert, SQL_DROP) != SQL_SUCCESS)
294 pDb->DispAllErrors(henv, hdbc);
295 if (hstmtDelete)
296 if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
297 pDb->DispAllErrors(henv, hdbc);
298 if (hstmtUpdate)
299 if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
300 pDb->DispAllErrors(henv, hdbc);
301 }
302 if (hstmtInternal)
303 if (SQLFreeStmt(hstmtInternal, SQL_DROP) != SQL_SUCCESS)
304 pDb->DispAllErrors(henv, hdbc);
305
306 // Delete dynamically allocated cursors
307 if (hstmtDefault)
308 DeleteCursor(hstmtDefault);
309 if (hstmtCount)
310 DeleteCursor(hstmtCount);
311
312 } // wxTable::~wxTable()
313
314
315
316 /***************************** PRIVATE FUNCTIONS *****************************/
317
318
319
320 /********** wxTable::bindInsertParams() **********/
321 bool wxTable::bindInsertParams(void)
322 {
323 assert(!queryOnly);
324 if (queryOnly)
325 return(FALSE);
326
327 SWORD fSqlType = 0;
328 UDWORD precision = 0;
329 SWORD scale = 0;
330
331 // Bind each column (that can be inserted) of the table to a parameter marker
332 int i,colNo;
333 for (i = 0, colNo = 1; i < noCols; i++)
334 {
335 if (! colDefs[i].InsertAllowed)
336 continue;
337 switch(colDefs[i].DbDataType)
338 {
339 case DB_DATA_TYPE_VARCHAR:
340 fSqlType = pDb->typeInfVarchar.FsqlType;
341 precision = colDefs[i].SzDataObj;
342 scale = 0;
343 colDefs[i].CbValue = SQL_NTS;
344 break;
345 case DB_DATA_TYPE_INTEGER:
346 fSqlType = pDb->typeInfInteger.FsqlType;
347 precision = pDb->typeInfInteger.Precision;
348 scale = 0;
349 colDefs[i].CbValue = 0;
350 break;
351 case DB_DATA_TYPE_FLOAT:
352 fSqlType = pDb->typeInfFloat.FsqlType;
353 precision = pDb->typeInfFloat.Precision;
354 scale = pDb->typeInfFloat.MaximumScale;
355 // SQL Sybase Anywhere v5.5 returned a negative number for the
356 // MaxScale. This caused ODBC to kick out an error on ibscale.
357 // I check for this here and set the scale = precision.
358 //if (scale < 0)
359 // scale = (short) precision;
360 colDefs[i].CbValue = 0;
361 break;
362 case DB_DATA_TYPE_DATE:
363 fSqlType = pDb->typeInfDate.FsqlType;
364 precision = pDb->typeInfDate.Precision;
365 scale = 0;
366 colDefs[i].CbValue = 0;
367 break;
368 }
369 // Null values
370 if (colDefs[i].Null)
371 {
372 colDefs[i].CbValue = SQL_NULL_DATA;
373 colDefs[i].Null = FALSE;
374 }
375 if (SQLBindParameter(hstmtInsert, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
376 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
377 precision+1,&colDefs[i].CbValue) != SQL_SUCCESS)
378 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
379 }
380
381 // Completed successfully
382 return(TRUE);
383
384 } // wxTable::bindInsertParams()
385
386
387 /********** wxTable::bindUpdateParams() **********/
388 bool wxTable::bindUpdateParams(void)
389 {
390 assert(!queryOnly);
391 if (queryOnly)
392 return(FALSE);
393
394 SWORD fSqlType = 0;
395 UDWORD precision = 0;
396 SWORD scale = 0;
397
398 // Bind each UPDATEABLE column of the table to a parameter marker
399 int i,colNo;
400 for (i = 0, colNo = 1; i < noCols; i++)
401 {
402 if (! colDefs[i].Updateable)
403 continue;
404 switch(colDefs[i].DbDataType)
405 {
406 case DB_DATA_TYPE_VARCHAR:
407 fSqlType = pDb->typeInfVarchar.FsqlType;
408 precision = colDefs[i].SzDataObj;
409 scale = 0;
410 colDefs[i].CbValue = SQL_NTS;
411 break;
412 case DB_DATA_TYPE_INTEGER:
413 fSqlType = pDb->typeInfInteger.FsqlType;
414 precision = pDb->typeInfInteger.Precision;
415 scale = 0;
416 colDefs[i].CbValue = 0;
417 break;
418 case DB_DATA_TYPE_FLOAT:
419 fSqlType = pDb->typeInfFloat.FsqlType;
420 precision = pDb->typeInfFloat.Precision;
421 scale = pDb->typeInfFloat.MaximumScale;
422 // SQL Sybase Anywhere v5.5 returned a negative number for the
423 // MaxScale. This caused ODBC to kick out an error on ibscale.
424 // I check for this here and set the scale = precision.
425 //if (scale < 0)
426 // scale = (short) precision;
427 colDefs[i].CbValue = 0;
428 break;
429 case DB_DATA_TYPE_DATE:
430 fSqlType = pDb->typeInfDate.FsqlType;
431 precision = pDb->typeInfDate.Precision;
432 scale = 0;
433 colDefs[i].CbValue = 0;
434 break;
435 }
436 if (SQLBindParameter(hstmtUpdate, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
437 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
438 precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
439 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
440 }
441
442 // Completed successfully
443 return(TRUE);
444
445 } // wxTable::bindUpdateParams()
446
447
448 /********** wxTable::bindCols() **********/
449 bool wxTable::bindCols(HSTMT cursor)
450 {
451 static SDWORD cb;
452
453 // Bind each column of the table to a memory address for fetching data
454 int i;
455 for (i = 0; i < noCols; i++)
456 {
457 if (SQLBindCol(cursor, i+1, colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
458 colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
459 return(pDb->DispAllErrors(henv, hdbc, cursor));
460 }
461
462 // Completed successfully
463 return(TRUE);
464
465 } // wxTable::bindCols()
466
467
468 /********** wxTable::getRec() **********/
469 bool wxTable::getRec(UWORD fetchType)
470 {
471 RETCODE retcode;
472
473 if (!pDb->FwdOnlyCursors())
474 {
475 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
476 UDWORD cRowsFetched;
477 UWORD rowStatus;
478
479 retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
480 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
481 if (retcode == SQL_NO_DATA_FOUND)
482 return(FALSE);
483 else
484 return(pDb->DispAllErrors(henv, hdbc, hstmt));
485 }
486 else
487 {
488 // Fetch the next record from the record set
489 retcode = SQLFetch(hstmt);
490 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
491 {
492 if (retcode == SQL_NO_DATA_FOUND)
493 return(FALSE);
494 else
495 return(pDb->DispAllErrors(henv, hdbc, hstmt));
496 }
497 }
498
499 // Completed successfully
500 return(TRUE);
501
502 } // wxTable::getRec()
503
504
505 /********** wxTable::execDelete() **********/
506 bool wxTable::execDelete(const char *pSqlStmt)
507 {
508 // Execute the DELETE statement
509 if (SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
510 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
511
512 // Record deleted successfully
513 return(TRUE);
514
515 } // wxTable::execDelete()
516
517
518 /********** wxTable::execUpdate() **********/
519 bool wxTable::execUpdate(const char *pSqlStmt)
520 {
521 // Execute the UPDATE statement
522 if (SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
523 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
524
525 // Record deleted successfully
526 return(TRUE);
527
528 } // wxTable::execUpdate()
529
530
531 /********** wxTable::query() **********/
532 bool wxTable::query(int queryType, bool forUpdate, bool distinct, char *pSqlStmt)
533 {
534 char sqlStmt[DB_MAX_STATEMENT_LEN];
535
536 // Set the selectForUpdate member variable
537 if (forUpdate)
538 // The user may wish to select for update, but the DBMS may not be capable
539 selectForUpdate = CanSelectForUpdate();
540 else
541 selectForUpdate = FALSE;
542
543 // Set the SQL SELECT string
544 if (queryType != DB_SELECT_STATEMENT) // A select statement was not passed in,
545 { // so generate a select statement.
546 GetSelectStmt(sqlStmt, queryType, distinct);
547 pDb->WriteSqlLog(sqlStmt);
548 }
549
550 // Make sure the cursor is closed first
551 if (! CloseCursor(hstmt))
552 return(FALSE);
553
554 // Execute the SQL SELECT statement
555 int retcode;
556
557 retcode = SQLExecDirect(hstmt, (UCHAR FAR *) (queryType == DB_SELECT_STATEMENT ? pSqlStmt : sqlStmt), SQL_NTS);
558 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
559 return(pDb->DispAllErrors(henv, hdbc, hstmt));
560
561 // Completed successfully
562 return(TRUE);
563
564 } // wxTable::query()
565
566
567 /***************************** PUBLIC FUNCTIONS *****************************/
568
569
570 /********** wxTable::Open() **********/
571 bool wxTable::Open(void)
572 {
573 if (!pDb)
574 return FALSE;
575
576 int i;
577 char sqlStmt[DB_MAX_STATEMENT_LEN];
578
579 // Verify that the table exists in the database
580 if (!pDb->TableExists(tableName,pDb->GetUsername(),tablePath))
581 {
582 char s[250];
583 if (wxStrcmp(tablePath,""))
584 sprintf(s, "Error opening '%s/%s'.\n",tablePath,tableName);
585 else
586 sprintf(s, "Error opening '%s'.\n", tableName);
587 if (!pDb->TableExists(tableName,NULL,tablePath))
588 wxStrcat(s,"Table/view does not exist in the database.\n");
589 else
590 wxStrcat(s,"Current logged in user does not have sufficient privileges to access this table.\n");
591 pDb->LogError(s);
592 return(FALSE);
593 }
594
595 // Bind the member variables for field exchange between
596 // the wxTable object and the ODBC record.
597 if (!queryOnly)
598 {
599 if (!bindInsertParams()) // Inserts
600 return(FALSE);
601 if (!bindUpdateParams()) // Updates
602 return(FALSE);
603 }
604 if (!bindCols(*hstmtDefault)) // Selects
605 return(FALSE);
606 if (!bindCols(hstmtInternal)) // Internal use only
607 return(FALSE);
608 /*
609 * Do NOT bind the hstmtCount cursor!!!
610 */
611
612 // Build an insert statement using parameter markers
613 if (!queryOnly && noCols > 0)
614 {
615 bool needComma = FALSE;
616 sprintf(sqlStmt, "INSERT INTO %s (", tableName);
617 for (i = 0; i < noCols; i++)
618 {
619 if (! colDefs[i].InsertAllowed)
620 continue;
621 if (needComma)
622 wxStrcat(sqlStmt, ",");
623 wxStrcat(sqlStmt, colDefs[i].ColName);
624 needComma = TRUE;
625 }
626 needComma = FALSE;
627 wxStrcat(sqlStmt, ") VALUES (");
628 for (i = 0; i < noCols; i++)
629 {
630 if (! colDefs[i].InsertAllowed)
631 continue;
632 if (needComma)
633 wxStrcat(sqlStmt, ",");
634 wxStrcat(sqlStmt, "?");
635 needComma = TRUE;
636 }
637 wxStrcat(sqlStmt, ")");
638
639 // pDb->WriteSqlLog(sqlStmt);
640
641 // Prepare the insert statement for execution
642 if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
643 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
644 }
645
646 // Completed successfully
647 return(TRUE);
648
649 } // wxTable::Open()
650
651
652 /********** wxTable::Query() **********/
653 bool wxTable::Query(bool forUpdate, bool distinct)
654 {
655
656 return(query(DB_SELECT_WHERE, forUpdate, distinct));
657
658 } // wxTable::Query()
659
660
661 /********** wxTable::QueryBySqlStmt() **********/
662 bool wxTable::QueryBySqlStmt(char *pSqlStmt)
663 {
664 pDb->WriteSqlLog(pSqlStmt);
665
666 return(query(DB_SELECT_STATEMENT, FALSE, FALSE, pSqlStmt));
667
668 } // wxTable::QueryBySqlStmt()
669
670
671 /********** wxTable::QueryMatching() **********/
672 bool wxTable::QueryMatching(bool forUpdate, bool distinct)
673 {
674
675 return(query(DB_SELECT_MATCHING, forUpdate, distinct));
676
677 } // wxTable::QueryMatching()
678
679
680 /********** wxTable::QueryOnKeyFields() **********/
681 bool wxTable::QueryOnKeyFields(bool forUpdate, bool distinct)
682 {
683
684 return(query(DB_SELECT_KEYFIELDS, forUpdate, distinct));
685
686 } // wxTable::QueryOnKeyFields()
687
688
689 /********** wxTable::GetPrev() **********/
690 bool wxTable::GetPrev(void)
691 {
692 if (pDb->FwdOnlyCursors())
693 {
694 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxTable"));
695 return FALSE;
696 }
697 else
698 return(getRec(SQL_FETCH_PRIOR));
699 } // wxTable::GetPrev()
700
701
702 /********** wxTable::operator-- **********/
703 bool wxTable::operator--(int)
704 {
705 if (pDb->FwdOnlyCursors())
706 {
707 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxTable"));
708 return FALSE;
709 }
710 else
711 return(getRec(SQL_FETCH_PRIOR));
712 } // wxTable::operator--
713
714
715 /********** wxTable::GetFirst() **********/
716 bool wxTable::GetFirst(void)
717 {
718 if (pDb->FwdOnlyCursors())
719 {
720 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxTable"));
721 return FALSE;
722 }
723 else
724 return(getRec(SQL_FETCH_FIRST));
725 } // wxTable::GetFirst()
726
727
728 /********** wxTable::GetLast() **********/
729 bool wxTable::GetLast(void)
730 {
731 if (pDb->FwdOnlyCursors())
732 {
733 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxTable"));
734 return FALSE;
735 }
736 else
737 return(getRec(SQL_FETCH_LAST));
738 } // wxTable::GetLast()
739
740
741 /********** wxTable::GetSelectStmt() **********/
742 void wxTable::GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
743 {
744 char whereClause[DB_MAX_WHERE_CLAUSE_LEN];
745
746 whereClause[0] = 0;
747
748 // Build a select statement to query the database
749 wxStrcpy(pSqlStmt, "SELECT ");
750
751 // SELECT DISTINCT values only?
752 if (distinct)
753 wxStrcat(pSqlStmt, "DISTINCT ");
754
755 // Was a FROM clause specified to join tables to the base table?
756 // Available for ::Query() only!!!
757 bool appendFromClause = FALSE;
758 if (typeOfSelect == DB_SELECT_WHERE && from && wxStrlen(from))
759 appendFromClause = TRUE;
760
761 // Add the column list
762 int i;
763 for (i = 0; i < noCols; i++)
764 {
765 // If joining tables, the base table column names must be qualified to avoid ambiguity
766 if (appendFromClause)
767 {
768 wxStrcat(pSqlStmt, queryTableName);
769 wxStrcat(pSqlStmt, ".");
770 }
771 wxStrcat(pSqlStmt, colDefs[i].ColName);
772 if (i + 1 < noCols)
773 wxStrcat(pSqlStmt, ",");
774 }
775
776 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
777 // the ROWID if querying distinct records. The rowid will always be unique.
778 if (!distinct && CanUpdByROWID())
779 {
780 // If joining tables, the base table column names must be qualified to avoid ambiguity
781 if (appendFromClause)
782 {
783 wxStrcat(pSqlStmt, ",");
784 wxStrcat(pSqlStmt, queryTableName);
785 wxStrcat(pSqlStmt, ".ROWID");
786 }
787 else
788 wxStrcat(pSqlStmt, ",ROWID");
789 }
790
791 // Append the FROM tablename portion
792 wxStrcat(pSqlStmt, " FROM ");
793 wxStrcat(pSqlStmt, queryTableName);
794
795 // Sybase uses the HOLDLOCK keyword to lock a record during query.
796 // The HOLDLOCK keyword follows the table name in the from clause.
797 // Each table in the from clause must specify HOLDLOCK or
798 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
799 // is parsed but ignored in SYBASE Transact-SQL.
800 if (selectForUpdate && (pDb->Dbms() == dbmsSYBASE_ASA || pDb->Dbms() == dbmsSYBASE_ASE))
801 wxStrcat(pSqlStmt, " HOLDLOCK");
802
803 if (appendFromClause)
804 wxStrcat(pSqlStmt, from);
805
806 // Append the WHERE clause. Either append the where clause for the class
807 // or build a where clause. The typeOfSelect determines this.
808 switch(typeOfSelect)
809 {
810 case DB_SELECT_WHERE:
811 if (where && wxStrlen(where)) // May not want a where clause!!!
812 {
813 wxStrcat(pSqlStmt, " WHERE ");
814 wxStrcat(pSqlStmt, where);
815 }
816 break;
817 case DB_SELECT_KEYFIELDS:
818 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS);
819 if (wxStrlen(whereClause))
820 {
821 wxStrcat(pSqlStmt, " WHERE ");
822 wxStrcat(pSqlStmt, whereClause);
823 }
824 break;
825 case DB_SELECT_MATCHING:
826 GetWhereClause(whereClause, DB_WHERE_MATCHING);
827 if (wxStrlen(whereClause))
828 {
829 wxStrcat(pSqlStmt, " WHERE ");
830 wxStrcat(pSqlStmt, whereClause);
831 }
832 break;
833 }
834
835 // Append the ORDER BY clause
836 if (orderBy && wxStrlen(orderBy))
837 {
838 wxStrcat(pSqlStmt, " ORDER BY ");
839 wxStrcat(pSqlStmt, orderBy);
840 }
841
842 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
843 // parses the FOR UPDATE clause but ignores it. See the comment above on the
844 // HOLDLOCK for Sybase.
845 if (selectForUpdate && CanSelectForUpdate())
846 wxStrcat(pSqlStmt, " FOR UPDATE");
847
848 } // wxTable::GetSelectStmt()
849
850
851 /********** wxTable::GetRowNum() **********/
852 UWORD wxTable::GetRowNum(void)
853 {
854 UDWORD rowNum;
855
856 if (SQLGetStmtOption(hstmt, SQL_ROW_NUMBER, (UCHAR*) &rowNum) != SQL_SUCCESS)
857 {
858 pDb->DispAllErrors(henv, hdbc, hstmt);
859 return(0);
860 }
861
862 // Completed successfully
863 return((UWORD) rowNum);
864
865 } // wxTable::GetRowNum()
866
867
868 /********** wxTable::CloseCursor() **********/
869 bool wxTable::CloseCursor(HSTMT cursor)
870 {
871 if (SQLFreeStmt(cursor, SQL_CLOSE) != SQL_SUCCESS)
872 return(pDb->DispAllErrors(henv, hdbc, cursor));
873
874 // Completed successfully
875 return(TRUE);
876
877 } // wxTable::CloseCursor()
878
879
880 /********** wxTable::CreateTable() **********/
881 bool wxTable::CreateTable(bool attemptDrop)
882 {
883 if (!pDb)
884 return FALSE;
885
886 int i, j;
887 char sqlStmt[DB_MAX_STATEMENT_LEN];
888
889 #ifdef DBDEBUG_CONSOLE
890 cout << "Creating Table " << tableName << "..." << endl;
891 #endif
892
893 // Drop table first
894 if (attemptDrop && !DropTable())
895 return FALSE;
896
897 // Create the table
898 #ifdef DBDEBUG_CONSOLE
899 for (i = 0; i < noCols; i++)
900 {
901 // Exclude derived columns since they are NOT part of the base table
902 if (colDefs[i].DerivedCol)
903 continue;
904 cout << i + 1 << ": " << colDefs[i].ColName << "; ";
905 switch(colDefs[i].DbDataType)
906 {
907 case DB_DATA_TYPE_VARCHAR:
908 cout << pDb->typeInfVarchar.TypeName << "(" << colDefs[i].SzDataObj << ")";
909 break;
910 case DB_DATA_TYPE_INTEGER:
911 cout << pDb->typeInfInteger.TypeName;
912 break;
913 case DB_DATA_TYPE_FLOAT:
914 cout << pDb->typeInfFloat.TypeName;
915 break;
916 case DB_DATA_TYPE_DATE:
917 cout << pDb->typeInfDate.TypeName;
918 break;
919 }
920 cout << endl;
921 }
922 #endif
923
924 // Build a CREATE TABLE string from the colDefs structure.
925 bool needComma = FALSE;
926 sprintf(sqlStmt, "CREATE TABLE %s (", tableName);
927 for (i = 0; i < noCols; i++)
928 {
929 // Exclude derived columns since they are NOT part of the base table
930 if (colDefs[i].DerivedCol)
931 continue;
932 // Comma Delimiter
933 if (needComma)
934 wxStrcat(sqlStmt, ",");
935 // Column Name
936 wxStrcat(sqlStmt, colDefs[i].ColName);
937 wxStrcat(sqlStmt, " ");
938 // Column Type
939 switch(colDefs[i].DbDataType)
940 {
941 case DB_DATA_TYPE_VARCHAR:
942 wxStrcat(sqlStmt, pDb->typeInfVarchar.TypeName); break;
943 case DB_DATA_TYPE_INTEGER:
944 wxStrcat(sqlStmt, pDb->typeInfInteger.TypeName); break;
945 case DB_DATA_TYPE_FLOAT:
946 wxStrcat(sqlStmt, pDb->typeInfFloat.TypeName); break;
947 case DB_DATA_TYPE_DATE:
948 wxStrcat(sqlStmt, pDb->typeInfDate.TypeName); break;
949 }
950 // For varchars, append the size of the string
951 if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)
952 {
953 char s[10];
954 // wxStrcat(sqlStmt, "(");
955 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
956 // wxStrcat(sqlStmt, ")");
957 sprintf(s, "(%d)", colDefs[i].SzDataObj);
958 wxStrcat(sqlStmt, s);
959 }
960
961 if (pDb->Dbms() == dbmsSYBASE_ASE || pDb->Dbms() == dbmsMY_SQL)
962 {
963 if (colDefs[i].KeyField)
964 {
965 wxStrcat(sqlStmt, " NOT NULL");
966 }
967 }
968
969 needComma = TRUE;
970 }
971 // If there is a primary key defined, include it in the create statement
972 for (i = j = 0; i < noCols; i++)
973 {
974 if (colDefs[i].KeyField)
975 {
976 j++;
977 break;
978 }
979 }
980 if (j && pDb->Dbms() != dbmsDBASE) // Found a keyfield
981 {
982 if (pDb->Dbms() != dbmsMY_SQL)
983 {
984 wxStrcat(sqlStmt, ",CONSTRAINT ");
985 wxStrcat(sqlStmt, tableName);
986 wxStrcat(sqlStmt, "_PIDX PRIMARY KEY (");
987 }
988 else
989 {
990 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
991 wxStrcat(sqlStmt, ", PRIMARY KEY (");
992 }
993
994 // List column name(s) of column(s) comprising the primary key
995 for (i = j = 0; i < noCols; i++)
996 {
997 if (colDefs[i].KeyField)
998 {
999 if (j++) // Multi part key, comma separate names
1000 wxStrcat(sqlStmt, ",");
1001 wxStrcat(sqlStmt, colDefs[i].ColName);
1002 }
1003 }
1004 wxStrcat(sqlStmt, ")");
1005 }
1006 // Append the closing parentheses for the create table statement
1007 wxStrcat(sqlStmt, ")");
1008
1009 pDb->WriteSqlLog(sqlStmt);
1010
1011 #ifdef DBDEBUG_CONSOLE
1012 cout << endl << sqlStmt << endl;
1013 #endif
1014
1015 // Execute the CREATE TABLE statement
1016 RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS);
1017 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1018 {
1019 pDb->DispAllErrors(henv, hdbc, hstmt);
1020 pDb->RollbackTrans();
1021 CloseCursor(hstmt);
1022 return(FALSE);
1023 }
1024
1025 // Commit the transaction and close the cursor
1026 if (! pDb->CommitTrans())
1027 return(FALSE);
1028 if (! CloseCursor(hstmt))
1029 return(FALSE);
1030
1031 // Database table created successfully
1032 return(TRUE);
1033
1034 } // wxTable::CreateTable()
1035
1036
1037 /********** wxTable::DropTable() **********/
1038 bool wxTable::DropTable()
1039 {
1040 // NOTE: This function returns TRUE if the Table does not exist, but
1041 // only for identified databases. Code will need to be added
1042 // below for any other databases when those databases are defined
1043 // to handle this situation consistently
1044
1045 char sqlStmt[DB_MAX_STATEMENT_LEN];
1046
1047 sprintf(sqlStmt, "DROP TABLE %s", tableName);
1048
1049 pDb->WriteSqlLog(sqlStmt);
1050
1051 #ifdef DBDEBUG_CONSOLE
1052 cout << endl << sqlStmt << endl;
1053 #endif
1054
1055 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1056 {
1057 // Check for "Base table not found" error and ignore
1058 pDb->GetNextError(henv, hdbc, hstmt);
1059 if (wxStrcmp(pDb->sqlState,"S0002")) // "Base table not found"
1060 {
1061 // Check for product specific error codes
1062 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // 5.x (and lower?)
1063 (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"S1000")) || // untested
1064 (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,"08S01")))) // untested
1065 {
1066 pDb->DispNextError();
1067 pDb->DispAllErrors(henv, hdbc, hstmt);
1068 pDb->RollbackTrans();
1069 CloseCursor(hstmt);
1070 return(FALSE);
1071 }
1072 }
1073 }
1074
1075 // Commit the transaction and close the cursor
1076 if (! pDb->CommitTrans())
1077 return(FALSE);
1078 if (! CloseCursor(hstmt))
1079 return(FALSE);
1080
1081 return(TRUE);
1082 } // wxTable::DropTable()
1083
1084
1085 /********** wxTable::CreateIndex() **********/
1086 bool wxTable::CreateIndex(const char * idxName, bool unique, int noIdxCols, CidxDef *pIdxDefs, bool attemptDrop)
1087 {
1088 char sqlStmt[DB_MAX_STATEMENT_LEN];
1089
1090 // Drop the index first
1091 if (attemptDrop && !DropIndex(idxName))
1092 return (FALSE);
1093
1094 // Build a CREATE INDEX statement
1095 wxStrcpy(sqlStmt, "CREATE ");
1096 if (unique)
1097 wxStrcat(sqlStmt, "UNIQUE ");
1098
1099 wxStrcat(sqlStmt, "INDEX ");
1100 wxStrcat(sqlStmt, idxName);
1101 wxStrcat(sqlStmt, " ON ");
1102 wxStrcat(sqlStmt, tableName);
1103 wxStrcat(sqlStmt, " (");
1104
1105 // Append list of columns making up index
1106 int i;
1107 for (i = 0; i < noIdxCols; i++)
1108 {
1109 wxStrcat(sqlStmt, pIdxDefs[i].ColName);
1110 /* Postgres doesn't cope with ASC */
1111 if (pDb->Dbms() != dbmsPOSTGRES)
1112 {
1113 if (pIdxDefs[i].Ascending)
1114 wxStrcat(sqlStmt, " ASC");
1115 else
1116 wxStrcat(sqlStmt, " DESC");
1117 }
1118
1119 if ((i + 1) < noIdxCols)
1120 wxStrcat(sqlStmt, ",");
1121 }
1122
1123 // Append closing parentheses
1124 wxStrcat(sqlStmt, ")");
1125
1126 pDb->WriteSqlLog(sqlStmt);
1127
1128 #ifdef DBDEBUG_CONSOLE
1129 cout << endl << sqlStmt << endl << endl;
1130 #endif
1131
1132 // Execute the CREATE INDEX statement
1133 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1134 {
1135 pDb->DispAllErrors(henv, hdbc, hstmt);
1136 pDb->RollbackTrans();
1137 CloseCursor(hstmt);
1138 return(FALSE);
1139 }
1140
1141 // Commit the transaction and close the cursor
1142 if (! pDb->CommitTrans())
1143 return(FALSE);
1144 if (! CloseCursor(hstmt))
1145 return(FALSE);
1146
1147 // Index Created Successfully
1148 return(TRUE);
1149
1150 } // wxTable::CreateIndex()
1151
1152
1153 /********** wxTable::DropIndex() **********/
1154 bool wxTable::DropIndex(const char * idxName)
1155 {
1156 // NOTE: This function returns TRUE if the Index does not exist, but
1157 // only for identified databases. Code will need to be added
1158 // below for any other databases when those databases are defined
1159 // to handle this situation consistently
1160
1161 char sqlStmt[DB_MAX_STATEMENT_LEN];
1162
1163 if (pDb->Dbms() == dbmsACCESS)
1164 sprintf(sqlStmt, "DROP INDEX %s ON %s",idxName,tableName);
1165 else if (pDb->Dbms() == dbmsSYBASE_ASE)
1166 sprintf(sqlStmt, "DROP INDEX %s.%s",tableName,idxName);
1167 else
1168 sprintf(sqlStmt, "DROP INDEX %s",idxName);
1169
1170 pDb->WriteSqlLog(sqlStmt);
1171
1172 #ifdef DBDEBUG_CONSOLE
1173 cout << endl << sqlStmt << endl;
1174 #endif
1175
1176 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1177 {
1178 // Check for "Index not found" error and ignore
1179 pDb->GetNextError(henv, hdbc, hstmt);
1180 if (wxStrcmp(pDb->sqlState,"S0012")) // "Index not found"
1181 {
1182 // Check for product specific error codes
1183 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // v5.x (and lower?)
1184 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,"S0002")) || // Base table not found
1185 (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"42S02")) // untested
1186 ))
1187 {
1188 pDb->DispNextError();
1189 pDb->DispAllErrors(henv, hdbc, hstmt);
1190 pDb->RollbackTrans();
1191 CloseCursor(hstmt);
1192 return(FALSE);
1193 }
1194 }
1195 }
1196
1197 // Commit the transaction and close the cursor
1198 if (! pDb->CommitTrans())
1199 return(FALSE);
1200 if (! CloseCursor(hstmt))
1201 return(FALSE);
1202
1203 return(TRUE);
1204 } // wxTable::DropIndex()
1205
1206
1207 /********** wxTable::Insert() **********/
1208 int wxTable::Insert(void)
1209 {
1210 assert(!queryOnly);
1211 if (queryOnly)
1212 return(DB_FAILURE);
1213
1214 bindInsertParams();
1215
1216 // Insert the record by executing the already prepared insert statement
1217 RETCODE retcode;
1218 retcode=SQLExecute(hstmtInsert);
1219 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1220 {
1221 // Check to see if integrity constraint was violated
1222 pDb->GetNextError(henv, hdbc, hstmtInsert);
1223 if (! wxStrcmp(pDb->sqlState, "23000")) // Integrity constraint violated
1224 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
1225 else
1226 {
1227 pDb->DispNextError();
1228 pDb->DispAllErrors(henv, hdbc, hstmtInsert);
1229 return(DB_FAILURE);
1230 }
1231 }
1232
1233 // Record inserted into the datasource successfully
1234 return(DB_SUCCESS);
1235
1236 } // wxTable::Insert()
1237
1238
1239 /********** wxTable::Update() **********/
1240 bool wxTable::Update(void)
1241 {
1242 assert(!queryOnly);
1243 if (queryOnly)
1244 return(FALSE);
1245
1246 char sqlStmt[DB_MAX_STATEMENT_LEN];
1247
1248 // Build the SQL UPDATE statement
1249 GetUpdateStmt(sqlStmt, DB_UPD_KEYFIELDS);
1250
1251 pDb->WriteSqlLog(sqlStmt);
1252
1253 #ifdef DBDEBUG_CONSOLE
1254 cout << endl << sqlStmt << endl << endl;
1255 #endif
1256
1257 // Execute the SQL UPDATE statement
1258 return(execUpdate(sqlStmt));
1259
1260 } // wxTable::Update()
1261
1262
1263 /********** wxTable::Update(pSqlStmt) **********/
1264 bool wxTable::Update(const char *pSqlStmt)
1265 {
1266 assert(!queryOnly);
1267 if (queryOnly)
1268 return(FALSE);
1269
1270 pDb->WriteSqlLog(pSqlStmt);
1271
1272 return(execUpdate(pSqlStmt));
1273
1274 } // wxTable::Update(pSqlStmt)
1275
1276
1277 /********** wxTable::UpdateWhere() **********/
1278 bool wxTable::UpdateWhere(const char *pWhereClause)
1279 {
1280 assert(!queryOnly);
1281 if (queryOnly)
1282 return(FALSE);
1283
1284 char sqlStmt[DB_MAX_STATEMENT_LEN];
1285
1286 // Build the SQL UPDATE statement
1287 GetUpdateStmt(sqlStmt, DB_UPD_WHERE, pWhereClause);
1288
1289 pDb->WriteSqlLog(sqlStmt);
1290
1291 #ifdef DBDEBUG_CONSOLE
1292 cout << endl << sqlStmt << endl << endl;
1293 #endif
1294
1295 // Execute the SQL UPDATE statement
1296 return(execUpdate(sqlStmt));
1297
1298 } // wxTable::UpdateWhere()
1299
1300
1301 /********** wxTable::Delete() **********/
1302 bool wxTable::Delete(void)
1303 {
1304 assert(!queryOnly);
1305 if (queryOnly)
1306 return(FALSE);
1307
1308 char sqlStmt[DB_MAX_STATEMENT_LEN];
1309
1310 // Build the SQL DELETE statement
1311 GetDeleteStmt(sqlStmt, DB_DEL_KEYFIELDS);
1312
1313 pDb->WriteSqlLog(sqlStmt);
1314
1315 // Execute the SQL DELETE statement
1316 return(execDelete(sqlStmt));
1317
1318 } // wxTable::Delete()
1319
1320
1321 /********** wxTable::DeleteWhere() **********/
1322 bool wxTable::DeleteWhere(const char *pWhereClause)
1323 {
1324 assert(!queryOnly);
1325 if (queryOnly)
1326 return(FALSE);
1327
1328 char sqlStmt[DB_MAX_STATEMENT_LEN];
1329
1330 // Build the SQL DELETE statement
1331 GetDeleteStmt(sqlStmt, DB_DEL_WHERE, pWhereClause);
1332
1333 pDb->WriteSqlLog(sqlStmt);
1334
1335 // Execute the SQL DELETE statement
1336 return(execDelete(sqlStmt));
1337
1338 } // wxTable::DeleteWhere()
1339
1340
1341 /********** wxTable::DeleteMatching() **********/
1342 bool wxTable::DeleteMatching(void)
1343 {
1344 assert(!queryOnly);
1345 if (queryOnly)
1346 return(FALSE);
1347
1348 char sqlStmt[DB_MAX_STATEMENT_LEN];
1349
1350 // Build the SQL DELETE statement
1351 GetDeleteStmt(sqlStmt, DB_DEL_MATCHING);
1352
1353 pDb->WriteSqlLog(sqlStmt);
1354
1355 // Execute the SQL DELETE statement
1356 return(execDelete(sqlStmt));
1357
1358 } // wxTable::DeleteMatching()
1359
1360
1361 /********** wxTable::GetUpdateStmt() **********/
1362 void wxTable::GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause)
1363 {
1364 assert(!queryOnly);
1365 if (queryOnly)
1366 return;
1367
1368 char whereClause[DB_MAX_WHERE_CLAUSE_LEN];
1369 bool firstColumn = TRUE;
1370
1371 whereClause[0] = 0;
1372 sprintf(pSqlStmt, "UPDATE %s SET ", tableName);
1373
1374 // Append a list of columns to be updated
1375 int i;
1376 for (i = 0; i < noCols; i++)
1377 {
1378 // Only append Updateable columns
1379 if (colDefs[i].Updateable)
1380 {
1381 if (! firstColumn)
1382 wxStrcat(pSqlStmt, ",");
1383 else
1384 firstColumn = FALSE;
1385 wxStrcat(pSqlStmt, colDefs[i].ColName);
1386 wxStrcat(pSqlStmt, " = ?");
1387 }
1388 }
1389
1390 // Append the WHERE clause to the SQL UPDATE statement
1391 wxStrcat(pSqlStmt, " WHERE ");
1392 switch(typeOfUpd)
1393 {
1394 case DB_UPD_KEYFIELDS:
1395 // If the datasource supports the ROWID column, build
1396 // the where on ROWID for efficiency purposes.
1397 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1398 if (CanUpdByROWID())
1399 {
1400 SDWORD cb;
1401 char rowid[ROWID_LEN];
1402
1403 // Get the ROWID value. If not successful retreiving the ROWID,
1404 // simply fall down through the code and build the WHERE clause
1405 // based on the key fields.
1406 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1407 {
1408 wxStrcat(pSqlStmt, "ROWID = '");
1409 wxStrcat(pSqlStmt, rowid);
1410 wxStrcat(pSqlStmt, "'");
1411 break;
1412 }
1413 }
1414 // Unable to delete by ROWID, so build a WHERE
1415 // clause based on the keyfields.
1416 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS);
1417 wxStrcat(pSqlStmt, whereClause);
1418 break;
1419 case DB_UPD_WHERE:
1420 wxStrcat(pSqlStmt, pWhereClause);
1421 break;
1422 }
1423 } // GetUpdateStmt()
1424
1425
1426 /********** wxTable::GetDeleteStmt() **********/
1427 void wxTable::GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause)
1428 {
1429 assert(!queryOnly);
1430 if (queryOnly)
1431 return;
1432
1433 char whereClause[DB_MAX_WHERE_CLAUSE_LEN];
1434
1435 whereClause[0] = 0;
1436
1437 // Handle the case of DeleteWhere() and the where clause is blank. It should
1438 // delete all records from the database in this case.
1439 if (typeOfDel == DB_DEL_WHERE && (pWhereClause == 0 || wxStrlen(pWhereClause) == 0))
1440 {
1441 sprintf(pSqlStmt, "DELETE FROM %s", tableName);
1442 return;
1443 }
1444
1445 sprintf(pSqlStmt, "DELETE FROM %s WHERE ", tableName);
1446
1447 // Append the WHERE clause to the SQL DELETE statement
1448 switch(typeOfDel)
1449 {
1450 case DB_DEL_KEYFIELDS:
1451 // If the datasource supports the ROWID column, build
1452 // the where on ROWID for efficiency purposes.
1453 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1454 if (CanUpdByROWID())
1455 {
1456 SDWORD cb;
1457 char rowid[ROWID_LEN];
1458
1459 // Get the ROWID value. If not successful retreiving the ROWID,
1460 // simply fall down through the code and build the WHERE clause
1461 // based on the key fields.
1462 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1463 {
1464 wxStrcat(pSqlStmt, "ROWID = '");
1465 wxStrcat(pSqlStmt, rowid);
1466 wxStrcat(pSqlStmt, "'");
1467 break;
1468 }
1469 }
1470 // Unable to delete by ROWID, so build a WHERE
1471 // clause based on the keyfields.
1472 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS);
1473 wxStrcat(pSqlStmt, whereClause);
1474 break;
1475 case DB_DEL_WHERE:
1476 wxStrcat(pSqlStmt, pWhereClause);
1477 break;
1478 case DB_DEL_MATCHING:
1479 GetWhereClause(whereClause, DB_WHERE_MATCHING);
1480 wxStrcat(pSqlStmt, whereClause);
1481 break;
1482 }
1483
1484 } // GetDeleteStmt()
1485
1486
1487 /********** wxTable::GetWhereClause() **********/
1488 void wxTable::GetWhereClause(char *pWhereClause, int typeOfWhere, const char *qualTableName)
1489 /*
1490 * Note: GetWhereClause() currently ignores timestamp columns.
1491 * They are not included as part of the where clause.
1492 */
1493 {
1494 bool moreThanOneColumn = FALSE;
1495 char colValue[255];
1496
1497 // Loop through the columns building a where clause as you go
1498 int i;
1499 for (i = 0; i < noCols; i++)
1500 {
1501 // Determine if this column should be included in the WHERE clause
1502 if ((typeOfWhere == DB_WHERE_KEYFIELDS && colDefs[i].KeyField) ||
1503 (typeOfWhere == DB_WHERE_MATCHING && (! IsColNull(i))))
1504 {
1505 // Skip over timestamp columns
1506 if (colDefs[i].SqlCtype == SQL_C_TIMESTAMP)
1507 continue;
1508 // If there is more than 1 column, join them with the keyword "AND"
1509 if (moreThanOneColumn)
1510 wxStrcat(pWhereClause, " AND ");
1511 else
1512 moreThanOneColumn = TRUE;
1513 // Concatenate where phrase for the column
1514 if (qualTableName && wxStrlen(qualTableName))
1515 {
1516 wxStrcat(pWhereClause, qualTableName);
1517 wxStrcat(pWhereClause, ".");
1518 }
1519 wxStrcat(pWhereClause, colDefs[i].ColName);
1520 wxStrcat(pWhereClause, " = ");
1521 switch(colDefs[i].SqlCtype)
1522 {
1523 case SQL_C_CHAR:
1524 sprintf(colValue, "'%s'", (UCHAR FAR *) colDefs[i].PtrDataObj);
1525 break;
1526 case SQL_C_SSHORT:
1527 sprintf(colValue, "%hi", *((SWORD *) colDefs[i].PtrDataObj));
1528 break;
1529 case SQL_C_USHORT:
1530 sprintf(colValue, "%hu", *((UWORD *) colDefs[i].PtrDataObj));
1531 break;
1532 case SQL_C_SLONG:
1533 sprintf(colValue, "%li", *((SDWORD *) colDefs[i].PtrDataObj));
1534 break;
1535 case SQL_C_ULONG:
1536 sprintf(colValue, "%lu", *((UDWORD *) colDefs[i].PtrDataObj));
1537 break;
1538 case SQL_C_FLOAT:
1539 sprintf(colValue, "%.6f", *((SFLOAT *) colDefs[i].PtrDataObj));
1540 break;
1541 case SQL_C_DOUBLE:
1542 sprintf(colValue, "%.6f", *((SDOUBLE *) colDefs[i].PtrDataObj));
1543 break;
1544 }
1545 wxStrcat(pWhereClause, colValue);
1546 }
1547 }
1548 } // wxTable::GetWhereClause()
1549
1550
1551 /********** wxTable::IsColNull() **********/
1552 bool wxTable::IsColNull(int colNo)
1553 {
1554 switch(colDefs[colNo].SqlCtype)
1555 {
1556 case SQL_C_CHAR:
1557 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1558 case SQL_C_SSHORT:
1559 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1560 case SQL_C_USHORT:
1561 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1562 case SQL_C_SLONG:
1563 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1564 case SQL_C_ULONG:
1565 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1566 case SQL_C_FLOAT:
1567 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1568 case SQL_C_DOUBLE:
1569 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1570 case SQL_C_TIMESTAMP:
1571 TIMESTAMP_STRUCT *pDt;
1572 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1573 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1574 return(TRUE);
1575 else
1576 return(FALSE);
1577 default:
1578 return(TRUE);
1579 }
1580 } // wxTable::IsColNull()
1581
1582
1583 /********** wxTable::CanSelectForUpdate() **********/
1584 bool wxTable::CanSelectForUpdate(void)
1585 {
1586 if (pDb->Dbms() == dbmsMY_SQL)
1587 return FALSE;
1588
1589 if (pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
1590 return(TRUE);
1591 else
1592 return(FALSE);
1593
1594 } // wxTable::CanSelectForUpdate()
1595
1596
1597 /********** wxTable::CanUpdByROWID() **********/
1598 bool wxTable::CanUpdByROWID(void)
1599 {
1600 /*
1601 * NOTE: Returning FALSE for now until this can be debugged,
1602 * as the ROWID is not getting updated correctly
1603 */
1604 return FALSE;
1605
1606 if (pDb->Dbms() == dbmsORACLE)
1607 return(TRUE);
1608 else
1609 return(FALSE);
1610
1611 } // wxTable::CanUpdByROWID()
1612
1613
1614 /********** wxTable::IsCursorClosedOnCommit() **********/
1615 bool wxTable::IsCursorClosedOnCommit(void)
1616 {
1617 if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
1618 return(FALSE);
1619 else
1620 return(TRUE);
1621
1622 } // wxTable::IsCursorClosedOnCommit()
1623
1624
1625 /********** wxTable::ClearMemberVars() **********/
1626 void wxTable::ClearMemberVars(void)
1627 {
1628 // Loop through the columns setting each member variable to zero
1629 int i;
1630 for (i = 0; i < noCols; i++)
1631 {
1632 switch(colDefs[i].SqlCtype)
1633 {
1634 case SQL_C_CHAR:
1635 ((UCHAR FAR *) colDefs[i].PtrDataObj)[0] = 0;
1636 break;
1637 case SQL_C_SSHORT:
1638 *((SWORD *) colDefs[i].PtrDataObj) = 0;
1639 break;
1640 case SQL_C_USHORT:
1641 *((UWORD*) colDefs[i].PtrDataObj) = 0;
1642 break;
1643 case SQL_C_SLONG:
1644 *((SDWORD *) colDefs[i].PtrDataObj) = 0;
1645 break;
1646 case SQL_C_ULONG:
1647 *((UDWORD *) colDefs[i].PtrDataObj) = 0;
1648 break;
1649 case SQL_C_FLOAT:
1650 *((SFLOAT *) colDefs[i].PtrDataObj) = 0.0f;
1651 break;
1652 case SQL_C_DOUBLE:
1653 *((SDOUBLE *) colDefs[i].PtrDataObj) = 0.0f;
1654 break;
1655 case SQL_C_TIMESTAMP:
1656 TIMESTAMP_STRUCT *pDt;
1657 pDt = (TIMESTAMP_STRUCT *) colDefs[i].PtrDataObj;
1658 pDt->year = 0;
1659 pDt->month = 0;
1660 pDt->day = 0;
1661 pDt->hour = 0;
1662 pDt->minute = 0;
1663 pDt->second = 0;
1664 pDt->fraction = 0;
1665 break;
1666 }
1667 }
1668
1669 } // wxTable::ClearMemberVars()
1670
1671
1672 /********** wxTable::SetQueryTimeout() **********/
1673 bool wxTable::SetQueryTimeout(UDWORD nSeconds)
1674 {
1675 if (SQLSetStmtOption(hstmtInsert, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1676 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
1677 if (SQLSetStmtOption(hstmtUpdate, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1678 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
1679 if (SQLSetStmtOption(hstmtDelete, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1680 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
1681 if (SQLSetStmtOption(hstmtInternal, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1682 return(pDb->DispAllErrors(henv, hdbc, hstmtInternal));
1683
1684 // Completed Successfully
1685 return(TRUE);
1686
1687 } // wxTable::SetQueryTimeout()
1688
1689
1690 /********** wxTable::SetColDefs() **********/
1691 void wxTable::SetColDefs (int index, const char *fieldName, int dataType, void *pData,
1692 int cType, int size, bool keyField, bool upd,
1693 bool insAllow, bool derivedCol)
1694 {
1695 if (!colDefs) // May happen if the database connection fails
1696 return;
1697
1698 if (wxStrlen(fieldName) > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
1699 {
1700 wxStrncpy (colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
1701 colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;
1702 }
1703 else
1704 wxStrcpy(colDefs[index].ColName, fieldName);
1705
1706 colDefs[index].DbDataType = dataType;
1707 colDefs[index].PtrDataObj = pData;
1708 colDefs[index].SqlCtype = cType;
1709 colDefs[index].SzDataObj = size;
1710 colDefs[index].KeyField = keyField;
1711 colDefs[index].DerivedCol = derivedCol;
1712 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1713 if (derivedCol)
1714 {
1715 colDefs[index].Updateable = FALSE;
1716 colDefs[index].InsertAllowed = FALSE;
1717 }
1718 else
1719 {
1720 colDefs[index].Updateable = upd;
1721 colDefs[index].InsertAllowed = insAllow;
1722 }
1723
1724 colDefs[index].Null = FALSE;
1725
1726 } // wxTable::SetColDefs()
1727
1728
1729 /********** wxTable::SetColDef() **********/
1730 // BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
1731 //bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
1732 wxColDataPtr* wxTable::SetColDefs (wxColInf *pColInfs, ULONG numCols)
1733 {
1734 assert(pColInfs);
1735 wxColDataPtr *pColDataPtrs = NULL;
1736
1737 if (pColInfs)
1738 {
1739 ULONG index;
1740
1741
1742 pColDataPtrs = new wxColDataPtr[numCols+1];
1743
1744 for (index = 0; index < numCols; index++)
1745 {
1746 /*
1747 wxString title,msg;
1748 title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
1749 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",
1750 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);
1751 msg += " \nDB_DATA_TYPE: ";
1752 switch(pColInfs[index].dbDataType)
1753 {
1754 case DB_DATA_TYPE_VARCHAR:
1755 msg += pDb->typeInfVarchar.TypeName; break;
1756 case DB_DATA_TYPE_INTEGER:
1757 msg += pDb->typeInfInteger.TypeName; break;
1758 case DB_DATA_TYPE_FLOAT:
1759 msg += pDb->typeInfFloat.TypeName; break;
1760 case DB_DATA_TYPE_DATE:
1761 msg += pDb->typeInfDate.TypeName; break;
1762 }
1763 wxMessageBox(msg.GetData(),title.GetData());
1764 */
1765 // Process the fields
1766 switch (pColInfs[index].dbDataType)
1767 {
1768 case DB_DATA_TYPE_VARCHAR:
1769 {
1770 pColDataPtrs[index].PtrDataObj = new char[pColInfs[index].bufferLength+1];
1771 pColDataPtrs[index].SzDataObj = pColInfs[index].bufferLength;
1772 pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
1773 break;
1774 }
1775 case DB_DATA_TYPE_INTEGER:
1776 {
1777 // Can be long or short
1778 if (pColInfs[index].bufferLength == sizeof(long))
1779 {
1780 pColDataPtrs[index].PtrDataObj = new long;
1781 pColDataPtrs[index].SzDataObj = sizeof(long);
1782 pColDataPtrs[index].SqlCtype = SQL_C_SLONG;
1783 }
1784 else
1785 {
1786 pColDataPtrs[index].PtrDataObj = new short;
1787 pColDataPtrs[index].SzDataObj = sizeof(short);
1788 pColDataPtrs[index].SqlCtype = SQL_C_SSHORT;
1789 }
1790 break;
1791 }
1792 case DB_DATA_TYPE_FLOAT:
1793 {
1794 // Can be float or double
1795 if (pColInfs[index].bufferLength == sizeof(float))
1796 {
1797 pColDataPtrs[index].PtrDataObj = new float;
1798 pColDataPtrs[index].SzDataObj = sizeof(float);
1799 pColDataPtrs[index].SqlCtype = SQL_C_FLOAT;
1800 }
1801 else
1802 {
1803 pColDataPtrs[index].PtrDataObj = new double;
1804 pColDataPtrs[index].SzDataObj = sizeof(double);
1805 pColDataPtrs[index].SqlCtype = SQL_C_DOUBLE;
1806 }
1807 break;
1808 }
1809 case DB_DATA_TYPE_DATE:
1810 {
1811 pColDataPtrs[index].PtrDataObj = new TIMESTAMP_STRUCT;
1812 pColDataPtrs[index].SzDataObj = sizeof(TIMESTAMP_STRUCT);
1813 pColDataPtrs[index].SqlCtype = SQL_C_TIMESTAMP;
1814 break;
1815 }
1816 }
1817
1818 SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
1819 }
1820 }
1821 return (pColDataPtrs);
1822 } // wxTable::SetColDef()
1823
1824
1825 /********** wxTable::SetCursor() **********/
1826 void wxTable::SetCursor(HSTMT *hstmtActivate)
1827 {
1828 if (hstmtActivate == DEFAULT_CURSOR)
1829 hstmt = *hstmtDefault;
1830 else
1831 hstmt = *hstmtActivate;
1832
1833 } // wxTable::SetCursor()
1834
1835
1836 /********** wxTable::Count(const char *) **********/
1837 ULONG wxTable::Count(const char *args)
1838 {
1839 ULONG l;
1840 char sqlStmt[DB_MAX_STATEMENT_LEN];
1841 SDWORD cb;
1842
1843 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1844 wxStrcpy(sqlStmt, "SELECT COUNT(");
1845 wxStrcat(sqlStmt, args);
1846 wxStrcat(sqlStmt, ") FROM ");
1847 wxStrcat(sqlStmt, queryTableName);
1848
1849 if (from && wxStrlen(from))
1850 wxStrcat(sqlStmt, from);
1851
1852 // Add the where clause if one is provided
1853 if (where && wxStrlen(where))
1854 {
1855 wxStrcat(sqlStmt, " WHERE ");
1856 wxStrcat(sqlStmt, where);
1857 }
1858
1859 pDb->WriteSqlLog(sqlStmt);
1860
1861 // Initialize the Count cursor if it's not already initialized
1862 if (!hstmtCount)
1863 {
1864 hstmtCount = NewCursor(FALSE,FALSE);
1865 assert(hstmtCount);
1866 if (!hstmtCount)
1867 return(0);
1868 }
1869
1870 // Execute the SQL statement
1871 if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1872 {
1873 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1874 return(0);
1875 }
1876
1877 // Fetch the record
1878 if (SQLFetch(*hstmtCount) != SQL_SUCCESS)
1879 {
1880 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1881 return(0);
1882 }
1883
1884 // Obtain the result
1885 if (SQLGetData(*hstmtCount, 1, SQL_C_ULONG, &l, sizeof(l), &cb) != SQL_SUCCESS)
1886 {
1887 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1888 return(0);
1889 }
1890
1891 // Free the cursor
1892 if (SQLFreeStmt(*hstmtCount, SQL_CLOSE) != SQL_SUCCESS)
1893 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1894
1895 // Return the record count
1896 return(l);
1897
1898 } // wxTable::Count()
1899
1900
1901 /********** wxTable::Refresh() **********/
1902 bool wxTable::Refresh(void)
1903 {
1904 bool result = TRUE;
1905
1906 // Switch to the internal cursor so any active cursors are not corrupted
1907 HSTMT currCursor = GetCursor();
1908 hstmt = hstmtInternal;
1909
1910 // Save the where and order by clauses
1911 char *saveWhere = where;
1912 char *saveOrderBy = orderBy;
1913
1914 // Build a where clause to refetch the record with. Try and use the
1915 // ROWID if it's available, ow use the key fields.
1916 char whereClause[DB_MAX_WHERE_CLAUSE_LEN+1];
1917 wxStrcpy(whereClause, "");
1918 if (CanUpdByROWID())
1919 {
1920 SDWORD cb;
1921 char rowid[ROWID_LEN+1];
1922
1923 // Get the ROWID value. If not successful retreiving the ROWID,
1924 // simply fall down through the code and build the WHERE clause
1925 // based on the key fields.
1926 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1927 {
1928 wxStrcat(whereClause, queryTableName);
1929 wxStrcat(whereClause, ".ROWID = '");
1930 wxStrcat(whereClause, rowid);
1931 wxStrcat(whereClause, "'");
1932 }
1933 }
1934
1935 // If unable to use the ROWID, build a where clause from the keyfields
1936 if (wxStrlen(whereClause) == 0)
1937 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS, queryTableName);
1938
1939 // Requery the record
1940 where = whereClause;
1941 orderBy = 0;
1942 if (!Query())
1943 result = FALSE;
1944
1945 if (result && !GetNext())
1946 result = FALSE;
1947
1948 // Switch back to original cursor
1949 SetCursor(&currCursor);
1950
1951 // Free the internal cursor
1952 if (SQLFreeStmt(hstmtInternal, SQL_CLOSE) != SQL_SUCCESS)
1953 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
1954
1955 // Restore the original where and order by clauses
1956 where = saveWhere;
1957 orderBy = saveOrderBy;
1958
1959 return(result);
1960
1961 } // wxTable::Refresh()
1962
1963
1964 /********** wxTable::SetNull(int colNo) **********/
1965 bool wxTable::SetNull(int colNo)
1966 {
1967 if (colNo < noCols)
1968 return(colDefs[colNo].Null = TRUE);
1969 else
1970 return(FALSE);
1971
1972 } // wxTable::SetNull(int colNo)
1973
1974
1975 /********** wxTable::SetNull(char *colName) **********/
1976 bool wxTable::SetNull(const char *colName)
1977 {
1978 int i;
1979 for (i = 0; i < noCols; i++)
1980 {
1981 if (!wxStricmp(colName, colDefs[i].ColName))
1982 break;
1983 }
1984
1985 if (i < noCols)
1986 return(colDefs[i].Null = TRUE);
1987 else
1988 return(FALSE);
1989
1990 } // wxTable::SetNull(char *colName)
1991
1992
1993 /********** wxTable::NewCursor() **********/
1994 HSTMT *wxTable::NewCursor(bool setCursor, bool bindColumns)
1995 {
1996 HSTMT *newHSTMT = new HSTMT;
1997 assert(newHSTMT);
1998 if (!newHSTMT)
1999 return(0);
2000
2001 if (SQLAllocStmt(hdbc, newHSTMT) != SQL_SUCCESS)
2002 {
2003 pDb->DispAllErrors(henv, hdbc);
2004 delete newHSTMT;
2005 return(0);
2006 }
2007
2008 if (SQLSetStmtOption(*newHSTMT, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
2009 {
2010 pDb->DispAllErrors(henv, hdbc, *newHSTMT);
2011 delete newHSTMT;
2012 return(0);
2013 }
2014
2015 if (bindColumns)
2016 {
2017 if(!bindCols(*newHSTMT))
2018 {
2019 delete newHSTMT;
2020 return(0);
2021 }
2022 }
2023
2024 if (setCursor)
2025 SetCursor(newHSTMT);
2026
2027 return(newHSTMT);
2028
2029 } // wxTable::NewCursor()
2030
2031
2032 /********** wxTable::DeleteCursor() **********/
2033 bool wxTable::DeleteCursor(HSTMT *hstmtDel)
2034 {
2035 bool result = TRUE;
2036
2037 if (!hstmtDel) // Cursor already deleted
2038 return(result);
2039
2040 if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2041 {
2042 pDb->DispAllErrors(henv, hdbc);
2043 result = FALSE;
2044 }
2045
2046 delete hstmtDel;
2047
2048 return(result);
2049
2050 } // wxTable::DeleteCursor()
2051
2052 #endif // wxUSE_ODBC
2053