basic insert support

master
Moritz Heidkamp 13 years ago
parent e6bd91c7bc
commit f0b456f288

@ -56,13 +56,17 @@
((pair->sql pair)
(self (car pair) (cdr pair)))
((vector->sql vec)
(format "(~A)"
((values vals)
(sprintf "(~A)"
(string-intersperse
(map (lambda (s) (self 'ssql->sql s))
(vector->list vec))
(map (lambda (s)
(self 'ssql->sql s #t))
vals)
", ")))
((vector->sql vec)
(self 'values (vector->list vec)))
((string->sql string)
(string-append "'" (self 'escape-string string) "'"))
@ -103,6 +107,14 @@
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)
(case type
((infix)
@ -138,12 +150,17 @@
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)))
(if handler
(self handler ssql)
(if (and parenthesize? (pair? ssql))
(sprintf "(~A)" (self handler ssql))
(self handler ssql))
(error "unknown datatype" ssql))))
((ssql->sql ssql)
(self 'ssql->sql ssql #f))
((insert-clause clause ssql)
(let ((order (self 'clauses-order)))
(let loop ((ssql ssql))
@ -181,8 +198,6 @@
(distinct prefix)
(all prefix)
(values function)
(upper function)
(lower function)
(string-append infix "||")

@ -36,6 +36,14 @@
"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 "set literals"

Loading…
Cancel
Save