-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDB-Rider.r
More file actions
4123 lines (3997 loc) · 174 KB
/
DB-Rider.r
File metadata and controls
4123 lines (3997 loc) · 174 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
REBOL [
Title: "DB-Rider"
Filename: %DB-Rider.r
Author: "Mike Yaunish"
Copyright: "2017 - Mike Yaunish"
Version: 0.7.1.1
Version-Notes: "See github for version details"
Maturity: 'alpha-release
Home: https://github.com/mikeyaunish/DB-Rider.git
License: {
BSD 3-Clause License
Copyright (c) 2017, Mike Yaunish
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}
History: [
See github for all of the Versions of DB-Rider
03-Nov-2017 0.7.0 "First public Alpha release"
]
]
capture-errors: true ; Global variable
root-path: what-dir
do join root-path %program-scripts/do-safe.r
do join root-path %community-scripts/foreach-file.r
do-all-scripts-in-folder: func [ the-path [ file! ] recovery-path [ file! ] /local filelist f file-op ] [
file-op: func [ file ] [
if not ( do-safe file "doing all scripts in a given folder" )[
change-dir recovery-path ; switch back directory because errors leave you hanging.
]
]
foreach-file/file-extension/only the-path :file-op ".r"
]
do-all-scripts-in-folder join root-path %community-scripts/ root-path
do-all-scripts-in-folder join root-path %program-scripts/ root-path
do-safe join root-path %menu/menu-config.r reduce [ "menu-config.r script" join root-path %menu/menu-config.r ]
drop-down-img: load %images/drop-down6.gif
db-rider-context: context [
redrawing-virtual: false
F5-field-actions-block: [] ; global in this context to be able to handle request-db-list and F5 calling of F5-field-actions-func
db-visit-history: copy []
set-field-history: copy []
virtual-box-size: 630x380
results: copy []
db-obj: make object! [
schema-file: copy ""
connection-name: copy ""
user: copy ""
host: copy ""
database: copy ""
table: copy ""
pass: copy ""
related-field-list: copy []
listing-layout: copy []
root-path: clean-path %./
overlay-path: clean-path %db-overlays/
last-date: now/date
text-editor: copy ""
text-editor-name: copy ""
text-editor-command-line: copy []
last-table: copy ""
last-database: copy ""
last-query-string: copy ""
current-report-name: copy ""
_field-details_: copy []
last-fd-table: copy ""
primary-index-name: ""
primary-index-offset: 0
current-record-number: 1
previous-record-number: 0
current-edit-field-name: ""
current-edit-row: 0
current-edit-field-data: ""
current-mouse-position: 0x0
displayed-db-table: ""
capture-errors: true
connection-changed?: false
connected?: false
report-layout-status: 0
report-layout-error: false
extended-totals-error: false
records-to-display: "15"
on-new-record-code: []
do-on-display-record-code-status: 0
internal-folders: [
"report" "print" "select" "go" "export" "import" "test"
"function-key" "common-import" "common-export"
"global-select" "global-function-key" "global-import" "global-export"
"user-scripts"
]
mysql-port: make object! [] ; port placeholder - assigned correctly after port is opened.
create-folder-structure: func [
db-name [ string! ]
table-name [ string! ]
/local folder-list folde full-path
]
[
folder-list: [
"export" "function-key" "import" "print" "report" "select" "user-scripts"
]
foreach folder folder-list [
full-path: join query-db/overlay-path rejoin [ db-name "/" table-name "/" folder "/" ]
if (not exists? full-path) [
make-dir/deep full-path
]
]
if (not exists? db-export-path: join query-db/get-database-path "export/" )[ ; " UltraEdit match quote problem = UMQP
make-dir/deep db-export-path
]
]
create-full-path-code-string: func [
full-path [ block! ]
the-code [ string!]
/local res-code i tab-depth
]
[
res-code: copy ""
tab-depth: ((length? full-path) - 1 )
insert res-code rejoin [
newline
( pad/with "" ((tab-depth + 1 )* 4 ) #" " ) trim the-code
]
foreach i reverse full-path [
res-code: rejoin [
newline
( pad/with "" (tab-depth * 4 ) #" " ) mold i { [ }
reduce res-code newline
( pad/with "" ((tab-depth )* 4 ) #" " )
{]}
]
tab-depth: tab-depth - 1
]
return res-code
]
insert-code-into-field-actions: func [
source-code [ string!]
location-block [block!]
the-code [ string!]
id-string [ string! ]
/local i entry-exists? search-at common-actions linked-actions non-linked-actions last-actions
]
[
entry-exists?: true
any-change?: true
do-safe [ source-code: load source-code ] "Function: insert-code-into-field-actions^/Error has blocked field actions from load"
search-at: source-code
foreach i location-block [
either (found? search-at: select search-at i ) [
][
entry-exists?: false
]
if (not entry-exists? ) [ break ]
]
either entry-exists? [
either (not found? find (mold search-at) id-string ) [
insert search-at load the-code
][
any-change?: false
]
][
; Insert a new entry right after the comment line at the top of the script
insert (skip source-code 2) load create-full-path-code-string location-block the-code
]
return reduce [ any-change? source-code ]
]
create-field-actions-initial-code: func [
db-obj [ object! ]
the-table-name [ string! ]
/local ret-code i field-details first-actions last-actions
]
[
;************************************
;***** START static string formatting
common-actions: {on-display-record [
]
on-new-record [
]
}
linked-actions: { [
assist-button [
]
on-return [
]
on-new-target-record [
source-record-actions [
]
target-record-actions [
]
]
]
}
non-linked-actions: { [
assist-button [
]
on-return [
]
]
}
last-actions: {on-duplicate-record [
source-record-actions [
]
target-record-actions [
]
]
}
;***** END static string formatting
;**********************************
rel-field-list: db-obj/get-related-field-list
ret-code: rejoin [ "comment {DB-Rider field-actions for DATABASE:'" db-obj/database {' TABLE:'} the-table-name "'" "}" newline]
append ret-code common-actions
field-details: db-obj/get-field-details/for-table the-table-name
foreach i skip field-details 1 [
either (find-in-array-at rel-field-list 1 i/1) [
append ret-code rejoin [ {"} i/1 {"} linked-actions ]
][
append ret-code rejoin [ {"} i/1 {"} non-linked-actions ]
]
]
append ret-code last-actions
return ret-code
]
update-datatype-assist-actions: func [
/reload /local fd the-field-name the-dataype
][
create-datatype-code: func [
data-type table-name field-name
/local semicolon identifier-message code-body identifier-string header-string code
]
[
semicolon: {;}
identifier-message: rejoin [ " comment {***** NOTE: The comment above is a code marker flag. If it is modified or deleted at all then the this portion of code will be recreated and inserted into this script again.}" ]
case [
data-type = "date" [
; ************ (date) START GENERATED CODE ******************************************************************
code-body: rejoin [ {
rd: request-date-for-field edit-db/table this-field
if rd [
set-field this-field rd
set-focus next-field
]
} ]
identifier-string: rejoin [ {***** ACTION:assist-button TABLE:} table-name { FIELD:} the-field-name { AUTO-GENERATED-CODE for field datatype:} data-type {. } ]
header-string: rejoin [ "comment {" identifier-string "}" newline identifier-message ]
code: rejoin [{
} header-string code-body " comment {***** END_OF_GENERATED_CODE ACTION:assist-button TABLE:" table-name { FIELD:} field-name "}" {
}]
; *********** (date) END GENERATED CODE ******************************************************************
]
data-type = "enum" [
; ************ (enum) START GENERATED CODE ******************************************************************
code-body: rejoin [ {
rd: request-enum-for-field edit-db/table this-field
if rd [
set-field this-field rd
set-focus next-field
]
} ]
identifier-string: rejoin [ {***** ACTION:assist-button TABLE:} table-name { FIELD:} the-field-name { AUTO-GENERATED-CODE for field datatype:} data-type {. } ]
header-string: rejoin [ "comment {" identifier-string "}" newline identifier-message ]
code: rejoin [{
} header-string code-body " comment {***** END_OF_GENERATED_CODE ACTION:assist-button TABLE:" table-name { FIELD:} field-name "}" {
}]
; *********** (enum) END GENERATED CODE ******************************************************************
]
data-type = "set" [
; ************(set) START GENERATED CODE ******************************************************************
code-body: rejoin [ {
rd: request-set-for-field edit-db/table this-field
if rd [
set-field this-field rd
set-focus next-field
]
} ]
identifier-string: rejoin [ {***** ACTION:assist-button TABLE:} table-name { FIELD:} the-field-name { AUTO-GENERATED-CODE for field datatype:} data-type {. } ]
header-string: rejoin [ "comment {" identifier-string "}" newline identifier-message ]
code: rejoin [{
} header-string code-body " comment {***** END_OF_GENERATED_CODE ACTION:assist-button TABLE:" table-name { FIELD:} field-name "}" {
}]
; *********** (set) END GENERATED CODE ******************************************************************
]
] ; END case
return reduce [ code identifier-string ]
] ; END create-datatype-code
table-list: query-db/run-sql rejoin [ "show tables from " query-db/database ]
foreach table-name table-list [
table-name: first table-name
fd: query-db/get-field-details/for-table table-name
foreach i fd [
the-field-name: copy ""
case [
(i/2 = "date") [
the-datatype: "date"
the-field-name: i/1
]
((copy/part i/2 4)= "enum") [
the-datatype: "enum"
the-field-name: i/1
]
((copy/part i/2 3)= "set") [
the-datatype: "set"
the-field-name: i/1
]
]
if (the-field-name <> "") [ ; using the-field-name as a flag to activate creation of code
code-vals: create-datatype-code the-datatype table-name the-field-name
the-code: first code-vals
id-string: second code-vals
field-actions-file: query-db/get-field-actions-filename/for-table table-name
either (exists? field-actions-file) [
field-actions: read field-actions-file
last-insert: copy ""
][
make-dir/deep mdd: first split-path field-actions-file
field-actions: create-field-actions-initial-code edit-db table-name
]
field-actions: insert-code-into-field-actions field-actions reduce [ the-field-name 'assist-button ] the-code id-string
either (field-actions/1) [
save field-actions-file field-actions/2
][
]
]
]
]
] ; END update-datatype-assist actions
get-field-actions-filename: func [ /for-table table-name /local the-table-name ] [
either for-table [
the-table-name: table-name
][
the-table-name: table
]
join (clean-path overlay-path) rejoin [ database "/" the-table-name "/field-actions.r" ]
]
flash-field: func [ fld ] [
fld/colors/1: 200.10.10
show fld
wait 0.05
fld/colors/1: 255.255.255
show fld
]
get-global-path: does [
join root-path "global-scripts/"
]
get-global-select-path: does [
join get-global-path "select/"
]
get-global-report-path: does [
join get-global-path "report/"
]
get-table-path: does [
join (clean-path overlay-path) rejoin [ database "/" table "/" ]
]
get-database-path: does [
join (clean-path overlay-path) rejoin [ database "/" ]
]
get-listing-layout-filename: does [
join (clean-path get-table-path) "listing-layout.r"
]
get-select-path: does [
join (clean-path get-table-path) "select/"
]
get-last-select-path: does [
clean-path get-table-path
]
get-import-path: does [
join (clean-path get-table-path) "import/"
]
get-common-import-path: does [
join (clean-path overlay-path) rejoin [ database "/import/" ]
]
get-global-import-path: does [
join get-global-path "import/"
]
get-export-path: does [
join (clean-path get-table-path) "export/"
]
get-common-export-path: does [
join (clean-path overlay-path) rejoin [ database "/export/" ]
]
get-global-export-path: does [
join get-global-path "export/"
]
get-go-path: does [
join (clean-path overlay-path) rejoin [ database "/GO/" ]
]
get-last-go-path: does [
join (clean-path overlay-path) rejoin [ database "/" ]
]
get-settings-path: does [
join root-path %settings/
]
get-query-path: does [
join (clean-path overlay-path) rejoin [ database "/query/" ]
]
get-function-key-path: does [
join (clean-path get-table-path) "function-key/"
]
get-global-function-key-path: does [
join get-global-path %function-key/
]
get-database-connections-path: does [
join root-path %settings/database-connections/
]
get-print-path: does [
join (clean-path get-table-path) "print/"
]
get-last-print-path: does [
get-table-path
]
get-user-scripts-path: does [
join (clean-path get-table-path) "user-scripts/"
]
get-test-path: does [
clean-path rejoin [ root-path "test/" ]
]
get-report-path: does [
join (clean-path get-table-path) "report/"
]
get-last-report-path: does [
get-table-path
]
init: func [
/local t fallback-database fallback-table table-list a-listing vl
][
fallback-database: copy last-database
fallback-table: copy last-table
table-list: run-sql "show tables"
if (table-list = false) [
return false
]
if table-list = [] [
my-request rejoin [ "The database " database " does NOT contain" newline "any tables. The last valid query will be re-loaded." ]
database: copy fallback-database
table: copy fallback-table
return false
]
_table-list_: copy table-list
do-all-global-scripts
if ( last-database <> database ) [
listing-layout: copy []
]
_field-details_: copy []
foreach t table-list [
table-name: first t
fd: run-sql rejoin ["show columns from " table-name ]
append _field-details_ reduce [ to-word table-name fd ]
listing-layout-filename: join (clean-path overlay-path) rejoin [ database "/" t "/listing-layout.r" ]
either exists? listing-layout-filename [
a-table-word: to-word first t
if ( not valid-listing-layout? listing-layout-filename ) [ return false ]
a-listing: load listing-layout-filename
append query-db/listing-layout reduce [ a-table-word a-listing ]
][
create-folder-structure database table-name
]
]
related-field-list: copy []
return true
]
valid-listing-layout?: func [
the-listing-layout [ file! block! ]
/report
/local vl err-msg user-err-msg the-listing
]
[
either((type? the-listing-layout) = block!) [
the-listing: the-listing-layout
][
the-listing: load the-listing-layout
]
err-filename: either(report) [
replace copy query-db/current-report-name ".r" ".datr"
][
the-listing-layout
]
either ((vl: validate-listing-layout the-listing) = true )[
return true
][
user-err-msg: copy "ERRORS: "
llfn: split-path get-listing-layout-filename
err-msg: copy rejoin [ "ERROR WITH: listing layout^/ FILE: '" llfn/2 "'^/ FOLDER: '" llfn/1 "'^/-----------------------------------------------------------------------------^/" ]
first-line-with-error: vl/1/1
foreach i vl [
append err-msg rejoin [ "Layout line #" i/1 " problem with '" i/2 "'^/" ]
append user-err-msg rejoin [ "Layout line #" i/1 " problem with '" i/2 "'. " ]
]
res: my-request/buttons err-msg [ " Edit File " ]
user-msg/query user-err-msg
if (res = " Edit File ") [
pos-in-file: find-position-in-file/rebol-script err-filename "" "layout"
line-num: ((first pos-in-file) + first-line-with-error )
edit-text-file/position err-filename reduce [ line-num 1 ]
]
return false
]
]
validate-listing-layout: func [
a-listing-layout
/local rules line-num error-report v h d f ii e s p a valid-list-template parse-result the-value
]
[
rules: [
set h 'heading string!
set d 'data block!
set f 'format into [
set ii [ 'info | 'field ] integer!
set e 'edge into [
set s set-word!
set p pair!
]
set a some [ word! ]
]
]
line-num: 0
error-report: copy []
foreach i a-listing-layout [
++ line-num
h: d: f: ii: e: s: p: a: none
valid-list-template: [ "heading" "data" "format" "info" "edge" "size" "size value" "left/right/middle alignment" ]
parse-result: parse i rules
valid-list: copy []
foreach v [ h d f ii e s p a ] [
the-value: get :v
if the-value <> none [
append valid-list the-value
]
]
if (not parse-result) [
append/only error-report reduce [ line-num (pick valid-list-template ((length? valid-list) + 1 )) ]
]
]
either error-report = [] [
return true
][
return error-report
]
]
edit-text-file: func [
filename [ file! ]
/position line-col-pos [ block! ]
/find-this find-string [ string! ]
/local quote-char cmd line-number column-number editor-program
]
[
if (value? 'query-db ) [
editor-program: query-db/text-editor
file-to-edit: to-local-file clean-path filename
quote-char: {"} ;"
either query-db/text-editor-command-line = [] [
cmd: rejoin [ editor-program " " quote-char file-to-edit quote-char ]
][
bind query-db/text-editor-command-line/plain-edit 'filename
bind query-db/text-editor-command-line/position-edit 'filename
either position [
line-number: line-col-pos/1 ; line-number variable local to editor config file
column-number: line-col-pos/2 ; column-number variable local to editor config file
cmd: rejoin reduce query-db/text-editor-command-line/position-edit
][
either(find-this) [
found-at: find infile: read/binary filename find-string
either found-at [
search-in: (copy/part infile (index? found-at ))
num-lines: num-occurrences search-in "^/"
last-line-break: find/reverse tail search-in "^/"
num-lines: num-lines + 1
line-number: num-lines
column-number: (index? found-at) - (index? last-line-break)
cmd: rejoin reduce query-db/text-editor-command-line/position-edit
][
cmd: rejoin reduce query-db/text-editor-command-line/plain-edit
]
][
cmd: rejoin reduce query-db/text-editor-command-line/plain-edit
]
]
]
call cmd
]
]
get-related-field-list: does [
if any [ (last-table <> table) (last-database <> database ) ( related-field-list = []) ] [ ; Then we've had a major switch of table or database
relationships-file: join (clean-path overlay-path) rejoin [ database "/relationships.r" ]
if exists? relationships-file [
related-field-list: parse-related-field-list relationships-file
if ((modified? relationships-file) > last-checked-relationships/get )[
update-relationship-assist-actions/reload
]
]
load-new-environment
]
last-table: copy table
return related-field-list
]
update-relationship-assist-actions: func [
/reload
/local semicolon quote open-curly close-curly tta code-body initial-table
]
[
create-code: func [
e
/local semicolon quote open-curly close-curly tta code-body header-string code
][
semicolon: ";"
quote: {"} ; " balancing quote here for syntax hilighting.
open-curly: "{"
close-curly: "}"
if ((type? e/human-readable-target-field) = block! ) [
e/human-readable-target-field: replace/all (to-csv e/human-readable-target-field) {"} {} ; "
]
;************ (request-db-list) START GENERATED CODE STATIC TEXT FORMATTING *******************************************
code-body: rejoin [
{
if ((a: request-db-list/one-click } open-curly { select } e/human-readable-target-field {,} e/target-field { from } e/target-table { ORDER by } e/human-readable-target-field { ASC } close-curly { } quote e/field-requester-prompt quote { ) <> none) [ set-field this-field last a ]
set-focus next-field
}
]
;*********** (request-db-list) END GENERATED CODE STATIC TEXT FORMATTING **********************************************
header-string: rejoin [ "comment {***** ACTION:assist-button TABLE:" e/source-table { FIELD:} e/source-field " GENERATED_CODE from relationship file. Do Not Modify this comment line. Modify the code below all you want.}" ]
code: rejoin [{
} header-string code-body "comment {***** END_OF_GENERATED_CODE ACTION:assist-button TABLE:" e/source-table { FIELD:} e/source-field "}" {
}]
return reduce [ code header-string ]
]
; END create-code ***********************************************
initial-table: copy table
relationships-file: join (clean-path overlay-path) rejoin [ database "/relationships.r" ]
if ( exists? relationships-file ) [
if any [ ( ( modified? relationships-file) > last-checked-relationships/get ) ( reload ) ] [
][
rf: load relationships-file
last-checked-relationships/set
foreach rfe rf [
table: rfe/source-table
if (rfe/source-table = table) [
code-vals: create-code rfe
the-code: first code-vals
id-string: second code-vals
field-actions-file: get-field-actions-filename
either (exists? field-actions-file) [
field-actions: read field-actions-file
last-insert: copy ""
][
make-dir/deep mdd: first split-path field-actions-file table
Field-Actions: Create-Field-Actions-Initial-Code edit-db table
]
field-actions: insert-code-into-field-actions field-actions reduce [ rfe/source-field 'assist-button ] the-code id-string
either (field-actions/1) [
save field-actions-file field-actions/2
][
]
]
] ; end foreach loop
table: copy initial-table
]
]
] ; END update-relationship-assist-actions
last-checked-relationships: func [ /get /set ] [
last-checked-file: join (clean-path overlay-path) rejoin [ database "/last-checked-relationships.datr" ]
if get [
either (exists? last-checked-file) [
return load last-checked-file
][
return to-date 1000-01-01
]
]
if set [
save last-checked-file now
]
]
set-table-name: func [ new-table-name ] [
if new-table-name <> table [
table: copy new-table-name
primary-index-name: copy ""
]
]
primary-index?: func [ /name /offset
/local results mysql-port item fd pi-name pi-oset
; Global to this context: primary-index-name primary-index-offset
]
[
if (primary-index-name = "") [ ; run sql query only if primary-index-name hasn't already been fetched.
results: run-sql rejoin [ { show index from } table ]
foreach item results [ ; Check for PRIMARY and UNIQUE values for proper primary key
if all [ (item/3 = "PRIMARY") (item/2 = "0" ) ] [
primary-index-name: item/5
break
]
]
fd: get-field-details
for i 1 (length? fd) 1 [
if fd/:i/1 = primary-index-name [
primary-index-offset: :i
break
]
]
]
if offset [
return primary-index-offset
]
return primary-index-name
]
get-field-details: func [
/for-table table-name [string!]
/for-field afield-name [string!]
/for-all
/return-field-num
][
if not for-all [
either for-table [
a-table: to-word table-name
][
a-table: to-word table
]
]
either any [ (for-table) ( not for-all) ] [
if ( not do-safe [ r: _field-details_/:a-table ] "get-field-details function^/Can't get valid information." ) [
return []
]
][
if ( not do-safe [ r: _field-details_ ] "get-field-details function. Can't get valid information." ) [
return []
]
]
if for-field [
either return-field-num [
r: second (find-in-array-at/with-index r 1 afield-name)
][
r: find-in-array-at r 1 afield-name
]
]
return r
]
get-table-list: func [
/local r fe
][
return _table-list_
]
get-field-datatype: func [ field-name /for-table table-name ] [
either for-table [
a-table: to-string table-name
][
a-table: to-string table
]
first parse ( second find-in-array-at ( get-field-details/for-table a-table ) 1 field-name) "("
]
get-human-readable-data: func [
src-fld-name src-fld-data
/local human-read f i hr-res
][
the-related-field-list: get-related-field-list
forskip the-related-field-list 2 [
if (table = first the-related-field-list ) [
if (found? f: find second the-related-field-list src-fld-name) [
i: f/2
if i/3 = none [ return "" ]
sel-str: trim i/3
replace/all sel-str " " ","
hr-sql-cmd: rejoin [ "select " sel-str " from " i/1 " WHERE " i/2 " = " "'" src-fld-data "'" ]
hr-res: run-sql hr-sql-cmd
either hr-res = [] [
return ""
][
return trim form first hr-res
]
]
]
]
return "" ; no human readable found for this table
]
run-sql: func [ sql-statement /limit hi-limit /return-block /debug /reopen reopen-database
/local r limit-count sql-cmd res-block rec-blk record ;field-details
; Global in context: last-table table last-database
][
if all [ (table = "") (( first parse sql-statement " " ) = "select" ) ] [
my-request rejoin [ "No table has been specified" newline "Please select a table." ]
return []
]tab
last-query-string: copy sql-statement
r: copy []
either reopen [
last-database: ""
open-database: copy reopen-database
][
open-database: copy database
]
either (last-database <> database ) [
if last-table <> "" [ ; Test if this is original open of table
close mysql-port ; Close and reopen port
]
sql-cmd: rejoin [ mysql:// user ":" pass "@" host "/" open-database ]
ds: do-safe [ mysql-port: open sql-cmd ] "Database Query"
either not ds [
return false
][
if (open-database <> "") [
database: copy open-database
]
]
][
]
if limit [
append sql-statement rejoin [ " LIMIT " hi-limit ]
]
if error? err: try [ ; TRY #1
r: send-sql mysql-port sql-statement
true ; make the try happy
][
the-error: disarm :err
either the-error/id = 'not-open [ trailer-msg: "^/WILL RETRY OPENING OF PORT" ] [ trailer-msg: "" ]
my-request rejoin [ {run-sql - PHASE 1 ERROR^/} dump-error-obj the-error {^/Original SQL Statement:} sql-statement trailer-msg ]
if the-error/id = 'not-open [ last-database: copy "" last-table: copy "" ] ; Force a re-open
return []
]
last-table: copy table
last-database: copy database
if return-block [ ; need to parse out 'select and 'from values.
sql-blk: parse sql-statement none
either ( found? fnd-pos: find sql-blk "from" )[
fd: get-field-details/for-table first next fnd-pos
selected: trim first delim-extract sql-statement "select" "from"
if selected <> "*" [
selected-items: parse selected ","
fd: copy []
foreach i selected-items [
append/only fd to-block i
]
]
][
my-request "run-sql/return-block ERROR: SQL statement doesn't contain a valid FROM table "
return
]
res-block: copy []
len-r: length? r
for r-index 1 len-r 1 [
fd-index: 0
rec-blk: copy []
foreach data-item r/:r-index [
++ fd-index
append rec-blk compose [ ( to-word fd/:fd-index/1 ) (data-item) ]
]
append/only res-block copy rec-blk
]
return res-block
]
return r
]
dupe-record: func [
table-name id-num
/local sql-cmd-head sql-cmd-tail
; Global to this context field-details
][
sql-cmd-head: copy rejoin [ "insert into " table-name "( " ]
sql-cmd-tail: copy " select "
fd: edit-db/get-field-details
foreach f (skip fd 1) [
append sql-cmd-head rejoin [ f/1 ", " ]
append sql-cmd-tail rejoin [ f/1 ", " ]
]
remove/part skip tail sql-cmd-head -2 2
remove/part skip tail sql-cmd-tail -2 2
append sql-cmd-head " )"
append sql-cmd-tail rejoin [ " from " table-name " WHERE ID='" id-num "'" ]
full-sql-cmd: rejoin [ sql-cmd-head sql-cmd-tail ]
run-sql full-sql-cmd
return first first run-sql {SELECT LAST_INSERT_ID()}
]
create-new-record: func [
table-name
/local values-str sql-cmd new-row f-details i
][
run-sql rejoin [ {INSERT INTO } database "." table-name { () VALUES()} ]
new-row: run-sql {SELECT LAST_INSERT_ID()}
if all [ ( new-row <> [[]]) ( new-row <> [["0"]] )] [
new-row: first first new-row
return to-integer new-row
]
auto-increment: last first query-db/get-field-details
either(auto-increment = "auto_increment") [
my-request rejoin [ "Unable to create new record^/Problem with creating a new record^/New Record ID = " new-row ]
][
my-request rejoin [ "auto_increment has not been set for the 'ID' field.^/This need to be done for table: '" query-db/table "' before it ^/can be used by DB-Rider" ]
]
return none
]
insert-new-record: func [
table-name record-values [ object! ]
/field-xref-block field-xref
/local ivalues-str sql-cmd new-row f-details get-field-value i
][
f-details: get-field-details/for-table table-name
get-field-value: func [ rec-vals field-name ] [
either field-xref-block [
either xref-name: select field-xref (to-word field-name) [
either ret-val: select rec-vals (to-lit-word xref-name ) [
return ret-val
][
my-request rejoin [ "insert-new-record ERROR. Cross reference name of:" xref-name " not present in record-values block." newline
"A value of NONE will be used for field name: " field-name
]
return none
]
][
return none
]
][ ; No xref block used. Record field names match database exactly
either ret-val: select rec-vals (to-lit-word field-name ) [
return ret-val
][
return none ; Not found in record-values
]
]
return none
]
ivalues-str: copy {VALUES (}
for i 1 (length? f-details) 1 [
either ( i = primary-index?/offset ) [ ; find the primary index position and make it NULL
append ivalues-str "NULL"
][
current-field-name: f-details/:i/1
field-val: get-field-value record-values current-field-name
either field-val [
verified-field-value: verify-set-field table-name current-field-name field-val
append ivalues-str rejoin [ {'} verified-field-value {'} ] ; insert value from record supplied
][
append ivalues-str rejoin [ {'} f-details/:i/5 {'} ] ; insert default value
]
]
either i = (length? f-details) [
break
][
append ivalues-str {, }
]
]
append ivalues-str {)}
sql-cmd: rejoin [ {INSERT INTO `} table-name {` } ivalues-str ]
run-sql sql-cmd
new-row: first first run-sql {SELECT LAST_INSERT_ID()}
return to-integer new-row
]
show-record: func [ table-name row-id /no-history-field /local old-field ] [
old-table: copy edit-db/table
old-record-number: edit-db/current-record-number
either no-history-field [
old-field: ""
][