]> git.saurik.com Git - wxWidgets.git/blame - src/common/dbtable.cpp
Some Unicode compilation fixes. This Unicode stuff in file functions is
[wxWidgets.git] / src / common / dbtable.cpp
CommitLineData
108106cf 1///////////////////////////////////////////////////////////////////////////////
1fc5dd6f 2// Name: dbtable.cpp
f6bcfd97 3// Purpose: Implementation of the wxDbTable class.
108106cf 4// Author: Doug Card
67e9aaa3 5// Modified by: George Tasker
3ca6a5f0
BP
6// Bart Jourquin
7// Mark Johnson
108106cf
JS
8// Created: 9.96
9// RCS-ID: $Id$
10// Copyright: (c) 1996 Remstar International, Inc.
11// Licence: wxWindows licence, plus:
1e92909e 12// Notice: This class library and its intellectual design are free of charge for use,
108106cf
JS
13// modification, enhancement, debugging under the following conditions:
14// 1) These classes may only be used as part of the implementation of a
15// wxWindows-based application
16// 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17// user groups free of all charges for use with the wxWindows library.
18// 3) These classes may not be distributed as part of any other class library,
19// DLL, text (written or electronic), other than a complete distribution of
20// the wxWindows GUI development toolkit.
21///////////////////////////////////////////////////////////////////////////////
22
23/*
24// SYNOPSIS START
25// SYNOPSIS STOP
26*/
27
a2115c88
GT
28// Use this line for wxWindows v1.x
29//#include "wx_ver.h"
30// Use this line for wxWindows v2.x
a2115c88 31#include "wx/wxprec.h"
f6bcfd97 32#include "wx/version.h"
a2115c88
GT
33
34#if wxMAJOR_VERSION == 2
3ca6a5f0
BP
35 #ifdef __GNUG__
36 #pragma implementation "dbtable.h"
37 #endif
108106cf 38#endif
108106cf 39
0b8410f3
GT
40#ifdef DBDEBUG_CONSOLE
41 #include "wx/ioswrap.h"
42#endif
108106cf 43
f6bcfd97 44
108106cf 45#ifdef __BORLANDC__
1e92909e 46 #pragma hdrstop
108106cf
JS
47#endif //__BORLANDC__
48
a2115c88 49#if wxMAJOR_VERSION == 2
1e92909e
GT
50 #ifndef WX_PRECOMP
51 #include "wx/string.h"
52 #include "wx/object.h"
53 #include "wx/list.h"
54 #include "wx/utils.h"
55 #include "wx/msgdlg.h"
0efe4a98 56 #include "wx/log.h"
89894079
VZ
57 #endif
58 #include "wx/filefn.h"
a2115c88 59#endif
108106cf 60
a2115c88 61#if wxMAJOR_VERSION == 1
89894079
VZ
62# if defined(wx_msw) || defined(wx_x)
63# ifdef WX_PRECOMP
64# include "wx_prec.h"
65# else
66# include "wx.h"
67# endif
68# endif
69# define wxUSE_ODBC 1
a2115c88 70#endif
108106cf 71
f6bcfd97 72
a2115c88 73#if wxUSE_ODBC
108106cf
JS
74
75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
4fdae997 78//#include <assert.h>
67e9aaa3 79
a2115c88 80#if wxMAJOR_VERSION == 1
89894079 81 #include "table.h"
a2115c88 82#elif wxMAJOR_VERSION == 2
89894079 83 #include "wx/dbtable.h"
a2115c88 84#endif
108106cf 85
1fc5dd6f 86#ifdef __UNIX__
108106cf
JS
87// The HPUX preprocessor lines below were commented out on 8/20/97
88// because macros.h currently redefines DEBUG and is unneeded.
89// # ifdef HPUX
90// # include <macros.h>
91// # endif
1fc5dd6f 92# ifdef LINUX
108106cf
JS
93# include <sys/minmax.h>
94# endif
95#endif
96
a2115c88
GT
97ULONG lastTableID = 0;
98
99
e041ce57 100#ifdef __WXDEBUG__
89894079 101 wxList TablesInUse;
a2115c88
GT
102#endif
103
104
da99271d
GT
105/********** wxDbColDef::wxDbColDef() Constructor **********/
106wxDbColDef::wxDbColDef()
107{
108 Initialize();
109} // Constructor
110
111
112bool wxDbColDef::Initialize()
113{
114 ColName[0] = 0;
115 DbDataType = DB_DATA_TYPE_INTEGER;
116 SqlCtype = SQL_C_LONG;
117 PtrDataObj = NULL;
118 SzDataObj = 0;
119 KeyField = FALSE;
120 Updateable = FALSE;
121 InsertAllowed = FALSE;
122 DerivedCol = FALSE;
123 CbValue = 0;
124 Null = FALSE;
125
126 return TRUE;
127} // wxDbColDef::Initialize()
128
129
130/********** wxDbTable::wxDbTable() Constructor **********/
4fdae997
GT
131wxDbTable::wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
132 const wxString &qryTblName, bool qryOnly, const wxString &tblPath)
108106cf 133{
4fdae997
GT
134 if (!initialize(pwxDb, tblName, nCols, qryTblName, qryOnly, tblPath))
135 cleanup();
136} // wxDbTable::wxDbTable()
137
138
139/***** DEPRECATED: use wxDbTable::wxDbTable() format above *****/
140wxDbTable::wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
141 const wxChar *qryTblName, bool qryOnly, const wxString &tblPath)
142{
143 wxString tempQryTblName;
144 tempQryTblName = qryTblName;
145 if (!initialize(pwxDb, tblName, nCols, tempQryTblName, qryOnly, tblPath))
146 cleanup();
147} // wxDbTable::wxDbTable()
148
149
150/********** wxDbTable::~wxDbTable() **********/
151wxDbTable::~wxDbTable()
152{
153 this->cleanup();
154} // wxDbTable::~wxDbTable()
155
156
157bool wxDbTable::initialize(wxDb *pwxDb, const wxString &tblName, const int nCols,
158 const wxString &qryTblName, bool qryOnly, const wxString &tblPath)
159{
160 // Initializing member variables
f6bcfd97 161 pDb = pwxDb; // Pointer to the wxDb object
89894079
VZ
162 henv = 0;
163 hdbc = 0;
164 hstmt = 0;
165 hstmtDefault = 0; // Initialized below
166 hstmtCount = 0; // Initialized first time it is needed
167 hstmtInsert = 0;
168 hstmtDelete = 0;
169 hstmtUpdate = 0;
170 hstmtInternal = 0;
171 colDefs = 0;
172 tableID = 0;
173 noCols = nCols; // No. of cols in the table
4fdae997
GT
174 where.Empty(); // Where clause
175 orderBy.Empty(); // Order By clause
176 from.Empty(); // From clause
89894079
VZ
177 selectForUpdate = FALSE; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
178 queryOnly = qryOnly;
f6bcfd97 179 insertable = TRUE;
4fdae997
GT
180 tablePath.Empty();
181 tableName.Empty();
182 queryTableName.Empty();
89894079 183
4fdae997
GT
184 wxASSERT(tblName.Length());
185 wxASSERT(pDb);
89894079 186
4fdae997
GT
187 if (!pDb)
188 return FALSE;
189
190 tableName = tblName; // Table Name
191 if (tblPath.Length())
192 tablePath = tblPath; // Table Path - used for dBase files
f02d4a64 193 else
4fdae997 194 tablePath.Empty();
3ca6a5f0 195
4fdae997
GT
196 if (qryTblName.Length()) // Name of the table/view to query
197 queryTableName = qryTblName;
89894079 198 else
4fdae997 199 queryTableName = tblName;
3ca6a5f0 200
f6bcfd97 201 pDb->incrementTableCount();
3ca6a5f0 202
1e92909e 203 wxString s;
89894079 204 tableID = ++lastTableID;
7d8c3dba 205 s.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName.c_str(), tableID, pDb);
3ca6a5f0 206
e041ce57 207#ifdef __WXDEBUG__
8128349e
GT
208 wxTablesInUse *tableInUse;
209 tableInUse = new wxTablesInUse();
89894079 210 tableInUse->tableName = tblName;
1e92909e
GT
211 tableInUse->tableID = tableID;
212 tableInUse->pDb = pDb;
89894079 213 TablesInUse.Append(tableInUse);
a2115c88 214#endif
3ca6a5f0 215
4fdae997 216 pDb->WriteSqlLog(s);
3ca6a5f0 217
f6bcfd97
BP
218 // Grab the HENV and HDBC from the wxDb object
219 henv = pDb->GetHENV();
220 hdbc = pDb->GetHDBC();
3ca6a5f0 221
89894079
VZ
222 // Allocate space for column definitions
223 if (noCols)
4fdae997 224 colDefs = new wxDbColDef[noCols]; // Points to the first column definition
3ca6a5f0 225
89894079
VZ
226 // Allocate statement handles for the table
227 if (!queryOnly)
228 {
229 // Allocate a separate statement handle for performing inserts
230 if (SQLAllocStmt(hdbc, &hstmtInsert) != SQL_SUCCESS)
231 pDb->DispAllErrors(henv, hdbc);
232 // Allocate a separate statement handle for performing deletes
233 if (SQLAllocStmt(hdbc, &hstmtDelete) != SQL_SUCCESS)
234 pDb->DispAllErrors(henv, hdbc);
235 // Allocate a separate statement handle for performing updates
236 if (SQLAllocStmt(hdbc, &hstmtUpdate) != SQL_SUCCESS)
237 pDb->DispAllErrors(henv, hdbc);
238 }
239 // Allocate a separate statement handle for internal use
240 if (SQLAllocStmt(hdbc, &hstmtInternal) != SQL_SUCCESS)
241 pDb->DispAllErrors(henv, hdbc);
3ca6a5f0 242
89894079
VZ
243 // Set the cursor type for the statement handles
244 cursorType = SQL_CURSOR_STATIC;
3ca6a5f0 245
89894079 246 if (SQLSetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
3ca6a5f0 247 {
89894079
VZ
248 // Check to see if cursor type is supported
249 pDb->GetNextError(henv, hdbc, hstmtInternal);
4fdae997 250 if (! wxStrcmp(pDb->sqlState, wxT("01S02"))) // Option Value Changed
3ca6a5f0 251 {
89894079
VZ
252 // Datasource does not support static cursors. Driver
253 // will substitute a cursor type. Call SQLGetStmtOption()
254 // to determine which cursor type was selected.
255 if (SQLGetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, &cursorType) != SQL_SUCCESS)
3ca6a5f0 256 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
a2115c88 257#ifdef DBDEBUG_CONSOLE
4fdae997 258 cout << wxT("Static cursor changed to: ");
89894079 259 switch(cursorType)
3ca6a5f0
BP
260 {
261 case SQL_CURSOR_FORWARD_ONLY:
4fdae997 262 cout << wxT("Forward Only");
3ca6a5f0
BP
263 break;
264 case SQL_CURSOR_STATIC:
4fdae997 265 cout << wxT("Static");
3ca6a5f0
BP
266 break;
267 case SQL_CURSOR_KEYSET_DRIVEN:
4fdae997 268 cout << wxT("Keyset Driven");
3ca6a5f0
BP
269 break;
270 case SQL_CURSOR_DYNAMIC:
4fdae997 271 cout << wxT("Dynamic");
3ca6a5f0
BP
272 break;
273 }
89894079 274 cout << endl << endl;
108106cf 275#endif
3ca6a5f0
BP
276 // BJO20000425
277 if (pDb->FwdOnlyCursors() && cursorType != SQL_CURSOR_FORWARD_ONLY)
278 {
279 // Force the use of a forward only cursor...
280 cursorType = SQL_CURSOR_FORWARD_ONLY;
281 if (SQLSetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
282 {
283 // Should never happen
284 pDb->GetNextError(henv, hdbc, hstmtInternal);
4fdae997 285 return FALSE;
3ca6a5f0
BP
286 }
287 }
288 }
289 else
290 {
89894079
VZ
291 pDb->DispNextError();
292 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
3ca6a5f0 293 }
89894079 294 }
a2115c88 295#ifdef DBDEBUG_CONSOLE
89894079 296 else
4fdae997 297 cout << wxT("Cursor Type set to STATIC") << endl << endl;
108106cf 298#endif
3ca6a5f0 299
89894079
VZ
300 if (!queryOnly)
301 {
302 // Set the cursor type for the INSERT statement handle
303 if (SQLSetStmtOption(hstmtInsert, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
304 pDb->DispAllErrors(henv, hdbc, hstmtInsert);
305 // Set the cursor type for the DELETE statement handle
306 if (SQLSetStmtOption(hstmtDelete, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
307 pDb->DispAllErrors(henv, hdbc, hstmtDelete);
308 // Set the cursor type for the UPDATE statement handle
309 if (SQLSetStmtOption(hstmtUpdate, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
310 pDb->DispAllErrors(henv, hdbc, hstmtUpdate);
311 }
3ca6a5f0 312
89894079 313 // Make the default cursor the active cursor
f6bcfd97 314 hstmtDefault = GetNewCursor(FALSE,FALSE);
4fdae997 315 wxASSERT(hstmtDefault);
89894079 316 hstmt = *hstmtDefault;
108106cf 317
4fdae997 318 return TRUE;
67e9aaa3 319
4fdae997
GT
320} // wxDbTable::initialize()
321
322
323void wxDbTable::cleanup()
108106cf 324{
1e92909e 325 wxString s;
89894079
VZ
326 if (pDb)
327 {
7d8c3dba 328 s.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName.c_str(), tableID, pDb);
4fdae997 329 pDb->WriteSqlLog(s);
89894079 330 }
a2115c88 331
e041ce57 332#ifdef __WXDEBUG__
89894079
VZ
333 if (tableID)
334 {
a4086039 335 TablesInUse.DeleteContents(TRUE);
89894079 336 bool found = FALSE;
1e92909e 337
0b8410f3 338 wxNode *pNode;
89894079
VZ
339 pNode = TablesInUse.First();
340 while (pNode && !found)
341 {
8128349e 342 if (((wxTablesInUse *)pNode->Data())->tableID == tableID)
89894079
VZ
343 {
344 found = TRUE;
345 if (!TablesInUse.DeleteNode(pNode))
4fdae997 346 wxLogDebug (s,wxT("Unable to delete node!"));
89894079
VZ
347 }
348 else
349 pNode = pNode->Next();
350 }
351 if (!found)
352 {
1e92909e 353 wxString msg;
4fdae997
GT
354 msg.Printf(wxT("Unable to find the tableID in the linked\nlist of tables in use.\n\n%s"),s);
355 wxLogDebug (msg,wxT("NOTICE..."));
89894079
VZ
356 }
357 }
a2115c88 358#endif
e041ce57 359
f6bcfd97 360 // Decrement the wxDb table count
89894079 361 if (pDb)
f6bcfd97 362 pDb->decrementTableCount();
89894079
VZ
363
364 // Delete memory allocated for column definitions
365 if (colDefs)
366 delete [] colDefs;
367
368 // Free statement handles
369 if (!queryOnly)
370 {
371 if (hstmtInsert)
7d8c3dba
GT
372 {
373/*
374ODBC 3.0 says to use this form
375 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
376*/
89894079
VZ
377 if (SQLFreeStmt(hstmtInsert, SQL_DROP) != SQL_SUCCESS)
378 pDb->DispAllErrors(henv, hdbc);
7d8c3dba 379 }
7c5c05ae 380
89894079 381 if (hstmtDelete)
7d8c3dba
GT
382 {
383/*
384ODBC 3.0 says to use this form
385 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
386*/
89894079 387 if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
7d8c3dba
GT
388 pDb->DispAllErrors(henv, hdbc);
389 }
7c5c05ae 390
89894079 391 if (hstmtUpdate)
7d8c3dba
GT
392 {
393/*
394ODBC 3.0 says to use this form
395 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
396*/
89894079
VZ
397 if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
398 pDb->DispAllErrors(henv, hdbc);
7d8c3dba 399 }
89894079 400 }
3ca6a5f0 401
89894079 402 if (hstmtInternal)
7d8c3dba 403 {
89894079
VZ
404 if (SQLFreeStmt(hstmtInternal, SQL_DROP) != SQL_SUCCESS)
405 pDb->DispAllErrors(henv, hdbc);
7d8c3dba 406 }
89894079
VZ
407
408 // Delete dynamically allocated cursors
409 if (hstmtDefault)
410 DeleteCursor(hstmtDefault);
7c5c05ae 411
89894079
VZ
412 if (hstmtCount)
413 DeleteCursor(hstmtCount);
4fdae997 414} // wxDbTable::cleanup()
67e9aaa3
GT
415
416
6919c53f
GT
417/***************************** PRIVATE FUNCTIONS *****************************/
418
67e9aaa3 419
4fdae997
GT
420/********** wxDbTable::bindUpdateParams() **********/
421bool wxDbTable::bindParams(bool forUpdate)
6919c53f 422{
4fdae997 423 wxASSERT(!queryOnly);
89894079
VZ
424 if (queryOnly)
425 return(FALSE);
4fdae997 426
89894079
VZ
427 SWORD fSqlType = 0;
428 UDWORD precision = 0;
429 SWORD scale = 0;
4fdae997
GT
430
431 // Bind each column of the table that should be bound
432 // to a parameter marker
89894079
VZ
433 int i,colNo;
434 for (i = 0, colNo = 1; i < noCols; i++)
435 {
4fdae997 436 if (forUpdate)
89894079 437 {
4fdae997
GT
438 if (! colDefs[i].Updateable)
439 continue;
89894079 440 }
4fdae997 441 else
3ca6a5f0 442 {
4fdae997
GT
443 if (! colDefs[i].InsertAllowed)
444 continue;
3ca6a5f0 445 }
6919c53f 446
89894079
VZ
447 switch(colDefs[i].DbDataType)
448 {
3ca6a5f0
BP
449 case DB_DATA_TYPE_VARCHAR:
450 fSqlType = pDb->GetTypeInfVarchar().FsqlType;
451 precision = colDefs[i].SzDataObj;
452 scale = 0;
f02d4a64
GT
453 if (colDefs[i].Null)
454 colDefs[i].CbValue = SQL_NULL_DATA;
455 else
456 colDefs[i].CbValue = SQL_NTS;
3ca6a5f0
BP
457 break;
458 case DB_DATA_TYPE_INTEGER:
459 fSqlType = pDb->GetTypeInfInteger().FsqlType;
460 precision = pDb->GetTypeInfInteger().Precision;
461 scale = 0;
f02d4a64
GT
462 if (colDefs[i].Null)
463 colDefs[i].CbValue = SQL_NULL_DATA;
464 else
465 colDefs[i].CbValue = 0;
3ca6a5f0
BP
466 break;
467 case DB_DATA_TYPE_FLOAT:
468 fSqlType = pDb->GetTypeInfFloat().FsqlType;
469 precision = pDb->GetTypeInfFloat().Precision;
470 scale = pDb->GetTypeInfFloat().MaximumScale;
471 // SQL Sybase Anywhere v5.5 returned a negative number for the
472 // MaxScale. This caused ODBC to kick out an error on ibscale.
473 // I check for this here and set the scale = precision.
474 //if (scale < 0)
475 // scale = (short) precision;
f02d4a64
GT
476 if (colDefs[i].Null)
477 colDefs[i].CbValue = SQL_NULL_DATA;
478 else
479 colDefs[i].CbValue = 0;
3ca6a5f0
BP
480 break;
481 case DB_DATA_TYPE_DATE:
482 fSqlType = pDb->GetTypeInfDate().FsqlType;
483 precision = pDb->GetTypeInfDate().Precision;
484 scale = 0;
f02d4a64
GT
485 if (colDefs[i].Null)
486 colDefs[i].CbValue = SQL_NULL_DATA;
487 else
488 colDefs[i].CbValue = 0;
3ca6a5f0 489 break;
bf5423ea
GT
490 case DB_DATA_TYPE_BLOB:
491 fSqlType = pDb->GetTypeInfBlob().FsqlType;
492 precision = 50000;
493 scale = 0;
494 if (colDefs[i].Null)
495 colDefs[i].CbValue = SQL_NULL_DATA;
496 else
497 colDefs[i].CbValue = SQL_LEN_DATA_AT_EXEC(colDefs[i].SzDataObj);
498 break;
3ca6a5f0 499 }
4fdae997
GT
500 if (forUpdate)
501 {
502 if (SQLBindParameter(hstmtUpdate, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
503 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
504 precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
505 {
506 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
507 }
508 }
509 else
3ca6a5f0 510 {
4fdae997
GT
511 if (SQLBindParameter(hstmtInsert, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
512 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
513 precision+1,&colDefs[i].CbValue) != SQL_SUCCESS)
514 {
515 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
516 }
89894079 517 }
89894079 518 }
3ca6a5f0 519
89894079
VZ
520 // Completed successfully
521 return(TRUE);
6919c53f 522
4fdae997
GT
523} // wxDbTable::bindParams()
524
525
526/********** wxDbTable::bindInsertParams() **********/
527bool wxDbTable::bindInsertParams(void)
528{
529 return bindParams(FALSE);
530} // wxDbTable::bindInsertParams()
531
532
533/********** wxDbTable::bindUpdateParams() **********/
534bool wxDbTable::bindUpdateParams(void)
535{
536 return bindParams(TRUE);
f6bcfd97 537} // wxDbTable::bindUpdateParams()
6919c53f 538
67e9aaa3 539
f6bcfd97
BP
540/********** wxDbTable::bindCols() **********/
541bool wxDbTable::bindCols(HSTMT cursor)
6919c53f 542{
f02d4a64 543//RG-NULL static SDWORD cb;
89894079
VZ
544
545 // Bind each column of the table to a memory address for fetching data
546 int i;
547 for (i = 0; i < noCols; i++)
3ca6a5f0
BP
548 {
549 if (SQLBindCol(cursor, i+1, colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
f02d4a64
GT
550//RG-NULL colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
551 colDefs[i].SzDataObj, &colDefs[i].CbValue ) != SQL_SUCCESS)
3ca6a5f0
BP
552 {
553 return (pDb->DispAllErrors(henv, hdbc, cursor));
554 }
555 }
89894079
VZ
556
557 // Completed successfully
558 return(TRUE);
6919c53f 559
f6bcfd97 560} // wxDbTable::bindCols()
6919c53f 561
67e9aaa3 562
f6bcfd97
BP
563/********** wxDbTable::getRec() **********/
564bool wxDbTable::getRec(UWORD fetchType)
6919c53f 565{
89894079
VZ
566 RETCODE retcode;
567
568 if (!pDb->FwdOnlyCursors())
569 {
570 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
571 UDWORD cRowsFetched;
572 UWORD rowStatus;
573
574 retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
575 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
3ca6a5f0 576 {
89894079
VZ
577 if (retcode == SQL_NO_DATA_FOUND)
578 return(FALSE);
579 else
580 return(pDb->DispAllErrors(henv, hdbc, hstmt));
3ca6a5f0 581 }
f02d4a64
GT
582 else
583 {
584 // Set the Null member variable to indicate the Null state
585 // of each column just read in.
586 int i;
587 for (i = 0; i < noCols; i++)
588 colDefs[i].Null = (colDefs[i].CbValue == SQL_NULL_DATA);
589 }
89894079
VZ
590 }
591 else
592 {
593 // Fetch the next record from the record set
594 retcode = SQLFetch(hstmt);
595 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
596 {
597 if (retcode == SQL_NO_DATA_FOUND)
598 return(FALSE);
599 else
600 return(pDb->DispAllErrors(henv, hdbc, hstmt));
601 }
f02d4a64
GT
602 else
603 {
604 // Set the Null member variable to indicate the Null state
605 // of each column just read in.
606 int i;
607 for (i = 0; i < noCols; i++)
608 colDefs[i].Null = (colDefs[i].CbValue == SQL_NULL_DATA);
609 }
89894079
VZ
610 }
611
612 // Completed successfully
613 return(TRUE);
6919c53f 614
f6bcfd97 615} // wxDbTable::getRec()
6919c53f 616
67e9aaa3 617
f6bcfd97 618/********** wxDbTable::execDelete() **********/
4fdae997 619bool wxDbTable::execDelete(const wxString &pSqlStmt)
6919c53f 620{
89894079 621 // Execute the DELETE statement
4fdae997 622 if (SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079 623 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
6919c53f 624
89894079
VZ
625 // Record deleted successfully
626 return(TRUE);
6919c53f 627
f6bcfd97 628} // wxDbTable::execDelete()
6919c53f 629
67e9aaa3 630
f6bcfd97 631/********** wxDbTable::execUpdate() **********/
4fdae997 632bool wxDbTable::execUpdate(const wxString &pSqlStmt)
6919c53f 633{
89894079 634 // Execute the UPDATE statement
4fdae997 635 if (SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079 636 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
6919c53f 637
89894079
VZ
638 // Record deleted successfully
639 return(TRUE);
6919c53f 640
f6bcfd97 641} // wxDbTable::execUpdate()
6919c53f 642
67e9aaa3 643
f6bcfd97 644/********** wxDbTable::query() **********/
4fdae997 645bool wxDbTable::query(int queryType, bool forUpdate, bool distinct, const wxString &pSqlStmt)
6919c53f 646{
4fdae997 647 wxString sqlStmt;
6919c53f 648
89894079
VZ
649 if (forUpdate)
650 // The user may wish to select for update, but the DBMS may not be capable
651 selectForUpdate = CanSelectForUpdate();
652 else
653 selectForUpdate = FALSE;
6919c53f 654
89894079
VZ
655 // Set the SQL SELECT string
656 if (queryType != DB_SELECT_STATEMENT) // A select statement was not passed in,
3ca6a5f0 657 { // so generate a select statement.
f6bcfd97 658 BuildSelectStmt(sqlStmt, queryType, distinct);
89894079 659 pDb->WriteSqlLog(sqlStmt);
3ca6a5f0 660 }
e93a3a18
GT
661/*
662 This is the block of code that got added during the 2.2.1 merge with
663 the 2.2 main branch that somehow got added here when it should not have. - gt
664
3ca6a5f0
BP
665 else
666 wxStrcpy(sqlStmt, pSqlStmt);
f6bcfd97
BP
667
668 SQLFreeStmt(hstmt, SQL_CLOSE);
669 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) == SQL_SUCCESS)
3ca6a5f0 670 return(TRUE);
f6bcfd97 671 else
3ca6a5f0 672 {
f6bcfd97
BP
673 pDb->DispAllErrors(henv, hdbc, hstmt);
674 return(FALSE);
3ca6a5f0 675 }
e93a3a18 676*/
89894079 677 // Make sure the cursor is closed first
e93a3a18 678 if (!CloseCursor(hstmt))
3ca6a5f0 679 return(FALSE);
6919c53f 680
89894079 681 // Execute the SQL SELECT statement
f6bcfd97 682 int retcode;
4fdae997 683 retcode = SQLExecDirect(hstmt, (UCHAR FAR *) (queryType == DB_SELECT_STATEMENT ? pSqlStmt.c_str() : sqlStmt.c_str()), SQL_NTS);
89894079 684 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
3ca6a5f0 685 return(pDb->DispAllErrors(henv, hdbc, hstmt));
6919c53f 686
89894079
VZ
687 // Completed successfully
688 return(TRUE);
6919c53f 689
f6bcfd97 690} // wxDbTable::query()
6919c53f
GT
691
692
67e9aaa3
GT
693/***************************** PUBLIC FUNCTIONS *****************************/
694
6919c53f 695
f6bcfd97 696/********** wxDbTable::Open() **********/
f02d4a64 697bool wxDbTable::Open(bool checkPrivileges)
108106cf 698{
89894079 699 if (!pDb)
f6bcfd97 700 return FALSE;
3ca6a5f0 701
89894079 702 int i;
1e92909e 703 wxString sqlStmt;
f02d4a64 704 wxString s;
89894079 705
4fdae997 706 s.Empty();
89894079 707 // Verify that the table exists in the database
f02d4a64 708 if (!pDb->TableExists(tableName,/*pDb->GetUsername()*/NULL,tablePath))
89894079 709 {
4fdae997
GT
710 s = wxT("Table/view does not exist in the database");
711 if ( *(pDb->dbInf.accessibleTables) == wxT('Y'))
712 s += wxT(", or you have no permissions.\n");
e16143f6 713 else
4fdae997 714 s += wxT(".\n");
f02d4a64
GT
715 }
716 else if (checkPrivileges)
717 {
718 // Verify the user has rights to access the table.
719 // Shortcut boolean evaluation to optimize out call to
720 // TablePrivileges
721 //
722 // Unfortunately this optimization doesn't seem to be
723 // reliable!
724 if (// *(pDb->dbInf.accessibleTables) == 'N' &&
4fdae997
GT
725 !pDb->TablePrivileges(tableName,wxT("SELECT"),NULL,pDb->GetUsername(),tablePath))
726 s = wxT("Current logged in user does not have sufficient privileges to access this table.\n");
f02d4a64
GT
727 }
728
729 if (!s.IsEmpty())
730 {
731 wxString p;
732
4fdae997 733 if (!tablePath.IsEmpty())
7d8c3dba 734 p.Printf(wxT("Error opening '%s/%s'.\n"),tablePath.c_str(),tableName.c_str());
e16143f6 735 else
7d8c3dba 736 p.Printf(wxT("Error opening '%s'.\n"), tableName.c_str());
f02d4a64
GT
737
738 p += s;
739 pDb->LogError(p.GetData());
740
89894079
VZ
741 return(FALSE);
742 }
743
744 // Bind the member variables for field exchange between
f6bcfd97 745 // the wxDbTable object and the ODBC record.
89894079
VZ
746 if (!queryOnly)
747 {
748 if (!bindInsertParams()) // Inserts
749 return(FALSE);
f6bcfd97 750
89894079
VZ
751 if (!bindUpdateParams()) // Updates
752 return(FALSE);
753 }
3ca6a5f0 754
89894079
VZ
755 if (!bindCols(*hstmtDefault)) // Selects
756 return(FALSE);
3ca6a5f0 757
89894079
VZ
758 if (!bindCols(hstmtInternal)) // Internal use only
759 return(FALSE);
f02d4a64
GT
760
761 /*
89894079
VZ
762 * Do NOT bind the hstmtCount cursor!!!
763 */
764
765 // Build an insert statement using parameter markers
766 if (!queryOnly && noCols > 0)
767 {
768 bool needComma = FALSE;
7d8c3dba 769 sqlStmt.Printf(wxT("INSERT INTO %s ("), tableName.c_str());
89894079
VZ
770 for (i = 0; i < noCols; i++)
771 {
772 if (! colDefs[i].InsertAllowed)
773 continue;
774 if (needComma)
4fdae997 775 sqlStmt += wxT(",");
1e92909e 776 sqlStmt += colDefs[i].ColName;
89894079
VZ
777 needComma = TRUE;
778 }
779 needComma = FALSE;
4fdae997 780 sqlStmt += wxT(") VALUES (");
f6bcfd97 781
3ca6a5f0 782 int insertableCount = 0;
f6bcfd97 783
89894079
VZ
784 for (i = 0; i < noCols; i++)
785 {
786 if (! colDefs[i].InsertAllowed)
787 continue;
788 if (needComma)
4fdae997
GT
789 sqlStmt += wxT(",");
790 sqlStmt += wxT("?");
89894079 791 needComma = TRUE;
3ca6a5f0 792 insertableCount++;
89894079 793 }
4fdae997 794 sqlStmt += wxT(")");
3ca6a5f0 795
89894079 796 // Prepare the insert statement for execution
f6bcfd97 797 if (insertableCount)
3ca6a5f0 798 {
f6bcfd97
BP
799 if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
800 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
801 }
3ca6a5f0 802 else
f6bcfd97 803 insertable= FALSE;
89894079
VZ
804 }
805
806 // Completed successfully
807 return(TRUE);
108106cf 808
f6bcfd97 809} // wxDbTable::Open()
108106cf 810
67e9aaa3 811
f6bcfd97
BP
812/********** wxDbTable::Query() **********/
813bool wxDbTable::Query(bool forUpdate, bool distinct)
108106cf
JS
814{
815
89894079 816 return(query(DB_SELECT_WHERE, forUpdate, distinct));
108106cf 817
f6bcfd97 818} // wxDbTable::Query()
108106cf 819
67e9aaa3 820
f6bcfd97 821/********** wxDbTable::QueryBySqlStmt() **********/
4fdae997 822bool wxDbTable::QueryBySqlStmt(const wxString &pSqlStmt)
108106cf 823{
89894079 824 pDb->WriteSqlLog(pSqlStmt);
108106cf 825
89894079 826 return(query(DB_SELECT_STATEMENT, FALSE, FALSE, pSqlStmt));
108106cf 827
f6bcfd97 828} // wxDbTable::QueryBySqlStmt()
108106cf 829
67e9aaa3 830
f6bcfd97
BP
831/********** wxDbTable::QueryMatching() **********/
832bool wxDbTable::QueryMatching(bool forUpdate, bool distinct)
108106cf
JS
833{
834
89894079 835 return(query(DB_SELECT_MATCHING, forUpdate, distinct));
108106cf 836
f6bcfd97 837} // wxDbTable::QueryMatching()
108106cf 838
67e9aaa3 839
f6bcfd97
BP
840/********** wxDbTable::QueryOnKeyFields() **********/
841bool wxDbTable::QueryOnKeyFields(bool forUpdate, bool distinct)
108106cf
JS
842{
843
89894079 844 return(query(DB_SELECT_KEYFIELDS, forUpdate, distinct));
108106cf 845
f6bcfd97 846} // wxDbTable::QueryOnKeyFields()
108106cf 847
67e9aaa3 848
f6bcfd97
BP
849/********** wxDbTable::GetPrev() **********/
850bool wxDbTable::GetPrev(void)
a3439c7d 851{
89894079
VZ
852 if (pDb->FwdOnlyCursors())
853 {
f6bcfd97 854 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
89894079
VZ
855 return FALSE;
856 }
857 else
858 return(getRec(SQL_FETCH_PRIOR));
3ca6a5f0 859
f6bcfd97 860} // wxDbTable::GetPrev()
a3439c7d 861
67e9aaa3 862
f6bcfd97
BP
863/********** wxDbTable::operator-- **********/
864bool wxDbTable::operator--(int)
a3439c7d 865{
89894079
VZ
866 if (pDb->FwdOnlyCursors())
867 {
f6bcfd97 868 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxDbTable"));
89894079
VZ
869 return FALSE;
870 }
871 else
872 return(getRec(SQL_FETCH_PRIOR));
3ca6a5f0 873
f6bcfd97 874} // wxDbTable::operator--
a3439c7d 875
67e9aaa3 876
f6bcfd97
BP
877/********** wxDbTable::GetFirst() **********/
878bool wxDbTable::GetFirst(void)
a3439c7d 879{
89894079
VZ
880 if (pDb->FwdOnlyCursors())
881 {
f6bcfd97 882 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxDbTable"));
89894079
VZ
883 return FALSE;
884 }
885 else
886 return(getRec(SQL_FETCH_FIRST));
3ca6a5f0 887
f6bcfd97 888} // wxDbTable::GetFirst()
a3439c7d 889
67e9aaa3 890
f6bcfd97
BP
891/********** wxDbTable::GetLast() **********/
892bool wxDbTable::GetLast(void)
a3439c7d 893{
89894079
VZ
894 if (pDb->FwdOnlyCursors())
895 {
f6bcfd97 896 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxDbTable"));
89894079
VZ
897 return FALSE;
898 }
899 else
900 return(getRec(SQL_FETCH_LAST));
3ca6a5f0 901
f6bcfd97 902} // wxDbTable::GetLast()
a3439c7d 903
67e9aaa3 904
4fdae997
GT
905/********** wxDbTable::BuildDeleteStmt() **********/
906void wxDbTable::BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxString &pWhereClause)
108106cf 907{
4fdae997
GT
908 wxASSERT(!queryOnly);
909 if (queryOnly)
910 return;
911
912 wxString whereClause;
89894079 913
4fdae997
GT
914 whereClause.Empty();
915
916 // Handle the case of DeleteWhere() and the where clause is blank. It should
917 // delete all records from the database in this case.
918 if (typeOfDel == DB_DEL_WHERE && (pWhereClause.Length() == 0))
919 {
7d8c3dba 920 pSqlStmt.Printf(wxT("DELETE FROM %s"), tableName.c_str());
4fdae997
GT
921 return;
922 }
923
7d8c3dba 924 pSqlStmt.Printf(wxT("DELETE FROM %s WHERE "), tableName.c_str());
4fdae997
GT
925
926 // Append the WHERE clause to the SQL DELETE statement
927 switch(typeOfDel)
928 {
929 case DB_DEL_KEYFIELDS:
930 // If the datasource supports the ROWID column, build
931 // the where on ROWID for efficiency purposes.
932 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
933 if (CanUpdByROWID())
934 {
935 SDWORD cb;
936 wxChar rowid[wxDB_ROWID_LEN+1];
937
938 // Get the ROWID value. If not successful retreiving the ROWID,
939 // simply fall down through the code and build the WHERE clause
940 // based on the key fields.
941 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, wxDB_ROWID_LEN, &cb) == SQL_SUCCESS)
942 {
943 pSqlStmt += wxT("ROWID = '");
944 pSqlStmt += rowid;
945 pSqlStmt += wxT("'");
946 break;
947 }
948 }
949 // Unable to delete by ROWID, so build a WHERE
950 // clause based on the keyfields.
951 BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS);
952 pSqlStmt += whereClause;
953 break;
954 case DB_DEL_WHERE:
955 pSqlStmt += pWhereClause;
956 break;
957 case DB_DEL_MATCHING:
958 BuildWhereClause(whereClause, DB_WHERE_MATCHING);
959 pSqlStmt += whereClause;
960 break;
961 }
962
963} // BuildDeleteStmt()
964
965
966/***** DEPRECATED: use wxDbTable::BuildDeleteStmt(wxString &....) form *****/
967void wxDbTable::BuildDeleteStmt(wxChar *pSqlStmt, int typeOfDel, const wxString &pWhereClause)
968{
969 wxString tempSqlStmt;
970 BuildDeleteStmt(tempSqlStmt, typeOfDel, pWhereClause);
971 wxStrcpy(pSqlStmt, tempSqlStmt);
972} // wxDbTable::BuildDeleteStmt()
973
974
975/********** wxDbTable::BuildSelectStmt() **********/
976void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool distinct)
977{
978 wxString whereClause;
979 whereClause.Empty();
89894079
VZ
980
981 // Build a select statement to query the database
4fdae997 982 pSqlStmt = wxT("SELECT ");
89894079
VZ
983
984 // SELECT DISTINCT values only?
985 if (distinct)
4fdae997 986 pSqlStmt += wxT("DISTINCT ");
89894079
VZ
987
988 // Was a FROM clause specified to join tables to the base table?
989 // Available for ::Query() only!!!
990 bool appendFromClause = FALSE;
f6bcfd97 991#if wxODBC_BACKWARD_COMPATABILITY
89894079
VZ
992 if (typeOfSelect == DB_SELECT_WHERE && from && wxStrlen(from))
993 appendFromClause = TRUE;
f6bcfd97
BP
994#else
995 if (typeOfSelect == DB_SELECT_WHERE && from.Length())
996 appendFromClause = TRUE;
997#endif
89894079
VZ
998
999 // Add the column list
1000 int i;
1001 for (i = 0; i < noCols; i++)
1002 {
1003 // If joining tables, the base table column names must be qualified to avoid ambiguity
1004 if (appendFromClause)
1005 {
4fdae997
GT
1006 pSqlStmt += queryTableName;
1007 pSqlStmt += wxT(".");
89894079 1008 }
4fdae997 1009 pSqlStmt += colDefs[i].ColName;
89894079 1010 if (i + 1 < noCols)
4fdae997 1011 pSqlStmt += wxT(",");
89894079
VZ
1012 }
1013
1014 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
1015 // the ROWID if querying distinct records. The rowid will always be unique.
1016 if (!distinct && CanUpdByROWID())
1017 {
1018 // If joining tables, the base table column names must be qualified to avoid ambiguity
1019 if (appendFromClause)
1020 {
4fdae997
GT
1021 pSqlStmt += wxT(",");
1022 pSqlStmt += queryTableName;
1023 pSqlStmt += wxT(".ROWID");
89894079
VZ
1024 }
1025 else
4fdae997 1026 pSqlStmt += wxT(",ROWID");
89894079
VZ
1027 }
1028
1029 // Append the FROM tablename portion
4fdae997
GT
1030 pSqlStmt += wxT(" FROM ");
1031 pSqlStmt += queryTableName;
89894079
VZ
1032
1033 // Sybase uses the HOLDLOCK keyword to lock a record during query.
1034 // The HOLDLOCK keyword follows the table name in the from clause.
1035 // Each table in the from clause must specify HOLDLOCK or
1036 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
1037 // is parsed but ignored in SYBASE Transact-SQL.
1038 if (selectForUpdate && (pDb->Dbms() == dbmsSYBASE_ASA || pDb->Dbms() == dbmsSYBASE_ASE))
4fdae997 1039 pSqlStmt += wxT(" HOLDLOCK");
89894079
VZ
1040
1041 if (appendFromClause)
4fdae997 1042 pSqlStmt += from;
89894079
VZ
1043
1044 // Append the WHERE clause. Either append the where clause for the class
1045 // or build a where clause. The typeOfSelect determines this.
1046 switch(typeOfSelect)
1047 {
3ca6a5f0 1048 case DB_SELECT_WHERE:
f6bcfd97 1049#if wxODBC_BACKWARD_COMPATABILITY
3ca6a5f0 1050 if (where && wxStrlen(where)) // May not want a where clause!!!
f6bcfd97 1051#else
3ca6a5f0 1052 if (where.Length()) // May not want a where clause!!!
f6bcfd97 1053#endif
3ca6a5f0 1054 {
4fdae997
GT
1055 pSqlStmt += wxT(" WHERE ");
1056 pSqlStmt += where;
3ca6a5f0
BP
1057 }
1058 break;
1059 case DB_SELECT_KEYFIELDS:
1060 BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS);
4fdae997 1061 if (whereClause.Length())
3ca6a5f0 1062 {
4fdae997
GT
1063 pSqlStmt += wxT(" WHERE ");
1064 pSqlStmt += whereClause;
3ca6a5f0
BP
1065 }
1066 break;
1067 case DB_SELECT_MATCHING:
1068 BuildWhereClause(whereClause, DB_WHERE_MATCHING);
4fdae997 1069 if (whereClause.Length())
3ca6a5f0 1070 {
4fdae997
GT
1071 pSqlStmt += wxT(" WHERE ");
1072 pSqlStmt += whereClause;
3ca6a5f0
BP
1073 }
1074 break;
89894079
VZ
1075 }
1076
1077 // Append the ORDER BY clause
f6bcfd97 1078#if wxODBC_BACKWARD_COMPATABILITY
89894079 1079 if (orderBy && wxStrlen(orderBy))
f6bcfd97 1080#else
3ca6a5f0 1081 if (orderBy.Length())
f6bcfd97 1082#endif
89894079 1083 {
4fdae997
GT
1084 pSqlStmt += wxT(" ORDER BY ");
1085 pSqlStmt += orderBy;
89894079
VZ
1086 }
1087
1088 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
1089 // parses the FOR UPDATE clause but ignores it. See the comment above on the
1090 // HOLDLOCK for Sybase.
1091 if (selectForUpdate && CanSelectForUpdate())
4fdae997
GT
1092 pSqlStmt += wxT(" FOR UPDATE");
1093
1094} // wxDbTable::BuildSelectStmt()
1095
108106cf 1096
4fdae997
GT
1097/***** DEPRECATED: use wxDbTable::BuildSelectStmt(wxString &....) form *****/
1098void wxDbTable::BuildSelectStmt(wxChar *pSqlStmt, int typeOfSelect, bool distinct)
1099{
1100 wxString tempSqlStmt;
1101 BuildSelectStmt(tempSqlStmt, typeOfSelect, distinct);
1102 wxStrcpy(pSqlStmt, tempSqlStmt);
f6bcfd97 1103} // wxDbTable::BuildSelectStmt()
108106cf 1104
67e9aaa3 1105
4fdae997
GT
1106/********** wxDbTable::BuildUpdateStmt() **********/
1107void wxDbTable::BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpd, const wxString &pWhereClause)
1108{
1109 wxASSERT(!queryOnly);
1110 if (queryOnly)
1111 return;
1112
1113 wxString whereClause;
1114 whereClause.Empty();
1115
1116 bool firstColumn = TRUE;
1117
7d8c3dba 1118 pSqlStmt.Printf(wxT("UPDATE %s SET "), tableName.c_str());
4fdae997
GT
1119
1120 // Append a list of columns to be updated
1121 int i;
1122 for (i = 0; i < noCols; i++)
1123 {
1124 // Only append Updateable columns
1125 if (colDefs[i].Updateable)
1126 {
1127 if (! firstColumn)
1128 pSqlStmt += wxT(",");
1129 else
1130 firstColumn = FALSE;
1131 pSqlStmt += colDefs[i].ColName;
1132 pSqlStmt += wxT(" = ?");
1133 }
1134 }
1135
1136 // Append the WHERE clause to the SQL UPDATE statement
1137 pSqlStmt += wxT(" WHERE ");
1138 switch(typeOfUpd)
1139 {
1140 case DB_UPD_KEYFIELDS:
1141 // If the datasource supports the ROWID column, build
1142 // the where on ROWID for efficiency purposes.
1143 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1144 if (CanUpdByROWID())
1145 {
1146 SDWORD cb;
1147 wxChar rowid[wxDB_ROWID_LEN+1];
1148
1149 // Get the ROWID value. If not successful retreiving the ROWID,
1150 // simply fall down through the code and build the WHERE clause
1151 // based on the key fields.
1152 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, wxDB_ROWID_LEN, &cb) == SQL_SUCCESS)
1153 {
1154 pSqlStmt += wxT("ROWID = '");
1155 pSqlStmt += rowid;
1156 pSqlStmt += wxT("'");
1157 break;
1158 }
1159 }
1160 // Unable to delete by ROWID, so build a WHERE
1161 // clause based on the keyfields.
1162 BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS);
1163 pSqlStmt += whereClause;
1164 break;
1165 case DB_UPD_WHERE:
1166 pSqlStmt += pWhereClause;
1167 break;
1168 }
1169} // BuildUpdateStmt()
1170
1171
1172/***** DEPRECATED: use wxDbTable::BuildUpdateStmt(wxString &....) form *****/
1173void wxDbTable::BuildUpdateStmt(wxChar *pSqlStmt, int typeOfUpd, const wxString &pWhereClause)
1174{
1175 wxString tempSqlStmt;
1176 BuildUpdateStmt(tempSqlStmt, typeOfUpd, pWhereClause);
1177 wxStrcpy(pSqlStmt, tempSqlStmt);
1178} // BuildUpdateStmt()
1179
1180
1181/********** wxDbTable::BuildWhereClause() **********/
1182void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
1183 const wxString &qualTableName, bool useLikeComparison)
1184/*
1185 * Note: BuildWhereClause() currently ignores timestamp columns.
1186 * They are not included as part of the where clause.
1187 */
1188{
1189 bool moreThanOneColumn = FALSE;
1190 wxString colValue;
1191
1192 // Loop through the columns building a where clause as you go
1193 int i;
1194 for (i = 0; i < noCols; i++)
1195 {
1196 // Determine if this column should be included in the WHERE clause
1197 if ((typeOfWhere == DB_WHERE_KEYFIELDS && colDefs[i].KeyField) ||
1198 (typeOfWhere == DB_WHERE_MATCHING && (!IsColNull(i))))
1199 {
1200 // Skip over timestamp columns
1201 if (colDefs[i].SqlCtype == SQL_C_TIMESTAMP)
1202 continue;
1203 // If there is more than 1 column, join them with the keyword "AND"
1204 if (moreThanOneColumn)
1205 pWhereClause += wxT(" AND ");
1206 else
1207 moreThanOneColumn = TRUE;
1208 // Concatenate where phrase for the column
1209 if (qualTableName.Length())
1210 {
1211 pWhereClause += qualTableName;
1212 pWhereClause += wxT(".");
1213 }
1214 pWhereClause += colDefs[i].ColName;
1215 if (useLikeComparison && (colDefs[i].SqlCtype == SQL_C_CHAR))
1216 pWhereClause += wxT(" LIKE ");
1217 else
1218 pWhereClause += wxT(" = ");
1219 switch(colDefs[i].SqlCtype)
1220 {
1221 case SQL_C_CHAR:
1222 colValue.Printf(wxT("'%s'"), (UCHAR FAR *) colDefs[i].PtrDataObj);
1223 break;
1224 case SQL_C_SSHORT:
1225 colValue.Printf(wxT("%hi"), *((SWORD *) colDefs[i].PtrDataObj));
1226 break;
1227 case SQL_C_USHORT:
1228 colValue.Printf(wxT("%hu"), *((UWORD *) colDefs[i].PtrDataObj));
1229 break;
1230 case SQL_C_SLONG:
1231 colValue.Printf(wxT("%li"), *((SDWORD *) colDefs[i].PtrDataObj));
1232 break;
1233 case SQL_C_ULONG:
1234 colValue.Printf(wxT("%lu"), *((UDWORD *) colDefs[i].PtrDataObj));
1235 break;
1236 case SQL_C_FLOAT:
1237 colValue.Printf(wxT("%.6f"), *((SFLOAT *) colDefs[i].PtrDataObj));
1238 break;
1239 case SQL_C_DOUBLE:
1240 colValue.Printf(wxT("%.6f"), *((SDOUBLE *) colDefs[i].PtrDataObj));
1241 break;
1242 }
1243 pWhereClause += colValue;
1244 }
1245 }
1246} // wxDbTable::BuildWhereClause()
1247
1248
1249/***** DEPRECATED: use wxDbTable::BuildWhereClause(wxString &....) form *****/
1250void wxDbTable::BuildWhereClause(wxChar *pWhereClause, int typeOfWhere,
1251 const wxString &qualTableName, bool useLikeComparison)
1252{
1253 wxString tempSqlStmt;
1254 BuildWhereClause(tempSqlStmt, typeOfWhere, qualTableName, useLikeComparison);
1255 wxStrcpy(pWhereClause, tempSqlStmt);
1256} // wxDbTable::BuildWhereClause()
1257
1258
f6bcfd97
BP
1259/********** wxDbTable::GetRowNum() **********/
1260UWORD wxDbTable::GetRowNum(void)
108106cf 1261{
89894079 1262 UDWORD rowNum;
108106cf 1263
89894079
VZ
1264 if (SQLGetStmtOption(hstmt, SQL_ROW_NUMBER, (UCHAR*) &rowNum) != SQL_SUCCESS)
1265 {
1266 pDb->DispAllErrors(henv, hdbc, hstmt);
1267 return(0);
1268 }
108106cf 1269
89894079
VZ
1270 // Completed successfully
1271 return((UWORD) rowNum);
108106cf 1272
f6bcfd97 1273} // wxDbTable::GetRowNum()
108106cf 1274
67e9aaa3 1275
f6bcfd97
BP
1276/********** wxDbTable::CloseCursor() **********/
1277bool wxDbTable::CloseCursor(HSTMT cursor)
108106cf 1278{
89894079
VZ
1279 if (SQLFreeStmt(cursor, SQL_CLOSE) != SQL_SUCCESS)
1280 return(pDb->DispAllErrors(henv, hdbc, cursor));
108106cf 1281
89894079
VZ
1282 // Completed successfully
1283 return(TRUE);
108106cf 1284
f6bcfd97 1285} // wxDbTable::CloseCursor()
108106cf 1286
67e9aaa3 1287
f6bcfd97
BP
1288/********** wxDbTable::CreateTable() **********/
1289bool wxDbTable::CreateTable(bool attemptDrop)
108106cf 1290{
89894079
VZ
1291 if (!pDb)
1292 return FALSE;
1fc5dd6f 1293
89894079 1294 int i, j;
1e92909e 1295 wxString sqlStmt;
108106cf 1296
a2115c88 1297#ifdef DBDEBUG_CONSOLE
4fdae997 1298 cout << wxT("Creating Table ") << tableName << wxT("...") << endl;
108106cf
JS
1299#endif
1300
89894079
VZ
1301 // Drop table first
1302 if (attemptDrop && !DropTable())
1303 return FALSE;
108106cf 1304
89894079 1305 // Create the table
a2115c88 1306#ifdef DBDEBUG_CONSOLE
89894079
VZ
1307 for (i = 0; i < noCols; i++)
1308 {
1309 // Exclude derived columns since they are NOT part of the base table
1310 if (colDefs[i].DerivedCol)
1311 continue;
4fdae997 1312 cout << i + 1 << wxT(": ") << colDefs[i].ColName << wxT("; ");
89894079
VZ
1313 switch(colDefs[i].DbDataType)
1314 {
1315 case DB_DATA_TYPE_VARCHAR:
4fdae997 1316 cout << pDb->typeInfVarchar.TypeName << wxT("(") << colDefs[i].SzDataObj << wxT(")");
89894079
VZ
1317 break;
1318 case DB_DATA_TYPE_INTEGER:
1319 cout << pDb->typeInfInteger.TypeName;
1320 break;
1321 case DB_DATA_TYPE_FLOAT:
1322 cout << pDb->typeInfFloat.TypeName;
1323 break;
1324 case DB_DATA_TYPE_DATE:
1325 cout << pDb->typeInfDate.TypeName;
1326 break;
bf5423ea
GT
1327 case DB_DATA_TYPE_BLOB:
1328 cout << pDb->typeInfBlob.TypeName;
1329 break;
89894079
VZ
1330 }
1331 cout << endl;
1332 }
108106cf
JS
1333#endif
1334
89894079
VZ
1335 // Build a CREATE TABLE string from the colDefs structure.
1336 bool needComma = FALSE;
7d8c3dba 1337 sqlStmt.Printf(wxT("CREATE TABLE %s ("), tableName.c_str());
1e92909e 1338
89894079
VZ
1339 for (i = 0; i < noCols; i++)
1340 {
1341 // Exclude derived columns since they are NOT part of the base table
1342 if (colDefs[i].DerivedCol)
1343 continue;
1344 // Comma Delimiter
1345 if (needComma)
4fdae997 1346 sqlStmt += wxT(",");
89894079 1347 // Column Name
1e92909e 1348 sqlStmt += colDefs[i].ColName;
4fdae997 1349 sqlStmt += wxT(" ");
89894079
VZ
1350 // Column Type
1351 switch(colDefs[i].DbDataType)
1352 {
1353 case DB_DATA_TYPE_VARCHAR:
3ca6a5f0
BP
1354 sqlStmt += pDb->GetTypeInfVarchar().TypeName;
1355 break;
89894079 1356 case DB_DATA_TYPE_INTEGER:
3ca6a5f0
BP
1357 sqlStmt += pDb->GetTypeInfInteger().TypeName;
1358 break;
89894079 1359 case DB_DATA_TYPE_FLOAT:
3ca6a5f0
BP
1360 sqlStmt += pDb->GetTypeInfFloat().TypeName;
1361 break;
89894079 1362 case DB_DATA_TYPE_DATE:
3ca6a5f0
BP
1363 sqlStmt += pDb->GetTypeInfDate().TypeName;
1364 break;
bf5423ea
GT
1365 case DB_DATA_TYPE_BLOB:
1366 sqlStmt += pDb->GetTypeInfBlob().TypeName;
1367 break;
89894079
VZ
1368 }
1369 // For varchars, append the size of the string
bf5423ea
GT
1370 if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)// ||
1371// colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
89894079 1372 {
1e92909e 1373 wxString s;
4fdae997
GT
1374 s.Printf(wxT("(%d)"), colDefs[i].SzDataObj);
1375 sqlStmt += s;
89894079
VZ
1376 }
1377
e93a3a18
GT
1378 if (pDb->Dbms() == dbmsDB2 ||
1379 pDb->Dbms() == dbmsMY_SQL ||
1380 pDb->Dbms() == dbmsSYBASE_ASE ||
1381 pDb->Dbms() == dbmsMS_SQL_SERVER)
89894079
VZ
1382 {
1383 if (colDefs[i].KeyField)
1384 {
4fdae997 1385 sqlStmt += wxT(" NOT NULL");
89894079
VZ
1386 }
1387 }
1388
1389 needComma = TRUE;
1390 }
1391 // If there is a primary key defined, include it in the create statement
1392 for (i = j = 0; i < noCols; i++)
1393 {
1394 if (colDefs[i].KeyField)
1395 {
1396 j++;
1397 break;
1398 }
1399 }
1400 if (j && pDb->Dbms() != dbmsDBASE) // Found a keyfield
1401 {
1402 if (pDb->Dbms() != dbmsMY_SQL)
1403 {
4fdae997 1404 sqlStmt += wxT(",CONSTRAINT ");
1e92909e 1405 sqlStmt += tableName;
4fdae997 1406 sqlStmt += wxT("_PIDX PRIMARY KEY (");
89894079
VZ
1407 }
1408 else
1409 {
1410 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
4fdae997 1411 sqlStmt += wxT(", PRIMARY KEY (");
89894079
VZ
1412 }
1413
1414 // List column name(s) of column(s) comprising the primary key
1415 for (i = j = 0; i < noCols; i++)
1416 {
1417 if (colDefs[i].KeyField)
1418 {
1419 if (j++) // Multi part key, comma separate names
4fdae997 1420 sqlStmt += wxT(",");
1e92909e 1421 sqlStmt += colDefs[i].ColName;
89894079
VZ
1422 }
1423 }
4fdae997 1424 sqlStmt += wxT(")");
89894079
VZ
1425 }
1426 // Append the closing parentheses for the create table statement
4fdae997 1427 sqlStmt += wxT(")");
a2115c88 1428
4fdae997 1429 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1430
a2115c88 1431#ifdef DBDEBUG_CONSOLE
f6bcfd97 1432 cout << endl << sqlStmt.c_str() << endl;
108106cf
JS
1433#endif
1434
89894079 1435 // Execute the CREATE TABLE statement
f6bcfd97 1436 RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
89894079
VZ
1437 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1438 {
1439 pDb->DispAllErrors(henv, hdbc, hstmt);
1440 pDb->RollbackTrans();
1441 CloseCursor(hstmt);
1442 return(FALSE);
1443 }
1444
1445 // Commit the transaction and close the cursor
3ca6a5f0 1446 if (!pDb->CommitTrans())
89894079 1447 return(FALSE);
3ca6a5f0 1448 if (!CloseCursor(hstmt))
89894079
VZ
1449 return(FALSE);
1450
1451 // Database table created successfully
1452 return(TRUE);
108106cf 1453
f6bcfd97 1454} // wxDbTable::CreateTable()
108106cf 1455
67e9aaa3 1456
f6bcfd97
BP
1457/********** wxDbTable::DropTable() **********/
1458bool wxDbTable::DropTable()
a2115c88 1459{
89894079
VZ
1460 // NOTE: This function returns TRUE if the Table does not exist, but
1461 // only for identified databases. Code will need to be added
0b8410f3 1462 // below for any other databases when those databases are defined
89894079 1463 // to handle this situation consistently
a2115c88 1464
1e92909e 1465 wxString sqlStmt;
a2115c88 1466
7d8c3dba 1467 sqlStmt.Printf(wxT("DROP TABLE %s"), tableName.c_str());
a2115c88 1468
4fdae997 1469 pDb->WriteSqlLog(sqlStmt);
a2115c88
GT
1470
1471#ifdef DBDEBUG_CONSOLE
f6bcfd97 1472 cout << endl << sqlStmt.c_str() << endl;
a2115c88
GT
1473#endif
1474
f6bcfd97 1475 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
1476 {
1477 // Check for "Base table not found" error and ignore
3ca6a5f0 1478 pDb->GetNextError(henv, hdbc, hstmt);
4fdae997
GT
1479 if (wxStrcmp(pDb->sqlState, wxT("S0002")) &&
1480 wxStrcmp(pDb->sqlState, wxT("S1000"))) // "Base table not found"
3ca6a5f0 1481 {
89894079 1482 // Check for product specific error codes
4fdae997
GT
1483 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // 5.x (and lower?)
1484 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
1485 (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))))
89894079
VZ
1486 {
1487 pDb->DispNextError();
1488 pDb->DispAllErrors(henv, hdbc, hstmt);
1489 pDb->RollbackTrans();
1490 CloseCursor(hstmt);
1491 return(FALSE);
1492 }
1493 }
1494 }
1495
1496 // Commit the transaction and close the cursor
1497 if (! pDb->CommitTrans())
1498 return(FALSE);
1499 if (! CloseCursor(hstmt))
1500 return(FALSE);
1501
1502 return(TRUE);
f6bcfd97 1503} // wxDbTable::DropTable()
a2115c88 1504
67e9aaa3 1505
f6bcfd97 1506/********** wxDbTable::CreateIndex() **********/
4fdae997 1507bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, int noIdxCols, wxDbIdxDef *pIdxDefs, bool attemptDrop)
108106cf 1508{
1e92909e 1509 wxString sqlStmt;
89894079
VZ
1510
1511 // Drop the index first
1512 if (attemptDrop && !DropIndex(idxName))
1513 return (FALSE);
1514
3ca6a5f0
BP
1515 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1516 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1517 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1518 // table was created, then months later you determine that an additional index while
1519 // give better performance, so you want to add an index).
1520 //
1521 // The following block of code will modify the column definition to make the column be
1522 // defined with the "NOT NULL" qualifier.
1523 if (pDb->Dbms() == dbmsMY_SQL)
1524 {
1525 wxString sqlStmt;
1526 int i;
1527 bool ok = TRUE;
1528 for (i = 0; i < noIdxCols && ok; i++)
1529 {
1530 int j = 0;
1531 bool found = FALSE;
1532 // Find the column definition that has the ColName that matches the
1533 // index column name. We need to do this to get the DB_DATA_TYPE of
1534 // the index column, as MySQL's syntax for the ALTER column requires
1535 // this information
1536 while (!found && (j < this->noCols))
1537 {
1538 if (wxStrcmp(colDefs[j].ColName,pIdxDefs[i].ColName) == 0)
1539 found = TRUE;
1540 if (!found)
1541 j++;
1542 }
1543
1544 if (found)
1545 {
4fdae997
GT
1546 ok = pDb->ModifyColumn(tableName, pIdxDefs[i].ColName,
1547 colDefs[j].DbDataType, colDefs[j].SzDataObj,
1548 wxT("NOT NULL"));
1549
3ca6a5f0
BP
1550 if (!ok)
1551 {
1552 wxODBC_ERRORS retcode;
1553 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1554 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1555 // This line is just here for debug checking of the value
1556 retcode = (wxODBC_ERRORS)pDb->DB_STATUS;
1557 }
1558 }
1559 else
1560 ok = FALSE;
1561 }
1562 if (ok)
1563 pDb->CommitTrans();
1564 else
1565 {
1566 pDb->RollbackTrans();
1567 return(FALSE);
1568 }
1569 }
1570
89894079 1571 // Build a CREATE INDEX statement
4fdae997 1572 sqlStmt = wxT("CREATE ");
89894079 1573 if (unique)
4fdae997 1574 sqlStmt += wxT("UNIQUE ");
3ca6a5f0 1575
4fdae997 1576 sqlStmt += wxT("INDEX ");
1e92909e 1577 sqlStmt += idxName;
4fdae997 1578 sqlStmt += wxT(" ON ");
1e92909e 1579 sqlStmt += tableName;
4fdae997 1580 sqlStmt += wxT(" (");
3ca6a5f0 1581
89894079
VZ
1582 // Append list of columns making up index
1583 int i;
1584 for (i = 0; i < noIdxCols; i++)
1585 {
1e92909e
GT
1586 sqlStmt += pIdxDefs[i].ColName;
1587 /* Postgres doesn't cope with ASC */
89894079
VZ
1588 if (pDb->Dbms() != dbmsPOSTGRES)
1589 {
1590 if (pIdxDefs[i].Ascending)
4fdae997 1591 sqlStmt += wxT(" ASC");
89894079 1592 else
4fdae997 1593 sqlStmt += wxT(" DESC");
89894079
VZ
1594 }
1595
1596 if ((i + 1) < noIdxCols)
4fdae997 1597 sqlStmt += wxT(",");
89894079
VZ
1598 }
1599
1600 // Append closing parentheses
4fdae997 1601 sqlStmt += wxT(")");
89894079 1602
4fdae997 1603 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1604
a2115c88 1605#ifdef DBDEBUG_CONSOLE
f6bcfd97 1606 cout << endl << sqlStmt.c_str() << endl << endl;
108106cf
JS
1607#endif
1608
89894079 1609 // Execute the CREATE INDEX statement
f6bcfd97 1610 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
1611 {
1612 pDb->DispAllErrors(henv, hdbc, hstmt);
1613 pDb->RollbackTrans();
1614 CloseCursor(hstmt);
1615 return(FALSE);
1616 }
108106cf 1617
89894079
VZ
1618 // Commit the transaction and close the cursor
1619 if (! pDb->CommitTrans())
1620 return(FALSE);
1621 if (! CloseCursor(hstmt))
1622 return(FALSE);
108106cf 1623
89894079
VZ
1624 // Index Created Successfully
1625 return(TRUE);
108106cf 1626
f6bcfd97 1627} // wxDbTable::CreateIndex()
108106cf 1628
67e9aaa3 1629
f6bcfd97 1630/********** wxDbTable::DropIndex() **********/
4fdae997 1631bool wxDbTable::DropIndex(const wxString &idxName)
a2115c88 1632{
89894079
VZ
1633 // NOTE: This function returns TRUE if the Index does not exist, but
1634 // only for identified databases. Code will need to be added
1635 // below for any other databases when those databases are defined
1636 // to handle this situation consistently
a2115c88 1637
1e92909e 1638 wxString sqlStmt;
a2115c88 1639
3ca6a5f0 1640 if (pDb->Dbms() == dbmsACCESS || pDb->Dbms() == dbmsMY_SQL)
7d8c3dba 1641 sqlStmt.Printf(wxT("DROP INDEX %s ON %s"),idxName.c_str(), tableName.c_str());
e93a3a18
GT
1642 else if ((pDb->Dbms() == dbmsMS_SQL_SERVER) ||
1643 (pDb->Dbms() == dbmsSYBASE_ASE))
7d8c3dba 1644 sqlStmt.Printf(wxT("DROP INDEX %s.%s"),tableName.c_str(), idxName.c_str());
89894079 1645 else
7d8c3dba 1646 sqlStmt.Printf(wxT("DROP INDEX %s"),idxName.c_str());
a2115c88 1647
4fdae997 1648 pDb->WriteSqlLog(sqlStmt);
a2115c88
GT
1649
1650#ifdef DBDEBUG_CONSOLE
f6bcfd97 1651 cout << endl << sqlStmt.c_str() << endl;
a2115c88
GT
1652#endif
1653
f6bcfd97 1654 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
1655 {
1656 // Check for "Index not found" error and ignore
1657 pDb->GetNextError(henv, hdbc, hstmt);
4fdae997 1658 if (wxStrcmp(pDb->sqlState,wxT("S0012"))) // "Index not found"
89894079
VZ
1659 {
1660 // Check for product specific error codes
4fdae997
GT
1661 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // v5.x (and lower?)
1662 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
1663 (pDb->Dbms() == dbmsMS_SQL_SERVER && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
1664 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("S0002"))) || // Base table not found
1665 (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1666 (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))
3ca6a5f0 1667 ))
89894079
VZ
1668 {
1669 pDb->DispNextError();
1670 pDb->DispAllErrors(henv, hdbc, hstmt);
1671 pDb->RollbackTrans();
1672 CloseCursor(hstmt);
1673 return(FALSE);
1674 }
1675 }
1676 }
1677
1678 // Commit the transaction and close the cursor
1679 if (! pDb->CommitTrans())
1680 return(FALSE);
1681 if (! CloseCursor(hstmt))
1682 return(FALSE);
1683
1684 return(TRUE);
f6bcfd97 1685} // wxDbTable::DropIndex()
a2115c88 1686
67e9aaa3 1687
38cfbffa
GT
1688/********** wxDbTable::SetOrderByColNums() **********/
1689bool wxDbTable::SetOrderByColNums(int first, ... )
1690{
1691 int colNo = first;
1692 va_list argptr;
1693
1694 bool abort = FALSE;
1695 wxString tempStr;
1696
1697 va_start(argptr, first); /* Initialize variable arguments. */
1698 while (!abort && (colNo != wxDB_NO_MORE_COLUMN_NUMBERS))
1699 {
1700 // Make sure the passed in column number
1701 // is within the valid range of columns
1702 //
1703 // Valid columns are 0 thru noCols-1
1704 if (colNo >= noCols || colNo < 0)
1705 {
1706 abort = TRUE;
1707 continue;
1708 }
1709
1710 if (colNo != first)
4fdae997 1711 tempStr += wxT(",");
38cfbffa
GT
1712
1713 tempStr += colDefs[colNo].ColName;
1714 colNo = va_arg (argptr, int);
1715 }
1716 va_end (argptr); /* Reset variable arguments. */
1717
4fdae997 1718 SetOrderByClause(tempStr);
38cfbffa
GT
1719
1720 return (!abort);
1721} // wxDbTable::SetOrderByColNums()
1722
1723
f6bcfd97
BP
1724/********** wxDbTable::Insert() **********/
1725int wxDbTable::Insert(void)
108106cf 1726{
4fdae997 1727 wxASSERT(!queryOnly);
f6bcfd97 1728 if (queryOnly || !insertable)
89894079
VZ
1729 return(DB_FAILURE);
1730
1731 bindInsertParams();
1732
1733 // Insert the record by executing the already prepared insert statement
1734 RETCODE retcode;
1735 retcode=SQLExecute(hstmtInsert);
1736 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1737 {
1738 // Check to see if integrity constraint was violated
1739 pDb->GetNextError(henv, hdbc, hstmtInsert);
4fdae997 1740 if (! wxStrcmp(pDb->sqlState, wxT("23000"))) // Integrity constraint violated
89894079
VZ
1741 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
1742 else
1743 {
1744 pDb->DispNextError();
1745 pDb->DispAllErrors(henv, hdbc, hstmtInsert);
1746 return(DB_FAILURE);
1747 }
1748 }
1749
1750 // Record inserted into the datasource successfully
1751 return(DB_SUCCESS);
108106cf 1752
f6bcfd97 1753} // wxDbTable::Insert()
108106cf 1754
67e9aaa3 1755
f6bcfd97
BP
1756/********** wxDbTable::Update() **********/
1757bool wxDbTable::Update(void)
108106cf 1758{
4fdae997 1759 wxASSERT(!queryOnly);
89894079
VZ
1760 if (queryOnly)
1761 return(FALSE);
a2115c88 1762
4fdae997 1763 wxString sqlStmt;
108106cf 1764
89894079 1765 // Build the SQL UPDATE statement
f6bcfd97 1766 BuildUpdateStmt(sqlStmt, DB_UPD_KEYFIELDS);
108106cf 1767
89894079 1768 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1769
a2115c88 1770#ifdef DBDEBUG_CONSOLE
4fdae997 1771 cout << endl << sqlStmt.c_str() << endl << endl;
108106cf
JS
1772#endif
1773
89894079
VZ
1774 // Execute the SQL UPDATE statement
1775 return(execUpdate(sqlStmt));
108106cf 1776
f6bcfd97 1777} // wxDbTable::Update()
108106cf 1778
67e9aaa3 1779
f6bcfd97 1780/********** wxDbTable::Update(pSqlStmt) **********/
4fdae997 1781bool wxDbTable::Update(const wxString &pSqlStmt)
6919c53f 1782{
4fdae997 1783 wxASSERT(!queryOnly);
89894079
VZ
1784 if (queryOnly)
1785 return(FALSE);
6919c53f 1786
89894079 1787 pDb->WriteSqlLog(pSqlStmt);
6919c53f 1788
89894079 1789 return(execUpdate(pSqlStmt));
6919c53f 1790
f6bcfd97 1791} // wxDbTable::Update(pSqlStmt)
6919c53f 1792
67e9aaa3 1793
f6bcfd97 1794/********** wxDbTable::UpdateWhere() **********/
4fdae997 1795bool wxDbTable::UpdateWhere(const wxString &pWhereClause)
108106cf 1796{
4fdae997 1797 wxASSERT(!queryOnly);
89894079
VZ
1798 if (queryOnly)
1799 return(FALSE);
a2115c88 1800
4fdae997 1801 wxString sqlStmt;
108106cf 1802
89894079 1803 // Build the SQL UPDATE statement
f6bcfd97 1804 BuildUpdateStmt(sqlStmt, DB_UPD_WHERE, pWhereClause);
108106cf 1805
89894079 1806 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1807
a2115c88 1808#ifdef DBDEBUG_CONSOLE
4fdae997 1809 cout << endl << sqlStmt.c_str() << endl << endl;
108106cf
JS
1810#endif
1811
89894079
VZ
1812 // Execute the SQL UPDATE statement
1813 return(execUpdate(sqlStmt));
108106cf 1814
f6bcfd97 1815} // wxDbTable::UpdateWhere()
108106cf 1816
67e9aaa3 1817
f6bcfd97
BP
1818/********** wxDbTable::Delete() **********/
1819bool wxDbTable::Delete(void)
108106cf 1820{
4fdae997 1821 wxASSERT(!queryOnly);
89894079
VZ
1822 if (queryOnly)
1823 return(FALSE);
a2115c88 1824
4fdae997
GT
1825 wxString sqlStmt;
1826 sqlStmt.Empty();
108106cf 1827
89894079 1828 // Build the SQL DELETE statement
f6bcfd97 1829 BuildDeleteStmt(sqlStmt, DB_DEL_KEYFIELDS);
108106cf 1830
89894079 1831 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1832
89894079
VZ
1833 // Execute the SQL DELETE statement
1834 return(execDelete(sqlStmt));
108106cf 1835
f6bcfd97 1836} // wxDbTable::Delete()
108106cf 1837
67e9aaa3 1838
f6bcfd97 1839/********** wxDbTable::DeleteWhere() **********/
4fdae997 1840bool wxDbTable::DeleteWhere(const wxString &pWhereClause)
108106cf 1841{
4fdae997 1842 wxASSERT(!queryOnly);
89894079
VZ
1843 if (queryOnly)
1844 return(FALSE);
a2115c88 1845
4fdae997
GT
1846 wxString sqlStmt;
1847 sqlStmt.Empty();
108106cf 1848
89894079 1849 // Build the SQL DELETE statement
f6bcfd97 1850 BuildDeleteStmt(sqlStmt, DB_DEL_WHERE, pWhereClause);
108106cf 1851
89894079 1852 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1853
89894079
VZ
1854 // Execute the SQL DELETE statement
1855 return(execDelete(sqlStmt));
108106cf 1856
f6bcfd97 1857} // wxDbTable::DeleteWhere()
108106cf 1858
67e9aaa3 1859
f6bcfd97
BP
1860/********** wxDbTable::DeleteMatching() **********/
1861bool wxDbTable::DeleteMatching(void)
108106cf 1862{
4fdae997 1863 wxASSERT(!queryOnly);
89894079
VZ
1864 if (queryOnly)
1865 return(FALSE);
a2115c88 1866
4fdae997
GT
1867 wxString sqlStmt;
1868 sqlStmt.Empty();
108106cf 1869
89894079 1870 // Build the SQL DELETE statement
f6bcfd97 1871 BuildDeleteStmt(sqlStmt, DB_DEL_MATCHING);
108106cf 1872
89894079 1873 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1874
89894079
VZ
1875 // Execute the SQL DELETE statement
1876 return(execDelete(sqlStmt));
108106cf 1877
f6bcfd97 1878} // wxDbTable::DeleteMatching()
108106cf 1879
67e9aaa3 1880
f6bcfd97
BP
1881/********** wxDbTable::IsColNull() **********/
1882bool wxDbTable::IsColNull(int colNo)
108106cf 1883{
f02d4a64
GT
1884/*
1885 This logic is just not right. It would indicate TRUE
1886 if a numeric field were set to a value of 0.
1887
89894079
VZ
1888 switch(colDefs[colNo].SqlCtype)
1889 {
3ca6a5f0
BP
1890 case SQL_C_CHAR:
1891 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1892 case SQL_C_SSHORT:
1893 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1894 case SQL_C_USHORT:
1895 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1896 case SQL_C_SLONG:
1897 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1898 case SQL_C_ULONG:
1899 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1900 case SQL_C_FLOAT:
1901 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1902 case SQL_C_DOUBLE:
1903 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1904 case SQL_C_TIMESTAMP:
1905 TIMESTAMP_STRUCT *pDt;
1906 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1907 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1908 return(TRUE);
1909 else
1910 return(FALSE);
1911 default:
89894079 1912 return(TRUE);
89894079 1913 }
f02d4a64
GT
1914*/
1915 return (colDefs[colNo].Null);
f6bcfd97 1916} // wxDbTable::IsColNull()
108106cf 1917
67e9aaa3 1918
f6bcfd97
BP
1919/********** wxDbTable::CanSelectForUpdate() **********/
1920bool wxDbTable::CanSelectForUpdate(void)
108106cf 1921{
38cfbffa
GT
1922 if (queryOnly)
1923 return FALSE;
1924
89894079
VZ
1925 if (pDb->Dbms() == dbmsMY_SQL)
1926 return FALSE;
a2115c88 1927
e93a3a18
GT
1928 if ((pDb->Dbms() == dbmsORACLE) ||
1929 (pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE))
89894079
VZ
1930 return(TRUE);
1931 else
1932 return(FALSE);
108106cf 1933
f6bcfd97 1934} // wxDbTable::CanSelectForUpdate()
108106cf 1935
67e9aaa3 1936
f6bcfd97
BP
1937/********** wxDbTable::CanUpdByROWID() **********/
1938bool wxDbTable::CanUpdByROWID(void)
108106cf 1939{
67e9aaa3
GT
1940/*
1941 * NOTE: Returning FALSE for now until this can be debugged,
89894079 1942 * as the ROWID is not getting updated correctly
67e9aaa3 1943 */
89894079 1944 return FALSE;
108106cf 1945
89894079
VZ
1946 if (pDb->Dbms() == dbmsORACLE)
1947 return(TRUE);
1948 else
1949 return(FALSE);
108106cf 1950
f6bcfd97 1951} // wxDbTable::CanUpdByROWID()
108106cf 1952
67e9aaa3 1953
f6bcfd97
BP
1954/********** wxDbTable::IsCursorClosedOnCommit() **********/
1955bool wxDbTable::IsCursorClosedOnCommit(void)
108106cf 1956{
89894079
VZ
1957 if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
1958 return(FALSE);
1959 else
1960 return(TRUE);
108106cf 1961
f6bcfd97 1962} // wxDbTable::IsCursorClosedOnCommit()
108106cf 1963
67e9aaa3 1964
f02d4a64
GT
1965
1966/********** wxDbTable::ClearMemberVar() **********/
1967void wxDbTable::ClearMemberVar(int colNo, bool setToNull)
108106cf 1968{
4fdae997 1969 wxASSERT(colNo < noCols);
f02d4a64
GT
1970
1971 switch(colDefs[colNo].SqlCtype)
89894079 1972 {
f02d4a64
GT
1973 case SQL_C_CHAR:
1974 ((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] = 0;
1975 break;
1976 case SQL_C_SSHORT:
1977 *((SWORD *) colDefs[colNo].PtrDataObj) = 0;
1978 break;
1979 case SQL_C_USHORT:
1980 *((UWORD*) colDefs[colNo].PtrDataObj) = 0;
1981 break;
1982 case SQL_C_SLONG:
1983 *((SDWORD *) colDefs[colNo].PtrDataObj) = 0;
1984 break;
1985 case SQL_C_ULONG:
1986 *((UDWORD *) colDefs[colNo].PtrDataObj) = 0;
1987 break;
1988 case SQL_C_FLOAT:
1989 *((SFLOAT *) colDefs[colNo].PtrDataObj) = 0.0f;
1990 break;
1991 case SQL_C_DOUBLE:
1992 *((SDOUBLE *) colDefs[colNo].PtrDataObj) = 0.0f;
1993 break;
1994 case SQL_C_TIMESTAMP:
1995 TIMESTAMP_STRUCT *pDt;
1996 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1997 pDt->year = 0;
1998 pDt->month = 0;
1999 pDt->day = 0;
2000 pDt->hour = 0;
2001 pDt->minute = 0;
2002 pDt->second = 0;
2003 pDt->fraction = 0;
2004 break;
89894079 2005 }
108106cf 2006
f02d4a64
GT
2007 if (setToNull)
2008 SetColNull(colNo);
2009} // wxDbTable::ClearMemberVar()
2010
2011
2012/********** wxDbTable::ClearMemberVars() **********/
2013void wxDbTable::ClearMemberVars(bool setToNull)
2014{
2015 int i;
2016
2017 // Loop through the columns setting each member variable to zero
2018 for (i=0; i < noCols; i++)
2019 ClearMemberVar(i,setToNull);
2020
f6bcfd97 2021} // wxDbTable::ClearMemberVars()
108106cf 2022
67e9aaa3 2023
f6bcfd97
BP
2024/********** wxDbTable::SetQueryTimeout() **********/
2025bool wxDbTable::SetQueryTimeout(UDWORD nSeconds)
108106cf 2026{
89894079
VZ
2027 if (SQLSetStmtOption(hstmtInsert, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2028 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
2029 if (SQLSetStmtOption(hstmtUpdate, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2030 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
2031 if (SQLSetStmtOption(hstmtDelete, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2032 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
2033 if (SQLSetStmtOption(hstmtInternal, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2034 return(pDb->DispAllErrors(henv, hdbc, hstmtInternal));
2035
2036 // Completed Successfully
2037 return(TRUE);
108106cf 2038
f6bcfd97 2039} // wxDbTable::SetQueryTimeout()
108106cf 2040
67e9aaa3 2041
f6bcfd97 2042/********** wxDbTable::SetColDefs() **********/
4fdae997 2043void wxDbTable::SetColDefs(int index, const wxString &fieldName, int dataType, void *pData,
e93a3a18
GT
2044 int cType, int size, bool keyField, bool upd,
2045 bool insAllow, bool derivedCol)
108106cf 2046{
89894079
VZ
2047 if (!colDefs) // May happen if the database connection fails
2048 return;
2049
4fdae997 2050 if (fieldName.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
89894079
VZ
2051 {
2052 wxStrncpy (colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
2053 colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;
2054 }
2055 else
2056 wxStrcpy(colDefs[index].ColName, fieldName);
2057
2058 colDefs[index].DbDataType = dataType;
2059 colDefs[index].PtrDataObj = pData;
2060 colDefs[index].SqlCtype = cType;
2061 colDefs[index].SzDataObj = size;
2062 colDefs[index].KeyField = keyField;
2063 colDefs[index].DerivedCol = derivedCol;
2064 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2065 if (derivedCol)
2066 {
2067 colDefs[index].Updateable = FALSE;
2068 colDefs[index].InsertAllowed = FALSE;
2069 }
2070 else
2071 {
2072 colDefs[index].Updateable = upd;
2073 colDefs[index].InsertAllowed = insAllow;
2074 }
2075
2076 colDefs[index].Null = FALSE;
2077
f6bcfd97 2078} // wxDbTable::SetColDefs()
108106cf 2079
67e9aaa3 2080
e93a3a18
GT
2081/********** wxDbTable::SetColDefs() **********/
2082wxDbColDataPtr* wxDbTable::SetColDefs(wxDbColInf *pColInfs, ULONG numCols)
67e9aaa3 2083{
4fdae997 2084 wxASSERT(pColInfs);
f6bcfd97 2085 wxDbColDataPtr *pColDataPtrs = NULL;
67e9aaa3 2086
89894079
VZ
2087 if (pColInfs)
2088 {
2089 ULONG index;
1b7274a8 2090
f6bcfd97 2091 pColDataPtrs = new wxDbColDataPtr[numCols+1];
67e9aaa3
GT
2092
2093 for (index = 0; index < numCols; index++)
89894079 2094 {
89894079
VZ
2095 // Process the fields
2096 switch (pColInfs[index].dbDataType)
2097 {
2098 case DB_DATA_TYPE_VARCHAR:
4fdae997 2099 pColDataPtrs[index].PtrDataObj = new wxChar[pColInfs[index].bufferLength+1];
0b8410f3
GT
2100 pColDataPtrs[index].SzDataObj = pColInfs[index].columnSize;
2101 pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
2102 break;
89894079 2103 case DB_DATA_TYPE_INTEGER:
67e9aaa3 2104 // Can be long or short
89894079
VZ
2105 if (pColInfs[index].bufferLength == sizeof(long))
2106 {
2107 pColDataPtrs[index].PtrDataObj = new long;
2108 pColDataPtrs[index].SzDataObj = sizeof(long);
2109 pColDataPtrs[index].SqlCtype = SQL_C_SLONG;
2110 }
2111 else
2112 {
2113 pColDataPtrs[index].PtrDataObj = new short;
2114 pColDataPtrs[index].SzDataObj = sizeof(short);
2115 pColDataPtrs[index].SqlCtype = SQL_C_SSHORT;
2116 }
2117 break;
89894079 2118 case DB_DATA_TYPE_FLOAT:
89894079
VZ
2119 // Can be float or double
2120 if (pColInfs[index].bufferLength == sizeof(float))
2121 {
2122 pColDataPtrs[index].PtrDataObj = new float;
2123 pColDataPtrs[index].SzDataObj = sizeof(float);
2124 pColDataPtrs[index].SqlCtype = SQL_C_FLOAT;
2125 }
2126 else
2127 {
2128 pColDataPtrs[index].PtrDataObj = new double;
2129 pColDataPtrs[index].SzDataObj = sizeof(double);
2130 pColDataPtrs[index].SqlCtype = SQL_C_DOUBLE;
2131 }
2132 break;
89894079 2133 case DB_DATA_TYPE_DATE:
89894079
VZ
2134 pColDataPtrs[index].PtrDataObj = new TIMESTAMP_STRUCT;
2135 pColDataPtrs[index].SzDataObj = sizeof(TIMESTAMP_STRUCT);
2136 pColDataPtrs[index].SqlCtype = SQL_C_TIMESTAMP;
2137 break;
bf5423ea
GT
2138 case DB_DATA_TYPE_BLOB:
2139 int notSupportedYet = 0;
2140 wxASSERT_MSG(notSupportedYet, wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
2141 pColDataPtrs[index].PtrDataObj = /*BLOB ADDITION NEEDED*/NULL;
2142 pColDataPtrs[index].SzDataObj = /*BLOB ADDITION NEEDED*/sizeof(void *);
2143 pColDataPtrs[index].SqlCtype = SQL_VARBINARY;
2144 break;
2145 }
2146 if (pColDataPtrs[index].PtrDataObj != NULL)
2147 SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
2148 else
2149 {
2150 // Unable to build all the column definitions, as either one of
2151 // the calls to "new" failed above, or there was a BLOB field
2152 // to have a column definition for. If BLOBs are to be used,
2153 // the other form of ::SetColDefs() must be used, as it is impossible
2154 // to know the maximum size to create the PtrDataObj to be.
2155 delete [] pColDataPtrs;
2156 return NULL;
89894079 2157 }
89894079
VZ
2158 }
2159 }
3ca6a5f0 2160
89894079 2161 return (pColDataPtrs);
3ca6a5f0 2162
e93a3a18 2163} // wxDbTable::SetColDefs()
67e9aaa3
GT
2164
2165
f6bcfd97
BP
2166/********** wxDbTable::SetCursor() **********/
2167void wxDbTable::SetCursor(HSTMT *hstmtActivate)
108106cf 2168{
f6bcfd97 2169 if (hstmtActivate == wxDB_DEFAULT_CURSOR)
89894079
VZ
2170 hstmt = *hstmtDefault;
2171 else
2172 hstmt = *hstmtActivate;
108106cf 2173
f6bcfd97 2174} // wxDbTable::SetCursor()
108106cf 2175
67e9aaa3 2176
4fdae997
GT
2177/********** wxDbTable::Count(const wxString &) **********/
2178ULONG wxDbTable::Count(const wxString &args)
108106cf 2179{
3ca6a5f0 2180 ULONG count;
1e92909e 2181 wxString sqlStmt;
89894079
VZ
2182 SDWORD cb;
2183
2184 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
4fdae997 2185 sqlStmt = wxT("SELECT COUNT(");
1e92909e 2186 sqlStmt += args;
4fdae997 2187 sqlStmt += wxT(") FROM ");
1e92909e 2188 sqlStmt += queryTableName;
f6bcfd97 2189#if wxODBC_BACKWARD_COMPATABILITY
89894079 2190 if (from && wxStrlen(from))
f6bcfd97
BP
2191#else
2192 if (from.Length())
2193#endif
1e92909e 2194 sqlStmt += from;
89894079
VZ
2195
2196 // Add the where clause if one is provided
f6bcfd97 2197#if wxODBC_BACKWARD_COMPATABILITY
89894079 2198 if (where && wxStrlen(where))
f6bcfd97
BP
2199#else
2200 if (where.Length())
2201#endif
89894079 2202 {
4fdae997 2203 sqlStmt += wxT(" WHERE ");
1e92909e 2204 sqlStmt += where;
89894079
VZ
2205 }
2206
4fdae997 2207 pDb->WriteSqlLog(sqlStmt);
89894079
VZ
2208
2209 // Initialize the Count cursor if it's not already initialized
2210 if (!hstmtCount)
2211 {
f6bcfd97 2212 hstmtCount = GetNewCursor(FALSE,FALSE);
4fdae997 2213 wxASSERT(hstmtCount);
89894079
VZ
2214 if (!hstmtCount)
2215 return(0);
2216 }
2217
2218 // Execute the SQL statement
f6bcfd97 2219 if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
2220 {
2221 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2222 return(0);
2223 }
2224
2225 // Fetch the record
2226 if (SQLFetch(*hstmtCount) != SQL_SUCCESS)
2227 {
2228 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2229 return(0);
2230 }
2231
2232 // Obtain the result
3ca6a5f0 2233 if (SQLGetData(*hstmtCount, 1, SQL_C_ULONG, &count, sizeof(count), &cb) != SQL_SUCCESS)
89894079
VZ
2234 {
2235 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2236 return(0);
2237 }
2238
2239 // Free the cursor
2240 if (SQLFreeStmt(*hstmtCount, SQL_CLOSE) != SQL_SUCCESS)
2241 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2242
2243 // Return the record count
3ca6a5f0 2244 return(count);
108106cf 2245
f6bcfd97 2246} // wxDbTable::Count()
108106cf 2247
67e9aaa3 2248
f6bcfd97
BP
2249/********** wxDbTable::Refresh() **********/
2250bool wxDbTable::Refresh(void)
108106cf 2251{
89894079
VZ
2252 bool result = TRUE;
2253
2254 // Switch to the internal cursor so any active cursors are not corrupted
2255 HSTMT currCursor = GetCursor();
2256 hstmt = hstmtInternal;
f6bcfd97 2257#if wxODBC_BACKWARD_COMPATABILITY
89894079
VZ
2258 // Save the where and order by clauses
2259 char *saveWhere = where;
2260 char *saveOrderBy = orderBy;
f6bcfd97
BP
2261#else
2262 wxString saveWhere = where;
2263 wxString saveOrderBy = orderBy;
2264#endif
89894079
VZ
2265 // Build a where clause to refetch the record with. Try and use the
2266 // ROWID if it's available, ow use the key fields.
4fdae997
GT
2267 wxString whereClause;
2268 whereClause.Empty();
2269
89894079
VZ
2270 if (CanUpdByROWID())
2271 {
2272 SDWORD cb;
4fdae997 2273 wxChar rowid[wxDB_ROWID_LEN+1];
89894079
VZ
2274
2275 // Get the ROWID value. If not successful retreiving the ROWID,
2276 // simply fall down through the code and build the WHERE clause
2277 // based on the key fields.
f6bcfd97 2278 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, wxDB_ROWID_LEN, &cb) == SQL_SUCCESS)
89894079 2279 {
4fdae997
GT
2280 whereClause += queryTableName;
2281 whereClause += wxT(".ROWID = '");
2282 whereClause += rowid;
2283 whereClause += wxT("'");
89894079
VZ
2284 }
2285 }
2286
2287 // If unable to use the ROWID, build a where clause from the keyfields
2288 if (wxStrlen(whereClause) == 0)
f6bcfd97 2289 BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS, queryTableName);
89894079
VZ
2290
2291 // Requery the record
2292 where = whereClause;
4fdae997 2293 orderBy.Empty();
89894079
VZ
2294 if (!Query())
2295 result = FALSE;
2296
2297 if (result && !GetNext())
2298 result = FALSE;
2299
2300 // Switch back to original cursor
2301 SetCursor(&currCursor);
2302
2303 // Free the internal cursor
2304 if (SQLFreeStmt(hstmtInternal, SQL_CLOSE) != SQL_SUCCESS)
2305 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
2306
2307 // Restore the original where and order by clauses
1e92909e 2308 where = saveWhere;
89894079
VZ
2309 orderBy = saveOrderBy;
2310
2311 return(result);
108106cf 2312
f6bcfd97 2313} // wxDbTable::Refresh()
108106cf 2314
67e9aaa3 2315
f02d4a64
GT
2316/********** wxDbTable::SetColNull(int colNo, bool set) **********/
2317bool wxDbTable::SetColNull(int colNo, bool set)
a2115c88 2318{
89894079 2319 if (colNo < noCols)
f02d4a64
GT
2320 {
2321 colDefs[colNo].Null = set;
2322 if (set) // Blank out the values in the member variable
2323 ClearMemberVar(colNo,FALSE); // Must call with FALSE, or infinite recursion will happen
2324 return(TRUE);
2325 }
89894079
VZ
2326 else
2327 return(FALSE);
a2115c88 2328
4fdae997 2329} // wxDbTable::SetColNull()
67e9aaa3 2330
a2115c88 2331
4fdae997
GT
2332/********** wxDbTable::SetColNull(const wxString &colName, bool set) **********/
2333bool wxDbTable::SetColNull(const wxString &colName, bool set)
a2115c88 2334{
89894079
VZ
2335 int i;
2336 for (i = 0; i < noCols; i++)
2337 {
2338 if (!wxStricmp(colName, colDefs[i].ColName))
2339 break;
2340 }
2341
2342 if (i < noCols)
f02d4a64
GT
2343 {
2344 colDefs[i].Null = set;
2345 if (set) // Blank out the values in the member variable
2346 ClearMemberVar(i,FALSE); // Must call with FALSE, or infinite recursion will happen
2347 return(TRUE);
2348 }
89894079
VZ
2349 else
2350 return(FALSE);
a2115c88 2351
4fdae997 2352} // wxDbTable::SetColNull()
a2115c88 2353
67e9aaa3 2354
f6bcfd97
BP
2355/********** wxDbTable::GetNewCursor() **********/
2356HSTMT *wxDbTable::GetNewCursor(bool setCursor, bool bindColumns)
a2115c88 2357{
89894079 2358 HSTMT *newHSTMT = new HSTMT;
4fdae997 2359 wxASSERT(newHSTMT);
89894079
VZ
2360 if (!newHSTMT)
2361 return(0);
2362
2363 if (SQLAllocStmt(hdbc, newHSTMT) != SQL_SUCCESS)
2364 {
2365 pDb->DispAllErrors(henv, hdbc);
2366 delete newHSTMT;
2367 return(0);
2368 }
2369
2370 if (SQLSetStmtOption(*newHSTMT, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
2371 {
2372 pDb->DispAllErrors(henv, hdbc, *newHSTMT);
2373 delete newHSTMT;
2374 return(0);
2375 }
2376
2377 if (bindColumns)
2378 {
2379 if(!bindCols(*newHSTMT))
2380 {
2381 delete newHSTMT;
2382 return(0);
2383 }
2384 }
2385
2386 if (setCursor)
2387 SetCursor(newHSTMT);
2388
2389 return(newHSTMT);
2390
f6bcfd97 2391} // wxDbTable::GetNewCursor()
67e9aaa3 2392
a2115c88 2393
f6bcfd97
BP
2394/********** wxDbTable::DeleteCursor() **********/
2395bool wxDbTable::DeleteCursor(HSTMT *hstmtDel)
a2115c88 2396{
89894079 2397 bool result = TRUE;
a2115c88 2398
89894079
VZ
2399 if (!hstmtDel) // Cursor already deleted
2400 return(result);
a2115c88 2401
7d8c3dba
GT
2402/*
2403ODBC 3.0 says to use this form
2404 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2405
2406*/
89894079
VZ
2407 if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2408 {
2409 pDb->DispAllErrors(henv, hdbc);
2410 result = FALSE;
2411 }
a2115c88 2412
89894079 2413 delete hstmtDel;
a2115c88 2414
89894079 2415 return(result);
a2115c88 2416
f6bcfd97 2417} // wxDbTable::DeleteCursor()
a2115c88
GT
2418
2419#endif // wxUSE_ODBC
1fc5dd6f 2420