Index: sqlite-tools.rkt ================================================================== --- sqlite-tools.rkt +++ sqlite-tools.rkt @@ -54,11 +54,11 @@ ;; Simple SQL makers [make-table-schema ((string? (listof stringish?)) (#:primary-key-cols (listof stringish?)) . ->* . string?)] [make-insert/replace-query (stringish? (listof stringish?) . -> . string?)] - [make-select-query (stringish? (listof stringish?) #:where string? . -> . string?)] + [make-select-query (stringish? (listof stringish?) #:where stringish? . -> . string?)] ;; Database operations [init-db! ((pathish?) #:rest (listof string?) . ->* . void?)] [query! ((string?) #:rest (listof any/c) . ->* . void?)] [select-rows! (case-> @@ -157,11 +157,11 @@ ;; Create a query that inserts a row if it doesn’t exist (based on the first ;; column only), or updates it if it does. The returned query is parameterized, ;; and must be used with a list of values equal in length to the number of ;; fields given. (define (make-insert/replace-query tablename fields) - (format "INSERT OR REPLACE INTO `~a` (`rowid`, ~a) values ((SELECT `rowid` FROM `~a` WHERE `~a`= ?1), ~a)" + (format "INSERT OR REPLACE INTO `~a` (`rowid`, ~a) VALUES ((SELECT `rowid` FROM `~a` WHERE `~a`= ?1), ~a)" tablename (list->sql-fields fields) tablename (first fields) (list->sql-parameters fields))) @@ -182,11 +182,11 @@ (check-equal? (make-table-schema 'posts '(title date) #:primary-key-cols '(author date)) "CREATE TABLE IF NOT EXISTS `posts` (`title`, `date`, PRIMARY KEY (`author`, `date`));") (check-equal? (make-insert/replace-query 'posts '(author title)) (string-append "INSERT OR REPLACE INTO `posts` (`rowid`, `author`, `title`) " - "values ((SELECT `rowid` FROM `posts` WHERE `author`= ?1), ?1, ?2)")) + "VALUES ((SELECT `rowid` FROM `posts` WHERE `author`= ?1), ?1, ?2)")) (check-equal? (make-select-query 'posts '(author title) #:where 1) "SELECT `author`, `title` FROM `posts` WHERE 1")) (define (good-connection?) @@ -193,10 +193,11 @@ (and (connection? (sqltools:dbc)) (connected? (sqltools:dbc)))) ;; Initialize the database connection, creating the database if it does not yet exist ;; and running any provided queries (e.g., "CREATE TABLE IF NOT EXISTS...") (define (init-db! filename . qs) + (cond [(good-connection?) (disconnect (sqltools:dbc))]) (sqltools:dbc (sqlite3-connect #:database filename #:mode 'create)) (unless (empty? qs) (for ([q (in-list qs)]) (query! q))))