basic insert support

master
Moritz Heidkamp 13 years ago
parent e6bd91c7bc
commit f0b456f288

@ -56,13 +56,17 @@
((pair->sql pair) ((pair->sql pair)
(self (car pair) (cdr pair))) (self (car pair) (cdr pair)))
((vector->sql vec) ((values vals)
(format "(~A)" (sprintf "(~A)"
(string-intersperse (string-intersperse
(map (lambda (s) (self 'ssql->sql s)) (map (lambda (s)
(vector->list vec)) (self 'ssql->sql s #t))
vals)
", "))) ", ")))
((vector->sql vec)
(self 'values (vector->list vec)))
((string->sql string) ((string->sql string)
(string-append "'" (self 'escape-string string) "'")) (string-append "'" (self 'escape-string string) "'"))
@ -103,6 +107,14 @@
values) values)
", "))) ", ")))
((insert (('into table) ('columns columns ...) values ...))
(sprintf "INSERT INTO ~A (~A) VALUES ~A"
table
(string-intersperse (map symbol->string columns) ", ")
(string-intersperse (map (lambda (val)
(self (car val) (cdr val)))
values) ", ")))
((operator->sql type operator separator operands) ((operator->sql type operator separator operands)
(case type (case type
((infix) ((infix)
@ -138,12 +150,17 @@
type))) type)))
(else (error "unknown operator syntax type" type)))) (else (error "unknown operator syntax type" type))))
((ssql->sql ssql) ((ssql->sql ssql parenthesize?)
(let ((handler (alist-ref (list ssql) (self 'type->sql-converters) apply))) (let ((handler (alist-ref (list ssql) (self 'type->sql-converters) apply)))
(if handler (if handler
(self handler ssql) (if (and parenthesize? (pair? ssql))
(sprintf "(~A)" (self handler ssql))
(self handler ssql))
(error "unknown datatype" ssql)))) (error "unknown datatype" ssql))))
((ssql->sql ssql)
(self 'ssql->sql ssql #f))
((insert-clause clause ssql) ((insert-clause clause ssql)
(let ((order (self 'clauses-order))) (let ((order (self 'clauses-order)))
(let loop ((ssql ssql)) (let loop ((ssql ssql))
@ -181,8 +198,6 @@
(distinct prefix) (distinct prefix)
(all prefix) (all prefix)
(values function)
(upper function) (upper function)
(lower function) (lower function)
(string-append infix "||") (string-append infix "||")

@ -36,6 +36,14 @@
"UPDATE actors SET firstname = 'Felix' WHERE (lastname = 'Winkelmann')" "UPDATE actors SET firstname = 'Felix' WHERE (lastname = 'Winkelmann')"
(ssql->sql #f '(update actors (set (firstname "Felix")) (where (= lastname "Winkelmann")))))) (ssql->sql #f '(update actors (set (firstname "Felix")) (where (= lastname "Winkelmann"))))))
(test-group "inserts"
(test "with sub-queries"
"INSERT INTO roles (character, movie_id, actor_id) VALUES ('Dr. Hasenbein', (SELECT id FROM movies WHERE (title = 'Praxis Dr. Hasenbein')), (SELECT id FROM actors WHERE ((firstname = 'Helge') AND (lastname = 'Schneider'))))"
(ssql->sql #f '(insert (into roles) (columns character movie_id actor_id)
(values "Dr. Hasenbein"
(select (columns id) (from movies) (where (= title "Praxis Dr. Hasenbein")))
(select (columns id) (from actors) (where (and (= firstname "Helge")
(= lastname "Schneider")))))))))
(test-group "syntax" (test-group "syntax"
(test "set literals" (test "set literals"

Loading…
Cancel
Save