File: sql.lisp

package info (click to toggle)
cl-hyperobject 2.11.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 300 kB
  • ctags: 224
  • sloc: lisp: 1,820; xml: 215; makefile: 161; sh: 28
file content (222 lines) | stat: -rw-r--r-- 5,901 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:          sql.lisp
;;;; Purpose:       SQL Generation functions for Hyperobject
;;;; Programmer:    Kevin M. Rosenberg
;;;; Date Started:  Apr 2000
;;;;
;;;; $Id: sql.lisp 7061 2003-09-07 06:34:45Z kevin $
;;;;
;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
;;;; *************************************************************************

(in-package #:hyperobject)

;;;; Metaclass initialization commands

(defun finalize-sql (cl)
  (setf (slot-value cl 'drop-table-cmd) (generate-drop-table-cmd
					 (slot-value cl 'sql-name)))
  (let ((esds (class-slots cl)))
    (setf (slot-value cl 'create-table-cmd)
      (generate-create-table-cmd 
       cl 
       (remove-if #'(lambda (esd) (null (esd-stored esd))) esds)))
    (setf (slot-value cl 'create-indices-cmds)
      (generate-create-indices-cmds (sql-name cl) esds))
    (dolist (esd esds)
      (when (slot-value esd 'inverse)
	(define-inverse cl esd))))
  )

			
(defun define-inverse (class esd)
  "Define an inverse function for a slot"
  (let ((inverse (slot-value esd 'inverse)))
    (when inverse
      (eval
       `(defun ,inverse (obj)
	  (format t "~&Finding key: ~S for class ~S ~%" obj ,class)
	  ;; create inverse function
	  ))
      ))
  )

(defun generate-create-table-cmd (cl esds)
  (with-output-to-string (s)
    (format s "CREATE TABLE ~A (~{~A~^, ~})" 
	    (slot-value cl 'sql-name)
	    (loop for esd in esds
		collect
		  (concatenate 
		      'string
		    (slot-value esd 'sql-name)  
		    " "
		    (sql-type-to-field-string (slot-value esd 'sql-type)
					      (slot-value esd 'sql-length)))))))

(defun sql-type-to-field-string (type length)
  (ecase type
    (:string
     (cond
      ((null length)
       "LONGTEXT")
      ((< length 8)
       (format nil "CHAR(~d)" length))
      (t
       (format nil "VARCHAR(~d)" length))))
    (:varchar
     (cond
      ((null length)
       "LONGTEXT")
      (t
       (format nil "VARCHAR(~d)" length))))
    (:text
     "LONGTEXT")
    (:datetime
     "VARCHAR(20)")
    (:char
     (unless length
       (setq length 1))
     (format nil "CHAR(~D)" length))
    ((or :fixnum :integer)
     "INTEGER")
    (:boolean
     "CHAR(1)")
    (:long-integer
     "BIGINT")
    ((or :short-float :float)
     "SINGLE")
    (:long-float
     "DOUBLE")))

(defun generate-drop-table-cmd (table-name)
  (format nil "DROP TABLE ~a" table-name))

(defun generate-create-indices-cmds (table-name slots)
  (let (indices)
    (dolist (slot slots)
      (when (slot-value slot 'indexed)
	(let ((sql-name (slot-value slot 'sql-name)))
	  (push (sql-cmd-index table-name sql-name (slot-value slot 'unique))
		indices))))
    indices))

(defun sql-cmd-index (table field unique)
  (let ((*print-circle* nil))
    (format nil "CREATE ~AINDEX ~A ON ~A(~A)"
	    (if unique "UNIQUE " "")
	    (sql-index-name table field)
	    table
	    field)))

(defun sql-index-name (table field)
  (format nil "~A_~A" table field))

;;;; Runtime Commands

(defgeneric sql-create (cl))
(defmethod sql-create (cl)
 (with-sql-connection (conn)
    (sql-execute (slot-value cl 'create-table-cmd) conn)
    (dolist (cmd (slot-value cl 'create-indices-cmds))
      (sql-execute cmd conn))
    (values)))

(defgeneric sql-drop (cl))
(defmethod sql-drop (cl)
  (mutex-sql-execute (slot-value cl 'drop-table-cmd))
  (values))

#|
(defmethod sql-insert (obj)
  (mutex-sql-execute
   (format nil "INSERT INTO ~a (~a) VALUES (~a)"
	   (sql-name self) (sql-cmd-field-names self) (format-values self))))

(defmethod sql-select (obj lisp-name key)
  (let ((tuple 
	 (car 
	  (mutex-sql-query
	   (format nil "SELECT ~a FROM ~a WHERE ~a=~a"
		   (sql-cmd-field-names self) (sql-name self)
		   (inverse-field-name self) key)))))
    (when tuple
      (format t "process returned fields"))))


(defun format-values (self)
  (let ((values "")
	(fields (fields self)))
    (dolist (field fields)
      (unless (eq field (car fields))
	(string-append values ","))
      (let ((name (car field)))
	(with-key-value-list (key value (rest field))
	  (when (eq key :type)
	    (string-append values
			      (ecase value
				((:fixnum :bigint :short-float :double-float)
				 (write-to-string 
				  (slot-value self name)))
				((:string :text)
				 (format nil "'~a'" 
					 (add-sql-quotes 
					  (slot-value self name))))))))))
    values))

(defun inverse-field-string (fields)
  (let (inverse)
    (dolist (field fields)
      (let ((name-string (write-to-string (car field))))
	(with-key-value-list (key value (rest field))
	  (when (eq key :inverse)
	    (setq inverse value)))))
    (when inverse
      (write-to-string inverse))))

(defun row-field-string (fields)
  (let ((names ""))
    (dolist (field fields)
      (unless (eq field (car fields))
	(string-append names ","))
      (string-append names (lisp-name-to-sql-name (car field))))
    names))

      
(defun parse-fields (table-name fields)
  (let (class-fields)
    (dolist (field fields)
      (let* ((fname (car field))
	     (name-string (write-to-string fname))
	     (initarg (intern name-string :keyword))concat-symbol
	     (def (list fname))
	     (options (rest field)))
	(with-key-value-list (key value options)
	  (case key
	    (:type
	     (setq def (nconc def (list :type 
					(ecase value
					  (:string
					   'string)
					  (:fixnum
					   'fixnum)
					  (:long-integer
					   'integer)
					  (:short-float
					   'short-float)
					  (:long
					   'long-float)
					  (:text
					   'string))))))))
	(setq def (nconc def (list 
			      :initarg initarg
			      :accessor (concat-symbol 
					 (write-to-string table-name) "-"
					 (write-to-string fname)))))
	(push def class-fields)))
    class-fields))

||#