r10742: 17 Sep 2005 Kevin Rosenberg <kevin@rosenberg.net>
[clsql.git] / examples / clsql-tutorial.lisp
index 2afb0f8e020312178a86c223b2782ddd47a0cff6..265ae6d2e34edb1930ac537b1a4c77d29f2b3aca 100644 (file)
@@ -4,8 +4,9 @@
 
 ;; You must set these variables to appropriate values. 
 (defvar *tutorial-database-type* nil 
-  "Possible values are :postgresql,:postgresql-socket :mysql or :sqlite")
-(defvar *tutorial-database-name* ""
+  "Possible values are :postgresql :postgresql-socket, :mysql,
+:oracle, :odbc, :aodbc or :sqlite")
+(defvar *tutorial-database-name* "clsqltut"
   "The name of the database we will work in.")
 (defvar *tutorial-database-user* "" 
   "The name of the database user we will work as.")
@@ -18,7 +19,6 @@
   ((emplid
     :db-kind :key
     :db-constraints :not-null
-    :nulls-ok nil
     :type integer
     :initarg :emplid)
    (first-name
    (email
     :accessor employee-email
     :type (string 100)
-    :nulls-ok t
     :initarg :email)
    (companyid
-    :type integer)
+    :type integer
+    :initarg :companyid)
    (company
     :accessor employee-company
     :db-kind :join
@@ -45,7 +45,7 @@
                          :set nil))
    (managerid
     :type integer
-    :nulls-ok t)
+    :initarg :managerid)
    (manager
     :accessor employee-manager
     :db-kind :join
@@ -57,7 +57,7 @@
 
 (clsql:def-view-class company ()
   ((companyid
-    :db-type :key
+    :db-kind :key
     :db-constraints :not-null
     :type integer
     :initarg :companyid)
@@ -65,7 +65,8 @@
     :type (string 100)
     :initarg :name)
    (presidentid
-    :type integer)
+    :type integer
+    :initarg :presidentid)
    (president
     :reader president
     :db-kind :join
 
 ;; Connect to the database (see the CLSQL documentation for vendor
 ;; specific connection specs).
-(clsql:connect `(,*tutorial-database-server* 
-              ,*tutorial-database-name*
-              ,*tutorial-database-user* 
-              ,*tutorial-database-password*)
-            :database-type *tutorial-database-type*)
+(case *tutorial-database-type*
+  ((:mysql :postgresql :postgresql-socket)
+   (clsql:connect `(,*tutorial-database-server* 
+                   ,*tutorial-database-name*
+                   ,*tutorial-database-user* 
+                   ,*tutorial-database-password*)
+                 :database-type *tutorial-database-type*))
+  ((:odbc :aodbc :oracle)
+   (clsql:connect `(,*tutorial-database-name* 
+                   ,*tutorial-database-user* 
+                   ,*tutorial-database-password*)
+                 :database-type *tutorial-database-type*))
+  (:sqlite
+   (clsql:connect `(,*tutorial-database-name*)
+                 :database-type *tutorial-database-type*)))
 
 ;; Record the sql going out, helps us learn what is going
 ;; on behind the scenes
 
 
 ;; Create some instances of our view classes
+(defvar company1 (make-instance 'company
+                             :companyid 1
+                             :name "Widgets Inc."
+                             ;; Lenin is president of Widgets Inc.
+                             :presidentid 1))
+
 (defvar employee1 (make-instance 'employee
                               :emplid 1
                               :first-name "Vladamir"
                               :last-name "Lenin"
-                              :email "lenin@soviet.org"))
-
-(defvar company1 (make-instance 'company
-                             :companyid 1
-                             :name "Widgets Inc."))
-                             
+                              :email "lenin@soviet.org"
+                              :companyid 1))
 
 (defvar employee2 (make-instance 'employee
                               :emplid 2
                               :first-name "Josef"
                               :last-name "Stalin"
-                              :email "stalin@soviet.org"))
-
-;; Lenin manages Stalin (for now)
-(clsql:add-to-relation employee2 'manager employee1)
-
-;; Lenin and Stalin both work for Widgets Inc.
-(clsql:add-to-relation company1 'employees employee1)
-(clsql:add-to-relation company1 'employees employee2)
-
-;; Lenin is president of Widgets Inc.
-(clsql:add-to-relation company1 'president employee1)
+                              :email "stalin@soviet.org"
+                              :companyid 1
+                              ;; Lenin manages Stalin (for now)
+                              :managerid 1))
 
 (clsql:update-records-from-instance employee1)
 (clsql:update-records-from-instance employee2)
 (clsql:update-records-from-instance company1)
 
-;; lets us use the functional
-;; sql interface 
+;; lets use the functional sql interface 
 (clsql:locally-enable-sql-reader-syntax)
 
-
 (format t "The email address of ~A ~A is ~A"
        (first-name employee1)
        (last-name employee1)
 
 (let ((new-lenin (car
                  (clsql:select 'employee
-                             :where [= [slot-value 'employee 'emplid] 1]))))
+                             :where [= [slot-value 'employee 'emplid] 1]
+                             :flatp t))))
   (format t "His new email is ~A"
          (employee-email new-lenin)))