This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathrevdb.cpp
More file actions
2354 lines (2018 loc) · 66.5 KB
/
revdb.cpp
File metadata and controls
2354 lines (2018 loc) · 66.5 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
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
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "large_buffer.h"
#if defined(_WINDOWS) || defined(_WINDOWS_SERVER)
#include "w32support.h"
#elif defined(_LINUX) || defined(_LINUX_SERVER) || defined(TARGET_SUBPLATFORM_ANDROID)
#include "unxsupport.h"
#elif defined(_MACOSX) || defined (_MAC_SERVER)
#include "osxsupport.h"
#elif defined(TARGET_SUBPLATFORM_IPHONE)
#include "iossupport.h"
#endif
#if defined(_WINDOWS)
#pragma optimize("", off)
#endif
#include <revolution/external.h>
#include <revolution/support.h>
#include "dbdriver.h"
#include "dbdrivercommon.h"
#define INTSTRSIZE 16
#define STDRESULTSIZE 32
#define DEMOSIZE 64000
#define REVDB_VERSIONSTRING "3.0.0"
#define istrdup strdup
static unsigned int idcounter = 0;
char *MCS_resolvepath(const char *path);
static char *revdbdriverpaths = NULL;
static Bool REVDBinited = True;
unsigned int *DBObject::idcounter = NULL;
enum RevDBErrs
{
REVDBERR_LICENSE = 0,
REVDBERR_SYNTAX,
REVDBERR_DBTYPE,
REVDBERR_BADCONNECTION,
REVDBERR_BADCURSOR,
REVDBERR_BADCOLUMNNUM,
REVDBERR_BADCOLUMNNAME,
REVDBERR_BADTABLE,
REVDBERR_NOT_SUPPORTED,
REVDBERR_NOFILEPERMS,
REVDBERR_NONETPERMS,
};
const char *errors[] = {
"revdberr, restricted under current license",
"revdberr,syntax error",
"revdberr,invalid database type",
"revdberr,invalid connection id",
"revdberr,invalid cursor id",
"revdberr,invalid column number",
"revdberr,invalid column name",
"revdberr,invalid table name",
"revdberr,not supported by driver",
"revdberr,file access not permitted",
"revdberr,network access not permitted",
};
#define REVDB_PERMISSION_NONE (0)
#define REVDB_PERMISSION_FILE (1<<0)
#define REVDB_PERMISSION_NETWORK (1<<1)
enum REVDBDatabaseType
{
kREVDBDatabaseTypeOracle,
kREVDBDatabaseTypeMySQL,
kREVDBDatabaseTypePostgreSQL,
kREVDBDatabaseTypeSQLite,
kREVDBDatabaseTypeODBC,
kREVDBDatabaseTypeValentina,
};
static const char *REVDBdatabasetypestrings[] = {
"oracle",
"mysql",
"postgresql",
"sqlite",
"odbc",
"valentina",
};
#define REVDB_DATABASETYPECOUNT (sizeof(REVDBdatabasetypestrings) / sizeof(char *))
// don't handle permissions for oracle, valentina as these drivers won't be included in plugin
static int REVDBdatabasepermissions[] = {
REVDB_PERMISSION_NONE,
REVDB_PERMISSION_NETWORK,
REVDB_PERMISSION_NETWORK,
REVDB_PERMISSION_FILE,
REVDB_PERMISSION_FILE | REVDB_PERMISSION_NETWORK,
REVDB_PERMISSION_NONE,
};
const char *dbtypestrings[] = {
"NULL",
"BIT",
"CHAR",
"STRING",
"WSTRING",
"BLOB",
"TIMESTAMP",
"DATE",
"TIME",
"DATETIME",
"FLOAT",
"DOUBLE",
"INTEGER",
"SMALLINT",
"WORD",
"BOOLEAN",
"LONG"
};
static void *DBcallback_loadmodule(const char *p_path)
{
int t_success;
void *t_handle;
LoadModuleByName(p_path, &t_handle, &t_success);
if (t_success == EXTERNAL_FAILURE)
return NULL;
return t_handle;
}
static void DBcallback_unloadmodule(void *p_handle)
{
int t_success;
UnloadModule(p_handle, &t_success);
}
static void *DBcallback_resolvesymbol(void *p_handle, const char *p_symbol)
{
int t_success;
void *t_address;
ResolveSymbolInModule(p_handle, p_symbol, &t_address, &t_success);
if (t_success == EXTERNAL_FAILURE)
return NULL;
return t_address;
}
static DBcallbacks dbcallbacks = {
DBcallbacks_version,
DBcallback_loadmodule,
DBcallback_unloadmodule,
DBcallback_resolvesymbol,
};
DATABASERECList databaselist;
DBList connectionlist;
#define simpleparse(a,b,c) (((b > a) | (c < a))?True:False)
static char *strlwr(char *str)
{
while (*str)
{
*str = tolower(*str);
str++;
}
return str;
}
void REVDB_Init(char *args[], int nargs, char **retstring,
Bool *pass, Bool *error)
{
*error = False;
*pass = False;
*retstring = (char *)calloc(1, 1);
}
/// Returns the version of revdb.
void REVDB_Version(char *args[], int nargs, char **retstring,
Bool *pass, Bool *error)
{
char *result = NULL;
*error = False;
*pass = False;
result = istrdup(REVDB_VERSIONSTRING);
*retstring = (result != NULL ? result : (char *)calloc(1,1));
}
int util_stringcompare(const char *s1, const char *s2, int n)
{
int i;
char c1, c2;
for (i=0; i<n; i++)
{
c1 = tolower(*s1++);
c2 = tolower(*s2++);
if (c1 < c2) return -1;
if (c1 > c2) return 1;
if (!c1) return 0;
}
return 0;
}
//utility function for iterating over connectionlist to find cursor
DBCursor *findcursor(int cursid)
{
DBObjectList *connlist = connectionlist.getList();
DBObjectList::iterator theIterator;
for (theIterator = connlist->begin(); theIterator != connlist->end(); theIterator++){
DBConnection *curconnection = (DBConnection *)(*theIterator);
DBCursor *tcursor = curconnection->findCursor(cursid);
if (tcursor) return tcursor;
}
return NULL;
}
// AL-2015-02-10: [[ SB Inclusions ]] Add function to load database driver using new module loading callbacks
DATABASEREC *LoadDatabaseDriverFromName(const char *p_type)
{
int t_retvalue;
void *t_handle;
t_handle = NULL;
LoadModuleByName(p_type, &t_handle, &t_retvalue);
if (t_handle == NULL)
return NULL;
DATABASEREC *t_result;
t_result = new (nothrow) DATABASEREC;
t_result -> driverref = t_handle;
void *id_counterref_ptr, *new_connectionref_ptr, *release_connectionref_ptr;
void *set_callbacksref_ptr;
id_counterref_ptr = NULL;
new_connectionref_ptr = NULL;
release_connectionref_ptr = NULL;
set_callbacksref_ptr = NULL;
ResolveSymbolInModule(t_handle, "setidcounterref", &id_counterref_ptr, &t_retvalue);
ResolveSymbolInModule(t_handle, "newdbconnectionref", &new_connectionref_ptr, &t_retvalue);
ResolveSymbolInModule(t_handle, "releasedbconnectionref", &release_connectionref_ptr, &t_retvalue);
ResolveSymbolInModule(t_handle, "setcallbacksref", &set_callbacksref_ptr, &t_retvalue);
t_result -> idcounterptr = (idcounterrefptr)id_counterref_ptr;
t_result -> newconnectionptr = (new_connectionrefptr)new_connectionref_ptr;
t_result -> releaseconnectionptr = (release_connectionrefptr)release_connectionref_ptr;
t_result -> setcallbacksptr = (set_callbacksrefptr)set_callbacksref_ptr;
return t_result;
}
DATABASEREC *LoadDatabaseDriverInFolder(const char *p_folder,
const char *p_type)
{
DATABASEREC *t_database_rec = nullptr;
char t_libname[256];
if (0 > snprintf(t_libname,
sizeof(t_libname),
"%s/db%s",
p_folder,
p_type))
{
return nullptr;
}
t_database_rec = LoadDatabaseDriverFromName(t_libname);
return t_database_rec;
}
DATABASEREC *LoadDatabaseDriver(const char *p_type)
{
DATABASEREC *t_database_rec = nullptr;
char t_type[32];
strcpy(t_type, p_type);
strlwr(t_type);
if (revdbdriverpaths != nullptr)
{
t_database_rec = LoadDatabaseDriverInFolder(revdbdriverpaths, p_type);
}
else
{
t_database_rec = LoadDatabaseDriverInFolder(".", t_type);
if (t_database_rec == nullptr)
t_database_rec = LoadDatabaseDriverInFolder("./Database Drivers",
t_type);
if (t_database_rec == nullptr)
t_database_rec = LoadDatabaseDriverInFolder("./database_drivers",
t_type);
if (t_database_rec == nullptr)
t_database_rec = LoadDatabaseDriverInFolder("./drivers",
t_type);
}
if (t_database_rec != nullptr)
{
databaselist . push_back(t_database_rec);
strcpy(t_database_rec -> dbname, p_type);
if (t_database_rec -> idcounterptr)
(*t_database_rec -> idcounterptr)(&idcounter);
if (t_database_rec -> setcallbacksptr)
(*t_database_rec -> setcallbacksptr)(&dbcallbacks);
}
return t_database_rec;
}
void UnloadDatabaseDriver(DATABASEREC *rec)
{
int t_retvalue;
UnloadModule(rec->driverref, &t_retvalue);
delete rec;
}
void REVDB_INIT()
{
}
void REVDB_QUIT()
{
DBObjectList::iterator theIterator;
DBObjectList *connlist = connectionlist.getList();
for (theIterator = connlist->begin(); theIterator != connlist->end(); theIterator++){
DBObject *curobject = (DBObject *)(*theIterator);
DATABASEREC *databaserec = NULL;
DATABASERECList::iterator theIterator2;
DBConnection *tconnection = (DBConnection *)curobject;
for (theIterator2 = databaselist.begin(); theIterator2 != databaselist.end(); theIterator2++){
DATABASEREC *tdatabaserec = (DATABASEREC *)(*theIterator2);
if (util_stringcompare(tdatabaserec->dbname,tconnection->getconnectionstring(),
strlen(tconnection->getconnectionstring())) == 0){
databaserec = tdatabaserec;
break;
}
}
if (databaserec != NULL && databaserec ->releaseconnectionptr != NULL)
(*databaserec->releaseconnectionptr)(tconnection);
}
connlist->clear();
DATABASERECList::iterator theIterator2;
for (theIterator2 = databaselist.begin(); theIterator2 != databaselist.end(); theIterator2++){
DATABASEREC *tdatabaserec = (DATABASEREC *)(*theIterator2);
UnloadDatabaseDriver(tdatabaserec);
}
databaselist.clear();
}
//get column number by name
int findcolumn(DBCursor *thecursor, char *colname)
{
int i;
for (i = 1; i <= thecursor->getFieldCount();i++)
if (util_stringcompare(colname,thecursor->getFieldName(i),strlen(colname)+1) == 0)
return i;
return 0;
}
/// @brief Utility function to parse a variable name that might be an array reference
/// @param p_full_name null terminated string containing the full variable name to parse
/// @param p_variable_name (output) a pointer that will be set to the address of a buffer containing the variable name
/// @param p_key_name (output) a pointer that will be set to the address of a buffer containing the key name.
/// The variable name can be something like "tVariable" in which case p_key_name will be set to null
/// and p_variable_name to "tVariable. Alternatively the variable name can be something like "tArray[key]", in which
/// case p_variable_name will be set to "tArray" and p_key_name will be set to "key". Both p_variable_name and p_key_name
/// should be free by the caller.
void parseVariableName(char *p_full_name, char *&p_variable_name, char *&p_key_name)
{
char *t_key_start;
t_key_start = strstr(p_full_name, "[");
if (t_key_start != NULL)
{
char *t_end;
t_end = &p_full_name[strlen(p_full_name) - 1];
p_variable_name = (char *)malloc(sizeof(char) * (t_key_start - p_full_name) + 1);
memcpy(p_variable_name, p_full_name, ((t_key_start - p_full_name)));
p_variable_name[t_key_start - p_full_name] = '\0';
p_key_name = (char *)malloc(sizeof(char) * (t_end - t_key_start));
memcpy(p_key_name, t_key_start + 1, t_end - t_key_start - 1);
p_key_name[t_end - t_key_start - 1] = '\0';
}
else
{
p_key_name = NULL;
p_variable_name = (char *)malloc(sizeof(char) * strlen(p_full_name) + 1);
strcpy(p_variable_name, p_full_name);
}
}
/// @brief Utility function to get the value of a variable that may be an array element. Also returns whether the value is binary.
/// @param p_variable_name null terminated string containing the name of the variable or array.
/// @param p_key_name null terminated string containing the name of the array key or NULL if not an array element.
/// @param r_value The value and length of the variable is placed into this return parameter
/// @param r_is_binary This is set to true if the variable is binary, false otherwise.
void processInputArray(char *p_variable_name, char *p_key_name, ExternalString &r_value, Bool &r_is_binary)
{
// OK-2007-10-05 : Changed behavior to use the variable name / key name to determine if the value is binary.
// Previous behavior checked the first two chars of the actual value. The variable and key name are adjusted if appropriate
// to remove the *b from them in order to allow their values to be retrieved.
r_is_binary = False;
char *t_adjusted_variable_name;
t_adjusted_variable_name = p_variable_name;
char *t_adjusted_key_name;
t_adjusted_key_name = p_key_name;
// If the variable / array name is prefixed by *b then the value is binary. (If the variable is an array prefixed by *b then
// all elements are treated as binary).
if (p_variable_name != NULL && strlen(p_variable_name) >= 2 && p_variable_name[0] == '*' && p_variable_name[1] == 'b')
{
r_is_binary = True;
// Remove the first two chars
t_adjusted_variable_name = t_adjusted_variable_name + 2;
}
// If the variable is an array element and the key name is prefixed by *b then it is binary.
if (p_key_name != NULL && strlen(p_key_name) >= 2 && p_key_name[0] == '*' && p_key_name[1] == 'b')
{
r_is_binary = True;
// Remove the first two chars
t_adjusted_key_name = t_adjusted_key_name + 2;
}
ExternalString t_value;
t_value . buffer = NULL;
t_value . length = 0;
int t_return_value;
GetVariableEx(t_adjusted_variable_name, t_adjusted_key_name == NULL ? "" : t_adjusted_key_name, &t_value, &t_return_value);
r_value = t_value;
}
char **s_sort_keys;
static int SortKeysCallback(const void *a, const void *b)
{
unsigned int t_index_a, t_index_b;
t_index_a = *(unsigned int *)a;
t_index_b = *(unsigned int *)b;
char *t_key_a, *t_key_b;
t_key_a = s_sort_keys[t_index_a];
t_key_b = s_sort_keys[t_index_b];
if (t_key_a[0] == '*' && t_key_a[1] == 'b')
t_key_a += 2;
if (t_key_b[0] == '*' && t_key_b[1] == 'b')
t_key_b += 2;
int t_key_index_a, t_key_index_b;
t_key_index_a = atoi(t_key_a);
t_key_index_b = atoi(t_key_b);
return t_key_index_a - t_key_index_b;
}
//extract arguments from variable list or array
DBString *BindVariables(char *p_arguments[],int p_argument_count, int &r_value_count)
{
DBString *t_values;
t_values = NULL;
r_value_count = 0;
// p_argument_count includes the connection id and the query. Therefore if it's 2 or less this means
// there are no parameters to bind.
if (p_argument_count <= 2)
return t_values;
int t_return_value;
// If there is only a single argument to bind, it may be an array. If there are more than 3 arguments, none of them
// can be arrays.
if (p_argument_count == 3)
{
int t_element_count;
t_element_count = 0;
// Work out if the third argument is an array or not
GetArray(p_arguments[2], &t_element_count, NULL, NULL, &t_return_value);
char **t_array_keys;
ExternalString *t_array_values;
if (t_element_count != 0)
{
t_array_keys = (char **)malloc(sizeof(char *) * t_element_count);
t_array_values = (ExternalString *)malloc(sizeof(ExternalString) * t_element_count);
GetArray(p_arguments[2], &t_element_count, t_array_values, t_array_keys, &t_return_value);
// MW-2008-02-29: [[ Bug 5893 ]] We need to sort the list of keys and values before binding
// as keys are unordered. To do this we create a temporary sequence of integers, and use
// qsort. This 'map' is then used to fetch keys and values from the array.
unsigned int *t_map;
t_map = (unsigned int *)malloc(sizeof(unsigned int) * t_element_count);
for(int i = 0; i < t_element_count; ++i)
t_map[i] = i;
s_sort_keys = t_array_keys;
qsort(t_map, t_element_count, sizeof(unsigned int), SortKeysCallback);
// If there are multiple elements...
t_values = new (nothrow) DBString[t_element_count];
for (int i = 0; i < t_element_count; i++)
{
char *t_key_buffer;
t_key_buffer = t_array_keys[t_map[i]];
// Ensure that the key is an integer (and at the same time determine if the element is binary)
char *t_end_pointer;
Bool t_is_binary;
t_is_binary = False;
strtol(t_key_buffer, &t_end_pointer, 10);
if (*t_end_pointer != 0)
{
if (strlen(t_key_buffer) >= 2 && t_key_buffer[0] == '*' && t_key_buffer[1] == 'b')
{
strtol((t_key_buffer + 2), &t_end_pointer, 10);
if (*t_end_pointer != 0)
{
// This array element is neither an integer or an integer prefixed by "*b"
// therefore we ignore it and skip onto the next element.
continue;
}
else
t_is_binary = True;
}
else
t_is_binary = False;
}
// OK-2008-12-09: Because the engine retains ownership of t_value . buffer, using it can lead
// to problems where the engine overwrites the buffer, leading to the wrong values being
// inserted into databases. The problem is fixed by creating duplicates of any buffers returned.
char *t_new_buffer;
t_new_buffer = (char *)malloc(t_array_values[t_map[i]] . length);
memcpy((void *)t_new_buffer, t_array_values[t_map[i]] . buffer, t_array_values[t_map[i]] . length);
t_values[r_value_count] . Set(t_new_buffer, t_array_values[t_map[i]] . length, t_is_binary);
r_value_count += 1;
}
free(t_map);
free(t_array_keys);
free(t_array_values);
return t_values;
}
}
t_values = new (nothrow) DBString[p_argument_count - 2];
for (int i = 2; i < p_argument_count; i++)
{
Bool t_is_binary;
t_is_binary = False;
char *t_name;
t_name = p_arguments[i];
if (strlen(t_name) >= 2 && (t_name[0] == '*' && t_name[1] == 'b'))
{
t_is_binary = True;
t_name += 2;
}
ExternalString t_value;
t_value . buffer = NULL;
t_value . length = 0;
// OK-2007-07-16: Enhancement 3603, allow for variable names in the form "pArray[key]"
char *t_variable_name;
char *t_key_name;
parseVariableName(t_name, t_variable_name, t_key_name);
GetVariableEx(t_variable_name, t_key_name != NULL ? t_key_name : "", &t_value, &t_return_value);
free(t_variable_name);
free(t_key_name);
if (t_value . buffer != NULL)
{
// OK-2008-12-09: Because the engine retains ownership of t_value . buffer, using it can lead
// to problems where the engine overwrites the buffer, leading to the wrong values being
// inserted into databases. The problem is fixed by creating duplicates of any buffers returned.
char *t_new_buffer;
t_new_buffer = (char *)malloc(t_value . length);
memcpy(t_new_buffer, t_value . buffer, t_value . length);
t_values[r_value_count] . Set(t_new_buffer, t_value . length, t_is_binary);
r_value_count += 1;
}
}
return t_values;
}
//utility function to get column data by number
void GetColumnByNumber(DBCursor *thecursor, char *&result, int columnid, char *varname)
{
unsigned int colsize;
colsize = 0;
// OK-2007-06-18 : Part of fix for bug 4211
char *coldata;
coldata = NULL;
if (thecursor -> getRecordCount() != 0)
coldata = thecursor -> getFieldDataBinary(columnid, colsize);
else
{
coldata = (char *)malloc(sizeof(char) * 1);
*coldata = '\0';
}
if (colsize == 0xFFFFFFFF)
colsize = strlen(coldata);
if (coldata)
{
if (varname)
{
// OK-2007-07-04: Bug 3602. Enhancement request to allow variable name to be an array key.
char *t_variable;
char *t_key;
parseVariableName(varname, t_variable, t_key);
int retvalue;
ExternalString val;
val.buffer = coldata;
val.length = colsize;
if (!REVDBinited && thecursor -> getConnection() -> getConnectionType() == CT_ORACLE && val.length > 64000)
result = istrdup(errors[REVDBERR_LICENSE]);
else
SetVariableEx(t_variable, t_key == NULL ? "" : t_key, &val, &retvalue);
free(t_variable);
free(t_key);
}
else
{
result = (char *)malloc(colsize + 1);
memcpy(result, coldata, colsize);
result[colsize] = '\0';
}
}
else
result = istrdup(errors[REVDBERR_BADCOLUMNNUM]);
if (thecursor -> getRecordCount() == 0)
free(coldata);
}
inline const char *BooltoStr(Bool b) {return b == True?"True":"False";}
/// @brief Sets the location that revdb should look for drivers next time a connection is attempted.
/// @param driverPath The folder path to look for drivers in. This should be a path in LiveCode format.
/// No input checking is carried out, if the path is invalid, then the intended drivers will just not be found.
void REVDB_SetDriverPath(char *args[], int nargs, char **retstring,
Bool *pass, Bool *error)
{
*error = False;
*pass = False;
if (nargs == 1)
{
if (revdbdriverpaths != NULL)
free(revdbdriverpaths);
revdbdriverpaths = istrdup(args[0]);
}
*retstring = static_cast<char*>(calloc(1,1));
}
void REVDB_GetDriverPath(char *p_arguments[], int p_argument_count, char **r_return_string, Bool *r_pass, Bool *r_error)
{
*r_error = False;
*r_pass = False;
*r_return_string = istrdup(revdbdriverpaths);
}
/// @brief Opens a connection to a database.
/// @param databaseType String used to determine which database driver is loaded.
/// @param host The host to connect to in the format address:port.
/// @param databaseName The name of the database to use.
/// @param username The username to log into the database with.
/// @param password The password to log into the database with.
/// @param useSSL Whether to use SSL or not. This parameter is ignored by all drivers except MySQL. The default for MySQL, if this parameter is empty is to use SSL.
/// @param valentinaCacheSize Ignored by all other drivers.
/// @param valentinaMacSerial Ignored by all other drivers.
/// @param valentinaWindowsSerial Ignored by all other drivers.
///
/// @return An integer connection id if successful. Otherwise the return value is an error string from the driver that describes the problem. The SQLite driver returns
/// empty if there was an error connecting. Throws an error if less than 5 arguments are given.
///
/// Finds and loads the appropriate database driver, then obtains access to a connection object from the driver.
/// Calls the connect() method of the connection object, passing all the parameters except the database type.
/// The only difference in semantics between drivers is that MySQL takes the useSSL parameter and Valentina takes the valentinaCacheSize,
/// valentinaMacSerial and valentinaWindowsSerial parameters.
void REVDB_Connect(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
*error = True;
*pass = False;
// MW-2014-01-30: [[ Sqlite382 ]] Make this a little more flexible - only require at least
// one argument.
if (nargs >= 1)
{
*error = False;
char *dbtype = args[0];
DBConnection *newconnection = NULL;
DATABASEREC *databaserec = NULL;
DATABASERECList::iterator theIterator;
for (theIterator = databaselist.begin(); theIterator != databaselist.end(); theIterator++)
{
DATABASEREC *tdatabaserec = (DATABASEREC *)(*theIterator);
if (util_stringcompare(tdatabaserec->dbname,dbtype,strlen(dbtype)) == 0)
{
databaserec = tdatabaserec;
break;
}
}
// check access permissions of known database types
size_t t_dbtype_index;
for (t_dbtype_index = 0; t_dbtype_index < REVDB_DATABASETYPECOUNT; t_dbtype_index++)
{
if (util_stringcompare(REVDBdatabasetypestrings[t_dbtype_index], dbtype, strlen(dbtype)) == 0)
break;
}
if (t_dbtype_index < REVDB_DATABASETYPECOUNT)
{
if ((REVDBdatabasepermissions[t_dbtype_index] & REVDB_PERMISSION_FILE) && !SecurityCanAccessFile(args[1]))
{
*error = True;
result = istrdup(errors[REVDBERR_NOFILEPERMS]);
}
else if ((REVDBdatabasepermissions[t_dbtype_index] & REVDB_PERMISSION_NETWORK) && !SecurityCanAccessHost(args[1]))
{
*error = True;
result = istrdup(errors[REVDBERR_NONETPERMS]);
}
}
if (!*error)
{
if (!databaserec)
databaserec = LoadDatabaseDriver(dbtype);
if (databaserec && databaserec ->newconnectionptr)
newconnection = (*databaserec->newconnectionptr)();
if (newconnection != NULL)
{
if (newconnection->connect(&args[1],nargs-1))
{
connectionlist.add(newconnection);
unsigned int connid = newconnection->GetID();
result = (char *)malloc(INTSTRSIZE);
sprintf(result,"%d",connid);
}
else
{
result = istrdup(newconnection->getErrorMessage());
if (databaserec && databaserec ->releaseconnectionptr)
(*databaserec->releaseconnectionptr)(newconnection);
}
}
else result = istrdup(errors[REVDBERR_DBTYPE]);
}
}
else
{
result = istrdup(errors[REVDBERR_SYNTAX]);
*error = True;
}
*retstring = (result != NULL ? result : (char *)calloc(1,1));
}
/// @brief Closes a connection to a database
/// @param connectionId The integer id of the connection to close.
/// @return Empty if successful, an error string otherwise.
///
/// Locates the appropriate connection object by its id, then uses the DATABASEREC::releaseconnectionptr method
/// to release the connection. Throws an error if the wrong number of parameters is given. If pConnectionId is invalid
/// then REVDB_Disconnect returns an error string.
void REVDB_Disconnect(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
*error = True;
*pass = False;
if (nargs != 1)
{
*retstring = istrdup(errors[REVDBERR_SYNTAX]);
*error = True;
return;
}
*error = False;
unsigned int connectionid = strtoul (*args, NULL, 10);
if (!connectionlist . find(connectionid))
{
*retstring = istrdup(errors[REVDBERR_BADCONNECTION]);
*error = True;
return;
}
DBObjectList::iterator t_iterator;
DBObjectList *connlist = connectionlist.getList();
for (t_iterator = connlist -> begin(); t_iterator != connlist -> end(); t_iterator++)
{
DBObject *curobject = (DBObject *)(*t_iterator);
if (curobject -> GetID() == connectionid)
{
DATABASEREC *databaserec = NULL;
DATABASERECList::iterator theIterator2;
DBConnection *tconnection = (DBConnection *)curobject;
for (theIterator2 = databaselist.begin(); theIterator2 != databaselist.end(); theIterator2++)
{
DATABASEREC *tdatabaserec = (DATABASEREC *)(*theIterator2);
if ((util_stringcompare(tdatabaserec->dbname,tconnection->getconnectionstring(), strlen(tconnection->getconnectionstring())) == 0) || (util_stringcompare(tdatabaserec->dbname,"odbc", strlen("odbc")) == 0))
{
databaserec = tdatabaserec;
break;
}
}
if (databaserec && databaserec->releaseconnectionptr)
{
(*databaserec -> releaseconnectionptr)(tconnection);
}
connlist->erase(t_iterator);
break;
}
}
*retstring = static_cast<char*>(calloc(1,1));
}
/// @brief Commit the last transaction.
/// @param connectionId The integer id of the connection to use.
/// @return Empty if successful, an error string otherwise.
///
/// Throws an error if the wrong number of parameters is given. Returns an error string
/// If pConnectionId is invalid then an error string is returned, otherwise will always return empty.
/// The behavior of this command depends on the driver being used in the following manner.
/// @par MySQL
/// Has no affect.
/// @par ODBC
/// Has no affect.
/// @par Oracle
/// Commits the last transaction using ocom()
/// @par Postgresql
/// Commits the last transaction using PQExec(..., "COMMIT").
/// @par SQLite
/// Commits the last transaction using basicExec("commit").
void REVDB_Commit(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
*error = True;
*pass = False;
if (nargs == 1)
{
*error = False;
int connectionid = atoi(*args);
DBConnection *theconnection = (DBConnection *)connectionlist.find(connectionid);
if (theconnection)
theconnection->transCommit();
else
{
result = istrdup(errors[REVDBERR_BADCONNECTION]);
*error = True;
}
}
else
{
result = istrdup(errors[REVDBERR_SYNTAX]);
*error = True;
}
*retstring = (result != NULL ? result : (char *)calloc(1,1));
}
/// @brief Rollback the last transaction
/// @param connectionId The integer id of the connection to use.
///
/// Throws an error if the wrong number of parameters are given. Returns an error string if an invalid
/// connection id is given, otherwise returns empty.
/// The behavior of this command depends on the driving being used in the following manner.
/// @par MySQL
/// Has no affect.
/// @par ODBC
/// Has no affect.
/// @par Oracle
/// Rolls back the last transaction using orol().
/// @par Postgresql
/// Rolls back the last trasaction using PQExec(..., "ROLLBACK").
/// @par SQLite
/// Rolls back the last transaction using basicExec("rollback").
/// \return Empty if successful, an error string otherwise.
void REVDB_Rollback(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
*error = True;
*pass = False;
if (nargs == 1)
{
*error = False;
int connectionid = atoi(*args);
DBConnection *theconnection = (DBConnection *)connectionlist.find(connectionid);
if (theconnection)
theconnection->transCommit();
else
{
result = istrdup(errors[REVDBERR_BADCONNECTION]);
*error = True;
}
}
else
result = istrdup(errors[REVDBERR_SYNTAX]);
*retstring = (result != NULL ? result : (char *)calloc(1,1));
}
/// @brief Returns the most recent connection error for a database.
/// @param connectionId The integer id of the connection to use.
///
/// Throws an error if the wrong number of parameters is given. Returns an error string if
/// an invalid connection id is given. Otherwise, the return value of this function depends on the current driver in the following manner.
/// @par MySQL
/// Returns error message for the most recent invoked API call that failed.
/// @par ODBC
/// Returns the first 512 characters of the error message for the most recent API call that failed.
/// @par Oracle
/// Returns the first 512 characters of the error message for the most recent API call that failed.
/// @par Postgresql
/// Returns the error message most recently generated by an operation on the connection.
/// @par SQLite
/// Returns error message for the last API call that failed.
void REVDB_ConnectionErr(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
*error = True;
*pass = False;
if (nargs == 1)
{
*error = False;
int connectionid = atoi(*args);
DBConnection *theconnection = (DBConnection *)connectionlist.find(connectionid);
if (theconnection)
// AL-2013-11-08 [[ Bug 11149 ]] Make sure most recent error string is available to revDatabaseConnectResult
result = istrdup(theconnection->getErrorMessage(True));