/** * Returns reflection information about a table. * * Caches the results in memory. * * @param string $table Table name. * @param boolean $force_refresh [Optional] Force a query to refresh the cache. Default false. * @return array Column info */ public function getColumns($table, $force_refresh = false) { if (!isset($this->columns[$table]) || true === $force_refresh) { $meta = array(); switch ($this->connection->getAttribute(PDO::ATTR_DRIVER_NAME)) { case 'pgsql': list($schema, $table) = stristr($table, '.') ? explode(".", $table) : array('public', $table); $result = $this->connection->prepareExecute("SELECT c.column_name, c.column_default, c.data_type, " . "(SELECT MAX(constraint_type) AS constraint_type FROM information_schema.constraint_column_usage cu " . "JOIN information_schema.table_constraints tc ON tc.constraint_name = cu.constraint_name " . "AND tc.constraint_type = 'PRIMARY KEY' " . "WHERE cu.column_name = c.column_name AND cu.table_name = c.table_name) AS constraint_type " . "FROM information_schema.columns c " . "WHERE c.table_schema = " . $this->connection->quote($schema) . " AND c.table_name = " . $this->connection->quote($table)); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { $meta[$row['column_name']] = array('pk' => $row['constraint_type'] == 'PRIMARY KEY', 'type' => $row['data_type'], 'blob' => preg_match('/(text|bytea)/', $row['data_type'])); if (stristr($row['column_default'], 'nextval')) { $meta[$row['column_name']]['default'] = null; } else { if (preg_match("/^'([^']+)'::(.+)\$/", $row['column_default'], $match)) { $meta[$row['column_name']]['default'] = $match[1]; } else { $meta[$row['column_name']]['default'] = $row['column_default']; } } } $this->columns[$table] = $meta; break; case 'sqlite': $result = $this->connection->query("PRAGMA table_info(" . $this->connection->quoteName($table) . ")"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { $meta[$row['name']] = array('pk' => $row['pk'] == '1', 'type' => $row['type'], 'default' => null, 'blob' => preg_match('/(TEXT|BLOB)/', $row['type'])); } $this->columns[$table] = $meta; break; default: $result = $this->connection->prepareExecute("select COLUMN_NAME, COLUMN_DEFAULT, DATA_TYPE, COLUMN_KEY " . "from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = DATABASE() and TABLE_NAME = :table_name", array(':table_name' => $table)); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { $meta[$row['COLUMN_NAME']] = array('pk' => $row['COLUMN_KEY'] == 'PRI', 'type' => $row['DATA_TYPE'], 'default' => in_array($row['COLUMN_DEFAULT'], array('NULL', 'CURRENT_TIMESTAMP')) ? null : $row['COLUMN_DEFAULT'], 'blob' => preg_match('/(TEXT|BLOB)/', $row['DATA_TYPE'])); } $this->columns[$table] = $meta; break; } } return $this->columns[$table]; }