From: Nicolas Boisselier Date: Tue, 9 Jun 2015 13:48:41 +0000 (+0100) Subject: db.php key and default X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=ce14ae42b1e9d1466b67898b90e3df346e63aeeb;p=nb.git db.php key and default --- diff --git a/lib/php/db.php b/lib/php/db.php index 1311a5da..a264ac16 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -230,13 +230,20 @@ class table { } function fields() { + if (!$this->fields) { if ($this->db->type == 'sqlite') { $sql = "PRAGMA table_info('$this->name')"; } elseif ($this->db->type == 'pgsql') { - $sql = "SELECT a.attname as name, pg_catalog.format_type(a.atttypid, a.atttypmod) as type, case a.attnotnull when 't' then 1 else 0 end as notnull FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname='$this->name' AND pg_catalog.pg_table_is_visible(c.oid) ) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum"; +$sql = "SELECT +a.attname AS name, +pg_catalog.format_type(a.atttypid, a.atttypmod) AS type, +CASE a.attnotnull WHEN 't' then 1 ELSE 0 END AS notnull, +(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS default, +(SELECT 1 FROM pg_index i WHERE a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) AND i.indrelid = 'tickets'::regclass AND i.indisprimary) as pk +FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname='tickets' AND pg_catalog.pg_table_is_visible(c.oid) ) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum"; } elseif ($this->db->type == 'mysql') { $sql = "SHOW COLUMNS FROM `$this->name`"; @@ -265,6 +272,16 @@ class table { } + foreach (array('dflt_value') as $f) { + if (!array_key_exists($f,$row)) continue; + $this->fields[$row['name']]['default'] = $row[$f]; + } + + foreach (array('pk','Key') as $f) { + if (!array_key_exists($f,$row)) continue; + $this->fields[$row['name']]['key'] = preg_match('/^1|yes|t/i',$row[$f]) ? 1 : 0; + } + } }