From 6483fe5b6bf025c5388d22699a3d423a6ace0f19 Mon Sep 17 00:00:00 2001
From: Pixel <pixel@nobis-crew.org>
Date: Mon, 6 Oct 2008 11:14:14 -0700
Subject: Changing slightly the primary key syntax, allowing multiple columns.

---
 lib/dblib.lua | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/lib/dblib.lua b/lib/dblib.lua
index b514375..b361cd1 100644
--- a/lib/dblib.lua
+++ b/lib/dblib.lua
@@ -99,11 +99,11 @@ _luadb = {
 
     generate_fields = function(db, ddl)
         local k, v
-        local r, r2 = {}, {}
+        local r, alters, keys = {}, {}, {}
         
         for k, v in pairs(ddl) do
             r[k] = ""
-            r2[k] = {}
+            alters[k] = {}
             
             r[k] = _luadb.get_canon_type(v.type, k)
             
@@ -132,11 +132,12 @@ _luadb = {
             end
             
             if options.PRI then
-                r[k] = r[k] .. " PRIMARY KEY"
+                table.insert(keys, k)
+                keys[k] = true
             end
             
             if options.UNIQ then
-                table.insert(r2[k], "ADD UNIQUE (`@fieldname@`)")
+                table.insert(alters[k], "ADD UNIQUE (`@fieldname@`)")
             end
 
             if options.AUTO then
@@ -150,11 +151,11 @@ _luadb = {
                 r[k] = r[k] .. ' DEFAULT "' .. db.sql_escape(v.default) .. '"'
             end
         end
-        return r, r2
+        return r, alters, keys
     end,
     
     opentable = function(db, tablename, ddl)
-        local fields, alters = _luadb.generate_fields(db, ddl)
+        local fields, alters, keys = _luadb.generate_fields(db, ddl)
         local tname = "`" .. db._.prefix .. db.sql_escape(tablename) .. "`"
         local operations = 0
         
@@ -175,7 +176,21 @@ _luadb = {
                     q = q .. "`" .. db.sql_escape(k) .. "` " .. v
                 end
                 
-                q = q .. ");"
+                if #keys then
+                    q = q .. ", PRIMARY KEY ("
+                    first = true
+                    for k, v in ipairs(fields) do
+                        if not first then
+                            q = q .. ", "
+                        else
+                            first = false
+                        end
+                        q = q .. "`" .. db.sql_escape(v) .. "`"
+                    end
+                    q = ")"
+                end
+                
+                q = q .. ") ENGINE=InnoDB;"
                 
                 if db:SafeQuery(q) ~= 0 then
                     error("Error creating table " .. tname .. ": " .. db:ErrNO() .. " - " .. db:Error() .. " - query run = " .. q)
@@ -235,11 +250,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 .. ";"
+                    q = "ALTER TABLE " .. tname .. " ADD `" .. db.sql_escape(k) .. "` " .. v .. (keys[k] and " PRIMARY KEY") .. ";"
                 else
                     local identicals, alters = _luadb.compare_fields(ddl, dfields[k])
                     if not identicals then
-                        q = "ALTER TABLE " .. tname .. " MODIFY `" .. db.sql_escape(k) .. "` " .. v .. ";"
+                        q = "ALTER TABLE " .. tname .. " MODIFY `" .. db.sql_escape(k) .. "` " .. v .. (keys[k] and " PRIMARY KEY") .. ";"
                         local _, a
                         for _, a in pairs(alters) do
                             a.stmt = string.gsub(a.stmt, "@fieldname@", db.sql_escape(k))
-- 
cgit v1.2.3