From: Nicolas Boisselier Date: Mon, 4 Jul 2016 14:23:57 +0000 (+0100) Subject: indexes X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=eb76c207c0d84f24c3b1f056032b70702d8e668c;p=nb.git indexes --- diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 9d9900c1..8baf459f 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -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; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 3ce8e962..97cddc1f 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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('',$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('',$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),