From 4739940df6009a35066eb0cc84b7c6a6bdc14ece Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 23 Jul 2015 00:22:07 +0100 Subject: [PATCH] db --- lib/php/db.php | 37 +++++++++++++++++++++++++------------ lib/php/db/table.php | 8 +++++--- lib/php/page.php | 4 +++- lib/php/phpinfo.php | 4 ++++ 4 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 lib/php/phpinfo.php diff --git a/lib/php/db.php b/lib/php/db.php index 631ed66e..de32a929 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -9,7 +9,6 @@ require_once(dirname(__FILE__).'/functions.php'); require_once(dirname(__FILE__).'/db/table.php'); require_once(dirname(__FILE__).'/db/field.php'); -if (!defined('DB_ERR_PRINT')) define('DB_ERR_PRINT',false); if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS','' .'' .'X' @@ -20,11 +19,16 @@ if (!defined('DB_HTML_BUTTON_SUBMIT')) define('DB_HTML_BUTTON_SUBMIT',' $db) : $db; + $db = is_scalar($db) ? array('pdo' => $db) : $db; foreach ($db as $k=>$v) $this->$k = $v; - $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$db['conn'])); + $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo)); + #bye($this->pdo); + #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m); - #bye($db['conn']); try { - $this->conn = new PDO($db['conn']); - if (DB_ERR_PRINT) $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $this->conn = new PDO($this->pdo,$this->username,$this->password,$options); + if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error); + } catch (PDOException $e) { + err($e->getMessage(), "Db.new()"); return false; + } - #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$db['conn']); - if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$db['conn'],$m)) { + #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$this->pdo); + if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$this->pdo,$m)) { $this->name = $m[1]; } if (empty($this->title)) $this->title = prettyText($this->name); @@ -87,8 +96,11 @@ class db { } */ - function query($sql) { - return $this->query2v($sql); + function exec($sql) { + return $this->conn->exec($sql); + } + + function query($sql,$o='') { return $this->conn->query($sql); } @@ -157,7 +169,7 @@ class db { } function help($tables=null) { - header('Content-type: text/plain'); + if (!empty($_SERVER['DOCUMENT_ROOT'])) header('Content-type: text/plain'); if ($tables === null) $tables = $this->tables(); $tables = join('',ar_map('" ".$a."\n"',$tables)); @@ -179,6 +191,7 @@ EOF; } function print_header($type) { + if (empty($_SERVER['DOCUMENT_ROOT'])) return null; if ($this->p('format')=='csv') { header('Content-type: text/csv'); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 56c3dbcc..28e7500d 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -2,9 +2,11 @@ require_once(dirname(__FILE__).'/../functions.php'); require_once(dirname(__FILE__).'/../db.php'); -if (empty($_SERVER['DOCUMENT_ROOT'])) { +if (false and empty($_SERVER['DOCUMENT_ROOT'])) { $_REQUEST['db'] = 'rent'; $name = 'tenant'; $_REQUEST['db'] = 'rt'; $name = 'classes'; + $_REQUEST['db'] = 'mysql'; $name = 'addr'; + $_REQUEST['db'] = 'izi'; $name = 'zone'; 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()); @@ -69,7 +71,7 @@ class table { } else { err('table.sql(): Unknow db type: '.$this->db->type); } - return $this->db->query($sql); + return $this->db->conn->query($sql,PDO::FETCH_COLUMN,0); } /* @@ -803,7 +805,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $sql = 'DELETE FROM ' . $this->sql_name() . $where; debug($sql); try { - $this->db->query($sql); + $this->db->conn->query($sql); } catch (PDOException $e) { return err($e->getMessage(), "Table.delete()"); } diff --git a/lib/php/page.php b/lib/php/page.php index 4d897598..e21f2fdf 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -228,6 +228,7 @@ class Page { function header($name,$value=null) { + if (empty($_SERVER['DOCUMENT_ROOT'])) return false; if ($value === null) return get_header($name); header("$name: $value"); @@ -237,7 +238,8 @@ class Page { #header('Content-type: ' . $this->content_type); $c = strtoupper ( $this->charset ); - header('Content-type: '.$this->content_type . ($c ? "; charset=$c" : "")); + $this->header('Content-type: ',$this->content_type . ($c ? "; charset=$c" : "")); +// NB 22.07.15 header('Content-type: '.$this->content_type . ($c ? "; charset=$c" : "")); return true; } diff --git a/lib/php/phpinfo.php b/lib/php/phpinfo.php new file mode 100644 index 00000000..04913db7 --- /dev/null +++ b/lib/php/phpinfo.php @@ -0,0 +1,4 @@ + -- 2.47.3