2

Suppose I have the following two tables

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

Now I want entries of class-a that do not have a corresponding entry in class-b. ("Corresponding" meaning that class-a.field-one maps to class-b.b-one.) Suppose that, class-a has the following two entries:

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

So, the query to issue would be:

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

However, this adds class-b as well. (With a purposeful error, I get the query executed as:)

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

On the other hand,

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

is translated to the expected:

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

Is this a bug or is there some reasoning behind this translation?

digikar
  • 576
  • 5
  • 13

1 Answers1

1

As of version 6.7.0, redefining the function clsql-sys::%tables-for-query makes it work "as expected", without breaking anything in the webapp I am working on:

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

But I haven't run the test-cases clsql-tests.

digikar
  • 576
  • 5
  • 13