Skip to content

Instantly share code, notes, and snippets.

@juan-reynoso
Last active June 22, 2022 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan-reynoso/f11915e617846e53f7d3a084c8e4fba3 to your computer and use it in GitHub Desktop.
Save juan-reynoso/f11915e617846e53f7d3a084c8e4fba3 to your computer and use it in GitHub Desktop.
Lisp s-sql with postmodern
(in-package #:pomo)
(defvar *db-spec* '("my_database" "my_user" "ultra-secret-password" "localhost" :POOLED-P T)
"Information about the connection of the database.")
(defmacro with-database-connection (&body query)
"The connection pooler.
This macro establish the connection with specified database in *db-parameter* variable and execute the QUERY.
IN: query that representing the users' query
OUT: result of the query"
`(postmodern:with-connection *db-spec* ,@query ))
;; It creates a table called students with id and name as columns
(with-database-connection
(query "create table students(id serial primary key, name text not null);"))
;;; Some notes
;;; s-expression = symbolic expression
;;; s-sql = symbolic expression Structured Query Language
;;; plain-sql = Structured Query Language as string
;;; Both insert functions execute the same action
(defun plain-sql-insert ()
(with-database-connection
(query "insert into students (name) values ('Juan Reynoso');")))
(defun s-sql-insert ()
(with-database-connection
(query (:insert-into 'students
:set
'name "Juan Reynoso"))))
;;; Both select functions execute the same action
(defun plain-sql-select ()
(with-database-connection
(query "select * from students;")))
(defun s-sql-select ()
(with-database-connection
(query (:select '*
:from 'students))))
;;; Both update functions execute the same action
(defun plain-sql-update ()
(with-database-connection
(query "update students set name='Juan Reynoso Elias' where id=1;")))
(defun s-sql-update ()
(with-database-connection
(query (:update 'students
:set
'name "Juan Reynoso Elias"
:where (:= 'id 1)))))
;;; Both delete functions execute the same action
(defun plain-sql-delete ()
(with-database-connection
(query "delete from students where id=1;")))
(defun s-sql-delete ()
(with-database-connection
(query (:delete-from 'students
:where (:= 'id 1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment