-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.lua
311 lines (263 loc) · 8.03 KB
/
client.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
--[[
Copyright 2012 Kengo Nakajima. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local table = require("table")
local math = require("math")
local string = require("string")
local net = require("net")
local Constants = require("./constants")
local Parser = require("./parser")
local Auth = require("./auth")
local Util = require("./util")
local Buffer = require("buffer").Buffer
local mysql = {}
local Query = require("./query")
local OutgoingPacket = require( "./outgoing_packet")
Client={}
function Client:new(conf)
local client = {}
-- defaults
client.host = "127.0.0.1"
client.port = 3306
client.user = "root"
client.password = nil
client.database = ""
client.logging = false
if conf.password then client.password = conf.password end
if conf.user then client.user = conf.user end
if conf.database then client.database = conf.database end
if conf.port then client.port = conf.port end
if conf.host then client.host = conf.host end
if conf.logfunc then client.logfunc = conf.logfunc else client.logfunc = function()end end
client.log = client.logfunc
local flags = {
Constants.CLIENT_LONG_PASSWORD,
Constants.CLIENT_FOUND_ROWS,
Constants.CLIENT_LONG_FLAG,
Constants.CLIENT_CONNECT_WITH_DB,
Constants.CLIENT_ODBC,
Constants.CLIENT_LOCAL_FILES,
Constants.CLIENT_IGNORE_SPACE,
Constants.CLIENT_PROTOCOL_41,
Constants.CLIENT_INTERACTIVE,
Constants.CLIENT_IGNORE_SIGPIPE,
Constants.CLIENT_TRANSACTIONS,
Constants.CLIENT_RESERVED,
Constants.CLIENT_SECURE_CONNECTION,
Constants.CLIENT_MULTI_STATEMENTS,
Constants.CLIENT_MULTI_RESULTS
}
client.flags = 0
for k,v in pairs(flags) do
client.flags = client.flags + v
end
client.typeCast = true
client.maxPacketSize = 0x01000000
client.charsetNumber = Constants.UTF8_UNICODE_CI
client.debug = true
client.ending = false
client.connected = false
client.greeting = nil
client.queue = {}
client.socket = nil
client.parser = nil
client.socket = net.createConnection( client.port, client.host, function(err)
if err then
p(err)
return
end
client:log("mysql:connected")
end)
function client:connectionErrorHandler()
return function(err)
local task = self.queue[1]
local delegate = nil
if task.delegate then delegate = task.delegate end
if type(delegate)=="table" and delegate.handlePacket then
delegate:emit("error",err)
return
end
if not delegate then
self:emit("error",err)
else
delegate(err)
table.remove( self.queue, 1 )
end
end
end
client.socket:on("error", function(err)
local f = client:connectionErrorHandler()
f(err)
end )
client.socket:on("data", function(data)
client.parser:receive(data)
end )
client.socket:on("end", function()
local f = client:connectionErrorHandler()
f("socket closed")
end )
client.parser = Parser:new({logfunc=client.logfunc})
client.parser:on("packet", function(packet)
client:handlePacket(packet)
end)
function client:handlePacket(packet)
self.log("client.handlePacket called. packet type:", packet.type )
if packet.type == Constants.GREETING_PACKET then -- 0
self.log("client:handlePacket: greeting packet. sending auth..")
self:sendAuth(packet)
return
end
if not self.connected then
self.log("client:handlePacket: NOT CONNECTED YET. packet.type:", packet.type )
if packet.type ~= Constants.ERROR_PACKET then
self.connected = true
if #self.queue > 0 then
self.queue[1].fn()
end
return
end
local fn = self:connectionErrorHandler()
fn( Util.packetToUserObject(packet) )
return
end
------
local task = self.queue[1]
local delegate = nil
if task then delegate = task.delegate end
if type(delegate)=="table" and delegate.handlePacket then -- for Query
delegate:handlePacket(packet)
return
end
if packet.type ~= Constants.ERROR_PACKET then
self.connected = true
if delegate then
delegate(nil, Util.packetToUserObject(packet))
end
else
local userpacket = Util.packetToUserObject(packet)
if delegate then
delegate(userpacket)
else
self:emit("error", userpacket )
end
end
self:dequeue()
end
function client:sendAuth(greetingPacket)
local token = Auth.token( self.password, Util.byteArrayToString(greetingPacket.scrambleBuffer ) )
local packetSize = ( 4 + 4 + 1 + 23 ) + ( #self.user + 1 ) + ( #token + 1 ) + ( #self.database + 1 )
local packet = OutgoingPacket:new( packetSize, greetingPacket.number + 1 )
packet:writeNumber( 4, self.flags )
packet:writeNumber( 4, self.maxPacketSize )
packet:writeNumber( 1, self.charsetNumber )
packet:writeFiller(23)
packet:writeNullTerminated(self.user)
packet:writeLengthCoded(token)
packet:writeNullTerminated(self.database)
self:write(packet)
self.greetingPacket = greetingPacket
end
function client:enqueue(f,delegate)
table.insert( self.queue, { fn=f, delegate=delegate } )
if #self.queue == 1 and self.connected then
f()
end
end
function client:query(sql,cb)
self.log("query:", sql )
assert( type(sql)=="string", "not implemented")
local q = Query:new({sql=sql, logfunc=self.log, typeCast=self.typeCast})
if cb then
q.fields={}
q.rows={}
q:on("error",function(err)
cb(err)
self:dequeue()
end)
q:on("field",function(field)
q.fields[field.name] = field
end)
q:on("row",function(row)
table.insert( q.rows, row)
end)
q:on("end",function(result)
if result then
cb(nil,result)
else
cb(nil, q.rows, q.fields)
end
self:dequeue()
end)
else
q:on("error",function(err)
error(err.message)
self:dequeue()
end)
q:on("end",function(result)
self:dequeue()
end)
end
-- put a func to a que
self:enqueue( function()
local pktlen = 1 + #sql
local packet = OutgoingPacket:new( pktlen )
packet:writeNumber( 1, Constants.COM_QUERY )
packet:write(sql, 'utf-8' )
self:write(packet)
end, q)
return q
end
function client:write( packet )
local s = Util.bufferToString(packet.buffer)
self.log( "->", packet.buffer.length, #s, packet.buffer:inspect() )
local wlen = self.socket:write( s, function(err)
assert(not err)
end)
end
function client:escape(val)
if nil == val then
return "NULL"
end
if type(val)=="boolean" then
if val then return "true" else return "false" end
end
if type(val)=="number" then
return tostring(val)
end
if type(val)=="table" then
if type( val.toISOString ) == "function" then
return val:toISOString()
else
return tostring(val)
end
end
-- to escape "\0" in lua, we cannot use string.gsub.
val = Util.escapeString( val )
return "'" .. val .. "'"
end
function client:ping(cb)
self:enqueue( function()
local packet = OutgoingPacket:new(1)
packet:writeNumber(1, Constants.COM_PING )
self:write(packet)
end, cb )
end
function client:dequeue()
table.remove( self.queue, 1 )
if #self.queue == 0 then
return
end
self.queue[1].fn()
end
return client
end
return Client