From: Nicolas Boisselier Date: Thu, 23 Jul 2015 10:32:36 +0000 (+0100) Subject: pg_count protect table with quote X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=50aeef15ecd5dc777814f1bd1a3c81eb26edd1c6;p=nb.git pg_count protect table with quote --- diff --git a/etc/profile.d/pg.sh b/etc/profile.d/pg.sh index ad055049..8ee920ce 100644 --- a/etc/profile.d/pg.sh +++ b/etc/profile.d/pg.sh @@ -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}'; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 56c3dbcc..b1f19224 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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];