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