]> git.saurik.com Git - wxWidgets.git/blob - src/common/dbtable.cpp
Cleaned up the API for class/structure/function names to follow the wxWindows convent...
[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 wxTablesInUse *tableInUse;
151 tableInUse = new wxTablesInUse();
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 (((wxTablesInUse *)pNode->Data())->tableID == tableID)
267 {
268 found = TRUE;
269 if (!TablesInUse.DeleteNode(pNode))
270 wxLogDebug (s.GetData(),"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, wxIdxDef *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,
1501 const char *qualTableName, bool useLikeComparison)
1502 /*
1503 * Note: GetWhereClause() currently ignores timestamp columns.
1504 * They are not included as part of the where clause.
1505 */
1506 {
1507 bool moreThanOneColumn = FALSE;
1508 char colValue[255];
1509
1510 // Loop through the columns building a where clause as you go
1511 int i;
1512 for (i = 0; i < noCols; i++)
1513 {
1514 // Determine if this column should be included in the WHERE clause
1515 if ((typeOfWhere == DB_WHERE_KEYFIELDS && colDefs[i].KeyField) ||
1516 (typeOfWhere == DB_WHERE_MATCHING && (! IsColNull(i))))
1517 {
1518 // Skip over timestamp columns
1519 if (colDefs[i].SqlCtype == SQL_C_TIMESTAMP)
1520 continue;
1521 // If there is more than 1 column, join them with the keyword "AND"
1522 if (moreThanOneColumn)
1523 wxStrcat(pWhereClause, " AND ");
1524 else
1525 moreThanOneColumn = TRUE;
1526 // Concatenate where phrase for the column
1527 if (qualTableName && wxStrlen(qualTableName))
1528 {
1529 wxStrcat(pWhereClause, qualTableName);
1530 wxStrcat(pWhereClause, ".");
1531 }
1532 wxStrcat(pWhereClause, colDefs[i].ColName);
1533 if (useLikeComparison && (colDefs[i].SqlCtype == SQL_C_CHAR))
1534 wxStrcat(pWhereClause, " LIKE ");
1535 else
1536 wxStrcat(pWhereClause, " = ");
1537 switch(colDefs[i].SqlCtype)
1538 {
1539 case SQL_C_CHAR:
1540 sprintf(colValue, "'%s'", (UCHAR FAR *) colDefs[i].PtrDataObj);
1541 break;
1542 case SQL_C_SSHORT:
1543 sprintf(colValue, "%hi", *((SWORD *) colDefs[i].PtrDataObj));
1544 break;
1545 case SQL_C_USHORT:
1546 sprintf(colValue, "%hu", *((UWORD *) colDefs[i].PtrDataObj));
1547 break;
1548 case SQL_C_SLONG:
1549 sprintf(colValue, "%li", *((SDWORD *) colDefs[i].PtrDataObj));
1550 break;
1551 case SQL_C_ULONG:
1552 sprintf(colValue, "%lu", *((UDWORD *) colDefs[i].PtrDataObj));
1553 break;
1554 case SQL_C_FLOAT:
1555 sprintf(colValue, "%.6f", *((SFLOAT *) colDefs[i].PtrDataObj));
1556 break;
1557 case SQL_C_DOUBLE:
1558 sprintf(colValue, "%.6f", *((SDOUBLE *) colDefs[i].PtrDataObj));
1559 break;
1560 }
1561 wxStrcat(pWhereClause, colValue);
1562 }
1563 }
1564 } // wxTable::GetWhereClause()
1565
1566
1567 /********** wxTable::IsColNull() **********/
1568 bool wxTable::IsColNull(int colNo)
1569 {
1570 switch(colDefs[colNo].SqlCtype)
1571 {
1572 case SQL_C_CHAR:
1573 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1574 case SQL_C_SSHORT:
1575 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1576 case SQL_C_USHORT:
1577 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1578 case SQL_C_SLONG:
1579 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1580 case SQL_C_ULONG:
1581 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1582 case SQL_C_FLOAT:
1583 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1584 case SQL_C_DOUBLE:
1585 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1586 case SQL_C_TIMESTAMP:
1587 TIMESTAMP_STRUCT *pDt;
1588 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1589 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1590 return(TRUE);
1591 else
1592 return(FALSE);
1593 default:
1594 return(TRUE);
1595 }
1596 } // wxTable::IsColNull()
1597
1598
1599 /********** wxTable::CanSelectForUpdate() **********/
1600 bool wxTable::CanSelectForUpdate(void)
1601 {
1602 if (pDb->Dbms() == dbmsMY_SQL)
1603 return FALSE;
1604
1605 if (pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
1606 return(TRUE);
1607 else
1608 return(FALSE);
1609
1610 } // wxTable::CanSelectForUpdate()
1611
1612
1613 /********** wxTable::CanUpdByROWID() **********/
1614 bool wxTable::CanUpdByROWID(void)
1615 {
1616 /*
1617 * NOTE: Returning FALSE for now until this can be debugged,
1618 * as the ROWID is not getting updated correctly
1619 */
1620 return FALSE;
1621
1622 if (pDb->Dbms() == dbmsORACLE)
1623 return(TRUE);
1624 else
1625 return(FALSE);
1626
1627 } // wxTable::CanUpdByROWID()
1628
1629
1630 /********** wxTable::IsCursorClosedOnCommit() **********/
1631 bool wxTable::IsCursorClosedOnCommit(void)
1632 {
1633 if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
1634 return(FALSE);
1635 else
1636 return(TRUE);
1637
1638 } // wxTable::IsCursorClosedOnCommit()
1639
1640
1641 /********** wxTable::ClearMemberVars() **********/
1642 void wxTable::ClearMemberVars(void)
1643 {
1644 // Loop through the columns setting each member variable to zero
1645 int i;
1646 for (i = 0; i < noCols; i++)
1647 {
1648 switch(colDefs[i].SqlCtype)
1649 {
1650 case SQL_C_CHAR:
1651 ((UCHAR FAR *) colDefs[i].PtrDataObj)[0] = 0;
1652 break;
1653 case SQL_C_SSHORT:
1654 *((SWORD *) colDefs[i].PtrDataObj) = 0;
1655 break;
1656 case SQL_C_USHORT:
1657 *((UWORD*) colDefs[i].PtrDataObj) = 0;
1658 break;
1659 case SQL_C_SLONG:
1660 *((SDWORD *) colDefs[i].PtrDataObj) = 0;
1661 break;
1662 case SQL_C_ULONG:
1663 *((UDWORD *) colDefs[i].PtrDataObj) = 0;
1664 break;
1665 case SQL_C_FLOAT:
1666 *((SFLOAT *) colDefs[i].PtrDataObj) = 0.0f;
1667 break;
1668 case SQL_C_DOUBLE:
1669 *((SDOUBLE *) colDefs[i].PtrDataObj) = 0.0f;
1670 break;
1671 case SQL_C_TIMESTAMP:
1672 TIMESTAMP_STRUCT *pDt;
1673 pDt = (TIMESTAMP_STRUCT *) colDefs[i].PtrDataObj;
1674 pDt->year = 0;
1675 pDt->month = 0;
1676 pDt->day = 0;
1677 pDt->hour = 0;
1678 pDt->minute = 0;
1679 pDt->second = 0;
1680 pDt->fraction = 0;
1681 break;
1682
1683 }
1684 }
1685
1686 } // wxTable::ClearMemberVars()
1687
1688
1689 /********** wxTable::SetQueryTimeout() **********/
1690 bool wxTable::SetQueryTimeout(UDWORD nSeconds)
1691 {
1692 if (SQLSetStmtOption(hstmtInsert, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1693 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
1694 if (SQLSetStmtOption(hstmtUpdate, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1695 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
1696 if (SQLSetStmtOption(hstmtDelete, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1697 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
1698 if (SQLSetStmtOption(hstmtInternal, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1699 return(pDb->DispAllErrors(henv, hdbc, hstmtInternal));
1700
1701 // Completed Successfully
1702 return(TRUE);
1703
1704 } // wxTable::SetQueryTimeout()
1705
1706
1707 /********** wxTable::SetColDefs() **********/
1708 void wxTable::SetColDefs (int index, const char *fieldName, int dataType, void *pData,
1709 int cType, int size, bool keyField, bool upd,
1710 bool insAllow, bool derivedCol)
1711 {
1712 if (!colDefs) // May happen if the database connection fails
1713 return;
1714
1715 if (wxStrlen(fieldName) > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
1716 {
1717 wxStrncpy (colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
1718 colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;
1719 }
1720 else
1721 wxStrcpy(colDefs[index].ColName, fieldName);
1722
1723 colDefs[index].DbDataType = dataType;
1724 colDefs[index].PtrDataObj = pData;
1725 colDefs[index].SqlCtype = cType;
1726 colDefs[index].SzDataObj = size;
1727 colDefs[index].KeyField = keyField;
1728 colDefs[index].DerivedCol = derivedCol;
1729 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1730 if (derivedCol)
1731 {
1732 colDefs[index].Updateable = FALSE;
1733 colDefs[index].InsertAllowed = FALSE;
1734 }
1735 else
1736 {
1737 colDefs[index].Updateable = upd;
1738 colDefs[index].InsertAllowed = insAllow;
1739 }
1740
1741 colDefs[index].Null = FALSE;
1742
1743 } // wxTable::SetColDefs()
1744
1745
1746 /********** wxTable::SetColDef() **********/
1747 wxColDataPtr* wxTable::SetColDefs (wxColInf *pColInfs, ULONG numCols)
1748 {
1749 assert(pColInfs);
1750 wxColDataPtr *pColDataPtrs = NULL;
1751
1752 if (pColInfs)
1753 {
1754 ULONG index;
1755
1756
1757 pColDataPtrs = new wxColDataPtr[numCols+1];
1758
1759 for (index = 0; index < numCols; index++)
1760 {
1761 // Process the fields
1762 switch (pColInfs[index].dbDataType)
1763 {
1764 case DB_DATA_TYPE_VARCHAR:
1765 {
1766 pColDataPtrs[index].PtrDataObj = new char[pColInfs[index].bufferLength+1];
1767 pColDataPtrs[index].SzDataObj = pColInfs[index].columnSize;
1768 pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
1769 break;
1770 }
1771 case DB_DATA_TYPE_INTEGER:
1772 {
1773 // Can be long or short
1774 if (pColInfs[index].bufferLength == sizeof(long))
1775 {
1776 pColDataPtrs[index].PtrDataObj = new long;
1777 pColDataPtrs[index].SzDataObj = sizeof(long);
1778 pColDataPtrs[index].SqlCtype = SQL_C_SLONG;
1779 }
1780 else
1781 {
1782 pColDataPtrs[index].PtrDataObj = new short;
1783 pColDataPtrs[index].SzDataObj = sizeof(short);
1784 pColDataPtrs[index].SqlCtype = SQL_C_SSHORT;
1785 }
1786 break;
1787 }
1788 case DB_DATA_TYPE_FLOAT:
1789 {
1790 // Can be float or double
1791 if (pColInfs[index].bufferLength == sizeof(float))
1792 {
1793 pColDataPtrs[index].PtrDataObj = new float;
1794 pColDataPtrs[index].SzDataObj = sizeof(float);
1795 pColDataPtrs[index].SqlCtype = SQL_C_FLOAT;
1796 }
1797 else
1798 {
1799 pColDataPtrs[index].PtrDataObj = new double;
1800 pColDataPtrs[index].SzDataObj = sizeof(double);
1801 pColDataPtrs[index].SqlCtype = SQL_C_DOUBLE;
1802 }
1803 break;
1804 }
1805 case DB_DATA_TYPE_DATE:
1806 {
1807 pColDataPtrs[index].PtrDataObj = new TIMESTAMP_STRUCT;
1808 pColDataPtrs[index].SzDataObj = sizeof(TIMESTAMP_STRUCT);
1809 pColDataPtrs[index].SqlCtype = SQL_C_TIMESTAMP;
1810 break;
1811 }
1812 }
1813
1814 SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
1815 }
1816 }
1817 return (pColDataPtrs);
1818 } // wxTable::SetColDef()
1819
1820
1821 /********** wxTable::SetCursor() **********/
1822 void wxTable::SetCursor(HSTMT *hstmtActivate)
1823 {
1824 if (hstmtActivate == DEFAULT_CURSOR)
1825 hstmt = *hstmtDefault;
1826 else
1827 hstmt = *hstmtActivate;
1828
1829 } // wxTable::SetCursor()
1830
1831
1832 /********** wxTable::Count(const char *) **********/
1833 ULONG wxTable::Count(const char *args)
1834 {
1835 ULONG l;
1836 wxString sqlStmt;
1837 SDWORD cb;
1838
1839 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1840 sqlStmt = "SELECT COUNT(";
1841 sqlStmt += args;
1842 sqlStmt += ") FROM ";
1843 sqlStmt += queryTableName;
1844
1845 if (from && wxStrlen(from))
1846 sqlStmt += from;
1847
1848 // Add the where clause if one is provided
1849 if (where && wxStrlen(where))
1850 {
1851 sqlStmt += " WHERE ";
1852 sqlStmt += where;
1853 }
1854
1855 pDb->WriteSqlLog(sqlStmt.GetData());
1856
1857 // Initialize the Count cursor if it's not already initialized
1858 if (!hstmtCount)
1859 {
1860 hstmtCount = NewCursor(FALSE,FALSE);
1861 assert(hstmtCount);
1862 if (!hstmtCount)
1863 return(0);
1864 }
1865
1866 // Execute the SQL statement
1867 if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
1868 {
1869 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1870 return(0);
1871 }
1872
1873 // Fetch the record
1874 if (SQLFetch(*hstmtCount) != SQL_SUCCESS)
1875 {
1876 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1877 return(0);
1878 }
1879
1880 // Obtain the result
1881 if (SQLGetData(*hstmtCount, 1, SQL_C_ULONG, &l, sizeof(l), &cb) != SQL_SUCCESS)
1882 {
1883 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1884 return(0);
1885 }
1886
1887 // Free the cursor
1888 if (SQLFreeStmt(*hstmtCount, SQL_CLOSE) != SQL_SUCCESS)
1889 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1890
1891 // Return the record count
1892 return(l);
1893
1894 } // wxTable::Count()
1895
1896
1897 /********** wxTable::Refresh() **********/
1898 bool wxTable::Refresh(void)
1899 {
1900 bool result = TRUE;
1901
1902 // Switch to the internal cursor so any active cursors are not corrupted
1903 HSTMT currCursor = GetCursor();
1904 hstmt = hstmtInternal;
1905
1906 // Save the where and order by clauses
1907 char *saveWhere = where;
1908 char *saveOrderBy = orderBy;
1909
1910 // Build a where clause to refetch the record with. Try and use the
1911 // ROWID if it's available, ow use the key fields.
1912 char whereClause[DB_MAX_WHERE_CLAUSE_LEN+1];
1913 wxStrcpy(whereClause, "");
1914 if (CanUpdByROWID())
1915 {
1916 SDWORD cb;
1917 char rowid[ROWID_LEN+1];
1918
1919 // Get the ROWID value. If not successful retreiving the ROWID,
1920 // simply fall down through the code and build the WHERE clause
1921 // based on the key fields.
1922 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1923 {
1924 wxStrcat(whereClause, queryTableName);
1925 wxStrcat(whereClause, ".ROWID = '");
1926 wxStrcat(whereClause, rowid);
1927 wxStrcat(whereClause, "'");
1928 }
1929 }
1930
1931 // If unable to use the ROWID, build a where clause from the keyfields
1932 if (wxStrlen(whereClause) == 0)
1933 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS, queryTableName);
1934
1935 // Requery the record
1936 where = whereClause;
1937 orderBy = 0;
1938 if (!Query())
1939 result = FALSE;
1940
1941 if (result && !GetNext())
1942 result = FALSE;
1943
1944 // Switch back to original cursor
1945 SetCursor(&currCursor);
1946
1947 // Free the internal cursor
1948 if (SQLFreeStmt(hstmtInternal, SQL_CLOSE) != SQL_SUCCESS)
1949 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
1950
1951 // Restore the original where and order by clauses
1952 where = saveWhere;
1953 orderBy = saveOrderBy;
1954
1955 return(result);
1956
1957 } // wxTable::Refresh()
1958
1959
1960 /********** wxTable::SetNull(int colNo) **********/
1961 bool wxTable::SetNull(int colNo)
1962 {
1963 if (colNo < noCols)
1964 return(colDefs[colNo].Null = TRUE);
1965 else
1966 return(FALSE);
1967
1968 } // wxTable::SetNull(int colNo)
1969
1970
1971 /********** wxTable::SetNull(char *colName) **********/
1972 bool wxTable::SetNull(const char *colName)
1973 {
1974 int i;
1975 for (i = 0; i < noCols; i++)
1976 {
1977 if (!wxStricmp(colName, colDefs[i].ColName))
1978 break;
1979 }
1980
1981 if (i < noCols)
1982 return(colDefs[i].Null = TRUE);
1983 else
1984 return(FALSE);
1985
1986 } // wxTable::SetNull(char *colName)
1987
1988
1989 /********** wxTable::NewCursor() **********/
1990 HSTMT *wxTable::NewCursor(bool setCursor, bool bindColumns)
1991 {
1992 HSTMT *newHSTMT = new HSTMT;
1993 assert(newHSTMT);
1994 if (!newHSTMT)
1995 return(0);
1996
1997 if (SQLAllocStmt(hdbc, newHSTMT) != SQL_SUCCESS)
1998 {
1999 pDb->DispAllErrors(henv, hdbc);
2000 delete newHSTMT;
2001 return(0);
2002 }
2003
2004 if (SQLSetStmtOption(*newHSTMT, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
2005 {
2006 pDb->DispAllErrors(henv, hdbc, *newHSTMT);
2007 delete newHSTMT;
2008 return(0);
2009 }
2010
2011 if (bindColumns)
2012 {
2013 if(!bindCols(*newHSTMT))
2014 {
2015 delete newHSTMT;
2016 return(0);
2017 }
2018 }
2019
2020 if (setCursor)
2021 SetCursor(newHSTMT);
2022
2023 return(newHSTMT);
2024
2025 } // wxTable::NewCursor()
2026
2027
2028 /********** wxTable::DeleteCursor() **********/
2029 bool wxTable::DeleteCursor(HSTMT *hstmtDel)
2030 {
2031 bool result = TRUE;
2032
2033 if (!hstmtDel) // Cursor already deleted
2034 return(result);
2035
2036 if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2037 {
2038 pDb->DispAllErrors(henv, hdbc);
2039 result = FALSE;
2040 }
2041
2042 delete hstmtDel;
2043
2044 return(result);
2045
2046 } // wxTable::DeleteCursor()
2047
2048 #endif // wxUSE_ODBC
2049