-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasslib.lua
470 lines (461 loc) · 16.3 KB
/
classlib.lua
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
--classlib.lua
local loadstring = loadstring
-------OOP
local getmetatable,setmetatable,setfenv = getmetatable,setmetatable,setfenv
local tonumber,tostring = tonumber,tostring
local _call = call
local function call(...)
local _source = source
local retValue = {_call(...)}
source = _source
return unpack(retValue)
end
-------Utils
local strToIntCache = {
["vector2"]=2,
["vector3"]=3,
["vector4"]=4,
}
oopUtil = {
classReg = {},
classMetaReg = {},
instanceReg = setmetatable({},{__mode="kv"}),
eventHandler = {},
transfromEventName = function(eventName,isReverse)
return isReverse and (eventName:sub(3,3):lower()..eventName:sub(4)) or ("on"..eventName:sub(1,1):upper()..eventName:sub(2))
end,
getVectorType = function(vec)
if type(vec) == "userdata" then
local typeName = getUserdataType(vec)
if typeName == "vector" then
return strToIntCache[typeName]
end
end
return false
end,
deepCopyWithMeta = function(obj)
local function Func(obj)
if type(obj) ~= "table" then return obj end
local NewTable = {}
for k,v in pairs(obj) do
NewTable[Func(k)] = Func(v)
end
return setmetatable(NewTable,getmetatable(obj))
end
return Func(obj)
end,
splitKeyValue = function(theTable)
local keyTable = {}
local valueTable = {}
for key,value in pairs(theTable) do
keyTable[#keyTable+1] = key
valueTable[#valueTable+1] = value
end
return keyTable,valueTable
end;
deepCopy = function(obj)
local function Func(obj)
if type(obj) ~= "table" then return obj end
local NewTable = {}
for k,v in pairs(obj) do
NewTable[Func(k)] = Func(v)
end
return NewTable
end
return Func(obj)
end,
shallowCopy = function(obj)
local InTable = {}
for k,v in pairs(obj) do
InTable[k] = v
end
return InTable
end,
assimilate = function(t1,t2,except)
if not t1 or not t2 then return end
local exceptTable = {}
if type(except) == "table" then
for i=1,#except do
exceptTable[ except[i] ] = true
end
end
for k,v in pairs(t2) do
if not exceptTable[k] then
t1[k] = v
end
end
end,
spreadFunctionsForClass = function(class,classTemplate)
oopUtil.assimilate(class,classTemplate,{"expose","constructor"})
oopUtil.assimilate(class,classTemplate)
end,
configToSql = function(self,dbType)
local strTable = {}
local specialKeys = {}
local theDataType
for columnName,dType in pairs(self) do
if type(dType) == "string" then
local extraTags = ""
if string.find(dType,";") then
local items = split(dType,";")
theDataType = sqlDataType[items[1]] or items[1]
for i=2,#items do
local tag = items[i]:lower():gsub(" ","")
if tag:sub(1,7) == "primary" then
extraTags = extraTags.." Primary Key "
elseif tag:sub(1,6) == "unique" then
extraTags = extraTags.." Unique "
elseif tag:sub(1,7) == "foreign" then
specialKeys[#specialKeys+1] = "Foreign Key (`"..columnName.."`) References "..items[i]:sub(8)
elseif tag == "notnull" then
extraTags = extraTags.." Not Null "
elseif tag == "defaultnull" then
extraTags = extraTags.." Default Null "
elseif tag == "autoincrement" or tag == "auto_increment" then
extraTags = extraTags.." Auto_Increment "
end
end
else
theDataType = sqlDataType[dType] or dType
end
strTable[#strTable+1] = "`"..columnName.."` "..theDataType.." "..extraTags
end
end
for i=1,#specialKeys do
strTable[#strTable+1] = specialKeys[i]
end
return table.concat(strTable,",")
end,
}
function class(name) return function(classTable)
oopUtil.classReg[name] = classTable --register class with class name
oopUtil.classMetaReg[name] = {__index = {}} --register class metatable with class name
local meta = {
__call = function(classTemplate,...)
local newInstance = {}
setmetatable(newInstance,oopUtil.classMetaReg[name])
if classTemplate.constructor then
classTemplate.constructor(newInstance,...)
else
local copyData = ...
if type(copyData) ~= "table" then return newInstance end
for k,v in pairs(copyData) do
newInstance[k] = v
end
end
return newInstance
end,
}
if classTable.extend then
if type(classTable.extend) ~= "table" then
local extendClass = oopUtil.classReg[classTable.extend]
for extKey,extFunction in pairs(extendClass) do
if classTable[extKey] == nil then classTable[extKey] = extFunction end --Don't overwrite child's function when copying parent's functions
end
else
for key,extend in ipairs(classTable.extend) do
local extendClass = oopUtil.classReg[extend]
for extKey,extFunction in pairs(extendClass) do
if classTable[extKey] == nil then classTable[extKey] = extFunction end --Don't overwrite child's function when copying parent's functions
end
end
end
end
if classTable.inject then
for theType,space in pairs(classTable.inject) do
local injectedData = oopUtil.classReg[theType]
if not injectedData then injectedData = {} end
for name,fnc in pairs(space or {}) do
injectedData[name] = fnc
end
for name,fnc in pairs(space or {}) do
injectedData[name] = fnc
end
end
end
meta.__index = {class=name}
setmetatable(classTable,meta)
oopUtil.spreadFunctionsForClass(oopUtil.classMetaReg[name].__index,classTable)
oopUtil.classMetaReg[name].__index.class = name
oopUtil.classMetaReg[name].__index.instance = true
if not classTable.expose then
_G[name] = classTable
elseif oopUtil.classMetaReg[classTable.expose] then
oopUtil.classMetaReg[classTable.expose].__index[name] = function(self,...) return classTable(...) end
end
end
end
oopUtil.class = class
--Types:
sqlDataType = {
integer = "int",
int = "int",
int8 = "tinyint",
int16 = "smallint",
int24 = "mediumint",
int32 = "int",
int64 = "bigint",
uint = "int unsigned",
uint8 = "tinyint unsigned",
uint16 = "smallint unsigned",
uint24 = "mediumint unsigned",
uint32 = "int unsigned",
uint64 = "bigint unsigned",
}
class "MORM" {
Open = function(self,...)
return oopUtil.classReg.DataBase(...)
end;
}
class "DataBase" {
expose = "MORM",
constructor = function(self,dbType,...)
self.db = dbConnect(dbType,...)
self.dbString = {}
self.dbType = dbType
self.dbPendingFill = nil
end;
AutoMigrate = function(self,...)
if select("#",...) == 2 then
local first,second = ...
--"tableName", classTemplate
if type(first) == "string" then
-- to do
end
end
end;
Create = function(self,...)
local tableName,template
if select("#",...) == 1 then
template = ...
if not(type(template) == "table" and template.class) then outputDebugString("@Create at argument 1, expect a Class/Instance got "..type(template),3) return false end
if not self.dbString.table then --if table name is not specified
if not(type(template.class) == "string") then outputDebugString("@Create, table name is not specified",3) return false end
end
tableName = self.dbString.table or template.class
else
tableName,template = ...
if not(type(tableName) == "string") then outputDebugString("@Create at argument 1, expect a string got "..type(tableName),3) end
if not(type(template) == "table") then outputDebugString("@Create at argument 2, expect a Class/table got "..type(template),3) end
tableName = self.dbString.table or tableName
end
if template.instance then --If is instance, just insert
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Insert Into `??` ",tableName)
local ks,vs = oopUtil.splitKeyValue(template)
local keys,values = {},{}
for i=1,#ks do
keys[#keys+1] = dbPrepareString(self.db,"`??`",ks[i])
values[#values+1] = dbPrepareString(self.db,"?",vs[i])
end
self.dbString[#self.dbString+1] = "("..table.concat(keys,",")..") Values ("..table.concat(values,",")..")"
else
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Create Table If Not Exists `??` (??)",self.dbString.table or template.class,oopUtil.configToSql(template,self.dbType))
end
return self
end;
Select = function(self,selectColumn)
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Select ?? ",selectColumn or "*")
if self.dbString.table then
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"From `??` ",self.dbString.table)
end
return self
end;
From = function(self,fromTable)
if not fromTable then
if not self.dbString.table then outputDebugString("@From, table name is not specified",3) return false end
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"From `??` ",self.dbString.table)
else
if self.dbString.table then outputDebugString("@From, table name is already specified",3) return false end
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"From `??` ",fromTable)
end
return self
end;
Where = function(self,columnName,value)
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Where `??` = ? ",columnName,value)
return self
end;
Find = function(self,...)
local argCount = select("#",...)
if argCount == 1 then
local instance = ...
if not(type(instance) == "table") then outputDebugString("@Find at argument 1, expect a table/Instance got "..type(instance),3) return false end
if instance.instance == true then --If is instance
if not self.dbString.table then self.dbString.table = instance.class end
self:Select()
local where = {}
for key,value in pairs(instance) do
where[#where+1] = dbPrepareString(self.db," `??` = ? ",key,value)
end
self.dbString[#self.dbString+1] = "Where"..table.concat(where,"and")
self.dbPendingFill = {instance}
else --Otherwise, Instance List
if not self.dbString.table then self.dbString.table = instance[1].class end
local conditions = {}
for i=1,#instance do
local where = {}
for key,value in pairs(instance[i]) do
where[#where+1] = dbPrepareString(self.db," `??` = ? ",key,value)
end
conditions[#conditions+1] = " ("..table.concat(where,"and")..") "
end
self:Select()
self.dbString[#self.dbString+1] = "Where"..table.concat(conditions,"or")
self.dbPendingFill = instance
end
end
return self
end;
Update = function(self,...)
local argCount = select("#",...)
if argCount == 1 then
local template = ...
if not(type(template) == "table") then outputDebugString("@Update at argument 1, expect a Class/Instance got "..type(template),3) return false end
if not self.dbString.table then
if not(type(template.class) == "string") then outputDebugString("@Update, table name is not specified",3) return false end
self.dbString.table = template.class
end
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Update `??` SET ",self.dbString.table)
local kvPair = {}
for key,value in pairs(template) do
local vType = type(value)
if vType ~= "userdata" and vType ~= "function" then
kvPair[#kvPair+1] = dbPrepareString(self.db," `??` = ? ",key,value)
end
end
self.dbString[#self.dbString+1] = table.concat(kvPair,",")
elseif argCount == 2 then
local key,value = ...
if type(value) == "table" then
local tableNameOrTemplate,template = ...
if not (type(tableNameOrTemplate) == "string" or (type(tableNameOrTemplate) == "table" and tableNameOrTemplate.class)) then outputDebugString("@Update at argument 1, expect a Class/string got "..type(tableNameOrTemplate),3) end
if not(type(template) == "table") then outputDebugString("@Update at argument 2, expect a table/Instance got "..type(template),3) return false end
if not self.dbString.table then
if type(tableNameOrTemplate) == "string" then
self.dbString.table = tableNameOrTemplate
else
self.dbString.table = tableNameOrTemplate.class
end
end --Skip if table is already specified
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Update `??` SET ",self.dbString.table)
local kvPair = {}
for key,value in pairs(template) do
local vType = type(value)
if vType ~= "userdata" and vType ~= "function" then
kvPair[#kvPair+1] = dbPrepareString(self.db," `??` = ? ",key,value)
end
end
self.dbString[#self.dbString+1] = table.concat(kvPair,",")
else
if not (type(key) == "string") then outputDebugString("@Update at argument 1, expected a string got "..type(key),3) return false end
if not self.dbString.table then outputDebugString("@Update, table name is not specified",3) return false end
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Update `??` SET `??` = ? ",self.dbString.table,key,value)
end
elseif argCount == 3 then
local tableNameOrTemplate,key,value = ...
if not (type(tableNameOrTemplate) == "string" or (type(tableNameOrTemplate) == "table" and tableNameOrTemplate.class)) then outputDebugString("@Update at argument 1, expect a Class/string got "..type(tableNameOrTemplate),3) end
if not (type(key) == "string") then outputDebugString("@Update at argument 2, expected a string got "..type(key),3) return false end
if not self.dbString.table then
if type(tableNameOrTemplate) == "string" then
self.dbString.table = tableNameOrTemplate
else
self.dbString.table = tableNameOrTemplate.class
end
end --Skip if table is already specified
if not self.dbString.table then outputDebugString("@Update, table name is not specified",3) return false end
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Update`??` SET `??` = ? ",self.dbString.table,key,value)
end
return self
end;
Delete = function(self,tableName)
self.dbString[#self.dbString+1] = dbPrepareString(self.db,"Drop Table If Exists ?",tableName)
return self
end;
Query = function(self,timedout,callback)
local _self = self
local pendingFill = self.dbPendingFill
self.dbPendingFill = nil
if callback then
dbQuery(function(handle)
self = _self
local pollData = dbPoll(handle,timedout or -1)
if pollData and pendingFill then --Pending To Fill
local successCount = 0
for i=1,#pendingFill do --Loop Pending To Fill List
local p = 1
while(pollData[p]) do --Loop Retrieved Data List
local isCopyData = true
for key,value in pairs(pendingFill[i]) do --Compare Data
if pollData[p][key] ~= value then isCopyData = false break end --Continue
end
if isCopyData then
for key,value in pairs(pollData[p]) do --Copy Data
pendingFill[i][key] = value
end
successCount = successCount+1
table.remove(pollData,p) --Remove copied data
p = #pollData
end
p = p+1
end
end
callback(self,successCount)
else
callback(self,pollData)
end
end,self.db,table.concat(self.dbString))
print(table.concat(self.dbString))
self.dbString = {}
return true
else
local handle = dbQuery(self.db,table.concat(self.dbString))
print(table.concat(self.dbString))
self.dbString = {}
local pollData = dbPoll(handle,timedout or -1)
if pollData and pendingFill then --Pending To Fill
local successCount = 0
for i=1,#pendingFill do --Loop Pending To Fill List
local p = 1
while(pollData[p]) do --Loop Retrieved Data List
local isCopyData = true
for key,value in pairs(pendingFill[i]) do --Compare Data
if pollData[p][key] ~= value then isCopyData = false break end --Continue
end
if isCopyData then
for key,value in pairs(pollData[p]) do --Copy Data
pendingFill[i][key] = value
end
successCount = successCount+1
table.remove(pollData,p) --Remove copied data
p = #pollData
end
p = p+1
end
end
return successCount
end
return pollData
end
end;
Raw = function(self,raw,...)
if not(type(raw) == "string") then return outputDebugString("@Raw at argument 1, expect a string got "..type(raw),3) end
self.dbString[#self.dbString+1] = dbPrepareString(self.db,raw,...)
return self
end;
Table = function(self,tableNameOrTemplate)
--Use this table if specified by :Table("tableName"), and this has the top priority.
if not (type(tableNameOrTemplate) == "string" or (type(tableNameOrTemplate) == "table" and tableNameOrTemplate.class)) then outputDebugString("@Table at argument 1, expect a Class/string got "..type(tableNameOrTemplate),3) end
self.dbString.table = (type(tableNameOrTemplate) == "string") and tableNameOrTemplate or tableNameOrTemplate.class
return self
end;
Alert = function(self,...)
end;
Drop = function(self,...)
end;
Change = function(self,...)
end;
Add = function(self,...)
end;
Modify = function(self,...)
end;
}
morm = MORM()