Remove CVS $Id$ keyword
[clsql.git] / examples / sqlite3 / init-func / example.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     example.lisp
6 ;;;; Purpose:  Sample code for SQLite 3 initialization functions
7 ;;;; Authors:  Aurelio Bignoli
8 ;;;; Created:  Oct 2004
9 ;;;;
10 ;;;; This file, part of CLSQL, is Copyright (c) 2004 by Aurelio Bignoli
11 ;;;;
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
16
17 ;;;; Load CLSQL.
18 (asdf:oos 'asdf:load-op :clsql-sqlite3)
19
20 ;;;; Load sqlite3-utils.so library. See Makefile for library creation.
21 (unless (uffi:load-foreign-library "/usr/lib/clsql/sqlite3-utils.so"
22                                    :module "sqlite3-utils"
23                                    :supporting-libraries '("c"))
24   (error "Unable to load foreign library"))
25
26 ;;;; Define the foreign function to be used as init function.
27 (uffi:def-function
28     ("create_iso_8859_15_ci_collation" create-coll)
29     ((db sqlite3:sqlite3-db))
30   :returning :int
31   :module "sqlite3-utils")
32
33 ;;;; Create the DB using create-coll as init function.
34 (defparameter db-name "init-func-test.db")
35 (clsql:destroy-database (list db-name) :database-type :sqlite3)
36 (clsql:connect (list db-name #'create-coll) :database-type :sqlite3)
37
38 ;;;; Create a table. Field f2 uses the newly defined collating
39 ;;;; sequence.
40 (clsql:execute-command
41  "CREATE TABLE t1 (f1 CHAR(1), f2 CHAR(1) COLLATE ISO_8859_15_CI)")
42
43 ;;;; Populate the table.
44 (clsql:execute-command "INSERT INTO t1 VALUES ('à', 'à')")
45 (clsql:execute-command "INSERT INTO t1 VALUES ('a', 'a')")
46 (clsql:execute-command "INSERT INTO t1 VALUES ('A', 'A')")
47 (clsql:execute-command "INSERT INTO t1 VALUES ('é', 'é')")
48 (clsql:execute-command "INSERT INTO t1 VALUES ('e', 'e')")
49 (clsql:execute-command "INSERT INTO t1 VALUES ('E', 'E')")
50 (clsql:execute-command "INSERT INTO t1 VALUES ('ì', 'ì')")
51 (clsql:execute-command "INSERT INTO t1 VALUES ('i', 'i')")
52 (clsql:execute-command "INSERT INTO t1 VALUES ('I', 'I')")
53 (clsql:execute-command "INSERT INTO t1 VALUES ('ò', 'ò')")
54 (clsql:execute-command "INSERT INTO t1 VALUES ('o', 'o')")
55 (clsql:execute-command "INSERT INTO t1 VALUES ('O', 'O')")
56 (clsql:execute-command "INSERT INTO t1 VALUES ('ù', 'ù')")
57 (clsql:execute-command "INSERT INTO t1 VALUES ('u', 'u')")
58 (clsql:execute-command "INSERT INTO t1 VALUES ('U', 'U')")
59
60 ;;;; Perform some SELECTs.
61 (format t "~&SELECT * FROM t1 ==> ~A~%"(clsql:query "SELECT * FROM t1"))
62 (format t "~&SELECT * FROM t1 ORDER BY f1 ==> ~A~%"
63         (clsql:query "SELECT * FROM t1 ORDER BY f1"))
64 (format t "~&SELECT * FROM t1 ORDER BY f2 ==> ~A~%"
65         (clsql:query "SELECT * FROM t1 ORDER BY f2"))
66
67 ;;;; Disconnect from database.
68 (clsql:disconnect)