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