]> git.saurik.com Git - wxWidgets.git/blame - src/common/dbtable.cpp
implementation of wxGetPasswordFromUser
[wxWidgets.git] / src / common / dbtable.cpp
CommitLineData
108106cf 1///////////////////////////////////////////////////////////////////////////////
1fc5dd6f 2// Name: dbtable.cpp
108106cf
JS
3// Purpose: Implementation of the wxTable class.
4// Author: Doug Card
67e9aaa3 5// Modified by: George Tasker
89894079
VZ
6// Mods: April 1999
7// -Dynamic cursor support - Only one predefined cursor, as many others as
8// you need may be created on demand
9// -Reduced number of active cursors significantly
10// -Query-Only wxTable objects
108106cf
JS
11// Created: 9.96
12// RCS-ID: $Id$
13// Copyright: (c) 1996 Remstar International, Inc.
14// Licence: wxWindows licence, plus:
89894079 15// Notice: This class library and its intellectual design are free of charge for use,
108106cf
JS
16// modification, enhancement, debugging under the following conditions:
17// 1) These classes may only be used as part of the implementation of a
18// wxWindows-based application
19// 2) All enhancements and bug fixes are to be submitted back to the wxWindows
20// user groups free of all charges for use with the wxWindows library.
21// 3) These classes may not be distributed as part of any other class library,
22// DLL, text (written or electronic), other than a complete distribution of
23// the wxWindows GUI development toolkit.
24///////////////////////////////////////////////////////////////////////////////
25
26/*
27// SYNOPSIS START
28// SYNOPSIS STOP
29*/
30
a2115c88
GT
31// Use this line for wxWindows v1.x
32//#include "wx_ver.h"
33// Use this line for wxWindows v2.x
34#include "wx/version.h"
35#include "wx/wxprec.h"
36
37#if wxMAJOR_VERSION == 2
89894079
VZ
38# ifdef __GNUG__
39# pragma implementation "dbtable.h"
40# endif
108106cf 41#endif
108106cf 42
a2115c88 43#ifdef DBDEBUG_CONSOLE
9199e66f 44 #include <iostream.h>
a2115c88 45#endif
108106cf
JS
46
47#ifdef __BORLANDC__
9199e66f 48 #pragma hdrstop
108106cf
JS
49#endif //__BORLANDC__
50
a2115c88 51#if wxMAJOR_VERSION == 2
9199e66f
RR
52 #ifndef WX_PRECOMP
53 #include "wx/string.h"
54 #include "wx/object.h"
55 #include "wx/list.h"
56 #include "wx/utils.h"
57 #include "wx/msgdlg.h"
89894079
VZ
58 #endif
59 #include "wx/filefn.h"
a2115c88 60#endif
108106cf 61
a2115c88 62#if wxMAJOR_VERSION == 1
89894079
VZ
63# if defined(wx_msw) || defined(wx_x)
64# ifdef WX_PRECOMP
65# include "wx_prec.h"
66# else
67# include "wx.h"
68# endif
69# endif
70# define wxUSE_ODBC 1
a2115c88 71#endif
108106cf 72
a2115c88 73#if wxUSE_ODBC
108106cf
JS
74
75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
1fc5dd6f 78#include <assert.h>
67e9aaa3 79
a2115c88 80#if wxMAJOR_VERSION == 1
89894079 81 #include "table.h"
a2115c88 82#elif wxMAJOR_VERSION == 2
89894079 83 #include "wx/dbtable.h"
a2115c88 84#endif
108106cf 85
1fc5dd6f 86#ifdef __UNIX__
108106cf
JS
87// The HPUX preprocessor lines below were commented out on 8/20/97
88// because macros.h currently redefines DEBUG and is unneeded.
89// # ifdef HPUX
90// # include <macros.h>
91// # endif
1fc5dd6f 92# ifdef LINUX
108106cf
JS
93# include <sys/minmax.h>
94# endif
95#endif
96
a2115c88
GT
97ULONG lastTableID = 0;
98
99
e041ce57 100#ifdef __WXDEBUG__
89894079 101 wxList TablesInUse;
a2115c88
GT
102#endif
103
104
108106cf 105/********** wxTable::wxTable() **********/
a2115c88 106wxTable::wxTable(wxDB *pwxDB, const char *tblName, const int nCols,
89894079 107 const char *qryTblName, bool qryOnly, const char *tblPath)
108106cf 108{
89894079
VZ
109 pDb = pwxDB; // Pointer to the wxDB object
110 henv = 0;
111 hdbc = 0;
112 hstmt = 0;
113 hstmtDefault = 0; // Initialized below
114 hstmtCount = 0; // Initialized first time it is needed
115 hstmtInsert = 0;
116 hstmtDelete = 0;
117 hstmtUpdate = 0;
118 hstmtInternal = 0;
119 colDefs = 0;
120 tableID = 0;
121 noCols = nCols; // No. of cols in the table
122 where = 0; // Where clause
123 orderBy = 0; // Order By clause
124 from = 0; // From clause
125 selectForUpdate = FALSE; // SELECT ... FOR UPDATE; Indicates whether to include the FOR UPDATE phrase
126 queryOnly = qryOnly;
127
128 assert (tblName);
129
130 wxStrcpy(tableName, tblName); // Table Name
131 if (tblPath)
132 wxStrcpy(tablePath, tblPath); // Table Path - used for dBase files
133
134 if (qryTblName) // Name of the table/view to query
135 wxStrcpy(queryTableName, qryTblName);
136 else
137 wxStrcpy(queryTableName, tblName);
138
139 if (!pDb)
140 return;
141
142 pDb->nTables++;
143
144 char s[200];
145 tableID = ++lastTableID;
146 sprintf(s, "wxTable constructor (%-20s) tableID:[%6lu] pDb:[%p]", tblName,tableID,pDb);
a2115c88 147
e041ce57 148#ifdef __WXDEBUG__
89894079
VZ
149 CstructTablesInUse *tableInUse;
150 tableInUse = new CstructTablesInUse();
151 tableInUse->tableName = tblName;
152 tableInUse->tableID = tableID;
153 tableInUse->pDb = pDb;
154 TablesInUse.Append(tableInUse);
a2115c88
GT
155#endif
156
89894079
VZ
157 pDb->WriteSqlLog(s);
158
159 // Grab the HENV and HDBC from the wxDB object
160 henv = pDb->henv;
161 hdbc = pDb->hdbc;
162
163 // Allocate space for column definitions
164 if (noCols)
165 colDefs = new wxColDef[noCols]; // Points to the first column defintion
166
167 // Allocate statement handles for the table
168 if (!queryOnly)
169 {
170 // Allocate a separate statement handle for performing inserts
171 if (SQLAllocStmt(hdbc, &hstmtInsert) != SQL_SUCCESS)
172 pDb->DispAllErrors(henv, hdbc);
173 // Allocate a separate statement handle for performing deletes
174 if (SQLAllocStmt(hdbc, &hstmtDelete) != SQL_SUCCESS)
175 pDb->DispAllErrors(henv, hdbc);
176 // Allocate a separate statement handle for performing updates
177 if (SQLAllocStmt(hdbc, &hstmtUpdate) != SQL_SUCCESS)
178 pDb->DispAllErrors(henv, hdbc);
179 }
180 // Allocate a separate statement handle for internal use
181 if (SQLAllocStmt(hdbc, &hstmtInternal) != SQL_SUCCESS)
182 pDb->DispAllErrors(henv, hdbc);
183
184 // Set the cursor type for the statement handles
185 cursorType = SQL_CURSOR_STATIC;
186 if (SQLSetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
187 {
188 // Check to see if cursor type is supported
189 pDb->GetNextError(henv, hdbc, hstmtInternal);
190 if (! wxStrcmp(pDb->sqlState, "01S02")) // Option Value Changed
191 {
192 // Datasource does not support static cursors. Driver
193 // will substitute a cursor type. Call SQLGetStmtOption()
194 // to determine which cursor type was selected.
195 if (SQLGetStmtOption(hstmtInternal, SQL_CURSOR_TYPE, &cursorType) != SQL_SUCCESS)
196 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
a2115c88 197#ifdef DBDEBUG_CONSOLE
89894079
VZ
198 cout << "Static cursor changed to: ";
199 switch(cursorType)
200 {
201 case SQL_CURSOR_FORWARD_ONLY:
202 cout << "Forward Only"; break;
203 case SQL_CURSOR_STATIC:
204 cout << "Static"; break;
205 case SQL_CURSOR_KEYSET_DRIVEN:
206 cout << "Keyset Driven"; break;
207 case SQL_CURSOR_DYNAMIC:
208 cout << "Dynamic"; break;
209 }
210 cout << endl << endl;
108106cf 211#endif
89894079
VZ
212 }
213 else
214 {
215 pDb->DispNextError();
216 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
217 }
218 }
a2115c88 219#ifdef DBDEBUG_CONSOLE
89894079
VZ
220 else
221 cout << "Cursor Type set to STATIC" << endl << endl;
108106cf
JS
222#endif
223
89894079
VZ
224 if (!queryOnly)
225 {
226 // Set the cursor type for the INSERT statement handle
227 if (SQLSetStmtOption(hstmtInsert, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
228 pDb->DispAllErrors(henv, hdbc, hstmtInsert);
229 // Set the cursor type for the DELETE statement handle
230 if (SQLSetStmtOption(hstmtDelete, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
231 pDb->DispAllErrors(henv, hdbc, hstmtDelete);
232 // Set the cursor type for the UPDATE statement handle
233 if (SQLSetStmtOption(hstmtUpdate, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY) != SQL_SUCCESS)
234 pDb->DispAllErrors(henv, hdbc, hstmtUpdate);
235 }
236
237 // Make the default cursor the active cursor
238 hstmtDefault = NewCursor(FALSE,FALSE);
239 assert(hstmtDefault);
240 hstmt = *hstmtDefault;
108106cf
JS
241
242} // wxTable::wxTable()
243
67e9aaa3 244
108106cf
JS
245/********** wxTable::~wxTable() **********/
246wxTable::~wxTable()
247{
89894079
VZ
248 char s[80];
249 if (pDb)
250 {
251 sprintf(s, "wxTable destructor (%-20s) tableID:[%6lu] pDb:[%p]", tableName,tableID,pDb);
252 pDb->WriteSqlLog(s);
253 }
a2115c88 254
e041ce57 255#ifdef __WXDEBUG__
89894079
VZ
256 if (tableID)
257 {
258 bool found = FALSE;
259 wxNode *pNode;
260 pNode = TablesInUse.First();
261 while (pNode && !found)
262 {
263 if (((CstructTablesInUse *)pNode->Data())->tableID == tableID)
264 {
265 found = TRUE;
266 if (!TablesInUse.DeleteNode(pNode))
267 wxMessageBox (s,"Unable to delete node!");
268 }
269 else
270 pNode = pNode->Next();
271 }
272 if (!found)
273 {
274 char msg[250];
275 sprintf(msg,"Unable to find the tableID in the linked\nlist of tables in use.\n\n%s",s);
276 wxMessageBox (msg,"NOTICE...");
277 }
278 }
a2115c88 279#endif
e041ce57 280
89894079
VZ
281 // Decrement the wxDB table count
282 if (pDb)
283 pDb->nTables--;
284
285 // Delete memory allocated for column definitions
286 if (colDefs)
287 delete [] colDefs;
288
289 // Free statement handles
290 if (!queryOnly)
291 {
292 if (hstmtInsert)
293 if (SQLFreeStmt(hstmtInsert, SQL_DROP) != SQL_SUCCESS)
294 pDb->DispAllErrors(henv, hdbc);
295 if (hstmtDelete)
296 if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
297 pDb->DispAllErrors(henv, hdbc);
298 if (hstmtUpdate)
299 if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
300 pDb->DispAllErrors(henv, hdbc);
301 }
302 if (hstmtInternal)
303 if (SQLFreeStmt(hstmtInternal, SQL_DROP) != SQL_SUCCESS)
304 pDb->DispAllErrors(henv, hdbc);
305
306 // Delete dynamically allocated cursors
307 if (hstmtDefault)
308 DeleteCursor(hstmtDefault);
309 if (hstmtCount)
310 DeleteCursor(hstmtCount);
108106cf
JS
311
312} // wxTable::~wxTable()
313
67e9aaa3
GT
314
315
6919c53f
GT
316/***************************** PRIVATE FUNCTIONS *****************************/
317
67e9aaa3
GT
318
319
6919c53f
GT
320/********** wxTable::bindInsertParams() **********/
321bool wxTable::bindInsertParams(void)
322{
89894079
VZ
323 assert(!queryOnly);
324 if (queryOnly)
325 return(FALSE);
326
327 SWORD fSqlType = 0;
328 UDWORD precision = 0;
329 SWORD scale = 0;
330
331 // Bind each column (that can be inserted) of the table to a parameter marker
332 int i,colNo;
333 for (i = 0, colNo = 1; i < noCols; i++)
334 {
335 if (! colDefs[i].InsertAllowed)
336 continue;
337 switch(colDefs[i].DbDataType)
338 {
339 case DB_DATA_TYPE_VARCHAR:
340 fSqlType = pDb->typeInfVarchar.FsqlType;
341 precision = colDefs[i].SzDataObj;
342 scale = 0;
343 colDefs[i].CbValue = SQL_NTS;
344 break;
345 case DB_DATA_TYPE_INTEGER:
346 fSqlType = pDb->typeInfInteger.FsqlType;
347 precision = pDb->typeInfInteger.Precision;
348 scale = 0;
349 colDefs[i].CbValue = 0;
350 break;
351 case DB_DATA_TYPE_FLOAT:
352 fSqlType = pDb->typeInfFloat.FsqlType;
353 precision = pDb->typeInfFloat.Precision;
354 scale = pDb->typeInfFloat.MaximumScale;
355 // SQL Sybase Anywhere v5.5 returned a negative number for the
356 // MaxScale. This caused ODBC to kick out an error on ibscale.
357 // I check for this here and set the scale = precision.
358 //if (scale < 0)
359 // scale = (short) precision;
360 colDefs[i].CbValue = 0;
361 break;
362 case DB_DATA_TYPE_DATE:
363 fSqlType = pDb->typeInfDate.FsqlType;
364 precision = pDb->typeInfDate.Precision;
365 scale = 0;
366 colDefs[i].CbValue = 0;
367 break;
368 }
369 // Null values
370 if (colDefs[i].Null)
371 {
372 colDefs[i].CbValue = SQL_NULL_DATA;
373 colDefs[i].Null = FALSE;
374 }
375 if (SQLBindParameter(hstmtInsert, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
376 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
377 precision+1,&colDefs[i].CbValue) != SQL_SUCCESS)
378 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
379 }
380
381 // Completed successfully
382 return(TRUE);
6919c53f
GT
383
384} // wxTable::bindInsertParams()
385
67e9aaa3 386
6919c53f
GT
387/********** wxTable::bindUpdateParams() **********/
388bool wxTable::bindUpdateParams(void)
389{
89894079
VZ
390 assert(!queryOnly);
391 if (queryOnly)
392 return(FALSE);
393
394 SWORD fSqlType = 0;
395 UDWORD precision = 0;
396 SWORD scale = 0;
397
398 // Bind each UPDATEABLE column of the table to a parameter marker
399 int i,colNo;
400 for (i = 0, colNo = 1; i < noCols; i++)
401 {
402 if (! colDefs[i].Updateable)
403 continue;
404 switch(colDefs[i].DbDataType)
405 {
406 case DB_DATA_TYPE_VARCHAR:
407 fSqlType = pDb->typeInfVarchar.FsqlType;
408 precision = colDefs[i].SzDataObj;
409 scale = 0;
410 colDefs[i].CbValue = SQL_NTS;
411 break;
412 case DB_DATA_TYPE_INTEGER:
413 fSqlType = pDb->typeInfInteger.FsqlType;
414 precision = pDb->typeInfInteger.Precision;
415 scale = 0;
416 colDefs[i].CbValue = 0;
417 break;
418 case DB_DATA_TYPE_FLOAT:
419 fSqlType = pDb->typeInfFloat.FsqlType;
420 precision = pDb->typeInfFloat.Precision;
421 scale = pDb->typeInfFloat.MaximumScale;
422 // SQL Sybase Anywhere v5.5 returned a negative number for the
423 // MaxScale. This caused ODBC to kick out an error on ibscale.
424 // I check for this here and set the scale = precision.
425 //if (scale < 0)
426 // scale = (short) precision;
427 colDefs[i].CbValue = 0;
428 break;
429 case DB_DATA_TYPE_DATE:
430 fSqlType = pDb->typeInfDate.FsqlType;
431 precision = pDb->typeInfDate.Precision;
432 scale = 0;
433 colDefs[i].CbValue = 0;
434 break;
435 }
436 if (SQLBindParameter(hstmtUpdate, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
437 fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
438 precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
439 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
440 }
441
442 // Completed successfully
443 return(TRUE);
6919c53f
GT
444
445} // wxTable::bindUpdateParams()
446
67e9aaa3 447
6919c53f
GT
448/********** wxTable::bindCols() **********/
449bool wxTable::bindCols(HSTMT cursor)
450{
89894079
VZ
451 static SDWORD cb;
452
453 // Bind each column of the table to a memory address for fetching data
454 int i;
455 for (i = 0; i < noCols; i++)
456 {
457 if (SQLBindCol(cursor, i+1, colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
458 colDefs[i].SzDataObj, &cb) != SQL_SUCCESS)
459 return(pDb->DispAllErrors(henv, hdbc, cursor));
460 }
461
462 // Completed successfully
463 return(TRUE);
6919c53f
GT
464
465} // wxTable::bindCols()
466
67e9aaa3 467
6919c53f
GT
468/********** wxTable::getRec() **********/
469bool wxTable::getRec(UWORD fetchType)
470{
89894079
VZ
471 RETCODE retcode;
472
473 if (!pDb->FwdOnlyCursors())
474 {
475 // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
476 UDWORD cRowsFetched;
477 UWORD rowStatus;
478
479 retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
480 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
481 if (retcode == SQL_NO_DATA_FOUND)
482 return(FALSE);
483 else
484 return(pDb->DispAllErrors(henv, hdbc, hstmt));
485 }
486 else
487 {
488 // Fetch the next record from the record set
489 retcode = SQLFetch(hstmt);
490 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
491 {
492 if (retcode == SQL_NO_DATA_FOUND)
493 return(FALSE);
494 else
495 return(pDb->DispAllErrors(henv, hdbc, hstmt));
496 }
497 }
498
499 // Completed successfully
500 return(TRUE);
6919c53f
GT
501
502} // wxTable::getRec()
503
67e9aaa3 504
6919c53f
GT
505/********** wxTable::execDelete() **********/
506bool wxTable::execDelete(const char *pSqlStmt)
507{
89894079
VZ
508 // Execute the DELETE statement
509 if (SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
510 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
6919c53f 511
89894079
VZ
512 // Record deleted successfully
513 return(TRUE);
6919c53f
GT
514
515} // wxTable::execDelete()
516
67e9aaa3 517
6919c53f
GT
518/********** wxTable::execUpdate() **********/
519bool wxTable::execUpdate(const char *pSqlStmt)
520{
89894079
VZ
521 // Execute the UPDATE statement
522 if (SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt, SQL_NTS) != SQL_SUCCESS)
523 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
6919c53f 524
89894079
VZ
525 // Record deleted successfully
526 return(TRUE);
6919c53f
GT
527
528} // wxTable::execUpdate()
529
67e9aaa3 530
6919c53f
GT
531/********** wxTable::query() **********/
532bool wxTable::query(int queryType, bool forUpdate, bool distinct, char *pSqlStmt)
533{
89894079 534 char sqlStmt[DB_MAX_STATEMENT_LEN];
6919c53f 535
89894079
VZ
536 // Set the selectForUpdate member variable
537 if (forUpdate)
538 // The user may wish to select for update, but the DBMS may not be capable
539 selectForUpdate = CanSelectForUpdate();
540 else
541 selectForUpdate = FALSE;
6919c53f 542
89894079
VZ
543 // Set the SQL SELECT string
544 if (queryType != DB_SELECT_STATEMENT) // A select statement was not passed in,
545 { // so generate a select statement.
546 GetSelectStmt(sqlStmt, queryType, distinct);
547 pDb->WriteSqlLog(sqlStmt);
548 }
6919c53f 549
89894079
VZ
550 // Make sure the cursor is closed first
551 if (! CloseCursor(hstmt))
552 return(FALSE);
6919c53f 553
89894079
VZ
554 // Execute the SQL SELECT statement
555 int retcode;
6919c53f 556
89894079
VZ
557 retcode = SQLExecDirect(hstmt, (UCHAR FAR *) (queryType == DB_SELECT_STATEMENT ? pSqlStmt : sqlStmt), SQL_NTS);
558 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
559 return(pDb->DispAllErrors(henv, hdbc, hstmt));
6919c53f 560
89894079
VZ
561 // Completed successfully
562 return(TRUE);
6919c53f
GT
563
564} // wxTable::query()
565
566
67e9aaa3
GT
567/***************************** PUBLIC FUNCTIONS *****************************/
568
6919c53f 569
108106cf
JS
570/********** wxTable::Open() **********/
571bool wxTable::Open(void)
572{
89894079
VZ
573 if (!pDb)
574 return FALSE;
575
576 int i;
577 char sqlStmt[DB_MAX_STATEMENT_LEN];
578
579 // Verify that the table exists in the database
580 if (!pDb->TableExists(tableName,pDb->GetUsername(),tablePath))
581 {
582 char s[250];
583 if (wxStrcmp(tablePath,""))
584 sprintf(s, "Error opening '%s/%s'.\n",tablePath,tableName);
585 else
586 sprintf(s, "Error opening '%s'.\n", tableName);
587 if (!pDb->TableExists(tableName,NULL,tablePath))
588 wxStrcat(s,"Table/view does not exist in the database.\n");
589 else
590 wxStrcat(s,"Current logged in user does not have sufficient privileges to access this table.\n");
591 pDb->LogError(s);
592 return(FALSE);
593 }
594
595 // Bind the member variables for field exchange between
596 // the wxTable object and the ODBC record.
597 if (!queryOnly)
598 {
599 if (!bindInsertParams()) // Inserts
600 return(FALSE);
601 if (!bindUpdateParams()) // Updates
602 return(FALSE);
603 }
604 if (!bindCols(*hstmtDefault)) // Selects
605 return(FALSE);
606 if (!bindCols(hstmtInternal)) // Internal use only
607 return(FALSE);
608 /*
609 * Do NOT bind the hstmtCount cursor!!!
610 */
611
612 // Build an insert statement using parameter markers
613 if (!queryOnly && noCols > 0)
614 {
615 bool needComma = FALSE;
616 sprintf(sqlStmt, "INSERT INTO %s (", tableName);
617 for (i = 0; i < noCols; i++)
618 {
619 if (! colDefs[i].InsertAllowed)
620 continue;
621 if (needComma)
622 wxStrcat(sqlStmt, ",");
623 wxStrcat(sqlStmt, colDefs[i].ColName);
624 needComma = TRUE;
625 }
626 needComma = FALSE;
627 wxStrcat(sqlStmt, ") VALUES (");
628 for (i = 0; i < noCols; i++)
629 {
630 if (! colDefs[i].InsertAllowed)
631 continue;
632 if (needComma)
633 wxStrcat(sqlStmt, ",");
634 wxStrcat(sqlStmt, "?");
635 needComma = TRUE;
636 }
637 wxStrcat(sqlStmt, ")");
638
639// pDb->WriteSqlLog(sqlStmt);
640
641 // Prepare the insert statement for execution
642 if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
643 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
644 }
645
646 // Completed successfully
647 return(TRUE);
108106cf
JS
648
649} // wxTable::Open()
650
67e9aaa3 651
108106cf
JS
652/********** wxTable::Query() **********/
653bool wxTable::Query(bool forUpdate, bool distinct)
654{
655
89894079 656 return(query(DB_SELECT_WHERE, forUpdate, distinct));
108106cf
JS
657
658} // wxTable::Query()
659
67e9aaa3 660
108106cf
JS
661/********** wxTable::QueryBySqlStmt() **********/
662bool wxTable::QueryBySqlStmt(char *pSqlStmt)
663{
89894079 664 pDb->WriteSqlLog(pSqlStmt);
108106cf 665
89894079 666 return(query(DB_SELECT_STATEMENT, FALSE, FALSE, pSqlStmt));
108106cf
JS
667
668} // wxTable::QueryBySqlStmt()
669
67e9aaa3 670
108106cf
JS
671/********** wxTable::QueryMatching() **********/
672bool wxTable::QueryMatching(bool forUpdate, bool distinct)
673{
674
89894079 675 return(query(DB_SELECT_MATCHING, forUpdate, distinct));
108106cf
JS
676
677} // wxTable::QueryMatching()
678
67e9aaa3 679
108106cf
JS
680/********** wxTable::QueryOnKeyFields() **********/
681bool wxTable::QueryOnKeyFields(bool forUpdate, bool distinct)
682{
683
89894079 684 return(query(DB_SELECT_KEYFIELDS, forUpdate, distinct));
108106cf
JS
685
686} // wxTable::QueryOnKeyFields()
687
67e9aaa3 688
a3439c7d
GT
689/********** wxTable::GetPrev() **********/
690bool wxTable::GetPrev(void)
691{
89894079
VZ
692 if (pDb->FwdOnlyCursors())
693 {
694 wxFAIL_MSG(wxT("GetPrev()::Backward scrolling cursors are not enabled for this instance of wxTable"));
695 return FALSE;
696 }
697 else
698 return(getRec(SQL_FETCH_PRIOR));
a3439c7d
GT
699} // wxTable::GetPrev()
700
67e9aaa3 701
a3439c7d
GT
702/********** wxTable::operator-- **********/
703bool wxTable::operator--(int)
704{
89894079
VZ
705 if (pDb->FwdOnlyCursors())
706 {
707 wxFAIL_MSG(wxT("operator--:Backward scrolling cursors are not enabled for this instance of wxTable"));
708 return FALSE;
709 }
710 else
711 return(getRec(SQL_FETCH_PRIOR));
a3439c7d
GT
712} // wxTable::operator--
713
67e9aaa3 714
a3439c7d
GT
715/********** wxTable::GetFirst() **********/
716bool wxTable::GetFirst(void)
717{
89894079
VZ
718 if (pDb->FwdOnlyCursors())
719 {
720 wxFAIL_MSG(wxT("GetFirst():Backward scrolling cursors are not enabled for this instance of wxTable"));
721 return FALSE;
722 }
723 else
724 return(getRec(SQL_FETCH_FIRST));
a3439c7d
GT
725} // wxTable::GetFirst()
726
67e9aaa3 727
a3439c7d
GT
728/********** wxTable::GetLast() **********/
729bool wxTable::GetLast(void)
730{
89894079
VZ
731 if (pDb->FwdOnlyCursors())
732 {
733 wxFAIL_MSG(wxT("GetLast()::Backward scrolling cursors are not enabled for this instance of wxTable"));
734 return FALSE;
735 }
736 else
737 return(getRec(SQL_FETCH_LAST));
a3439c7d
GT
738} // wxTable::GetLast()
739
67e9aaa3 740
108106cf
JS
741/********** wxTable::GetSelectStmt() **********/
742void wxTable::GetSelectStmt(char *pSqlStmt, int typeOfSelect, bool distinct)
743{
89894079
VZ
744 char whereClause[DB_MAX_WHERE_CLAUSE_LEN];
745
746 whereClause[0] = 0;
747
748 // Build a select statement to query the database
749 wxStrcpy(pSqlStmt, "SELECT ");
750
751 // SELECT DISTINCT values only?
752 if (distinct)
753 wxStrcat(pSqlStmt, "DISTINCT ");
754
755 // Was a FROM clause specified to join tables to the base table?
756 // Available for ::Query() only!!!
757 bool appendFromClause = FALSE;
758 if (typeOfSelect == DB_SELECT_WHERE && from && wxStrlen(from))
759 appendFromClause = TRUE;
760
761 // Add the column list
762 int i;
763 for (i = 0; i < noCols; i++)
764 {
765 // If joining tables, the base table column names must be qualified to avoid ambiguity
766 if (appendFromClause)
767 {
768 wxStrcat(pSqlStmt, queryTableName);
769 wxStrcat(pSqlStmt, ".");
770 }
771 wxStrcat(pSqlStmt, colDefs[i].ColName);
772 if (i + 1 < noCols)
773 wxStrcat(pSqlStmt, ",");
774 }
775
776 // If the datasource supports ROWID, get this column as well. Exception: Don't retrieve
777 // the ROWID if querying distinct records. The rowid will always be unique.
778 if (!distinct && CanUpdByROWID())
779 {
780 // If joining tables, the base table column names must be qualified to avoid ambiguity
781 if (appendFromClause)
782 {
783 wxStrcat(pSqlStmt, ",");
784 wxStrcat(pSqlStmt, queryTableName);
785 wxStrcat(pSqlStmt, ".ROWID");
786 }
787 else
788 wxStrcat(pSqlStmt, ",ROWID");
789 }
790
791 // Append the FROM tablename portion
792 wxStrcat(pSqlStmt, " FROM ");
793 wxStrcat(pSqlStmt, queryTableName);
794
795 // Sybase uses the HOLDLOCK keyword to lock a record during query.
796 // The HOLDLOCK keyword follows the table name in the from clause.
797 // Each table in the from clause must specify HOLDLOCK or
798 // NOHOLDLOCK (the default). Note: The "FOR UPDATE" clause
799 // is parsed but ignored in SYBASE Transact-SQL.
800 if (selectForUpdate && (pDb->Dbms() == dbmsSYBASE_ASA || pDb->Dbms() == dbmsSYBASE_ASE))
801 wxStrcat(pSqlStmt, " HOLDLOCK");
802
803 if (appendFromClause)
804 wxStrcat(pSqlStmt, from);
805
806 // Append the WHERE clause. Either append the where clause for the class
807 // or build a where clause. The typeOfSelect determines this.
808 switch(typeOfSelect)
809 {
810 case DB_SELECT_WHERE:
811 if (where && wxStrlen(where)) // May not want a where clause!!!
812 {
813 wxStrcat(pSqlStmt, " WHERE ");
814 wxStrcat(pSqlStmt, where);
815 }
816 break;
817 case DB_SELECT_KEYFIELDS:
818 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS);
819 if (wxStrlen(whereClause))
820 {
821 wxStrcat(pSqlStmt, " WHERE ");
822 wxStrcat(pSqlStmt, whereClause);
823 }
824 break;
825 case DB_SELECT_MATCHING:
826 GetWhereClause(whereClause, DB_WHERE_MATCHING);
827 if (wxStrlen(whereClause))
828 {
829 wxStrcat(pSqlStmt, " WHERE ");
830 wxStrcat(pSqlStmt, whereClause);
831 }
832 break;
833 }
834
835 // Append the ORDER BY clause
836 if (orderBy && wxStrlen(orderBy))
837 {
838 wxStrcat(pSqlStmt, " ORDER BY ");
839 wxStrcat(pSqlStmt, orderBy);
840 }
841
842 // SELECT FOR UPDATE if told to do so and the datasource is capable. Sybase
843 // parses the FOR UPDATE clause but ignores it. See the comment above on the
844 // HOLDLOCK for Sybase.
845 if (selectForUpdate && CanSelectForUpdate())
846 wxStrcat(pSqlStmt, " FOR UPDATE");
108106cf
JS
847
848} // wxTable::GetSelectStmt()
849
67e9aaa3 850
108106cf
JS
851/********** wxTable::GetRowNum() **********/
852UWORD wxTable::GetRowNum(void)
853{
89894079 854 UDWORD rowNum;
108106cf 855
89894079
VZ
856 if (SQLGetStmtOption(hstmt, SQL_ROW_NUMBER, (UCHAR*) &rowNum) != SQL_SUCCESS)
857 {
858 pDb->DispAllErrors(henv, hdbc, hstmt);
859 return(0);
860 }
108106cf 861
89894079
VZ
862 // Completed successfully
863 return((UWORD) rowNum);
108106cf
JS
864
865} // wxTable::GetRowNum()
866
67e9aaa3 867
108106cf
JS
868/********** wxTable::CloseCursor() **********/
869bool wxTable::CloseCursor(HSTMT cursor)
870{
89894079
VZ
871 if (SQLFreeStmt(cursor, SQL_CLOSE) != SQL_SUCCESS)
872 return(pDb->DispAllErrors(henv, hdbc, cursor));
108106cf 873
89894079
VZ
874 // Completed successfully
875 return(TRUE);
108106cf
JS
876
877} // wxTable::CloseCursor()
878
67e9aaa3 879
108106cf 880/********** wxTable::CreateTable() **********/
a2115c88 881bool wxTable::CreateTable(bool attemptDrop)
108106cf 882{
89894079
VZ
883 if (!pDb)
884 return FALSE;
1fc5dd6f 885
89894079
VZ
886 int i, j;
887 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 888
a2115c88 889#ifdef DBDEBUG_CONSOLE
89894079 890 cout << "Creating Table " << tableName << "..." << endl;
108106cf
JS
891#endif
892
89894079
VZ
893 // Drop table first
894 if (attemptDrop && !DropTable())
895 return FALSE;
108106cf 896
89894079 897 // Create the table
a2115c88 898#ifdef DBDEBUG_CONSOLE
89894079
VZ
899 for (i = 0; i < noCols; i++)
900 {
901 // Exclude derived columns since they are NOT part of the base table
902 if (colDefs[i].DerivedCol)
903 continue;
904 cout << i + 1 << ": " << colDefs[i].ColName << "; ";
905 switch(colDefs[i].DbDataType)
906 {
907 case DB_DATA_TYPE_VARCHAR:
908 cout << pDb->typeInfVarchar.TypeName << "(" << colDefs[i].SzDataObj << ")";
909 break;
910 case DB_DATA_TYPE_INTEGER:
911 cout << pDb->typeInfInteger.TypeName;
912 break;
913 case DB_DATA_TYPE_FLOAT:
914 cout << pDb->typeInfFloat.TypeName;
915 break;
916 case DB_DATA_TYPE_DATE:
917 cout << pDb->typeInfDate.TypeName;
918 break;
919 }
920 cout << endl;
921 }
108106cf
JS
922#endif
923
89894079
VZ
924 // Build a CREATE TABLE string from the colDefs structure.
925 bool needComma = FALSE;
926 sprintf(sqlStmt, "CREATE TABLE %s (", tableName);
927 for (i = 0; i < noCols; i++)
928 {
929 // Exclude derived columns since they are NOT part of the base table
930 if (colDefs[i].DerivedCol)
931 continue;
932 // Comma Delimiter
933 if (needComma)
934 wxStrcat(sqlStmt, ",");
935 // Column Name
936 wxStrcat(sqlStmt, colDefs[i].ColName);
937 wxStrcat(sqlStmt, " ");
938 // Column Type
939 switch(colDefs[i].DbDataType)
940 {
941 case DB_DATA_TYPE_VARCHAR:
942 wxStrcat(sqlStmt, pDb->typeInfVarchar.TypeName); break;
943 case DB_DATA_TYPE_INTEGER:
944 wxStrcat(sqlStmt, pDb->typeInfInteger.TypeName); break;
945 case DB_DATA_TYPE_FLOAT:
946 wxStrcat(sqlStmt, pDb->typeInfFloat.TypeName); break;
947 case DB_DATA_TYPE_DATE:
948 wxStrcat(sqlStmt, pDb->typeInfDate.TypeName); break;
949 }
950 // For varchars, append the size of the string
951 if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)
952 {
953 char s[10];
954 // wxStrcat(sqlStmt, "(");
955 // wxStrcat(sqlStmt, itoa(colDefs[i].SzDataObj, s, 10));
956 // wxStrcat(sqlStmt, ")");
957 sprintf(s, "(%d)", colDefs[i].SzDataObj);
958 wxStrcat(sqlStmt, s);
959 }
960
961 if (pDb->Dbms() == dbmsSYBASE_ASE || pDb->Dbms() == dbmsMY_SQL)
962 {
963 if (colDefs[i].KeyField)
964 {
965 wxStrcat(sqlStmt, " NOT NULL");
966 }
967 }
968
969 needComma = TRUE;
970 }
971 // If there is a primary key defined, include it in the create statement
972 for (i = j = 0; i < noCols; i++)
973 {
974 if (colDefs[i].KeyField)
975 {
976 j++;
977 break;
978 }
979 }
980 if (j && pDb->Dbms() != dbmsDBASE) // Found a keyfield
981 {
982 if (pDb->Dbms() != dbmsMY_SQL)
983 {
984 wxStrcat(sqlStmt, ",CONSTRAINT ");
985 wxStrcat(sqlStmt, tableName);
986 wxStrcat(sqlStmt, "_PIDX PRIMARY KEY (");
987 }
988 else
989 {
990 /* MySQL goes out on this one. We also declare the relevant key NON NULL above */
991 wxStrcat(sqlStmt, ", PRIMARY KEY (");
992 }
993
994 // List column name(s) of column(s) comprising the primary key
995 for (i = j = 0; i < noCols; i++)
996 {
997 if (colDefs[i].KeyField)
998 {
999 if (j++) // Multi part key, comma separate names
1000 wxStrcat(sqlStmt, ",");
1001 wxStrcat(sqlStmt, colDefs[i].ColName);
1002 }
1003 }
1004 wxStrcat(sqlStmt, ")");
1005 }
1006 // Append the closing parentheses for the create table statement
285c163f 1007 wxStrcat(sqlStmt, ")");
a2115c88 1008
89894079 1009 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1010
a2115c88 1011#ifdef DBDEBUG_CONSOLE
89894079 1012 cout << endl << sqlStmt << endl;
108106cf
JS
1013#endif
1014
89894079 1015 // Execute the CREATE TABLE statement
67e9aaa3 1016 RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS);
89894079
VZ
1017 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1018 {
1019 pDb->DispAllErrors(henv, hdbc, hstmt);
1020 pDb->RollbackTrans();
1021 CloseCursor(hstmt);
1022 return(FALSE);
1023 }
1024
1025 // Commit the transaction and close the cursor
1026 if (! pDb->CommitTrans())
1027 return(FALSE);
1028 if (! CloseCursor(hstmt))
1029 return(FALSE);
1030
1031 // Database table created successfully
1032 return(TRUE);
108106cf
JS
1033
1034} // wxTable::CreateTable()
1035
67e9aaa3 1036
a2115c88
GT
1037/********** wxTable::DropTable() **********/
1038bool wxTable::DropTable()
1039{
89894079
VZ
1040 // NOTE: This function returns TRUE if the Table does not exist, but
1041 // only for identified databases. Code will need to be added
1042 // below for any other databases when those databases are defined
1043 // to handle this situation consistently
a2115c88 1044
89894079 1045 char sqlStmt[DB_MAX_STATEMENT_LEN];
a2115c88 1046
89894079 1047 sprintf(sqlStmt, "DROP TABLE %s", tableName);
a2115c88 1048
89894079 1049 pDb->WriteSqlLog(sqlStmt);
a2115c88
GT
1050
1051#ifdef DBDEBUG_CONSOLE
89894079 1052 cout << endl << sqlStmt << endl;
a2115c88
GT
1053#endif
1054
89894079
VZ
1055 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1056 {
1057 // Check for "Base table not found" error and ignore
1058 pDb->GetNextError(henv, hdbc, hstmt);
1059 if (wxStrcmp(pDb->sqlState,"S0002")) // "Base table not found"
1060 {
1061 // Check for product specific error codes
1062 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // 5.x (and lower?)
1063 (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"S1000")) || // untested
1064 (pDb->Dbms() == dbmsPOSTGRES && !wxStrcmp(pDb->sqlState,"08S01")))) // untested
1065 {
1066 pDb->DispNextError();
1067 pDb->DispAllErrors(henv, hdbc, hstmt);
1068 pDb->RollbackTrans();
1069 CloseCursor(hstmt);
1070 return(FALSE);
1071 }
1072 }
1073 }
1074
1075 // Commit the transaction and close the cursor
1076 if (! pDb->CommitTrans())
1077 return(FALSE);
1078 if (! CloseCursor(hstmt))
1079 return(FALSE);
1080
1081 return(TRUE);
a2115c88
GT
1082} // wxTable::DropTable()
1083
67e9aaa3 1084
108106cf 1085/********** wxTable::CreateIndex() **********/
6919c53f 1086bool wxTable::CreateIndex(const char * idxName, bool unique, int noIdxCols, CidxDef *pIdxDefs, bool attemptDrop)
108106cf 1087{
89894079
VZ
1088 char sqlStmt[DB_MAX_STATEMENT_LEN];
1089
1090 // Drop the index first
1091 if (attemptDrop && !DropIndex(idxName))
1092 return (FALSE);
1093
1094 // Build a CREATE INDEX statement
1095 wxStrcpy(sqlStmt, "CREATE ");
1096 if (unique)
1097 wxStrcat(sqlStmt, "UNIQUE ");
1098
1099 wxStrcat(sqlStmt, "INDEX ");
1100 wxStrcat(sqlStmt, idxName);
1101 wxStrcat(sqlStmt, " ON ");
1102 wxStrcat(sqlStmt, tableName);
1103 wxStrcat(sqlStmt, " (");
1104
1105 // Append list of columns making up index
1106 int i;
1107 for (i = 0; i < noIdxCols; i++)
1108 {
1109 wxStrcat(sqlStmt, pIdxDefs[i].ColName);
a2115c88 1110 /* Postgres doesn't cope with ASC */
89894079
VZ
1111 if (pDb->Dbms() != dbmsPOSTGRES)
1112 {
1113 if (pIdxDefs[i].Ascending)
1114 wxStrcat(sqlStmt, " ASC");
1115 else
1116 wxStrcat(sqlStmt, " DESC");
1117 }
1118
1119 if ((i + 1) < noIdxCols)
1120 wxStrcat(sqlStmt, ",");
1121 }
1122
1123 // Append closing parentheses
1124 wxStrcat(sqlStmt, ")");
1125
1126 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1127
a2115c88 1128#ifdef DBDEBUG_CONSOLE
89894079 1129 cout << endl << sqlStmt << endl << endl;
108106cf
JS
1130#endif
1131
89894079
VZ
1132 // Execute the CREATE INDEX statement
1133 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1134 {
1135 pDb->DispAllErrors(henv, hdbc, hstmt);
1136 pDb->RollbackTrans();
1137 CloseCursor(hstmt);
1138 return(FALSE);
1139 }
108106cf 1140
89894079
VZ
1141 // Commit the transaction and close the cursor
1142 if (! pDb->CommitTrans())
1143 return(FALSE);
1144 if (! CloseCursor(hstmt))
1145 return(FALSE);
108106cf 1146
89894079
VZ
1147 // Index Created Successfully
1148 return(TRUE);
108106cf
JS
1149
1150} // wxTable::CreateIndex()
1151
67e9aaa3 1152
a2115c88 1153/********** wxTable::DropIndex() **********/
6919c53f 1154bool wxTable::DropIndex(const char * idxName)
a2115c88 1155{
89894079
VZ
1156 // NOTE: This function returns TRUE if the Index does not exist, but
1157 // only for identified databases. Code will need to be added
1158 // below for any other databases when those databases are defined
1159 // to handle this situation consistently
a2115c88 1160
89894079 1161 char sqlStmt[DB_MAX_STATEMENT_LEN];
a2115c88 1162
89894079
VZ
1163 if (pDb->Dbms() == dbmsACCESS)
1164 sprintf(sqlStmt, "DROP INDEX %s ON %s",idxName,tableName);
1165 else if (pDb->Dbms() == dbmsSYBASE_ASE)
1166 sprintf(sqlStmt, "DROP INDEX %s.%s",tableName,idxName);
1167 else
1168 sprintf(sqlStmt, "DROP INDEX %s",idxName);
a2115c88 1169
89894079 1170 pDb->WriteSqlLog(sqlStmt);
a2115c88
GT
1171
1172#ifdef DBDEBUG_CONSOLE
89894079 1173 cout << endl << sqlStmt << endl;
a2115c88
GT
1174#endif
1175
89894079
VZ
1176 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1177 {
1178 // Check for "Index not found" error and ignore
1179 pDb->GetNextError(henv, hdbc, hstmt);
1180 if (wxStrcmp(pDb->sqlState,"S0012")) // "Index not found"
1181 {
1182 // Check for product specific error codes
1183 if (!((pDb->Dbms() == dbmsSYBASE_ASA && !wxStrcmp(pDb->sqlState,"42000")) || // v5.x (and lower?)
1184 (pDb->Dbms() == dbmsSYBASE_ASE && !wxStrcmp(pDb->sqlState,"S0002")) || // Base table not found
1185 (pDb->Dbms() == dbmsMY_SQL && !wxStrcmp(pDb->sqlState,"42S02")) // untested
1186 ))
1187 {
1188 pDb->DispNextError();
1189 pDb->DispAllErrors(henv, hdbc, hstmt);
1190 pDb->RollbackTrans();
1191 CloseCursor(hstmt);
1192 return(FALSE);
1193 }
1194 }
1195 }
1196
1197 // Commit the transaction and close the cursor
1198 if (! pDb->CommitTrans())
1199 return(FALSE);
1200 if (! CloseCursor(hstmt))
1201 return(FALSE);
1202
1203 return(TRUE);
a2115c88
GT
1204} // wxTable::DropIndex()
1205
67e9aaa3 1206
108106cf
JS
1207/********** wxTable::Insert() **********/
1208int wxTable::Insert(void)
1209{
89894079
VZ
1210 assert(!queryOnly);
1211 if (queryOnly)
1212 return(DB_FAILURE);
1213
1214 bindInsertParams();
1215
1216 // Insert the record by executing the already prepared insert statement
1217 RETCODE retcode;
1218 retcode=SQLExecute(hstmtInsert);
1219 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
1220 {
1221 // Check to see if integrity constraint was violated
1222 pDb->GetNextError(henv, hdbc, hstmtInsert);
1223 if (! wxStrcmp(pDb->sqlState, "23000")) // Integrity constraint violated
1224 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
1225 else
1226 {
1227 pDb->DispNextError();
1228 pDb->DispAllErrors(henv, hdbc, hstmtInsert);
1229 return(DB_FAILURE);
1230 }
1231 }
1232
1233 // Record inserted into the datasource successfully
1234 return(DB_SUCCESS);
108106cf
JS
1235
1236} // wxTable::Insert()
1237
67e9aaa3 1238
108106cf
JS
1239/********** wxTable::Update() **********/
1240bool wxTable::Update(void)
1241{
89894079
VZ
1242 assert(!queryOnly);
1243 if (queryOnly)
1244 return(FALSE);
a2115c88 1245
89894079 1246 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1247
89894079
VZ
1248 // Build the SQL UPDATE statement
1249 GetUpdateStmt(sqlStmt, DB_UPD_KEYFIELDS);
108106cf 1250
89894079 1251 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1252
a2115c88 1253#ifdef DBDEBUG_CONSOLE
89894079 1254 cout << endl << sqlStmt << endl << endl;
108106cf
JS
1255#endif
1256
89894079
VZ
1257 // Execute the SQL UPDATE statement
1258 return(execUpdate(sqlStmt));
108106cf
JS
1259
1260} // wxTable::Update()
1261
67e9aaa3 1262
6919c53f
GT
1263/********** wxTable::Update(pSqlStmt) **********/
1264bool wxTable::Update(const char *pSqlStmt)
1265{
89894079
VZ
1266 assert(!queryOnly);
1267 if (queryOnly)
1268 return(FALSE);
6919c53f 1269
89894079 1270 pDb->WriteSqlLog(pSqlStmt);
6919c53f 1271
89894079 1272 return(execUpdate(pSqlStmt));
6919c53f
GT
1273
1274} // wxTable::Update(pSqlStmt)
1275
67e9aaa3 1276
108106cf 1277/********** wxTable::UpdateWhere() **********/
6919c53f 1278bool wxTable::UpdateWhere(const char *pWhereClause)
108106cf 1279{
89894079
VZ
1280 assert(!queryOnly);
1281 if (queryOnly)
1282 return(FALSE);
a2115c88 1283
89894079 1284 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1285
89894079
VZ
1286 // Build the SQL UPDATE statement
1287 GetUpdateStmt(sqlStmt, DB_UPD_WHERE, pWhereClause);
108106cf 1288
89894079 1289 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1290
a2115c88 1291#ifdef DBDEBUG_CONSOLE
89894079 1292 cout << endl << sqlStmt << endl << endl;
108106cf
JS
1293#endif
1294
89894079
VZ
1295 // Execute the SQL UPDATE statement
1296 return(execUpdate(sqlStmt));
108106cf
JS
1297
1298} // wxTable::UpdateWhere()
1299
67e9aaa3 1300
108106cf
JS
1301/********** wxTable::Delete() **********/
1302bool wxTable::Delete(void)
1303{
89894079
VZ
1304 assert(!queryOnly);
1305 if (queryOnly)
1306 return(FALSE);
a2115c88 1307
89894079 1308 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1309
89894079
VZ
1310 // Build the SQL DELETE statement
1311 GetDeleteStmt(sqlStmt, DB_DEL_KEYFIELDS);
108106cf 1312
89894079 1313 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1314
89894079
VZ
1315 // Execute the SQL DELETE statement
1316 return(execDelete(sqlStmt));
108106cf
JS
1317
1318} // wxTable::Delete()
1319
67e9aaa3 1320
108106cf 1321/********** wxTable::DeleteWhere() **********/
6919c53f 1322bool wxTable::DeleteWhere(const char *pWhereClause)
108106cf 1323{
89894079
VZ
1324 assert(!queryOnly);
1325 if (queryOnly)
1326 return(FALSE);
a2115c88 1327
89894079 1328 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1329
89894079
VZ
1330 // Build the SQL DELETE statement
1331 GetDeleteStmt(sqlStmt, DB_DEL_WHERE, pWhereClause);
108106cf 1332
89894079 1333 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1334
89894079
VZ
1335 // Execute the SQL DELETE statement
1336 return(execDelete(sqlStmt));
108106cf
JS
1337
1338} // wxTable::DeleteWhere()
1339
67e9aaa3 1340
108106cf
JS
1341/********** wxTable::DeleteMatching() **********/
1342bool wxTable::DeleteMatching(void)
1343{
89894079
VZ
1344 assert(!queryOnly);
1345 if (queryOnly)
1346 return(FALSE);
a2115c88 1347
89894079 1348 char sqlStmt[DB_MAX_STATEMENT_LEN];
108106cf 1349
89894079
VZ
1350 // Build the SQL DELETE statement
1351 GetDeleteStmt(sqlStmt, DB_DEL_MATCHING);
108106cf 1352
89894079 1353 pDb->WriteSqlLog(sqlStmt);
1fc5dd6f 1354
89894079
VZ
1355 // Execute the SQL DELETE statement
1356 return(execDelete(sqlStmt));
108106cf
JS
1357
1358} // wxTable::DeleteMatching()
1359
67e9aaa3 1360
108106cf 1361/********** wxTable::GetUpdateStmt() **********/
6919c53f 1362void wxTable::GetUpdateStmt(char *pSqlStmt, int typeOfUpd, const char *pWhereClause)
108106cf 1363{
89894079
VZ
1364 assert(!queryOnly);
1365 if (queryOnly)
1366 return;
1367
1368 char whereClause[DB_MAX_WHERE_CLAUSE_LEN];
1369 bool firstColumn = TRUE;
1370
1371 whereClause[0] = 0;
1372 sprintf(pSqlStmt, "UPDATE %s SET ", tableName);
1373
1374 // Append a list of columns to be updated
1375 int i;
1376 for (i = 0; i < noCols; i++)
1377 {
1378 // Only append Updateable columns
1379 if (colDefs[i].Updateable)
1380 {
1381 if (! firstColumn)
1382 wxStrcat(pSqlStmt, ",");
1383 else
1384 firstColumn = FALSE;
1385 wxStrcat(pSqlStmt, colDefs[i].ColName);
1386 wxStrcat(pSqlStmt, " = ?");
1387 }
1388 }
1389
1390 // Append the WHERE clause to the SQL UPDATE statement
1391 wxStrcat(pSqlStmt, " WHERE ");
1392 switch(typeOfUpd)
1393 {
1394 case DB_UPD_KEYFIELDS:
1395 // If the datasource supports the ROWID column, build
1396 // the where on ROWID for efficiency purposes.
1397 // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
1398 if (CanUpdByROWID())
1399 {
1400 SDWORD cb;
1401 char rowid[ROWID_LEN];
1402
1403 // Get the ROWID value. If not successful retreiving the ROWID,
1404 // simply fall down through the code and build the WHERE clause
1405 // based on the key fields.
1406 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1407 {
1408 wxStrcat(pSqlStmt, "ROWID = '");
1409 wxStrcat(pSqlStmt, rowid);
1410 wxStrcat(pSqlStmt, "'");
1411 break;
1412 }
1413 }
1414 // Unable to delete by ROWID, so build a WHERE
1415 // clause based on the keyfields.
1416 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS);
1417 wxStrcat(pSqlStmt, whereClause);
1418 break;
1419 case DB_UPD_WHERE:
1420 wxStrcat(pSqlStmt, pWhereClause);
1421 break;
1422 }
108106cf
JS
1423} // GetUpdateStmt()
1424
67e9aaa3 1425
108106cf 1426/********** wxTable::GetDeleteStmt() **********/
6919c53f 1427void wxTable::GetDeleteStmt(char *pSqlStmt, int typeOfDel, const char *pWhereClause)
108106cf 1428{
89894079
VZ
1429 assert(!queryOnly);
1430 if (queryOnly)
1431 return;
1432
1433 char whereClause[DB_MAX_WHERE_CLAUSE_LEN];
1434
1435 whereClause[0] = 0;
1436
1437 // Handle the case of DeleteWhere() and the where clause is blank. It should
1438 // delete all records from the database in this case.
1439 if (typeOfDel == DB_DEL_WHERE && (pWhereClause == 0 || wxStrlen(pWhereClause) == 0))
1440 {
1441 sprintf(pSqlStmt, "DELETE FROM %s", tableName);
1442 return;
1443 }
1444
1445 sprintf(pSqlStmt, "DELETE FROM %s WHERE ", tableName);
1446
1447 // Append the WHERE clause to the SQL DELETE statement
1448 switch(typeOfDel)
1449 {
1450 case DB_DEL_KEYFIELDS:
1451 // If the datasource supports the ROWID column, build
1452 // the where on ROWID for efficiency purposes.
1453 // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
1454 if (CanUpdByROWID())
1455 {
1456 SDWORD cb;
1457 char rowid[ROWID_LEN];
1458
1459 // Get the ROWID value. If not successful retreiving the ROWID,
1460 // simply fall down through the code and build the WHERE clause
1461 // based on the key fields.
1462 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1463 {
1464 wxStrcat(pSqlStmt, "ROWID = '");
1465 wxStrcat(pSqlStmt, rowid);
1466 wxStrcat(pSqlStmt, "'");
1467 break;
1468 }
1469 }
1470 // Unable to delete by ROWID, so build a WHERE
1471 // clause based on the keyfields.
1472 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS);
1473 wxStrcat(pSqlStmt, whereClause);
1474 break;
1475 case DB_DEL_WHERE:
1476 wxStrcat(pSqlStmt, pWhereClause);
1477 break;
1478 case DB_DEL_MATCHING:
1479 GetWhereClause(whereClause, DB_WHERE_MATCHING);
1480 wxStrcat(pSqlStmt, whereClause);
1481 break;
1482 }
108106cf
JS
1483
1484} // GetDeleteStmt()
1485
67e9aaa3 1486
108106cf 1487/********** wxTable::GetWhereClause() **********/
67e9aaa3 1488void wxTable::GetWhereClause(char *pWhereClause, int typeOfWhere, const char *qualTableName)
108106cf
JS
1489/*
1490 * Note: GetWhereClause() currently ignores timestamp columns.
1491 * They are not included as part of the where clause.
1492 */
108106cf 1493{
89894079
VZ
1494 bool moreThanOneColumn = FALSE;
1495 char colValue[255];
1496
1497 // Loop through the columns building a where clause as you go
1498 int i;
1499 for (i = 0; i < noCols; i++)
1500 {
1501 // Determine if this column should be included in the WHERE clause
1502 if ((typeOfWhere == DB_WHERE_KEYFIELDS && colDefs[i].KeyField) ||
1503 (typeOfWhere == DB_WHERE_MATCHING && (! IsColNull(i))))
1504 {
1505 // Skip over timestamp columns
1506 if (colDefs[i].SqlCtype == SQL_C_TIMESTAMP)
1507 continue;
1508 // If there is more than 1 column, join them with the keyword "AND"
1509 if (moreThanOneColumn)
1510 wxStrcat(pWhereClause, " AND ");
1511 else
1512 moreThanOneColumn = TRUE;
1513 // Concatenate where phrase for the column
1514 if (qualTableName && wxStrlen(qualTableName))
1515 {
1516 wxStrcat(pWhereClause, qualTableName);
1517 wxStrcat(pWhereClause, ".");
1518 }
1519 wxStrcat(pWhereClause, colDefs[i].ColName);
1520 wxStrcat(pWhereClause, " = ");
1521 switch(colDefs[i].SqlCtype)
1522 {
1523 case SQL_C_CHAR:
1524 sprintf(colValue, "'%s'", (UCHAR FAR *) colDefs[i].PtrDataObj);
1525 break;
1526 case SQL_C_SSHORT:
1527 sprintf(colValue, "%hi", *((SWORD *) colDefs[i].PtrDataObj));
1528 break;
1529 case SQL_C_USHORT:
1530 sprintf(colValue, "%hu", *((UWORD *) colDefs[i].PtrDataObj));
1531 break;
1532 case SQL_C_SLONG:
1533 sprintf(colValue, "%li", *((SDWORD *) colDefs[i].PtrDataObj));
1534 break;
1535 case SQL_C_ULONG:
1536 sprintf(colValue, "%lu", *((UDWORD *) colDefs[i].PtrDataObj));
1537 break;
1538 case SQL_C_FLOAT:
1539 sprintf(colValue, "%.6f", *((SFLOAT *) colDefs[i].PtrDataObj));
1540 break;
1541 case SQL_C_DOUBLE:
1542 sprintf(colValue, "%.6f", *((SDOUBLE *) colDefs[i].PtrDataObj));
1543 break;
1544 }
1545 wxStrcat(pWhereClause, colValue);
1546 }
1547 }
108106cf
JS
1548} // wxTable::GetWhereClause()
1549
67e9aaa3 1550
108106cf
JS
1551/********** wxTable::IsColNull() **********/
1552bool wxTable::IsColNull(int colNo)
1553{
89894079
VZ
1554 switch(colDefs[colNo].SqlCtype)
1555 {
1556 case SQL_C_CHAR:
1557 return(((UCHAR FAR *) colDefs[colNo].PtrDataObj)[0] == 0);
1558 case SQL_C_SSHORT:
1559 return(( *((SWORD *) colDefs[colNo].PtrDataObj)) == 0);
1560 case SQL_C_USHORT:
1561 return(( *((UWORD*) colDefs[colNo].PtrDataObj)) == 0);
1562 case SQL_C_SLONG:
1563 return(( *((SDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1564 case SQL_C_ULONG:
1565 return(( *((UDWORD *) colDefs[colNo].PtrDataObj)) == 0);
1566 case SQL_C_FLOAT:
1567 return(( *((SFLOAT *) colDefs[colNo].PtrDataObj)) == 0);
1568 case SQL_C_DOUBLE:
1569 return((*((SDOUBLE *) colDefs[colNo].PtrDataObj)) == 0);
1570 case SQL_C_TIMESTAMP:
1571 TIMESTAMP_STRUCT *pDt;
1572 pDt = (TIMESTAMP_STRUCT *) colDefs[colNo].PtrDataObj;
1573 if (pDt->year == 0 && pDt->month == 0 && pDt->day == 0)
1574 return(TRUE);
1575 else
1576 return(FALSE);
1577 default:
1578 return(TRUE);
1579 }
108106cf
JS
1580} // wxTable::IsColNull()
1581
67e9aaa3 1582
108106cf 1583/********** wxTable::CanSelectForUpdate() **********/
108106cf
JS
1584bool wxTable::CanSelectForUpdate(void)
1585{
89894079
VZ
1586 if (pDb->Dbms() == dbmsMY_SQL)
1587 return FALSE;
a2115c88 1588
89894079
VZ
1589 if (pDb->dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
1590 return(TRUE);
1591 else
1592 return(FALSE);
108106cf
JS
1593
1594} // wxTable::CanSelectForUpdate()
1595
67e9aaa3 1596
108106cf
JS
1597/********** wxTable::CanUpdByROWID() **********/
1598bool wxTable::CanUpdByROWID(void)
1599{
67e9aaa3
GT
1600/*
1601 * NOTE: Returning FALSE for now until this can be debugged,
89894079 1602 * as the ROWID is not getting updated correctly
67e9aaa3 1603 */
89894079 1604 return FALSE;
108106cf 1605
89894079
VZ
1606 if (pDb->Dbms() == dbmsORACLE)
1607 return(TRUE);
1608 else
1609 return(FALSE);
108106cf
JS
1610
1611} // wxTable::CanUpdByROWID()
1612
67e9aaa3 1613
108106cf
JS
1614/********** wxTable::IsCursorClosedOnCommit() **********/
1615bool wxTable::IsCursorClosedOnCommit(void)
1616{
89894079
VZ
1617 if (pDb->dbInf.cursorCommitBehavior == SQL_CB_PRESERVE)
1618 return(FALSE);
1619 else
1620 return(TRUE);
108106cf
JS
1621
1622} // wxTable::IsCursorClosedOnCommit()
1623
67e9aaa3 1624
108106cf
JS
1625/********** wxTable::ClearMemberVars() **********/
1626void wxTable::ClearMemberVars(void)
1627{
89894079
VZ
1628 // Loop through the columns setting each member variable to zero
1629 int i;
1630 for (i = 0; i < noCols; i++)
1631 {
1632 switch(colDefs[i].SqlCtype)
1633 {
1634 case SQL_C_CHAR:
1635 ((UCHAR FAR *) colDefs[i].PtrDataObj)[0] = 0;
1636 break;
1637 case SQL_C_SSHORT:
1638 *((SWORD *) colDefs[i].PtrDataObj) = 0;
1639 break;
1640 case SQL_C_USHORT:
1641 *((UWORD*) colDefs[i].PtrDataObj) = 0;
1642 break;
1643 case SQL_C_SLONG:
1644 *((SDWORD *) colDefs[i].PtrDataObj) = 0;
1645 break;
1646 case SQL_C_ULONG:
1647 *((UDWORD *) colDefs[i].PtrDataObj) = 0;
1648 break;
1649 case SQL_C_FLOAT:
1650 *((SFLOAT *) colDefs[i].PtrDataObj) = 0.0f;
1651 break;
1652 case SQL_C_DOUBLE:
1653 *((SDOUBLE *) colDefs[i].PtrDataObj) = 0.0f;
1654 break;
1655 case SQL_C_TIMESTAMP:
1656 TIMESTAMP_STRUCT *pDt;
1657 pDt = (TIMESTAMP_STRUCT *) colDefs[i].PtrDataObj;
1658 pDt->year = 0;
1659 pDt->month = 0;
1660 pDt->day = 0;
1661 pDt->hour = 0;
1662 pDt->minute = 0;
1663 pDt->second = 0;
1664 pDt->fraction = 0;
1665 break;
1666 }
1667 }
108106cf
JS
1668
1669} // wxTable::ClearMemberVars()
1670
67e9aaa3 1671
108106cf
JS
1672/********** wxTable::SetQueryTimeout() **********/
1673bool wxTable::SetQueryTimeout(UDWORD nSeconds)
1674{
89894079
VZ
1675 if (SQLSetStmtOption(hstmtInsert, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1676 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
1677 if (SQLSetStmtOption(hstmtUpdate, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1678 return(pDb->DispAllErrors(henv, hdbc, hstmtUpdate));
1679 if (SQLSetStmtOption(hstmtDelete, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1680 return(pDb->DispAllErrors(henv, hdbc, hstmtDelete));
1681 if (SQLSetStmtOption(hstmtInternal, SQL_QUERY_TIMEOUT, nSeconds) != SQL_SUCCESS)
1682 return(pDb->DispAllErrors(henv, hdbc, hstmtInternal));
1683
1684 // Completed Successfully
1685 return(TRUE);
108106cf
JS
1686
1687} // wxTable::SetQueryTimeout()
1688
67e9aaa3 1689
108106cf 1690/********** wxTable::SetColDefs() **********/
6919c53f 1691void wxTable::SetColDefs (int index, const char *fieldName, int dataType, void *pData,
89894079
VZ
1692 int cType, int size, bool keyField, bool upd,
1693 bool insAllow, bool derivedCol)
108106cf 1694{
89894079
VZ
1695 if (!colDefs) // May happen if the database connection fails
1696 return;
1697
1698 if (wxStrlen(fieldName) > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
1699 {
1700 wxStrncpy (colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
1701 colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;
1702 }
1703 else
1704 wxStrcpy(colDefs[index].ColName, fieldName);
1705
1706 colDefs[index].DbDataType = dataType;
1707 colDefs[index].PtrDataObj = pData;
1708 colDefs[index].SqlCtype = cType;
1709 colDefs[index].SzDataObj = size;
1710 colDefs[index].KeyField = keyField;
1711 colDefs[index].DerivedCol = derivedCol;
1712 // Derived columns by definition would NOT be "Insertable" or "Updateable"
1713 if (derivedCol)
1714 {
1715 colDefs[index].Updateable = FALSE;
1716 colDefs[index].InsertAllowed = FALSE;
1717 }
1718 else
1719 {
1720 colDefs[index].Updateable = upd;
1721 colDefs[index].InsertAllowed = insAllow;
1722 }
1723
1724 colDefs[index].Null = FALSE;
1725
108106cf
JS
1726} // wxTable::SetColDefs()
1727
67e9aaa3
GT
1728
1729/********** wxTable::SetColDef() **********/
1b7274a8
BJ
1730// BJO20000121 : changed prototype in order to return proper pointer on wxColDataPtr's array
1731//bool wxTable::SetColDefs(wxColInf *pColInfs, ULONG numCols, wxColDataPtr *pColDataPtrs)
1732wxColDataPtr* wxTable::SetColDefs (wxColInf *pColInfs, ULONG numCols)
67e9aaa3 1733{
89894079
VZ
1734 assert(pColInfs);
1735 wxColDataPtr *pColDataPtrs = NULL;
67e9aaa3 1736
89894079
VZ
1737 if (pColInfs)
1738 {
1739 ULONG index;
67e9aaa3 1740
1b7274a8 1741
89894079 1742 pColDataPtrs = new wxColDataPtr[numCols+1];
67e9aaa3
GT
1743
1744 for (index = 0; index < numCols; index++)
89894079 1745 {
67e9aaa3 1746/*
89894079
VZ
1747 wxString title,msg;
1748 title.sprintf("Catalog: %s, Schema: %s, Table name: %s",pColInfs[index].catalog,pColInfs[index].schema,pColInfs[index].tableName);
1749 msg.sprintf("Column name: %s\nData type: %04d\nType name: %s\nColumn size: %d\nBuffer len: %d\nDecimals:%d\nRadix: %d\nNullable: %d\nRemarks: %s",
1750 pColInfs[index].colName,pColInfs[index].sqlDataType,pColInfs[index].typeName,pColInfs[index].columnSize,pColInfs[index].bufferLength,pColInfs[index].decimalDigits,pColInfs[index].numPrecRadix,pColInfs[index].nullable,pColInfs[index].remarks);
1751 msg += " \nDB_DATA_TYPE: ";
1752 switch(pColInfs[index].dbDataType)
1753 {
1754 case DB_DATA_TYPE_VARCHAR:
1755 msg += pDb->typeInfVarchar.TypeName; break;
1756 case DB_DATA_TYPE_INTEGER:
1757 msg += pDb->typeInfInteger.TypeName; break;
1758 case DB_DATA_TYPE_FLOAT:
1759 msg += pDb->typeInfFloat.TypeName; break;
1760 case DB_DATA_TYPE_DATE:
1761 msg += pDb->typeInfDate.TypeName; break;
1762 }
1763 wxMessageBox(msg.GetData(),title.GetData());
67e9aaa3 1764*/
89894079
VZ
1765 // Process the fields
1766 switch (pColInfs[index].dbDataType)
1767 {
1768 case DB_DATA_TYPE_VARCHAR:
1769 {
1770 pColDataPtrs[index].PtrDataObj = new char[pColInfs[index].bufferLength+1];
1771 pColDataPtrs[index].SzDataObj = pColInfs[index].bufferLength;
1772 pColDataPtrs[index].SqlCtype = SQL_C_CHAR;
1773 break;
1774 }
1775 case DB_DATA_TYPE_INTEGER:
1776 {
67e9aaa3 1777 // Can be long or short
89894079
VZ
1778 if (pColInfs[index].bufferLength == sizeof(long))
1779 {
1780 pColDataPtrs[index].PtrDataObj = new long;
1781 pColDataPtrs[index].SzDataObj = sizeof(long);
1782 pColDataPtrs[index].SqlCtype = SQL_C_SLONG;
1783 }
1784 else
1785 {
1786 pColDataPtrs[index].PtrDataObj = new short;
1787 pColDataPtrs[index].SzDataObj = sizeof(short);
1788 pColDataPtrs[index].SqlCtype = SQL_C_SSHORT;
1789 }
1790 break;
1791 }
1792 case DB_DATA_TYPE_FLOAT:
1793 {
1794 // Can be float or double
1795 if (pColInfs[index].bufferLength == sizeof(float))
1796 {
1797 pColDataPtrs[index].PtrDataObj = new float;
1798 pColDataPtrs[index].SzDataObj = sizeof(float);
1799 pColDataPtrs[index].SqlCtype = SQL_C_FLOAT;
1800 }
1801 else
1802 {
1803 pColDataPtrs[index].PtrDataObj = new double;
1804 pColDataPtrs[index].SzDataObj = sizeof(double);
1805 pColDataPtrs[index].SqlCtype = SQL_C_DOUBLE;
1806 }
1807 break;
1808 }
1809 case DB_DATA_TYPE_DATE:
1810 {
1811 pColDataPtrs[index].PtrDataObj = new TIMESTAMP_STRUCT;
1812 pColDataPtrs[index].SzDataObj = sizeof(TIMESTAMP_STRUCT);
1813 pColDataPtrs[index].SqlCtype = SQL_C_TIMESTAMP;
1814 break;
1815 }
1816 }
67e9aaa3
GT
1817
1818 SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
89894079
VZ
1819 }
1820 }
1821 return (pColDataPtrs);
67e9aaa3
GT
1822} // wxTable::SetColDef()
1823
1824
108106cf 1825/********** wxTable::SetCursor() **********/
a2115c88 1826void wxTable::SetCursor(HSTMT *hstmtActivate)
108106cf 1827{
89894079
VZ
1828 if (hstmtActivate == DEFAULT_CURSOR)
1829 hstmt = *hstmtDefault;
1830 else
1831 hstmt = *hstmtActivate;
108106cf
JS
1832
1833} // wxTable::SetCursor()
1834
67e9aaa3 1835
285c163f
GT
1836/********** wxTable::Count(const char *) **********/
1837ULONG wxTable::Count(const char *args)
108106cf 1838{
89894079
VZ
1839 ULONG l;
1840 char sqlStmt[DB_MAX_STATEMENT_LEN];
1841 SDWORD cb;
1842
1843 // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
1844 wxStrcpy(sqlStmt, "SELECT COUNT(");
1845 wxStrcat(sqlStmt, args);
1846 wxStrcat(sqlStmt, ") FROM ");
1847 wxStrcat(sqlStmt, queryTableName);
1848
1849 if (from && wxStrlen(from))
1850 wxStrcat(sqlStmt, from);
1851
1852 // Add the where clause if one is provided
1853 if (where && wxStrlen(where))
1854 {
1855 wxStrcat(sqlStmt, " WHERE ");
1856 wxStrcat(sqlStmt, where);
1857 }
1858
1859 pDb->WriteSqlLog(sqlStmt);
1860
1861 // Initialize the Count cursor if it's not already initialized
1862 if (!hstmtCount)
1863 {
1864 hstmtCount = NewCursor(FALSE,FALSE);
1865 assert(hstmtCount);
1866 if (!hstmtCount)
1867 return(0);
1868 }
1869
1870 // Execute the SQL statement
1871 if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1872 {
1873 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1874 return(0);
1875 }
1876
1877 // Fetch the record
1878 if (SQLFetch(*hstmtCount) != SQL_SUCCESS)
1879 {
1880 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1881 return(0);
1882 }
1883
1884 // Obtain the result
1885 if (SQLGetData(*hstmtCount, 1, SQL_C_ULONG, &l, sizeof(l), &cb) != SQL_SUCCESS)
1886 {
1887 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1888 return(0);
1889 }
1890
1891 // Free the cursor
1892 if (SQLFreeStmt(*hstmtCount, SQL_CLOSE) != SQL_SUCCESS)
1893 pDb->DispAllErrors(henv, hdbc, *hstmtCount);
1894
1895 // Return the record count
1896 return(l);
108106cf
JS
1897
1898} // wxTable::Count()
1899
67e9aaa3 1900
108106cf
JS
1901/********** wxTable::Refresh() **********/
1902bool wxTable::Refresh(void)
1903{
89894079
VZ
1904 bool result = TRUE;
1905
1906 // Switch to the internal cursor so any active cursors are not corrupted
1907 HSTMT currCursor = GetCursor();
1908 hstmt = hstmtInternal;
1909
1910 // Save the where and order by clauses
1911 char *saveWhere = where;
1912 char *saveOrderBy = orderBy;
1913
1914 // Build a where clause to refetch the record with. Try and use the
1915 // ROWID if it's available, ow use the key fields.
1916 char whereClause[DB_MAX_WHERE_CLAUSE_LEN+1];
1917 wxStrcpy(whereClause, "");
1918 if (CanUpdByROWID())
1919 {
1920 SDWORD cb;
1921 char rowid[ROWID_LEN+1];
1922
1923 // Get the ROWID value. If not successful retreiving the ROWID,
1924 // simply fall down through the code and build the WHERE clause
1925 // based on the key fields.
1926 if (SQLGetData(hstmt, noCols+1, SQL_C_CHAR, (UCHAR*) rowid, ROWID_LEN, &cb) == SQL_SUCCESS)
1927 {
1928 wxStrcat(whereClause, queryTableName);
1929 wxStrcat(whereClause, ".ROWID = '");
1930 wxStrcat(whereClause, rowid);
1931 wxStrcat(whereClause, "'");
1932 }
1933 }
1934
1935 // If unable to use the ROWID, build a where clause from the keyfields
1936 if (wxStrlen(whereClause) == 0)
1937 GetWhereClause(whereClause, DB_WHERE_KEYFIELDS, queryTableName);
1938
1939 // Requery the record
1940 where = whereClause;
1941 orderBy = 0;
1942 if (!Query())
1943 result = FALSE;
1944
1945 if (result && !GetNext())
1946 result = FALSE;
1947
1948 // Switch back to original cursor
1949 SetCursor(&currCursor);
1950
1951 // Free the internal cursor
1952 if (SQLFreeStmt(hstmtInternal, SQL_CLOSE) != SQL_SUCCESS)
1953 pDb->DispAllErrors(henv, hdbc, hstmtInternal);
1954
1955 // Restore the original where and order by clauses
1956 where = saveWhere;
1957 orderBy = saveOrderBy;
1958
1959 return(result);
108106cf
JS
1960
1961} // wxTable::Refresh()
1962
67e9aaa3
GT
1963
1964/********** wxTable::SetNull(int colNo) **********/
a2115c88
GT
1965bool wxTable::SetNull(int colNo)
1966{
89894079
VZ
1967 if (colNo < noCols)
1968 return(colDefs[colNo].Null = TRUE);
1969 else
1970 return(FALSE);
a2115c88 1971
67e9aaa3
GT
1972} // wxTable::SetNull(int colNo)
1973
a2115c88
GT
1974
1975/********** wxTable::SetNull(char *colName) **********/
6919c53f 1976bool wxTable::SetNull(const char *colName)
a2115c88 1977{
89894079
VZ
1978 int i;
1979 for (i = 0; i < noCols; i++)
1980 {
1981 if (!wxStricmp(colName, colDefs[i].ColName))
1982 break;
1983 }
1984
1985 if (i < noCols)
1986 return(colDefs[i].Null = TRUE);
1987 else
1988 return(FALSE);
a2115c88
GT
1989
1990} // wxTable::SetNull(char *colName)
1991
67e9aaa3 1992
a2115c88
GT
1993/********** wxTable::NewCursor() **********/
1994HSTMT *wxTable::NewCursor(bool setCursor, bool bindColumns)
1995{
89894079
VZ
1996 HSTMT *newHSTMT = new HSTMT;
1997 assert(newHSTMT);
1998 if (!newHSTMT)
1999 return(0);
2000
2001 if (SQLAllocStmt(hdbc, newHSTMT) != SQL_SUCCESS)
2002 {
2003 pDb->DispAllErrors(henv, hdbc);
2004 delete newHSTMT;
2005 return(0);
2006 }
2007
2008 if (SQLSetStmtOption(*newHSTMT, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
2009 {
2010 pDb->DispAllErrors(henv, hdbc, *newHSTMT);
2011 delete newHSTMT;
2012 return(0);
2013 }
2014
2015 if (bindColumns)
2016 {
2017 if(!bindCols(*newHSTMT))
2018 {
2019 delete newHSTMT;
2020 return(0);
2021 }
2022 }
2023
2024 if (setCursor)
2025 SetCursor(newHSTMT);
2026
2027 return(newHSTMT);
2028
2029} // wxTable::NewCursor()
67e9aaa3 2030
a2115c88
GT
2031
2032/********** wxTable::DeleteCursor() **********/
2033bool wxTable::DeleteCursor(HSTMT *hstmtDel)
2034{
89894079 2035 bool result = TRUE;
a2115c88 2036
89894079
VZ
2037 if (!hstmtDel) // Cursor already deleted
2038 return(result);
a2115c88 2039
89894079
VZ
2040 if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
2041 {
2042 pDb->DispAllErrors(henv, hdbc);
2043 result = FALSE;
2044 }
a2115c88 2045
89894079 2046 delete hstmtDel;
a2115c88 2047
89894079 2048 return(result);
a2115c88
GT
2049
2050} // wxTable::DeleteCursor()
2051
2052#endif // wxUSE_ODBC
1fc5dd6f 2053