-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.lua
executable file
·146 lines (141 loc) · 3.8 KB
/
test.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
#!/usr/bin/env lua
--[[
assume the MySQL server is running on localhost:3306
and user "test" with password "mypas" is granted any
privileges on database "test";
feel free to modify real_connect()'s parameters to
fit the sample code for your very case;
--]]
local mysql = require("mysql");
assert(type(mysql) == "table", "MySQL module loading error");
function main()
-- run a constructor
local sql, e = mysql.Mysql:new(true);
assert(sql ~= nil, e);
--[[ uncomment the following block to limit the Address Space
-- load [sg]etrlimit functions
for _, v in ipairs { "setrlimit", "getrlimit" } do
_G[v] = dl.load("libc.so.6", v, dl.ffi_type_sint,
{ dl.ffi_type_sint, dl.ffi_type_pointer }
);
assert(_G[v] ~= nil, "Unable to load " .. v);
end;
-- declare a rlimit structure
local struct = dl.Dlffi_t:new(
"rlimit",
{ dl.ffi_type_ulong, dl.ffi_type_ulong }
);
assert(struct ~= nil, "Cannot initialize type");
local buf = dl.dlffi_Pointer(dl.sizeof(struct["rlimit"]), true);
assert(buf ~= nil, "buffer malloc() failed");
dl.type_element(buf, struct["rlimit"], 1, 5267460);
dl.type_element(buf, struct["rlimit"], 2, 5267460);
local r = setrlimit(9, buf);
assert(tonumber(r) == 0, "setrlimit() failed");
local r = getrlimit(9, buf);
assert(tonumber(r) == 0, "getrlimit() failed");
print("soft:", dl.type_element(buf, struct["rlimit"], 1));
print("hard:", dl.type_element(buf, struct["rlimit"], 2));
--]]
-- connect to a test server
local con = sql:real_connect(
"localhost",
"test",
"mypas",
"test",
3306,
mysql.dl.NULL,
0
);
assert(con ~= mysql.dl.NULL, "unable to connect");
-- get prepared to use transactions
local r, e = sql:autocommit(0);
assert(r == 0, e and e or "mysql_auto_commit() failed");
-- create a `sample` table
local que = [=[
CREATE TABLE IF NOT EXISTS `sample` (
`id` INT (11) auto_increment,
`name` TINYTEXT NOT NULL,
`misc` MEDIUMTEXT NOT NULL,
PRIMARY KEY(`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
COMMENT='a sample DB'
]=]
local r, e = sql:query(que);
assert((r == nil) and (e == nil),
"Unable to create `sample` table: " .. tostring(e));
local r, e = sql:query("BEGIN");
assert((r == nil) and (e == nil),
"Transaction is not started: " .. tostring(e));
-- truncate it in case it has been here
local que = [=[TRUNCATE `sample`]=];
local r, e = sql:query(que);
assert(
(r == nil) and (e == nil),
"TRUNCATE `sample` failed with: " .. tostring(e)
);
-- fill the `sample` table with 1000 records
for i = 1, 1000, 1 do
-- tag `name` with \0 to emulate binary data
local que = [=[
INSERT INTO `sample` SET
`name` = "]=] ..
--[[ nil asserting is omitted here
in sake of simplicity
--]]
sql:real_escape_string(
"id_" ..
tostring(i) ..
"\0"
) ..
[=[", `misc` = "]=] ..
--[[ nil asserting is omitted here
in sake of simplicity
--]]
sql:real_escape_string(
"comment for id_" ..
tostring(i)
) ..
[=["
]=];
local r, e = sql:query(que);
assert(
(r == nil) and (e == nil),
"INSERT #" .. tostring(i) ..
" failed with " .. tostring(e)
);
collectgarbage();
end;
local r, e = sql:query("COMMIT");
assert((r == nil) and (e == nil),
"Transaction is not committed: " .. tostring(e));
-- read the data back
local que = [=[
SELECT `id`, `name`, `misc`
FROM `sample`
ORDER BY `id`
]=];
local res = sql:query(que, true);
assert(
res ~= nil,
"Failed to read `sample` table with " ..
tostring(r)
);
-- read all records
repeat
local r = res:fetch_assoc();
--[[
mysql.fetch_assoc() will return nil on error or
if no more records are available
--]]
if not r then break end;
-- replace \0 with something printable
r["name"] = string.gsub(r["name"], "%z", "\\0");
print(r["id"], r["name"], r["misc"]);
collectgarbage();
until false;
end;
main();