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