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