unimportant

This commit is contained in:
Cosmin Apreutesei 2021-09-01 23:28:25 +03:00
parent 39522271f8
commit f6a430275c
2 changed files with 39 additions and 18 deletions

View File

@ -920,6 +920,8 @@ local function get_err_packet(buf)
return message, errno, sqlstate return message, errno, sqlstate
end end
local get_collation --fw. decl.
function mysql.connect(opt) function mysql.connect(opt)
local tcp = opt and opt.tcp or require'sock'.tcp local tcp = opt and opt.tcp or require'sock'.tcp
@ -933,17 +935,20 @@ function mysql.connect(opt)
local user = opt.user or '' local user = opt.user or ''
local collation = 0 --default local collation = 0 --default
if opt.collation then print(opt.collation)
collation = assert(collation_codes[opt.collation], 'invalid collation') if opt.collation ~= 'server' then
self.collation = opt.collation if opt.collation then
self.charset = self.collation:match'^[^_]+' collation = assert(collation_codes[opt.collation], 'invalid collation')
elseif opt.charset then self.collation = opt.collation
local collation_name = assert(default_collations[opt.charset], 'invalid charset') self.charset = self.collation:match'^[^_]+'
collation = assert(collation_codes[collation_name]) elseif opt.charset then
self.charset = opt.charset local collation_name = assert(default_collations[opt.charset], 'invalid charset')
self.collation = collation_name collation = assert(collation_codes[collation_name])
self.charset = opt.charset
self.collation = collation_name
end
assert(self.collation, 'charset and/or collation required')
end end
self.charset_is_ascii_superset = self.charset and not mb_charsets[self.charset]
local host = opt.host local host = opt.host
local port = opt.port or 3306 local port = opt.port or 3306
@ -996,7 +1001,15 @@ function mysql.connect(opt)
return nil, 'old pre-4.1 authentication protocol not supported' return nil, 'old pre-4.1 authentication protocol not supported'
end end
check(self, typ == 'OK', 'bad packet type') check(self, typ == 'OK', 'bad packet type')
self.state = 'ready' self.state = 'ready'
if opt.collation == 'server' then
self.collation, self.charset = get_collation(self)
end
self.charset_is_ascii_superset = self.charset and not mb_charsets[self.charset]
return self return self
end end
conn.connect = protect(conn.connect) conn.connect = protect(conn.connect)
@ -1013,7 +1026,7 @@ function conn:close()
end end
conn.close = protect(conn.close) conn.close = protect(conn.close)
function conn:send_query(query) local function send_query(self, query)
assert(self.state == 'ready') assert(self.state == 'ready')
self.packet_no = -1 self.packet_no = -1
local buf = send_buffer(1 + #query) local buf = send_buffer(1 + #query)
@ -1023,9 +1036,9 @@ function conn:send_query(query)
self.state = 'read' self.state = 'read'
return true return true
end end
conn.send_query = protect(conn.send_query) conn.send_query = protect(send_query)
function conn:read_result(opt) local function read_result(self, opt)
assert(self.state == 'read' or self.state == 'read_binary') assert(self.state == 'read' or self.state == 'read_binary')
local typ, buf = recv_packet(self) local typ, buf = recv_packet(self)
if typ == 'ERR' then if typ == 'ERR' then
@ -1147,13 +1160,19 @@ function conn:read_result(opt)
self.state = 'ready' self.state = 'ready'
return rows, nil, cols return rows, nil, cols
end end
conn.read_result = protect(conn.read_result) conn.read_result = protect(read_result)
function conn:query(query, opt) local function query(self, sql, opt)
local bytes, err, errcode = self:send_query(query) send_query(self, sql)
if not bytes then return nil, err, errcode end return read_result(self, opt)
return self:read_result(opt)
end end
conn.query = protect(query)
function get_collation(self)
local t = query(self, 'select @@collation_connection cl, @@character_set_connection cs')[1]
return t.cl, t.cs
end
conn.get_collation = protect(get_collation)
local stmt = {} local stmt = {}
@ -1324,6 +1343,7 @@ if not ... then --demo
user = 'root', user = 'root',
password = 'abcd12', password = 'abcd12',
database = 'sp', database = 'sp',
collation = 'server'
} }
pp(conn:query'select * from val where val = 1') pp(conn:query'select * from val where val = 1')
local stmt = conn:prepare('select * from val where val = ?') local stmt = conn:prepare('select * from val where val = ?')

View File

@ -51,6 +51,7 @@ The `options` argument is a Lua table holding the following keys:
* `user`: MySQL account name for login. * `user`: MySQL account name for login.
* `password`: MySQL account password for login (in clear text). * `password`: MySQL account password for login (in clear text).
* `collation`: the collation used for the connection (`charset` is implied with this). * `collation`: the collation used for the connection (`charset` is implied with this).
* required if `charset` not given: use `'server'` to get the server's default for the connection.
* `charset`: the character set used for the connection (the default collation for the charset is selected). * `charset`: the character set used for the connection (the default collation for the charset is selected).
* `max_packet_size`: the upper limit for the reply packets sent from the server (default to 1MB). * `max_packet_size`: the upper limit for the reply packets sent from the server (default to 1MB).
* `ssl`: if `true`, then uses SSL to connect to MySQL (default to `false`). * `ssl`: if `true`, then uses SSL to connect to MySQL (default to `false`).