diff --git a/sqlog/compiler.fnl b/sqlog/compiler.fnl index 276072a..e2ec9e2 100644 --- a/sqlog/compiler.fnl +++ b/sqlog/compiler.fnl @@ -98,7 +98,10 @@ "Defines the column names of a table and their expected ordering" (when (. name self.rules) (error "tables and rules must not overlap")) (tset self.tables name [...]) - {:query (.. "CREATE TABLE " name "(" (cat [...] ", ") ")") :constants [] :selection []}) + {:query (.. "CREATE TABLE " (quoteid name) "(" (cat [...] ", " quoteid) + ", PRIMARY KEY (" (cat [...] ", " quoteid) "))") + :constants [] + :selection []}) (fn Compiler.defrule [self head ...] "Defines a new rule or expands the definition of an existing rule." @@ -108,7 +111,6 @@ (tset self.rules name rulelist)) _ (error "Expected literal for head, got " (fv head)))) - (fn new-analysis [?parent] "Creates a new empty analysis object. If ?parent is supplied, share the list of constants and referenced rules, as these are shared with the full SQL expression." diff --git a/sqlog/init.fnl b/sqlog/init.fnl index 6ca2850..5e4e870 100644 --- a/sqlog/init.fnl +++ b/sqlog/init.fnl @@ -10,7 +10,6 @@ (fn Sqlog.deftable [self name ...] (self:execute (self.compiler:deftable name ...))) (fn Sqlog.defrule [self head ...] (self.compiler:defrule head ...)) -(fn Sqlog.defrules [self ...] (self.compiler:defrules ...)) (fn Sqlog.compile-sql [self analysis] (when (= analysis.stmt nil) @@ -46,8 +45,11 @@ (fn Sqlog.specify [self action ...] (when (not= action nil) (match action - [:* & rule] (self:defrule (table.unpack rule)) - _ (self:execute (self:compile-action action))) + [:* & rule] (self:defrule (table.unpack rule)) + [:table & params] (self:deftable (table.unpack (icollect [_ param (ipairs params)] + (match param [_ name] name)))) + [:sql query & constants] (self:execute {: query : constants}) + _ (self:execute (self:compile-action action))) (self:specify ...))) (fn Sqlog.query [self ...] (self:execute (self:compile-query ...) true)) diff --git a/sqlog/macros.fnl b/sqlog/macros.fnl index 25ef066..419f902 100644 --- a/sqlog/macros.fnl +++ b/sqlog/macros.fnl @@ -4,7 +4,7 @@ (fn clause [c] (match c - (where [:hashfn expr] (list? c)) `[:const ,expr] + (where [escape expr] (list? c) (= (tostring escape) :hashfn)) `[:const ,expr] (where [name & params] (sequence? c)) `[:literal ,(tostring name) ,(icollect [_ param (ipairs params)] (clause param))] (where [head & args] (list? c) (sym? head)) (icollect [_ expr (ipairs args) :into [(tostring head)]] (clause expr)) (where c (list? c)) (icollect [_ val (ipairs c)] (clause val)) diff --git a/sqlog/sqltest.fnl b/sqlog/sqltest.fnl index f6e1432..f1951fe 100644 --- a/sqlog/sqltest.fnl +++ b/sqlog/sqltest.fnl @@ -2,16 +2,16 @@ (import-macros {: $ : query : specify} :sqlog.macros) (local s (Sqlog)) -(s:deftable :parent :parent :child) (specify s + (table parent parent child) (* [generation name (|| name " jr") 2] [parent name (|| name " jr")]) (* [generation name (|| name " iii") 3] [ancestor name (|| name " iii")]) - (* [ancestor x y 1] [parent x y]) + (* [ancestor x y 2] [parent x y]) (* [ancestor x y (+ gen 1)] [parent x z] [ancestor z y gen]) [parent :bob "bob jr"] [parent "bob jr" "bob iii"] - [parent :bob :fred] + [parent :bob #(.. :fred :dy)] [parent :fred :jim] [parent :fred :betty]) -(pp (query s [generation :bob descendant gen])) +(pp (query s [ancestor :bob descendant gen]))