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