2003-04-07 13:41:29 +00:00
|
|
|
#!/usr/local/bin/lua
|
2003-12-01 16:08:38 +00:00
|
|
|
-- See Copyright Notice in license.html
|
2003-04-07 13:41:29 +00:00
|
|
|
|
2003-05-08 21:40:15 +00:00
|
|
|
TOTAL_ROWS = 200
|
|
|
|
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
-- checks for a value and throw an error if it's not the expected.
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
---------------------------------------------------------------------
|
2003-03-26 15:06:20 +00:00
|
|
|
|
2003-04-07 13:41:29 +00:00
|
|
|
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]
|
2003-05-08 21:40:15 +00:00
|
|
|
local datasource = arg[2] or "luasql-test"
|
2003-04-07 13:41:29 +00:00
|
|
|
local username = arg[3] or nil
|
|
|
|
local password = arg[4] or nil
|
|
|
|
|
|
|
|
require (arg[1])
|
|
|
|
assert (luasql, "no luasql table")
|
2003-03-26 15:06:20 +00:00
|
|
|
|
2003-04-07 13:41:29 +00:00
|
|
|
local env, err = luasql[driver] ()
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (env, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
conn, err = env:connect (datasource, username, password)
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (conn, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
conn:execute ("drop table fetch_test")
|
2003-03-26 15:06:20 +00:00
|
|
|
-- Create test table
|
2003-04-07 13:41:29 +00:00
|
|
|
local n, err = conn:execute ([[
|
2003-03-26 15:06:20 +00:00
|
|
|
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
|
2003-04-07 13:41:29 +00:00
|
|
|
local n, err = conn:execute (
|
2003-03-26 15:06:20 +00:00
|
|
|
"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
|
2003-04-07 13:41:29 +00:00
|
|
|
local cur, err = conn:execute ("select * from fetch_test")
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (cur, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
--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()
|
2003-03-26 15:06:20 +00:00
|
|
|
end
|
2003-04-07 13:41:29 +00:00
|
|
|
print ("default: ", os.clock() - t1)
|
|
|
|
assert (cur:close () == 1, "couldn't close cursor object")
|
2003-03-26 15:06:20 +00:00
|
|
|
|
|
|
|
-- using the same table
|
2003-04-07 13:41:29 +00:00
|
|
|
local cur, err = conn:execute ("select * from fetch_test")
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (cur, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
|
|
|
|
t1 = os.clock()
|
2003-03-26 15:06:20 +00:00
|
|
|
local t = {}
|
2003-04-07 13:41:29 +00:00
|
|
|
--for i = 1, cur:numrows() do
|
|
|
|
--t = cur:fetch (t)
|
|
|
|
--end
|
|
|
|
t = cur:fetch(t)
|
|
|
|
while t do
|
|
|
|
t = cur:fetch(t)
|
2003-03-26 15:06:20 +00:00
|
|
|
end
|
2003-04-07 13:41:29 +00:00
|
|
|
print ("same table: ", os.clock() - t1)
|
|
|
|
assert (cur:close () == 1, "couldn't close cursor object")
|
2003-03-26 15:06:20 +00:00
|
|
|
|
|
|
|
-- using the same table with alphanumeric keys
|
2003-04-07 13:41:29 +00:00
|
|
|
local cur, err = conn:execute ("select * from fetch_test")
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (cur, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
|
|
|
|
t1 = os.clock()
|
2003-03-26 15:06:20 +00:00
|
|
|
local t = {}
|
2003-04-07 13:41:29 +00:00
|
|
|
--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")
|
2003-03-26 15:06:20 +00:00
|
|
|
end
|
2003-04-07 13:41:29 +00:00
|
|
|
print ("alpha keys: ", os.clock() - t1)
|
|
|
|
assert (cur:close () == 1, "couldn't close cursor object")
|
2003-03-26 15:06:20 +00:00
|
|
|
|
|
|
|
-- using the same table with numeric and alphanumeric keys
|
2003-04-07 13:41:29 +00:00
|
|
|
local cur, err = conn:execute ("select * from fetch_test")
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (cur, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
--assert (cur:numrows() == TOTAL_ROWS, "wrong number of rows")
|
|
|
|
t1 = os.clock()
|
2003-03-26 15:06:20 +00:00
|
|
|
local t = {}
|
2003-04-07 13:41:29 +00:00
|
|
|
--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")
|
2003-03-26 15:06:20 +00:00
|
|
|
end
|
2003-04-07 13:41:29 +00:00
|
|
|
print ("all keys: ", os.clock() - t1)
|
|
|
|
assert (cur:close () == 1, "couldn't close cursor object")
|
2003-03-26 15:06:20 +00:00
|
|
|
|
|
|
|
-- creating a table
|
2003-04-07 13:41:29 +00:00
|
|
|
local cur, err = conn:execute ("select * from fetch_test")
|
2003-03-26 15:06:20 +00:00
|
|
|
assert (cur, err)
|
2003-04-07 13:41:29 +00:00
|
|
|
--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
|
2003-03-26 15:06:20 +00:00
|
|
|
end
|
2003-04-07 13:41:29 +00:00
|
|
|
print ("new table: ", os.clock() - t1)
|
|
|
|
assert (cur:close () == 1, "couldn't close cursor object")
|
2003-03-26 15:06:20 +00:00
|
|
|
|
2003-04-07 13:41:29 +00:00
|
|
|
assert (conn:close () == 1, "couldn't close connection object")
|
|
|
|
assert (env:close () == 1, "couldn't close environment object")
|