2003-04-07 13:41:29 +00:00
|
|
|
#!/usr/local/bin/lua
|
|
|
|
|
2003-03-26 15:06:20 +00:00
|
|
|
TOTAL_ROWS = 8000
|
|
|
|
|
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]
|
|
|
|
local datasouce = 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")
|
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")
|