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