-
Notifications
You must be signed in to change notification settings - Fork 0
/
sv_sickql.lua
165 lines (139 loc) · 3.5 KB
/
sv_sickql.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
---@class SickQLInit
---@field Driver 'sqlite' | 'tmysql' | 'mysqloo' | string
---@field Hostname? string
---@field Username? string
---@field Password? string
---@field Database? string
---@field Port? integer
---@class SickQLConnection
---@field Escape fun(self, string: string): string
---@field Query fun(self, query: string, onData?: fun(data: (table<string, string>)[]), onError?: fun(why: string))
---@field Disconnect fun(self)
if util.IsBinaryModuleInstalled('mysqloo') then
require('mysqloo')
end
if util.IsBinaryModuleInstalled('tmysql4') then
require('tmysql4')
end
SickQL = SickQL or {}
SickQL.Implementations = SickQL.Implementations or {}
---@param init SickQLInit
---@return SickQLConnection | nil, string | nil
function SickQL.New(init)
local impl = SickQL.Implementations[init.Driver:lower()]
if impl == nil then
return nil, 'No such SickQL implementation!'
end
local driver, err = impl.Connect(init)
if err ~= nil then
return nil, err
end
return setmetatable({
impl = impl,
driver = driver,
}, SickQL.CONNECTION_META), nil
end
SickQL.Implementations['sqlite'] = {
Connect = function(init)
return nil, nil
end,
Escape = function(string)
return sql.SQLStr(string, true)
end,
Query = function(driver, query, onData, onError)
local res = sql.Query(query)
if res == false then
onError(sql.LastError())
return
end
local data = res or {}
onData(data)
end,
Disconnect = function(driver) end,
}
SickQL.Implementations['tmysql'] = {
Connect = function(init)
local connection, err = tmysql.Connect(
init.Hostname,
init.Username,
init.Password,
init.Database,
init.Port
)
if err ~= nil then
return nil, err
end
hook.Add('Think', string.format('SickQL::TMySQLPolling(%s)', connection), function()
connection:Poll()
end)
return connection, nil
end,
Escape = function(driver, string)
return driver:Escape(string)
end,
Query = function(driver, query, onData, onError)
driver:Query(query, function(res)
res = res[1]
if res.status == true then
onData(res.data)
else
onError(res.error)
end
end)
end,
Disconnect = function(driver)
driver:Disconnect()
end,
}
SickQL.Implementations['mysqloo'] = {
Connect = function(init)
local db = mysqloo.connect(
init.Hostname,
init.Username,
init.Password,
init.Database,
init.Port
)
local err
function db:onConnectionFailed(why)
err = why
end
db:connect()
db:wait()
if db:status() == mysqloo.DATABASE_CONNECTED then
return db, nil
else
return nil, err
end
end,
Escape = function(driver, string)
return driver:escape(string)
end,
Query = function(driver, query, onData, onError)
local q = driver:query(query)
function q:onSuccess(data)
onData(data)
end
function q:onError(why)
onError(why)
end
q:start()
end,
Disconnect = function(driver)
driver:disconnect()
end,
}
local CONNECTION_META = {}
CONNECTION_META.__index = CONNECTION_META
function CONNECTION_META:Escape(string)
return self.impl.Escape(self.driver, string)
end
function CONNECTION_META:Query(query, onData, onError)
onData = onData or function() end
onError = onError or function() end
return self.impl.Query(self.driver, query, onData, onError)
end
function CONNECTION_META:Disconnect()
self.impl.Disconnect(self.driver)
end
SickQL.CONNECTION_META = CONNECTION_META