Skip to content

Instantly share code, notes, and snippets.

@gurzgri
Last active June 10, 2022 09:13
Show Gist options
  • Save gurzgri/a75c23b5e32fda1d3a5d7f62c23ecb46 to your computer and use it in GitHub Desktop.
Save gurzgri/a75c23b5e32fda1d3a5d7f62c23ecb46 to your computer and use it in GitHub Desktop.
Table Template meets ODBC scheme
Red [
Author: ["Toomas Vooglaid" "Christian Ensel"]
Needs: 'View
]
#include %table-template.red
port?: func [value [any-type!]] [port! = type? :value] ;-- missing helper
context [
≣sources: ≣catalogs: ≣schemas: ▤tables: ▢sql: ⨀tables: ⊞content: none ;-- forgive me :^)
source: infos: catalog: schema: table: none
connection: statement: tables: none
sources: extract system/schemes/odbc/state/sources 2
update-⊞content: function [/row-count rows] [
either row-count [
records: compose/deep [["Result"] [(rejoin ["Operation affected " rows " row" pick ["s" ""] rows > 1])]]
][
insert/only records: copy change statement [flat?: no]
headers: extract/index statement/state/columns 10 2
]
⊞content/data:
⊞content/actors/data: records
⊞content/actors/use-state/with ⊞content [frozen-rows: [1]]
]
update-≣catalogs: does [≣catalogs/data: unique extract/index tables 5 1 ≣catalogs/selected: 1]
update-≣schemas: does [ ≣schemas/data: unique extract/index tables 5 2 ≣schemas/selected: 1]
update-≣tables: does [ ▤tables/data: unique extract/index tables 5 3 ▤tables/selected: 1]
view [
title "RedSQL"
on-close [all [port? connection open? connection close connection]]
panel [
origin 0x0 across
text 45 "Source" ≣sources: drop-down 95 data sources on-change [
source: pick face/data face/selected
attempt [close connection]
catalog: schema: table: none
statement: open connection: open rejoin [odbc:// source]
infos: query connection
insert change statement [flat?: yes] [tables none none none "TABLE"]
tables: copy statement
update-≣catalogs
do-actor ≣catalogs none 'change
] return
text 45 "Catalog:" ≣catalogs: drop-down 95 on-change [
catalog: pick face/data face/selected
close statement ;-- changing catalog doesn't work with Text Driver while having an open statemenet
change connection compose [catalog: (catalog)]
statement: open connection
insert change statement [flat?: yes] compose [tables (catalog) none none "TABLE"]
tables: copy statement
update-≣schemas
do-actor ≣schemas none 'change
] return
text 45 "Schema:" ≣schemas: drop-down 95 on-change [
schema: pick face/data face/selected
attempt [insert statement rejoin ["SET search_path TO " schema]] ;-- postgres
insert change statement [flat?: yes] compose [tables (catalog) (schema) none "TABLE"]
tables: copy statement
update-≣tables
do-actor ▤tables none 'change
] return
]
▢sql: area 490x90 wrap
panel [
origin 1x0 below
button "Execute" 99 [/local [result]
unless all [port? statement open? statement] [exit]
either number? result: insert statement copy ▢sql/text [ ;-- needs COPY to trigger preparation
update-⊞content/row-count result
][
update-⊞content
]
]
⨀tables: radio 99 "Tables" data on on-change [do-actor ▤tables none 'change]
radio 99 "Columns"
]
return
▤tables: text-list 150x400 data [] on-change [/local [limit quote]
table: pick face/data face/selected
unless all [port? statement open? statement] [exit]
either ⨀tables/data [
quote: switch/default infos/"dbms-name" ["TEXT" "MariaDB" [#"`"]] [{"}]
insert statement rejoin [{SELECT * FROM } quote table quote]
update-⊞content
][
limit: statement/state/window ;-- hack to make SQLColumns work with MariaDB
statement/state/window: 1 ;
insert statement compose [columns (catalog) (schema) (table)] off
update-⊞content
statement/state/window: limit
]
]
⊞content: table 617x417
]
]
@gurzgri
Copy link
Author

gurzgri commented Jun 9, 2022

infos/"dbms-name" aware table identifier quoting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment