* @param string $name
* @return array
*/
- abstract public function indexes($tableName, $name=null);
+ abstract public function indexes($tableName, $name = null);
/**
* Returns an array of Column objects for the table specified by +table_name+.
* @param string $name
* @return array
*/
- abstract public function columns($tableName, $name=null);
+ abstract public function columns($tableName, $name = null);
/**
* Creates a new table
*/
protected function _clearTableCache($tableName)
{
- $this->_cache->set("tables/$tableName", null);
+ $this->_cache->set("tables/columns/$tableName", null);
+ $this->_cache->set("tables/indexes/$tableName", null);
}
}
*/
public function indexes($tableName, $name=null)
{
- $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());
+ $indexes = @unserialize($this->_cache->get("tables/indexes/$tableName"));
+
+ if (!$indexes) {
+ $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());
+ }
+ $indexes[count($indexes) - 1]->columns[] = $row[4];
}
- $indexes[sizeof($indexes)-1]->columns[] = $row[4];
+
+ $this->_cache->set("tables/indexes/$tableName", serialize($indexes));
}
+
return $indexes;
}
*/
public function columns($tableName, $name=null)
{
- // check cache
- $rows = @unserialize($this->_cache->get("tables/$tableName"));
+ $rows = @unserialize($this->_cache->get("tables/columns/$tableName"));
- // query to build rows
if (!$rows) {
$rows = $this->selectAll('SHOW FIELDS FROM ' . $this->quoteTableName($tableName), $name);
- // write cache
- $this->_cache->set("tables/$tableName", serialize($rows));
+ $this->_cache->set("tables/columns/$tableName", serialize($rows));
}
// create columns from rows
$columns[] = $this->componentFactory('Column', array(
$row[0], $row[4], $row[1], $row[2] == 'YES'));
}
+
return $columns;
}
*/
public function indexes($tableName, $name = null)
{
- $schemas = array();
- foreach (explode(',', $this->getSchemaSearchPath()) as $p) {
- $schemas[] = $this->quote($p);
- }
+ $indexes = @unserialize($this->_cache->get("tables/indexes/$tableName"));
- $sql = "
- SELECT distinct i.relname, d.indisunique, a.attname
- FROM pg_class t, pg_class i, pg_index d, pg_attribute a
- WHERE i.relkind = 'i'
- AND d.indexrelid = i.oid
- AND d.indisprimary = 'f'
- AND t.oid = d.indrelid
- 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 )
- ORDER BY i.relname";
-
- $result = $this->select($sql, $name);
-
- $currentIndex = null;
- $indexes = array();
-
- 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];
+ if (!$indexes) {
+ $schemas = array();
+ foreach (explode(',', $this->getSchemaSearchPath()) as $p) {
+ $schemas[] = $this->quote($p);
}
- $indexes[sizeof($indexes)-1]->columns[] = $row[2];
+
+ $sql = "
+ SELECT distinct i.relname, d.indisunique, a.attname
+ FROM pg_class t, pg_class i, pg_index d, pg_attribute a
+ WHERE i.relkind = 'i'
+ AND d.indexrelid = i.oid
+ AND d.indisprimary = 'f'
+ AND t.oid = d.indrelid
+ 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 )
+ ORDER BY i.relname";
+
+ $result = $this->select($sql, $name);
+
+ $currentIndex = null;
+ $indexes = array();
+
+ 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[sizeof($indexes)-1]->columns[] = $row[2];
+ }
+
+ $this->_cache->set("tables/indexes/$tableName", serialize($indexes));
}
return $indexes;
*/
public function columns($tableName, $name = null)
{
- // check cache
- $rows = @unserialize($this->_cache->get("tables/$tableName"));
+ $rows = @unserialize($this->_cache->get("tables/columns/$tableName"));
- // query to build rows
if (!$rows) {
$rows = $this->columnDefinitions($tableName, $name);
- // write cache
- $this->_cache->set("tables/$tableName", serialize($rows));
+ $this->_cache->set("tables/columns/$tableName", serialize($rows));
}
// create columns from rows
*
* @param string $name
*/
- public function tables($name=null)
+ public function tables($name = null)
{
return $this->selectValues("SELECT name FROM sqlite_master WHERE type = 'table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type = 'table' AND name != 'sqlite_sequence' ORDER BY name");
}
* @param string $tableName
* @param string $name
*/
- public function indexes($tableName, $name=null)
+ public function indexes($tableName, $name = null)
{
- $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());
- foreach ($this->select('PRAGMA index_info(' . $this->quoteColumnName($index->name) . ')') as $field) {
- $index->columns[] = $field[2];
+ $indexes = @unserialize($this->_cache->get("tables/indexes/$tableName"));
+
+ 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());
+ foreach ($this->select('PRAGMA index_info(' . $this->quoteColumnName($index->name) . ')') as $field) {
+ $index->columns[] = $field[2];
+ }
+
+ $indexes[] = $index;
}
- $indexes[] = $index;
+ $this->_cache->set("tables/indexes/$tableName", serialize($indexes));
}
+
return $indexes;
}
* @param string $tableName
* @param string $name
*/
- public function columns($tableName, $name=null)
+ public function columns($tableName, $name = null)
{
- // check cache
- $rows = @unserialize($this->_cache->get("tables/$tableName"));
+ $rows = @unserialize($this->_cache->get("tables/columns/$tableName"));
- // query to build rows
if (!$rows) {
$rows = $this->selectAll('PRAGMA table_info(' . $this->quoteTableName($tableName) . ')', $name);
- // write cache
- $this->_cache->set("tables/$tableName", serialize($rows));
+ $this->_cache->set("tables/columns/$tableName", serialize($rows));
}
// create columns from rows
$columns[] = $this->componentFactory('Column', array(
$row[1], $row[4], $row[2], !(bool)$row[3]));
}
+
return $columns;
}
public function testCachedTableDescription()
{
// remove any current cache.
- $this->_cache->set('tables/cache_table', '');
- $this->assertEquals('', $this->_cache->get('tables/cache_table'));
+ $this->_cache->set('tables/columns/cache_table', '');
+ $this->assertEquals('', $this->_cache->get('tables/columns/cache_table'));
$this->_createTestTable('cache_table');
$cols = $this->_conn->columns('cache_table');
- $this->assertNotEquals('', $this->_cache->get('tables/cache_table'));
+ $this->assertNotEquals('', $this->_cache->get('tables/columns/cache_table'));
}
public function testCachedTableDescription()
{
// remove any current cache.
- $this->_cache->set('tables/cache_table', '');
- $this->assertEquals('', $this->_cache->get('tables/cache_table'));
+ $this->_cache->set('tables/columns/cache_table', '');
+ $this->assertEquals('', $this->_cache->get('tables/columns/cache_table'));
$this->_createTestTable('cache_table');
$cols = $this->_conn->columns('cache_table');
- $this->assertNotEquals('', $this->_cache->get('tables/cache_table'));
+ $this->assertNotEquals('', $this->_cache->get('tables/columns/cache_table'));
}
public function testCachedTableDescription()
{
// remove any current cache.
- $this->_cache->set('tables/cache_table', '');
- $this->assertEquals('', $this->_cache->get('tables/cache_table'));
+ $this->_cache->set('tables/columns/cache_table', '');
+ $this->assertEquals('', $this->_cache->get('tables/columns/cache_table'));
$this->_createTestTable('cache_table');
$cols = $this->_conn->columns('cache_table');
- $this->assertNotEquals('', $this->_cache->get('tables/cache_table'));
+ $this->assertNotEquals('', $this->_cache->get('tables/columns/cache_table'));
}
public function testCachedTableDescription()
{
// remove any current cache.
- $this->_cache->set('tables/cache_table', '');
- $this->assertEquals('', $this->_cache->get('tables/cache_table'));
+ $this->_cache->set('tables/columns/cache_table', '');
+ $this->assertEquals('', $this->_cache->get('tables/columns/cache_table'));
$this->_createTestTable('cache_table');
$cols = $this->_conn->columns('cache_table');
- $this->assertNotEquals('', $this->_cache->get('tables/cache_table'));
+ $this->assertNotEquals('', $this->_cache->get('tables/columns/cache_table'));
}