sxql-composer

2020-03-25

Build and compose SXQL queries dynamically

Upstream URL

github.com/mmontone/sxql-composer

Author

Mariano Montone <marianomontone@gmail.com>

License

MIT
README

SXQL-COMPOSER

Quicklisp MIT License

Build and compose SXQL queries dynamically.

Motivation

It is not possible to dynamically build and compose SXQL queries because it uses macros for query building. For example, something like:

(defun build-query (fields &key order-by)
    (select fields
      (where (:= 'user.id 1))
      (when order-by
        (order-by order-by))))

is not possible.

SXQL-COMPOSER implements an equivalent set of functions in order to be able to either build new queries dynamically, or modify and composer already existent queries.

With SXQL-COMPOSER:

(defun build-query (fields &key order-by)
   (let ((q (select fields
               (where (:= 'user.id 1)))))
      (when order-by (order-by= q order-by))
      q))

Also, it is not possible to modify already existent queries. That is useful for filtering:

(defun all-users ()
   (select :* (from :users)))

(defun married-users ()
   (and-where (all-users)
       '(:= status "married")))

CL-ARROWS library is potentially useful for building the queries:

(defun married-users ()
   (-> (all-users)
      (and-where '(:= status "married"))))

Functions

and-where

(statement expression)

Add an AND clause to the WHERE clause of SQL STATEMENT

fields+

(statement &rest fields)

Add FIELDS to the SELECT SQL STATEMENT

fields=

(statement fields)

Set the FIELDS of the SELECT SQL STATEMENT

from+

(statement &rest tables)

Add TABLES to the FROM clause of the SQL STATEMENT

from=

(statement &rest tables)

Set the FROM clause of the SQL STATEMENT

group-by+

(statement &rest expressions)

Add expressions to the GROUP BY clause of the SQL STATEMENT

group-by=

(statement &rest expressions)

Set the GROUP BY clause of the SQL statement

limit=

(statement count1 &optional count2)

Set the LIMIT clause of the SQL STATEMENT

offset=

(statement offset)

Set the OFFSET clause of the SQL STATEMENT

or-where

(statement expression)

Add an OR clause to the WHERE clause of SQL STATEMENT

order-by+

(statement &rest expressions)

Add EXPRESSIONS ot the ORDER BY clause of the SQL STATEMENT

order-by=

(statement &rest expressions)

Set the ORDER BY clause of the SQL STATEMENT

returning=

(statement expression)

Set the RETURNING clause of SQL STATEMENT

where=

(statement expression)

Set SQL WHERE clause of the SQL STATEMENT

Dependencies (1)

  • sxql

Dependents (0)

    • GitHub
    • Quicklisp