1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
-- CDB Database --
-- Hooks # Start ------------------------------------
addhook("spawn","Onspawn")		--|
addhook("parse","Onparse")		--|
-- Hooks # End --------------------------------------
-- Function # Start --------------------
if CDB==nil then CDB = {} end
CDB.database = {}
CDB.table = {}
CDB.selected_database = nil
CDB.table.extension = '.ctbl'
function csplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
local function directory_exists( sPath )
	if type( sPath ) ~= "string" then return false end
	local response = os.execute( "cd " .. sPath )
	if response == 0 then
	return true
	end
	return false
end
function file_exists( sPath )
local f=io.open(sPath,"r")
if f~=nil then io.close(f) return true else return false end
end
function open_file(path)
	local open = io.open
local file = open(path, "rb") -- r read mode and b binary mode
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
return content
end
function save_file(data,path)
	io.output(io.open(path,"w+"))
	local sv = io.write(data)
	io.close()
	return sv
end
	--++++++++++ INITIAL ++++++++++--
function CDB.install(dn)
	if (not directory_exists('sys/lua/CDB')) then
	c = os.execute("mkdir sys\\lua\\CDB")
	if (c == 0) then
		print('©127255000[*] CDB installed !')
		if (dn ~= nil) then
			local dbs = csplit(dn,',')
			for _,db in pairs(dbs) do
			 CDB.database.create(db)
			end
		end
		return true
	else
		print('©220020060[*] CDB error : 1')
		return false
	end
	else
		print('©127255000[*] CDB installed !')
		return true
	end
end
function CDB.uninstall()
	c = os.execute('rmdir /s /q "sys\\lua\\CDB\\"')
	if (c == 0) then
		print('©135206250[*] CDB database uninstalled !')
		return true
		else
		print('©220020060[*] CDB error : 2')
		return false
	end
end
	--++++++++++ DATABASE ++++++++++--
function CDB.database.create(name)
	local db_name = name
	if (not directory_exists('sys/lua/CDB/'..db_name)) then
		c = os.execute("mkdir sys\\lua\\CDB\\"..db_name)
		if (c == 0) then
			print('©127255000[*] CDB database ('..db_name..') created !')
			return true
			else
			print('©220020060[*] CDB error : 3')
			return false
		end
	else
		print('©220020060[*] Database ('..db_name..') already exist !')
		return true
	end
end
function CDB.database.drop(name)
	local db_name = name
	if (directory_exists('sys/lua/CDB/'..db_name)) then
		c = os.execute("rd /s/q sys\\lua\\CDB\\"..db_name)
		if (c == 0) then
			print('©135206250[*] CDB database ('..db_name..') droped !')
			return true
			else
			print('©220020060[*] CDB error : 4')
			return false
		end
	else
		print("©220020060[*] Database ("..db_name..") doesn't exist !")
		return false
	end
end
function CDB.database.rename(name,new_name)
	local source = name
	local destination = new_name
	if (directory_exists('sys/lua/CDB/'..source)) then
		c = os.execute("rename sys\\lua\\CDB\\"..source.." "..destination)
		if (c == 0) then
			print('©127255000[*] CDB database ('..source..') renamed to : '..destination..' !')
			return true
			else
			print('©220020060[*] CDB error : 5')
			return false
		end
	else
		print("©220020060[*] Database ("..source..") doesn't exist !")
		return false
	end
end
function CDB.database.exist(name)
	local db_name = name
	if (directory_exists('sys/lua/CDB/'..db_name)) then
		return true
	else
		return false
	end
end
function CDB.database.select(name)
	if (CDB.database.exist(name)) then
		CDB.selected_database = name
		print('©127255000[*] CDB database ('..CDB.selected_database..') selected !')
	else
		CDB.selected_database = nil
		print('©220020060[*] CDB database ('..name..') not found !')
	end
end
	--++++++++++ TABLE ++++++++++--
function CDB.table.create(name,data)
	if (CDB.selected_database ~= nil) then
		if (directory_exists('sys/lua/CDB/'..CDB.selected_database)) then -- if database exist
			local tname = name
			local fields = csplit(data,',')
			local fields_list = ''
			local x = 0
			for _,field in pairs(fields) do
				if (x == 1) then fields_list = fields_list..'|' end
			 fields_list = fields_list..field
			 x = 1
			end
			fields_list = fields_list..'#'
			if (save_file(fields_list,'sys/lua/CDB/'..CDB.selected_database..'/'..tname..CDB.table.extension)) then
				print('©127255000[*] CDB table ('..tname..') created !')
			else
				print('©220020060[*] CDB error : 6')	
			end
		else
			print('©220020060[*] CDB no database found !')
			return false
		end
	else
		print('©220020060[*] CDB no database selected !')
	end
