#lang racket (require racket/runtime-path db (for-syntax racket/base syntax/parse))
(define-syntax (define-sql-statement stx) (syntax-parse stx [(_ name #;path) (define location (syntax-source #'name)) (define-values (path file _) (split-path location)) (with-syntax ([path path]) #'(begin (define-runtime-path rpath path) (define name (let ([vq #f]) (lambda () (unless vq (let ([s (call-with-input-file rpath port->string)]) (set! vq (virtual-statement (lambda (_) s))))) vq)))))])) (define-sql-statement a #;"b") (provide define-sql-statement) (But this creates a system-specific path.) > On Aug 31, 2018, at 6:25 AM, Alex Harsanyi <alexharsa...@gmail.com> wrote: > > > I have defined a macro to help with using SQL queries from external files: it > uses `define-runtime-path` to store the path to the file and defines a > function that reads the data when called the first time and constructs a > `virtual-statement`: > > #lang racket > (require racket/runtime-path db (for-syntax racket/base)) > > (define-syntax (define-sql-statement stx) > (syntax-case stx () > [(_ name path) > #'(begin > (define-runtime-path rpath path) > (define name > (let ([vq #f]) > (lambda () > (unless vq > (let ([s (call-with-input-file rpath port->string)]) > (set! vq (virtual-statement (lambda (_) s))))) > vq))))])) > > (provide define-sql-statement) > > Unfortunately, the generated runtime path is relative to the location of the > file defining the macro, not the file that uses it. That is, in the example > below, "query.sql" needs to be in "subdir" together with > "define-sql-statement.rkt": > > #lang racket > (require "subdir/define-sql-statement.rkt") > (define-sql-statement x "query.sql") > > Is there a way to write this macro such that the runtime path is defined > relative to the location of the file that uses `define-sql-statement`? > > If you want to test this, you can put any text in query.sql (e.g "select * > from test") > > Thanks, > Alex. > > > > -- > 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.