1
0
mirror of https://github.com/luapower/mysql.git synced 2024-12-29 20:30:28 +01:00

pp update

This commit is contained in:
Cosmin Apreuetsei 2014-05-05 19:21:58 +03:00
parent b8655147f4
commit 20e651d1d6

View File

@ -1,7 +1,7 @@
--mysql test unit (see comments for problems with libmariadb) --mysql test unit (see comments for problems with libmariadb)
local mysql = require'mysql' local mysql = require'mysql'
local glue = require'glue' local glue = require'glue'
local pformat = require'pp'.pformat local pp = require'pp'
local myprint = require'mysql_print' local myprint = require'mysql_print'
local ffi = require'ffi' local ffi = require'ffi'
@ -17,7 +17,7 @@ local function assert_deepequal(t1, t2) --assert the equality of two values
for k,v in pairs(t1) do assert_deepequal(t2[k], v) end for k,v in pairs(t1) do assert_deepequal(t2[k], v) end
for k,v in pairs(t2) do assert_deepequal(t1[k], v) end for k,v in pairs(t2) do assert_deepequal(t1[k], v) end
else else
assert(t1 == t2, pformat(t1) .. ' ~= ' .. pformat(t2)) assert(t1 == t2, pp.format(t1) .. ' ~= ' .. pp.format(t2))
end end
end end
@ -39,9 +39,9 @@ end
--client library --client library
print('mysql.thread_safe() ', '->', pformat(mysql.thread_safe())) print('mysql.thread_safe() ', '->', pp.format(mysql.thread_safe()))
print('mysql.client_info() ', '->', pformat(mysql.client_info())) print('mysql.client_info() ', '->', pp.format(mysql.client_info()))
print('mysql.client_version()', '->', pformat(mysql.client_version())) print('mysql.client_version()', '->', pp.format(mysql.client_version()))
--connections --connections
@ -58,24 +58,24 @@ local t = {
}, },
} }
local conn = mysql.connect(t) local conn = mysql.connect(t)
print('mysql.connect ', pformat(t, ' '), '->', conn) print('mysql.connect ', pp.format(t, ' '), '->', conn)
print('conn:change_user( ', pformat(t.user), ')', conn:change_user(t.user)) print('conn:change_user( ', pp.format(t.user), ')', conn:change_user(t.user))
print('conn:select_db( ', pformat(t.db), ')', conn:select_db(t.db)) print('conn:select_db( ', pp.format(t.db), ')', conn:select_db(t.db))
print('conn:set_multiple_statements(', pformat(true), ')', conn:set_multiple_statements(true)) print('conn:set_multiple_statements(', pp.format(true), ')', conn:set_multiple_statements(true))
print('conn:set_charset( ', pformat('utf8'), ')', conn:set_charset('utf8')) print('conn:set_charset( ', pp.format('utf8'), ')', conn:set_charset('utf8'))
--conn info --conn info
print('conn:charset_name() ', '->', pformat(conn:charset())); assert(conn:charset() == 'utf8') print('conn:charset_name() ', '->', pp.format(conn:charset())); assert(conn:charset() == 'utf8')
print('conn:charset_info() ', '->', pformat(conn:charset_info(), ' ')) --crashes libmariadb print('conn:charset_info() ', '->', pp.format(conn:charset_info(), ' ')) --crashes libmariadb
print('conn:ping() ', '->', pformat(conn:ping())) print('conn:ping() ', '->', pp.format(conn:ping()))
print('conn:thread_id() ', '->', pformat(conn:thread_id())) print('conn:thread_id() ', '->', pp.format(conn:thread_id()))
print('conn:stat() ', '->', pformat(conn:stat())) print('conn:stat() ', '->', pp.format(conn:stat()))
print('conn:server_info() ', '->', pformat(conn:server_info())) print('conn:server_info() ', '->', pp.format(conn:server_info()))
print('conn:host_info() ', '->', pformat(conn:host_info())) print('conn:host_info() ', '->', pp.format(conn:host_info()))
print('conn:server_version() ', '->', pformat(conn:server_version())) print('conn:server_version() ', '->', pp.format(conn:server_version()))
print('conn:proto_info() ', '->', pformat(conn:proto_info())) print('conn:proto_info() ', '->', pp.format(conn:proto_info()))
print('conn:ssl_cipher() ', '->', pformat(conn:ssl_cipher())) print('conn:ssl_cipher() ', '->', pp.format(conn:ssl_cipher()))
--transactions --transactions
@ -310,9 +310,9 @@ local bind_types = {
--queries --queries
local esc = "'escape me'" local esc = "'escape me'"
print('conn:escape( ', pformat(esc), ')', '->', pformat(conn:escape(esc))) print('conn:escape( ', pp.format(esc), ')', '->', pp.format(conn:escape(esc)))
local q1 = 'drop table if exists binding_test' local q1 = 'drop table if exists binding_test'
print('conn:query( ', pformat(q1), ')', conn:query(q1)) print('conn:query( ', pp.format(q1), ')', conn:query(q1))
local field_defs = '' local field_defs = ''
for i,field in ipairs(test_fields) do for i,field in ipairs(test_fields) do
@ -336,16 +336,16 @@ select * from binding_test;
--query info --query info
print('conn:field_count() ', '->', pformat(conn:field_count())) print('conn:field_count() ', '->', pp.format(conn:field_count()))
print('conn:affected_rows() ', '->', pformat(conn:affected_rows())) print('conn:affected_rows() ', '->', pp.format(conn:affected_rows()))
print('conn:insert_id() ', '->', conn:insert_id()) print('conn:insert_id() ', '->', conn:insert_id())
print('conn:errno() ', '->', pformat(conn:errno())) print('conn:errno() ', '->', pp.format(conn:errno()))
print('conn:sqlstate() ', '->', pformat(conn:sqlstate())) print('conn:sqlstate() ', '->', pp.format(conn:sqlstate()))
print('conn:warning_count() ', '->', pformat(conn:warning_count())) print('conn:warning_count() ', '->', pp.format(conn:warning_count()))
print('conn:info() ', '->', pformat(conn:info())) print('conn:info() ', '->', pp.format(conn:info()))
for i=1,3 do for i=1,3 do
print('conn:more_results() ', '->', pformat(conn:more_results())); assert(conn:more_results()) print('conn:more_results() ', '->', pp.format(conn:more_results())); assert(conn:more_results())
print('conn:next_result() ', '->', pformat(conn:next_result())) print('conn:next_result() ', '->', pp.format(conn:next_result()))
end end
assert(not conn:more_results()) assert(not conn:more_results())
@ -353,15 +353,15 @@ assert(not conn:more_results())
local res = conn:store_result() --TODO: local res = conn:use_result() local res = conn:store_result() --TODO: local res = conn:use_result()
print('conn:store_result() ', '->', res) print('conn:store_result() ', '->', res)
print('res:row_count() ', '->', pformat(res:row_count())); assert(res:row_count() == 2) print('res:row_count() ', '->', pp.format(res:row_count())); assert(res:row_count() == 2)
print('res:field_count() ', '->', pformat(res:field_count())); assert(res:field_count() == #test_fields) print('res:field_count() ', '->', pp.format(res:field_count())); assert(res:field_count() == #test_fields)
print('res:eof() ', '->', pformat(res:eof())); assert(res:eof() == true) print('res:eof() ', '->', pp.format(res:eof())); assert(res:eof() == true)
print('res:fields() ', '->') print_fields(res:fields()) print('res:fields() ', '->') print_fields(res:fields())
print('res:field_info(1) ', '->', pformat(res:field_info(1))) print('res:field_info(1) ', '->', pp.format(res:field_info(1)))
--first row: fetch as array and test values --first row: fetch as array and test values
local row = assert(res:fetch'n') local row = assert(res:fetch'n')
print("res:fetch'n' ", '->', pformat(row)) print("res:fetch'n' ", '->', pp.format(row))
for i,field in res:fields() do for i,field in res:fields() do
assert_deepequal(row[i], test_values[field.name]) assert_deepequal(row[i], test_values[field.name])
end end
@ -369,7 +369,7 @@ end
--first row again: fetch as assoc. array and test values --first row again: fetch as assoc. array and test values
print('res:seek(1) ', '->', res:seek(1)) print('res:seek(1) ', '->', res:seek(1))
local row = assert(res:fetch'a') local row = assert(res:fetch'a')
print("res:fetch'a' ", '->', pformat(row)) print("res:fetch'a' ", '->', pp.format(row))
for i,field in res:fields() do for i,field in res:fields() do
assert_deepequal(row[field.name], test_values[field.name]) assert_deepequal(row[field.name], test_values[field.name])
end end
@ -384,7 +384,7 @@ local function pack(_, ...)
return t return t
end end
local row = pack(res:fetch()) local row = pack(res:fetch())
print("res:fetch() ", '-> packed: ', pformat(row)) print("res:fetch() ", '-> packed: ', pp.format(row))
for i,field in res:fields() do for i,field in res:fields() do
assert_deepequal(row[i], test_values[field.name]) assert_deepequal(row[i], test_values[field.name])
end end
@ -398,13 +398,13 @@ print()
print(fit('', 4, 'right') .. ' ' .. fit('field', 20) .. fit('unparsed', 40) .. ' ' .. 'parsed') print(fit('', 4, 'right') .. ' ' .. fit('field', 20) .. fit('unparsed', 40) .. ' ' .. 'parsed')
print(('-'):rep(4 + 2 + 20 + 40 + 40)) print(('-'):rep(4 + 2 + 20 + 40 + 40))
for i,field in res:fields() do for i,field in res:fields() do
print(fit(tostring(i), 4, 'right') .. ' ' .. fit(field.name, 20) .. fit(pformat(row_s[i]), 40) .. ' ' .. pformat(row[i])) print(fit(tostring(i), 4, 'right') .. ' ' .. fit(field.name, 20) .. fit(pp.format(row_s[i]), 40) .. ' ' .. pp.format(row[i]))
end end
print() print()
--second row: all nulls --second row: all nulls
local row = assert(res:fetch'n') local row = assert(res:fetch'n')
print("res:fetch'n' ", '->', pformat(row)) print("res:fetch'n' ", '->', pp.format(row))
assert(#row == 0) assert(#row == 0)
for i=1,res:field_count() do for i=1,res:field_count() do
assert(row[i] == nil) assert(row[i] == nil)
@ -433,8 +433,8 @@ print('res:list_processes() ', '->'); print_result(conn:list_processes())
local query = 'select '.. table.concat(test_fields, ', ')..' from binding_test' local query = 'select '.. table.concat(test_fields, ', ')..' from binding_test'
local stmt = conn:prepare(query) local stmt = conn:prepare(query)
print('conn:prepare( ', pformat(query), ')', '->', stmt) print('conn:prepare( ', pp.format(query), ')', '->', stmt)
print('stmt:field_count() ', '->', pformat(stmt:field_count())); assert(stmt:field_count() == #test_fields) print('stmt:field_count() ', '->', pp.format(stmt:field_count())); assert(stmt:field_count() == #test_fields)
--we can get the fields and their types before execution so we can create create our bind structures. --we can get the fields and their types before execution so we can create create our bind structures.
--max. length is not computed though, but length is, so we can use that. --max. length is not computed though, but length is, so we can use that.
print('stmt:fields() ', '->'); print_fields(stmt:fields()) print('stmt:fields() ', '->'); print_fields(stmt:fields())
@ -446,7 +446,7 @@ for i,field in ipairs(test_fields) do
btypes[i] = bind_types[field] btypes[i] = bind_types[field]
end end
local bind = stmt:bind_result(btypes) local bind = stmt:bind_result(btypes)
print('stmt:bind_result( ', pformat(btypes), ')', '->', pformat(bind)) print('stmt:bind_result( ', pp.format(btypes), ')', '->', pp.format(bind))
--execution and loading --execution and loading
@ -455,10 +455,10 @@ print('stmt:store_result() ', stmt:store_result())
--result info --result info
print('stmt:row_count() ', '->', pformat(stmt:row_count())) print('stmt:row_count() ', '->', pp.format(stmt:row_count()))
print('stmt:affected_rows() ', '->', pformat(stmt:affected_rows())) print('stmt:affected_rows() ', '->', pp.format(stmt:affected_rows()))
print('stmt:insert_id() ', '->', pformat(stmt:insert_id())) print('stmt:insert_id() ', '->', pp.format(stmt:insert_id()))
print('stmt:sqlstate() ', '->', pformat(stmt:sqlstate())) print('stmt:sqlstate() ', '->', pp.format(stmt:sqlstate()))
--result data (different API since we don't get a result object) --result data (different API since we don't get a result object)
@ -466,9 +466,9 @@ print('stmt:fetch() ', stmt:fetch())
print('stmt:fields() ', '->'); print_fields(stmt:fields()) print('stmt:fields() ', '->'); print_fields(stmt:fields())
print('bind:is_truncated(1) ', '->', pformat(bind:is_truncated(1))); assert(bind:is_truncated(1) == false) print('bind:is_truncated(1) ', '->', pp.format(bind:is_truncated(1))); assert(bind:is_truncated(1) == false)
print('bind:is_null(1) ', '->', pformat(bind:is_null(1))); assert(bind:is_null(1) == false) print('bind:is_null(1) ', '->', pp.format(bind:is_null(1))); assert(bind:is_null(1) == false)
print('bind:get(1) ', '->', pformat(bind:get(1))); assert(bind:get(1) == test_values.fdecimal) print('bind:get(1) ', '->', pp.format(bind:get(1))); assert(bind:get(1) == test_values.fdecimal)
local i = field_indices.fdate local i = field_indices.fdate
print('bind:get_date( ', i, ')', '->', bind:get_date(i)); assert_deepequal({bind:get_date(i)}, {2013, 10, 5}) print('bind:get_date( ', i, ')', '->', bind:get_date(i)); assert_deepequal({bind:get_date(i)}, {2013, 10, 5})
local i = field_indices.ftime local i = field_indices.ftime
@ -488,7 +488,7 @@ local function print_bind_buffer(bind)
assert_deepequal(v, test_values[field]) assert_deepequal(v, test_values[field])
assert(bind:is_truncated(i) == false) assert(bind:is_truncated(i) == false)
assert(bind:is_null(i) == (test_values[field] == nil)) assert(bind:is_null(i) == (test_values[field] == nil))
print(fit(tostring(i), 4, 'right') .. ' ' .. fit(field, 20) .. pformat(v)) print(fit(tostring(i), 4, 'right') .. ' ' .. fit(field, 20) .. pp.format(v))
end end
print() print()
end end
@ -496,7 +496,7 @@ print_bind_buffer(bind)
print('stmt:free_result() ', stmt:free_result()) print('stmt:free_result() ', stmt:free_result())
--local next_result = stmt:next_result() --local next_result = stmt:next_result()
--print('stmt:next_result() ', '->', pformat(next_result)); assert(next_result == false) --print('stmt:next_result() ', '->', pp.format(next_result)); assert(next_result == false)
print('stmt:reset() ', stmt:reset()) print('stmt:reset() ', stmt:reset())
print('stmt:close() ', stmt:close()) print('stmt:close() ', stmt:close())
@ -506,11 +506,11 @@ print('stmt:close() ', stmt:close())
for i,field in ipairs(test_fields) do for i,field in ipairs(test_fields) do
local query = 'select * from binding_test where '..field..' = ?' local query = 'select * from binding_test where '..field..' = ?'
local stmt = conn:prepare(query) local stmt = conn:prepare(query)
print('conn:prepare( ', pformat(query), ')') print('conn:prepare( ', pp.format(query), ')')
local param_bind_def = {bind_types[field]} local param_bind_def = {bind_types[field]}
local bind = stmt:bind_params(param_bind_def) local bind = stmt:bind_params(param_bind_def)
print('stmt:bind_params ', pformat(param_bind_def)) print('stmt:bind_params ', pp.format(param_bind_def))
local function exec() local function exec()
print('stmt:exec() ', stmt:exec()) print('stmt:exec() ', stmt:exec())
@ -521,7 +521,7 @@ for i,field in ipairs(test_fields) do
local v = test_values[field] local v = test_values[field]
if v ~= nil then if v ~= nil then
print('bind:set( ', 1, pformat(v), ')'); bind:set(1, v); exec() print('bind:set( ', 1, pp.format(v), ')'); bind:set(1, v); exec()
if field:find'date' or field:find'time' then if field:find'date' or field:find'time' then
print('bind:set_date( ', 1, v.year, v.month, v.day, v.hour, v.min, v.sec, v.frac, ')') print('bind:set_date( ', 1, v.year, v.month, v.day, v.hour, v.min, v.sec, v.frac, ')')
@ -545,7 +545,7 @@ print_bind_buffer(bind)
stmt:close() stmt:close()
local q = 'drop table binding_test' local q = 'drop table binding_test'
print('conn:query( ', pformat(q), ')', conn:query(q)) print('conn:query( ', pp.format(q), ')', conn:query(q))
print('conn:commit() ', conn:commit()) print('conn:commit() ', conn:commit())
print('conn:close() ', conn:close()) print('conn:close() ', conn:close())