diff --git a/mysql_client.lua b/mysql_client.lua index ccdfd9d..23822b3 100644 --- a/mysql_client.lua +++ b/mysql_client.lua @@ -772,7 +772,7 @@ local function set_str(buf, s) end local function set_token(buf, password, scramble) - if password == '' then + if not password or password == '' then return '' end local stage1 = sha1(password) @@ -931,9 +931,6 @@ function mysql.connect(opt) self.max_packet_size = opt.max_packet_size or 16 * 1024 * 1024 --16 MB local ok, err - local database = opt.database or '' - local user = opt.user or '' - local collation = 0 --default if opt.collation ~= 'server' then if opt.collation then @@ -988,9 +985,9 @@ function mysql.connect(opt) set_u32(buf, self.max_packet_size) set_u8(buf, collation) buf(23) - set_cstring(buf, user) - set_token(buf, opt.password or '', scramble) - set_cstring(buf, database) + set_cstring(buf, opt.user or '') + set_token(buf, opt.password, scramble) + set_cstring(buf, opt.schema or '') send_packet(self, buf) local typ, buf = recv_packet(self) @@ -1008,6 +1005,8 @@ function mysql.connect(opt) end self.charset_is_ascii_superset = self.charset and not mb_charsets[self.charset] + self.schema = opt.schema + self.user = opt.user return self end @@ -1336,14 +1335,14 @@ if not ... then --demo local sock = require'sock' local pp = require'pp' sock.run(function() - local conn = mysql.connect{ + local conn = assert(mysql.connect{ host = '127.0.0.1', port = 3307, user = 'root', password = 'abcd12', - database = 'sp', + schema = 'sp', collation = 'server', - } + }) print(conn.charset, conn.collation) pp(conn:query'select * from val where val = 1') local stmt = conn:prepare('select * from val where val = ?') diff --git a/mysql_client.md b/mysql_client.md index fa5240b..d3a4c6b 100644 --- a/mysql_client.md +++ b/mysql_client.md @@ -12,9 +12,9 @@ local mysql = require'mysql_client' assert(mysql.connect{ host = '127.0.0.1', port = 3306, - database = 'foo', user = 'bar', password = 'baz', + schema = 'foo', charset = 'utf8mb4', max_packet_size = 1024 * 1024, }) @@ -44,20 +44,21 @@ Connect to a MySQL server. The `options` argument is a Lua table holding the following keys: - * `host`: the host name for the MySQL server. - * `port`: the port that the MySQL server is listening on. Default to 3306. - * `path`: the path of the unix socket file listened by the MySQL server. - * `database`: the MySQL database name. - * `user`: MySQL account name for login. - * `password`: MySQL account password for login (in clear text). - * `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). - * `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`). + * `host`: server's IP address (required). + * `port`: server's port (optional, defaults to 3306). + * `user`: user name. + * `password`: password (optional). + * `schema`: the schema to set as current schema (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. + * if `collation` not set, the default collation for the charset is selected. + * `max_packet_size`: the upper limit for the reply packets sent from the server (defaults to 16 MB). + * `ssl`: if `true`, then uses SSL to connect to MySQL (defaults to `false`). If the server does not have SSL support (or just disabled), the error string - "ssl disabled on server" will be returned. - * `ssl_verify`: if `true`, then verifies the validity of the server SSL certificate (default to `false`). + "ssl disabled on server" is returned. + * `ssl_verify`: if `true`, then verifies the validity of the server SSL + certificate (default is `false`). ### `cn:close() -> 1 | nil,err` @@ -140,7 +141,8 @@ The MySQL server version string. ### `cn:quote(s) -> s` -Quote literal string to be used in queries. +Quote literal string to be used in queries. Quoting only works if current +collation is known (ses `collation` arg on `connect()`). ### Multiple result set support