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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#!/usr/local/bin/lua
-- See Copyright Notice in license.html
TOTAL_ROWS = 200
---------------------------------------------------------------------
-- checks for a value and throw an error if it is invalid.
---------------------------------------------------------------------
function assert2 (expected, value, msg)
if not msg then
msg = ''
else
msg = msg..'\n'
end
return assert (value == expected,
msg.."wrong value (["..tostring(value).."] instead of "..
tostring(expected)..")")
end
---------------------------------------------------------------------
-- object test.
---------------------------------------------------------------------
function test_object (obj, objmethods)
-- checking object type.
assert2 ("userdata", type(obj), "incorrect object type")
-- trying to get metatable.
assert2 ("LuaSQL: you're not allowed to get this metatable",
getmetatable(obj), "error permitting access to object's metatable")
-- trying to set metatable.
assert2 (false, pcall (setmetatable, ENV, {}))
-- checking existence of object's methods.
for i = 1, table.getn (objmethods) do
local method = objmethods[i]
assert2 ("function", type(obj[method]))
end
return obj
end
ENV_OK = function (obj)
return test_object (obj, { "close", "connect", })
end
CONN_OK = function (obj)
return test_object (obj, { "close", "commit", "execute", "rollback", "setautocommit", })
end
CUR_OK = function (obj)
return test_object (obj, { "close", "fetch", "getcolnames", "getcoltypes", })
end
---------------------------------------------------------------------
-- Main
---------------------------------------------------------------------
if type(arg[1]) ~= "string" then
print (string.format ("Usage %s <driver> [<data source> [, <user> [, <password>]]]", arg[0]))
os.exit()
end
local driver = arg[1]
local datasource = arg[2] or "luasql-test"
local username = arg[3] or nil
local password = arg[4] or nil
require (arg[1])
assert (luasql, "no luasql table")
local env, err = luasql[driver] ()
assert (env, err)
conn, err = env:connect (datasource, username, password)
assert (conn, err)
conn:execute ("drop table fetch_test")
-- Create test table
local n, err = conn:execute ([[
create table fetch_test (
f1 varchar(30),
f2 varchar(30),
f3 varchar(30),
f4 varchar(30),
f5 varchar(30),
f6 varchar(30),
f7 varchar(30),
f8 varchar(30)
)]])
assert (n, err)
assert (type(n) == "number", "couldn't create fetch_test table")
-- Insert rows
for i = 1, TOTAL_ROWS do
local n, err = conn:execute (
"insert into fetch_test values ('f1','f2','f3','f4','f5','f6','f7','f8')")
assert (n, err)
assert (type (n) == "number", "couldn't insert rows")
end
print ("table created; rows inserted")
-- default
local cur, err = conn:execute ("select * from fetch_test")
assert (cur, err)
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
local t1 = os.clock()
--for i = 1, cur:numrows() do
--local f1,f2,f3,f4,f5,f6,f7,f8 = cur:fetch()
--end
local f1,f2,f3,f4,f5,f6,f7,f8 = cur:fetch()
while f1 do
f1,f2,f3,f4,f5,f6,f7,f8 = cur:fetch()
end
print ("default: ", os.clock() - t1)
assert (cur:close () == 1, "couldn't close cursor object")
-- using the same table
local cur, err = conn:execute ("select * from fetch_test")
assert (cur, err)
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
t1 = os.clock()
local t = {}
--for i = 1, cur:numrows() do
--t = cur:fetch (t)
--end
t = cur:fetch(t)
while t do
t = cur:fetch(t)
end
print ("same table: ", os.clock() - t1)
assert (cur:close () == 1, "couldn't close cursor object")
-- using the same table with alphanumeric keys
local cur, err = conn:execute ("select * from fetch_test")
assert (cur, err)
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
t1 = os.clock()
local t = {}
--for i = 1, cur:numrows() do
--t = cur:fetch (t,"a")
--end
t = cur:fetch (t, "a")
while t do
t = cur:fetch (t, "a")
end
print ("alpha keys: ", os.clock() - t1)
assert (cur:close () == 1, "couldn't close cursor object")
-- using the same table with numeric and alphanumeric keys
local cur, err = conn:execute ("select * from fetch_test")
assert (cur, err)
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
t1 = os.clock()
local t = {}
--for i = 1, cur:numrows() do
--t = cur:fetch (t,"an")
--end
t = cur:fetch (t, "an")
while t do
t = cur:fetch (t, "an")
end
print ("all keys: ", os.clock() - t1)
assert (cur:close () == 1, "couldn't close cursor object")
-- creating a table
local cur, err = conn:execute ("select * from fetch_test")
assert (cur, err)
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
t1 = os.clock()
--for i = 1, cur:numrows() do
--local t = cur:fetch{}
--end
while cur:fetch{} do
end
print ("new table: ", os.clock() - t1)
assert (cur:close () == 1, "couldn't close cursor object")
assert (conn:close () == 1, "couldn't close connection object")
assert (env:close () == 1, "couldn't close environment object")
|