JDBC driver bug fixes and changes to work in a similar way to other drivers.

This commit is contained in:
thiago 2006-06-20 03:06:09 +00:00
parent 88965d6a4c
commit e4a1779ba2
2 changed files with 135 additions and 19 deletions

View File

@ -64,18 +64,19 @@ public class LuaSQLCursor
{
if (!rs.next())
return null;
table.push();
ResultSetMetaData md = rs.getMetaData();
for (int i = 1; i <= md.getColumnCount(); i++)
int columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++)
{
int type = md.getColumnType(i);
if ("a".equalsIgnoreCase(modeString))
/* if ("a".equalsIgnoreCase(modeString))
L.pushString(md.getColumnName(i));
else
L.pushNumber(i);
L.pushNumber(i);*/
switch (type)
{
@ -107,6 +108,11 @@ public class LuaSQLCursor
L.pushString(rs.getDate(i).toString());
break;
case Types.NULL:
L.pushNil();
break;
default:
@ -114,7 +120,20 @@ public class LuaSQLCursor
break;
}
L.setTable(-3);
if (modeString.contains("a"))
{
L.pushString(md.getColumnName(i));
L.pushValue(-2);
L.setTable(-4);
}
if (modeString.contains("n"))
{
L.pushNumber(i);
L.pushValue(-2);
L.setTable(-4);
}
L.pop(1);
}
L.pop(1);

View File

@ -8,6 +8,8 @@
---------------------------------------------------------------------
local libName = "luasql"
local LUASQL_PREFIX = "LuaSQL: "
local Private = {}
luasql = type(_G[libName]) == "table" and _G[libName] or {}
@ -58,8 +60,13 @@ function Private.createEnv()
return true
end
function env.close()
function env:close()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."environment expected")
end
if isClosed or openConns.n ~= 0 then
return false
end
@ -69,10 +76,16 @@ function Private.createEnv()
return true
end
function env.connect(self, sourcename, username, password)
function env:connect(sourcename, username, password)
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."environment expected")
end
if isClosed then
return nil, "Environment closed."
error(LUASQL_PREFIX.."environment is closed")
--return nil, "Environment closed."
end
if sourcename == nil then
return nil, "Invalid sourcename."
@ -97,6 +110,9 @@ function Private.createEnv()
return Private.createConnection(con, closeConn)
end
-- For compatibility with other drivers
setmetatable(env, {__metatable = LUASQL_PREFIX.."you're not allowed to get this metatable"})
return env
end
@ -121,8 +137,13 @@ function Private.createConnection(conObj, closeFunc)
openCursors.n = openCursors.n - 1
end
function con.close()
function con:close()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."connection expected")
end
if conObj:isClosed() or openCursors.n ~= 0 then
return false
end
@ -133,16 +154,30 @@ function Private.createConnection(conObj, closeFunc)
return true
end
function con.commit()
function con:commit()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."connection expected")
end
local cond, err = pcall(conObj.commit, conObj)
if not cond then
return nil, err
end
end
function con.execute(self, sql)
function con:execute(sql)
if conObj:isClosed() then
error(LUASQL_PREFIX.."connection is closed")
end
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."connection expected")
end
local st = conObj:createStatement()
local cond, isRS = pcall(st.execute, st, sql)
@ -152,7 +187,7 @@ function Private.createConnection(conObj, closeFunc)
local res;
if isRS then
res = Private.createCursor(st:getResultSet(), st, closeCursor)
res = Private.createCursor(st:getResultSet(), st, closeCursor, con)
openCursors[res] = true
openCursors.n = openCursors.n + 1
else
@ -163,15 +198,25 @@ function Private.createConnection(conObj, closeFunc)
return res
end
function con.rollback()
function con:rollback()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."connection expected")
end
local cond, err = pcall(conObj.rollback, conObj)
if not cond then
return nil, err
end
end
function con.setautocommit(self, bool)
function con:setautocommit(bool)
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."connection expected")
end
local cond, err = pcall(conObj.setAutoCommit, conObj, bool)
if not cond then
@ -179,20 +224,32 @@ function Private.createConnection(conObj, closeFunc)
end
end
-- For compatibility with other drivers
setmetatable(con, {__metatable = LUASQL_PREFIX.."you're not allowed to get this metatable"})
return con
end
---------------------------------------------------------------------
-- creates a jdbc cursor
---------------------------------------------------------------------
function Private.createCursor(rs, st, closeFunc)
function Private.createCursor(rs, st, closeFunc, con)
local isClosed = false
local cursor = Private.createJavaCursor(rs)
local res = {}
local names
local types
function res.close()
res._con = con
function res:close()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."cursor expected")
end
if isClosed then
return false
end
@ -206,10 +263,25 @@ function Private.createCursor(rs, st, closeFunc)
return true
end
function res.fetch(self, tb, modestring)
function res:fetch(tb, modestring)
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."cursor expected")
end
if tb == nil or type(tb) ~= "table" then
tb = {}
local cond, tb = pcall(cursor.fetch, cursor, tb, "n")
if not cond then
error(LUASQ_PREFIX.."error fetching result")
end
if tb then
return unpack(tb)
else
return nil
end
end
if modestring == nil or type(modestring) ~= "string" then
@ -224,25 +296,50 @@ function Private.createCursor(rs, st, closeFunc)
return tb
end
function res.getcolnames()
function res:getcolnames()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."cursor expected")
end
if names then
return names
end
local cond, tb = pcall (cursor.getcolnames, cursor)
if not cond then
return cond, tb
end
names = tb
return tb
end
function res.getcoltypes()
function res:getcoltypes()
-- For compatibility with other drivers
if type(self) ~= "table" then
error(LUASQL_PREFIX.."cursor expected")
end
if types then
return types
end
local cond, tb = pcall(cursor.getcoltypes, cursor)
if not cond then
return nil, tb
end
types = tb
return tb
end
-- For compatibility with other drivers
setmetatable(res, {__metatable = LUASQL_PREFIX.."you're not allowed to get this metatable"})
return res
end