Tagged as LISP, Personal, Programming
Written on 2010-04-12 01:11:48
Things have been crazy lately but I'm not here to give a full update. I will say that there's been good with the bad, family, friends and supporters all the while and that the bad is mostly the usual bureaucratic and financial troubles that are just a part of life. I'm trying to post more regularly. Today is a brief programming post.(defclass user ()
((username
:initarg :username :reader :username)
(password
:initarg :password :reader :password)
(salt
:initarg :salt :reader :salt)
(email
:initarg :email :reader :email)
(first-name
:initarg :first-name :reader :first-name)
(last-name
:initarg :last-name :reader :last-name)
(zip
:initarg :zip :reader :zip)))
(defclass user ()
((username
:col-type string
:initarg :username :reader :username)
(password
:col-type string
:initarg :password :reader :password)
(salt
:col-type string
:initarg :salt :reader :salt)
(email
:col-type string
:initarg :email :reader :email)
(first-name
:col-type string
:initarg :first-name :reader :first-name)
(last-name
:col-type string
:initarg :last-name :reader :last-name)
(zip
:col-type integer
:initarg :zip :reader :zip))
(:metaclass dao-class)
(:keys username))
(execute (dao-table-definition 'user))
. However, this is really intended as a shortcut for cases where you have a simple table definition. Say you wanted to allowed users to own collections of things, maybe collectible cards, and track those in the database as well. You ought to have foreign key constraints on the database so that collections couldn't be owned by users that didn't exist or consist of cards that didn't exist or were made up.(create-table 'class)
or (create-all-tables)
if you have several tables. This would make for nasty code duplication since you'd still need a dao-object class to interact with the tables as nicely as possible. Thankfully, there's a macro to clear the situation up and import the simple parts from your dao-class specification. A possible deftable for the collection class might look like this:(deftable collection
(!dao-def) ;; Import the existing info from the dao-class definition.
(!foreign 'user 'username) ;; Ensure that the username that owns the collection exists in our user table.
;; Ensure that each card in a collection has a name and edition corresponding to a card in our database.
(!foreign 'card '(card-name card-edition)
'(name edition)))