I think the first question is "what are you trying to achieve?"  What
differences should the system demonstrate if it's hooked to MySQL vs
PostgreSQL?

Personally, I just pass connection objects around.  As long as you stick
with standard SQL this is all that's required to make your system work with
any DB.  (Obviously, you need to avoid DB-specific extensions like
Postgres's incredibly convenient 'INSERT ... RETURNING <columns>' clause.)

Here's an example:

(define (dbh)
   (postgresql-connect #:user "postgres"
                        #:database "dbname"
                        #:password "pass"))

(define (placeholders-for lst [start-from 1])
  (string-join
      (for/list ((i (in-naturals start-from))
                (ignored lst))
                (~a "$" i))  ;; returns, e.g. "$1,$2,$3"
  ","))

(define (add-user username [db (dbh)])
    (query-exec
      db
      (format "INSERT INTO users (username) VALUES (~a)"
          (placeholders-for (list username)))
      username))

This is the bare bones for a completely engine-agnostic system.  You could
add conditionals to dbh and placeholders-for so that they knew to generate
a MySQL handle and the string "?,?,?" instead of a Pg handle and
"$1,$2,$3"  Then you can use add-user without caring what engine you're
attached to, just by passing in a connection object of the right type.








On Fri, Nov 11, 2016 at 9:39 AM, Ken MacKenzie <deviloc...@gmail.com> wrote:

> I am trying to figure out the best way to do this and granted I am not
> sure my architecture is the most sane or sensible way to handle it.  I am
> trying to build my DB interface and I want to allow the system to deal with
> numerous types of data stores.  So I have a config file that is read and
> gets the information needed.  So to pseudo lay it out:
>
> (define (query-db where-clause))
>
> (define (db-type)
>   (cond
>     [(=? db-conf '(mysql)) (????)]) ;something to redefine how query-db
> actually works.
> )
>
> Maybe this is silly as in query-db I can just do a conditional for each
> type.  But then I have to do a conditional for each DB action, connect,
> drop, query, update, etc.  I was thinking loading the conditional and
> redefining place holders would be a more elegant solution.  I don't know
> just spit balling with a different design pattern here.
>
> Ken
>
> --
> 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