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