forked from willamowius/gnugk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gksql_firebird.cxx
675 lines (594 loc) · 20.6 KB
/
gksql_firebird.cxx
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
* gksql_firebird.cxx
*
* Firebird/Interbase driver module for GnuGk
*
* Copyright (c) 2006, Michal Zygmuntowicz
* Copyright (c) 2006-2013, Jan Willamowius
*
* This work is published under the GNU Public License version 2 (GPLv2)
* see file COPYING for details.
* We also explicitly grant the right to link this code
* with the OpenH323/H323Plus and OpenSSL library.
*
*/
#include "config.h"
#if HAS_FIREBIRD
#include <ptlib.h>
#include <cmath>
#include <ibase.h>
#include "gksql.h"
const unsigned int FB_BUFF_SIZE = 2048;
static PDynaLink g_sharedLibrary;
static ISC_LONG (ISC_EXPORT *g_fb_interpret)(ISC_SCHAR*, unsigned int, const ISC_STATUS**) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_attach_database)(ISC_STATUS*,
short,
const ISC_SCHAR*,
isc_db_handle*,
short,
const ISC_SCHAR*) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_commit_transaction)(ISC_STATUS *, isc_tr_handle *) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_detach_database)(ISC_STATUS *, isc_db_handle *) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_dsql_allocate_statement)(ISC_STATUS *,
isc_db_handle *,
isc_stmt_handle *) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_dsql_describe)(ISC_STATUS *,
isc_stmt_handle *,
unsigned short,
XSQLDA *) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_dsql_execute)(ISC_STATUS*,
isc_tr_handle*,
isc_stmt_handle*,
unsigned short,
XSQLDA*) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_dsql_fetch)(ISC_STATUS *,
isc_stmt_handle *,
unsigned short,
XSQLDA *) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_dsql_free_statement)(ISC_STATUS *, isc_stmt_handle *, unsigned short) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_dsql_prepare)(ISC_STATUS*,
isc_tr_handle*,
isc_stmt_handle*,
unsigned short,
const ISC_SCHAR*,
unsigned short,
XSQLDA*) = NULL;
static ISC_STATUS (ISC_EXPORT *g_isc_rollback_transaction)(ISC_STATUS *, isc_tr_handle *) = NULL;
static ISC_LONG (ISC_EXPORT *g_isc_sqlcode)(const ISC_STATUS*) = NULL;
static void (ISC_EXPORT *g_isc_sql_interprete)(short, ISC_SCHAR*, short) = NULL;
static ISC_STATUS (ISC_EXPORT_VARARG *g_isc_start_transaction)(ISC_STATUS *,
isc_tr_handle *,
short, ...) = NULL;
namespace {
PString XSQLVARToPString(XSQLVAR *sqlvar)
{
if (sqlvar->sqltype & 1)
if (*(sqlvar->sqlind) == -1)
return PString::Empty();
switch (sqlvar->sqltype & ~1L) {
case SQL_TEXT:
return PString(sqlvar->sqldata, sqlvar->sqllen);
case SQL_SHORT:
return sqlvar->sqlscale < 0
? PString(PString::Decimal, *(int*)(sqlvar->sqldata) * pow(10.0, sqlvar->sqlscale), abs(sqlvar->sqlscale))
: PString(*(short*)(sqlvar->sqldata));
case SQL_LONG:
return sqlvar->sqlscale < 0
? PString(PString::Decimal, *(long*)(sqlvar->sqldata) * pow(10.0, sqlvar->sqlscale), abs(sqlvar->sqlscale))
: PString(*(long*)(sqlvar->sqldata));
case SQL_DOUBLE:
return sqlvar->sqlscale < 0
? PString(PString::Decimal, *(double*)(sqlvar->sqldata) * pow(10.0, sqlvar->sqlscale), abs(sqlvar->sqlscale))
: PString(PString::Printf, "%f", *(double*)(sqlvar->sqldata));
case SQL_INT64:
return sqlvar->sqlscale < 0
? PString(PString::Decimal, *(ISC_INT64*)(sqlvar->sqldata) * pow(10.0, sqlvar->sqlscale), abs(sqlvar->sqlscale))
: PString(*(ISC_INT64*)(sqlvar->sqldata));
case SQL_FLOAT:
return PString(PString::Printf, "%f", (double)*(float*)(sqlvar->sqldata));
case SQL_VARYING:
return PString(sqlvar->sqldata + 2, *(short*)(sqlvar->sqldata));
default:
return PString::Empty();
}
}
} // end of namespace
/** Class that encapsulates SQL query result for Firebird backend.
It does not provide any multithread safety, so should be accessed
from a single thread at time.
*/
class GkIBSQLResult : public GkSQLResult
{
public:
/// Build the result from SELECT type query
GkIBSQLResult(
/// transaction handle
isc_tr_handle tr,
/// statement handle
isc_stmt_handle stmt,
/// SELECT type query result
XSQLDA* selectResult
);
/// Build the empty result and store query execution error information
GkIBSQLResult(
/// Firebird specific error code
unsigned int errorCode,
/// Firebird specific error message text
const char* errorMsg,
/// transaction handle
isc_tr_handle tr = 0L,
/// statement handle
isc_stmt_handle stmt = 0L,
/// SELECT type query result
XSQLDA* selectResult = NULL
);
virtual ~GkIBSQLResult();
/** @return
Backend specific error message, if the query failed.
*/
virtual PString GetErrorMessage();
/** @return
Backend specific error code, if the query failed.
*/
virtual long GetErrorCode();
/** Fetch a single row from the result set. After each row is fetched,
cursor position is moved to a next row.
@return
True if the row has been fetched, false if no more rows are available.
*/
virtual bool FetchRow(
/// array to be filled with string representations of the row fields
PStringArray& result
);
virtual bool FetchRow(
/// array to be filled with string representations of the row fields
ResultRow& result
);
private:
GkIBSQLResult();
GkIBSQLResult(const GkIBSQLResult&);
GkIBSQLResult& operator=(const GkIBSQLResult&);
protected:
/// query result for SELECT type queries
XSQLDA* m_sqlResult;
/// transaction handle
isc_tr_handle m_tr;
/// statement handle
isc_stmt_handle m_stmt;
/// the most recent row returned by fetch operation
int m_sqlRow;
/// Firebird specific error code (if the query failed)
unsigned int m_errorCode;
/// Firebird specific error message text (if the query failed)
PString m_errorMessage;
};
/// Firebird/Interbase backend connection implementation.
class GkIBSQLConnection : public GkSQLConnection
{
public:
/// Build a new Firebird connection object
GkIBSQLConnection(
/// name to use in the log
const char* name = "Firebird"
);
virtual ~GkIBSQLConnection();
protected:
class IBSQLConnWrapper : public GkSQLConnection::SQLConnWrapper
{
public:
IBSQLConnWrapper(
/// unique identifier for this connection
int id,
/// host:port this connection is made to
const PString& host,
/// Firebird connection object
isc_db_handle conn
) : SQLConnWrapper(id, host), m_conn(conn) {}
virtual ~IBSQLConnWrapper();
private:
IBSQLConnWrapper();
IBSQLConnWrapper(const IBSQLConnWrapper&);
IBSQLConnWrapper& operator=(const IBSQLConnWrapper&);
public:
isc_db_handle m_conn;
};
/** Create a new SQL connection using parameters stored in this object.
When the connection is to be closed, the object is simply deleted
using delete operator.
@return
NULL if database connection could not be established
or an object of IBSQLConnWrapper class.
*/
virtual SQLConnPtr CreateNewConnection(
/// unique identifier for this connection
int id
);
/** Execute the query using specified SQL connection.
@return
Query execution result.
*/
virtual GkSQLResult* ExecuteQuery(
/// SQL connection to use for query execution
SQLConnPtr conn,
/// query string
const char* queryStr,
/// maximum time (ms) for the query execution, -1 means infinite
long timeout = -1
);
/** Escape any special characters in the string, so it can be used in a SQL query.
@return
Escaped string.
*/
virtual PString EscapeString(
/// SQL connection to get escaping parameters from
SQLConnPtr conn,
/// string to be escaped
const char* str
);
private:
GkIBSQLConnection(const GkIBSQLConnection&);
GkIBSQLConnection& operator=(const GkIBSQLConnection&);
};
GkIBSQLResult::GkIBSQLResult(
/// transaction handle
isc_tr_handle tr,
/// statement handle
isc_stmt_handle stmt,
/// SELECT type query result
XSQLDA* selectResult
)
: GkSQLResult(false), m_sqlResult(selectResult), m_tr(tr), m_stmt(stmt), m_sqlRow(-1),
m_errorCode(0)
{
if (m_sqlResult != NULL) {
m_numRows = 100000;
m_numFields = m_sqlResult->sqld;
for (int i = 0; i < m_sqlResult->sqld; ++i) {
m_sqlResult->sqlvar[i].sqlind = new short;
if ((m_sqlResult->sqlvar[i].sqltype & ~1L) == SQL_VARYING)
m_sqlResult->sqlvar[i].sqldata = new char[m_sqlResult->sqlvar[i].sqllen + 2];
else
m_sqlResult->sqlvar[i].sqldata = new char[m_sqlResult->sqlvar[i].sqllen];
}
} else
m_queryError = true;
}
GkIBSQLResult::GkIBSQLResult(
/// Firebird specific error code
unsigned int errorCode,
/// Firebird specific error message text
const char* errorMsg,
/// transaction handle
isc_tr_handle tr,
/// statement handle
isc_stmt_handle stmt,
/// SELECT type query result
XSQLDA* selectResult
)
: GkSQLResult(true), m_sqlResult(selectResult), m_tr(tr), m_stmt(stmt), m_sqlRow(-1),
m_errorCode(errorCode), m_errorMessage(errorMsg)
{
}
GkIBSQLResult::~GkIBSQLResult()
{
ISC_STATUS status[20];
if (m_stmt != 0L)
(*g_isc_dsql_free_statement)(status, &m_stmt, DSQL_drop);
if (m_sqlResult != NULL) {
for (int i = 0; i < m_sqlResult->sqld; ++i) {
if (m_sqlResult->sqlvar[i].sqldata != NULL) {
delete [] m_sqlResult->sqlvar[i].sqldata;
m_sqlResult->sqlvar[i].sqldata = NULL;
}
if (m_sqlResult->sqlvar[i].sqlind != NULL) {
delete m_sqlResult->sqlvar[i].sqlind;
m_sqlResult->sqlvar[i].sqlind = NULL;
}
}
delete [] reinterpret_cast<char*>(m_sqlResult);
m_sqlResult = NULL;
}
if (m_tr != 0L) {
if (m_queryError) {
(*g_isc_rollback_transaction)(status, &m_tr);
} else {
(*g_isc_commit_transaction)(status, &m_tr);
}
}
}
PString GkIBSQLResult::GetErrorMessage()
{
return m_errorMessage;
}
long GkIBSQLResult::GetErrorCode()
{
return m_errorCode;
}
bool GkIBSQLResult::FetchRow(
/// array to be filled with string representations of the row fields
PStringArray& result
)
{
if (m_sqlResult == NULL || m_numRows <= 0)
return false;
if (m_sqlRow < 0)
m_sqlRow = 0;
if (m_sqlRow >= m_numRows)
return false;
ISC_STATUS retval;
ISC_STATUS status[20];
retval = (*g_isc_dsql_fetch)(status, &m_stmt, 1, m_sqlResult);
if (status[0] == 1 && status[1] != 0) {
m_numRows = m_sqlRow;
if (retval != 100) {
long errcode = (*g_isc_sqlcode)(status);
char errormsg[FB_BUFF_SIZE];
if (errcode == -999) {
errcode = status[1];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
} else {
(*g_isc_sql_interprete)(static_cast<short>(errcode), errormsg, FB_BUFF_SIZE);
}
PTRACE(2, "Firebird\tFailed to fetch query row (" << errcode
<< "): SQL:" << errormsg);
SNMP_TRAP(5, SNMPError, Database, "Firebird query failed");
}
return false;
}
result.SetSize(m_sqlResult->sqld);
for (PINDEX i = 0; i < m_sqlResult->sqld; ++i)
result[i] = XSQLVARToPString(&(m_sqlResult->sqlvar[i]));
m_sqlRow++;
return true;
}
bool GkIBSQLResult::FetchRow(
/// array to be filled with string representations of the row fields
ResultRow& result
)
{
if (m_sqlResult == NULL || m_numRows <= 0)
return false;
if (m_sqlRow < 0)
m_sqlRow = 0;
if (m_sqlRow >= m_numRows)
return false;
ISC_STATUS retval;
ISC_STATUS status[20];
retval = (*g_isc_dsql_fetch)(status, &m_stmt, 1, m_sqlResult);
if (status[0] == 1 && status[1] != 0) {
m_numRows = m_sqlRow;
if (retval != 100) {
long errcode = (*g_isc_sqlcode)(status);
char errormsg[FB_BUFF_SIZE];
if (errcode == -999) {
errcode = status[1];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
} else {
(*g_isc_sql_interprete)(static_cast<short>(errcode), errormsg, FB_BUFF_SIZE);
}
PTRACE(2, "Firebird\tFailed to fetch query row (" << errcode
<< "): SQL:" << errormsg);
SNMP_TRAP(5, SNMPError, Database, "Firebird query failed");
}
return false;
}
result.resize(m_numFields);
for (PINDEX i = 0; i < m_numFields; i++) {
result[i].first = XSQLVARToPString(&(m_sqlResult->sqlvar[i]));
result[i].second = PString(m_sqlResult->sqlvar[i].aliasname, m_sqlResult->sqlvar[i].aliasname_length);
}
m_sqlRow++;
return true;
}
GkIBSQLConnection::GkIBSQLConnection(
/// name to use in the log
const char * name
) : GkSQLConnection(name)
{
}
GkIBSQLConnection::~GkIBSQLConnection()
{
}
GkIBSQLConnection::IBSQLConnWrapper::~IBSQLConnWrapper()
{
ISC_STATUS status[20];
(*g_isc_detach_database)(status, &m_conn);
}
GkSQLConnection::SQLConnPtr GkIBSQLConnection::CreateNewConnection(
/// unique identifier for this connection
int id
)
{
if (!g_sharedLibrary.IsLoaded()) {
if (m_library.IsEmpty()) {
#ifdef _WIN32
m_library = "fbclient" + g_sharedLibrary.GetExtension();
#else
m_library = "libfbclient" + g_sharedLibrary.GetExtension();
#endif
}
if (!g_sharedLibrary.Open(m_library)) {
PTRACE (1, GetName() << "\tCan't load library " << m_library);
return NULL;
}
if (!g_sharedLibrary.GetFunction("fb_interpret", (PDynaLink::Function &)g_fb_interpret)
|| !g_sharedLibrary.GetFunction("isc_attach_database", (PDynaLink::Function &)g_isc_attach_database)
|| !g_sharedLibrary.GetFunction("isc_commit_transaction", (PDynaLink::Function &)g_isc_commit_transaction)
|| !g_sharedLibrary.GetFunction("isc_detach_database", (PDynaLink::Function &)g_isc_detach_database)
|| !g_sharedLibrary.GetFunction("isc_dsql_allocate_statement", (PDynaLink::Function &)g_isc_dsql_allocate_statement)
|| !g_sharedLibrary.GetFunction("isc_dsql_describe", (PDynaLink::Function &)g_isc_dsql_describe)
|| !g_sharedLibrary.GetFunction("isc_dsql_execute", (PDynaLink::Function &)g_isc_dsql_execute)
|| !g_sharedLibrary.GetFunction("isc_dsql_fetch", (PDynaLink::Function &)g_isc_dsql_fetch)
|| !g_sharedLibrary.GetFunction("isc_dsql_free_statement", (PDynaLink::Function &)g_isc_dsql_free_statement)
|| !g_sharedLibrary.GetFunction("isc_dsql_prepare", (PDynaLink::Function &)g_isc_dsql_prepare)
|| !g_sharedLibrary.GetFunction("isc_rollback_transaction", (PDynaLink::Function &)g_isc_rollback_transaction)
|| !g_sharedLibrary.GetFunction("isc_sqlcode", (PDynaLink::Function &)g_isc_sqlcode)
|| !g_sharedLibrary.GetFunction("isc_sql_interprete", (PDynaLink::Function &)g_isc_sql_interprete)
|| !g_sharedLibrary.GetFunction("isc_start_transaction", (PDynaLink::Function &)g_isc_start_transaction)
) {
#ifdef hasDynaLinkGetLastError
PTRACE (1, GetName() << "\tFailed to load shared database library: " << g_sharedLibrary.GetLastError());
#else
PTRACE (1, GetName() << "\tFailed to load shared database library: unknown error");
#endif
g_sharedLibrary.Close();
SNMP_TRAP(5, SNMPError, Database, GetName() + " DLL load error");
return NULL;
}
}
unsigned dpb_offset = 0;
std::vector<char> dpb(1);
dpb[dpb_offset++] = isc_dpb_version1;
if (!m_username) {
dpb.resize(dpb.size() + 2 + m_username.GetLength());
dpb[dpb_offset++] = isc_dpb_user_name;
dpb[dpb_offset++] = m_username.GetLength();
memcpy(&(dpb[dpb_offset]), (const char*)m_username, m_username.GetLength());
dpb_offset += m_username.GetLength();
}
if (!m_password) {
dpb.resize(dpb.size() + 2 + m_password.GetLength());
dpb[dpb_offset++] = isc_dpb_password;
dpb[dpb_offset++] = m_password.GetLength();
memcpy(&(dpb[dpb_offset]), (const char*)m_password, m_password.GetLength());
dpb_offset += m_password.GetLength();
}
ISC_STATUS status[20];
isc_db_handle conn = 0L;
std::string dbname((const char*)m_database);
if (!m_host) {
dbname.insert(0, ":");
dbname.insert(0, (const char *)m_host);
}
(*g_isc_attach_database)(status, 0, const_cast<char*>(dbname.c_str()), &conn, dpb_offset, &(dpb[0]));
if (status[0] == 1 && status[1] != 0) {
char errormsg[FB_BUFF_SIZE];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
PTRACE(2, GetName() << "\tFirebird connection to " << m_username << '@' << dbname
<< " failed (isc_attach_database failed): " << errormsg);
SNMP_TRAP(5, SNMPError, Database, GetName() + " connection failed");
return NULL;
}
PTRACE(5, GetName() << "\tFirebird connection to " << m_username << '@' << dbname
<< " established successfully"
);
return new IBSQLConnWrapper(id, dbname, conn);
}
GkSQLResult* GkIBSQLConnection::ExecuteQuery(
/// SQL connection to use for query execution
GkSQLConnection::SQLConnPtr con,
/// query string
const char* queryStr,
/// maximum time (ms) for the query execution, -1 means infinite
long /*timeout*/
)
{
isc_db_handle conn = ((IBSQLConnWrapper*)con)->m_conn;
char errormsg[FB_BUFF_SIZE];
ISC_STATUS status[20];
isc_tr_handle tr = 0L;
isc_stmt_handle stmt = 0L;
(*g_isc_start_transaction)(status, &tr, 1, &conn, 0, NULL);
if (status[0] == 1 && status[1] != 0) {
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
return new GkIBSQLResult(status[1], errormsg);
}
(*g_isc_dsql_allocate_statement)(status, &conn, &stmt);
if (status[0] == 1 && status[1] != 0) {
long errorcode = (*g_isc_sqlcode)(status);
if (errorcode == -999) {
errorcode = status[1];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
} else {
(*g_isc_sql_interprete)(static_cast<short>(errorcode), errormsg, FB_BUFF_SIZE);
}
Disconnect();
return new GkIBSQLResult(errorcode, errormsg, tr);
}
int numcols = 1;
XSQLDA *result = reinterpret_cast<XSQLDA*>(new char[XSQLDA_LENGTH(numcols)]);
memset(result, 0, XSQLDA_LENGTH(numcols));
result->version = SQLDA_VERSION1;
result->sqln = numcols;
(*g_isc_dsql_prepare)(status, &tr, &stmt, 0, const_cast<char*>(queryStr), SQL_DIALECT_CURRENT, result);
if (status[0] == 1 && status[1] != 0) {
long errorcode = (*g_isc_sqlcode)(status);
if (errorcode == -999) {
errorcode = status[1];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
} else {
(*g_isc_sql_interprete)(static_cast<short>(errorcode), errormsg, FB_BUFF_SIZE);
}
Disconnect();
return new GkIBSQLResult(errorcode, errormsg, tr, stmt);
}
if (result->sqld > result->sqln) {
numcols = result->sqld;
delete [] reinterpret_cast<char*>(result);
result = reinterpret_cast<XSQLDA*>(new char[XSQLDA_LENGTH(numcols)]);
memset(result, 0, XSQLDA_LENGTH(numcols));
result->version = SQLDA_VERSION1;
result->sqln = numcols;
(*g_isc_dsql_describe)(status, &stmt, SQLDA_VERSION1, result);
if (status[0] == 1 && status[1] != 0) {
long errorcode = (*g_isc_sqlcode)(status);
if (errorcode == -999) {
errorcode = status[1];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
} else {
(*g_isc_sql_interprete)(static_cast<short>(errorcode), errormsg, FB_BUFF_SIZE);
}
delete [] reinterpret_cast<char*>(result);
result = NULL;
Disconnect();
return new GkIBSQLResult(errorcode, errormsg, tr, stmt);
}
}
(*g_isc_dsql_execute)(status, &tr, &stmt, SQLDA_VERSION1, NULL);
if (status[0] == 1 && status[1] != 0) {
long errorcode = (*g_isc_sqlcode)(status);
if (errorcode == -999) {
errorcode = status[1];
const ISC_STATUS *pvector = status;
(*g_fb_interpret)(errormsg, FB_BUFF_SIZE, &pvector); // fetch first error message only
} else {
(*g_isc_sql_interprete)(static_cast<short>(errorcode), errormsg, FB_BUFF_SIZE);
}
delete [] reinterpret_cast<char*>(result);
result = NULL;
Disconnect();
return new GkIBSQLResult(errorcode, errormsg, tr, stmt);
}
return new GkIBSQLResult(tr, stmt, result);
}
PString GkIBSQLConnection::EscapeString(
/// SQL connection to get escaping parameters from
SQLConnPtr /*conn*/,
/// string to be escaped
const char* str
)
{
PString s(str);
// remove all special characters 0x00-0x1f, except TAB
PINDEX i = 0;
while (i < s.GetLength()) {
if (s.Mid(i, 1) < PString(' ') && s.Mid(i, 1) != PString('\t')) {
s.Delete(i, 1);
} else {
++i;
}
}
s.Replace("'", "''", TRUE);
return s;
}
namespace {
GkSQLCreator<GkIBSQLConnection> IBSQLCreator("Firebird");
}
#endif /* HAS_FIREBIRD */