]> git.nbdom.net Git - nb.git/commitdiff
pg_count protect table with quote
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 23 Jul 2015 10:32:36 +0000 (11:32 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 23 Jul 2015 10:32:36 +0000 (11:32 +0100)
etc/profile.d/pg.sh
lib/php/db/table.php

index ad0550494642dbff79cc06755cd8a00f11c447e7..8ee920ce931ba03cbf865a7b341247cd5913edf5 100644 (file)
@@ -16,7 +16,7 @@ pg_count() {
 
       (
         for tb in $tables; do
-          echo "SELECT '$tb',count(*) FROM $tb;";
+          echo "SELECT '$tb',count(*) FROM \"$tb\";";
         done
       ) | psql -At $db | awk -F'|' '{printf "%-'$length's %9d\n",$1,$2}';
 
index 56c3dbcc7ca5c7d1559ec1b8ca29a93786187875..b1f19224c6e9b1bb8318660fa30d6be644e77aa3 100644 (file)
@@ -3,13 +3,13 @@ require_once(dirname(__FILE__).'/../functions.php');
 require_once(dirname(__FILE__).'/../db.php');
 
 if (empty($_SERVER['DOCUMENT_ROOT'])) {
-  $_REQUEST['db'] = 'rent'; $name = 'tenant';
-  $_REQUEST['db'] = 'rt'; $name = 'classes';
+  $_REQUEST['db'] = 'rent'; $name = 'test';
+  #$_REQUEST['db'] = 'rt'; $name = 'classes';
   require_once('/opt/rent/www/config/db.php');
-  #$db = new db('mysql:host=big;port=3306;dbname=rent;user=nico;password=');
   print_r($Db->tables());
   $Table = $Db->table($name);
-  print_r($Table->sql());
+  print_r($Table->fields());
+  #print_r($Table->sql());
 }
 
 if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
@@ -60,6 +60,7 @@ class table {
    *
    */
   function sql() {
+    if (isset($this->sql)) return $this->sql;
     if ($this->db->type == 'sqlite') {
       $sql = "SELECT sql FROM sqlite_master WHERE name='$this->name'";
     } elseif ($this->db->type == 'mysql') {
@@ -69,7 +70,8 @@ class table {
     } else {
       err('table.sql(): Unknow db type: '.$this->db->type);
     }
-    return $this->db->query($sql);
+    $this->sql = $this->db->query($sql);
+    return $this->sql;
   }
 
   /*
@@ -92,7 +94,7 @@ class table {
 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 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 pg_default,
 (SELECT 1 FROM pg_index i WHERE a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) AND i.indrelid = '$this->name'::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='$this->name' AND pg_catalog.pg_table_is_visible(c.oid) ) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum";
 
@@ -119,7 +121,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
           'autoincrement' => false,
         );
 
-        $this->fields[$row['name']]['type'] = $row['type'];
+        $this->fields[$row['name']]['type'] = strtolower($row['type']);
 
         if (isset($row['notnull'])) {
           $this->fields[$row['name']]['null'] = $row['notnull'] == '0' ? 1 : 0;
@@ -129,6 +131,32 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
         }
 
+        # sqlite autoincrement
+        if ($this->db->type == 'sqlite' and preg_match('/[\(,]'.$row['name'].' [^\),]+ AUTOINCREMENT/',$this->sql())) {
+          $this->fields[$row['name']]['autoincrement'] = true;
+        }
+
+        # mysql autoincrement
+        if (isset($row['Extra']) and $row['Extra'] == 'auto_increment') {
+          $this->fields[$row['name']]['autoincrement'] = true;
+        }
+
+        # Postgres autoincrement / default
+        if (isset($row['pg_default'])) {
+
+          if (preg_match('/^nextval\(/',$row['pg_default'])) {
+            $this->fields[$row['name']]['autoincrement'] = true;
+
+          } elseif (preg_match("/^''/",$row['pg_default'])) {
+            $this->fields[$row['name']]['default'] = '';
+
+          } else {
+            $this->fields[$row['name']]['default'] = $row['pg_default'];
+
+          }
+        }
+
+        # sqlite default
         foreach (array('dflt_value') as $f) {
           if (!isset($row[$f])) continue;
           $this->fields[$row['name']]['default'] = $row[$f];