database.lisp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ;;;; database.lisp
  2. ;;;;
  3. ;;;; Copyright (c) 2016 Samuel W. Flint <swflint@flintfam.org>
  4. (in-package #:collect)
  5. ;;; "collect" goes here.
  6. (defun format-column (column-definition)
  7. (if (not (listp column-definition))
  8. (string-downcase (format nil "~a" column-definition))
  9. (if (eq 'as (first column-definition))
  10. (format nil "~a AS ~a"
  11. (format-column (second column-definition))
  12. (format-column (third column-definition)))
  13. (string-downcase (format nil "~a.~a"
  14. (first column-definition)
  15. (format-table-name (second column-definition)))))))
  16. (defun format-table-name (table-name)
  17. (string-downcase (format nil "~a" table-name)))
  18. (defun format-where-clause (where &aux (where-clause (if (listp where) where (list where))))
  19. (let* ((operator (first where-clause))
  20. (lhs (second where-clause))
  21. (rhs (third where-clause)))
  22. (case operator
  23. (<
  24. (format nil "(~a < ~a)" (format-where-clause lhs) (format-where-clause rhs)))
  25. (>
  26. (format nil "(~a > ~a)" (format-where-clause lhs) (format-where-clause rhs)))
  27. (=
  28. (format nil "(~a = ~a)" (format-where-clause lhs) (format-where-clause rhs)))
  29. (or
  30. (format nil "(~a OR ~a)" (format-where-clause lhs) (format-where-clause rhs)))
  31. (and
  32. (format nil "(~a AND ~a)" (format-where-clause lhs) (format-where-clause rhs)))
  33. (t
  34. (if (numberp operator)
  35. operator
  36. (format-table-name where))))))
  37. (defun query-maker (query)
  38. (destructuring-bind (nil columns &key from where group order limit) query
  39. ;; (list columns from where group order limit)
  40. (let ((column-format (map 'list #'format-column columns))
  41. (tables (map 'list #'format-table-name from))
  42. (where (format-where-clause where)))
  43. (format nil "SELECT ~{~a~^, ~} FROM ~{~a~^, ~}~@[ WHERE ~a~]~@[ GROUP BY ~a~]~@[ ORDER BY ~a~]~@[ LIMIT ~a~];"
  44. column-format
  45. tables
  46. where
  47. group
  48. order
  49. limit))))
  50. (defun open-database ())
  51. (defun get-all-forms ())
  52. (defun get-all-queries ())
  53. ;;; End collect