Simple OODB in Pico Lisp

Let’s walk through a simple and usual example, what would constitute a user table in MySQL:

(class +User +Entity)
(rel username (+Need +Key +String))
(rel password (+Need +String))

(pool "users.db")

(new! '(+User) 'username "sam" 'password "parrotno5")
(new! '(+User) 'username "fred" 'password "MegaPizza")
(new! '(+User) 'username "anna" 'password "swooosh")
(new! '(+User) 'username "fred" 'password "yiiihaaa")
(new! '(+User) 'username NIL 'password "asdf")

(mapcar show (collect 'username '+User))


{6} (+User)
   password "swooosh"
   username "anna"
{5} (+User)
   password "MegaPizza"
   username "fred"
{2} (+User)
   password "parrotno5"
   username "sam"
-> ({6} {5} {2})

That was quite a lot at the same time. All classes that are to generate objects stored in the database needs to be children of +Entity. Furthermore some relations are needed in the form of prefix classes, rel will in this case take the name of the relation, username, and the list of classes that will define the behavior of the relation. Prefix classes has been explained in the prior tutorial.

In the case of the username we have +Need which denotes that this relation is needed for successful creation of the persistent object. As you can see in the output the last call to new! (only difference from new is that we create the object in a file instead of in the RAM) never resulted in an object on disc since no key was created. In our case the username will be used as key and needs to be unique, hence no second “fred” in the output. Of course both username and password will both be strings. There is a short description of more relations in the reference.

Instead of the above run this (don’t delete users.db!):

(class +User +Entity)
(rel username (+Need +Key +String))
(rel password (+Need +String))

(pool "users.db")

(show '{2})

This will give us “sam” which means that he is directly accessible through {2} which Alex explains better than me:

External symbol names are surrounded by braces (’{’ and ‘}’). The characters of the symbol’s name itself identify the physical location of the external object. This is currently the number of the starting block in the database file, encoded in base-64 notation (characters ‘0′ through ‘9′, ‘:’ through ‘;’, ‘A’ through ‘Z’ and ‘a’ through ‘z’).

Instead of (show ‘{2}) try:

(show (db 'username '+User "sam"))

This is basically the equivalent of:

SELECT * FROM `user` WHERE BINARY username = 'sam'


(show (db 'username '+User "fred" 'password "MegaPizza"))

is of course the same as the login SQL:

SELECT * FROM `user` WHERE BINARY username = 'fred' AND BINARY password = 'MegaPizza'

Related Posts

Tags: ,