First off, many thanks for bringing to my attention the fact that Ryan has 
started racket-izing SQL! And thanks, Ryan, for racket-izing SQL! Maybe I can 
stop stapling together all of those long horrible strings now.

Next, to answer your question, I believe that eval is not necessary here, and 
is in fact the wrong approach.

I recommend that you take a look at section 3.7 of the documentation, "Dynamic 
Statement Composition and SQL Injection”, which I believe does exactly what you 
want. Specifically, you want to dynamically construct a select statement.

Also… I admit that I’m not *totally* sure that you get exactly the primitives 
you need, here. This might be a feature request.

John


> On Jan 31, 2019, at 7:58 AM, hashim muqtadir <hashim.muqta...@gmail.com> 
> wrote:
> 
> Hello,
> 
> The test in my following code raises an error saying that when I call 
> select/f, the result doesn't satisfy `sql-statement?`.
> 
> I've no idea why, the only vague idea I have is that it may have something to 
> do with the fact that it was essentially eval'd in a separate namespace.
> 
> Any ideas?
> 
>     #lang racket
>     
>     (require db)
>     
>     (define sql-namespace (make-base-empty-namespace))
>     (parameterize ([current-namespace sql-namespace])
>       (namespace-require 'sql sql-namespace))
>     
>     (struct Select-spec
>       (tbl  ;; symbol
>        col-exprs  ;; listof symbols
>        limit  ;; number
>        order-by)) ;; listof select symbols
>     
>     (define (select/f sel-spec)
>       (eval
>        `(select ,@(Select-spec-col-exprs sel-spec)
>                 #:from ,(Select-spec-tbl sel-spec)
>                 ,@(let ([lim (Select-spec-limit sel-spec)]
>                         [order (Select-spec-order-by sel-spec)])
>                     (if lim
>                         `(#:limit ,lim #:order-by ,@order)
>                         '())))
>        sql-namespace))
>     
>     (module+ test
>       (require sql)
>       (require rackunit)
>       (check-equal?
>        (sql-statement->string
>         (select/f
>           (Select-spec
>            'd
>            '(a b c)
>            10
>            '(a b))))
>        (sql-statement->string
>         (select a b c
>                 #:from d #:order-by a b #:limit 10))))
>     
>     (provide select/f
>              Select-spec)
>     
> 
> My main reason for doing this was that the statement-producing macros 
> provided by the sql package were, well, macros, so I couldn't pass in a list 
> of columns and get a statement. What I want to do is have a struct like the 
> one I made in this snippet, and use functions to include/exclude columns, 
> before finally passing it off to a function that produces the statement.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to