From f882a20966fca59450c79cd909cf007e5e3f00b0 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Mon, 3 Nov 2014 19:38:39 +0000 Subject: Extend db.listcolumns to also return the default values and nullable flag --- db.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'db.lua') diff --git a/db.lua b/db.lua index 2de44c3..16a6581 100644 --- a/db.lua +++ b/db.lua @@ -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) -- cgit v1.2.3