add array literal support, as well as the @> and <@ operators

master
Moritz Heidkamp 13 years ago
parent 33cf06740e
commit eaf45653a5

@ -3,7 +3,7 @@
(*postgresql-translator*) (*postgresql-translator*)
(import chicken scheme) (import chicken scheme)
(use ssql postgresql foops) (use ssql postgresql foops data-structures)
(define *postgresql-translator* (define *postgresql-translator*
(let ((type->sql-converters (let ((type->sql-converters
@ -12,7 +12,7 @@
(clauses-order (append (*ansi-translator* 'clauses-order) (clauses-order (append (*ansi-translator* 'clauses-order)
'(returning)))) '(returning))))
(derive-object (*ansi-translator*) (derive-object (*ansi-translator* self)
((escape-string string) ((escape-string string)
(escape-string (ssql-connection) string)) (escape-string (ssql-connection) string))
@ -21,13 +21,23 @@
((clauses-order) clauses-order) ((clauses-order) clauses-order)
((type->sql-converters) type->sql-converters)))) ((type->sql-converters) type->sql-converters)
((array (elements ...))
(sprintf "ARRAY[~A]"
(string-intersperse
(map (lambda (el)
(self 'ssql->sql el))
elements)
", "))))))
(define-operators *postgresql-translator* (define-operators *postgresql-translator*
(limit prefix) (limit prefix)
(offset prefix) (offset prefix)
(returning prefix "RETURNING" ", ") (returning prefix "RETURNING" ", ")
(random function)) (random function)
(@> infix)
(<@ infix))
(register-sql-engine! connection? *postgresql-translator*) (register-sql-engine! connection? *postgresql-translator*)

@ -27,10 +27,24 @@
(test "returning" (test "returning"
"INSERT INTO widgets VALUES ('foo', 'bar') RETURNING id, name" "INSERT INTO widgets VALUES ('foo', 'bar') RETURNING id, name"
(ssql->sql #t '(insert (into widgets) (values ("foo" "bar")) (returning id name)))) (ssql->sql #t '(insert (into widgets) (values #("foo" "bar")) (returning id name))))
(test "compose returning" (test "compose returning"
'(insert (into widgets) (values (1 2 3)) (returning id)) '(insert (into widgets) (values #(1 2 3)) (returning id))
(ssql-compose #t '(insert (into widgets) (values (1 2 3))) '((returning id))))) (ssql-compose #t '(insert (into widgets) (values #(1 2 3))) '((returning id)))))
(test-group "arrays"
(test "literals"
"SELECT ARRAY[1, 2, 3]"
(ssql->sql #t '(select (array 1 2 3))))
(test "contains operator"
"SELECT (ARRAY['foo', 'bar'] @> ARRAY['bar'])"
(ssql->sql #t '(select (@> (array "foo" "bar") (array "bar")))))
(test "is contained operator"
"SELECT (ARRAY['bar'] <@ ARRAY['foo', 'bar'])"
(ssql->sql #t '(select (<@ (array "bar") (array "foo" "bar"))))))
(test-exit) (test-exit)
Loading…
Cancel
Save