diff options
-rw-r--r-- | lib/dblib.lua | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/lib/dblib.lua b/lib/dblib.lua index 43d719c..b5d889f 100644 --- a/lib/dblib.lua +++ b/lib/dblib.lua @@ -106,12 +106,12 @@ _luadb = { local r, alters, keys = {}, {}, {} for k, v in pairs(ddl) do - local type + local ttype r[k] = "" alters[k] = {} - type = _luadb.get_canon_type(v.type, k) - r[k] = type + ttype = _luadb.get_canon_type(v.type, k) + r[k] = ttype if v.length == nil then -- do nothing @@ -155,7 +155,7 @@ _luadb = { error("Default value for field " .. k .. " isn't usable.") end r[k] = r[k] .. ' DEFAULT "' .. db.sql_escape(v.default) .. '"' - elseif type == "TIMESTAMP" then + elseif ttype == "TIMESTAMP" then r[k] = r[k] .. ' DEFAULT CURRENT_TIMESTAMP' end end @@ -184,10 +184,10 @@ _luadb = { q = q .. "`" .. db.sql_escape(k) .. "` " .. v end - if #keys then + if #keys ~= 0 then q = q .. ", PRIMARY KEY (" first = true - for k, v in ipairs(fields) do + for k, v in ipairs(keys) do if not first then q = q .. ", " else @@ -195,7 +195,7 @@ _luadb = { end q = q .. "`" .. db.sql_escape(v) .. "`" end - q = ")" + q = q .. ")" end q = q .. ") ENGINE=InnoDB;" @@ -258,11 +258,11 @@ _luadb = { for k, v in pairs(fields) do q = nil if not dfields[k] then - q = "ALTER TABLE " .. tname .. " ADD `" .. db.sql_escape(k) .. "` " .. v .. (keys[k] and " PRIMARY KEY") .. ";" + q = "ALTER TABLE " .. tname .. " ADD `" .. db.sql_escape(k) .. "` " .. v .. ";" else local identicals, alters = _luadb.compare_fields(ddl, dfields[k]) if not identicals then - q = "ALTER TABLE " .. tname .. " MODIFY `" .. db.sql_escape(k) .. "` " .. v .. (keys[k] and " PRIMARY KEY") .. ";" + q = "ALTER TABLE " .. tname .. " MODIFY `" .. db.sql_escape(k) .. "` " .. v .. ";" local _, a for _, a in pairs(alters) do a.stmt = string.gsub(a.stmt, "@fieldname@", db.sql_escape(k)) @@ -289,7 +289,7 @@ _luadb = { table.sort(deffered_alters, function(a, b) return a.pri < b.pri end) for k, v in pairs(deffered_alters) do - q = "ALTER TABLE " .. tname .. " " .. a.stmt .. ";" + q = "ALTER TABLE " .. tname .. " " .. v.stmt .. ";" if db:SafeQuery(q) ~= 0 then error("Error altering table " .. tname .. ": " .. db:ErrNO() .. " - " .. db:Error() .. " - query run = " .. q) end @@ -339,6 +339,9 @@ _luadb = { desctype, desclength = field.Type:match "(%w+)%((%d+)%)" end if desctype == nil then + desctype = field.Type:match "(%w+)" + end + if desctype == nil then error("Error parsing type string from database: " .. f .. ": " .. field.Type) end desctype = _luadb.get_canon_type(desctype, f) @@ -360,13 +363,13 @@ _luadb = { if options.PRI and field.Key ~= "PRI" then -- needs to add a primary key here, with a specific ALTER TABLE statement identical = false - table.insert(alters, { stmt = "ADD PRIMARY KEY(@fieldname@)", pri = -2 } ) + table.insert(alters, { stmt = "ADD PRIMARY KEY(@fieldname@)", pri = -1 } ) end if not options.PRI and field.Key == "PRI" then -- needs to drop the primary key here. identical = false - table.insert(alters, { stmt = "DROP PRIMARY KEY", pri = -1 } ) + table.insert(alters, { stmt = "DROP PRIMARY KEY", pri = -2 } ) end if options.UNI and field.Key ~= "UNI" then @@ -497,7 +500,7 @@ _luadb = { local foreign_conds = "1" stmt = stmt .. " FROM " .. t._.tname local found_table, fname, f - for k, v in foreign do + for k, v in pairs(foreign) do stmt = stmt .. ", " .. v._.tname found_table = false for fname, f in pairs(t._.ddl) do @@ -609,7 +612,7 @@ _luadb = { end if type(expr) == "string" or type(expr) == "number" then - return r._.tbl._.tname ".`" .. field .. "` " .. op .. ' "' .. t._.db.sql_escape(expr) .. '"' + return r._.tbl._.tname .. ".`" .. field .. "` " .. op .. ' "' .. r._.tbl._.db.sql_escape(expr) .. '"' elseif type(expr) == "table" then error("Complex queries not handled for now") else |