chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] rails-like framework


From: Thomas Chust
Subject: Re: [Chicken-users] rails-like framework
Date: Sat, 22 Apr 2006 18:40:41 +0000 (GMT)

On Sat, 22 Apr 2006, PeterBex wrote:

[...]
I think the hardest thing will be the ActiveRecord-like support since
Scheme isn't especially object-oriented.  The question is whether
that is really required.  The most important aspect about Rails is that
it has decent defaults.  You can just build a database and run Rake
to set up a Rails environment and you have a bare-bones interface to
the database out of the box.  To do things, you need the very minimum
of custom code.  This is the essence of what makes Rails so popular.
I'm not sure if this can be done as easily in a non-object-oriented
environment.  Ruby makes it easy to automatically build objects from
information because it allows you to add members to existing classes.
I think with the Prometheus egg we could do something like this.
[...]

Hello,

I don't know exactly what you would expect from a runtime object <-> database row interface, but I can't imagine that it requires a lot of effort to create something simple that gets the job done -- no matter which object system is used.

A small example using TinyCLOS and SQLite3 is attached. It does runtime generation of classes and accessor methods completely automatically from the database schema and is terribly easy to use because I didn't have the time to make up something sophisticated. The code allows you to write something like this:

    ;;; load extensions
    (require-extension
      sqlite3 tinyclos sqlite3-tinyclos)

    ;;; create a database
    (define db
      (sqlite3:open "test.db"))

    (sqlite3:exec db #<<EOD
      CREATE TABLE eggs(name TEXT NOT NULL,
                        license TEXT, is_module INTEGER NOT NULL DEFAULT 0,
                        PRIMARY KEY(name), CHECK(is_module IN (0, 1)));
EOD
)

    ;;; define the object class
    (sqlite3:define-stored-object-class db "eggs")

    ;;; add some data
    (let ((egg (make <egg> "sqlite3")))
      ;; now the entry should already exist
      (print (sqlite3:in-store? egg))
      ;; let's add some data
      (set-license! egg "BSD")
      ;; and read it back
      (print (license egg))
      ;; other fields are filled with default data
      (print (module? egg))
      ;; changing the primary key is possible as well
      (sqlite3:set-pk! egg "foo")
      (print (sqlite3:pk egg))
      ;; which does not change data, of course
      (print (license egg))
      ;; and we can also delete the entry
      (print (sqlite3:remove-from-store! egg))
      ;; then it is gone
      (print (sqlite3:in-store? egg)))

    ;; close database connection
    (sqlite3:finalize! db)

cu,
Thomas

Attachment: sqlite3-tinyclos.scm
Description: Mini bridge between SQLite3 and TinyCLOS


reply via email to

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