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