https://docs.racket-lang.org/sql/ is a nice DSL for preparing SQL statements, which works with the db
library.
The docs show an example like:
(require sql)
(create-table #:temporary the_numbers
#:columns [n integer #:not-null] [d varchar])
In this code the_numbers
is not an identifier, it's treated literally as the name of the table.
What I want to do is something like:
(require sql)
(define (my-create-table table-name)
(create-table #:temporary table-name
#:columns [n integer #:not-null] [d varchar]))
This gives an error because it treats table-name
as the actual table name and it doesn't like the hyphen in it (I think it should be possible to use that as a table name but I guess I need to do something more to have the lib quote it properly...)
I am new to Racket and don't know many tricks. I tried using 'table-name
but that doesn't work.
The features you need are described in the Dynamic Statement Composition and SQL Injection of the docs. You can write the Ident for the table's name as (Ident:AST ,expr)
, where expr
produces an Ident AST value.
> (define (my-create-table table-name)
(create-table #:temporary (Ident:AST ,(make-ident-ast table-name))
#:columns [n integer #:not-null] [d varchar]))
> (my-create-table 'the_numbers)
(sql-statement
"CREATE TEMPORARY TABLE the_numbers (n integer NOT NULL, d varchar)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With