On 7/31/2017 10:46 PM, Alex Harsanyi wrote:
I'm trying to write a function to keep the SQL query text outside of the
Racket source code, as this would make it easier to write and test the SQL
code.
Instead of writing:
(define query (virtual-statement (lambda (dbsys) "select ...")))
I would like to put the "select ..." part in a separate file and write
something like:
(define query (sql-query "./some-file.sql"))
The "sql-query" definition would look like this:
#lang racket
(require racket/runtime-path)
(define (sql-query file-name)
(define-runtime-path rp file-name)
(let ((qtext #f))
(virtual-statement
(lambda (dbsys)
(unless qtext
(set! qtext (file->string file)))
qtext))))
Unfortunately, `define-runtime-path` can only be used at top-level, so the
above code does not compile. It works fine without the `define-runtime-path',
I need to use it so they query files are found when the application is
compiled to a stand-alone executable.
Does anyone have an idea on how `sql-query` might be defined?
Thanks,
Alex.
Hmm. A change to a query often also means a change to its arguments
and/or its result columns, so I don't see that there is much utility in
keeping the query strings separate from the program. I certainly do
test/debug my queries first using a SQL command line, but once they are
working as I expect, I embed them into my application. I have a macro
that allows me to write [nicely formatted] free-form SQL which gets
converted into a query string suitable for use with the db library.
That said, if you really want to keep your SQL separate, I would not try
to load query strings at the point of use. Instead I would load all the
query strings at startup and store them in a hash table with appropriate
identifiers. Then the query function could [minimally] be something like:
(define queryhash (hash))
:
(define (sql-query qident . args)
(let ((dbc ...)
(qtext (hashref queryhash qident #f))
(if (and (connection? dbc)
qtext)
(apply query dbc qtext args)
(error ... )
)))
Beware that I made this up on the spot - it is only intended to
illustrate the way I would approach the problem.
YMMV,
George
--
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.