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