Handle unquoted values for field lists in clog-data

This commit is contained in:
David Botton 2022-04-26 16:08:26 -04:00
parent 3d8d7f7136
commit 7df2be3541
3 changed files with 52 additions and 18 deletions

View file

@ -153,23 +153,34 @@ stringified first. If :QUOTE-ALL t then all fields are in quotes."
(push field result)) (push field result))
(format nil "~{~A~}" result))) (format nil "~{~A~}" result)))
;;;;;;;;;;;;;;;
;; sql-quote ;;
;;;;;;;;;;;;;;;
(defun sql-quote (value)
"Returns value single quoted if string (single quote quoted by doubling)
unless is the single character '?'. If value is a list the car is returned
unquoted"
(cond ((and (stringp value)
(not (equal value "?")))
(format nil "'~A'"
(ppcre:regex-replace-all "'" value "''")))
((consp value)
(car value))
(t
value)))
;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;
;; sql-value-list ;; ;; sql-value-list ;;
;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;
(defun sql-value-list (value-list) (defun sql-value-list (value-list)
"Given list of values returns a string for use in a SQL insert value "Given list of values each passed to SQL-QUOTE returns a string for
list. If a value is a string it is quoted with single quotes use in a SQL insert value list."
(and single quotes qutoed by doubling) unless is the single
character '?'."
(let ((result)) (let ((result))
(dolist (value (reverse value-list)) (dolist (value (reverse value-list))
(setf value (format nil "~A~A" (setf value (format nil "~A~A"
(if (and (stringp value) (sql-quote value)
(not (equal value "?")))
(format nil "'~A'"
(ppcre:regex-replace-all "'" value "''"))
(format nil "~A" value))
(if result ", " ""))) (if result ", " "")))
(push value result)) (push value result))
(format nil "~{~A~}" result))) (format nil "~{~A~}" result)))
@ -179,10 +190,9 @@ character '?'."
;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;
(defun sql-update-list (plist) (defun sql-update-list (plist)
"Given plist of field names and values returns a string for use in a "Given plist of field names and values each passed to SQL-QUOTE and
SQL update. if the 'key' is a cons the first 'key' used. If a value returns a string for use in a SQL update. if the 'key' is a cons the
is a string it is quoted with single quotes (and single quotes qutoed first 'key' used."
by doubling) unless is the single character '?'."
(let ((result)) (let ((result))
(loop for (key value) on plist by #'cddr while value (loop for (key value) on plist by #'cddr while value
do do
@ -190,11 +200,7 @@ by doubling) unless is the single character '?'."
(if (consp key) (if (consp key)
(car key) (car key)
key) key)
(if (and (stringp value) (sql-quote value)
(not (equal value "?")))
(format nil "'~A'"
(ppcre:regex-replace-all "'" value "''"))
(format nil "~A" value))
(if result ", " "")) (if result ", " ""))
result)) result))
(format nil "~{~A~}" result))) (format nil "~{~A~}" result)))

View file

@ -21,6 +21,7 @@
(get-profile function) (get-profile function)
(sign-up function) (sign-up function)
(make-token function) (make-token function)
(load-content function)
(create-base-tables function)) (create-base-tables function))
;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;
@ -135,11 +136,37 @@ if one is present and login fails."
(dbi:do-sql (dbi:do-sql
sql-connection sql-connection
"create table config (key varchar, value varchar)") "create table config (key varchar, value varchar)")
(dbi:do-sql
sql-connection
(format nil "create table content (key varchar, value varchar, parent varchar, title varchar, username varchar, createdate date)"))
(dbi:do-sql
sql-connection
"create table tags (key varchar, value varchar, category varchar)")
(dbi:do-sql (dbi:do-sql
sql-connection sql-connection
"create table users (username varchar, password varchar, token varchar)") "create table users (username varchar, password varchar, token varchar)")
(dbi:do-sql
sql-connection
(sql-insert* "content" '(:key "main"
:value "<h3>Welcome to CLOG</h3>"
:createdate ("date()"))))
(dbi:do-sql (dbi:do-sql
sql-connection sql-connection
(sql-insert* "users" `(:username "admin" (sql-insert* "users" `(:username "admin"
:password "admin" :password "admin"
:token ,(make-token))))) :token ,(make-token)))))
;;;;;;;;;;;;;;;;;;
;; load-content ;;
;;;;;;;;;;;;;;;;;;
(defun load-content (sql-connection table key-value &key (key-col "key"))
"Returns list of records found in TABLE where KEY-COL = KEY-VALUE"
(let ((contents (dbi:fetch-all
(dbi:execute
(dbi:prepare
sql-connection
(format nil "select * from ~A where ~A=?"
table key-col))
(list key-value)))))
contents))

View file

@ -535,6 +535,7 @@ embedded in a native template application.)"
(data-write-plist function) (data-write-plist function)
"SQL Writing Helpers" "SQL Writing Helpers"
(sql-quote function)
(sql-field-list function) (sql-field-list function)
(sql-value-list function) (sql-value-list function)
(sql-update-list function) (sql-update-list function)