- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathutl.cpp
3507 lines (2989 loc) · 79.1 KB
/
utl.cpp
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROGRAM: JRD Access Method
* MODULE: utl.cpp
* DESCRIPTION: User callable routines
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2001.06.14 Claudio Valderrama: isc_set_path() will append slash if missing,
* so ISC_PATH environment variable won't fail for this cause.
* 2002.02.15 Sean Leyne - Code Cleanup is required of obsolete "EPSON", "XENIX" ports
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "Apollo" port
* 23-Feb-2002 Dmitry Yemanov - Events wildcarding
*
* 2002.10.27 Sean Leyne - Completed removal of obsolete "DG_X86" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "UNIXWARE" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "Ultrix" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "Ultrix/MIPS" port
*
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "MPEXL" port
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
* 2002.10.30 Sean Leyne - Removed support for obsolete "PC_PLATFORM" define
*
*/
#include"firebird.h"
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../jrd/license.h"
#include<stdarg.h>
#include"../common/gdsassert.h"
#include"ibase.h"
#include"../yvalve/msg.h"
#include"../jrd/event.h"
#include"../yvalve/gds_proto.h"
#include"../yvalve/utl_proto.h"
#include"../yvalve/YObjects.h"
#include"../yvalve/why_proto.h"
#include"../yvalve/prepa_proto.h"
#include"../yvalve/PluginManager.h"
#include"../jrd/constants.h"
#include"../jrd/build_no.h"
#include"../common/TimeZoneUtil.h"
#include"../common/classes/ClumpletWriter.h"
#include"../common/utils_proto.h"
#include"../common/classes/MetaString.h"
#include"../common/classes/TempFile.h"
#include"../common/classes/DbImplementation.h"
#include"../common/ThreadStart.h"
#include"../common/isc_f_proto.h"
#include"../common/StatusHolder.h"
#include"../common/classes/ImplementHelper.h"
#include"../common/classes/fb_tls.h"
#include"../common/os/os_utils.h"
#ifdef HAVE_UNISTD_H
#include<unistd.h>
#endif
#include<sys/types.h>
#include<sys/stat.h>
#if defined(WIN_NT)
#include<io.h>// mktemp, unlink ..
#include<process.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include<sys/file.h>
#endif
usingnamespaceFirebird;
IAttachment* handleToIAttachment(CheckStatusWrapper*, FB_API_HANDLE*);
ITransaction* handleToITransaction(CheckStatusWrapper*, FB_API_HANDLE*);
// Bug 7119 - BLOB_load will open external file for read in BINARY mode.
#ifdef WIN_NT
staticconstchar* const FOPEN_READ_TYPE = "rb";
staticconstchar* const FOPEN_WRITE_TYPE = "wb";
staticconstchar* const FOPEN_READ_TYPE_TEXT = "rt";
staticconstchar* const FOPEN_WRITE_TYPE_TEXT = "wt";
#else
staticconstchar* const FOPEN_READ_TYPE = "r";
staticconstchar* const FOPEN_WRITE_TYPE = "w";
staticconstchar* const FOPEN_READ_TYPE_TEXT = FOPEN_READ_TYPE;
staticconstchar* const FOPEN_WRITE_TYPE_TEXT = FOPEN_WRITE_TYPE;
#endif
#defineLOWER7(c) ( (c >= 'A' && c<= 'Z') ? c + 'a' - 'A': c )
// Blob stream stuff
constint BSTR_input = 0;
constint BSTR_output = 1;
constint BSTR_alloc = 2;
staticvoidget_ods_version(CheckStatusWrapper*, IAttachment*, USHORT*, USHORT*);
staticvoidisc_expand_dpb_internal(const UCHAR** dpb, SSHORT* dpb_size, ...);
// Blob info stuff
staticconstchar blob_items[] =
{
isc_info_blob_max_segment, isc_info_blob_num_segments,
isc_info_blob_total_length
};
// gds__version stuff
staticconstunsignedchar info[] =
{ isc_info_firebird_version, isc_info_implementation, fb_info_implementation, isc_info_end };
staticconstunsignedchar ods_info[] =
{ isc_info_ods_version, isc_info_ods_minor_version, isc_info_end };
staticconst TEXT* const impl_class[] =
{
NULL, // 0
"access method", // 1
"Y-valve", // 2
"remote interface", // 3
"remote server", // 4
NULL, // 5
NULL, // 6
"pipe interface", // 7
"pipe server", // 8
"central interface", // 9
"central server", // 10
"gateway", // 11
"classic server", // 12
"super server"// 13
};
namespace {
classVersionCallback : publicAutoIface<IVersionCallbackImpl<VersionCallback, CheckStatusWrapper> >
{
public:
VersionCallback(FPTR_VERSION_CALLBACK routine, void* user)
: func(routine), arg(user)
{ }
// IVersionCallback implementation
voidcallback(CheckStatusWrapper*, constchar* text)
{
func(arg, text);
}
private:
FPTR_VERSION_CALLBACK func;
void* arg;
};
voidload(CheckStatusWrapper* status, ISC_QUAD* blobId, IAttachment* att, ITransaction* tra, FILE* file)
{
/**************************************
*
* l o a d
*
**************************************
*
* Functional description
* Load a blob from a file.
*
**************************************/
LocalStatus ls;
CheckStatusWrapper temp(&ls);
// Open the blob. If it failed, what the hell -- just return failure
IBlob* blob = att->createBlob(status, tra, blobId, 0, NULL);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
// Copy data from file to blob. Make up boundaries at end of line.
TEXT buffer[512];
TEXT* p = buffer;
const TEXT* const buffer_end = buffer + sizeof(buffer);
for (;;)
{
const SSHORT c = fgetc(file);
if (feof(file))
break;
*p++ = static_cast<TEXT>(c);
if (c != '\n' && p < buffer_end)
continue;
const SSHORT l = p - buffer;
blob->putSegment(status, l, buffer);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
{
blob->close(&temp);
return;
}
p = buffer;
}
const SSHORT l = p - buffer;
if (l != 0)
blob->putSegment(status, l, buffer);
blob->close(&temp);
return;
}
voiddump(CheckStatusWrapper* status, ISC_QUAD* blobId, IAttachment* att, ITransaction* tra, FILE* file)
{
/**************************************
*
* d u m p
*
**************************************
*
* Functional description
* Dump a blob into a file.
*
**************************************/
// Open the blob. If it failed, what the hell -- just return failure
IBlob* blob = att->openBlob(status, tra, blobId, 0, NULL);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
// Copy data from blob to scratch file
SCHAR buffer[256];
const SSHORT short_length = sizeof(buffer);
for (bool cond = true; cond; )
{
unsigned l = 0;
switch (blob->getSegment(status, short_length, buffer, &l))
{
case Firebird::IStatus::RESULT_ERROR:
case Firebird::IStatus::RESULT_NO_DATA:
cond = false;
break;
}
if (l)
FB_UNUSED(fwrite(buffer, 1, l, file));
}
// Close the blob
LocalStatus ls;
CheckStatusWrapper temp(&ls);
blob->close(&temp);
}
FB_BOOLEAN edit(CheckStatusWrapper* status, ISC_QUAD* blob_id, IAttachment* att, ITransaction* tra,
int type, constchar* field_name)
{
/**************************************
*
* e d i t
*
**************************************
*
* Functional description
* Open a blob, dump it to a file, allow the user to edit the
* window, and dump the data back into a blob. If the user
* bails out, return FALSE, otherwise return TRUE.
*
* If the field name coming in is too big, truncate it.
*
**************************************/
#if (defined WIN_NT)
TEXT buffer[9];
#else
TEXT buffer[25];
#endif
const TEXT* q = field_name;
if (!q)
q = "gds_edit";
TEXT* p;
for (p = buffer; *q && p < buffer + sizeof(buffer) - 1; q++)
{
if (*q == '$')
*p++ = '_';
else
*p++ = LOWER7(*q);
}
*p = 0;
// Moved this out of #ifndef mpexl to get mktemp/mkstemp to work for Linux
// This has been done in the inprise tree some days ago.
// Would have saved me a lot of time, if I had seen this earlier :-(
// FSG 15.Oct.2000
PathName tmpf = TempFile::create(status, buffer);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return FB_FALSE;
FILE* file = os_utils::fopen(tmpf.c_str(), FOPEN_WRITE_TYPE_TEXT);
if (!file)
{
unlink(tmpf.c_str());
system_error::raise("fopen");
}
dump(status, blob_id, att, tra, file);
if (status->getState() & IStatus::STATE_ERRORS)
{
isc_print_status(status->getErrors());
fclose(file);
unlink(tmpf.c_str());
return FB_FALSE;
}
fclose(file);
if (gds__edit(tmpf.c_str(), type))
{
if (!(file = os_utils::fopen(tmpf.c_str(), FOPEN_READ_TYPE_TEXT)))
{
unlink(tmpf.c_str());
system_error::raise("fopen");
}
load(status, blob_id, att, tra, file);
fclose(file);
return status->getState() & IStatus::STATE_ERRORS ? FB_FALSE : FB_TRUE;
}
unlink(tmpf.c_str());
return FB_FALSE;
}
} // anonymous namespace
namespaceWhy {
UtilInterface utilInterface;
voidUtilInterface::dumpBlob(CheckStatusWrapper* status, ISC_QUAD* blobId,
IAttachment* att, ITransaction* tra, constchar* file_name, FB_BOOLEAN txt)
{
FILE* file = os_utils::fopen(file_name, txt ? FOPEN_WRITE_TYPE_TEXT : FOPEN_WRITE_TYPE);
try
{
if (!file)
system_error::raise("fopen");
if (!att)
Arg::Gds(isc_bad_db_handle).raise();
dump(status, blobId, att, tra, file);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
if (file)
fclose(file);
}
voidUtilInterface::loadBlob(CheckStatusWrapper* status, ISC_QUAD* blobId,
IAttachment* att, ITransaction* tra, constchar* file_name, FB_BOOLEAN txt)
{
/**************************************
*
* l o a d B l o b
*
**************************************
*
* Functional description
* Load a blob with the contents of a file.
*
**************************************/
FILE* file = os_utils::fopen(file_name, txt ? FOPEN_READ_TYPE_TEXT : FOPEN_READ_TYPE);
try
{
if (!file)
system_error::raise("fopen");
if (!att)
Arg::Gds(isc_bad_db_handle).raise();
load(status, blobId, att, tra, file);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
if (file)
fclose(file);
}
voidUtilInterface::getFbVersion(CheckStatusWrapper* status, IAttachment* att,
IVersionCallback* callback)
{
/**************************************
*
* g d s _ $ v e r s i o n
*
**************************************
*
* Functional description
* Obtain and print information about a database.
*
**************************************/
try
{
if (!att)
Arg::Gds(isc_bad_db_handle).raise();
UCharBuffer buffer;
USHORT buf_len = 256;
UCHAR* buf = buffer.getBuffer(buf_len);
const TEXT* versions = 0;
const TEXT* implementations = 0;
const UCHAR* dbis = NULL;
bool redo;
do
{
att->getInfo(status, sizeof(info), info, buf_len, buf);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
ClumpletReader p(ClumpletReader::InfoResponse, buf, buf_len);
for (redo = false; !(redo || p.isEof()); p.moveNext())
{
switch (p.getClumpTag())
{
case isc_info_firebird_version:
versions = (TEXT*) p.getBytes();
break;
case isc_info_implementation:
implementations = (TEXT*) p.getBytes();
break;
case fb_info_implementation:
dbis = p.getBytes();
if (dbis[0] * 6u + 1u > p.getClumpLength())
{
// fb_info_implementation value appears incorrect
dbis = NULL;
}
break;
case isc_info_error:
// old server does not understand fb_info_implementation
break;
case isc_info_truncated:
redo = true;
// fall down...
case isc_info_end:
break;
default:
(Arg::Gds(isc_random) << "Invalid info item").raise();
}
}
// Our buffer wasn't large enough to hold all the information,
// make a larger one and try again.
if (redo)
{
buf_len += 1024;
buf = buffer.getBuffer(buf_len);
}
} while (redo);
UCHAR count = MIN(*versions, *implementations);
++versions;
++implementations;
UCHAR diCount = 0;
if (dbis)
diCount = *dbis++;
string s;
UCHAR diCurrent = 0;
for (UCHAR level = 0; level < count; ++level)
{
const USHORT implementation_nr = *implementations++;
const USHORT impl_class_nr = *implementations++;
constint l = *versions++; // it was UCHAR
const TEXT* implementation_string;
string dbi_string;
if (dbis && dbis[diCurrent * 6 + 5] == level)
{
dbi_string = DbImplementation::pick(&dbis[diCurrent * 6]).implementation();
implementation_string = dbi_string.c_str();
if (++diCurrent >= diCount)
dbis = NULL;
}
else
{
dbi_string = DbImplementation::fromBackwardCompatibleByte(implementation_nr).implementation();
implementation_string = dbi_string.nullStr();
if (!implementation_string)
implementation_string = "**unknown**";
}
const TEXT* class_string;
if (impl_class_nr >= FB_NELEM(impl_class) || !(class_string = impl_class[impl_class_nr]))
class_string = "**unknown**";
s.printf("%s (%s), version \"%.*s\"", implementation_string, class_string, l, versions);
callback->callback(status, s.c_str());
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
versions += l;
}
USHORT ods_version, ods_minor_version;
get_ods_version(status, att, &ods_version, &ods_minor_version);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
return;
s.printf("on disk structure version %d.%d", ods_version, ods_minor_version);
callback->callback(status, s.c_str());
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
YAttachment* UtilInterface::executeCreateDatabase(
Firebird::CheckStatusWrapper* status, unsigned stmtLength, constchar* creatDBstatement,
unsigned dialect, FB_BOOLEAN* stmtIsCreateDb)
{
try
{
bool stmtEaten;
YAttachment* att = NULL;
if (stmtIsCreateDb)
*stmtIsCreateDb = FB_FALSE;
string statement(creatDBstatement,
(stmtLength == 0 && creatDBstatement ? strlen(creatDBstatement) : stmtLength));
if (!PREPARSE_execute(status, &att, statement, &stmtEaten, dialect))
returnNULL;
if (stmtIsCreateDb)
*stmtIsCreateDb = FB_TRUE;
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
returnNULL;
LocalStatus tempStatus;
CheckStatusWrapper tempCheckStatusWrapper(&tempStatus);
ITransaction* crdbTrans = att->startTransaction(status, 0, NULL);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
{
att->dropDatabase(&tempCheckStatusWrapper);
returnNULL;
}
bool v3Error = false;
if (!stmtEaten)
{
att->execute(status, crdbTrans, statement.length(), statement.c_str(), dialect, NULL, NULL, NULL, NULL);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
{
crdbTrans->rollback(&tempCheckStatusWrapper);
att->dropDatabase(&tempCheckStatusWrapper);
returnNULL;
}
}
crdbTrans->commit(status);
if (status->getState() & Firebird::IStatus::STATE_ERRORS)
{
crdbTrans->rollback(&tempCheckStatusWrapper);
att->dropDatabase(&tempCheckStatusWrapper);
returnNULL;
}
return att;
}
catch (const Exception& ex)
{
ex.stuffException(status);
returnNULL;
}
}
voidUtilInterface::decodeDate(ISC_DATE date, unsigned* year, unsigned* month, unsigned* day)
{
tm times;
isc_decode_sql_date(&date, ×);
if (year)
*year = times.tm_year + 1900;
if (month)
*month = times.tm_mon + 1;
if (day)
*day = times.tm_mday;
}
voidUtilInterface::decodeTime(ISC_TIME time,
unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions)
{
tm times;
isc_decode_sql_time(&time, ×);
if (hours)
*hours = times.tm_hour;
if (minutes)
*minutes = times.tm_min;
if (seconds)
*seconds = times.tm_sec;
if (fractions)
*fractions = time % ISC_TIME_SECONDS_PRECISION;
}
voiddecodeTimeTzWithFallback(CheckStatusWrapper* status, const ISC_TIME_TZ* timeTz, SLONG gmtFallback,
unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
try
{
tm times;
int intFractions;
bool tzLookup = TimeZoneUtil::decodeTime(*timeTz, true, gmtFallback, ×, &intFractions);
if (hours)
*hours = times.tm_hour;
if (minutes)
*minutes = times.tm_min;
if (seconds)
*seconds = times.tm_sec;
if (fractions)
*fractions = (unsigned) intFractions;
if (timeZoneBuffer)
TimeZoneUtil::format(timeZoneBuffer, timeZoneBufferLength, timeTz->time_zone, !tzLookup, gmtFallback);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voidUtilInterface::decodeTimeTz(CheckStatusWrapper* status, const ISC_TIME_TZ* timeTz,
unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
decodeTimeTzWithFallback(status, timeTz, TimeZoneUtil::NO_OFFSET,
hours, minutes, seconds, fractions, timeZoneBufferLength, timeZoneBuffer);
}
voidUtilInterface::decodeTimeTzEx(Firebird::CheckStatusWrapper* status, const ISC_TIME_TZ_EX* timeEx,
unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
decodeTimeTzWithFallback(status, reinterpret_cast<const ISC_TIME_TZ*>(timeEx),
timeZoneBuffer ? timeEx->ext_offset : TimeZoneUtil::NO_OFFSET,
hours, minutes, seconds, fractions, timeZoneBufferLength, timeZoneBuffer);
}
voidUtilInterface::encodeTimeTz(CheckStatusWrapper* status, ISC_TIME_TZ* timeTz,
unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions, constchar* timeZone)
{
try
{
timeTz->utc_time = encodeTime(hours, minutes, seconds, fractions);
timeTz->time_zone = TimeZoneUtil::parse(timeZone, strlen(timeZone));
TimeZoneUtil::localTimeToUtc(*timeTz);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voiddecodeTimeStampWithFallback(CheckStatusWrapper* status, const ISC_TIMESTAMP_TZ* timeStampTz, SLONG gmtFallback,
unsigned* year, unsigned* month, unsigned* day, unsigned* hours, unsigned* minutes, unsigned* seconds,
unsigned* fractions, unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
try
{
tm times;
int intFractions;
bool tzLookup = TimeZoneUtil::decodeTimeStamp(*timeStampTz, true, gmtFallback, ×, &intFractions);
if (year)
*year = times.tm_year + 1900;
if (month)
*month = times.tm_mon + 1;
if (day)
*day = times.tm_mday;
if (hours)
*hours = times.tm_hour;
if (minutes)
*minutes = times.tm_min;
if (seconds)
*seconds = times.tm_sec;
if (fractions)
*fractions = (unsigned) intFractions;
if (timeZoneBuffer)
TimeZoneUtil::format(timeZoneBuffer, timeZoneBufferLength, timeStampTz->time_zone, !tzLookup, gmtFallback);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voidUtilInterface::decodeTimeStampTz(CheckStatusWrapper* status, const ISC_TIMESTAMP_TZ* timeStampTz,
unsigned* year, unsigned* month, unsigned* day, unsigned* hours, unsigned* minutes, unsigned* seconds,
unsigned* fractions, unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
decodeTimeStampWithFallback(status, timeStampTz, TimeZoneUtil::NO_OFFSET,
year, month, day, hours, minutes, seconds, fractions, timeZoneBufferLength, timeZoneBuffer);
}
voidUtilInterface::decodeTimeStampTzEx(CheckStatusWrapper* status, const ISC_TIMESTAMP_TZ_EX* timeStampEx,
unsigned* year, unsigned* month, unsigned* day, unsigned* hours, unsigned* minutes, unsigned* seconds,
unsigned* fractions, unsigned timeZoneBufferLength, char* timeZoneBuffer)
{
decodeTimeStampWithFallback(status, reinterpret_cast<const ISC_TIMESTAMP_TZ*>(timeStampEx),
timeZoneBuffer ? timeStampEx->ext_offset : TimeZoneUtil::NO_OFFSET,
year, month, day, hours, minutes, seconds, fractions, timeZoneBufferLength, timeZoneBuffer);
}
voidUtilInterface::encodeTimeStampTz(CheckStatusWrapper* status, ISC_TIMESTAMP_TZ* timeStampTz,
unsigned year, unsigned month, unsigned day, unsigned hours, unsigned minutes, unsigned seconds,
unsigned fractions, constchar* timeZone)
{
try
{
timeStampTz->utc_timestamp.timestamp_date = encodeDate(year, month, day);
timeStampTz->utc_timestamp.timestamp_time = encodeTime(hours, minutes, seconds, fractions);
timeStampTz->time_zone = TimeZoneUtil::parse(timeZone, strlen(timeZone));
TimeZoneUtil::localTimeStampToUtc(*timeStampTz);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
ISC_DATE UtilInterface::encodeDate(unsigned year, unsigned month, unsigned day)
{
tm times;
times.tm_year = year - 1900;
times.tm_mon = month - 1;
times.tm_mday = day;
ISC_DATE date;
isc_encode_sql_date(×, &date);
return date;
}
ISC_TIME UtilInterface::encodeTime(unsigned hours, unsigned minutes, unsigned seconds,
unsigned fractions)
{
tm times;
times.tm_hour = hours;
times.tm_min = minutes;
times.tm_sec = seconds;
ISC_TIME time;
isc_encode_sql_time(×, &time);
time += fractions;
returntime;
}
unsignedUtilInterface::formatStatus(char* buffer, unsigned bufferSize, IStatus* status)
{
unsigned state = status->getState();
unsigned states[] = {IStatus::STATE_ERRORS, IStatus::STATE_WARNINGS};
const ISC_STATUS* vectors[] = {status->getErrors(), status->getWarnings()};
string s;
for (int i = 0; i < 2; ++i)
{
if (state & states[i])
{
const ISC_STATUS* vector = vectors[i];
SLONG n;
while ((n = fb_interpret(buffer, bufferSize, &vector)) != 0)
{
if (!s.empty())
s += "\n-";
s += string(buffer, n);
}
}
}
unsigned ret = MIN((unsigned) s.length(), bufferSize);
memcpy(buffer, s.c_str(), ret);
if (ret < bufferSize)
buffer[ret] = 0;
return ret;
}
unsignedUtilInterface::getClientVersion()
{
int fv[] = { FILE_VER_NUMBER };
return fv[0] * 0x100 + fv[1];
}
// End-user proxy for ClumpletReader & Writer
classXpbBuilderfinal : public DisposeIface<IXpbBuilderImpl<XpbBuilder, CheckStatusWrapper> >
{
public:
XpbBuilder(unsigned kind, constunsignedchar* buf, unsigned len)
: pb(NULL), strVal(getPool())
{
ClumpletReader::Kind k;
UCHAR tag = 0;
const ClumpletReader::KindList* kl = NULL;
switch(kind)
{
case DPB:
kl = ClumpletReader::dpbList;
break;
case SPB_ATTACH:
kl = ClumpletReader::spbList;
break;
case SPB_START:
k = ClumpletReader::SpbStart;
break;
case TPB:
k = ClumpletReader::Tpb;
tag = isc_tpb_version3;
break;
case BATCH:
k = ClumpletReader::WideTagged;
tag = IBatch::VERSION1;
break;
case BPB:
k = ClumpletReader::Tagged;
tag = isc_bpb_version1;
break;
case SPB_SEND:
k = ClumpletReader::SpbSendItems;
break;
case SPB_RECEIVE:
k = ClumpletReader::SpbReceiveItems;
break;
case SPB_RESPONSE:
k = ClumpletReader::SpbResponse;
break;
case INFO_SEND:
k = ClumpletReader::InfoItems;
break;
case INFO_RESPONSE:
k = ClumpletReader::InfoResponse;
break;
default:
fatal_exception::raiseFmt("Wrong parameters block kind %d, should be from %d to %d", kind, DPB, INFO_RESPONSE);
break;
}
if (!buf)
{
if (kl)
pb = FB_NEW_POOL(getPool()) ClumpletWriter(getPool(), kl, MAX_DPB_SIZE);
else
pb = FB_NEW_POOL(getPool()) ClumpletWriter(getPool(), k, MAX_DPB_SIZE, tag);
}
else
{
if (kl)
pb = FB_NEW_POOL(getPool()) ClumpletWriter(getPool(), kl, MAX_DPB_SIZE, buf, len);
else
pb = FB_NEW_POOL(getPool()) ClumpletWriter(getPool(), k, MAX_DPB_SIZE, buf, len);
}
}
// IXpbBuilder implementation
voidclear(CheckStatusWrapper* status)
{
try
{
pb->clear();
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voidremoveCurrent(CheckStatusWrapper* status)
{
try
{
pb->deleteClumplet();
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voidinsertInt(CheckStatusWrapper* status, unsignedchar tag, int value)
{
try
{
pb->insertInt(tag, value);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voidinsertBigInt(CheckStatusWrapper* status, unsignedchar tag, ISC_INT64 value)
{
try
{
pb->insertBigInt(tag, value);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
voidinsertBytes(CheckStatusWrapper* status, unsignedchar tag, constvoid* bytes, unsigned length)
{
try
{
pb->insertBytes(tag, bytes, length);
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}