change insert syntax

master
Moritz Heidkamp 13 years ago
parent b2a6da33c2
commit d1de827a00

@ -56,16 +56,24 @@
((pair->sql pair) ((pair->sql pair)
(self (car pair) (cdr pair))) (self (car pair) (cdr pair)))
((values vals) ((list->sql-tuple list)
(sprintf "(~A)" (sprintf "(~A)"
(string-intersperse (string-intersperse
(map (lambda (s) (map (lambda (s)
(self 'ssql->sql s #t)) (self 'ssql->sql s #t))
vals) list)
", ")))
((values records)
(sprintf "VALUES ~A"
(string-intersperse
(map (lambda (record)
(self 'list->sql-tuple record))
records)
", "))) ", ")))
((vector->sql vec) ((vector->sql vec)
(self 'values (vector->list vec))) (self 'list->sql-tuple (vector->list vec)))
((string->sql string) ((string->sql string)
(string-append "'" (self 'escape-string string) "'")) (string-append "'" (self 'escape-string string) "'"))
@ -100,29 +108,31 @@
((set values) ((set values)
(string-append "SET " (string-append "SET "
(string-intersperse (map (lambda (val) (string-intersperse
(map (lambda (val)
(sprintf "~A = ~A" (sprintf "~A = ~A"
(first val) (first val)
(self 'ssql->sql (second val)))) (self 'ssql->sql (second val))))
values) values)
", "))) ", ")))
((insert into values) ((insert into rest)
(sprintf "INSERT INTO ~A VALUES ~A" (sprintf "INSERT INTO ~A ~A"
into into
(string-intersperse (map (lambda (val) (string-intersperse
(self 'ssql->sql val)) (map (lambda (s)
values) ", "))) (self 'ssql->sql s))
rest))))
((insert (('into table) ('columns columns ...) values ...)) ((insert (('into table) ('columns columns ...) rest ...))
(self 'insert (self 'insert
(sprintf "~A (~A)" (sprintf "~A (~A)"
table table
(string-intersperse (map symbol->string columns) ", ")) (string-intersperse (map symbol->string columns) ", "))
values)) rest))
((insert (('into table) values ...)) ((insert (('into table) rest ...))
(self 'insert table values)) (self 'insert table rest))
((operator->sql type operator separator operands) ((operator->sql type operator separator operands)
(case type (case type

@ -39,19 +39,23 @@
(test-group "inserts" (test-group "inserts"
(test "with sub-queries" (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'))))" "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) (ssql->sql #f '(insert (into roles)
(values "Dr. Hasenbein" (columns character movie_id actor_id)
(values ("Dr. Hasenbein"
(select (columns id) (from movies) (where (= title "Praxis Dr. Hasenbein"))) (select (columns id) (from movies) (where (= title "Praxis Dr. Hasenbein")))
(select (columns id) (from actors) (where (and (= firstname "Helge") (select (columns id) (from actors) (where (and (= firstname "Helge")
(= lastname "Schneider")))))))) (= lastname "Schneider")))))))))
(test "multiple records using vectors for the records" (test "multiple records using vectors for the records"
"INSERT INTO actors (firstname, lastname) VALUES ('Sylvester', 'Stallone'), ('Arnold', 'Schwarzenegger')" "INSERT INTO actors (firstname, lastname) VALUES ('Sylvester', 'Stallone'), ('Arnold', 'Schwarzenegger')"
(ssql->sql #f '(insert (into actors) (columns firstname lastname) #("Sylvester" "Stallone") #("Arnold" "Schwarzenegger")))) (ssql->sql #f '(insert (into actors)
(columns firstname lastname)
(values ("Sylvester" "Stallone")
("Arnold" "Schwarzenegger")))))
(test "without explicit columns" (test "without explicit columns"
"INSERT INTO actors VALUES ('Marlon', 'Brando')" "INSERT INTO actors VALUES ('Marlon', 'Brando')"
(ssql->sql #f '(insert (into actors) (values "Marlon" "Brando"))))) (ssql->sql #f '(insert (into actors) (values ("Marlon" "Brando"))))))
(test-group "syntax" (test-group "syntax"
(test "set literals" (test "set literals"

Loading…
Cancel
Save