]> git.nbdom.net Git - nb.git/commitdiff
db
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 22 Jul 2015 23:22:07 +0000 (00:22 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 22 Jul 2015 23:22:07 +0000 (00:22 +0100)
lib/php/db.php
lib/php/db/table.php
lib/php/page.php
lib/php/phpinfo.php [new file with mode: 0644]

index 631ed66e39de4c4f6eb39b6652c5460f1174cae3..de32a92909b15ea549168bae90f8ae4b6bb22938 100644 (file)
@@ -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',''
   .'<input type="submit" class="button" name="edit" value="Add"/>'
   .'<a class="button" href="#" onclick="return nb.form_clear(this.parentElement)">X</a>'
@@ -20,11 +19,16 @@ if (!defined('DB_HTML_BUTTON_SUBMIT')) define('DB_HTML_BUTTON_SUBMIT','<input ty
 
 class db {
 
-  public $tables = array();
+  public $pdo;
   public $conn;
+  public $options;
+  public $username = null;
+  public $password = null;
+  public $pdo_error = PDO::ERRMODE_EXCEPTION; # See: http://php.net/manual/en/pdo.error-handling.php
   public $title;
   public $name;
   public $type;
+  public $tables = array();
   public $help_criterias = array(
     ' * or % for wildcar',
     ' ! to negate',
@@ -34,21 +38,26 @@ class db {
 
   function __construct($db) {
 
-    $db = is_scalar($db) ? array('conn' => $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');
index 56c3dbcc7ca5c7d1559ec1b8ca29a93786187875..28e7500d017924d5c19dbc22df9063e1a1f679f7 100644 (file)
@@ -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()");
     }
index 4d8975989159d9a406c16418670646107f32bd67..e21f2fdf66d0a70882a5b2243e7b2575ade272c4 100644 (file)
@@ -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 (file)
index 0000000..04913db
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+#echo $_SERVER['SERVER_ADDR '];
+phpinfo();
+?>