Thank you for trying Snooze. Read on to find out how you can get everything up and running on your system. If you have any questions please don’t hesisitate to contact us; our email addresses are at the beginning of this manual.
We hope you enjoy Snooze!
The first thing you’ll need to do is download and install a DBMS. Snooze currently supports two database backends: SQLite 3.6+ and PostgreSQL 8.3+. Get hold of one of these, install it, configure it, and set up a blank database:
if you are using SQLite, creating a database is as simple as creating a file somewhere on your local filesystem;
if you are using PostgreSQL your will need to create a user with the necessary privileges and set up your "pg_hba.conf" to allow TCP/IP connections from localhost.
This is probably the hardest part of the setup process, and unfortunately it falls outside the scope of this document. If you are looking for a quick-and-easy setup for your system, you might want to follow the installation instructions for your platform on the SQLite web site.
Once have chosen your DBMS platform, you will need to create a Snooze interface in Scheme. This involves creating two scheme/class objects: one contains database-independent code, the other contains code specific to your database.
Create a blank file in your project directory called "db.ss" and enter the following template code:
( ( untyped/snooze:2)
( ( ; TODO: arguments...)))
( ( ( untyped/snooze:2))
Change <<DB>> to postgresql8 or sqlite3 depending on your choice of DBMS.
is an unhygeinic macro that takes a snooze object as an argument and defines wrapper procedures for each of its important methods. provides those procedures to the rest of your application. All of the procedures and macros described in the rest of this manual are provided by or directly from "snooze.ss".
All you need to do to complete "db.ss" is fill in the arguments to . The arguments will be different depending on whether you are using SQLite or PostgreSQL:
if you are using SQLite, rewrite the call to like this:
where <<PATH>> is either a path to your database file or one of the special symbols ':memory: (in-memory database) or ':temp: (temporary file);
if you are using PostgreSQL, rewrite the call to like this:
( #:server "<<SERVER>>"
<<SERVER>> is the hostname of your database server (e.g. "localhost");
<<PORT>> is the port number to connect on (usually 5432; note that this is an integer - not a string);
<<DATABASE>> the name of the relevant database on your server;
<<USERNAME>> is the username you want to connect with;
<<PASSWORD>> is the password for this username (this is an optional argument);
<<SSL>>> is one of the symbols 'yes, 'no (recommended for localhost connections) or 'optional (the default, allowing PostgreSQL to make the decision). Note that using SSL imposes a significant overhead on query time.
Note: the PostgreSQL version of make-database has additional keyword arguments to configure SSL if you need it.
Now you are ready to connect to a database and start storing data. Create a new module called "test.ss" in the same directory as "db.ss". Edit this new module and enter the following test script:
; Define a datatype that we can save to the database:
; Print the people that are saved in the database:
( people ( ( (select #:from person))))
( "People in the database:~n")
( ( people)
( " NONE~n")
( ([person people])
( " ~s~n" person))))
; Create some people (in memory):
( (alice bob charlie)
( (make-person "Alice" 20 'F)
(make-person "Bob" 25 'M)
(make-person "Charlie" 30 'F)))
; Connect to the database:
; Create a database table to house person data:
; There aren't any people saved yet:
; Now they're in the database:
; And now they're gone again:
; Okay, we're done. Drop the table:
Run the code with the following command line:
You should see the following output:
People in the database:
People in the database:
#(struct:person "Alice" 20 'F)
#(struct:person "Bob" 25 'M)
#(struct:person "Charlie" 30 'F)
People in the database:
Several bits of code Snooze functionality are demonstrated in this example. Click on the names below to see the definitions of the relevant procedures:
is a macro that defines a structure type that can be saved to the database: the type specifiers give Snooze hints about how to serialize and deserialize field values;
sets up a default connection that is used by many Snooze API calls: the connection is established and terminated as control enters and leaves the argument thunk via continuation jumps or the natural flow of the program;
and q:select are part of the Snooze query mechanism (which has most of the expressiveness of full SQL);
and are used to issue the corresponding CREATE TABLE and DROP TBALE commands to the database;
is used to save a persistent struct to the database and is used to delete it again.
That completes this quick start guide. In "db.ss" you now have everything you need to start defining your own persistent struct types and saving structs to your database. The remainder of this manual serves a reference for the various parts of Snooze.