123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- ;;;; database.lisp
- ;;;;
- ;;;; Copyright (c) 2016 Samuel W. Flint <swflint@flintfam.org>
- (in-package #:collect)
- ;;; "collect" goes here.
- (defun format-column (column-definition)
- (if (not (listp column-definition))
- (string-downcase (format nil "~a" column-definition))
- (if (eq 'as (first column-definition))
- (format nil "~a AS ~a"
- (format-column (second column-definition))
- (format-column (third column-definition)))
- (string-downcase (format nil "~a.~a"
- (first column-definition)
- (format-table-name (second column-definition)))))))
- (defun format-table-name (table-name)
- (string-downcase (format nil "~a" table-name)))
- (defun format-where-clause (where &aux (where-clause (if (listp where) where (list where))))
- (let* ((operator (first where-clause))
- (lhs (second where-clause))
- (rhs (third where-clause)))
- (case operator
- (<
- (format nil "(~a < ~a)" (format-where-clause lhs) (format-where-clause rhs)))
- (>
- (format nil "(~a > ~a)" (format-where-clause lhs) (format-where-clause rhs)))
- (=
- (format nil "(~a = ~a)" (format-where-clause lhs) (format-where-clause rhs)))
- (or
- (format nil "(~a OR ~a)" (format-where-clause lhs) (format-where-clause rhs)))
- (and
- (format nil "(~a AND ~a)" (format-where-clause lhs) (format-where-clause rhs)))
- (t
- (if (numberp operator)
- operator
- (format-table-name where))))))
- (defun query-maker (query)
- (destructuring-bind (nil columns &key from where group order limit) query
- ;; (list columns from where group order limit)
- (let ((column-format (map 'list #'format-column columns))
- (tables (map 'list #'format-table-name from))
- (where (format-where-clause where)))
- (format nil "SELECT ~{~a~^, ~} FROM ~{~a~^, ~}~@[ WHERE ~a~]~@[ GROUP BY ~a~]~@[ ORDER BY ~a~]~@[ LIMIT ~a~];"
- column-format
- tables
- where
- group
- order
- limit))))
- (defun open-database ())
- (defun get-all-forms ())
- (defun get-all-queries ())
- ;;; End collect
|