diff options
author | Ted Trask <ttrask01@yahoo.com> | 2014-11-03 19:38:39 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2014-11-03 19:38:39 +0000 |
commit | f882a20966fca59450c79cd909cf007e5e3f00b0 (patch) | |
tree | ca7dbf5126f20ed9d8e121a0be3c6ea4e0e0196f | |
parent | 1b55948641c15d15ca01d09e2fb093b97f3cdb70 (diff) | |
download | acf-lib-f882a20966fca59450c79cd909cf007e5e3f00b0.tar.bz2 acf-lib-f882a20966fca59450c79cd909cf007e5e3f00b0.tar.xz |
Extend db.listcolumns to also return the default values and nullable flag
-rw-r--r-- | db.lua | 21 |
1 files changed, 14 insertions, 7 deletions
@@ -138,14 +138,21 @@ export.listtables = function(dbobject) end export.listcolumns = function(dbobject, table) - local result = {} - if dbobject.engine == mymodule.engine.postgresql then - local col = dbobject.getselectresponse("SELECT a.attname AS field FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = '"..dbobject.escape(table).."' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ORDER BY a.attnum") - for i,c in ipairs(col) do - result[#result+1] = c.field - end + local columns = {} + local defaults = {} + local nullable = {} + -- There is no good way to get default values from pg_attribute, so may as well use information_schema +-- if dbobject.engine == mymodule.engine.postgresql then +-- local col = dbobject.getselectresponse("SELECT a.attname AS field, a.attnotnull FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = '"..dbobject.escape(table).."' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ORDER BY a.attnum") + + local col = dbobject.getselectresponse("SELECT column_name, column_default, is_nullable FROM information_schema.columns WHERE table_name = '"..dbobject.escape(table).."' ORDER BY ordinal_position") + for i,c in ipairs(col) do + columns[#columns+1] = c.column_name + defaults[c.column_name] = c.column_default + nullable[c.column_name] = c.is_nullable == "YES" end - return result + + return columns, defaults, nullable end export.listkeycolumns = function(dbobject, table) |