;; ============================================== ;; Script: btn-sqlite.r ;; downloaded from: www.REBOL.org ;; on: 19-Mar-2024 ;; at: 11:01:08.177389 UTC ;; owner: piotrgapinsk [script library member who ;; can update this script] ;; ============================================== REBOL [ Title: "better-than-nothing sqlite3 handler" Purpose: "easy access to sqlite3 database without /Pro or /Command features" Comment: "based on mysql-protocol 1.0.2 by Nenad Rakocevic / SOFTINNOV" Author: "Piotr Gapinski" Email: {news [at] rowery! olsztyn.pl} File: %btn-sqlite.r Date: 2006-01-30 Version: 0.2.2 Copyright: "Olsztynska Strona Rowerowa http://www.rowery.olsztyn.pl/" License: "GNU General Public License (GPL)" History: [0.1.0 2006-01-20 0.1.1 2006-01-20 0.2.0 2006-01-25 0.2.1 2006-01-27 0.2.2 2006-01-30] Library: [ level: 'intermediate platform: [Linux Windows] type: [protocol tool] domain: [protocol database] tested-under: [ view 1.3.2 on [Linux WinXP] core 2.6.2 on [Linux WinXP] ] support: none license: 'GPL ] ] make root-protocol [ scheme: 'btn port-id: 0 port-flags: system/standard/port-flags/pass-thru awake: none open-check: none sqlite: none options: none linux?: equal? fourth system/version 4 sys-copy: get in system/words 'copy sys-insert: get in system/words 'insert sys-pick: get in system/words 'pick sys-close: get in system/words 'close sys-write: get in system/words 'write net-log: get in net-utils 'net-log init: func [[catch] port spec] [ if not url? spec [net-error "Bad URL"] net-utils/url-parser/parse-url port spec if none? port/target [net-error reform ["No database name for" port/scheme "is specified"]] port/locals: make object! [columns: none rows: 0 values: none sqlite-rc: 0 index: 0] port/url: spec sqlite: any [ select [3 "sqlite3.exe" 4 "/usr/bin/sqlite3"] (fourth system/version) "sqlite3" ] options: {-html -header} ] open: func [port [port!]][ port/state/flags: port/state/flags or port-flags ] close: func [port [port!]][] sql-escape: func [value [string!] /local chars no-chars want escaped escape mark] [ chars: charset want: {^(00)^/^-^M^(08)'"\} no-chars: complement chars escaped: ["\0" "\n" "\t" "\r" "\b" "\'" {\"} "\\"] escape: func [value][ mark: sys-insert remove mark sys-pick escaped index? find want value ] parse/all value [any [mark: chars (escape mark/1) :mark | no-chars]] value ] to-sql: func [value /local res] [ switch/default type?/word value [ none! ["NULL"] date! [ rejoin ["'" value/year "-" value/month "-" value/day either value: value/time [ rejoin [" " value/hour ":" value/minute ":" value/second] ][""] "'" ] ] time! [join "'" [value/hour ":" value/minute ":" value/second "'"]] money! [head remove find mold value "$"] string! [join "'" [sql-escape sys-copy value "'"]] binary! [to-sql to string! value] block! [ if empty? value: reduce value [return "(NULL)"] res: append make string! 100 #"(" forall value [repend res [to-sql value/1 #","]] head change back tail res #")" ] ][form value] ] map-rebol-values: func [data [block!] /local args sql mark] [ args: reduce next data sql: sys-copy sys-pick data 1 mark: sql while [found? mark: find mark #"?"][ mark: sys-insert remove mark either tail? args ["NULL"] [to-sql args/1] if not tail? args [args: next args] ] sql ] insert-query: func [port [port!] data [string! block!] /local cmd] [ cmd: reform [sqlite options port/target rejoin [{"} data {"}]] net-log ["call" cmd] port/locals/sqlite-rc: call/wait/output cmd port/state/inBuffer ] parse-schema: func [port [port!] /local headers parts] [ headers: sys-copy [] parts: [