From 2bed2e5c6c502bf2f25ecbb75c37de0019ab2f84 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 10 Jun 2015 15:27:39 +0100 Subject: [PATCH] db.edit --- lib/php/db.php | 53 ++++++++++++++++++++++++++++++++++--------- lib/php/functions.php | 38 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 5dff605a..7f784342 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -6,6 +6,9 @@ from any database *****************************************************************************/ +require_once(dirname(__FILE__).'/functions.php'); +if (!defined('DEBUG')) define('DEBUG',0); + if (!function_exists('err')) { function err($err) { die( is_scalar($err) ? $err : print_r($err) ); @@ -227,7 +230,7 @@ class table { if (@$opt['extras']) $this->add_extras($opt['extras']); - return array(); + return $this->fields(); } function fields() { @@ -311,13 +314,39 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } - function edit($ids) { - if (!is_array($ids)) $ids = array($ids); + function edit($values = null) { + if ($values === null) $values = $_REQUEST; + if (!is_array($values)) $values = array($values); - return $this->fields_keys(); $sql = "SELECT *" . $this->select_extras(); - $sql .= " FROM $this->name".$this->where_criterias(); + $sql .= " FROM $this->name".$this->where_criterias($values); + $sql .= " LIMIT 1"; $this->sql = $sql; + + $this->debug($sql,1); + $st = $this->db->conn->prepare($sql); + $st->execute(); + + $count = 0; + if ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) { + $count ++; + + foreach ($this->fields() as $name => $attr) { + + if (array_key_exists($name,$row)) { + $value = $row[$name]; + } else { + $value = $attr['default']; + } + + echo '' + .'' + .PHP_EOL; + } + } + + $this->row = $row; + return $this; } function debug($msg,$level=0) { @@ -467,13 +496,13 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. echo ''.PHP_EOL; } - function where_criterias() { + function where_criterias($values,$logic='AND') { $having = $where = array(); foreach ($this->fields() as $k => $spec) { // No empty values - $v = @$_REQUEST[$k]; + $v = @$values[$k]; if (strcmp($v,'')==0 or $v=='!' or $v=='~') continue; // Equal / Not Equal @@ -489,7 +518,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. // Text } elseif (preg_match('/text|char|blob/',$spec['type']) - or !preg_match('/^[\d+\.]$/',$v) # text char in search + or !preg_match('/^\d+(\.\d*)?$/',$v) # text criteria ) { if (strtolower($v)=='null') $v = ''; @@ -515,8 +544,10 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. if ($this->db->type == 'mysql' and $spec['extra']) { $having[] = "$k$equal$v"; + } elseif ($this->db->type == 'pgsql' and $spec['extra']) { $where[] = $this->extras[$k]."$equal$v"; + } else { $where[] = "$k$equal$v"; } @@ -524,8 +555,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. } $sql = ''; - if ($where) $sql .= ' WHERE '.join(' '. @$_REQUEST['op'].' ',$where); - if ($having) $sql .= ' HAVING '.join(' '. @$_REQUEST['op'].' ',$having); + if ($where) $sql .= ' WHERE '.join(' '. $logic.' ',$where); + if ($having) $sql .= ' HAVING '.join(' '. $logic.' ',$having); return $sql; } @@ -583,7 +614,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. // Select // $sql = "SELECT *" . $this->select_extras(); - $sql .= " FROM $this->name".$this->where_criterias(); + $sql .= " FROM $this->name".$this->where_criterias($_REQUEST,$_REQUEST['op']); $this->sql = $sql; #$this->debug($sql); $this->debug($sql,1); diff --git a/lib/php/functions.php b/lib/php/functions.php index a7cbc4f9..2abe2103 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -95,5 +95,43 @@ function cmd2str($cmd,$write=null) { } +} + +function argv2request() { +/****************************************************************************** +# NB 14.07.11 - Put argv options into _REQUEST. Return argv without options +******************************************************************************/ + + global $argv; + + $new_argv = array(); + + for ($i=1;$i -- 2.47.3