rmculpepper / sql

Embedding of some of SQL into Racket
33 stars 5 forks source link

Add make-values*-table-expr-ast function #11

Closed LiberalArtist closed 6 years ago

LiberalArtist commented 6 years ago

This change makes it possible to construct INSERT statements for dynamic numbers of rows, for example.

Other changes:

Closes https://github.com/rmculpepper/sql/issues/9

LiberalArtist commented 6 years ago

I noticed while working on this that the functions from "private/dynamic.rkt" don't currently have tests. I'd be happy to try writing some.

LiberalArtist commented 6 years ago

Also, just to put it somewhere for posterity, here is a little macro I've been experimenting with using on top of make-values*-table-expr-ast:

(define-syntax-parser for/insert-statement 
  [(_ (#:into name-ast-form
       for-clause ...)
      body-or-break ...
      #:set
      [column-ident-form
       scalar-expr-form] ...)
   #`(insert
      #:into name-ast-form
      #:columns column-ident-form ...
      #:from
      (TableExpr:AST
       ,(make-values*-table-expr-ast
         (for/fold/derived #,this-syntax
           ([so-far '()])
           (for-clause ...)
           body-or-break ...
           (cons (list (scalar-expr-qq scalar-expr-form)
                       ...)
                 so-far)))))])

(for/insert-statement
    (#:into somewhere
     [row '(["apples" 1 "standard"]
            ["bananas" 5 "organic"]
            ["cranberries" 50 "canned"])])
  (match-define (list name num type)
    row)
  #:set
  [name ,name]
  [num ,num]
  [type ,type])
rmculpepper commented 6 years ago

Thanks!