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