Jaidyn Levesque authored on 2018-05-02 08:23:35
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,11 @@
1
+(defsystem "simple-sql"
2
+  :version "0.0"
3
+  :author "Jaidyn Lev <jadedctrl@teknik.io>"
4
+  :license "GPLv3+"
5
+  :depends-on ("cl-mysql") 
6
+  :components ((:module "src"
7
+                :components
8
+		((:file "package")
9
+		(:file "main"))))
10
+  :description
11
+	"A basic wrapper for cl-mysql")
0 12
new file mode 100644
... ...
@@ -0,0 +1,175 @@
1
+(in-package :simple-sql)
2
+
3
+(defun init (database)
4
+  "Initializes the mySQL database."
5
+
6
+	(com.hackinghat.cl-mysql:connect)
7
+	(com.hackinghat.cl-mysql:use database))
8
+
9
+(defun fetch (table query)
10
+  "Fetches a set query from a table."
11
+
12
+	(com.hackinghat.cl-mysql:query
13
+	(concatenate `string
14
+		"select " query " from " table)))
15
+
16
+(defun fetch-where (table variable value query)
17
+  "Fetches a specific row-- using 'where variable == value' to select the row.
18
+  Returns the query."
19
+
20
+	(com.hackinghat.cl-mysql:query
21
+	(concatenate `string
22
+		"select " query " from " table " where " variable "=" value ";")))
23
+
24
+(defun insert-row (table &rest values)
25
+  "Inserts a new row into a table."
26
+	(com.hackinghat.cl-mysql:query
27
+	(concatenate `string
28
+		"insert into " table " values ("
29
+		(apply 'comma-seperate values)
30
+		");")))
31
+
32
+(defun switch-where (table variable value modded-variable modded-value)
33
+  "Replaces one value of one row in one table with another value. :-)"
34
+
35
+	(com.hackinghat.cl-mysql:query
36
+	(concatenate `string
37
+		"update " table " set " modded-variable " = " modded-value
38
+		" where " variable " = " value ";")))
39
+		
40
+(defun destroy-where (table variable value)
41
+  "Completely deletes row from table (using 'where variable == value' to select
42
+  the row."
43
+
44
+	(com.hackinghat.cl-mysql:query
45
+	(concatenate `string
46
+		"delete from " table " where " variable " = " value ";")))
47
+
48
+(defun table-create (table &rest values)
49
+  "Creates a table, with value pairs (value -> variable) as cons."
50
+
51
+	(format t (concatenate `string
52
+		"create table " (string table) " ("
53
+MAPCAR OUTPUTS RESULTS AS FUCKING *LISTS*, SO MAKE SURE TO FIX THAT SO IT'S JUST
54
+A PLAIN GODDAMN STRING, IDIOT
55
+		( (mapcar (lambda (x)
56
+			(if (equal (car (reverse values)) x)
57
+				(format nil "~A ~A);" (car x) (car (cdr x)))
58
+				(format nil "~A ~A, " (car x) (car (cdr x)))))
59
+			values)))))
60
+
61
+(defun tables-init ()
62
+  "Creates new tables if they don't already exist."
63
+	(simple-sql:table-create "pages"
64
+		`("centre" "varchar(70)")
65
+		`("title" "varchar(70)")
66
+		`("language" "varchar(3)")
67
+		`("genre" "varchar(70)")
68
+		`("category" "varchar(70)")
69
+		`("content" "longtext")
70
+		`("modifier" "varchar(70)")
71
+		`("diff" "longtext")
72
+		`("last" "bigint")
73
+		`("date" "bigint"))
74
+	(simple-sql:table-create "history"
75
+		`("centre" "varchar(70)")
76
+		`("title" "varchar(70)")
77
+		`("language" "varchar(3)")
78
+		`("genre" "varchar(70)")
79
+		`("category" "varchar(70)")
80
+		`("content" "longtext")
81
+		`("modifier" "varchar(70)")
82
+		`("diff" "longtext")
83
+		`("last" "bigint")
84
+		`("date" "bigint"))
85
+	(simple-sql:table-create "profiles"
86
+		`("centre" "varchar(70)")
87
+		`("title" "varchar(70)")
88
+		`("language" "varchar(3)")
89
+		`("genre" "varchar(70)")
90
+		`("category" "varchar(70)")
91
+		`("content" "longtext")
92
+		`("modifier" "varchar(70)")
93
+		`("diff" "longtext")
94
+		`("last" "bigint")
95
+		`("date" "bigint"))
96
+
97
+	(simple-sql:table-create "moderation_log"
98
+		`("moderator" "varchar(70)")
99
+		`("victim" "varchar(70)")
100
+		`("victim_ip" "varchar(45)")
101
+		`("action" "mediumtext")
102
+		`("rationale" "mediumtext")
103
+		`("date" "bigint"))
104
+	(simple-sql:table-create "general_log"
105
+		`("username" "varchar(70)")
106
+		`("user_ip" "varchar(45)")
107
+		`("action" "mediumtext")
108
+		`("location" "mediumtext")
109
+		`("date" "bigint")))
110
+
111
+(defun database-create (database)
112
+  "Creates a new database."
113
+
114
+	(com.hackinghat.cl-mysql:query
115
+		"create database " database))
116
+
117
+(defun comma-seperate (&rest values)
118
+  "Creates a string made up of all values seperated by commas."
119
+	(string-trim "," (format nil "~{~A~^ ~}"
120
+		(mapcar (lambda (x)
121
+			(format nil "'~A',"
122
+				(string-sanitize (format nil "~A" x)))) values))))
123
+
124
+(defun replace-all (string part replacement &key (test #'char=))
125
+"Returns a new string in which all the occurences of the part 
126
+is replaced with replacement.
127
+From http://cl-cookbook.sourceforge.net/strings.html#manip <3"
128
+	(with-output-to-string (out)
129
+	(loop with part-length = (length part)
130
+		for old-pos = 0 then (+ pos part-length)
131
+		for pos = (search part string
132
+				:start2 old-pos
133
+				:test test)
134
+			do (write-string string out
135
+				:start old-pos
136
+				:end (or pos (length string)))
137
+		when pos do (write-string replacement out)
138
+			while pos))) 
139
+
140
+(defun positions (needle operated-list)
141
+   "Returns all positions of a set 'needle' in a list."
142
+        (let ((list operated-list)
143
+                (positions ()))
144
+
145
+        (loop
146
+                :while (find needle list :test #'equal)
147
+                :do
148
+                        (setq positions (append (list (position needle list :test #'equal)) positions))
149
+                        (setf (nth (position needle list :test #'equal) list) nil))
150
+        positions))
151
+
152
+
153
+(defun strz (&rest args)
154
+   "Converts string to string surrounded by escaped quotation marks."
155
+	(format nil "\"~A\"" (car args)))
156
+
157
+(defun list-split (list split)
158
+   "Splits a list into sub-lists based on a set element. I.E.,
159
+  'split at instances of 2 in `(1 2 3 4 2 5 6 7)'"
160
+	(let ((current-list `())
161
+		(overarching-resultant-list `())
162
+		(overarching-list list))
163
+	(loop
164
+		:for x
165
+		:in list
166
+		:do
167
+		(if (equal split x)
168
+			(setq overarching-resultant-list (append overarching-resultant-list (list current-list)))
169
+			(setq current-list (append current-list (list x))))
170
+		(if (equal split x)
171
+			(setq current-list `()))
172
+		(if (eq (length overarching-list) 1)
173
+			(setq overarching-resultant-list (append overarching-resultant-list (list current-list))))
174
+		(setq overarching-list (cdr overarching-list)))
175
+	overarching-resultant-list))
0 176
new file mode 100644
... ...
@@ -0,0 +1,20 @@
1
+(defpackage :simple-sql
2
+	(:use :cl)
3
+	(:export 
4
+		:init
5
+
6
+		:fetch
7
+		:fetch-where
8
+
9
+		:insert-row
10
+		
11
+		:switch-where
12
+
13
+		:destroy-where
14
+
15
+		:table-create
16
+
17
+		:database-create))
18
+
19
+(in-package :simple-sql)
20
+