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