mirror of
https://github.com/luapower/mysql.git
synced 2025-07-14 14:30:16 +02:00
Compare commits
5 Commits
f3bd1020b2
...
720d2a3955
Author | SHA1 | Date | |
---|---|---|---|
720d2a3955 | |||
afadb60a14 | |||
bf6b50d966 | |||
cc85cffef9 | |||
1164e0fc20 |
25
mysql.lua
25
mysql.lua
@ -1,11 +1,11 @@
|
||||
|
||||
--mySQL client library ffi binding.
|
||||
--MySQL client library ffi binding.
|
||||
--Written by Cosmin Apreutesei. Public domain.
|
||||
|
||||
--Supports mysql Connector/C 6.1.
|
||||
--Based on mySQL 5.7 manual.
|
||||
--Supports MySQL Connector/C 6.1.
|
||||
--Based on MySQL 5.7 manual.
|
||||
|
||||
if not ... then require'mysql_test' end
|
||||
if not ... then require'mysql_test'; return end
|
||||
|
||||
local ffi = require'ffi'
|
||||
local bit = require'bit'
|
||||
@ -15,12 +15,12 @@ local C
|
||||
local M = {}
|
||||
|
||||
--select a mysql client library implementation.
|
||||
local function config(lib)
|
||||
local function bind(lib)
|
||||
if not C then
|
||||
if not lib or lib == 'mysql' then
|
||||
C = ffi.load(ffi.abi'win' and 'libmysql' or 'mysqlclient')
|
||||
elseif lib == 'mariadb' then
|
||||
C = ffi.load(ffi.abi'win' and 'libmariadb' or 'mariadb')
|
||||
C = ffi.load'mariadb'
|
||||
elseif type(lib) == 'string' then
|
||||
C = ffi.load(lib)
|
||||
else
|
||||
@ -31,7 +31,7 @@ local function config(lib)
|
||||
return M
|
||||
end
|
||||
|
||||
M.config = config
|
||||
M.bind = bind
|
||||
|
||||
--we compare NULL pointers against NULL instead of nil for compatibility with luaffi.
|
||||
local NULL = ffi.cast('void*', nil)
|
||||
@ -72,17 +72,17 @@ end
|
||||
--client library info
|
||||
|
||||
function M.thread_safe()
|
||||
config()
|
||||
bind()
|
||||
return C.mysql_thread_safe() == 1
|
||||
end
|
||||
|
||||
function M.client_info()
|
||||
config()
|
||||
bind()
|
||||
return cstring(C.mysql_get_client_info())
|
||||
end
|
||||
|
||||
function M.client_version()
|
||||
config()
|
||||
bind()
|
||||
return tonumber(C.mysql_get_client_version())
|
||||
end
|
||||
|
||||
@ -126,7 +126,7 @@ local option_encoders = {
|
||||
}
|
||||
|
||||
function M.connect(t, ...)
|
||||
config()
|
||||
bind()
|
||||
local host, user, pass, db, charset, port
|
||||
local unix_socket, flags, options, attrs
|
||||
local key, cert, ca, capath, cipher
|
||||
@ -665,6 +665,7 @@ function res.fetch(res, mode, t)
|
||||
local field_count = C.mysql_num_fields(res)
|
||||
local fields = fetch_fields and C.mysql_fetch_fields(res)
|
||||
local row = fetch_row(res, numeric, assoc, decode, field_count, fields, t or {})
|
||||
if not row then return nil end
|
||||
if packed then
|
||||
return row
|
||||
else
|
||||
@ -680,7 +681,7 @@ function res.rows(res, mode, t)
|
||||
res:seek(1)
|
||||
return function()
|
||||
local row = fetch_row(res, numeric, assoc, decode, field_count, fields, t or {})
|
||||
if not row then return end
|
||||
if not row then return nil end
|
||||
i = i + 1
|
||||
if packed then
|
||||
return i, row
|
||||
|
7
mysql.md
7
mysql.md
@ -1,18 +1,15 @@
|
||||
---
|
||||
tagline: mysql database client
|
||||
---
|
||||
|
||||
## `local mysql = require'mysql'`
|
||||
|
||||
A complete, lightweight ffi binding of the mysql client library.
|
||||
|
||||
> NOTE: binaries are in separate packages [libmysql] and [libmariadb].
|
||||
Works with both libmysql and [libmariadb].
|
||||
|
||||
## Summary
|
||||
|
||||
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
|
||||
**[Initialization]**
|
||||
`mysql.config(['mysql'|'mariadb'|libname|clib]) -> mysql`
|
||||
`mysql.bind(['mysql'|'mariadb'|libname|clib]) -> mysql`
|
||||
**[Connections]**
|
||||
`mysql.connect(host, [user], [pass], [db], [charset], [port]) -> conn` connect to a mysql server
|
||||
`mysql.connect(options_t) -> conn` connect to a mysql server
|
||||
|
@ -1,5 +1,7 @@
|
||||
--mysql table pretty printing
|
||||
|
||||
if not ... then require'mysql_test'; return end
|
||||
|
||||
local function ellipsis(s,n)
|
||||
return #s > n and (s:sub(1,n-3) .. '...') or s
|
||||
end
|
||||
@ -132,8 +134,6 @@ local function print_statement(stmt, minsize, print)
|
||||
print_table(fields, rows, aligns, minsize, print)
|
||||
end
|
||||
|
||||
if not ... then require'mysql_test' end
|
||||
|
||||
return {
|
||||
fit = fit,
|
||||
format_cell = format_cell,
|
||||
|
@ -6,6 +6,8 @@ local pp = require'pp'
|
||||
local myprint = require'mysql_print'
|
||||
local ffi = require'ffi'
|
||||
|
||||
mysql.bind'mariadb'
|
||||
|
||||
--helpers
|
||||
|
||||
local print_table = myprint.table
|
||||
|
Reference in New Issue
Block a user