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