]> git.nbdom.net Git - nb.git/commitdiff
db.php key and default
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 9 Jun 2015 13:48:41 +0000 (14:48 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 9 Jun 2015 13:48:41 +0000 (14:48 +0100)
lib/php/db.php

index 1311a5da8122ab4bd73bcd102022bbe30ea109d8..a264ac16a8ec2d654133681edeb736716a45b2bb 100644 (file)
@@ -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;
+        }
+
       }
 
     }