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