]> git.saurik.com Git - wxWidgets.git/blame - src/common/dbtable.cpp
Corrected my correction.
[wxWidgets.git] / src / common / dbtable.cpp
CommitLineData
108106cf 1///////////////////////////////////////////////////////////////////////////////
1fc5dd6f 2// Name: dbtable.cpp
108106cf
JS
3// Purpose: Implementation of the wxTable class.
4// Author: Doug Card
67e9aaa3 5// Modified by: George Tasker
89894079
VZ
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
108106cf
JS
11// Created: 9.96
12// RCS-ID: $Id$
13// Copyright: (c) 1996 Remstar International, Inc.
14// Licence: wxWindows licence, plus:
1e92909e 15// Notice: This class library and its intellectual design are free of charge for use,
108106cf
JS
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
a2115c88
GT
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
89894079
VZ
38# ifdef __GNUG__
39# pragma implementation "dbtable.h"
40# endif
108106cf 41#endif
108106cf 42
0b8410f3
GT
43#ifdef DBDEBUG_CONSOLE
44 #include "wx/ioswrap.h"
45#endif
108106cf
JS
46
47#ifdef __BORLANDC__
1e92909e 48 #pragma hdrstop
108106cf
JS
49#endif //__BORLANDC__
50
a2115c88 51#if wxMAJOR_VERSION == 2
1e92909e
GT
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"
0efe4a98 58 #include "wx/log.h"
89894079
VZ
59 #endif
60 #include "wx/filefn.h"
a2115c88 61#endif
108106cf 62
a2115c88 63#if wxMAJOR_VERSION == 1
89894079
VZ
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
a2115c88 72#endif
108106cf 73
a2115c88 74#if wxUSE_ODBC
108106cf
JS
75
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
1fc5dd6f 79#include <assert.h>
67e9aaa3 80
a2115c88 81#if wxMAJOR_VERSION == 1
89894079 82 #include "table.h"
a2115c88 83#elif wxMAJOR_VERSION == 2
89894079 84 #include "wx/dbtable.h"
a2115c88 85#endif
108106cf 86
1fc5dd6f 87#ifdef __UNIX__
108106cf
JS
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
1fc5dd6f 93# ifdef LINUX
108106cf
JS
94# include <sys/minmax.h>
95# endif
96#endif
97
a2115c88
GT
98ULONG lastTableID = 0;
99
100
e041ce57 101#ifdef __WXDEBUG__
89894079 102 wxList TablesInUse;
a2115c88
GT
103#endif
104
105
108106cf 106/********** wxTable::wxTable() **********/
a2115c88 107wxTable::wxTable(wxDB *pwxDB, const char *tblName, const int nCols,
89894079 108 const char *qryTblName, bool qryOnly, const char *tblPath)
108106cf 109{
89894079
VZ
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
1e92909e 145 wxString s;
89894079 146 tableID = ++lastTableID;
1e92909e 147 s.sprintf("wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName,tableID,pDb);
a2115c88 148
e041ce57 149#ifdef __WXDEBUG__
8128349e
GT
150 wxTablesInUse *tableInUse;
151 tableInUse = new wxTablesInUse();
89894079 152 tableInUse->tableName = tblName;
1e92909e
GT
153 tableInUse->tableID = tableID;
154 tableInUse->pDb = pDb;
89894079 155 TablesInUse.Append(tableInUse);
a2115c88
GT
156#endif
157
1e92909e 158 pDb->WriteSqlLog(s.GetData());
89894079
VZ
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);
a2115c88 198#ifdef DBDEBUG_CONSOLE
89894079
VZ
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:
1e92909e 205 cout << "Static"; break;
89894079
VZ
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;
108106cf 212#endif
89894079
VZ
213 }
214 else
215 {
216 pDb->DispNextError();
217 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
218 }
219 }
a2115c88 220#ifdef DBDEBUG_CONSOLE
89894079
VZ
221 else
222 cout << "Cursor Type set to STATIC" << endl << endl;
108106cf
JS
223#endif
224
89894079
VZ
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;
108106cf
JS
242
243} // wxTable::wxTable()
244
67e9aaa3 245
108106cf
JS
246/********** wxTable::~wxTable() **********/
247wxTable::~wxTable()
248{
1e92909e 249 wxString s;
89894079
VZ
250 if (pDb)
251 {
1e92909e
GT
252 s.sprintf("wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName,tableID,pDb);
253 pDb->WriteSqlLog(s.GetData());
89894079 254 }
a2115c88 255
e041ce57 256#ifdef __WXDEBUG__
89894079
VZ
257 if (tableID)
258 {
a4086039 259 TablesInUse.DeleteContents(TRUE);
89894079 260 bool found = FALSE;
1e92909e 261
0b8410f3 262 wxNode *pNode;
89894079
VZ
263 pNode = TablesInUse.First();
264 while (pNode && !found)
265 {
8128349e 266 if (((wxTablesInUse *)pNode->Data())->tableID == tableID)
89894079
VZ
267 {
268 found = TRUE;
269 if (!TablesInUse.DeleteNode(pNode))
8128349e 270 wxLogDebug (s.GetData(),"Unable to delete node!");
89894079
VZ
271 }
272 else
273 pNode = pNode->Next();
274 }
275 if (!found)
276 {
1e92909e
GT
277 wxString msg;
278 msg.sprintf("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s.GetData());
0b8410f3 279 wxLogDebug (msg.GetData(),"NOTICE...");
89894079
VZ
280 }
281 }
a2115c88 282#endif
e041ce57 283
7c5c05ae
BJ
284
285
89894079
VZ
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);
7c5c05ae 300
89894079
VZ
301 if (hstmtDelete)
302 if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
7c5c05ae 303
89894079
VZ
304 if (hstmtUpdate)
305 if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
306 pDb->DispAllErrors(henv, hdbc);
7c5c05ae 307
89894079
VZ
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);
7c5c05ae 316
89894079
VZ
317 if (hstmtCount)
318 DeleteCursor(hstmtCount);
108106cf 319
7c5c05ae 320
108106cf
JS
321} // wxTable::~wxTable()
322
67e9aaa3
GT
323
324
6919c53f
GT
325/***************************** PRIVATE FUNCTIONS *****************************/
326
67e9aaa3
GT
327
328
6919c53f
GT
329/********** wxTable::bindInsertParams() **********/
330bool wxTable::bindInsertParams(void)
331{
89894079
VZ
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);
6919c53f
GT
392
393} // wxTable::bindInsertParams()
394
67e9aaa3 395
6919c53f
GT
396/********** wxTable::bindUpdateParams() **********/
397bool wxTable::bindUpdateParams(void)
398{
89894079
VZ
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,
1e92909e
GT
446 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
447 precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
89894079
VZ
448 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
449 }
450
451 // Completed successfully
452 return(TRUE);
6919c53f
GT
453
454} // wxTable::bindUpdateParams()
455
67e9aaa3 456
6919c53f
GT
457/********** wxTable::bindCols() **********/
458bool wxTable::bindCols(HSTMT cursor)
459{
89894079
VZ
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,
1e92909e 467 colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
89894079
VZ
468 return(pDb->DispAllErrors(henv, hdbc, cursor));
469 }
470
471 // Completed successfully
472 return(TRUE);
6919c53f
GT
473
474} // wxTable::bindCols()
475
67e9aaa3 476
6919c53f
GT
477/********** wxTable::getRec() **********/
478bool wxTable::getRec(UWORD fetchType)
479{
89894079
VZ
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);
6919c53f
GT
510
511} // wxTable::getRec()
512
67e9aaa3 513
6919c53f
GT
514/********** wxTable::execDelete() **********/
515bool wxTable::execDelete(const char *pSqlStmt)
516{
89894079
VZ
517 // Execute the DELETE statement
518 if (SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
519 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
6919c53f 520
89894079
VZ
521 // Record deleted successfully
522 return(TRUE);
6919c53f
GT
523
524} // wxTable::execDelete()
525
67e9aaa3 526
6919c53f
GT
527/********** wxTable::execUpdate() **********/
528bool wxTable::execUpdate(const char *pSqlStmt)
529{
89894079
VZ
530 // Execute the UPDATE statement
531 if (SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
532 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
6919c53f 533
89894079
VZ
534 // Record deleted successfully
535 return(TRUE);
6919c53f
GT
536
537} // wxTable::execUpdate()
538
67e9aaa3 539
6919c53f
GT
540/********** wxTable::query() **********/
541bool wxTable::query(int queryType, bool forUpdate, bool distinct, char *pSqlStmt)
542{
89894079 543 char sqlStmt[DB_MAX_STATEMENT_LEN];
6919c53f 544
89894079
VZ
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;
6919c53f 551
89894079
VZ
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 }
6919c53f 558
89894079
VZ
559 // Make sure the cursor is closed first
560 if (! CloseCursor(hstmt))
561 return(FALSE);
6919c53f 562
89894079
VZ
563 // Execute the SQL SELECT statement
564 int retcode;
6919c53f 565
89894079
VZ
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));
6919c53f 569
89894079
VZ
570 // Completed successfully
571 return(TRUE);
6919c53f
GT
572
573} // wxTable::query()
574
575
67e9aaa3
GT
576/***************************** PUBLIC FUNCTIONS *****************************/
577
6919c53f 578
108106cf
JS
579/********** wxTable::Open() **********/
580bool wxTable::Open(void)
581{
89894079
VZ
582 if (!pDb)
583 return FALSE;
584
585 int i;
1e92909e 586 wxString sqlStmt;
89894079
VZ
587
588 // Verify that the table exists in the database
e16143f6 589 if (!pDb->TableExists(tableName,pDb->GetUsername(),tablePath))
89894079 590 {
e16143f6 591 wxString s;
89894079 592 if (wxStrcmp(tablePath,""))
e16143f6
GT
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());
89894079
VZ
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;
1e92909e 625 sqlStmt.sprintf("INSERT INTO %s (", tableName);
89894079
VZ
626 for (i = 0; i < noCols; i++)
627 {
628 if (! colDefs[i].InsertAllowed)
629 continue;
630 if (needComma)
1e92909e
GT
631 sqlStmt += ",";
632 sqlStmt += colDefs[i].ColName;
89894079
VZ
633 needComma = TRUE;
634 }
635 needComma = FALSE;
1e92909e 636 sqlStmt += ") VALUES (";
89894079
VZ
637 for (i = 0; i < noCols; i++)
638 {
639 if (! colDefs[i].InsertAllowed)
640 continue;
641 if (needComma)
1e92909e
GT
642 sqlStmt += ",";
643 sqlStmt += "?";
89894079
VZ
644 needComma = TRUE;
645 }
1e92909e 646 sqlStmt += ")";
89894079
VZ
647
648// pDb->WriteSqlLog(sqlStmt);
649
650 // Prepare the insert statement for execution
1e92909e 651 if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
652 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
653 }
654
655 // Completed successfully
656 return(TRUE);
108106cf
JS
657
658} // wxTable::Open()
659
67e9aaa3 660
108106cf
JS
661/********** wxTable::Query() **********/
662bool wxTable::Query(bool forUpdate, bool distinct)
663{
664
89894079 665 return(query(DB_SELECT_WHERE, forUpdate, distinct));
108106cf
JS
666
667} // wxTable::Query()
668
67e9aaa3 669
108106cf
JS
670/********** wxTable::QueryBySqlStmt() **********/
671bool wxTable::QueryBySqlStmt(char *pSqlStmt)
672{
89894079 673 pDb->WriteSqlLog(pSqlStmt);
108106cf 674
89894079 675 return(query(DB_SELECT_STATEMENT, FALSE, FALSE, pSqlStmt));
108106cf
JS
676
677} // wxTable::QueryBySqlStmt()
678
67e9aaa3 679
108106cf
JS
680/********** wxTable::QueryMatching() **********/
681bool wxTable::QueryMatching(bool forUpdate, bool distinct)
682{
683
89894079 684 return(query(DB_SELECT_MATCHING, forUpdate, distinct));
108106cf
JS
685
686} // wxTable::QueryMatching()
687
67e9aaa3 688
108106cf
JS
689/********** wxTable::QueryOnKeyFields() **********/
690bool wxTable::QueryOnKeyFields(bool forUpdate, bool distinct)
691{
692
89894079 693 return(query(DB_SELECT_KEYFIELDS, forUpdate, distinct));
108106cf
JS
694
695} // wxTable::QueryOnKeyFields()
696
67e9aaa3 697
a3439c7d
GT
698/********** wxTable::GetPrev() **********/
699bool wxTable::GetPrev(void)
700{
89894079
VZ
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));
a3439c7d
GT
708} // wxTable::GetPrev()
709
67e9aaa3 710
a3439c7d
GT
711/********** wxTable::operator-- **********/
712bool wxTable::operator--(int)
713{
89894079
VZ
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));
a3439c7d
GT
721} // wxTable::operator--
722
67e9aaa3 723
a3439c7d
GT
724/********** wxTable::GetFirst() **********/
725bool wxTable::GetFirst(void)
726{
89894079
VZ
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));
a3439c7d
GT
734} // wxTable::GetFirst()
735
67e9aaa3 736
a3439c7d
GT
737/********** wxTable::GetLast() **********/
738bool wxTable::GetLast(void)
739{
89894079
VZ
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));
a3439c7d
GT
747} // wxTable::GetLast()
748
67e9aaa3 749
108106cf
JS
750/********** wxTable::GetSelectStmt() **********/
751void wxTable::GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
752{
89894079
VZ
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");
108106cf
JS
856
857} // wxTable::GetSelectStmt()
858
67e9aaa3 859
108106cf
JS
860/********** wxTable::GetRowNum() **********/
861UWORD wxTable::GetRowNum(void)
862{
89894079 863 UDWORD rowNum;
108106cf 864
89894079
VZ
865 if (SQLGetStmtOption(hstmt, SQL_ROW_NUMBER, (UCHAR*) &rowNum) != SQL_SUCCESS)
866 {
867 pDb->DispAllErrors(henv, hdbc, hstmt);
868 return(0);
869 }
108106cf 870
89894079
VZ
871 // Completed successfully
872 return((UWORD) rowNum);
108106cf
JS
873
874} // wxTable::GetRowNum()
875
67e9aaa3 876
108106cf
JS
877/********** wxTable::CloseCursor() **********/
878bool wxTable::CloseCursor(HSTMT cursor)
879{
89894079
VZ
880 if (SQLFreeStmt(cursor, SQL_CLOSE) != SQL_SUCCESS)
881 return(pDb->DispAllErrors(henv, hdbc, cursor));
108106cf 882
89894079
VZ
883 // Completed successfully
884 return(TRUE);
108106cf
JS
885
886} // wxTable::CloseCursor()
887
67e9aaa3 888
108106cf 889/********** wxTable::CreateTable() **********/
a2115c88 890bool wxTable::CreateTable(bool attemptDrop)
108106cf 891{
89894079
VZ
892 if (!pDb)
893 return FALSE;
1fc5dd6f 894
89894079 895 int i, j;
1e92909e
GT
896// char sqlStmt[DB_MAX_STATEMENT_LEN];
897 wxString sqlStmt;
108106cf 898
a2115c88 899#ifdef DBDEBUG_CONSOLE
89894079 900 cout << "Creating Table " << tableName << "..." << endl;
108106cf
JS
901#endif
902
89894079
VZ
903 // Drop table first
904 if (attemptDrop && !DropTable())
905 return FALSE;
108106cf 906
89894079 907 // Create the table
a2115c88 908#ifdef DBDEBUG_CONSOLE
89894079
VZ
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 }
108106cf
JS
932#endif
933
89894079
VZ
934 // Build a CREATE TABLE string from the colDefs structure.
935 bool needComma = FALSE;
1e92909e
GT
936 sqlStmt.sprintf("CREATE TABLE %s (", tableName);
937
89894079
VZ
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)
1e92909e 945 sqlStmt += ",";
89894079 946 // Column Name
1e92909e
GT
947 sqlStmt += colDefs[i].ColName;
948 sqlStmt += " ";
89894079
VZ
949 // Column Type
950 switch(colDefs[i].DbDataType)
951 {
952 case DB_DATA_TYPE_VARCHAR:
1e92909e 953 sqlStmt += pDb->typeInfVarchar.TypeName; break;
89894079 954 case DB_DATA_TYPE_INTEGER:
1e92909e 955 sqlStmt += pDb->typeInfInteger.TypeName; break;
89894079 956 case DB_DATA_TYPE_FLOAT:
1e92909e 957 sqlStmt += pDb->typeInfFloat.TypeName; break;
89894079 958 case DB_DATA_TYPE_DATE:
1e92909e 959 sqlStmt += pDb->typeInfDate.TypeName; break;
89894079
VZ
960 }
961 // For varchars, append the size of the string
962 if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)
963 {
1e92909e 964 wxString s;
89894079
VZ
965 // wxStrcat(sqlStmt, "(");
966 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
967 // wxStrcat(sqlStmt, ")");
1e92909e
GT
968 s.sprintf("(%d)", colDefs[i].SzDataObj);
969 sqlStmt += s.GetData();
89894079
VZ
970 }
971
972 if (pDb->Dbms() == dbmsSYBASE_ASE || pDb->Dbms() == dbmsMY_SQL)
973 {
974 if (colDefs[i].KeyField)
975 {
1e92909e 976 sqlStmt += " NOT NULL";
89894079
VZ
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 {
1e92909e
GT
995 sqlStmt += ",CONSTRAINT ";
996 sqlStmt += tableName;
997 sqlStmt += "_PIDX PRIMARY KEY (";
89894079
VZ
998 }
999 else
1000 {
1001 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
1e92909e 1002 sqlStmt += ", PRIMARY KEY (";
89894079
VZ
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
1e92909e
GT
1011 sqlStmt += ",";
1012 sqlStmt += colDefs[i].ColName;
89894079
VZ
1013 }
1014 }
1e92909e 1015 sqlStmt += ")";
89894079
VZ
1016 }
1017 // Append the closing parentheses for the create table statement
1e92909e 1018 sqlStmt += ")";
a2115c88 1019
1e92909e 1020 pDb->WriteSqlLog(sqlStmt.GetData());
1fc5dd6f 1021
a2115c88 1022#ifdef DBDEBUG_CONSOLE
1e92909e 1023 cout << endl << sqlStmt.GetData() << endl;
108106cf
JS
1024#endif
1025
89894079 1026 // Execute the CREATE TABLE statement
1e92909e 1027 RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS);
89894079
VZ
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);
108106cf
JS
1044
1045} // wxTable::CreateTable()
1046
67e9aaa3 1047
a2115c88
GT
1048/********** wxTable::DropTable() **********/
1049bool wxTable::DropTable()
1050{
89894079
VZ
1051 // NOTE: This function returns TRUE if the Table does not exist, but
1052 // only for identified databases. Code will need to be added
0b8410f3 1053 // below for any other databases when those databases are defined
89894079 1054 // to handle this situation consistently
a2115c88 1055
1e92909e 1056 wxString sqlStmt;
a2115c88 1057
1e92909e 1058 sqlStmt.sprintf("DROP TABLE %s", tableName);
a2115c88 1059
1e92909e 1060 pDb->WriteSqlLog(sqlStmt.GetData());
a2115c88
GT
1061
1062#ifdef DBDEBUG_CONSOLE
1e92909e 1063 cout << endl << sqlStmt.GetData() << endl;
a2115c88
GT
1064#endif
1065
1e92909e 1066 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
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
1e92909e
GT
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
89894079
VZ
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);
a2115c88
GT
1093} // wxTable::DropTable()
1094
67e9aaa3 1095
108106cf 1096/********** wxTable::CreateIndex() **********/
8128349e 1097bool wxTable::CreateIndex(const char * idxName, bool unique, int noIdxCols, wxIdxDef *pIdxDefs, bool attemptDrop)
108106cf 1098{
1e92909e
GT
1099// char sqlStmt[DB_MAX_STATEMENT_LEN];
1100 wxString sqlStmt;
89894079
VZ
1101
1102 // Drop the index first
1103 if (attemptDrop && !DropIndex(idxName))
1104 return (FALSE);
1105
1106 // Build a CREATE INDEX statement
1e92909e 1107 sqlStmt = "CREATE ";
89894079 1108 if (unique)
1e92909e 1109 sqlStmt += "UNIQUE ";
89894079 1110
1e92909e
GT
1111 sqlStmt += "INDEX ";
1112 sqlStmt += idxName;
1113 sqlStmt += " ON ";
1114 sqlStmt += tableName;
1115 sqlStmt += " (";
89894079
VZ
1116
1117 // Append list of columns making up index
1118 int i;
1119 for (i = 0; i < noIdxCols; i++)
1120 {
1e92909e
GT
1121 sqlStmt += pIdxDefs[i].ColName;
1122 /* Postgres doesn't cope with ASC */
89894079
VZ
1123 if (pDb->Dbms() != dbmsPOSTGRES)
1124 {
1125 if (pIdxDefs[i].Ascending)
1e92909e 1126 sqlStmt += " ASC";
89894079 1127 else
1e92909e 1128 sqlStmt += " DESC";
89894079
VZ
1129 }
1130
1131 if ((i + 1) < noIdxCols)
1e92909e 1132 sqlStmt += ",";
89894079
VZ
1133 }
1134
1135 // Append closing parentheses
1e92909e 1136 sqlStmt += ")";
89894079 1137
1e92909e 1138 pDb->WriteSqlLog(sqlStmt.GetData());
1fc5dd6f 1139
a2115c88 1140#ifdef DBDEBUG_CONSOLE
1e92909e 1141 cout << endl << sqlStmt.GetData() << endl << endl;
108106cf
JS
1142#endif
1143
89894079 1144 // Execute the CREATE INDEX statement
1e92909e 1145 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
1146 {
1147 pDb->DispAllErrors(henv, hdbc, hstmt);
1148 pDb->RollbackTrans();
1149 CloseCursor(hstmt);
1150 return(FALSE);
1151 }
108106cf 1152
89894079
VZ
1153 // Commit the transaction and close the cursor
1154 if (! pDb->CommitTrans())
1155 return(FALSE);
1156 if (! CloseCursor(hstmt))
1157 return(FALSE);
108106cf 1158
89894079
VZ
1159 // Index Created Successfully
1160 return(TRUE);
108106cf
JS
1161
1162} // wxTable::CreateIndex()
1163
67e9aaa3 1164
a2115c88 1165/********** wxTable::DropIndex() **********/
6919c53f 1166bool wxTable::DropIndex(const char * idxName)
a2115c88 1167{
89894079
VZ
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
a2115c88 1172
1e92909e 1173 wxString sqlStmt;
a2115c88 1174
89894079 1175 if (pDb->Dbms() == dbmsACCESS)
1e92909e 1176 sqlStmt.sprintf("DROP INDEX %s ON %s",idxName,tableName);
89894079 1177 else if (pDb->Dbms() == dbmsSYBASE_ASE)
1e92909e 1178 sqlStmt.sprintf("DROP INDEX %s.%s",tableName,idxName);
89894079 1179 else
1e92909e 1180 sqlStmt.sprintf("DROP INDEX %s",idxName);
a2115c88 1181
1e92909e 1182 pDb->WriteSqlLog(sqlStmt.GetData());
a2115c88
GT
1183
1184#ifdef DBDEBUG_CONSOLE
1e92909e 1185 cout << endl << sqlStmt.GetData() << endl;
a2115c88
GT
1186#endif
1187
1e92909e 1188 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
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
1e92909e
GT
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
89894079
VZ
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);
a2115c88
GT
1216} // wxTable::DropIndex()
1217
67e9aaa3 1218
108106cf
JS
1219/********** wxTable::Insert() **********/
1220int wxTable::Insert(void)
1221{
89894079
VZ
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);
108106cf
JS
1247
1248} // wxTable::Insert()
1249
67e9aaa3 1250
108106cf
JS
1251/********** wxTable::Update() **********/
1252bool wxTable::Update(void)
1253{
89894079
VZ
1254 assert(!queryOnly);
1255 if (queryOnly)
1256 return(FALSE);
a2115c88 1257
89894079 1258 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1259
89894079
VZ
1260 // Build the SQL UPDATE statement
1261 GetUpdateStmt(sqlStmt, DB_UPD_KEYFIELDS);
108106cf 1262
89894079 1263 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1264
a2115c88 1265#ifdef DBDEBUG_CONSOLE
0b8410f3 1266 cout << endl << sqlStmt << endl << endl;
108106cf
JS
1267#endif
1268
89894079
VZ
1269 // Execute the SQL UPDATE statement
1270 return(execUpdate(sqlStmt));
108106cf
JS
1271
1272} // wxTable::Update()
1273
67e9aaa3 1274
6919c53f
GT
1275/********** wxTable::Update(pSqlStmt) **********/
1276bool wxTable::Update(const char *pSqlStmt)
1277{
89894079
VZ
1278 assert(!queryOnly);
1279 if (queryOnly)
1280 return(FALSE);
6919c53f 1281
89894079 1282 pDb->WriteSqlLog(pSqlStmt);
6919c53f 1283
89894079 1284 return(execUpdate(pSqlStmt));
6919c53f
GT
1285
1286} // wxTable::Update(pSqlStmt)
1287
67e9aaa3 1288
108106cf 1289/********** wxTable::UpdateWhere() **********/
6919c53f 1290bool wxTable::UpdateWhere(const char *pWhereClause)
108106cf 1291{
89894079
VZ
1292 assert(!queryOnly);
1293 if (queryOnly)
1294 return(FALSE);
a2115c88 1295
89894079 1296 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1297
89894079
VZ
1298 // Build the SQL UPDATE statement
1299 GetUpdateStmt(sqlStmt, DB_UPD_WHERE, pWhereClause);
108106cf 1300
89894079 1301 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1302
a2115c88 1303#ifdef DBDEBUG_CONSOLE
0b8410f3 1304 cout << endl << sqlStmt << endl << endl;
108106cf
JS
1305#endif
1306
89894079
VZ
1307 // Execute the SQL UPDATE statement
1308 return(execUpdate(sqlStmt));
108106cf
JS
1309
1310} // wxTable::UpdateWhere()
1311
67e9aaa3 1312
108106cf
JS
1313/********** wxTable::Delete() **********/
1314bool wxTable::Delete(void)
1315{
89894079
VZ
1316 assert(!queryOnly);
1317 if (queryOnly)
1318 return(FALSE);
a2115c88 1319
89894079 1320 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1321
89894079
VZ
1322 // Build the SQL DELETE statement
1323 GetDeleteStmt(sqlStmt, DB_DEL_KEYFIELDS);
108106cf 1324
89894079 1325 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1326
89894079
VZ
1327 // Execute the SQL DELETE statement
1328 return(execDelete(sqlStmt));
108106cf
JS
1329
1330} // wxTable::Delete()
1331
67e9aaa3 1332
108106cf 1333/********** wxTable::DeleteWhere() **********/
6919c53f 1334bool wxTable::DeleteWhere(const char *pWhereClause)
108106cf 1335{
89894079
VZ
1336 assert(!queryOnly);
1337 if (queryOnly)
1338 return(FALSE);
a2115c88 1339
89894079 1340 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1341
89894079
VZ
1342 // Build the SQL DELETE statement
1343 GetDeleteStmt(sqlStmt, DB_DEL_WHERE, pWhereClause);
108106cf 1344
89894079 1345 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1346
89894079
VZ
1347 // Execute the SQL DELETE statement
1348 return(execDelete(sqlStmt));
108106cf
JS
1349
1350} // wxTable::DeleteWhere()
1351
67e9aaa3 1352
108106cf
JS
1353/********** wxTable::DeleteMatching() **********/
1354bool wxTable::DeleteMatching(void)
1355{
89894079
VZ
1356 assert(!queryOnly);
1357 if (queryOnly)
1358 return(FALSE);
a2115c88 1359
89894079 1360 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1361
89894079
VZ
1362 // Build the SQL DELETE statement
1363 GetDeleteStmt(sqlStmt, DB_DEL_MATCHING);
108106cf 1364
89894079 1365 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1366
89894079
VZ
1367 // Execute the SQL DELETE statement
1368 return(execDelete(sqlStmt));
108106cf
JS
1369
1370} // wxTable::DeleteMatching()
1371
67e9aaa3 1372
108106cf 1373/********** wxTable::GetUpdateStmt() **********/
6919c53f 1374void wxTable::GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause)
108106cf 1375{
89894079
VZ
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 }
108106cf
JS
1435} // GetUpdateStmt()
1436
67e9aaa3 1437
108106cf 1438/********** wxTable::GetDeleteStmt() **********/
6919c53f 1439void wxTable::GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause)
108106cf 1440{
89894079
VZ
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 }
108106cf
JS
1495
1496} // GetDeleteStmt()
1497
67e9aaa3 1498
108106cf 1499/********** wxTable::GetWhereClause() **********/
8128349e
GT
1500void wxTable::GetWhereClause(char *pWhereClause, int typeOfWhere,
1501 const char *qualTableName, bool useLikeComparison)
108106cf
JS
1502/*
1503 * Note: GetWhereClause() currently ignores timestamp columns.
1504 * They are not included as part of the where clause.
1505 */
108106cf 1506{
89894079
VZ
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);
8128349e
GT
1533 if (useLikeComparison && (colDefs[i].SqlCtype == SQL_C_CHAR))
1534 wxStrcat(pWhereClause, " LIKE ");
1535 else
1536 wxStrcat(pWhereClause, " = ");
89894079
VZ
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 }
108106cf
JS
1564} // wxTable::GetWhereClause()
1565
67e9aaa3 1566
108106cf
JS
1567/********** wxTable::IsColNull() **********/
1568bool wxTable::IsColNull(int colNo)
1569{
89894079
VZ
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 }
108106cf
JS
1596} // wxTable::IsColNull()
1597
67e9aaa3 1598
108106cf 1599/********** wxTable::CanSelectForUpdate() **********/
108106cf
JS
1600bool wxTable::CanSelectForUpdate(void)
1601{
89894079
VZ
1602 if (pDb->Dbms() == dbmsMY_SQL)
1603 return FALSE;
a2115c88 1604
89894079
VZ
1605 if (pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
1606 return(TRUE);
1607 else
1608 return(FALSE);
108106cf
JS
1609
1610} // wxTable::CanSelectForUpdate()
1611
67e9aaa3 1612
108106cf
JS
1613/********** wxTable::CanUpdByROWID() **********/
1614bool wxTable::CanUpdByROWID(void)
1615{
67e9aaa3
GT
1616/*
1617 * NOTE: Returning FALSE for now until this can be debugged,
89894079 1618 * as the ROWID is not getting updated correctly
67e9aaa3 1619 */
89894079 1620 return FALSE;
108106cf 1621
89894079
VZ
1622 if (pDb->Dbms() == dbmsORACLE)
1623 return(TRUE);
1624 else
1625 return(FALSE);
108106cf
JS
1626
1627} // wxTable::CanUpdByROWID()
1628
67e9aaa3 1629
108106cf
JS
1630/********** wxTable::IsCursorClosedOnCommit() **********/
1631bool wxTable::IsCursorClosedOnCommit(void)
1632{
89894079
VZ
1633 if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
1634 return(FALSE);
1635 else
1636 return(TRUE);
108106cf
JS
1637
1638} // wxTable::IsCursorClosedOnCommit()
1639
67e9aaa3 1640
108106cf
JS
1641/********** wxTable::ClearMemberVars() **********/
1642void wxTable::ClearMemberVars(void)
1643{
89894079
VZ
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;
7c5c05ae 1682
89894079
VZ
1683 }
1684 }
108106cf
JS
1685
1686} // wxTable::ClearMemberVars()
1687
67e9aaa3 1688
108106cf
JS
1689/********** wxTable::SetQueryTimeout() **********/
1690bool wxTable::SetQueryTimeout(UDWORD nSeconds)
1691{
89894079
VZ
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);
108106cf
JS
1703
1704} // wxTable::SetQueryTimeout()
1705
67e9aaa3 1706
108106cf 1707/********** wxTable::SetColDefs() **********/
6919c53f 1708void wxTable::SetColDefs (int index, const char *fieldName, int dataType, void *pData,
89894079
VZ
1709 int cType, int size, bool keyField, bool upd,
1710 bool insAllow, bool derivedCol)
108106cf 1711{
89894079
VZ
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
108106cf
JS
1743} // wxTable::SetColDefs()
1744
67e9aaa3
GT
1745
1746/********** wxTable::SetColDef() **********/
1b7274a8 1747wxColDataPtr* wxTable::SetColDefs (wxColInf *pColInfs, ULONG numCols)
67e9aaa3 1748{
89894079
VZ
1749 assert(pColInfs);
1750 wxColDataPtr *pColDataPtrs = NULL;
67e9aaa3 1751
89894079
VZ
1752 if (pColInfs)
1753 {
1754 ULONG index;
67e9aaa3 1755
1b7274a8 1756
89894079 1757 pColDataPtrs = new wxColDataPtr[numCols+1];
67e9aaa3
GT
1758
1759 for (index = 0; index < numCols; index++)
89894079 1760 {
89894079
VZ
1761 // Process the fields
1762 switch (pColInfs[index].dbDataType)
1763 {
1764 case DB_DATA_TYPE_VARCHAR:
1765 {
0b8410f3
GT
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;
89894079
VZ
1770 }
1771 case DB_DATA_TYPE_INTEGER:
1772 {
67e9aaa3 1773 // Can be long or short
89894079
VZ
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 }
67e9aaa3
GT
1813
1814 SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
89894079
VZ
1815 }
1816 }
1817 return (pColDataPtrs);
67e9aaa3
GT
1818} // wxTable::SetColDef()
1819
1820
108106cf 1821/********** wxTable::SetCursor() **********/
a2115c88 1822void wxTable::SetCursor(HSTMT *hstmtActivate)
108106cf 1823{
89894079
VZ
1824 if (hstmtActivate == DEFAULT_CURSOR)
1825 hstmt = *hstmtDefault;
1826 else
1827 hstmt = *hstmtActivate;
108106cf
JS
1828
1829} // wxTable::SetCursor()
1830
67e9aaa3 1831
285c163f
GT
1832/********** wxTable::Count(const char *) **********/
1833ULONG wxTable::Count(const char *args)
108106cf 1834{
89894079 1835 ULONG l;
1e92909e 1836 wxString sqlStmt;
89894079
VZ
1837 SDWORD cb;
1838
1839 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1e92909e
GT
1840 sqlStmt = "SELECT COUNT(";
1841 sqlStmt += args;
1842 sqlStmt += ") FROM ";
1843 sqlStmt += queryTableName;
89894079
VZ
1844
1845 if (from && wxStrlen(from))
1e92909e 1846 sqlStmt += from;
89894079
VZ
1847
1848 // Add the where clause if one is provided
1849 if (where && wxStrlen(where))
1850 {
1e92909e
GT
1851 sqlStmt += " WHERE ";
1852 sqlStmt += where;
89894079
VZ
1853 }
1854
1e92909e 1855 pDb->WriteSqlLog(sqlStmt.GetData());
89894079
VZ
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
1e92909e 1867 if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt.GetData(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
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);
108106cf
JS
1893
1894} // wxTable::Count()
1895
67e9aaa3 1896
108106cf
JS
1897/********** wxTable::Refresh() **********/
1898bool wxTable::Refresh(void)
1899{
89894079
VZ
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
1e92909e 1952 where = saveWhere;
89894079
VZ
1953 orderBy = saveOrderBy;
1954
1955 return(result);
108106cf
JS
1956
1957} // wxTable::Refresh()
1958
67e9aaa3
GT
1959
1960/********** wxTable::SetNull(int colNo) **********/
a2115c88
GT
1961bool wxTable::SetNull(int colNo)
1962{
89894079
VZ
1963 if (colNo < noCols)
1964 return(colDefs[colNo].Null = TRUE);
1965 else
1966 return(FALSE);
a2115c88 1967
67e9aaa3
GT
1968} // wxTable::SetNull(int colNo)
1969
a2115c88
GT
1970
1971/********** wxTable::SetNull(char *colName) **********/
6919c53f 1972bool wxTable::SetNull(const char *colName)
a2115c88 1973{
89894079
VZ
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);
a2115c88
GT
1985
1986} // wxTable::SetNull(char *colName)
1987
67e9aaa3 1988
a2115c88
GT
1989/********** wxTable::NewCursor() **********/
1990HSTMT *wxTable::NewCursor(bool setCursor, bool bindColumns)
1991{
89894079
VZ
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()
67e9aaa3 2026
a2115c88
GT
2027
2028/********** wxTable::DeleteCursor() **********/
2029bool wxTable::DeleteCursor(HSTMT *hstmtDel)
2030{
89894079 2031 bool result = TRUE;
a2115c88 2032
89894079
VZ
2033 if (!hstmtDel) // Cursor already deleted
2034 return(result);
a2115c88 2035
89894079
VZ
2036 if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2037 {
2038 pDb->DispAllErrors(henv, hdbc);
2039 result = FALSE;
2040 }
a2115c88 2041
89894079 2042 delete hstmtDel;
a2115c88 2043
89894079 2044 return(result);
a2115c88
GT
2045
2046} // wxTable::DeleteCursor()
2047
2048#endif // wxUSE_ODBC
1fc5dd6f 2049