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