diff --git a/mysql_client.lua b/mysql_client.lua index 48f741a..d1df19b 100644 --- a/mysql_client.lua +++ b/mysql_client.lua @@ -883,7 +883,8 @@ end --that's how you have to declare a varchar these days. function mysql.char_size(byte_size, collation) charset = collation:match'^[^_]+' - return floor(byte_size / max_char_widths[charset] + .5) + local mcw = max_char_widths[charset] + return mcw and floor(byte_size / mcw + .5) or byte_size end --NOTE: MySQL doesn't give enough metadata to generate a form in a UI, @@ -893,7 +894,7 @@ end local function get_field_packet(buf) local col = {} local _ = get_name(buf) --always "def" - col.schema = get_name(buf) + col.db = get_name(buf) col.table_alias = get_name(buf) col.table = get_name(buf) --name of origin table col.name = get_name(buf) --alias column name @@ -961,6 +962,8 @@ local function get_field_packet(buf) return col end +local function tonum(x) return tonumber(x) end + local function recv_field_packets(self, field_count, field_attrs, to_lua) local fields = {} to_lua = to_lua or self.to_lua @@ -968,7 +971,7 @@ local function recv_field_packets(self, field_count, field_attrs, to_lua) local typ, buf = recv_packet(self) checkp(self, typ == 'DATA', 'bad packet type') local field = get_field_packet(buf) - field.to_lua = to_lua or (field.type == 'number' and tonumber or nil) + field.to_lua = to_lua or (field.type == 'number' and tonum or nil) field.index = i fields[i] = field fields[field.name] = field @@ -1016,8 +1019,8 @@ function mysql.connect(opt) local host = opt.host local port = opt.port or 3306 - mysql.note('connect', 'host=%s:%s user=%s schema=%s', - host, port, opt.user, opt.schema or '') + mysql.note('connect', 'host=%s:%s user=%s db=%s', + host, port, opt.user, opt.db or '') local tcp = opt and opt.tcp or require'sock'.tcp tcp = check_io(self, tcp()) @@ -1080,7 +1083,7 @@ function mysql.connect(opt) buf(23) set_cstring(buf, opt.user or '') set_token(buf, opt.password, scramble) - set_cstring(buf, opt.schema or '') + set_cstring(buf, opt.db or '') send_packet(self, buf) local typ, buf = recv_packet(self) @@ -1099,7 +1102,7 @@ function mysql.connect(opt) end self.charset_is_ascii_superset = self.charset and not mb_charsets[self.charset] - self.schema = opt.schema + self.db = opt.db self.user = opt.user return self @@ -1227,7 +1230,7 @@ local function read_result(self, opt) end local to_lua = col.to_lua if to_lua then - v = to_lua(v) + v = to_lua(v, col) end else v = null_value @@ -1246,7 +1249,7 @@ local function read_result(self, opt) if v ~= nil then local to_lua = col.to_lua if to_lua then - v = to_lua(v) + v = to_lua(v, col) end else v = null_value @@ -1283,13 +1286,13 @@ end conn.get_collation = protect(get_collation) do -local function pass(self, schema, ret, ...) +local function pass(self, db, ret, ...) if not ret then return nil, ... end - self.schema = schema + self.db = db return ret, ... end -function conn:use(schema) - return pass(self, schema, self:query('use `' .. schema .. '`')) +function conn:use(db) + return pass(self, db, self:query('use `' .. db .. '`')) end end diff --git a/mysql_client.md b/mysql_client.md index 9bf6fdc..4cd596c 100644 --- a/mysql_client.md +++ b/mysql_client.md @@ -16,7 +16,7 @@ assert(mysql.connect{ port = 3306, user = 'bar', password = 'baz', - schema = 'foo', + db = 'foo', charset = 'utf8mb4', max_packet_size = 1024 * 1024, }) @@ -50,7 +50,7 @@ The `options` argument is a Lua table holding the following keys: * `port`: server's port (optional, defaults to 3306). * `user`: user name. * `password`: password (optional). - * `schema`: the schema to set as current schema (optional). + * `db`: the database to set as current database (optional). * `collation`: the collation used for the connection (`charset` is implied by this). * use `'server'` to get the server's collation and charset for the connection. * `charset`: the character set used for the connection. @@ -61,7 +61,7 @@ The `options` argument is a Lua table holding the following keys: "ssl disabled on server" is returned. * `ssl_verify`: if `true`, then verifies the validity of the server SSL certificate (default is `false`). - * `to_lua = f(v) -> v` -- custom value converter (defaults to `mysql.to_lua`). + * `to_lua = f(v, col) -> v` -- custom value converter (defaults to `mysql.to_lua`). ### `cn:close() -> true | nil,err` @@ -90,7 +90,7 @@ The `options` arg can contain: (but `#cols` will). * `to_array = true` -- return an array of values for single-column results. * `null_value = val` -- value to use for `null` (defaults to `nil`). - * `to_lua = f(v) -> v` -- custom value converter (defaults to `cn.to_lua`). + * `to_lua = f(v, col) -> v` -- custom value converter (defaults to `cn.to_lua`). * `field_attrs = {name -> attr}` -- extra field attributes. For queries that return a result set, it returns an array of rows. @@ -113,7 +113,7 @@ if MySQL does not return them. __NOTE:__ Decimals with up to 15 digits of precision and 64 bit integers are converted to Lua numbers by default. That limits the useful range of integer types to 15 significant digits. If you have other needs, provide -your own `to_lua` (which you can set atmodule, connection and query level, +your own `to_lua` (which you can set at module, connection and query level, and even per field with `field_attrs`). ### `cn:query(query, [options]) -> res,nil,cols | nil,err,errcode,sqlstate` diff --git a/mysql_client_test.lua b/mysql_client_test.lua index d5da66e..cc939a9 100644 --- a/mysql_client_test.lua +++ b/mysql_client_test.lua @@ -11,7 +11,7 @@ sock.run(function() port = 3307, user = 'root', password = 'root', - schema = 'sp', + db = 'sp', collation = 'server', })