: ) wonderful world ( :

the metasyntactic variable

hu.dwim.perec tutorial, lisp clos persistency

with one comment

Let us see a session which demonstrates the possibility of persisting objects into a database and get it back later on by using project hu.dwim.perec (sometimes available on dwim.hu, when the server is up and running). The slots here are of type integer, but there’s a persistence strategy even for type t, when (AFAIK) some kind of internal representation is dumped. To setup a postgresql database for this session, feel free to read further here.

CL-USER> (setf *load-verbose* nil)
NIL
CL-USER> (setf *compile-verbose* nil)
NIL
CL-USER> (asdf:oos 'asdf:load-op :hu.dwim.perec.postgresql :verbose nil)
#<ASDF:LOAD-OP (:VERBOSE NIL) {B2BC6C9}>
CL-USER> (defpackage :ptest
	   (:use :hu.dwim.common
		 :hu.dwim.def
		 :hu.dwim.defclass-star
		 :hu.dwim.perec
		 :hu.dwim.rdbms))
#<PACKAGE "PTEST">
CL-USER> (in-package :ptest)
#<PACKAGE "PTEST">
PTEST> (def special-variable
	   *psql-db*
	 (make-instance 'postgresql/perec
			:generated-transaction-class-name 'transaction
			:default-result-type 'vector
			:muffle-warnings t
			:connection-specification '(:database "perec_db1"
						    :user-name "perec_user"
						    :port 5433
						    :host "localhost"
						    :password "perec999pass")))
#<POSTGRESQL/PEREC {B4BF189}>
PTEST> (def persistent-class*
	   some-user-info ()
	 ((userid :type integer)
	  (some-data :type integer)))
#<PERSISTENT-CLASS SOME-USER-INFO>
PTEST> (make-compiled-query-cache)
#<HASH-TABLE :TEST EQUAL :COUNT 0 {C53BBC1}>
PTEST> (setf hu.dwim.perec::*compiled-query-cache* *)
#<HASH-TABLE :TEST EQUAL :COUNT 0 {C53BBC1}>
PTEST> (setf (hu.dwim.logger:log-level 'hu.dwim.rdbms::rdbms)
	     hu.dwim.logger:+fatal+
	     (hu.dwim.logger:log-level 'hu.dwim.rdbms::sql)
	     hu.dwim.logger:+fatal+)
5
PTEST> (with-database *psql-db*
	 (with-transaction
	   (make-instance 'some-user-info :userid 1 :some-data -1)))
#<SOME-USER-INFO :persistent #t 12>
PTEST> (with-database *psql-db*
	 (with-transaction
	   (make-instance 'some-user-info :userid 2 :some-data -2)))
#<SOME-USER-INFO :persistent #t 13>
PTEST> (with-database *psql-db*
	 (with-transaction
	   (select (o)
	     (from (o some-user-info))
	     (where (= 1 (userid-of o)))))) 
(#<SOME-USER-INFO :persistent #? 12 {B14A2E1}>)
PTEST> (with-database *psql-db*
	 (with-transaction
	   (some-data-of (revive-instance (car *)))))
-1
PTEST> 

 

Tables are created automatically in the appropriate database:

perec_db1=# select * from _some_user_info;
  _oid   | _userid | _some_data 
---------+---------+------------
  960005 |       1 |         -1
 1025541 |       2 |         -2
(2 rows)

perec_db1=# \d _some_user_info
  Table "public._some_user_info"
   Column   |  Type   | Modifiers 
------------+---------+-----------
 _oid       | bigint  | not null
 _userid    | numeric | 
 _some_data | numeric | 
Indexes:
    "_some_user_info_pkey" PRIMARY KEY, btree (_oid)

perec_db1=# 

Written by grault

January 6, 2010 - 9:36 am at January 6, 2010 - 9:36 am

Posted in linux, lisp

One Response

Subscribe to comments with RSS.

  1. [...] leave a comment » Update: in case you’ve already had a database, like the one below, a more up to date session is available here. [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.