Permalink
Browse files

subquery

  • Loading branch information...
1 parent 8d8b2fe commit c9540a45aec398555bde46a693911d7229735873 @Rudolph-Miller Rudolph-Miller committed Feb 17, 2015
Showing with 38 additions and 4 deletions.
  1. +8 −3 src/sql-type.lisp
  2. +1 −1 sxql.asd
  3. +29 −0 t/operator.lisp
View
@@ -180,7 +180,8 @@
@export-constructors
(defstruct (infix-list-op (:include sql-op))
(left nil :type sql-expression)
- (right nil :type proper-list))
+ (right nil :type (or proper-list
+ sql-statement)))
@export 'expressions
@export
@@ -336,7 +337,9 @@
(format nil "(~A ~A ~A)"
(yield (infix-op-left op))
(sql-op-name op)
- (yield (infix-op-right op)))))
+ (if (sql-statement-p (infix-op-right op))
+ (format nil "(~A)" (yield (infix-op-right op)))
+ (yield (infix-op-right op))))))
(defmethod yield ((op infix-splicing-op))
(with-yield-binds
@@ -354,7 +357,9 @@
(format nil "(~A ~A ~A)"
(yield (infix-list-op-left op))
(sql-op-name op)
- (yield (apply #'make-sql-list (infix-list-op-right op))))))
+ (if (sql-statement-p (infix-list-op-right op))
+ (format nil "(~A)" (yield (infix-list-op-right op)))
+ (yield (apply #'make-sql-list (infix-list-op-right op)))))))
(defmethod yield ((op conjunctive-op))
(with-yield-binds
View
@@ -45,4 +45,4 @@
:fill-pointer t)))
(setf (fill-pointer seq) (read-sequence seq stream))
seq)))
- :in-order-to ((test-op (load-op sxql-test))))
+ :in-order-to ((test-op (test-op sxql-test))))
View
@@ -225,4 +225,33 @@
(yield (make-op :* op (make-op :- 10 2) op)))
'("((? + ? + ?) * (? - ?) * (? + ? + ?))" (1 2 3 10 2 1 2 3))))
+(diag "subquery")
+(is (multiple-value-list
+ (yield (make-op :=
+ :id
+ (select :id (from :users)))))
+ (list "(`id` = (SELECT `id` FROM `users`))" NIL)
+ "=")
+
+(is (multiple-value-list
+ (yield (make-op :!=
+ :id
+ (select :id (from :users)))))
+ (list "(`id` != (SELECT `id` FROM `users`))" NIL)
+ "=")
+
+(is (multiple-value-list
+ (yield (make-op :in
+ :id
+ (select :id (from :users)))))
+ (list "(`id` IN (SELECT `id` FROM `users`))" NIL)
+ "IN")
+
+(is (multiple-value-list
+ (yield (make-op :not-in
+ :id
+ (select :id (from :users)))))
+ (list "(`id` NOT IN (SELECT `id` FROM `users`))" NIL)
+ "NOT IN")
+
(finalize)

0 comments on commit c9540a4

Please sign in to comment.