-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteLib.livecodescript
More file actions
8030 lines (6024 loc) · 201 KB
/
SQLiteLib.livecodescript
File metadata and controls
8030 lines (6024 loc) · 201 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
script "SQLiteLib"
--==================================================================--
--
-- SQLiteLib
--
-- library of handlers and functions based on revIgniter 2.1.6 to be used with SQLite
--
-- ©2019 revIgniter.com
--
-- Version 1.1.2
--
-- License: Apache 2.0
--
--==================================================================--
--> Library
--==================================================================--
--
-- Start using/Stop using
--
on libraryStack
if the short name of the target = the short name of me then
-- Put initialization stuff here:
dbSetInitialValues
if the environment is "development" then
answer the target & " got libraryStack message"
end if
--
else
pass libraryStack
end if
end libraryStack
on releaseStack
if the short name of the target = the short name of me then
-- Put cleanup stuff here:
get rigCloseDB()
if the environment is "development" then
answer the target & " got releaseStack message"
end if
--
else
pass releaseStack
end if
end releaseStack
--==================================================================--
--
-- Main Stuff
--
--> Declarations
local sScriptLocal = "test"
local sDB, sActiveGroup, sDatabaseID, sDBdriverSettings
--> Handler
/*----------------------------------------------------------------------
--| CONTROL STRUCTURE getprop
--|
--| Author: Trevor DeVore
--| Version: 1.0
--| Created: 15-10-08
--| Last Mod: --
--| Requires: --
--|
--| Summary: Access the values of script local variables.
--|
--| Format: put the uScriptLocal["sScriptLocal"] of stack "SQLiteLib"
--|
--| Parameters: <pVarName>
----------------------------------------------------------------------*/
getprop uScriptLocal[pVarName]
local theDo
put "return" && pVarName into theDo
do theDo
end uScriptLocal
/*----------------------------------------------------------------------
--| CONTROL STRUCTURE setprop
--|
--| Author: rabit
--| Version: 1.0
--| Created: 18-04-14
--| Last Mod: --
--| Requires: --
--|
--| Summary: Set values of script local variables.
--|
--| Format: set the uScriptLocal["sScriptLocal"] of stack "SQLiteLib" to value
--|
--| Parameters: <pVarName>, <pVal>
----------------------------------------------------------------------*/
setprop uScriptLocal[pVarName] pVal
local tVal, theDo
# ARRAYS CAN NOT BE USED WITH do
# SO, CONVERT THEM TO STRINGS
if pVal is an array then
put base64Encode(arrayEncode(pVal)) into tVal
else
put pVal into tVal
end if
put "put" && quote & tVal & quote && "into" && pVarName into theDo
do theDo
# CONVERT THE VALUE OF THE LOCAL VARIABLE TO AN ARRAY AGAIN
if pVal is an array then
put "put arrayDecode(base64Decode(" & pVarName & ")) into" && pVarName into theDo
do theDo
end if
end uScriptLocal
/*----------------------------------------------------------------------
--| FUNCTION closeDBCursor
--|
--| Author: rabit
--| Version: 1.0
--| Created: 09-04-14
--| Last Mod: --
--| Requires: --
--|
--| Summary: Close a record set.
--|
--| Format: closeDBCursor(param1)
--|
--| Parameters: integer <pCursorID>
--|
--| Return: mixed
----------------------------------------------------------------------*/
function closeDBCursor pCursorID
try
revCloseCursor pCursorID
catch e
# NOTE debugging
dbErrorHandling "Closing cursor" && e
return e
end try
return TRUE
end closeDBCursor
/*----------------------------------------------------------------------
--| COMMAND dbErrorHandling
--|
--| Author: rabit
--| Version: 1.0
--| Created: 09-04-14
--| Last Mod: --
--| Requires: --
--|
--| Summary: Save or display errors. Calls an optional error handling handler (log)
--| which is not included!
--|
--| Format: dbErrorHandling param1
--|
--| Parameters: string <pError>
--|
--| Return: empty
----------------------------------------------------------------------*/
command dbErrorHandling pError
try
log pError
catch e
answer error pError
end try
end dbErrorHandling
/*----------------------------------------------------------------------
--| COMMAND queryResultArrayToDgArray
--|
--| Author: rabit
--| Version: 1.0
--| Created: 23-04-14
--| Last Mod: --
--| Requires: --
--|
--| Summary: Convert query result array to datagrid array.
--|
--| Format: queryResultArrayToDgArray param1, param2
--|
--| Parameters: array <pArray>, array <pFields>
--|
--| Return: empty
----------------------------------------------------------------------*/
command queryResultArrayToDgArray @pArray pFields
local tDataGridArray, tKey, tTempArray, tFieldName
put empty into tDataGridArray
# REPLACE KEY FIELD NUMBER WITH FIELD NAME IN pArray
#
repeat for each key tKey in pArray
repeat for each key tFieldKey in pArray[tKey]
# GET DATA OF CURRENT FIELD
put pArray[tKey][tFieldKey] into tTempArray
put pFields[tFieldKey] into tFieldName
# BUILD DATAGRID ARRAY
put tTempArray into tDataGridArray[tKey][tFieldName]
end repeat
end repeat
put tDataGridArray into pArray
end queryResultArrayToDgArray
/*----------------------------------------------------------------------
--| COMMAND setupDatabase
--|
--| Author: rabit
--| Version: 1.1
--| Created: 2014-04-09
--| Last Mod: 2019-05-13
--| Requires: --
--|
--| Summary: Store database configuration values in variable.
--|
--| Format: setupDatabase param1, param2[, param3]
--|
--| Parameters: string <pDbPath> the path to the SQLite file, string <pActiveGroup> the config group,
--| <pOptions> comma delimited list of SQLite options (optional)
--|
--| Return: empty
----------------------------------------------------------------------*/
private command setupDatabase pDbPath pActiveGroup pOptions
if pActiveGroup is empty then
put "default" into sActiveGroup
else
put pActiveGroup into sActiveGroup
end if
if sActiveGroup is "default" then
put pDbPath into sDB[sActiveGroup]["hostname"]
end if
set the itemDel to "/"
put item -1 of pDbPath into sDB[sActiveGroup]["database"]
put "" into sDB[sActiveGroup]["dbprefix"]
put TRUE into sDB[sActiveGroup]["dbdebug"]
put pOptions into sDB[sActiveGroup]["options"]
end setupDatabase
--====================================================================--
--
-- revIgniter Stuff
--
/*----------------------------------------------------------------------
--| FUNCTION rigLoadDatabase
--|
--| Author: rabit
--| Version: 1.1
--| Created: 2014-04-18
--| Last Mod: 2019-05-13
--| Requires: dbErrorHandling, setupDatabase, rigDB()
--|
--| Summary: Database Loader
--|
--| Format: rigLoadDatabase(param1[, param2][, param3][, param4])
--|
--| Parameters: string <pParams> the DB credentials, bool <pReturn> (optional), string <pActiveGroup> (optional),
--| <pOptions> comma delimited list of SQLite options (optional)
--|
--| Return: mixed
----------------------------------------------------------------------*/
function rigLoadDatabase pParams pReturn pActiveGroup pOptions
local tDBresult
if pParams is empty then
dbErrorHandling "Error: Please specify a path to a database or an array of database settings!"
exit to top
end if
if pParams is not an array then
setupDatabase pParams, pActiveGroup, pOptions
end if
put rigDB(pParams) into tDBresult
if pReturn is TRUE then
return tDBresult
end if
end rigLoadDatabase
/*----------------------------------------------------------------------
--| FUNCTION rigDB
--|
--| Author: rabit
--| Version: 1.0
--| Created: 18-04-14
--| Last Mod: --
--| Requires: dbErrorHandling, rigDbDriver, rigDbInitialize()
--|
--| Summary: Initialize the database.
--|
--| Format: rigDB(param1, param2)
--|
--| Parameters: string <pParams> the DB credentials
--|
--| Return: mixed
----------------------------------------------------------------------*/
private function rigDB pParams
if pParams is not an array then
if sDB is not an array then
dbErrorHandling "No database connection settings were found."
exit to top
end if
if sDB[sActiveGroup] is not an array then
dbErrorHandling "You have specified an invalid database connection group."
exit to top
end if
put sDB[sActiveGroup] into pParams
end if
# SET THE DATABASE DRIVER LIBRARY VARIABLES VALUES
rigDbDriver pParams
if sDBdriverSettings["autoinit"] is TRUE then
get rigDbInitialize()
end if
return sDatabaseID
end rigDB
## -------------------------------------------------------------------
# Database Driver Library
#
# This is the platform-independent base DB implementation library.
# This library will not be called directly. Rather, the adapter
# library for the specific database (in this case the SQLite driver) will extend it.
#
# @package revIgniter
# @subpackage Drivers
# @category Database
# @author rabit@revigniter.com
# @link http://revigniter.com/userGuide/database/
##
# DECLARE VARIABLES
local _sEscapeChar, _sCountString, _sRandomKeyword, _sLikeEscapeStr, _sLikeEscapeChr
local sDBresult
local sTableInfo
local sActiveRecord
# SET INITIAL VALUES
private command dbSetInitialValues
put "" into sDBdriverSettings["hostname"]
put "" into sDBdriverSettings["database"]
put "sqlite" into sDBdriverSettings["dbdriver"]
put "" into sDBdriverSettings["dbprefix"]
put TRUE into sDBdriverSettings["autoinit"] -- Whether to automatically initialize the DB
put "" into sDBdriverSettings["swappre"]
put FALSE into sDBdriverSettings["connid"]
put FALSE into sDBdriverSettings["resultid"]
put FALSE into sDBdriverSettings["dbdebug"]
put 0 into sDBdriverSettings["benchmark"]
put 0 into sDBdriverSettings["querycount"]
put TRUE into sDBdriverSettings["savequeries"]
put "" into sDBdriverSettings["queries"]
put "" into sDBdriverSettings["querytimes"]
put "" into sDBdriverSettings["datacache"]
put TRUE into sDBdriverSettings["transenabled"]
put TRUE into sDBdriverSettings["transstrict"]
put 0 into sDBdriverSettings["transdepth"]
put TRUE into sDBdriverSettings["transstatus"]
put FALSE into sDBdriverSettings["transfailure"]
put TRUE into sDBdriverSettings["protectIdentifiers"]
put "*" into sDBdriverSettings["reservedIdentifiers"][1]
put FALSE into sDBdriverSettings["iswritetype"]
put FALSE into sDBdriverSettings["affectedrows"]
-----------------------------------------
# THE CHARACTER USED FOR ESCAPING
put quote into _sEscapeChar
# CLAUSE AND CHARACTER USED FOR LIKE ESCAPE SEQUENCES
put " ESCAPE '%s' " into _sLikeEscapeStr
put "!" into _sLikeEscapeChr
##
# THE SYNTAX TO COUNT ROWS IS SLIGHTLY DIFFERENT ACROSS DIFFERENT
# DATABASE ENGINES, SO THIS STRING APPEARS IN EACH DRIVER AND IS
# USED FOR THE COUNT_ALL() AND COUNT_ALL_RESULTS() FUNCTIONS.
##
put "SELECT COUNT(*) AS " into _sCountString
put " Random()" into _sRandomKeyword -- database specific random keyword
------------------------------------------
put empty into sDBresult["resultarray"]
put 0 into sDBresult["currentrow"]
put 0 into sDBresult["numrows"]
put empty into sDBresult["rowdata"]
put empty into sDBresult["fieldNames"]
put empty into sDBresult["fieldNumbers"]
--------------------------------------------
put "" into sActiveRecord["select"]
put FALSE into sActiveRecord["distinct"]
put "" into sActiveRecord["from"]
put "" into sActiveRecord["join"]
put "" into sActiveRecord["where"]
put "" into sActiveRecord["like"]
put "" into sActiveRecord["groupby"]
put "" into sActiveRecord["having"]
put FALSE into sActiveRecord["limit"]
put FALSE into sActiveRecord["offset"]
put FALSE into sActiveRecord["order"]
put "" into sActiveRecord["orderby"]
put "" into sActiveRecord["set"]
put "" into sActiveRecord["wherein"]
put "" into sActiveRecord["aliasedtables"]
put "" into sActiveRecord["storearray"]
# ACTIVE RECORD CACHING VARIABLES
put FALSE into sActiveRecord["caching"]
put "" into sActiveRecord["cacheexists"]
put "" into sActiveRecord["cacheselect"]
put "" into sActiveRecord["cachefrom"]
put "" into sActiveRecord["cachejoin"]
put "" into sActiveRecord["cachewhere"]
put "" into sActiveRecord["cachelike"]
put "" into sActiveRecord["cachegroupby"]
put "" into sActiveRecord["cachehaving"]
put "" into sActiveRecord["cacheorderby"]
put "" into sActiveRecord["cacheset"]
end dbSetInitialValues
/*----------------------------------------------------------------------
--| COMMAND rigDbDriver
--|
--| Author: rabit
--| Version: 1.0
--| Created: 18-04-14
--| Last Mod: --
--| Requires: --
--|
--| Summary: Sets the library variables values. Accepts one parameter
--| containing the database connection settings.
--|
--| Format: rigDbDriver param1
--|
--| Parameters: array <pParams>
--|
--| Return: empty
----------------------------------------------------------------------*/
command rigDbDriver pParams
if pParams is an array then
repeat for each key tKey in pParams
put pParams[tKey] into sDBdriverSettings[tKey]
if tKey is "connid" then
put pParams[tKey] into sDatabaseID
end if
end repeat
end if
end rigDbDriver
/*----------------------------------------------------------------------
--| FUNCTION rigFetchDBdriverSetting
--|
--| Author: rabit
--| Version: 1.1
--| Created: 2009-07-06
--| Last Mod: 2020-03-14
--| Requires: --
--|
--| Summary: Fetch setting from DB driver settings.
--|
--| Format: rigFetchDBdriverSetting(param1)
--|
--| Parameters: string<pSettingsKey>
--|
--| Return: mixed
----------------------------------------------------------------------*/
function rigFetchDBdriverSetting pSettingsKey
local tKey, tSettingsA
if comma is in pSettingsKey then
repeat for each item tKey in pSettingsKey
put sDBdriverSettings[tKey] into tSettingsA[tKey]
end repeat
return tSettingsA
end if
return sDBdriverSettings[pSettingsKey]
end rigFetchDBdriverSetting
/*----------------------------------------------------------------------
--| COMMAND rigSetDBdriverSetting
--|
--| Author: rabit
--| Version: 1.0
--| Created: 06-07-09
--| Last Mod: 06-07-09
--| Requires: --
--|
--| Summary: Set driver values.
--|
--| Format: rigSetDBdriverSetting param1, param2
--|
--| Parameters: string <pKey>, mixed <pValue>
--|
--| Return: empty
----------------------------------------------------------------------*/
command rigSetDBdriverSetting pKey pValue
put pValue into sDBdriverSettings[pKey]
end rigSetDBdriverSetting
/*----------------------------------------------------------------------
--| FUNCTION rigDbInitialize
--|
--| Author: rabit
--| Version: 1.0
--| Created: 18-04-14
--| Last Mod: --
--| Requires: rigDbConnect(), dbErrorHandling
--|
--| Summary: Initialize Database Settings.
--|
--| Format: rigDbInitialize()
--|
--| Parameters: --
--|
--| Return: bool
----------------------------------------------------------------------*/
function rigDbInitialize
# IF AN EXISTING CONNECTION RESOURCE IS AVAILABLE
# THERE IS NO NEED TO CONNECT AND SELECT THE DATABASE
if sDBdriverSettings["connid"] is an integer then
return TRUE
end if
# ----------------------------------------------------------------
# CONNECT TO THE DATABASE AND SET THE CONNECTION ID
put rigDbConnect() into sDBdriverSettings["connid"]
put sDBdriverSettings["connid"] into sDatabaseID
# NO CONNECTION RESOURCE? THROW AN ERROR
if sDBdriverSettings["connid"] is not an integer then
dbErrorHandling "Unable to connect to the database:" && sDBdriverSettings["database"]
return FALSE
end if
return TRUE
end rigDbInitialize
/*----------------------------------------------------------------------
--| FUNCTION rigDbVersion
--|
--| Author: rabit
--| Version: 1.1
--| Created: 2014-04-18
--| Last Mod: 2019-04-27
--| Requires: _rigDbVersion(), dbErrorHandling, rigDbQuery()
--|
--| Summary: Database Version Number. Returns a string containing the
--| version of the database being used.
--|
--| Format: rigDbVersion()
--|
--| Parameters: --
--|
--| Return: string
----------------------------------------------------------------------*/
function rigDbVersion
local tSql, tQuery
put _rigDbVersion() into tSql
if tSql is FALSE then
# ERROR HANDLING
if sDBdriverSettings["dbdebug"] is TRUE then
dbErrorHandling "Unsupported database function"
end if
return FALSE
end if
put rigDbQuery(tSql) into tQuery
return rigDbRow("ver")
end rigDbVersion
/*----------------------------------------------------------------------
--| FUNCTION rigDbQuery
--|
--| Author: rabit
--| Version: 1.1
--| Created: 2014-04-18
--| Last Mod: 2019-04-27
--| Requires: rigLoadRdriver, rigCompileBinds, rigSimpleQuery(), rigListFields(), dbErrorHandling
--| rigTransComplete(), rigIsWriteType(), rigNumRows()
--|
--| Summary: Accepts an SQL string as input and returns the result upon
--| successful execution of a "read" type query. Returns boolean TRUE
--| upon successful execution of a "write" type query. Returns boolean
--| FALSE upon failure, and if the dbdebug variable in the database config file is set to TRUE
--| will raise an error.
--|
--| Format: rigDbQuery(param1, param2)
--|
--| Parameters: string <pSQL> the sql query string, array <pBinds> array of binding data
--|
--| Return: mixed
----------------------------------------------------------------------*/
function rigDbQuery pSQL pBinds
local tBinds, tSQL, tStrFound, tRegEx, tFoundVar1, tFoundVar2, tStrToSwap, tReplacementStr
local tQueriesNum, tNewQueriesNum, tTimeStart, tQueryResult, tQueryTimesNum, tNewQueryTimesNum
local tErrorMessage, tTimeEnd, tRES, tChangeFieldNamesListDelimiter
if pBinds is not an array and pBinds is "" then
put FALSE into tBinds
else
put pBinds into tBinds
end if
put pSQL into tSQL
if tSQL is "" then
# ERROR HANDLING
if sDBdriverSettings["dbdebug"] is TRUE then
dbErrorHandling "Invalid query:" && tSQL
end if
return FALSE
end if
# VERIFY TABLE PREFIX AND REPLACE IF NECESSARY
if ((sDBdriverSettings["dbprefix"]) <> "" and (sDBdriverSettings["swappre"] <> "")) and (sDBdriverSettings["dbprefix"] <> sDBdriverSettings["swappre"]) then
put TRUE into tStrFound
put empty into tFoundVar1 -- needs to be declared before calling matchText since LC version 8.1.5 and 9.0.0 dp 7
put empty into tFoundVar2
repeat until tStrFound is not TRUE
put "(\W)" & sDBdriverSettings["swappre"] & "(\S+?)" into tRegEx
get matchText(tSQL,tRegEx, tFoundVar1, tFoundVar2)
if it is TRUE then
put tFoundVar1 & sDBdriverSettings["swappre"] & tFoundVar2 into tStrToSwap
put tFoundVar1 & sDBdriverSettings["dbprefix"] & tFoundVar2 into tReplacementStr
replace tStrToSwap with tReplacementStr in tSQL
else
put FALSE into tStrFound
end if
end repeat
end if
# COMPILE BINDS IF NEEDED
if tBinds is not FALSE then
put rigCompileBinds(tSQL, tBinds) into tSQL
end if
# SAVE THE QUERY FOR DEBUGGING
if sDBdriverSettings["savequeries"] is TRUE then
if sDBdriverSettings["queries"] is an array then
put the number of lines in the keys of sDBdriverSettings["queries"] into tQueriesNum
put tQueriesNum + 1 into tNewQueriesNum
put tSQL into sDBdriverSettings["queries"][tNewQueriesNum]
else
put tSQL into sDBdriverSettings["queries"][1]
end if
end if
# CHECK IF QUERY IS OF TYPE WRITE TO CHOOSE THE APPROPRIATE QUERY COMMAND
# IN THE _rigDbExecute COMMAND
put rigIsWriteType(tSQL) into sDBdriverSettings["iswritetype"]
# START THE QUERY TIMER
put the long seconds into tTimeStart
# RUN THE QUERY
put rigDbSimpleQuery(tSQL) into tQueryResult
# STORE RECORD SET ID OR AFFECTED ROWS
if sDBdriverSettings["iswritetype"] is TRUE then
put tQueryResult into sDBdriverSettings["affectedrows"]
put FALSE into sDBdriverSettings["resultid"]
else
put tQueryResult into sDBdriverSettings["resultid"]
put FALSE into sDBdriverSettings["affectedrows"]
end if
# CHECK FOR ERROR IN RESULT
if tQueryResult is FALSE then
if sDBdriverSettings["savequeries"] is TRUE then
put the number of lines in the keys of sDBdriverSettings["querytimes"] into tQueryTimesNum
put tQueryTimesNum + 1 into tNewQueryTimesNum
put 0 into sDBdriverSettings["querytimes"][tNewQueryTimesNum]
end if
# THIS WILL TRIGGER A ROLLBACK IF TRANSACTIONS ARE BEING USED
put FALSE into sDBdriverSettings["transstatus"]
# ERROR HANDLING
if sDBdriverSettings["dbdebug"] is TRUE then
# GRAB THE ERROR MESSAGE NOW, AS WE MIGHT RUN SOME
# ADDITIONAL QUERIES BEFORE DISPLAYING THE ERROR
put rigDbErrorMessage() into tErrorMessage
# WE CALL THIS FUNCTION IN ORDER TO ROLL-BACK QUERIES
# IF TRANSACTIONS ARE ENABLED. IF WE DON'T CALL THIS HERE
# THE ERROR MESSAGE WILL TRIGGER AN EXIT, CAUSING THE
# TRANSACTIONS TO REMAIN IN LIMBO.
get rigTransComplete()
# LOG AND DISPLAY ERRORS
dbErrorHandling "Query error:" & tErrorMessage && tSQL
end if
return FALSE
end if
# STOP AND AGGREGATE THE QUERY TIME RESULTS
put the long seconds into tTimeEnd
add (tTimeEnd - tTimeStart) to sDBdriverSettings["benchmark"]
if sDBdriverSettings["savequeries"] is TRUE then
put the number of lines in the keys of sDBdriverSettings["querytimes"] into tQueryTimesNum
put tQueryTimesNum + 1 into tNewQueryTimesNum
put (tTimeEnd - tTimeStart) into sDBdriverSettings["querytimes"][tNewQueryTimesNum]
end if
# INCREMENT THE QUERY COUNTER
add 1 to sDBdriverSettings["querycount"]
# WAS THE QUERY A "WRITE" TYPE?
# IF SO WE'LL SIMPLY RETURN TRUE
if sDBdriverSettings["iswritetype"] is TRUE then
return TRUE
end if
# STORE THE RESULT DATA IN AN ARRAY
#
put sDBdriverSettings["connid"] into tRES["connid"]
put sDBdriverSettings["resultid"] into tRES["resultid"]
# CLEAR RESULT ARRAY
rigDbResetResultValue "resultarray"
# CHECK IF IT IS A "SELECT IF" OR A "SELECT GREATEST" QUERY
put FALSE into tChangeFieldNamesListDelimiter
if ("SELECT GREATEST" is in tSQL) or ("SELECT IF" is in tSQL) then
put TRUE into tChangeFieldNamesListDelimiter
end if
put rigDBresult(tChangeFieldNamesListDelimiter) into tRES["resultarray"]
# OCI8 VARS MUST BE SET BEFORE CALLING THIS
put rigNumRows(sDBdriverSettings["resultid"]) into tRES["numrows"]
# EXTRA BONUS
put rigListFields(sDBdriverSettings["resultid"]) into tRES["fieldnames"]
put rigFieldNumbers(tRES["fieldnames"]) into tRES["fieldnumbers"]
return tRES
end rigDbQuery
/*----------------------------------------------------------------------
--| FUNCTION rigDbSimpleQuery
--|
--| Author: rabit
--| Version: 1.0
--| Created: 19-07-09
--| Last Mod: 19-07-09
--| Requires: rigDbInitialize(), _rigDbExecute()
--|
--| Summary: This is a simplified version of the rigDbQuery() function. Internally
--| we only use it when running transaction commands since they do
--| not require all the features of the main query() function.
--|
--| Format: rigDbSimpleQuery(param1)
--|
--| Parameters: string <pSQL> the sql statement
--|
--| Return: mixed
----------------------------------------------------------------------*/
function rigDbSimpleQuery pSQL
if sDBdriverSettings["connid"] is FALSE then
get rigDbInitialize()
end if
return _rigDbExecute(pSQL, sDBdriverSettings["connid"])
end rigDbSimpleQuery
/*----------------------------------------------------------------------
--| COMMAND rigDbTransOff
--|
--| Author: rabit
--| Version: 1.0
--| Created: 30-09-09
--| Last Mod: 30-09-09
--| Requires: --
--|
--| Summary: This permits transactions to be disabled at run-time.
--|
--| Format: rigDbTransOff
--|
--| Parameters: --
--|
--| Return: empty
----------------------------------------------------------------------*/
command rigDbTransOff
put FALSE into sDBdriverSettings["transenabled"]
end rigDbTransOff
/*----------------------------------------------------------------------
--| COMMAND rigDbTransStrict
--|
--| Author: rabit
--| Version: 1.0
--| Created: 30-09-09
--| Last Mod: 30-09-09
--| Requires: --
--|
--| Summary: When strict mode is enabled, if you are running multiple groups of
--| transactions, if one group fails all groups will be rolled back.
--| If strict mode is disabled, each group is treated autonomously, meaning
--| a failure of one group will not affect any others.
--|
--| Format: rigDbTransStrict param1
--|
--| Parameters: bool <pMode>
--|
--| Return: empty
----------------------------------------------------------------------*/
command rigDbTransStrict pMode
local tMode
if pMode is empty then
put TRUE into tMode
else
put pMode into tMode
end if
if tMode is a boolean then
put tMode into sDBdriverSettings["transstrict"]
else
put TRUE into sDBdriverSettings["transstrict"]
end if
end rigDbTransStrict
/*----------------------------------------------------------------------
--| COMMAND rigDbTransStart
--|
--| Author: rabit
--| Version: 1.1