]> git.nbdom.net Git - nb.git/commitdiff
indexes
authorNicolas Boisselier <nicolas.boisselier@semantico.com>
Mon, 4 Jul 2016 14:23:57 +0000 (15:23 +0100)
committerNicolas Boisselier <nicolas.boisselier@semantico.com>
Mon, 4 Jul 2016 14:23:57 +0000 (15:23 +0100)
lib/php/db/field.php
lib/php/db/table.php

index 9d9900c11fb9345330f78d872741903980d9bb2e..8baf459fcc50a69f3442a62f4732419a53a5eac3 100644 (file)
@@ -16,6 +16,7 @@ class field extends nb {
   private $textarea_size = 300;
   public $key = 0;
   public $uniq = 0;
+  public $index = 0;
   public $default;
   public $autoincrement;
   public $extra;
index 3ce8e9628672c43d6999dc5fa738bb9bb86c3690..97cddc1f1422a1da637ab28e14cb30737722c130 100644 (file)
@@ -36,6 +36,7 @@ Class Table extends nb {
   );
 
   public $fields;
+  public $indexes;
 
   function __construct($name,$opt=array()) {
 
@@ -173,20 +174,22 @@ Class Table extends nb {
    *
    */
   public function indexes() {
-    $sql = str_replace('<NAME>',$this->name,$this->db()->type('table.sql.index'));
-    if (is_array($sql)) list($sql,$fct) = count($sql)==1 ? [$sql[0],null] : $sql;
-    if (!$sql) return [];
+    if (!isset($this->indexes)) {
+      $sql = str_replace('<NAME>',$this->name,$this->db()->type('table.sql.index'));
+      if (is_array($sql)) list($sql,$fct) = count($sql)==1 ? [$sql[0],null] : $sql;
+      if (!$sql) return [];
 
-    $st = $this->db()->conn->prepare($sql);
-    $st->execute();
-    $return = [];
-    while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
+      $st = $this->db()->conn->prepare($sql);
+      $st->execute();
+      $this->indexes = [];
+      while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
 
-      if (!$fct) $return[] = $row;
-      elseif ($r = $fct($row)) $return[] = $r;
+        if (!$fct) $this->indexes[] = $row;
+        elseif ($r = $fct($row)) $this->indexes[] = $r;
 
+      }
     }
-    return $return;
+    return $this->indexes;
   }
 
   /*
@@ -246,6 +249,12 @@ Class Table extends nb {
       $rows = $this->db()->conn->query($conf['sql']);
       $rows->setFetchMode(PDO::FETCH_ASSOC);
 
+      // Get other indexes
+      $indexes = [];
+      foreach ($this->indexes() as $i) {
+        if (empty($i['unique'])) $indexes[$i['field']] = 1;
+      }
+
       foreach ($rows as $row) {
 
         $row = array_change_key_case($row,CASE_LOWER);
@@ -265,6 +274,7 @@ Class Table extends nb {
           'name'          => $row['name'],
           'type'          => strtolower($row['type']),
           'key'           => (preg_match('/^(f.*|no|0)?\s*$/i',$row['key']) ? 0 : 1),
+          'index'         => (empty($indexes[$row['name']]) ? 0 : $indexes[$row['name']]),
           'null'          => (preg_match('/^(f.*|no|0)?\s*$/i',$row['null']) ? 0 : 1),
           'extra'         => (isset($row['extra']) ? $row['extra'] : null), # !!! nothing todo with array $extra, this info from the sql server
           'autoincrement' => (isset($row['autoincrement']) ? $row['autoincrement'] : 0),