]> git.nbdom.net Git - nb.git/commitdiff
change db.yaml and move function to nb
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 10 Jan 2016 01:05:32 +0000 (01:05 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 10 Jan 2016 01:05:32 +0000 (01:05 +0000)
lib/php/db.php
lib/php/db/table.php
lib/php/functions.php
lib/php/nb.php

index 6fc708ad83894ae3c3bc733f131c5ccdcdc0fec1..7019970190249c466042fd77f17f4061501646d5 100644 (file)
@@ -195,14 +195,15 @@ class db extends nb {
   }
 
   function table($name) {
-    return new Table($name,array('db'=>$this));
-    # TODEL - NB 18.08.15
-    if (!$this->table) $this->table = new Table($name,array('db'=>$this));
-    return $this->table;
+    $opt = array('db' => $this);
+    if (isset($this->tables) and array_key_exists($name,$this->tables)) {
+      foreach ($this->tables[$name] as $k=>$v) { $opt[$k] = $v; }
+    }
+    return new Table($name,array_merge($opt));
   }
 
   function tables() {
-    if ($this->tables) return $this->tables;
+    if (isset($this->_tables) and $this->_tables) return $this->_tables;
 
     if ($this->type == 'sqlite') {
       $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name";
@@ -220,10 +221,10 @@ class db extends nb {
 
     $rows = $this->conn->query($sql);
     foreach ($rows as $row) {
-      $this->tables[] = current($row);
+      $this->_tables[] = current($row);
     }
 
-    return $this->tables;
+    return $this->_tables;
 
   }
 
index 462d62b17a9c8d5c2a4166b58e2451bf7f85cab0..138193ddeb6cf2ce7f9e395029e47a05b22b44e3 100644 (file)
@@ -32,9 +32,13 @@ class table extends nb {
     'limit',
     'debug',
   );
+  public $order_by = null;
 
   function __construct($name,$opt=array()) {
 
+    foreach ($opt as $k => $v) { $this->$k = $v; }
+    #unset($opt['db']); bye($opt);
+
     // Connection
     if (isset($opt['db'])) {
       $this->db = $opt['db'];
@@ -67,10 +71,11 @@ class table extends nb {
     }
 
     // Sort
-    if (
-      isset($this->db->sort)
-      and isset($this->db->sort[$this->name])
-    ) $this->db->pset('sort',$this->db->sort[$this->name]);
+# NB 10.01.16     if (
+# NB 10.01.16       isset($this->db->sort)
+# NB 10.01.16       and isset($this->db->sort[$this->name])
+# NB 10.01.16     ) $this->db->pset('sort',$this->db->sort[$this->name]);
+    if ($this->order_by) $this->pset('sort',$this->order_by);
 
     return $this->fields();
   }
@@ -593,7 +598,6 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     $sql .= " FROM ".$this->sql_name();
 
     $this->sql = $sql = $sql . $where;
-    $this->debug($sql,1);
 
     if ($this->p('sort')) $sql .= ' ORDER BY '.$this->p('sort');
 
@@ -607,6 +611,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     //
     // Get results
     //
+    $this->debug($sql,1);
     $st = $this->db->conn->prepare($sql);
     $st->execute();
 
index 1103b57972a4d8490d58f92c3987b9b2fe83441e..97dc5cc8690ba7d81a7274665cae1fa5c96ff237 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+require_once(dirname(__FILE__).'/nb.php');
 /*
 Vim:
 
@@ -186,68 +187,7 @@ 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<count($argv);$i++) {
-
-    if (preg_match('/^([\w_\-\.]+)=(.+)\s*$/',$argv[$i],$f)) {
-      $k = $f[1];
-      $v = $f[2];
-
-      // Array
-      if (preg_match('/^(.*)\[\]$/',$k,$p)) {
-        #bye($p);
-        if (($_REQUEST[$p[1]]==NULL) or !in_array($v,$_REQUEST[$p[1]])) $_REQUEST[$p[1]][] = $v;
-
-      } else {
-         $_REQUEST[$k] = $v;
-
-      }
-
-    } else {
-
-      if (preg_match('/^-(h|-?help)$/',$argv[$i])) $_REQUEST['help'] = 1;
-      else $new_argv[] = $argv[$i];
-
-    }
-
-  }
-
-  return $new_argv;
-
-}
-
-function benchmark($function=null,$limit=1000000) {
-  global $_benchmark;
-
-  if ($_benchmark === null) {
-    $_benchmark = array('.' => microtime(true));
-  }
-
-  if ($function === null) {
-    $prev = $_benchmark['.'];
-    echo "Iteration: $limit\n";
-    foreach ($_benchmark as $lib => $sec) {
-      if ($lib === '.') continue;
-      printf("%-30s %s\n",$lib,($sec-$prev));
-      $prev = $sec;
-    }
-    return;
-  }
-
-  for ($i=0; $i<$limit; $i++) {
-    $function();
-  }
-
-  $_benchmark[$function] = microtime(true);
-}
+function argv2request() { return nb::argv2request(); }
 
 function html_select_array($data,$opt=array()) {
 
index e64b49adab81ddca727b68abf372a7775d4550a8..5a62f15856011e7801e39fcba3d4421425851688 100644 (file)
@@ -93,6 +93,75 @@ class nb {
     return null;
   }
 
+  /*
+   * Function: argv2request
+   * Set $_REQUEST from argv
+   */
+  static function argv2request() {
+
+    global $argv;
+
+    $new_argv = array();
+   
+    for ($i=1;$i<count($argv);$i++) {
+
+      if (preg_match('/^([\w_\-\.]+)=(.+)\s*$/',$argv[$i],$f)) {
+        $k = $f[1];
+        $v = $f[2];
+
+        // Array
+        if (preg_match('/^(.*)\[\]$/',$k,$p)) {
+          #bye($p);
+          if (($_REQUEST[$p[1]]==NULL) or !in_array($v,$_REQUEST[$p[1]])) $_REQUEST[$p[1]][] = $v;
+
+        } else {
+           $_REQUEST[$k] = $v;
+
+        }
+
+      } else {
+
+        if (preg_match('/^-(h|-?help)$/',$argv[$i])) $_REQUEST['help'] = 1;
+        else $new_argv[] = $argv[$i];
+
+      }
+
+    }
+
+    return $new_argv;
+
+  }
+
+  /*
+   * Function: benchmark
+   * Call a function several times
+   * Usage: benchmark("f1"); benchmark("f2"); benchmark();
+   */
+  static function benchmark($function=null,$limit=1000000) {
+    global $_benchmark;
+
+    if ($_benchmark === null) {
+      $_benchmark = array('.' => microtime(true));
+    }
+
+    if ($function === null) {
+      $prev = $_benchmark['.'];
+      echo "Iteration: $limit\n";
+      foreach ($_benchmark as $lib => $sec) {
+        if ($lib === '.') continue;
+        printf("%-30s %s\n",$lib,($sec-$prev));
+        $prev = $sec;
+      }
+      return;
+    }
+
+    for ($i=0; $i<$limit; $i++) {
+      $function();
+    }
+
+    $_benchmark[$function] = microtime(true);
+  }
+
 }
 #die(nb::ext2mime('csv'));
 ?>