Lisp HUG Maillist Archive

Common SQL question

Assume the following example


(def-view-class 
 t1 ()
 ((id :type integer)
  (foreign-id :type integer
              :column foreign_id)
  (name :db-kind :join
        :db-info (:join-class t2
                  :retrieval :deferred
                  :target-slot name
                  :set nil
                  :home-key foreign-id
                  :foreign-key id)
        :accessor category)))

(def-view-class 
 t2 ()
 ((id :type integer
      :db-kind :key)
  (name :type (string 50))))
        
                
(let (db-obj)
  (defun full-example ()
    (db:connect) ;; use what you need to connect to you database
    (drop-view-from-class 't1)
    (drop-view-from-class 't2)
    (create-view-from-class 't1)
    (create-view-from-class 't2)
    (enable-sql-reader-syntax)
    (insert-records :into [t1]
                    :values '(1 1))
    (insert-records :into [t2]
                    :values '(1  "eins"))
    
    (setf db-obj (car (car (select 't1
                                   :where [= [slot-value 't1 'id] 1]))))
    (print (category db-obj))
    (print (car (category db-obj)))))


Output:
 (full-example)

("eins" #<db-instance T2 543546020>) 
"eins" 
"eins"




How could it be that category yields a list?

If there have to be more than one return value why aren't multiple
values returned;

Why do I have to use car on that slot which is definitly not supposed
to be a list?

second question:
How is the correct way to achieve this

(defconstant +some-const+ 1)

(select :from [t1]
        :where [= [id] +some-const+])

this yields 

Sql-Database-Data-Error id 42000[1064] :
[unixODBC][MySQL][ODBC 3.51
Driver][mysqld-3.23.55]You have an error in your SQL
syntax near 'FROM T1 WHERE (ID = +SOME-CONST+)'
at line 1

Now I can solve it this way 


 (select [*] :from [t1] 
        :where [= [id] (sql::lisp-to-sql-formatt +some-const+ 'integer)])

which is questionable at best or:


(select [*] :from [t1] 
        :where [= [id] #.+some-const+])

which seems to be better but not really the way it is intended to be
used.

Regards
Friedrich


        





Re: Common SQL question

Hello Friedrich,

I can only comment the second example.

| How is the correct way to achieve this
| 
| (defconstant +some-const+ 1)
| 
| (select :from [t1]
|         :where [= [id] +some-const+])
|...snip...| 

AFAIK the documented way is 

 (select :from [t1]
         :where (sql-operation '= [id] +some-const+))
--
Sincerely,
Dmitri Ivanov
www.aha.ru/~divanov


Updated at: 2020-12-10 09:00 UTC