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