Permalink
Browse files

(fixes #30) Add :if-not-exists to create-index.

  • Loading branch information...
1 parent 2becf64 commit fa68755711bf5489e5450286d05976b6fd834d79 @fukamachi committed Apr 1, 2016
Showing with 19 additions and 8 deletions.
  1. +8 −5 src/statement.lisp
  2. +2 −2 src/sxql.lisp
  3. +9 −1 t/sxql.lisp
View
@@ -174,12 +174,13 @@
@export
(defstruct (create-index-statement (:include sql-statement (name "CREATE INDEX"))
- (:constructor make-create-index-statement (index-name table-name columns &key unique using)))
+ (:constructor make-create-index-statement (index-name table-name columns &key unique using if-not-exists)))
(index-name nil :type sql-symbol)
(table-name nil :type sql-symbol)
(columns nil :type sql-list)
(unique nil :type boolean)
- (using nil :type (or null sql-keyword)))
+ (using nil :type (or null sql-keyword))
+ (if-not-exists nil :type boolean))
@export
(defstruct (drop-index-statement (:include sql-statement (name "DROP INDEX"))
@@ -248,14 +249,15 @@
:if-exists if-exists)))
(defmethod make-statement ((statement-name (eql :create-index)) &rest args)
- (destructuring-bind (index-name &key unique using on) args
+ (destructuring-bind (index-name &key unique using on if-not-exists) args
(make-create-index-statement (detect-and-convert index-name)
(detect-and-convert (car on))
(apply #'make-sql-list
(mapcar #'detect-and-convert (cdr on)))
:unique unique
:using (and using
- (make-sql-keyword (string using))))))
+ (make-sql-keyword (string using)))
+ :if-not-exists if-not-exists)))
(defmethod make-statement ((statement-name (eql :drop-index)) &rest args)
(destructuring-bind (index-name &key if-exists on) args
@@ -291,9 +293,10 @@
(defmethod yield ((statement create-index-statement))
(values
- (format nil "CREATE~:[~; UNIQUE~] INDEX ~A~:[~; USING ~:*~A~] ON ~A ~A"
+ (format nil "CREATE~:[~; UNIQUE~] INDEX ~A~:[~; IF NOT EXISTS~]~:[~; USING ~:*~A~] ON ~A ~A"
(create-index-statement-unique statement)
(yield (create-index-statement-index-name statement))
+ (create-index-statement-if-not-exists statement)
(and (create-index-statement-using statement)
(yield (create-index-statement-using statement)))
(yield (create-index-statement-table-name statement))
View
@@ -132,8 +132,8 @@
(apply #'make-op :union-all queries))
@export
-(defun create-index (index-name &rest args &key unique using on)
- (declare (ignore unique using on))
+(defun create-index (index-name &rest args &key unique using on if-not-exists)
+ (declare (ignore unique using on if-not-exists))
(apply #'make-statement :create-index index-name
args))
View
@@ -9,7 +9,7 @@
:is-error))
(in-package :t.sxql)
-(plan 65)
+(plan 66)
(defmacro is-mv (test result &optional desc)
`(is (multiple-value-list (yield ,test))
@@ -283,6 +283,14 @@
'("CREATE UNIQUE INDEX `index_name` USING BTREE ON `table` (`column1`, `column2`)" nil)
"CREATE UNIQUE INDEX")
+(is-mv (create-index :index_name
+ :unique t
+ :using :btree
+ :on '(:table :column1 :column2)
+ :if-not-exists t)
+ '("CREATE UNIQUE INDEX `index_name` IF NOT EXISTS USING BTREE ON `table` (`column1`, `column2`)" nil)
+ "CREATE UNIQUE INDEX IF NOT EXISTS")
+
(is-mv (drop-index "index_name" :if-exists t)
'("DROP INDEX IF EXISTS `index_name`" nil)
"DROP INDEX")

0 comments on commit fa68755

Please sign in to comment.