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