Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Racket SQL: how to use variable value as table name in create-table?

Tags:

racket

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.

like image 946
Anentropic Avatar asked Sep 12 '25 08:09

Anentropic


1 Answers

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)")
like image 123
Ryan Culpepper Avatar answered Sep 16 '25 07:09

Ryan Culpepper