--- /dev/null
+<?php
+/**
+ * Copyright 2007 Maintainable Software, LLC
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_Db
+ * @subpackage Adapter
+ */
+
+/**
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_Db
+ * @subpackage Adapter
+ */
+class Horde_Db_Adapter_Abstract_Index
+{
+ public $table;
+ public $name;
+ public $unique;
+ public $primary;
+ public $columns;
+
+
+ /*##########################################################################
+ # Construct/Destruct
+ ##########################################################################*/
+
+ /**
+ * Construct
+ *
+ * @param string $table The table the index is on
+ * @param string $name The index's name
+ * @param boolean $primary Is this a primary key?
+ * @param boolean $unique Is this a unique index?
+ * @param array $columns The columns this index covers
+ */
+ public function __construct($table, $name, $primary, $unique, $columns)
+ {
+ $this->table = $table;
+ $this->name = $name;
+ $this->primary = $primary;
+ $this->unique = $unique;
+ $this->columns = $columns;
+ }
+
+
+ /*##########################################################################
+ # Casting
+ ##########################################################################*/
+
+ /**
+ * Comma-separated list of the columns in the primary key
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return implode(',', $this->columns);
+ }
+
+}
$indexes = array();
$currentIndex = null;
foreach ($this->select('SHOW KEYS FROM ' . $this->quoteTableName($tableName)) as $row) {
- if ($currentIndex != $row[2]) {
- if ($row[2] == 'PRIMARY') continue;
- $currentIndex = $row[2];
- $indexes[] = (object)array('table' => $row[0],
- 'name' => $row[2],
- 'unique' => $row[1] == '0',
- 'columns' => array());
+ if ($currentIndex != $row['Key_name']) {
+ if ($row['Key_name'] == 'PRIMARY') {
+ continue;
+ }
+ $currentIndex = $row['Key_name'];
+ $indexes[] = $this->componentFactory('Index', array(
+ $tableName, $row['Key_name'], false, $row['Non_unique'] == '0', array()));
}
- $indexes[count($indexes) - 1]->columns[] = $row[4];
+ $indexes[count($indexes) - 1]->columns[] = $row['Column_name'];
}
$this->_cache->set("tables/indexes/$tableName", serialize($indexes));
$columns = array();
foreach ($rows as $row) {
$columns[] = $this->componentFactory('Column', array(
- $row[0], $row[4], $row[1], $row[2] == 'YES'));
+ $row['Field'], $row['Default'], $row['Type'], $row['Null'] == 'YES'));
}
return $columns;
AND t.relname = " . $this->quote($tableName) . "
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (" . implode(',', $schemas) . ") )
AND a.attrelid = t.oid
- AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
- OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
- OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum
- OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum
- OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )
+ AND (d.indkey[0] = a.attnum OR d.indkey[1] = a.attnum
+ OR d.indkey[2] = a.attnum OR d.indkey[3] = a.attnum
+ OR d.indkey[4] = a.attnum OR d.indkey[5] = a.attnum
+ OR d.indkey[6] = a.attnum OR d.indkey[7] = a.attnum
+ OR d.indkey[8] = a.attnum OR d.indkey[9] = a.attnum)
ORDER BY i.relname";
$result = $this->select($sql, $name);
foreach ($result as $row) {
if ($currentIndex != $row[0]) {
- $indexes[] = (object)array('table' => $tableName,
- 'name' => $row[0],
- 'unique' => $row[1] == 't',
- 'columns' => array());
$currentIndex = $row[0];
+ $indexes[] = $this->componentFactory('Index', array(
+ $tableName, $row[0], false, $row[1] == 't', array()));
}
- $indexes[sizeof($indexes)-1]->columns[] = $row[2];
+ $indexes[count($indexes) - 1]->columns[] = $row[2];
}
$this->_cache->set("tables/indexes/$tableName", serialize($indexes));
if (!$indexes) {
$indexes = array();
foreach ($this->select('PRAGMA index_list(' . $this->quoteTableName($tableName) . ')') as $row) {
- $index = (object)array('table' => $tableName,
- 'name' => $row[1],
- 'unique' => (bool)$row[2],
- 'columns' => array());
+ $index = $this->componentFactory('Index', array(
+ $tableName, $row['name'], false, (bool)$row['unique'], array()));
foreach ($this->select('PRAGMA index_info(' . $this->quoteColumnName($index->name) . ')') as $field) {
- $index->columns[] = $field[2];
+ $index->columns[] = $field['name'];
}
$indexes[] = $index;
<dir name="Abstract">
<file name="Column.php" role="php" />
<file name="ColumnDefinition.php" role="php" />
+ <file name="Index.php" role="php" />
<file name="Schema.php" role="php" />
<file name="TableDefinition.php" role="php" />
</dir> <!-- /lib/Horde/Db/Adapter/Abstract -->
<filelist>
<install name="lib/Horde/Db/Adapter/Abstract/Column.php" as="Horde/Db/Adapter/Abstract/Column.php" />
<install name="lib/Horde/Db/Adapter/Abstract/ColumnDefinition.php" as="Horde/Db/Adapter/Abstract/ColumnDefinition.php" />
+ <install name="lib/Horde/Db/Adapter/Abstract/Index.php" as="Horde/Db/Adapter/Abstract/Index.php" />
<install name="lib/Horde/Db/Adapter/Abstract/Schema.php" as="Horde/Db/Adapter/Abstract/Schema.php" />
<install name="lib/Horde/Db/Adapter/Abstract/TableDefinition.php" as="Horde/Db/Adapter/Abstract/TableDefinition.php" />
<install name="lib/Horde/Db/Adapter/Abstract.php" as="Horde/Db/Adapter/Abstract.php" />