end
function CDB.table.drop(name)
	if (CDB.selected_database ~= nil) then
		if (directory_exists('sys/lua/CDB/'..CDB.selected_database)) then -- if database exist
			local tname = name
			if (file_exists('sys/lua/CDB/'..CDB.selected_database..'/'..tname..CDB.table.extension)) then
				c = os.execute("del sys\\lua\\CDB\\"..CDB.selected_database..'\\'..tname..CDB.table.extension)
				if (c == 0) then
					print('©135206250[*] CDB table ('..tname..') droped from database ('..CDB.selected_database..') !')
					return true
					else
					print('©220020060[*] CDB error : 7')
					return false
				end
			else
				print("©220020060[*] CDB Table ("..tname..") doesn't exist !")
				return false
			end
		else
			print('©220020060[*] CDB no database found !')
			return false
		end
	else
		print('©220020060[*] CDB no database selected !')
	end
end
function CDB.table.rename(name,new_name)
	if (CDB.selected_database ~= nil) then
		if (directory_exists('sys/lua/CDB/'..CDB.selected_database)) then -- if database exist
			local source = name
			local destination = new_name
			if (file_exists('sys/lua/CDB/'..CDB.selected_database..'/'..source..CDB.table.extension)) then
				c = os.execute("rename sys\\lua\\CDB\\"..CDB.selected_database.."\\"..source..CDB.table.extension.." "..destination..CDB.table.extension)
				if (c == 0) then
					print('©127255000[*] CDB database ('..source..') renamed to : '..destination..' !')
					return true
					else
					print('©220020060[*] CDB error : 8')
					return false
				end
			else
				print("©220020060[*] CDB Table ("..source..") doesn't exist !")
				return false
			end
		else
			print('©220020060[*] CDB no database found !')
			return false
		end
	else
		print('©220020060[*] CDB no database selected !')
	end
end
function CDB.table.empty(name)
	if (CDB.selected_database ~= nil) then
		if (directory_exists('sys/lua/CDB/'..CDB.selected_database)) then -- if database exist
			if (file_exists('sys/lua/CDB/'..CDB.selected_database..'/'..name..CDB.table.extension)) then
				local tbl = open_file('sys/lua/CDB/'..CDB.selected_database..'/'..name..CDB.table.extension)
				tbl = csplit(tbl,'#')
				local rows = tbl[1]..'#'
				local res = save_file(rows,'sys/lua/CDB/'..CDB.selected_database..'/'..name..CDB.table.extension)
				if (res) then
					print("©135206250[*] CDB Table ("..name..") is empty now !")
				else
					print('©220020060[*] CDB error : 9')
				end
			else
				print("©220020060[*] CDB Table ("..name..") doesn't exist !")
				return false
			end
		else
			print('©220020060[*] CDB no database found !')
			return false
		end
	else
		print('©220020060[*] CDB no database selected !')
	end
end
function CDB.table.exist(name)
	if (CDB.selected_database ~= nil) then
		if (directory_exists('sys/lua/CDB/'..CDB.selected_database)) then -- if database exist
			local tname = name
			if (file_exists('sys/lua/CDB/'..CDB.selected_database..'/'..tname..CDB.table.extension)) then
				return true
			else
				return false
			end
		else
			print('©220020060[*] CDB no database found !')
			return false
		end
	else
		print('©220020060[*] CDB no database selected !')
	end
end
function CDB.table.move(name,destination)
	if (CDB.selected_database ~= nil) then
		if (directory_exists('sys/lua/CDB/'..CDB.selected_database)) then -- if database exist
			local source = name
			if (file_exists('sys/lua/CDB/'..CDB.selected_database..'/'..source..CDB.table.extension)) then
				c = os.execute("move sys\\lua\\CDB\\"..CDB.selected_database.."\\"..source..CDB.table.extension.." sys\\lua\\CDB\\"..destination.."\\"..source..CDB.table.extension)
				if (c == 0) then
					print('©127255000[*] CDB table ('..source..') moved to : '..destination..' !')
					return true
					else
					print('©220020060[*] CDB error : 10')
					return false
				end
			else
				print("©220020060[*] Table ("..source..") doesn't exist !")
				return false
			end
		else
			print('©220020060[*] CDB no database found !')
			return false
		end
	else
		print('©220020060[*] CDB no database selected !')
	end
end
	--++++++++++ GENERAL ++++++++++--
function CDB.query(query)
	-- Under construction !
end