clsql:为什么要“神奇地”添加表?

时间:2019-10-05 08:10:45

标签: common-lisp clsql

假设我有以下两个表

(clsql:def-view-class class-a ()
  ((field-one :type integer
              :db-kind :key
              :db-constraints :auto-increment)
   (field-two :type string
              :initarg :two
              :db-constraints :not-null)))

(clsql:def-view-class class-b ()
  ((b-one :type integer
          :initarg :b-one
          :db-kind :key)))

(clsql:create-view-from-class 'class-a)
(clsql:create-view-from-class 'class-b)

现在,我想要类别a的条目,而在类别b中没有相应的条目。 (“对应”表示class-a.field-one映射到class-b.b-one。)假设class-a具有以下两个条目:

(clsql:update-records-from-instance (make-instance 'class-a :two "hello"))
(clsql:update-records-from-instance (make-instance 'class-a :two "world"))

因此,要发出的查询将是:

(clsql:select 'class-a :from [class-a] ; class-b is added "magically"
              :where [not [in [class-a field-one]
                              [select [class-b b-one] :from [class-b]
                                      :where [= [class-a field-one]
                                                [class-b b-one]]]]])

但是,这也增加了b类。 (由于故意的错误,我将查询执行为:)

SELECT CLASS_A.FIELD_ONE,CLASS_A.FIELD_TWO FROM CLASS_A,CLASS_B 
  WHERE (NOT ((CLASS_A.FIELD_ONE 
                IN (SELECT CLASS_B.B_ONE FROM CLASS_B 
                      WHERE (CLASS_A.FIELD_ONE = CLASS_B.B_ONE)))))

另一方面

(clsql:select [*] :from [class-a] ; see the [*]
              :where [not [in [class-a field-one]
                              [select [class-b b-one] :from [class-b]
                                      :where [= [class-a field-one]
                                                [class-b b-one]]]]])

已翻译为预期的版本:

SELECT * FROM CLASS_A 
  WHERE (NOT ((CLASS_A.FIELD_ONE 
                IN (SELECT CLASS_B.B_ONE FROM CLASS_B 
                      WHERE (CLASS_A.FIELD_ONE = CLASS_B.B_ONE)))))

这是错误还是翻译背后的原因?

1 个答案:

答案 0 :(得分:1)

从6.7.0版开始,重新定义功能clsql-sys::%tables-for-query使其“按预期”工作,而不会破坏我正在处理的Web应用程序中的任何内容:

(in-package :clsql-sys)

(defun %tables-for-query (classes from where inner-joins)
  (declare (ignore where inner-joins))
  (union (mapcar #'select-table-sql-expr classes)
         (listify from)))

但是我还没有运行测试用例clsql-tests