Skip to content

Commit

Permalink
Merge pull request #25 from jackcarrozzo/master
Browse files Browse the repository at this point in the history
Add 'returning' clause
  • Loading branch information
fukamachi committed Oct 12, 2015
2 parents 734b071 + c994dff commit 5b9f740
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ Creates a SELECT query. It takes a field (or a list of fields) and SQL Clauses.
:name "Eitaro Fukamachi"))
;=> #<SXQL-STATEMENT: INSERT INTO person SET sex = 'male', age = 25, name = 'Eitaro Fukamachi'>
(insert-into :users
(set= :name "Jack"
:jinbei-size "small")
(returning :id))
;=> #<SXQL-STATEMENT: INSERT INTO `users` (`name`, `jinbei-size`) VALUES ('Jack', 'small') RETURNING `id`>
(insert-into :person
(:id :name)
(select (:id :name)
Expand Down Expand Up @@ -242,6 +248,20 @@ Creates a SELECT query. It takes a field (or a list of fields) and SQL Clauses.
;=> #<SXQL-CLAUSE: GROUP BY sex>
```

### having

```common-lisp
(having (:>= (:sum :hoge) 88))
;=> #<SXQL-CLAUSE: HAVING (SUM(`hoge`) >= 88)>
```

### returning

```common-lisp
(returning :id)
;=> #<SXQL-CLAUSE: RETURNING `id`>
```

### limit

```common-lisp
Expand Down
4 changes: 4 additions & 0 deletions src/clause.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
(defstruct (having-clause (:include expression-clause (name "HAVING"))
(:constructor make-having-clause (expression))))

@export
(defstruct (returning-clause (:include expression-clause (name "RETURNING"))
(:constructor make-returning-clause (expression))))

@export
(defstruct (join-clause (:include statement-clause)
(:constructor make-join-clause))
Expand Down
5 changes: 5 additions & 0 deletions src/statement.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
:where-clause
:group-by-clause
:having-clause
:returning-clause
:order-by-clause
:limit-clause
:offset-clause)
Expand Down Expand Up @@ -49,6 +50,7 @@
where-clause
group-by-clause
having-clause
returning-clause
order-by-clause
limit-clause
offset-clause))
Expand Down Expand Up @@ -76,6 +78,7 @@
where-clause
group-by-clause
having-clause
returning-clause
order-by-clause
limit-clause
offset-clause
Expand All @@ -95,6 +98,7 @@
(where-clause nil)
(group-by-clause nil)
(having-clause nil)
(returning-clause nil)
(order-by-clause nil)
(limit-clause nil)
(offset-clause nil))
Expand All @@ -109,6 +113,7 @@
where-clause
group-by-clause
having-clause
returning-clause
order-by-clause
limit-clause
offset-clause))
Expand Down
8 changes: 8 additions & 0 deletions src/sxql.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@
(expand-op expression)
`,expression)))

@export
(defmacro returning (expression)
`(make-clause :returning
,(if (and (listp expression)
(keywordp (car expression)))
(expand-op expression)
`,expression)))

@export
(defun limit (count1 &optional count2)
(apply #'make-clause :limit `(,count1 ,@(and count2 (list count2)))))
Expand Down
5 changes: 5 additions & 0 deletions t/clause.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
(make-op :>= (make-sql-symbol "hoge") (make-sql-variable 88)))))
(list "HAVING (`hoge` >= ?)" '(88)))

(is (multiple-value-list
(yield
(make-clause :returning (make-sql-symbol "id"))))
(list "RETURNING `id`" nil))

(ok (make-clause :limit (make-sql-variable 1)) "LIMIT")
(ok (make-clause :limit (make-sql-variable 0) (make-sql-variable 10)))
(is (multiple-value-list
Expand Down

0 comments on commit 5b9f740

Please sign in to comment.