]> git.saurik.com Git - wxWidgets.git/blame - src/common/dbtable.cpp
don't try to remove -g flag from CFLAGS, it is not necessary and doesn't work correct...
[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
e25cdb86
GT
1387 if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR &&
1388 (pDb->Dbms() != dbmsMY_SQL || pDb->GetTypeInfVarchar().TypeName != "text"))// ||
bf5423ea 1389// colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
89894079 1390 {
1e92909e 1391 wxString s;
4fdae997
GT
1392 s.Printf(wxT("(%d)"), colDefs[i].SzDataObj);
1393 sqlStmt += s;
89894079
VZ
1394 }
1395
e93a3a18
GT
1396 if (pDb->Dbms() == dbmsDB2 ||
1397 pDb->Dbms() == dbmsMY_SQL ||
1398 pDb->Dbms() == dbmsSYBASE_ASE ||
9082f1a9 1399 pDb->Dbms() == dbmsINTERBASE ||
e93a3a18 1400 pDb->Dbms() == dbmsMS_SQL_SERVER)
89894079
VZ
1401 {
1402 if (colDefs[i].KeyField)
1403 {
4fdae997 1404 sqlStmt += wxT(" NOT NULL");
89894079
VZ
1405 }
1406 }
da38429d 1407
a144affe 1408 needComma = TRUE;
89894079
VZ
1409 }
1410 // If there is a primary key defined, include it in the create statement
1411 for (i = j = 0; i < noCols; i++)
1412 {
1413 if (colDefs[i].KeyField)
1414 {
1415 j++;
1416 break;
1417 }
1418 }
1419 if (j && pDb->Dbms() != dbmsDBASE) // Found a keyfield
1420 {
87cc3456 1421 switch (pDb->Dbms())
89894079 1422 {
1dee6b39 1423 case dbmsACCESS:
597fadce 1424 case dbmsINFORMIX:
87cc3456
GT
1425 case dbmsSYBASE_ASA:
1426 case dbmsSYBASE_ASE:
1427 case dbmsMY_SQL:
1428 {
2beca662 1429 // MySQL goes out on this one. We also declare the relevant key NON NULL above
87cc3456
GT
1430 sqlStmt += wxT(",PRIMARY KEY (");
1431 break;
1432 }
1433 default:
1434 {
1435 sqlStmt += wxT(",CONSTRAINT ");
2beca662
GT
1436 // DB2 is limited to 18 characters for index names
1437 if (pDb->Dbms() == dbmsDB2)
1438 {
1439 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
1440 sqlStmt += pDb->SQLTableName(tableName.substr(0, 13).c_str());
1441// sqlStmt += tableName.substr(0, 13);
2beca662
GT
1442 }
1443 else
243d4b36
GT
1444 sqlStmt += pDb->SQLTableName(tableName.c_str());
1445// sqlStmt += tableName;
2beca662 1446
87cc3456
GT
1447 sqlStmt += wxT("_PIDX PRIMARY KEY (");
1448 break;
1449 }
89894079
VZ
1450 }
1451
1452 // List column name(s) of column(s) comprising the primary key
1453 for (i = j = 0; i < noCols; i++)
1454 {
1455 if (colDefs[i].KeyField)
1456 {
1457 if (j++) // Multi part key, comma separate names
4fdae997 1458 sqlStmt += wxT(",");
243d4b36 1459 sqlStmt += pDb->SQLColumnName(colDefs[i].ColName);
e25cdb86
GT
1460
1461 if (pDb->Dbms() == dbmsMY_SQL &&
1462 colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)
1463 {
1464 wxString s;
1465 s.Printf(wxT("(%d)"), colDefs[i].SzDataObj);
1466 sqlStmt += s;
1467 }
89894079
VZ
1468 }
1469 }
2beca662
GT
1470 sqlStmt += wxT(")");
1471
597fadce
GT
1472 if (pDb->Dbms() == dbmsINFORMIX ||
1473 pDb->Dbms() == dbmsSYBASE_ASA ||
2beca662
GT
1474 pDb->Dbms() == dbmsSYBASE_ASE)
1475 {
1476 sqlStmt += wxT(" CONSTRAINT ");
243d4b36
GT
1477 sqlStmt += pDb->SQLTableName(tableName);
1478// sqlStmt += tableName;
2beca662
GT
1479 sqlStmt += wxT("_PIDX");
1480 }
89894079
VZ
1481 }
1482 // Append the closing parentheses for the create table statement
4fdae997 1483 sqlStmt += wxT(")");
a2115c88 1484
4fdae997 1485 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1486
a2115c88 1487#ifdef DBDEBUG_CONSOLE
f6bcfd97 1488 cout << endl << sqlStmt.c_str() << endl;
108106cf
JS
1489#endif
1490
89894079 1491 // Execute the CREATE TABLE statement
f6bcfd97 1492 RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
89894079
VZ
1493 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1494 {
1495 pDb->DispAllErrors(henv, hdbc, hstmt);
1496 pDb->RollbackTrans();
1497 CloseCursor(hstmt);
a144affe 1498 return(FALSE);
89894079
VZ
1499 }
1500
1501 // Commit the transaction and close the cursor
3ca6a5f0 1502 if (!pDb->CommitTrans())
a144affe 1503 return(FALSE);
3ca6a5f0 1504 if (!CloseCursor(hstmt))
a144affe 1505 return(FALSE);
89894079
VZ
1506
1507 // Database table created successfully
a144affe 1508 return(TRUE);
108106cf 1509
f6bcfd97 1510} // wxDbTable::CreateTable()
108106cf 1511
67e9aaa3 1512
f6bcfd97
BP
1513/********** wxDbTable::DropTable() **********/
1514bool wxDbTable::DropTable()
a2115c88 1515{
a144affe 1516 // NOTE: This function returns TRUE if the Table does not exist, but
89894079 1517 // only for identified databases. Code will need to be added
0b8410f3 1518 // below for any other databases when those databases are defined
89894079 1519 // to handle this situation consistently
a2115c88 1520
1e92909e 1521 wxString sqlStmt;
a2115c88 1522
1d6f2348
VZ
1523 sqlStmt.Printf(wxT("DROP TABLE %s"),
1524 pDb->SQLTableName(tableName.c_str()).c_str());
a2115c88 1525
4fdae997 1526 pDb->WriteSqlLog(sqlStmt);
a2115c88
GT
1527
1528#ifdef DBDEBUG_CONSOLE
f6bcfd97 1529 cout << endl << sqlStmt.c_str() << endl;
a2115c88
GT
1530#endif
1531
5a226de0 1532 RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
2beca662 1533 if (retcode != SQL_SUCCESS)
89894079
VZ
1534 {
1535 // Check for "Base table not found" error and ignore
da38429d 1536 pDb->GetNextError(henv, hdbc, hstmt);
2beca662 1537 if (wxStrcmp(pDb->sqlState, wxT("S0002")) /*&&
da38429d 1538 wxStrcmp(pDb->sqlState, wxT("S1000"))*/) // "Base table not found"
2beca662 1539 {
89894079 1540 // Check for product specific error codes
2beca662 1541 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // 5.x (and lower?)
da38429d 1542 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
2beca662 1543 (pDb->Dbms() == dbmsPERVASIVE_SQL && !wxStrcmp(pDb->sqlState,wxT("S1000"))) || // Returns an S1000 then an S0002
da38429d 1544 (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))))
89894079
VZ
1545 {
1546 pDb->DispNextError();
1547 pDb->DispAllErrors(henv, hdbc, hstmt);
1548 pDb->RollbackTrans();
5a226de0 1549// CloseCursor(hstmt);
a144affe 1550 return(FALSE);
89894079
VZ
1551 }
1552 }
1553 }
1554
1555 // Commit the transaction and close the cursor
1556 if (! pDb->CommitTrans())
a144affe 1557 return(FALSE);
89894079 1558 if (! CloseCursor(hstmt))
a144affe 1559 return(FALSE);
89894079 1560
a144affe 1561 return(TRUE);
f6bcfd97 1562} // wxDbTable::DropTable()
a2115c88 1563
67e9aaa3 1564
f6bcfd97 1565/********** wxDbTable::CreateIndex() **********/
87cc3456 1566bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCols,
2beca662 1567 wxDbIdxDef *pIdxDefs, bool attemptDrop)
108106cf 1568{
1e92909e 1569 wxString sqlStmt;
89894079
VZ
1570
1571 // Drop the index first
1572 if (attemptDrop && !DropIndex(idxName))
a144affe 1573 return (FALSE);
89894079 1574
3ca6a5f0
BP
1575 // MySQL (and possibly Sybase ASE?? - gt) require that any columns which are used as portions
1576 // of an index have the columns defined as "NOT NULL". During initial table creation though,
1577 // it may not be known which columns are necessarily going to be part of an index (e.g. the
1578 // table was created, then months later you determine that an additional index while
1579 // give better performance, so you want to add an index).
1580 //
1581 // The following block of code will modify the column definition to make the column be
1582 // defined with the "NOT NULL" qualifier.
1583 if (pDb->Dbms() == dbmsMY_SQL)
1584 {
1585 wxString sqlStmt;
1586 int i;
a144affe 1587 bool ok = TRUE;
3ca6a5f0
BP
1588 for (i = 0; i < noIdxCols && ok; i++)
1589 {
1590 int j = 0;
a144affe 1591 bool found = FALSE;
3ca6a5f0
BP
1592 // Find the column definition that has the ColName that matches the
1593 // index column name. We need to do this to get the DB_DATA_TYPE of
1594 // the index column, as MySQL's syntax for the ALTER column requires
1595 // this information
1596 while (!found && (j < this->noCols))
1597 {
1598 if (wxStrcmp(colDefs[j].ColName,pIdxDefs[i].ColName) == 0)
a144affe 1599 found = TRUE;
3ca6a5f0
BP
1600 if (!found)
1601 j++;
1602 }
da38429d 1603
3ca6a5f0
BP
1604 if (found)
1605 {
4fdae997
GT
1606 ok = pDb->ModifyColumn(tableName, pIdxDefs[i].ColName,
1607 colDefs[j].DbDataType, colDefs[j].SzDataObj,
1608 wxT("NOT NULL"));
1609
3ca6a5f0
BP
1610 if (!ok)
1611 {
1612 wxODBC_ERRORS retcode;
1613 // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
1614 // defined to be NOT NULL, but reportedly MySQL doesn't mind.
1615 // This line is just here for debug checking of the value
1616 retcode = (wxODBC_ERRORS)pDb->DB_STATUS;
1617 }
1618 }
1619 else
a144affe 1620 ok = FALSE;
3ca6a5f0
BP
1621 }
1622 if (ok)
1623 pDb->CommitTrans();
1624 else
1625 {
1626 pDb->RollbackTrans();
a144affe 1627 return(FALSE);
3ca6a5f0
BP
1628 }
1629 }
da38429d 1630
89894079 1631 // Build a CREATE INDEX statement
4fdae997 1632 sqlStmt = wxT("CREATE ");
89894079 1633 if (unique)
4fdae997 1634 sqlStmt += wxT("UNIQUE ");
da38429d 1635
4fdae997 1636 sqlStmt += wxT("INDEX ");
243d4b36 1637 sqlStmt += pDb->SQLTableName(idxName);
4fdae997 1638 sqlStmt += wxT(" ON ");
243d4b36
GT
1639
1640 sqlStmt += pDb->SQLTableName(tableName);
1641// sqlStmt += tableName;
4fdae997 1642 sqlStmt += wxT(" (");
da38429d 1643
89894079
VZ
1644 // Append list of columns making up index
1645 int i;
1646 for (i = 0; i < noIdxCols; i++)
1647 {
243d4b36
GT
1648 sqlStmt += pDb->SQLColumnName(pIdxDefs[i].ColName);
1649// sqlStmt += pIdxDefs[i].ColName;
9082f1a9
GT
1650
1651 // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
1652 if (!((pDb->Dbms() == dbmsMS_SQL_SERVER) && (strncmp(pDb->dbInf.dbmsVer,"07",2)==0)) &&
1653 !(pDb->Dbms() == dbmsPOSTGRES))
89894079
VZ
1654 {
1655 if (pIdxDefs[i].Ascending)
4fdae997 1656 sqlStmt += wxT(" ASC");
89894079 1657 else
4fdae997 1658 sqlStmt += wxT(" DESC");
89894079 1659 }
9082f1a9 1660 else
882fc8a9 1661 wxASSERT_MSG(pIdxDefs[i].Ascending, "Datasource does not support DESCending index columns");
89894079
VZ
1662
1663 if ((i + 1) < noIdxCols)
4fdae997 1664 sqlStmt += wxT(",");
89894079 1665 }
da38429d 1666
89894079 1667 // Append closing parentheses
4fdae997 1668 sqlStmt += wxT(")");
89894079 1669
4fdae997 1670 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1671
a2115c88 1672#ifdef DBDEBUG_CONSOLE
f6bcfd97 1673 cout << endl << sqlStmt.c_str() << endl << endl;
108106cf
JS
1674#endif
1675
89894079 1676 // Execute the CREATE INDEX statement
f6bcfd97 1677 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
1678 {
1679 pDb->DispAllErrors(henv, hdbc, hstmt);
1680 pDb->RollbackTrans();
1681 CloseCursor(hstmt);
a144affe 1682 return(FALSE);
89894079 1683 }
108106cf 1684
89894079
VZ
1685 // Commit the transaction and close the cursor
1686 if (! pDb->CommitTrans())
a144affe 1687 return(FALSE);
89894079 1688 if (! CloseCursor(hstmt))
a144affe 1689 return(FALSE);
108106cf 1690
89894079 1691 // Index Created Successfully
a144affe 1692 return(TRUE);
108106cf 1693
f6bcfd97 1694} // wxDbTable::CreateIndex()
108106cf 1695
67e9aaa3 1696
f6bcfd97 1697/********** wxDbTable::DropIndex() **********/
4fdae997 1698bool wxDbTable::DropIndex(const wxString &idxName)
a2115c88 1699{
a144affe 1700 // NOTE: This function returns TRUE if the Index does not exist, but
89894079 1701 // only for identified databases. Code will need to be added
5a226de0 1702 // below for any other databases when those databases are defined
89894079 1703 // to handle this situation consistently
a2115c88 1704
1e92909e 1705 wxString sqlStmt;
a2115c88 1706
5a226de0
GT
1707 if (pDb->Dbms() == dbmsACCESS || pDb->Dbms() == dbmsMY_SQL ||
1708 pDb->Dbms() == dbmsDBASE /*|| Paradox needs this syntax too when we add support*/)
1d6f2348
VZ
1709 sqlStmt.Printf(wxT("DROP INDEX %s ON %s"),
1710 pDb->SQLTableName(idxName.c_str()).c_str(),
6bbff0aa 1711 pDb->SQLTableName(tableName.c_str()).c_str());
e93a3a18
GT
1712 else if ((pDb->Dbms() == dbmsMS_SQL_SERVER) ||
1713 (pDb->Dbms() == dbmsSYBASE_ASE))
1d6f2348
VZ
1714 sqlStmt.Printf(wxT("DROP INDEX %s.%s"),
1715 pDb->SQLTableName(tableName.c_str()).c_str(),
1716 pDb->SQLTableName(idxName.c_str()).c_str());
89894079 1717 else
1d6f2348
VZ
1718 sqlStmt.Printf(wxT("DROP INDEX %s"),
1719 pDb->SQLTableName(idxName.c_str()).c_str());
a2115c88 1720
4fdae997 1721 pDb->WriteSqlLog(sqlStmt);
a2115c88
GT
1722
1723#ifdef DBDEBUG_CONSOLE
f6bcfd97 1724 cout << endl << sqlStmt.c_str() << endl;
a2115c88
GT
1725#endif
1726
f6bcfd97 1727 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
1728 {
1729 // Check for "Index not found" error and ignore
1730 pDb->GetNextError(henv, hdbc, hstmt);
4fdae997 1731 if (wxStrcmp(pDb->sqlState,wxT("S0012"))) // "Index not found"
89894079
VZ
1732 {
1733 // Check for product specific error codes
4fdae997
GT
1734 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,wxT("42000"))) || // v5.x (and lower?)
1735 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
1736 (pDb->Dbms() == dbmsMS_SQL_SERVER && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
9082f1a9 1737 (pDb->Dbms() == dbmsINTERBASE && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
4fdae997
GT
1738 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,wxT("S0002"))) || // Base table not found
1739 (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,wxT("42S12"))) || // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
1740 (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,wxT("08S01")))
3ca6a5f0 1741 ))
89894079
VZ
1742 {
1743 pDb->DispNextError();
1744 pDb->DispAllErrors(henv, hdbc, hstmt);
1745 pDb->RollbackTrans();
1746 CloseCursor(hstmt);
a144affe 1747 return(FALSE);
89894079
VZ
1748 }
1749 }
1750 }
1751
1752 // Commit the transaction and close the cursor
1753 if (! pDb->CommitTrans())
a144affe 1754 return(FALSE);
89894079 1755 if (! CloseCursor(hstmt))
a144affe 1756 return(FALSE);
89894079 1757
a144affe 1758 return(TRUE);
f6bcfd97 1759} // wxDbTable::DropIndex()
a2115c88 1760
67e9aaa3 1761
38cfbffa 1762/********** wxDbTable::SetOrderByColNums() **********/
e938ff5e 1763bool wxDbTable::SetOrderByColNums(UWORD first, ... )
38cfbffa 1764{
2beca662 1765 int colNo = first; // using 'int' to be able to look for wxDB_NO_MORE_COLUN_NUMBERS
38cfbffa
GT
1766 va_list argptr;
1767
a144affe 1768 bool abort = FALSE;
38cfbffa
GT
1769 wxString tempStr;
1770
1771 va_start(argptr, first); /* Initialize variable arguments. */
1772 while (!abort && (colNo != wxDB_NO_MORE_COLUMN_NUMBERS))
1773 {
1774 // Make sure the passed in column number
1775 // is within the valid range of columns
1776 //
1777 // Valid columns are 0 thru noCols-1
1778 if (colNo >= noCols || colNo < 0)
1779 {
a144affe 1780 abort = TRUE;
38cfbffa
GT
1781 continue;
1782 }
1783
1784 if (colNo != first)
4fdae997 1785 tempStr += wxT(",");
38cfbffa
GT
1786
1787 tempStr += colDefs[colNo].ColName;
1788 colNo = va_arg (argptr, int);
1789 }
1790 va_end (argptr); /* Reset variable arguments. */
1791
4fdae997 1792 SetOrderByClause(tempStr);
38cfbffa
GT
1793
1794 return (!abort);
1795} // wxDbTable::SetOrderByColNums()
1796
1797
f6bcfd97
BP
1798/********** wxDbTable::Insert() **********/
1799int wxDbTable::Insert(void)
108106cf 1800{
4fdae997 1801 wxASSERT(!queryOnly);
f6bcfd97 1802 if (queryOnly || !insertable)
89894079
VZ
1803 return(DB_FAILURE);
1804
1805 bindInsertParams();
1806
1807 // Insert the record by executing the already prepared insert statement
1808 RETCODE retcode;
1809 retcode=SQLExecute(hstmtInsert);
1810 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1811 {
1812 // Check to see if integrity constraint was violated
1813 pDb->GetNextError(henv, hdbc, hstmtInsert);
4fdae997 1814 if (! wxStrcmp(pDb->sqlState, wxT("23000"))) // Integrity constraint violated
89894079
VZ
1815 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
1816 else
1817 {
1818 pDb->DispNextError();
1819 pDb->DispAllErrors(henv, hdbc, hstmtInsert);
1820 return(DB_FAILURE);
1821 }
1822 }
1823
1824 // Record inserted into the datasource successfully
1825 return(DB_SUCCESS);
108106cf 1826
f6bcfd97 1827} // wxDbTable::Insert()
108106cf 1828
67e9aaa3 1829
f6bcfd97
BP
1830/********** wxDbTable::Update() **********/
1831bool wxDbTable::Update(void)
108106cf 1832{
4fdae997 1833 wxASSERT(!queryOnly);
89894079 1834 if (queryOnly)
a144affe 1835 return(FALSE);
a2115c88 1836
4fdae997 1837 wxString sqlStmt;
108106cf 1838
89894079 1839 // Build the SQL UPDATE statement
f6bcfd97 1840 BuildUpdateStmt(sqlStmt, DB_UPD_KEYFIELDS);
108106cf 1841
89894079 1842 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1843
a2115c88 1844#ifdef DBDEBUG_CONSOLE
4fdae997 1845 cout << endl << sqlStmt.c_str() << endl << endl;
108106cf
JS
1846#endif
1847
89894079
VZ
1848 // Execute the SQL UPDATE statement
1849 return(execUpdate(sqlStmt));
108106cf 1850
f6bcfd97 1851} // wxDbTable::Update()
108106cf 1852
67e9aaa3 1853
f6bcfd97 1854/********** wxDbTable::Update(pSqlStmt) **********/
4fdae997 1855bool wxDbTable::Update(const wxString &pSqlStmt)
6919c53f 1856{
4fdae997 1857 wxASSERT(!queryOnly);
89894079 1858 if (queryOnly)
a144affe 1859 return(FALSE);
6919c53f 1860
89894079 1861 pDb->WriteSqlLog(pSqlStmt);
6919c53f 1862
89894079 1863 return(execUpdate(pSqlStmt));
6919c53f 1864
f6bcfd97 1865} // wxDbTable::Update(pSqlStmt)
6919c53f 1866
67e9aaa3 1867
f6bcfd97 1868/********** wxDbTable::UpdateWhere() **********/
4fdae997 1869bool wxDbTable::UpdateWhere(const wxString &pWhereClause)
108106cf 1870{
4fdae997 1871 wxASSERT(!queryOnly);
89894079 1872 if (queryOnly)
a144affe 1873 return(FALSE);
a2115c88 1874
4fdae997 1875 wxString sqlStmt;
108106cf 1876
89894079 1877 // Build the SQL UPDATE statement
f6bcfd97 1878 BuildUpdateStmt(sqlStmt, DB_UPD_WHERE, pWhereClause);
108106cf 1879
89894079 1880 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1881
a2115c88 1882#ifdef DBDEBUG_CONSOLE
4fdae997 1883 cout << endl << sqlStmt.c_str() << endl << endl;
108106cf
JS
1884#endif
1885
89894079
VZ
1886 // Execute the SQL UPDATE statement
1887 return(execUpdate(sqlStmt));
108106cf 1888
f6bcfd97 1889} // wxDbTable::UpdateWhere()
108106cf 1890
67e9aaa3 1891
f6bcfd97
BP
1892/********** wxDbTable::Delete() **********/
1893bool wxDbTable::Delete(void)
108106cf 1894{
4fdae997 1895 wxASSERT(!queryOnly);
89894079 1896 if (queryOnly)
a144affe 1897 return(FALSE);
a2115c88 1898
4fdae997
GT
1899 wxString sqlStmt;
1900 sqlStmt.Empty();
108106cf 1901
89894079 1902 // Build the SQL DELETE statement
f6bcfd97 1903 BuildDeleteStmt(sqlStmt, DB_DEL_KEYFIELDS);
108106cf 1904
89894079 1905 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1906
89894079
VZ
1907 // Execute the SQL DELETE statement
1908 return(execDelete(sqlStmt));
108106cf 1909
f6bcfd97 1910} // wxDbTable::Delete()
108106cf 1911
67e9aaa3 1912
f6bcfd97 1913/********** wxDbTable::DeleteWhere() **********/
4fdae997 1914bool wxDbTable::DeleteWhere(const wxString &pWhereClause)
108106cf 1915{
4fdae997 1916 wxASSERT(!queryOnly);
89894079 1917 if (queryOnly)
a144affe 1918 return(FALSE);
a2115c88 1919
4fdae997
GT
1920 wxString sqlStmt;
1921 sqlStmt.Empty();
108106cf 1922
89894079 1923 // Build the SQL DELETE statement
f6bcfd97 1924 BuildDeleteStmt(sqlStmt, DB_DEL_WHERE, pWhereClause);
108106cf 1925
89894079 1926 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1927
89894079
VZ
1928 // Execute the SQL DELETE statement
1929 return(execDelete(sqlStmt));
108106cf 1930
f6bcfd97 1931} // wxDbTable::DeleteWhere()
108106cf 1932
67e9aaa3 1933
f6bcfd97
BP
1934/********** wxDbTable::DeleteMatching() **********/
1935bool wxDbTable::DeleteMatching(void)
108106cf 1936{
4fdae997 1937 wxASSERT(!queryOnly);
89894079 1938 if (queryOnly)
a144affe 1939 return(FALSE);
a2115c88 1940
4fdae997
GT
1941 wxString sqlStmt;
1942 sqlStmt.Empty();
108106cf 1943
89894079 1944 // Build the SQL DELETE statement
f6bcfd97 1945 BuildDeleteStmt(sqlStmt, DB_DEL_MATCHING);
108106cf 1946
89894079 1947 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1948
89894079
VZ
1949 // Execute the SQL DELETE statement
1950 return(execDelete(sqlStmt));
108106cf 1951
f6bcfd97 1952} // wxDbTable::DeleteMatching()
108106cf 1953
67e9aaa3 1954
f6bcfd97 1955/********** wxDbTable::IsColNull() **********/
882fc8a9 1956bool wxDbTable::IsColNull(UWORD colNo) const
108106cf 1957{
f02d4a64 1958/*
a144affe 1959 This logic is just not right. It would indicate TRUE
f02d4a64
GT
1960 if a numeric field were set to a value of 0.
1961
89894079
VZ
1962 switch(colDefs[colNo].SqlCtype)
1963 {
3ca6a5f0
BP
1964 case SQL_C_CHAR:
1965 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1966 case SQL_C_SSHORT:
1967 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1968 case SQL_C_USHORT:
1969 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1970 case SQL_C_SLONG:
1971 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1972 case SQL_C_ULONG:
1973 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1974 case SQL_C_FLOAT:
1975 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1976 case SQL_C_DOUBLE:
1977 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1978 case SQL_C_TIMESTAMP:
1979 TIMESTAMP_STRUCT *pDt;
1980 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1981 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
a144affe 1982 return(TRUE);
3ca6a5f0 1983 else
a144affe 1984 return(FALSE);
3ca6a5f0 1985 default:
a144affe 1986 return(TRUE);
89894079 1987 }
f02d4a64
GT
1988*/
1989 return (colDefs[colNo].Null);
f6bcfd97 1990} // wxDbTable::IsColNull()
108106cf 1991
67e9aaa3 1992
f6bcfd97
BP
1993/********** wxDbTable::CanSelectForUpdate() **********/
1994bool wxDbTable::CanSelectForUpdate(void)
108106cf 1995{
38cfbffa 1996 if (queryOnly)
a144affe 1997 return FALSE;
38cfbffa 1998
89894079 1999 if (pDb->Dbms() == dbmsMY_SQL)
a144affe 2000 return FALSE;
a2115c88 2001
e93a3a18
GT
2002 if ((pDb->Dbms() == dbmsORACLE) ||
2003 (pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE))
a144affe 2004 return(TRUE);
89894079 2005 else
a144affe 2006 return(FALSE);
108106cf 2007
f6bcfd97 2008} // wxDbTable::CanSelectForUpdate()
108106cf 2009
67e9aaa3 2010
f6bcfd97
BP
2011/********** wxDbTable::CanUpdByROWID() **********/
2012bool wxDbTable::CanUpdByROWID(void)
108106cf 2013{
67e9aaa3 2014/*
a144affe 2015 * NOTE: Returning FALSE for now until this can be debugged,
89894079 2016 * as the ROWID is not getting updated correctly
67e9aaa3 2017 */
a144affe 2018 return FALSE;
6b3f4fb8 2019/*
89894079 2020 if (pDb->Dbms() == dbmsORACLE)
a144affe 2021 return(TRUE);
89894079 2022 else
a144affe 2023 return(FALSE);
6b3f4fb8 2024*/
f6bcfd97 2025} // wxDbTable::CanUpdByROWID()
108106cf 2026
67e9aaa3 2027
f6bcfd97
BP
2028/********** wxDbTable::IsCursorClosedOnCommit() **********/
2029bool wxDbTable::IsCursorClosedOnCommit(void)
108106cf 2030{
89894079 2031 if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
a144affe 2032 return(FALSE);
89894079 2033 else
a144affe 2034 return(TRUE);
108106cf 2035
f6bcfd97 2036} // wxDbTable::IsCursorClosedOnCommit()
108106cf 2037
67e9aaa3 2038
f02d4a64
GT
2039
2040/********** wxDbTable::ClearMemberVar() **********/
e938ff5e 2041void wxDbTable::ClearMemberVar(UWORD colNo, bool setToNull)
108106cf 2042{
4fdae997 2043 wxASSERT(colNo < noCols);
f02d4a64
GT
2044
2045 switch(colDefs[colNo].SqlCtype)
89894079 2046 {
f02d4a64
GT
2047 case SQL_C_CHAR:
2048 ((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] = 0;
2049 break;
2050 case SQL_C_SSHORT:
2051 *((SWORD *) colDefs[colNo].PtrDataObj) = 0;
2052 break;
2053 case SQL_C_USHORT:
2054 *((UWORD*) colDefs[colNo].PtrDataObj) = 0;
2055 break;
2056 case SQL_C_SLONG:
2057 *((SDWORD *) colDefs[colNo].PtrDataObj) = 0;
2058 break;
2059 case SQL_C_ULONG:
2060 *((UDWORD *) colDefs[colNo].PtrDataObj) = 0;
2061 break;
2062 case SQL_C_FLOAT:
2063 *((SFLOAT *) colDefs[colNo].PtrDataObj) = 0.0f;
2064 break;
2065 case SQL_C_DOUBLE:
2066 *((SDOUBLE *) colDefs[colNo].PtrDataObj) = 0.0f;
2067 break;
2068 case SQL_C_TIMESTAMP:
2069 TIMESTAMP_STRUCT *pDt;
2070 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
2071 pDt->year = 0;
2072 pDt->month = 0;
2073 pDt->day = 0;
2074 pDt->hour = 0;
2075 pDt->minute = 0;
2076 pDt->second = 0;
2077 pDt->fraction = 0;
2078 break;
89894079 2079 }
108106cf 2080
f02d4a64
GT
2081 if (setToNull)
2082 SetColNull(colNo);
2083} // wxDbTable::ClearMemberVar()
2084
2085
2086/********** wxDbTable::ClearMemberVars() **********/
2087void wxDbTable::ClearMemberVars(bool setToNull)
2088{
2089 int i;
2090
2091 // Loop through the columns setting each member variable to zero
2092 for (i=0; i < noCols; i++)
2093 ClearMemberVar(i,setToNull);
2094
f6bcfd97 2095} // wxDbTable::ClearMemberVars()
108106cf 2096
67e9aaa3 2097
f6bcfd97
BP
2098/********** wxDbTable::SetQueryTimeout() **********/
2099bool wxDbTable::SetQueryTimeout(UDWORD nSeconds)
108106cf 2100{
89894079
VZ
2101 if (SQLSetStmtOption(hstmtInsert, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2102 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
2103 if (SQLSetStmtOption(hstmtUpdate, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2104 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
2105 if (SQLSetStmtOption(hstmtDelete, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2106 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
2107 if (SQLSetStmtOption(hstmtInternal, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
2108 return(pDb->DispAllErrors(henv, hdbc, hstmtInternal));
2109
2110 // Completed Successfully
a144affe 2111 return(TRUE);
108106cf 2112
f6bcfd97 2113} // wxDbTable::SetQueryTimeout()
108106cf 2114
67e9aaa3 2115
f6bcfd97 2116/********** wxDbTable::SetColDefs() **********/
87cc3456 2117void wxDbTable::SetColDefs(UWORD index, const wxString &fieldName, int dataType, void *pData,
6b3f4fb8 2118 SWORD cType, int size, bool keyField, bool upd,
e93a3a18 2119 bool insAllow, bool derivedCol)
108106cf 2120{
89894079
VZ
2121 if (!colDefs) // May happen if the database connection fails
2122 return;
2123
4fdae997 2124 if (fieldName.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
89894079 2125 {
9082f1a9 2126 wxStrncpy(colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
89894079 2127 colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;
da38429d
VZ
2128
2129#ifdef __WXDEBUG__
9082f1a9 2130 wxString tmpMsg;
da38429d
VZ
2131 tmpMsg.Printf(_T("Column name '%s' is too long. Truncated to '%s'."),
2132 fieldName.c_str(),colDefs[index].ColName);
2133 wxFAIL_MSG(tmpMsg);
2134#endif // __WXDEBUG__
89894079
VZ
2135 }
2136 else
2137 wxStrcpy(colDefs[index].ColName, fieldName);
2138
2139 colDefs[index].DbDataType = dataType;
2140 colDefs[index].PtrDataObj = pData;
2141 colDefs[index].SqlCtype = cType;
2142 colDefs[index].SzDataObj = size;
2143 colDefs[index].KeyField = keyField;
2144 colDefs[index].DerivedCol = derivedCol;
2145 // Derived columns by definition would NOT be "Insertable" or "Updateable"
2146 if (derivedCol)
2147 {
a144affe
GT
2148 colDefs[index].Updateable = FALSE;
2149 colDefs[index].InsertAllowed = FALSE;
89894079
VZ
2150 }
2151 else
2152 {
2153 colDefs[index].Updateable = upd;
2154 colDefs[index].InsertAllowed = insAllow;
2155 }
2156
a144affe 2157 colDefs[index].Null = FALSE;
da38429d 2158
f6bcfd97 2159} // wxDbTable::SetColDefs()
108106cf 2160
67e9aaa3 2161
e93a3a18 2162/********** wxDbTable::SetColDefs() **********/
87cc3456 2163wxDbColDataPtr* wxDbTable::SetColDefs(wxDbColInf *pColInfs, UWORD numCols)
67e9aaa3 2164{
4fdae997 2165 wxASSERT(pColInfs);
f6bcfd97 2166 wxDbColDataPtr *pColDataPtrs = NULL;
67e9aaa3 2167
89894079
VZ
2168 if (pColInfs)
2169 {
87cc3456 2170 UWORD index;
da38429d 2171
f6bcfd97 2172 pColDataPtrs = new wxDbColDataPtr[numCols+1];
67e9aaa3
GT
2173
2174 for (index = 0; index < numCols; index++)
89894079 2175 {
89894079
VZ
2176 // Process the fields
2177 switch (pColInfs[index].dbDataType)
2178 {
2179 case DB_DATA_TYPE_VARCHAR:
4fdae997 2180 pColDataPtrs[index].PtrDataObj = new wxChar[pColInfs[index].bufferLength+1];
0b8410f3
GT
2181 pColDataPtrs[index].SzDataObj = pColInfs[index].columnSize;
2182 pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
2183 break;
89894079 2184 case DB_DATA_TYPE_INTEGER:
67e9aaa3 2185 // Can be long or short
89894079
VZ
2186 if (pColInfs[index].bufferLength == sizeof(long))
2187 {
2188 pColDataPtrs[index].PtrDataObj = new long;
2189 pColDataPtrs[index].SzDataObj = sizeof(long);
2190 pColDataPtrs[index].SqlCtype = SQL_C_SLONG;
2191 }
2192 else
2193 {
2194 pColDataPtrs[index].PtrDataObj = new short;
2195 pColDataPtrs[index].SzDataObj = sizeof(short);
2196 pColDataPtrs[index].SqlCtype = SQL_C_SSHORT;
2197 }
2198 break;
89894079 2199 case DB_DATA_TYPE_FLOAT:
89894079
VZ
2200 // Can be float or double
2201 if (pColInfs[index].bufferLength == sizeof(float))
2202 {
2203 pColDataPtrs[index].PtrDataObj = new float;
2204 pColDataPtrs[index].SzDataObj = sizeof(float);
2205 pColDataPtrs[index].SqlCtype = SQL_C_FLOAT;
2206 }
2207 else
2208 {
2209 pColDataPtrs[index].PtrDataObj = new double;
2210 pColDataPtrs[index].SzDataObj = sizeof(double);
2211 pColDataPtrs[index].SqlCtype = SQL_C_DOUBLE;
da38429d 2212 }
89894079 2213 break;
89894079 2214 case DB_DATA_TYPE_DATE:
89894079
VZ
2215 pColDataPtrs[index].PtrDataObj = new TIMESTAMP_STRUCT;
2216 pColDataPtrs[index].SzDataObj = sizeof(TIMESTAMP_STRUCT);
2217 pColDataPtrs[index].SqlCtype = SQL_C_TIMESTAMP;
2218 break;
bf5423ea 2219 case DB_DATA_TYPE_BLOB:
da38429d 2220 wxFAIL_MSG(wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
bf5423ea
GT
2221 pColDataPtrs[index].PtrDataObj = /*BLOB ADDITION NEEDED*/NULL;
2222 pColDataPtrs[index].SzDataObj = /*BLOB ADDITION NEEDED*/sizeof(void *);
2223 pColDataPtrs[index].SqlCtype = SQL_VARBINARY;
2224 break;
2225 }
2226 if (pColDataPtrs[index].PtrDataObj != NULL)
2227 SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
2228 else
2229 {
da38429d 2230 // Unable to build all the column definitions, as either one of
bf5423ea
GT
2231 // the calls to "new" failed above, or there was a BLOB field
2232 // to have a column definition for. If BLOBs are to be used,
2233 // the other form of ::SetColDefs() must be used, as it is impossible
2234 // to know the maximum size to create the PtrDataObj to be.
2235 delete [] pColDataPtrs;
2236 return NULL;
89894079 2237 }
89894079
VZ
2238 }
2239 }
3ca6a5f0 2240
89894079 2241 return (pColDataPtrs);
3ca6a5f0 2242
e93a3a18 2243} // wxDbTable::SetColDefs()
67e9aaa3
GT
2244
2245
f6bcfd97
BP
2246/********** wxDbTable::SetCursor() **********/
2247void wxDbTable::SetCursor(HSTMT *hstmtActivate)
108106cf 2248{
f6bcfd97 2249 if (hstmtActivate == wxDB_DEFAULT_CURSOR)
89894079
VZ
2250 hstmt = *hstmtDefault;
2251 else
2252 hstmt = *hstmtActivate;
108106cf 2253
f6bcfd97 2254} // wxDbTable::SetCursor()
108106cf 2255
67e9aaa3 2256
4fdae997
GT
2257/********** wxDbTable::Count(const wxString &) **********/
2258ULONG wxDbTable::Count(const wxString &args)
108106cf 2259{
3ca6a5f0 2260 ULONG count;
1e92909e 2261 wxString sqlStmt;
89894079
VZ
2262 SDWORD cb;
2263
2264 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
4fdae997 2265 sqlStmt = wxT("SELECT COUNT(");
1e92909e 2266 sqlStmt += args;
4fdae997 2267 sqlStmt += wxT(") FROM ");
243d4b36
GT
2268 sqlStmt += pDb->SQLTableName(queryTableName);
2269// sqlStmt += queryTableName;
f6bcfd97 2270#if wxODBC_BACKWARD_COMPATABILITY
89894079 2271 if (from && wxStrlen(from))
f6bcfd97
BP
2272#else
2273 if (from.Length())
2274#endif
1e92909e 2275 sqlStmt += from;
89894079
VZ
2276
2277 // Add the where clause if one is provided
f6bcfd97 2278#if wxODBC_BACKWARD_COMPATABILITY
89894079 2279 if (where && wxStrlen(where))
f6bcfd97
BP
2280#else
2281 if (where.Length())
2282#endif
89894079 2283 {
4fdae997 2284 sqlStmt += wxT(" WHERE ");
1e92909e 2285 sqlStmt += where;
89894079
VZ
2286 }
2287
4fdae997 2288 pDb->WriteSqlLog(sqlStmt);
89894079
VZ
2289
2290 // Initialize the Count cursor if it's not already initialized
2291 if (!hstmtCount)
2292 {
a144affe 2293 hstmtCount = GetNewCursor(FALSE,FALSE);
4fdae997 2294 wxASSERT(hstmtCount);
89894079
VZ
2295 if (!hstmtCount)
2296 return(0);
2297 }
2298
2299 // Execute the SQL statement
f6bcfd97 2300 if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
89894079
VZ
2301 {
2302 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2303 return(0);
2304 }
2305
2306 // Fetch the record
2307 if (SQLFetch(*hstmtCount) != SQL_SUCCESS)
2308 {
2309 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2310 return(0);
2311 }
2312
2313 // Obtain the result
6b3f4fb8 2314 if (SQLGetData(*hstmtCount, (UWORD)1, SQL_C_ULONG, &count, sizeof(count), &cb) != SQL_SUCCESS)
89894079
VZ
2315 {
2316 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2317 return(0);
2318 }
2319
2320 // Free the cursor
2321 if (SQLFreeStmt(*hstmtCount, SQL_CLOSE) != SQL_SUCCESS)
2322 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
2323
2324 // Return the record count
3ca6a5f0 2325 return(count);
108106cf 2326
f6bcfd97 2327} // wxDbTable::Count()
108106cf 2328
67e9aaa3 2329
f6bcfd97
BP
2330/********** wxDbTable::Refresh() **********/
2331bool wxDbTable::Refresh(void)
108106cf 2332{
a144affe 2333 bool result = TRUE;
89894079
VZ
2334
2335 // Switch to the internal cursor so any active cursors are not corrupted
2336 HSTMT currCursor = GetCursor();
2337 hstmt = hstmtInternal;
f6bcfd97 2338#if wxODBC_BACKWARD_COMPATABILITY
89894079
VZ
2339 // Save the where and order by clauses
2340 char *saveWhere = where;
2341 char *saveOrderBy = orderBy;
f6bcfd97
BP
2342#else
2343 wxString saveWhere = where;
2344 wxString saveOrderBy = orderBy;
2345#endif
89894079
VZ
2346 // Build a where clause to refetch the record with. Try and use the
2347 // ROWID if it's available, ow use the key fields.
4fdae997
GT
2348 wxString whereClause;
2349 whereClause.Empty();
2350
89894079
VZ
2351 if (CanUpdByROWID())
2352 {
2353 SDWORD cb;
4fdae997 2354 wxChar rowid[wxDB_ROWID_LEN+1];
89894079
VZ
2355
2356 // Get the ROWID value. If not successful retreiving the ROWID,
2357 // simply fall down through the code and build the WHERE clause
2358 // based on the key fields.
6b3f4fb8 2359 if (SQLGetData(hstmt, (UWORD)(noCols+1), SQL_C_CHAR, (UCHAR*) rowid, wxDB_ROWID_LEN, &cb) == SQL_SUCCESS)
89894079 2360 {
243d4b36
GT
2361 whereClause += pDb->SQLTableName(queryTableName);
2362// whereClause += queryTableName;
4fdae997
GT
2363 whereClause += wxT(".ROWID = '");
2364 whereClause += rowid;
2365 whereClause += wxT("'");
89894079
VZ
2366 }
2367 }
2368
2369 // If unable to use the ROWID, build a where clause from the keyfields
2370 if (wxStrlen(whereClause) == 0)
f6bcfd97 2371 BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS, queryTableName);
89894079
VZ
2372
2373 // Requery the record
2374 where = whereClause;
4fdae997 2375 orderBy.Empty();
89894079 2376 if (!Query())
a144affe 2377 result = FALSE;
89894079
VZ
2378
2379 if (result && !GetNext())
a144affe 2380 result = FALSE;
89894079
VZ
2381
2382 // Switch back to original cursor
2383 SetCursor(&currCursor);
2384
2385 // Free the internal cursor
2386 if (SQLFreeStmt(hstmtInternal, SQL_CLOSE) != SQL_SUCCESS)
2387 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
2388
2389 // Restore the original where and order by clauses
1e92909e 2390 where = saveWhere;
89894079
VZ
2391 orderBy = saveOrderBy;
2392
2393 return(result);
108106cf 2394
f6bcfd97 2395} // wxDbTable::Refresh()
108106cf 2396
67e9aaa3 2397
e938ff5e
GT
2398/********** wxDbTable::SetColNull() **********/
2399bool wxDbTable::SetColNull(UWORD colNo, bool set)
a2115c88 2400{
89894079 2401 if (colNo < noCols)
f02d4a64
GT
2402 {
2403 colDefs[colNo].Null = set;
2404 if (set) // Blank out the values in the member variable
a144affe
GT
2405 ClearMemberVar(colNo,FALSE); // Must call with FALSE, or infinite recursion will happen
2406 return(TRUE);
f02d4a64 2407 }
89894079 2408 else
a144affe 2409 return(FALSE);
a2115c88 2410
4fdae997 2411} // wxDbTable::SetColNull()
67e9aaa3 2412
a2115c88 2413
e938ff5e 2414/********** wxDbTable::SetColNull() **********/
4fdae997 2415bool wxDbTable::SetColNull(const wxString &colName, bool set)
a2115c88 2416{
89894079
VZ
2417 int i;
2418 for (i = 0; i < noCols; i++)
2419 {
2420 if (!wxStricmp(colName, colDefs[i].ColName))
2421 break;
2422 }
2423
2424 if (i < noCols)
f02d4a64
GT
2425 {
2426 colDefs[i].Null = set;
2427 if (set) // Blank out the values in the member variable
a144affe
GT
2428 ClearMemberVar(i,FALSE); // Must call with FALSE, or infinite recursion will happen
2429 return(TRUE);
f02d4a64 2430 }
89894079 2431 else
a144affe 2432 return(FALSE);
a2115c88 2433
4fdae997 2434} // wxDbTable::SetColNull()
a2115c88 2435
67e9aaa3 2436
f6bcfd97
BP
2437/********** wxDbTable::GetNewCursor() **********/
2438HSTMT *wxDbTable::GetNewCursor(bool setCursor, bool bindColumns)
a2115c88 2439{
89894079 2440 HSTMT *newHSTMT = new HSTMT;
4fdae997 2441 wxASSERT(newHSTMT);
89894079
VZ
2442 if (!newHSTMT)
2443 return(0);
2444
2445 if (SQLAllocStmt(hdbc, newHSTMT) != SQL_SUCCESS)
2446 {
2447 pDb->DispAllErrors(henv, hdbc);
2448 delete newHSTMT;
2449 return(0);
2450 }
2451
2452 if (SQLSetStmtOption(*newHSTMT, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
2453 {
2454 pDb->DispAllErrors(henv, hdbc, *newHSTMT);
2455 delete newHSTMT;
2456 return(0);
2457 }
2458
2459 if (bindColumns)
2460 {
882fc8a9 2461 if (!bindCols(*newHSTMT))
89894079
VZ
2462 {
2463 delete newHSTMT;
2464 return(0);
2465 }
2466 }
2467
2468 if (setCursor)
2469 SetCursor(newHSTMT);
2470
2471 return(newHSTMT);
2472
f6bcfd97 2473} // wxDbTable::GetNewCursor()
67e9aaa3 2474
a2115c88 2475
f6bcfd97
BP
2476/********** wxDbTable::DeleteCursor() **********/
2477bool wxDbTable::DeleteCursor(HSTMT *hstmtDel)
a2115c88 2478{
a144affe 2479 bool result = TRUE;
a2115c88 2480
89894079
VZ
2481 if (!hstmtDel) // Cursor already deleted
2482 return(result);
a2115c88 2483
7d8c3dba
GT
2484/*
2485ODBC 3.0 says to use this form
2486 if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
da38429d 2487
7d8c3dba 2488*/
89894079
VZ
2489 if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2490 {
2491 pDb->DispAllErrors(henv, hdbc);
a144affe 2492 result = FALSE;
89894079 2493 }
a2115c88 2494
89894079 2495 delete hstmtDel;
a2115c88 2496
89894079 2497 return(result);
a2115c88 2498
f6bcfd97 2499} // wxDbTable::DeleteCursor()
a2115c88 2500
882fc8a9
GT
2501//////////////////////////////////////////////////////////////
2502// wxDbGrid support functions
2503//////////////////////////////////////////////////////////////
2504
2505void wxDbTable::SetRowMode(const rowmode_t rowmode)
2506{
2507 if (!m_hstmtGridQuery)
2508 {
a144affe 2509 m_hstmtGridQuery = GetNewCursor(FALSE,FALSE);
882fc8a9
GT
2510 if (!bindCols(*m_hstmtGridQuery))
2511 return;
2512 }
2513
2514 m_rowmode = rowmode;
2515 switch (m_rowmode)
2516 {
2517 case WX_ROW_MODE_QUERY:
2518 SetCursor(m_hstmtGridQuery);
2519 break;
2520 case WX_ROW_MODE_INDIVIDUAL:
2521 SetCursor(hstmtDefault);
2522 break;
2523 default:
2524 assert(0);
2525 }
2526} // wxDbTable::SetRowMode()
2527
2528
1dee6b39 2529wxVariant wxDbTable::GetCol(const int colNo) const
882fc8a9
GT
2530{
2531 wxVariant val;
1dee6b39 2532 if ((colNo < noCols) && (!IsColNull(colNo)))
882fc8a9 2533 {
1dee6b39 2534 switch (colDefs[colNo].SqlCtype)
882fc8a9
GT
2535 {
2536 case SQL_CHAR:
2537 case SQL_VARCHAR:
1dee6b39 2538 val = (wxChar *)(colDefs[colNo].PtrDataObj);
882fc8a9
GT
2539 break;
2540 case SQL_C_LONG:
2541 case SQL_C_SLONG:
1dee6b39 2542 val = *(long *)(colDefs[colNo].PtrDataObj);
882fc8a9
GT
2543 break;
2544 case SQL_C_SHORT:
2545 case SQL_C_SSHORT:
1dee6b39 2546 val = (long int )(*(short *)(colDefs[colNo].PtrDataObj));
882fc8a9
GT
2547 break;
2548 case SQL_C_ULONG:
1dee6b39 2549 val = (long)(*(unsigned long *)(colDefs[colNo].PtrDataObj));
882fc8a9
GT
2550 break;
2551 case SQL_C_TINYINT:
1dee6b39 2552 val = (long)(*(char *)(colDefs[colNo].PtrDataObj));
882fc8a9
GT
2553 break;
2554 case SQL_C_UTINYINT:
1dee6b39 2555 val = (long)(*(unsigned char *)(colDefs[colNo].PtrDataObj));
882fc8a9
GT
2556 break;
2557 case SQL_C_USHORT:
1dee6b39 2558 val = (long)(*(UWORD *)(colDefs[colNo].PtrDataObj));
882fc8a9
GT
2559 break;
2560 case SQL_C_DATE:
1dee6b39 2561 val = (DATE_STRUCT *)(colDefs[colNo].PtrDataObj);
882fc8a9
GT
2562 break;
2563 case SQL_C_TIME:
1dee6b39 2564 val = (TIME_STRUCT *)(colDefs[colNo].PtrDataObj);
882fc8a9
GT
2565 break;
2566 case SQL_C_TIMESTAMP:
1dee6b39 2567 val = (TIMESTAMP_STRUCT *)(colDefs[colNo].PtrDataObj);
882fc8a9
GT
2568 break;
2569 case SQL_C_DOUBLE:
1dee6b39 2570 val = *(double *)(colDefs[colNo].PtrDataObj);
882fc8a9
GT
2571 break;
2572 default:
2573 assert(0);
2574 }
2575 }
2576 return val;
2577} // wxDbTable::GetCol()
2578
2579
2580void csstrncpyt(char *s, const char *t, int n)
2581{
2582 while ((*s++ = *t++) && --n)
2583 {};
2584
2585 *s = '\0';
2586}
2587
1dee6b39 2588void wxDbTable::SetCol(const int colNo, const wxVariant val)
882fc8a9
GT
2589{
2590 //FIXME: Add proper wxDateTime support to wxVariant..
2591 wxDateTime dateval;
2592
1dee6b39 2593 SetColNull(colNo, val.IsNull());
882fc8a9
GT
2594
2595 if (!val.IsNull())
2596 {
1dee6b39
GT
2597 if ((colDefs[colNo].SqlCtype == SQL_C_DATE)
2598 || (colDefs[colNo].SqlCtype == SQL_C_TIME)
2599 || (colDefs[colNo].SqlCtype == SQL_C_TIMESTAMP))
882fc8a9
GT
2600 {
2601 //Returns null if invalid!
2602 if (!dateval.ParseDate(val.GetString()))
1dee6b39 2603 SetColNull(colNo, TRUE);
da38429d 2604 }
882fc8a9 2605
1dee6b39 2606 switch (colDefs[colNo].SqlCtype)
882fc8a9
GT
2607 {
2608 case SQL_CHAR:
2609 case SQL_VARCHAR:
1dee6b39 2610 csstrncpyt((char *)(colDefs[colNo].PtrDataObj),
da38429d 2611 val.GetString().c_str(),
1dee6b39 2612 colDefs[colNo].SzDataObj-1);
882fc8a9
GT
2613 break;
2614 case SQL_C_LONG:
2615 case SQL_C_SLONG:
1dee6b39 2616 *(long *)(colDefs[colNo].PtrDataObj) = val;
882fc8a9
GT
2617 break;
2618 case SQL_C_SHORT:
2619 case SQL_C_SSHORT:
1dee6b39 2620 *(short *)(colDefs[colNo].PtrDataObj) = val.GetLong();
882fc8a9
GT
2621 break;
2622 case SQL_C_ULONG:
1dee6b39 2623 *(unsigned long *)(colDefs[colNo].PtrDataObj) = val.GetLong();
882fc8a9
GT
2624 break;
2625 case SQL_C_TINYINT:
1dee6b39 2626 *(char *)(colDefs[colNo].PtrDataObj) = val.GetChar();
882fc8a9
GT
2627 break;
2628 case SQL_C_UTINYINT:
1dee6b39 2629 *(unsigned char *)(colDefs[colNo].PtrDataObj) = val.GetChar();
882fc8a9
GT
2630 break;
2631 case SQL_C_USHORT:
1dee6b39 2632 *(unsigned short *)(colDefs[colNo].PtrDataObj) = val.GetLong();
882fc8a9
GT
2633 break;
2634 //FIXME: Add proper wxDateTime support to wxVariant..
2635 case SQL_C_DATE:
2636 {
2637 DATE_STRUCT *dataptr =
1dee6b39 2638 (DATE_STRUCT *)colDefs[colNo].PtrDataObj;
da38429d 2639
882fc8a9
GT
2640 dataptr->year = dateval.GetYear();
2641 dataptr->month = dateval.GetMonth()+1;
2642 dataptr->day = dateval.GetDay();
2643 }
2644 break;
2645 case SQL_C_TIME:
2646 {
2647 TIME_STRUCT *dataptr =
1dee6b39 2648 (TIME_STRUCT *)colDefs[colNo].PtrDataObj;
da38429d 2649
882fc8a9
GT
2650 dataptr->hour = dateval.GetHour();
2651 dataptr->minute = dateval.GetMinute();
2652 dataptr->second = dateval.GetSecond();
2653 }
2654 break;
2655 case SQL_C_TIMESTAMP:
2656 {
2657 TIMESTAMP_STRUCT *dataptr =
1dee6b39 2658 (TIMESTAMP_STRUCT *)colDefs[colNo].PtrDataObj;
882fc8a9
GT
2659 dataptr->year = dateval.GetYear();
2660 dataptr->month = dateval.GetMonth()+1;
2661 dataptr->day = dateval.GetDay();
da38429d 2662
882fc8a9
GT
2663 dataptr->hour = dateval.GetHour();
2664 dataptr->minute = dateval.GetMinute();
2665 dataptr->second = dateval.GetSecond();
2666 }
2667 break;
2668 case SQL_C_DOUBLE:
1dee6b39 2669 *(double *)(colDefs[colNo].PtrDataObj) = val;
882fc8a9
GT
2670 break;
2671 default:
2672 assert(0);
2673 } // switch
2674 } // if (!val.IsNull())
2675} // wxDbTable::SetCol()
2676
2677
2678GenericKey wxDbTable::GetKey()
2679{
2680 void *blk;
1dee6b39 2681 wxChar *blkptr;
da38429d 2682
882fc8a9 2683 blk = malloc(m_keysize);
1dee6b39 2684 blkptr = (wxChar *) blk;
da38429d 2685
882fc8a9
GT
2686 int i;
2687 for (i=0; i < noCols; i++)
2688 {
2689 if (colDefs[i].KeyField)
2690 {
2691 memcpy(blkptr,colDefs[i].PtrDataObj, colDefs[i].SzDataObj);
2692 blkptr += colDefs[i].SzDataObj;
2693 }
2694 }
2695
2696 GenericKey k = GenericKey(blk, m_keysize);
2697 free(blk);
2698
2699 return k;
2700} // wxDbTable::GetKey()
2701
2702
2703void wxDbTable::SetKey(const GenericKey& k)
2704{
1dee6b39
GT
2705 void *blk;
2706 wxChar *blkptr;
da38429d 2707
882fc8a9 2708 blk = k.GetBlk();
1dee6b39 2709 blkptr = (wxChar *)blk;
882fc8a9
GT
2710
2711 int i;
2712 for (i=0; i < noCols; i++)
2713 {
2714 if (colDefs[i].KeyField)
2715 {
a144affe 2716 SetColNull(i, FALSE);
882fc8a9
GT
2717 memcpy(colDefs[i].PtrDataObj, blkptr, colDefs[i].SzDataObj);
2718 blkptr += colDefs[i].SzDataObj;
2719 }
2720 }
2721} // wxDbTable::SetKey()
2722
2723
a2115c88 2724#endif // wxUSE_ODBC
1fc5dd6f 2725