2

假设我有以下两个表

(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)

现在我想要在 class-b 中没有对应条目的 class-a 条目。(“对应”表示 class-a.field-one 映射到 class-bb-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)))))

这是一个错误还是这个翻译背后有一些原因?

4

1 回答 1

1

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

(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

于 2019-10-05T15:23:46.747 回答