r9531: * db-postgresql/postgresl-sql.lisp: Avoid computing
[clsql.git] / examples / clsql-tutorial.lisp
1 (asdf:operate 'asdf:load-op 'clsql)
2
3 (in-package #:clsql-user)
4
5 ;; You must set these variables to appropriate values. 
6 (defvar *tutorial-database-type* nil 
7   "Possible values are :postgresql,:postgresql-socket :mysql or :sqlite")
8 (defvar *tutorial-database-name* ""
9   "The name of the database we will work in.")
10 (defvar *tutorial-database-user* "" 
11   "The name of the database user we will work as.")
12 (defvar *tutorial-database-server* ""
13   "The name of the database server if required")
14 (defvar *tutorial-database-password* "" 
15   "The password if required")
16
17 (clsql:def-view-class employee ()
18   ((emplid
19     :db-kind :key
20     :db-constraints :not-null
21     :nulls-ok nil
22     :type integer
23     :initarg :emplid)
24    (first-name
25     :accessor first-name
26     :type (string 30)
27     :initarg :first-name)
28    (last-name
29     :accessor last-name
30     :type (string 30)
31     :initarg :last-name)
32    (email
33     :accessor employee-email
34     :type (string 100)
35     :nulls-ok t
36     :initarg :email)
37    (companyid
38     :type integer)
39    (company
40     :accessor employee-company
41     :db-kind :join
42     :db-info (:join-class company
43                           :home-key companyid
44                           :foreign-key companyid
45                           :set nil))
46    (managerid
47     :type integer
48     :nulls-ok t
49     :initarg :managerid)
50    (manager
51     :accessor employee-manager
52     :db-kind :join
53     :db-info (:join-class employee
54                           :home-key managerid
55                           :foreign-key emplid
56                           :set nil)))
57   (:base-table employee))
58
59 (clsql:def-view-class company ()
60   ((companyid
61     :db-kind :key
62     :db-constraints :not-null
63     :type integer
64     :initarg :companyid)
65    (name
66     :type (string 100)
67     :initarg :name)
68    (presidentid
69     :type integer
70     :initarg :presidentid)
71    (president
72     :reader president
73     :db-kind :join
74     :db-info (:join-class employee
75                           :home-key presidentid
76                           :foreign-key emplid
77                           :set nil))
78    (employees
79     :reader company-employees
80     :db-kind :join
81     :db-info (:join-class employee
82                           :home-key companyid
83                           :foreign-key companyid
84                           :set t)))
85   (:base-table company))
86
87 ;; Connect to the database (see the CLSQL documentation for vendor
88 ;; specific connection specs).
89 (clsql:connect `(,*tutorial-database-server* 
90                ,*tutorial-database-name*
91                ,*tutorial-database-user* 
92                ,*tutorial-database-password*)
93              :database-type *tutorial-database-type*)
94
95 ;; Record the sql going out, helps us learn what is going
96 ;; on behind the scenes
97 (clsql:start-sql-recording)
98
99 ;; Create the tables for our view classes
100 ;; First we drop them, ignoring any errors
101 (ignore-errors
102  (clsql:drop-view-from-class 'employee)
103  (clsql:drop-view-from-class 'company))
104
105 (clsql:create-view-from-class 'employee)
106 (clsql:create-view-from-class 'company)
107
108
109 ;; Create some instances of our view classes
110 (defvar company1 (make-instance 'company
111                               :companyid 1
112                               :name "Widgets Inc."
113                               ;; Lenin is president of Widgets Inc.
114                               :presidentid 1))
115
116 (defvar employee1 (make-instance 'employee
117                                :emplid 1
118                                :first-name "Vladamir"
119                                :last-name "Lenin"
120                                :email "lenin@soviet.org"
121                                :companyid 1))
122
123 (defvar employee2 (make-instance 'employee
124                                :emplid 2
125                                :first-name "Josef"
126                                :last-name "Stalin"
127                                :email "stalin@soviet.org"
128                                :companyid 1
129                                ;; Lenin manages Stalin (for now)
130                                :managerid 1))
131
132 (clsql:update-records-from-instance employee1)
133 (clsql:update-records-from-instance employee2)
134 (clsql:update-records-from-instance company1)
135
136 ;; lets use the functional sql interface 
137 (clsql:locally-enable-sql-reader-syntax)
138
139 (format t "The email address of ~A ~A is ~A"
140         (first-name employee1)
141         (last-name employee1)
142         (employee-email employee1))
143
144 (setf (employee-email employee1) "lenin-nospam@soviets.org")
145
146 ;; Update the database
147 (clsql:update-records-from-instance employee1)
148
149 (let ((new-lenin (car
150                   (clsql:select 'employee
151                               :where [= [slot-value 'employee 'emplid] 1]))))
152   (format t "His new email is ~A"
153           (employee-email new-lenin)))
154
155
156 ;; Some queries
157
158 ;; all employees
159 (clsql:select 'employee)
160 ;; all companies
161 (clsql:select 'company)
162
163 ;; employees named Lenin
164 (clsql:select 'employee :where [= [slot-value 'employee 'last-name]
165                                 "Lenin"])
166
167 (clsql:select 'company :where [= [slot-value 'company 'name]
168                                "Widgets Inc."])
169
170 ;; Employees of Widget's Inc.
171 (clsql:select 'employee
172             :where [and [= [slot-value 'employee 'companyid]
173                            [slot-value 'company 'companyid]]
174                         [= [slot-value 'company 'name]
175                            "Widgets Inc."]])
176
177 ;; Same thing, except that we are using the employee
178 ;; relation in the company view class to do the join for us,
179 ;; saving us the work of writing out the SQL!
180 (company-employees company1)
181
182 ;; President of Widgets Inc.
183 (president company1)
184
185 ;; Manager of Josef Stalin
186 (employee-manager employee2)