From d1de827a005f2faff31f656b9fcb5d75603c3cfc Mon Sep 17 00:00:00 2001 From: Moritz Heidkamp Date: Thu, 18 Aug 2011 16:14:03 +0200 Subject: [PATCH] change insert syntax --- ssql.scm | 50 +++++++++++++++++++++++++++------------------ tests/ansi-test.scm | 18 +++++++++------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/ssql.scm b/ssql.scm index b739c15..dacfbe8 100644 --- a/ssql.scm +++ b/ssql.scm @@ -55,17 +55,25 @@ ((pair->sql pair) (self (car pair) (cdr pair))) - - ((values vals) + + ((list->sql-tuple list) (sprintf "(~A)" (string-intersperse (map (lambda (s) (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) - (self 'values (vector->list vec))) + (self 'list->sql-tuple (vector->list vec))) ((string->sql string) (string-append "'" (self 'escape-string string) "'")) @@ -100,29 +108,31 @@ ((set values) (string-append "SET " - (string-intersperse (map (lambda (val) - (sprintf "~A = ~A" - (first val) - (self 'ssql->sql (second val)))) - values) - ", "))) - - ((insert into values) - (sprintf "INSERT INTO ~A VALUES ~A" + (string-intersperse + (map (lambda (val) + (sprintf "~A = ~A" + (first val) + (self 'ssql->sql (second val)))) + values) + ", "))) + + ((insert into rest) + (sprintf "INSERT INTO ~A ~A" into - (string-intersperse (map (lambda (val) - (self 'ssql->sql val)) - values) ", "))) + (string-intersperse + (map (lambda (s) + (self 'ssql->sql s)) + rest)))) - ((insert (('into table) ('columns columns ...) values ...)) + ((insert (('into table) ('columns columns ...) rest ...)) (self 'insert (sprintf "~A (~A)" table (string-intersperse (map symbol->string columns) ", ")) - values)) + rest)) - ((insert (('into table) values ...)) - (self 'insert table values)) + ((insert (('into table) rest ...)) + (self 'insert table rest)) ((operator->sql type operator separator operands) (case type diff --git a/tests/ansi-test.scm b/tests/ansi-test.scm index b9ddb08..b267e14 100644 --- a/tests/ansi-test.scm +++ b/tests/ansi-test.scm @@ -39,19 +39,23 @@ (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")))))))) + (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 "multiple records using vectors for the records" "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" "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 "set literals"