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