From 0c1fc7effd1503574af54a39340c50449f167d40 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 4 Apr 2016 01:55:17 +0100 Subject: [PATCH] Bed --- etc/dbs.yaml | 15 +++++- lib/php/cache.php | 2 +- lib/php/config.php | 7 +-- lib/php/db.php | 18 ++++--- lib/php/db/table.php | 98 +++++++++++++++++++--------------- lib/php/db/types/mysql.php | 2 + lib/php/db/types/pgsql.php | 2 + lib/php/db/types/sqlite.php | 10 ++-- lib/php/nb.php | 5 +- lib/php/out.php | 2 +- lib/php/page.php | 102 ++++++++++++++++++++---------------- 11 files changed, 157 insertions(+), 106 deletions(-) diff --git a/etc/dbs.yaml b/etc/dbs.yaml index d3429978..ffbe6204 100644 --- a/etc/dbs.yaml +++ b/etc/dbs.yaml @@ -16,6 +16,7 @@ nb: _import: - _nico - _mysql + #- _rent puppetdb: #pdo: 'pgsql:host=big;dbname=puppetdb;user=puppetdb;password=' @@ -47,13 +48,20 @@ izi: title: Mysql Izi on Admin default_table: process host: admin.izideal.vpn - user: izideal + #user: izideal + name: izi _import: - _nico - _mysql + tables: + site: + replace: + idlang: lang.id izidev: + host: big.cascais.loc title: Mysql Izi on Big + #user: nico _import: - izi @@ -81,6 +89,10 @@ rt: rent: order: 1 pdo: 'sqlite:/opt/rent/rent.db' + _import: + - _rent + +_rent: title: 'Rent' notice: 'Micro foncier 4BE' default_table: 'rent' @@ -110,6 +122,7 @@ rent: rent_year: type: "float(8,2)" extra: "rent * (1 + ( strftime('%m',end)+12*strftime('%Y',end) ) - ( strftime('%m',start)+12*strftime('%Y',start) ))" + #extra: "rent * (1 + ( date_format(end,'%m')+12*date_format(end,'%Y') ) - ( date_format(start,'%m')+12*date_format(start,'%Y') ))" #revision: (SELECT GROUP_CONCAT(''||substr(id,0,instr(id,'.'))||'',' ') FROM template) # TODEL - NB 10.01.16 diff --git a/lib/php/cache.php b/lib/php/cache.php index e304bf6a..0b29e34c 100644 --- a/lib/php/cache.php +++ b/lib/php/cache.php @@ -1,5 +1,5 @@ diff --git a/lib/php/db.php b/lib/php/db.php index a8841a40..2b1c58e4 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -135,8 +135,6 @@ class Db extends nb { if (!$this->type(null)) $this->bye("unknow type = `".$this->type."`"); # Connect -# NB 28.03.16 if (preg_match('/^sqlite:(.*)$/',$this->pdo,$m) and !is_readable($m[1])) { -# NB 28.03.16 $this->bye("Can't connect to database `".$m[1]."` type=$this->type",false); if ($this->type('use_path') and !is_readable($this->name)) { $this->bye("Can't read database file `".$this->name."` type=`$this->type`",false); @@ -337,7 +335,7 @@ class Db extends nb { # NB 28.03.16 } function help($tables=null) { - if (!empty($_SERVER['DOCUMENT_ROOT'])) header('Content-type: text/plain'); + if (!self::php_cli()) header('Content-type: text/plain'); if ($tables === null) $tables = array_keys($this->tables()); $tables = join('',ar_map('" ".$a."\n"',$tables)); @@ -368,9 +366,7 @@ EOF; } function sql_name($value) { - if ($this->type == 'mysql' - or $this->type == 'sqlite' - ) return '`'.$value.'`'; + if ($q=$this->type('quote_name')) return $q.$value.$q; return '"'.$value.'"'; } @@ -658,6 +654,16 @@ EOF; ."-- Pdo : ".$this->pdo_info()."\n" #."-- Host : ".$this->host."\n" ; + +# NB 03.04.16 if ($insert) { +# NB 03.04.16 if ($sql = $this->type('post_connect')) { +# NB 03.04.16 if (is_scalar($sql)) $sql = array($sql); +# NB 03.04.16 foreach ($sql as $s) { +# NB 03.04.16 echo "$sql;\n"; +# NB 03.04.16 } +# NB 03.04.16 } +# NB 03.04.16 } + foreach ($this->tables() as $t) { if ($insert) { echo "\n-- Table: ".$t->name."\n"; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index a954cae5..1fc26d67 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -2,6 +2,7 @@ require_once(dirname(__FILE__).'/../db.php'); require_once(dirname(__FILE__).'/../db/field.php'); require_once(dirname(__FILE__).'/../out.php'); +#$a = array('a','b','c'); $b = array('c','a'); debug(array_diff($a,$b)); define('TABLE_INDENT',NB_EOL ? "\t" : ""); define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t"); @@ -26,7 +27,7 @@ Class Table extends nb { public $created; # hidden, sort, ... fields - public static $params = array( 'table', 'limit', 'debug', 'action'); + public static $params = array( 'db', 'table', 'limit', 'debug', 'action'); public $orderby = null; public $fields; @@ -84,6 +85,7 @@ Class Table extends nb { if (stripos($this->name,'SELECT ')===0) { #$temp = '_'.substr(md5($this->name),0,6); $temp = DB_TABLE_QUERY_NAME; + #bye("CREATE TEMPORARY VIEW $temp AS $this->name"); $this->db()->conn->query("CREATE TEMPORARY TABLE $temp AS $this->name"); $this->name = $temp; @@ -128,7 +130,7 @@ Class Table extends nb { $sql = str_replace('',$this->name,$this->db()->type('table.sql',true)); $this->sql = $this->db()->row($sql); - # Noise before CREATE from mysql + # Noise before CREATE (ex: from mysql) $this->sql = preg_replace("/^\w+\s+(CREATE)/i",'$1',$this->sql); # Remove comments @@ -219,8 +221,8 @@ Class Table extends nb { ; } - if (($v = $this->p('db'))) $url[] = 'db='.$this->p('db'); - return $url ? '?table='.$this->p('table').$sep.join($sep,$url) : ''; + foreach (self::$params as $p) if ($v=self::p($p)) $url[] = $p.'='.$v; + return $url ? '?'.$sep.join($sep,$url) : ''; } public function fields_keys(&$others=array()) { @@ -239,6 +241,17 @@ Class Table extends nb { } + public static function form_hidden($ignore=array()) { + $h = ''; + foreach (array_diff(self::$params,$ignore) as $p) { + if ($v=self::p($p)) { + if (self::p('debug')) $h .= ""; + $h .= ''; + } + } + return $h; + } + public function html_edit($values = null) { if ($values === null) $values = $this->p(); if (!is_array($values)) $values = array($values); @@ -257,7 +270,7 @@ Class Table extends nb { $sql .= " FROM ".$this->sql_name().$where; # NB 28.03.16 $this->sql = $sql; - $this->debug($sql,1); + $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1); $st = $this->db()->conn->prepare($sql); $st->execute(); @@ -276,7 +289,7 @@ Class Table extends nb { } } - echo ''.NB_EOL; + echo ''.NB_EOL; # < fields echo '' .'
' @@ -284,11 +297,12 @@ Class Table extends nb { .'' .'' .'
'.NB_EOL - .'' - .'' - .'' - .'' - .'' + ; + + echo '' + .'' + .self::form_hidden(array('action','HTTP_REFERER')) + .(!empty($_SERVER['HTTP_REFERER']) ? '' : '') .''.NB_EOL; } @@ -298,7 +312,7 @@ Class Table extends nb { $params = array(); $fields = ($this->p('action') == 'delete') ? array() : $this->fields(); - foreach (array_merge( self::$params, array_keys($fields) ) as $f) { + foreach ( array_diff( array_merge(self::$params,array_keys($fields)), array('action') ) as $f) { if (strcmp($this->p($f,''),'')==0) continue; $params[$f] = $this->p($f); @@ -315,7 +329,7 @@ Class Table extends nb { } - if ($this->p('db')) $params['db'] = $this->p('db'); +# NB 03.04.16 if ($this->p('db')) $params['db'] = $this->p('db'); $flat = array(); foreach ($params as $k=>$v) { $flat[] = $k.'='.urlencode($v); } return $flat ? '?'. join('&',$flat) : ''; @@ -469,10 +483,12 @@ Class Table extends nb { $k = "COALESCE($k,".$this->db()->quote('').")"; } - if ($this->db()->type == 'mysql' and $field->extra) { + # having, denorm, EMPTY + $extra_where = (string)$this->db()->type('extra_where'); + if ($extra_where == 'having' and $field->extra) { $having[] = "$k$equal$v"; - } elseif ($this->db()->type == 'pgsql' and $field->extra) { + } elseif ($extra_where == 'denorm' and $field->extra) { $where[] = $this->extras[$k]->sql_name()."$equal$v"; } else { @@ -532,9 +548,10 @@ Class Table extends nb { // Select // $where = $this->where_criterias($this->p(),$this->p('op')); - $select_count = ( (false and $where and $this->db()->type =='mysql') ? " SQL_CALC_FOUND_ROWS" : ""); + $select_count = ( $where ? $this->db()->type('select_count') : null ); + if (empty($select_count)) $select_count = array('',''); - $sql = "SELECT$select_count *" . $this->select_extras(); + $sql = "SELECT ".trim("$select_count[0] *"). $this->select_extras(); $sql .= " FROM ".$this->sql_name(); $sql .= $where; @@ -551,7 +568,7 @@ Class Table extends nb { // Get results // # NB 28.03.16 $this->sql = $sql; - $this->debug($sql,1); + $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1); $st = $this->db()->conn->prepare($sql); $st->execute(); @@ -628,16 +645,18 @@ Class Table extends nb { // Tot // if (!$where and !$limit) { - debug("Table.rows(): Not using count(*)",1); + debug("Table.rows(): Not using count(*), use $count",1); $query = $this->db()->conn->query("SELECT $count"); - } elseif ($select_count) { - $query = $this->db()->conn->query('SELECT FOUND_ROWS()'); + } elseif ($select_count[1]) { + debug("Table.rows(): Using $select_count[1]",1); + $query = $this->db()->conn->query($select_count[1]); } elseif ($where) { $sql_count = $sql; $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count); $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count); + debug("Table.rows(): Using $sql_count",1); $query = $this->db()->conn->query($sql_count); } else { @@ -913,32 +932,33 @@ Class Table extends nb { } public function insert($hvalues) { - $fields = $values = array(); + $sql_names = $fields = $values = array(); #var_dump($hvalues); foreach ($this->fields() as $name => $field) { if (!isset($hvalues[$name])) continue; if ($field->key and $field->autoincrement()) continue; - $fields[$field->sql_name()] = $field; + #$fields[$field->sql_name()] = $field; + $fields[$name] = $field; + $sql_names[$name] = $field->sql_name(); $values[] = $hvalues[$name]; } #bye($values); $sql = - 'INSERT INTO '. $this->sql_name() . ' (' . join(',',array_keys($fields)).')' + 'INSERT INTO '. $this->sql_name() . ' (' . join(',',array_values($sql_names)).')' #.' VALUES (' . join(',',$values).')' .' VALUES (' . join(',',ar_map('":$a"',array_keys($fields))) . ')' ; if (!($query = $this->db()->conn->prepare($sql))) { - err('PDO::errorInfo(): ' .join(' ', $this->db()->conn->errorInfo()) .NB_EOL); + $this->err_sql($sql); return false; } foreach ($fields as $name => $field) $field->bindParam($query,$hvalues[$name],":$name"); -#debug(array($sql,$values)); if (!($execute = $query->execute())) { - err('PDO::errorInfo(): ' .join(' ', $this->db()->conn->errorInfo()) .NB_EOL); + $this->err_sql($sql); return false; } @@ -1033,7 +1053,7 @@ Class Table extends nb { } $sql = 'DELETE FROM ' . $this->sql_name() . $where; - bye($sql); + #bye($sql); return $this->db()->exec($sql); } @@ -1124,6 +1144,7 @@ Class Table extends nb { } public function html_rows_top() { + if (self::p('replace') === '0') return ''; $html = ''; if (!empty($this->replace)) { @@ -1157,7 +1178,7 @@ Class Table extends nb { } - public static function err_sql($sql) { + public function err_sql($sql) { $err = $this->db()->conn->errorInfo(); $err[] = $sql; self::bye(join(' | ',$err)); @@ -1167,22 +1188,15 @@ Class Table extends nb { $r = '