Skip to content

Commit

Permalink
Merge pull request #289 from sabracrolleton/master
Browse files Browse the repository at this point in the history
Adding s-sql boolean tests :is-true, :is-false
  • Loading branch information
sabracrolleton authored Oct 27, 2021
2 parents 626d3f0 + b7abea0 commit 9e5b10c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
31 changes: 31 additions & 0 deletions doc/s-sql.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions doc/s-sql.org
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,26 @@ applied to a subquery.
(:= 'countries.name "Costa Rica")
(:= 'regions.id 'countries.region-id))))))
#+END_SRC
** sql-op :is-false (arg)
:PROPERTIES:
:CUSTOM_ID: sql-op-is-false
:END:

Test whether a boolean value is false.
#+BEGIN_SRC lisp
(query (:select 'ta :from 'a :where (:is-false 'ta)))
(select-dao 'account (:is-false 'active))
#+END_SRC
** sql-op :is-true (arg)
:PROPERTIES:
:CUSTOM_ID: sql-op-is-true
:END:

Test whether a boolean value is true.
#+BEGIN_SRC lisp
(query (:select 'ta :from 'a :where (:is-true 'ta)))
(select-dao 'account (:is-true 'active))
#+END_SRC
** sql-op :is-null (arg)
:PROPERTIES:
:CUSTOM_ID: sql-op-is-null
Expand Down
13 changes: 12 additions & 1 deletion postmodern/tests/test-dao.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@
(is (eq (test-b new-database-dao) t))
(is (eq (test-b database-dao) nil))
(delete-dao dao)))
(is (not (select-dao 'test-data)))))))
(is (not (select-dao 'test-data)))
(insert-dao (make-instance 'test-data :id 4 :a "boolean test dao" :b t))))))

(test dao-class-1
(with-test-connection
Expand All @@ -211,6 +212,16 @@
(is (equal (test-d new-dao)
1))))))

(test dao-class-2
(with-test-connection
(with-dao-test-table-fixture
(insert-dao (make-instance 'test-data :id 4 :a "boolean test dao 1" :b t))
(insert-dao (make-instance 'test-data :id 5 :a "boolean test dao 2" :b nil))
(is (equal (test-a (first (select-dao 'test-data (:is-true 'b))))
"boolean test dao 1"))
(is (equal (test-a (first (select-dao 'test-data (:is-false 'b))))
"boolean test dao 2")))))

(defclass test-data-nil ()
((id :col-type serial :initarg :id :accessor test-id)
(a :col-type (or text db-null) :initarg :a :accessor test-a)
Expand Down
6 changes: 6 additions & 0 deletions s-sql/s-sql.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,12 @@ e.g. (make-interval (\"days\" 10)(\"hours\" 4))."
(def-sql-op :exists (query)
`("(EXISTS " ,@(sql-expand query) ")"))

(def-sql-op :is-true (arg)
`("(" ,@(sql-expand arg) " IS TRUE)"))

(def-sql-op :is-false (arg)
`("(" ,@(sql-expand arg) " IS FALSE)"))

(def-sql-op :is-null (arg)
`("(" ,@(sql-expand arg) " IS NULL)"))

Expand Down
31 changes: 31 additions & 0 deletions s-sql/tests/tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2662,3 +2662,34 @@ that the table will need to be scanned twice. Everything is a trade-off."
:from (:as (:values (:set 0) (:set 1) (:set 2))
(:t 'x))))
'((0 0) (1 0) (1 1) (2 0) (2 1) (2 2))))))

(defun build-boolean-table ()
(pomo:drop-table 'boolean-test :if-exists t)
(query "create table boolean_test (id integer, a boolean, b text)")
(query "insert into boolean_test values (1, true, 'I am true')")
(query "insert into boolean_test values (2, false, 'I am false')")
(query "insert into boolean_test values (3, null, 'I am NULL')"))

(test is-true
(is (equal (sql (:select '* :from 'table1 :where (:is-true 'col)))
"(SELECT * FROM table1 WHERE (col IS TRUE))"))
(with-test-connection
(build-boolean-table)
(is (equal (query (:select '* :from 'boolean-test :where (:is-true 'a)))
'((1 T "I am true"))))))

(test is-false
(is (equal (sql (:select '* :from 'table1 :where (:is-false 'col)))
"(SELECT * FROM table1 WHERE (col IS FALSE))"))
(with-test-connection
(build-boolean-table)
(is (equal (query (:select '* :from 'boolean-test :where (:is-false 'a)))
'((2 NIL "I am false"))))))

(test is-null
(is (equal (sql (:select '* :from 'table1 :where (:is-false 'col)))
"(SELECT * FROM table1 WHERE (col IS FALSE))"))
(with-test-connection
(build-boolean-table)
(is (equal (query (:select '* :from 'boolean-test :where (:is-null 'a)))
'((3 :NULL "I am NULL"))))))

0 comments on commit 9e5b10c

Please sign in to comment.