chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Thoughts on an O/RM library?


From: Graham Fawcett
Subject: [Chicken-users] Thoughts on an O/RM library?
Date: Fri, 3 Feb 2006 15:01:10 -0500

Hi folks,

I'm playing with the idea of writing an object-relational mapper
(O/RM) library for Chicken. My personal goal for the O/RM would be to
auto-generate a domain-specific syntax for an application, mapped onto
the actual, relational schema of the database, and encapsulating the
low-level database API. Theoretically, an O/RM can make it possible to
switch an application between different back-end databases, but I
think the free syntax and high-level functions are the real win.

Before getting too ahead of myself (i.e. before writing any actual
code!) I thought I'd post a sample syntax/API to the list for any
feedback. Fancy macros aren't my forte, and I may be creating a
(define-schema) syntax that's unncessarily hard to parse.

Is anyone else interested in this kind of thing? I'd love to hear your feedback.

Graham

;;; Possible syntax for an ORM for (Chicken) Scheme.

(require-extension orm)
(define *db-connection-url* "postgres://address@hidden/database")

;; set up the db connection via a parameter call...
(orm:default-connection (orm:make-connection *db-connection-url*))

;; define schema for an application.
;; Example: a poll, where each poll has multiple choices
;; and each choice keeps a tally of votes for that choice.
;; Example inspired by Django demo,
;; http://www.djangoproject.com/documentation/tutorial1/

(orm:define-schema "poll-application"
  (table poll
         ;; column definitions. An (id primary-key) column is implicit
         ;; if no primary-key is specified.
         ((question string
                    allow-null: #f
                    length: 200)
          (pub-date date-time
                    allow-null: #f
                    default: (lambda () (current-time)))))
  (table choices
         ((poll foreign-key
                foreign-table: "poll"))
         (choice string
                 allow-null: #f)
         (votes integer
                default: 0))

  ;; indexes, counters, other schema constructs...
  (options
   create-tables-if-missing: #t))

;; at this point, tables have been created, as have numerous
;; constructors, getter and setter functions.

;; "class methods" e.g. "create a new poll" use a slash syntax,
;; class-name/method, e.g. (poll/new) or (poll/save-all!)

;; "instance methods" eg. "get date of this poll" use a colon syntax,
;; classname:method, e.g. (poll:pub-date my-poll-record)

;; use the schema;
;; create a question and manipulate it.

(let ((p (poll/new
          question: "What do you think of Chicken?")))

  ;; predicate checks
  (assert (orm:record? p))   ;; is an ORM record?
  (assert (poll/record? p))    ;; is an ORM record from table "poll"?

  ;; update the question
  (poll:question-set! p (conc "Really, tell me: " (poll:question p)))
  ;; explicit save.
  (polls/save-all!)                     ; or (poll:save p)

  ;; add some choices
  (choice/new poll: p
              choice: "My favourite Scheme.")
  (choice/new poll: p
              choice: "Good with rice and peas.")
  (choices/save-all!)

  ;; return the choices for our poll.
  (map choice:choice (poll:choices-list p)))

;; would evaluate to
;; '("My favourite Scheme." "Good with rice and peas.")




reply via email to

[Prev in Thread] Current Thread [Next in Thread]