-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtclodbc.hxx
549 lines (428 loc) · 17.5 KB
/
tclodbc.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/**********************************************************************
- TCLODBC COPYRIGHT NOTICE -
This software is copyrighted by Roy Nurmi, contact address by email at
Roy.Nurmi@iki.fi. The following terms apply to all files associated with
the software unless explicitly disclaimed in individual files.
The author hereby grant permission to use, copy, modify, distribute,
and license this software and its documentation for any purpose, provided
that existing copyright notices are retained in all copies and that this
notice is included verbatim in any distributions. No written agreement,
license, or royalty fee is required for any of the authorized uses.
Modifications to this software may be copyrighted by their authors
and need not follow the licensing terms described here, provided that
the new terms are clearly indicated on the first page of each file where
they apply.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
GOVERNMENT USE: If you are acquiring this software on behalf of the
U.S. government, the Government shall have only "Restricted Rights"
in the software and related documentation as defined in the Federal
Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
are acquiring the software on behalf of the Department of Defense, the
software shall be classified as "Commercial Computer Software" and the
Government shall have only "Restricted Rights" as defined in Clause
252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
authors grant the U.S. Government and others acting in its behalf
permission to use and distribute the software in accordance with the
terms specified in this license.
***********************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// ODBC-interface extension to tcl language by Roy Nurmi //
// Version 2.1 //
// //
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// //
// VERSION HISTORY //
// //
// 1.0 Initial version. Requires tcl 8.0 alpha 2 release //
// //
// 1.1 New version, compatible with tcl 8.0 beta 1 release //
// //
// 1.2 New version, enhanced functionality of statement columns command //
// //
// 1.3 New version, added database configure option, index queries, //
// new error object definition //
// //
// 1.4 New version, added database datasources and drivers options //
// and connecting with odbc connection string //
// //
// 1.4 patch 1 Bug corrections, enhanced portability //
// //
// 1.5 New version, added set and get commands for setting some //
// useful connection and statement options //
// //
// 1.6 New version, argument type definition moved to statement //
// creation. //
// //
// 1.7 New version, compilable also under tcl 7.6 //
// //
// 2.0 New version, cursor types & rowset_size also configurable //
// contributed by Ric Klaren (klaren@trc.nl). //
// optional array argument to statement fetch command //
// thread safety provided //
// Unicode support with tcl 8.1 //
// Emprovements in large columns support //
// //
// 2.1 New version, added database typeinfo and statement rowcount //
// //
// 2.2 New version, new binary column handling and some bug fixes //
// some contributions from Robert Black //
// //
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// STANDARD INCLUDES
//
extern "C" {
#include <string.h>
#include <stdlib.h>
#include <tcl.h>
}
#ifdef WIN32
#include <windows.h>
#define INCLUDE_EXTENSIONS
#else
#define _declspec(x) int
#ifdef HAVE_UNIXODBC
#define INCLUDE_EXTENSIONS // Uncomment this if odbc extensions available
#endif
#ifdef HAVE_IODBC
#define INCLUDE_EXTENSIONS // Uncomment this if odbc extensions available
#endif
#endif
#include "tclobj.hxx"
//////////////////////////////////////////////////////////////////////////
// ODBC INCLUDES
//
extern "C" {
#ifdef HAVE_IODBC // Using the free IODBC driver
/* I am not sure what version this is...
#include <iodbc.h>
#include <isql.h>
#include <isqlext.h>*/
/* iODBC 2.12 */
#include <isql.h>
#include <isqlext.h>
#include <iodbcinst.h>
#else
#include <sql.h>
#include <odbcinst.h>
#include <sqlext.h>
#endif
}
// We need these definitions if we're not using VC++
#ifndef _MSC_VER
typedef unsigned char BOOLEAN;
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
//////////////////////////////////////////////////////////////////////////
// DEFINES
//
#define SQL_MULTIPLE_RESULTSETS 12345
// environment handle
extern HENV env;
// Default lenght for blob buffer. If the database does not return
// the display lenght of a column, this value is used as default.
// However, if more data is available after fetch, more space
// is allocated when necessary.
#define MAX_BLOB_COLUMN_LENGTH 1024
// Maximum number of accepted sql arguments
#define MAX_SQL_ARGUMENTS 32
// A struct for storing strings with integer constants
struct NumStr
{
short numeric;
const char* string;
};
#ifndef BOOL
#define BOOL int
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define TCLODBC_OPTIONS 1000
#define TCLODBC_ENCODING 1000
// A struct for storing necessary data for retrieving sql results
struct ResultBuffer
{
SQLLEN cbValue;
SQLLEN cbValueMax;
SQLLEN fSqlType;
BOOL boundColumn;
char* strResult;
SWORD fTargetType;
};
struct ArgDefBuffer
{
SWORD fSqlType;
SQLULEN cbColDef;
SWORD ibScale;
SWORD fNullable;
SQLLEN cbValue;
SWORD fSourceType;
};
//////////////////////////////////////////////////////////////////////////
// DATABASE OBJECT CLASS HEADERS
//
// Common TclCmdObject ancestor to all database objects. This defines
// common destructing and command dispatching methods.
class TclCmdObject {
public:
TclCmdObject() : tclCommand(NULL), pNext(NULL), pPrev(NULL) {};
virtual ~TclCmdObject();
// Virtual destructor for all database objects
static void Destroy(ClientData);
// Static function for destroying database objects
static int Dispatch(ClientData clientData, Tcl_Interp *interp,
int objc, TCL_CMDARGS);
// Static function for dispatching commands to objects
Tcl_Command tclCommand;
// token for tcl command in the interpreter
void AddToMyList (TclCmdObject *);
// add an object to list
void RemoveFromList ();
// add an object to list
TclCmdObject* Next () {return pNext;};
TclCmdObject* Prev () {return pPrev;};
// list navigation
private:
virtual int Dispatch(Tcl_Interp *interp, int objc, TCL_CMDARGS) = 0;
// Virtual function for class dependent implementation of
// static Dispatch().
TclCmdObject *pNext;
TclCmdObject *pPrev;
// links for linking certain objects to each other
};
// Class TclDatabase provides a general database object, with
// connecting and transaction handling methods. Dispatch interface
// is defined for handling database object commands.
class TclStatement; // forward declaration
class TclDatabase : public TclCmdObject {
public:
enum COMMANDS {STATEMENT, S, RECORDSET, DISCONNECT, TABLES,
COLUMNS, INDEXES, AUTOCOMMIT, COMMIT, ROLLBACK, SET, GET,
TYPEINFO, PRIMARYKEYS, EVAL, READ};
enum OPTIONS {CONFIGURE, DATASOURCES, DRIVERS, VERSION, CONNECT};
TclDatabase(TclObj db, TclObj uid, TclObj passwd);
// Constructor creates a database object with connection
// to a named datasource.
TclDatabase(TclObj connectionstring);
// Constructor creates a database object with a given connection
// string.
virtual ~TclDatabase();
// Virtual destructor, disconnects from the database and destroys
// the object.
void Autocommit(BOOL on);
// Autocommit sets the autocommit property on or off.
void Transact(UWORD operation);
// Transact is used for transaction handling. It is called with
// constant SQL_COMMIT or SQL_ROLLBACK.
void SetOption(Tcl_Interp *interp, char* option, char* value);
// SetOption is used for setting various connection object
// otions.
TclObj GetOption(char* option);
// GetOption is used for querying object options current values.
const Tcl_Encoding Encoding() {return encoding;};
// GetOption is used for querying object options current values.
static TclObj Datasources();
// List of all registered data source names
static TclObj Drivers();
// List of all registered drivers
static int Configure(Tcl_Interp *interp, int objc, TCL_CMDARGS);
// List of all registered drivers
HDBC DBC() {return dbc;};
// return dbc handle
UDWORD DriverInfo() {return infoExtensions;};
// return info from driver
private:
virtual int Dispatch(Tcl_Interp *interp, int objc, TCL_CMDARGS);
void AddStatement(TclStatement *);
bool useMultipleResultSets;
HDBC dbc;
Tcl_Encoding encoding;
UDWORD infoExtensions;
};
// Class TclStatement provides general statement handling methods,
// including statement handle creation and destroying, executing
// statement, and handling result set and result buffer.
class TclStatement : public TclCmdObject {
public:
enum COMMANDS {EXECUTE, MORERESULTS, FETCH, SET, GET, EVAL, RUN, READ, DROP, COLUMNS, ROWCOUNT, CURSORNAME};
virtual ~TclStatement();
// Destructor drops the statement handle and frees dynamic structures
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS) = 0;
// executes the statement with given arguments
int ColumnCount();
// returns the count of columns in current result set.
TclObj ColumnLabels();
// returns the labels of the columns
TclObj Result();
// The result set of an executed statement in a single TclObj.
TclObj Value(Tcl_Interp *interp, int objc = 0, TCL_CMDARGS = NULL);
// This first executes the statement, and then returns the result set
TclObj Columns(int objc, TCL_CMDARGS);
// returns requested information of the columns in a tcl list.
TclObj ColumnInfo(int col, UWORD attr);
// returns requested information of a columns
BOOL Fetch(TclObj&);
// Retches and returns the next row in the result set. Returns
// false if no more data
void SetOption(char* option, char* value);
// SetOption is used for setting various connection object
// otions.
TclObj GetOption(char* option);
// GetOption is used for querying object options current values.
void Eval(Tcl_Interp *interp, TclObj proc, int objc, TCL_CMDARGS);
// execute statement and eval tcl procedure for each result row
void Read(Tcl_Interp *interp, TclObj array, int objc, TCL_CMDARGS);
// execute statement and read result rows to tcl array
void FreeStmt();
// statement cleanup
void SqlWait(int delay);
// do something while waiting asynchronous sql statement finalize
protected:
TclStatement(TclDatabase& db);
// Protected constructor creates a statement connected to a given data
// source. This is called from descendents constructors.
HSTMT stmt;
TclDatabase* pDb;
virtual int Dispatch(Tcl_Interp *interp, int objc, TCL_CMDARGS);
bool useMultipleResultSets;
private:
int colCount;
TclObj colLabels;
ResultBuffer *resultBuffer;
void ReserveResultBuffer();
void FreeResultBuffer();
virtual RETCODE Fetch1 ();
};
class TclSqlStatement : public TclStatement {
public:
TclSqlStatement(TclDatabase& db, TclObj sql, bool multiSets /*, BOOL recordset = FALSE*/);
// creates a sql statement object connected to a given data
// source. Sql clause is given as argument.
void SetArgDefs (Tcl_Interp *interp, TclObj defObj);
// sets argument definitions based on tcl list object
protected:
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS);
private:
ArgDefBuffer *argDefBuffer;
SQLSMALLINT argCount;
};
class TclTableQuery : public TclStatement {
public:
TclTableQuery(TclDatabase& db) : TclStatement(db) {};
// creates a table query object connected to a given
// data source.
private:
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS);
};
class TclColumnQuery : public TclStatement {
public:
TclColumnQuery(TclDatabase& db) : TclStatement(db) {};
// creates a column query object connected to a given
// data source.
private:
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS);
};
class TclIndexQuery : public TclStatement {
public:
TclIndexQuery(TclDatabase& db) : TclStatement(db) {};
// creates a index query object connected to a given
// data source.
private:
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS);
};
class TclTypeInfoQuery : public TclStatement {
public:
TclTypeInfoQuery(TclDatabase& db) : TclStatement(db) {};
// creates a typeinfo query object connected to a given
// data source.
private:
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS);
};
class TclPrimaryKeysQuery : public TclStatement {
public:
TclPrimaryKeysQuery(TclDatabase& db) : TclStatement(db) {};
// creates a primary keys query object connected to a given
// data source.
private:
virtual void Execute(Tcl_Interp *interp, int objc, TCL_CMDARGS);
};
//////////////////////////////////////////////////////////////////////////
// COMMON STRINGS AND STRINGTABLES
//
extern const char* strVersion;
extern const char* strMemoryAllocationFailed;
extern const char* strInvalidHandle;
extern const char* strOK;
extern const char* strTables;
extern const char* strColumns;
extern const char* strIndexes;
extern const char* strPrimarykeys;
extern const char* strTypeinfo;
extern const char* strEval;
extern const char* strRead;
extern const char* strGet;
extern const char* strSet;
extern const char* strAutocommit;
extern const char* strWrongArgs;
extern const char* strUsage;
extern const char* strCmdNotAvailable;
extern const char* strEmpty;
extern const char* strConcurrency;
extern const char* strMaxrows;
extern const char* strTimeout;
extern const char* strMaxlenght;
extern const char* strRowsetsize;
extern const char* strCursortype;
extern const char* strInvalidOption;
extern const char* strOldSyntax;
extern NumStr sqlType [];
extern NumStr attrDef [];
extern NumStr concurrencyOp [];
extern NumStr cursorOp [];
extern NumStr connectOp [];
extern NumStr booleanOp [];
extern NumStr stmtOp [];
extern NumStr configOp [];
extern NumStr databaseOptions [];
extern NumStr databaseCmds [];
extern NumStr statementCmds [];
extern NumStr globalOptions [];
//////////////////////////////////////////////////////////////////////////
// FUNCTION DECLARATIONS
//
TclObj SqlErr (HENV env, HDBC dbc, HSTMT stmt);
short StrToNum (const char* str, NumStr array[],
const char* errMsg = strInvalidOption,
BOOLEAN allowNumeric = TRUE);
TclObj NumToStr (short num, NumStr array[]);
inline short SqlType (char *strType) {
return StrToNum(strType, sqlType, (char *)"Invalid sql type: ");
};
inline short AttrDef (char *strDef) {
return StrToNum(strDef, attrDef, (char *)"Invalid attribute: ");
};
inline short ConfigOp (char *strDef) {
return StrToNum(strDef, configOp);
};
BOOL EncodedType (int i);
SWORD MapSqlType (SDWORD colType);
#ifdef _DEBUG
int tclodbc_validateNumStrArrays();
int tclodbc_validateStrToNumFunction();
#endif