-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodbc.h
More file actions
300 lines (252 loc) · 8.94 KB
/
Copy pathodbc.h
File metadata and controls
300 lines (252 loc) · 8.94 KB
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
/*
Name: odbc.h
Copyright: Mark Zammit
Author: Mark Zammit
Date: 01/02/12
Description: ODBC class wrapper for windows odbc.lib API
*/
// If not running a windows compiler you will need to
// add into your search directories the windows ODBC library paths:
// - .\windows\system32
// - .\Program Files\Microsoft SDKs\Windows\v7.0A\Include
// - .\Program Files\Microsoft SDKs\Windows\v7.0A\Lib
// If you have Visual Studio installed then the above microsoft SDKs can be
// depreciated to the following:
// - .\Program Files\Microsoft Visual Studio x.x\VC\include
// - .\Program Files\Microsoft Visual Studio x.x\VC\lib
// - .\Program Files\Microsoft Visual Studio x.x\VC\bin
#ifndef ODBC_CON_H
#define ODBC_CON_H
#define SQL_SUCCEEDED(rc) (((rc)&(~1))==0)
#define RMAP std::map<unsigned long,unordered_row>
#define DSNMAP std::map<TSTR,TSTR>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>
#include <windows.h>
#include <tchar.h>
#include <sql.h>
#include <sqltypes.h>
#include <sqlucode.h>
#include <sqlext.h>
#include <conio.h>
#include <comdef.h>
#include <mbstring.h>
#include "table.h"
#include <map>
#include <unordered_map>
#pragma comment( lib, "odbc32.lib" )
#pragma warning(disable: 4996)3
/** UNICODE SUPPORT **/
#if !defined(TSTR)
#if defined(UNICODE) || defined(_UNICODE_)
#define TSTR std::wstring
#else
#define TSTR std::string
#endif
#endif
#if defined(UNICODE) || defined(_UNICODE_)
#define FTPRINTF(x,y,...) fwprintf(x,y,__VA_ARGS__)
#else
#define FTPRINTF(x,y,...) fprintf(x,y,__VA_ARGS__)
#endif
// [SQL DATA TYPES]
//---------------------------------------------------------------------------
// SQL ODBC DATA TYPE | SQL DATA TYPE |
//---------------------------------------------------------------------------
// SQL_CHAR | CHAR(n) |
// SQL_VARHCHAR | VARCHAR(n) |
// SQL_LONGVARCHAR | LONG VARCHAR |
// SQL_WCHAR | WCHAR(n) |
// SQL_WLONGVARCHAR | LONGWVARCHAR |
// SQL_DECIMAL | DECIMAL(p,s) |
// SQL_NUMERIC | NUMERIC(p,s) |
// SQL_SMALLINT | SMALLINT |
// SQL_INTEGER | INTEGER |
// SQL_REAL | REAL |
// SQL_FLOAT | FLOAT(p) |
// SQL_DOUBLE | DOUBLE PRECISION |
// SQL_BIT | BIT |
// SQL_TINYINT | TINYINT |
// SQL_BIGINT | BIGINT |
// SQL_BINARY | BINARY(n) |
// SQL_VARBINARY | VARBINARY(n) |
// SQL_LONGVARBINARY | LONG VARBINARY |
// SQL_TYPE_DATE | DATE |
// SQL_TYPE_TIME | TIME(p) |
// SQL_TYPE_TIMESTAMP | TIMESTAMP(p) |
// SQL_TYPE_UTCDATETIME | UTCDATETIME |
// SQL_TYPE_UTCTIME | UTCTIME |
// SQL_INTERVAL_MONTH | INTERVAL MONTH(p) |
// SQL_INTERVAL_YEAR | INTERVAL YEAR(p) |
// SQL_INTERVAL_YEAR_TO_MONTH | INTERVAL YEAR(p) TO MONTH |
// SQL_INTERVAL_DAY | INTERVAL DAY(p) |
// SQL_INTERVAL_HOUR | INTERVAL HOUR(p) |
// SQL_INTERVAL_MINUTE | INTERVAL MINUTE(p) |
// SQL_INTERVAL_SECOND | INTERVAL SECOND(p,q) |
// SQL_INTERVAL_DAY_TO_HOUR | INTERVAL DAY(p) TO HOUR |
// SQL_INTERVAL_DAY_TO_MINUTE | INTERVAL DAY(p) TO MINUTE |
// SQL_INTERVAL_DAY_TO_SECOND | INTERVAL DAY(p) TO SECOND(q) |
// SQL_INTERVAL_HOUR_TO_MINUTE | INTERVAL HOUR(p) TO MINUTE |
// SQL_INTERVAL_HOUR_TO_SECOND | INTERVAL HOUR(p) TO SECOND(q) |
// SQL_INTERVAL_MINUTE_TO_SECOND | INTERVAL MINUTE(p) TO SECOND(q) |
// SQL_GUID | GUID |
//---------------------------------------------------------------------------
// [SQL RETURN VALUES]
// SQL_SUCCESS = 0
// SQL_SUCCESS_WITH_INFO = 1
// SQL_ERROR = -1
// SQL_INVALID_HANDLE = -2
// SQL_NO_DATA = 99
class odbc;
struct param;
struct param
{
SQLSMALLINT col;
TSTR val;
SQLSMALLINT type;
SQLULEN size;
SQLSMALLINT decimals;
};
class odbc
{
public:
// default constructor
odbc();
// initializes with a DSN
odbc(TSTR dsn);
// initializes with a DSN, username and password, only necessary
// if ODBC connection requires login each time
odbc(TSTR dsn, TSTR uid, TSTR pwd);
// disconnects on destruct
~odbc();
// changes the DSN, this also calls free_link() which
// which frees up all the handles and DSN specific data
// to stop memory leaks or result double ups
void set_connector(TSTR dsn);
void set_connector(TSTR dsn, TSTR uid, TSTR pwd);
// connects to the database
bool connect();
// disconnects from the database
bool disconnect();
// returns the current connection status
bool connection_status();
// returns the last return code for the last SQL operation
SQLRETURN last_status();
// returns a custom error message if any
TSTR last_error();
// returns odbc formatted connection string on successful connection
TSTR connection_string();
// prepares a SQL statement and then binds a list of parameters
// requires the list to be param structs to build the binding
bool prepare_and_bind(TSTR sql_stmt,std::vector<param> params);
// prepares a SQL statement
bool prepare(TSTR sql_stmt);
// binds parameters to the prepared statement
bool bind_param(short col, TSTR val, short sql_field_type, SQLULEN col_size ,short decimal_pts);
// clears the last prepared statement without closing the connection
void free_session();
// clears the memory allocated to the statement handle to allow
// multiple statements to be executed during the single connection
void free_statement();
// executes a prepared statement
bool execute();
// executes a non-bindable statement
bool execute_direct(TSTR sql_stmt);
// fetches a unordered_row at a time
bool fetch(unordered_row &r);
// fetches a unordered_row at a time by reference
// this allows unordered_row data to have settings changed
bool fetch(unordered_row *&r);
// fetches a specific unordered_row from result set
unordered_row fetch_row(unsigned long row_id);
// fetches each row directly from the database
// slower but will handle very large data set sizes since
// it doesnt load the data into memory first and eliminates memory errors
bool fetch_direct(unordered_row &r);
// fetches each DSN & DSN Description from the DSN table
// initialized on ODBC init, will return blank if ODBC failed to connect
bool fetch_dsn(TSTR &dsn, TSTR &dsn_desc);
// returns a field name of a particular column
TSTR get_field_name(unsigned long col);
// moves the result set cursor to a specific result set
// once the result sets have been passed they will be lost!
bool move_to_result_set(unsigned long set_pos);
// returns the number of fields in the result set
unsigned long fields();
// returns the number of rows in the result set
unsigned long rows();
// returns the affected rows from statements like INSERT/DELETE/UPDATE
unsigned long affected_rows();
private:
// err/info value
TSTR _err;
// stores field data for active table
struct field_description
{
SQLSMALLINT colNumber;
SQLTCHAR colName[80];
SQLSMALLINT nameLen;
SQLSMALLINT dataType;
SQLULEN colSize;
SQLSMALLINT decimalDigits;
SQLSMALLINT nullable;
};
// ODBC handlers
// Environment handler
// must be initialized before connection
SQLHANDLE _henv;
// Connection handler necessary before statement handler
SQLHANDLE _hdbc;
// Statement handler wipes when connection handler
// disconnects, needs to be re-established each time
SQLHANDLE _hstmt;
// connection string for driver access
TSTR _dsn;
// user id for DB
TSTR _uid;
// user password for DB
TSTR _pwd;
TSTR _sql_stmt;
unsigned long _fields;
unsigned long _rows;
unsigned long _affected_rows;
unsigned long _row_ptr;
// return code from ODBC based on last operation
SQLRETURN _rc;
// current connection status
bool _connected;
bool _init;
bool _bound;
bool _built;
bool _executed;
bool _fetching;
// stores specific field info
std::vector<field_description> field_info;
// stores vector of field names
std::vector<TSTR> field_names;
RMAP _table;
RMAP::iterator _itr;
RMAP::iterator __itr;
// holds DSN list
DSNMAP _dsntable;
DSNMAP::iterator _dsn_itr;
//std::tr1::unordered_map<unsigned int, row> _table;
//std::tr1::unordered_map<unsigned int, row>::iterator _itr;
// initializes handlers
void init();
// sets up the DSN listing from connected ODBC
void set_dsn_list();
// sets up the field_description vector
void set_field_descriptors();
// returns a field_descriptor containing field data
// queried from the DB
SQLRETURN Describe(field_description& c);
void error_out();
void build_result_set();
void extract_error(TCHAR *fn,SQLHANDLE handle,SQLSMALLINT type);
void free_link();
void reset_iterator();
};
#endif