Browse code

Added 'update-where' function

Jaidyn Lev authored on 2018-06-14 05:15:41
Showing 2 changed files
... ...
@@ -30,6 +30,21 @@
30 30
                        " where " variable "=" value ";")))))
31 31
 
32 32
 
33
+(defun update-where (table variable value new-value &optional test-variable test-value)
34
+  "Update the value of a variable (in the first row found 'where variable == value')
35
+  with new-value.
36
+  If optional `test-variable and `test-value args are definied, they'll be used in
37
+  the 'where variable == value' statement, rather than `variable and `value."
38
+
39
+  (cl-mysql:query
40
+    (concatenate `string
41
+                 "update " table " set " variable " = '" new-value "' where "
42
+                 (if (ignore-errors test-variable)
43
+                   (format nil "~A = ~A;" test-variable test-value)
44
+                   (format nil "~A = ~A;" variable value)))))
45
+
46
+
47
+
33 48
 (defun insert-row (table &rest values)
34 49
   "Inserts a new row into a table."
35 50
 
... ...
@@ -67,7 +82,7 @@
67 82
   "Creates a string made up of all values seperated by commas."
68 83
 
69 84
   (string-trim "," (format nil "~{~A~^ ~}"
70
-                           (mapcar (lambda (x) (format nil "'~A',"
85
+                           (mapcar (lambda (x) (format nil "~A,"
71 86
                                              (string-sanitize
72 87
                                                (format nil "~A" x)))) values))))
73 88
 
... ...
@@ -4,6 +4,7 @@
4 4
     :init
5 5
     :fetch
6 6
     :fetch-where
7
+    :update-where
7 8
     :insert-row
8 9
     :switch-where
9 10
     :destroy-where))