diff options
Diffstat (limited to 'kohana/libraries')
-rw-r--r-- | kohana/libraries/Cache.php | 12 | ||||
-rw-r--r-- | kohana/libraries/Database.php | 6 | ||||
-rw-r--r-- | kohana/libraries/ORM.php | 34 | ||||
-rw-r--r-- | kohana/libraries/Router.php | 13 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database.php | 14 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Mssql.php | 55 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Mysql.php | 107 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Mysqli.php | 39 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Pdosqlite.php | 37 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Pgsql.php | 99 |
10 files changed, 206 insertions, 210 deletions
diff --git a/kohana/libraries/Cache.php b/kohana/libraries/Cache.php index 3fb5a31c..3dcf0312 100644 --- a/kohana/libraries/Cache.php +++ b/kohana/libraries/Cache.php @@ -135,13 +135,13 @@ class Cache_Core { * Set a cache item by id. Tags may also be added and a custom lifetime * can be set. Non-string data is automatically serialized. * - * @param string unique cache id - * @param mixed data to cache - * @param array tags for this item - * @param integer number of seconds until the cache expires + * @param string unique cache id + * @param mixed data to cache + * @param array|string tags for this item + * @param integer number of seconds until the cache expires * @return boolean */ - function set($id, $data, array $tags = NULL, $lifetime = NULL) + function set($id, $data, $tags = NULL, $lifetime = NULL) { if (is_resource($data)) throw new Kohana_Exception('cache.resources'); @@ -155,7 +155,7 @@ class Cache_Core { $lifetime = $this->config['lifetime']; } - return $this->driver->set($id, $data, $tags, $lifetime); + return $this->driver->set($id, $data, (array) $tags, $lifetime); } /** diff --git a/kohana/libraries/Database.php b/kohana/libraries/Database.php index 49241675..59e47621 100644 --- a/kohana/libraries/Database.php +++ b/kohana/libraries/Database.php @@ -341,10 +341,10 @@ class Database_Core { foreach ($sql as $val) { - if (($val = trim($val)) === '') continue; - if (is_string($val)) { + if (($val = trim($val)) === '') continue; + // TODO: Temporary solution, this should be moved to database driver (AS is checked for twice) if (stripos($val, ' AS ') !== FALSE) { @@ -1187,7 +1187,7 @@ class Database_Core { { $this->link or $this->connect(); - return $this->driver->list_tables($this); + return $this->driver->list_tables(); } /** diff --git a/kohana/libraries/ORM.php b/kohana/libraries/ORM.php index 4cbec50a..ae214649 100644 --- a/kohana/libraries/ORM.php +++ b/kohana/libraries/ORM.php @@ -319,8 +319,9 @@ class ORM_Core { elseif (in_array($column, $this->has_many)) { // one<>many relationship - return $this->related[$column] = ORM::factory(inflector::singular($column)) - ->where($this->foreign_key($column, $column), $this->object[$this->primary_key]) + $model = ORM::factory(inflector::singular($column)); + return $this->related[$column] = $model + ->where($this->foreign_key($column, $model->table_name), $this->object[$this->primary_key]) ->find_all(); } elseif (in_array($column, $this->has_and_belongs_to_many)) @@ -638,10 +639,10 @@ class ORM_Core { */ public function validate(Validation $array, $save = FALSE) { + $safe_array = $array->safe_array(); + if ( ! $array->submitted()) { - $safe_array = $array->safe_array(); - foreach ($safe_array as $key => $value) { // Get the value from this object @@ -661,12 +662,16 @@ class ORM_Core { // Validate the array if ($status = $array->validate()) { - $safe_array = $array->safe_array(); + // Grab only set fields (excludes missing data, unlike safe_array) + $fields = $array->as_array(); - foreach ($safe_array as $key => $value) + foreach ($fields as $key => $value) { - // Set new data - $this->$key = $value; + if (isset($safe_array[$key])) + { + // Set new data, ignoring any missing fields or fields without rules + $this->$key = $value; + } } if ($save === TRUE OR is_string($save)) @@ -840,7 +845,7 @@ class ORM_Core { elseif (is_null($ids)) { // Delete all records - $this->db->where(TRUE); + $this->db->where('1=1'); } else { @@ -902,7 +907,7 @@ class ORM_Core { else { // Load table columns - ORM::$column_cache[$this->object_name] = $this->table_columns = $this->db->list_fields($this->table_name, TRUE); + ORM::$column_cache[$this->object_name] = $this->table_columns = $this->list_fields(); } } @@ -1021,11 +1026,16 @@ class ORM_Core { /** * Proxy method to Database list_fields. * - * @param string table name + * @param string table name or NULL to use this table * @return array */ - public function list_fields($table) + public function list_fields($table = NULL) { + if ($table === NULL) + { + $table = $this->table_name; + } + // Proxy to database return $this->db->list_fields($table); } diff --git a/kohana/libraries/Router.php b/kohana/libraries/Router.php index 172c5f92..a47a9993 100644 --- a/kohana/libraries/Router.php +++ b/kohana/libraries/Router.php @@ -213,15 +213,12 @@ class Router_Core { elseif (isset($_SERVER['PHP_SELF']) AND $_SERVER['PHP_SELF']) { Router::$current_uri = $_SERVER['PHP_SELF']; - } - - // The front controller directory and filename - $fc = substr(realpath($_SERVER['SCRIPT_FILENAME']), strlen(DOCROOT)); - if (($strpos_fc = strpos(Router::$current_uri, $fc)) !== FALSE) - { - // Remove the front controller from the current uri - Router::$current_uri = substr(Router::$current_uri, $strpos_fc + strlen($fc)); + if (($strpos_fc = strpos(Router::$current_uri, KOHANA)) !== FALSE) + { + // Remove the front controller from the current uri + Router::$current_uri = substr(Router::$current_uri, $strpos_fc + strlen(KOHANA)); + } } // Remove slashes from the start and end of the URI diff --git a/kohana/libraries/drivers/Database.php b/kohana/libraries/drivers/Database.php index 96562240..f5adf924 100644 --- a/kohana/libraries/drivers/Database.php +++ b/kohana/libraries/drivers/Database.php @@ -11,7 +11,7 @@ */ abstract class Database_Driver { - static $query_cache; + protected $query_cache; /** * Connect to our database. @@ -124,7 +124,7 @@ abstract class Database_Driver { } else { - if ( ! $this->has_operator($key)) + if ( ! $this->has_operator($key) AND ! empty($key)) { $key = $this->escape_column($key).' ='; } @@ -290,7 +290,7 @@ abstract class Database_Driver { */ public function has_operator($str) { - return (bool) preg_match('/[<>!=]|\sIS(?:\s+NOT\s+)?\b/i', trim($str)); + return (bool) preg_match('/[<>!=]|\sIS(?:\s+NOT\s+)?\b|BETWEEN/i', trim($str)); } /** @@ -337,7 +337,7 @@ abstract class Database_Driver { * * @return array */ - abstract public function list_tables(Database $db); + abstract public function list_tables(); /** * Lists all fields in a table. @@ -431,11 +431,11 @@ abstract class Database_Driver { { if (empty($sql)) { - self::$query_cache = array(); + $this->query_cache = array(); } else { - unset(self::$query_cache[$this->query_hash($sql)]); + unset($this->query_cache[$this->query_hash($sql)]); } Kohana::log('debug', 'Database cache cleared: '.get_class($this)); @@ -633,4 +633,4 @@ abstract class Database_Result implements ArrayAccess, Iterator, Countable { return $this->offsetExists($this->current_row); } -} // End Database Result Interface
\ No newline at end of file +} // End Database Result Interface diff --git a/kohana/libraries/drivers/Database/Mssql.php b/kohana/libraries/drivers/Database/Mssql.php index 3e89faba..6947679a 100644 --- a/kohana/libraries/drivers/Database/Mssql.php +++ b/kohana/libraries/drivers/Database/Mssql.php @@ -89,19 +89,19 @@ class Database_Mssql_Driver extends Database_Driver { $hash = $this->query_hash($sql); - if ( ! isset(self::$query_cache[$hash])) + if ( ! isset($this->query_cache[$hash])) { // Set the cached object - self::$query_cache[$hash] = new Mssql_Result(mssql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql); + $this->query_cache[$hash] = new Mssql_Result(mssql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql); } else { // Rewind cached result - self::$query_cache[$hash]->rewind(); + $this->query_cache[$hash]->rewind(); } // Return the cached query - return self::$query_cache[$hash]; + return $this->query_cache[$hash]; } return new Mssql_Result(mssql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql); @@ -128,9 +128,22 @@ class Database_Mssql_Driver extends Database_Driver if (!$this->db_config['escape']) return $column; - if (strtolower($column) == 'count(*)' OR $column == '*') + if ($column == '*') return $column; + // This matches any functions we support to SELECT. + if ( preg_match('/(avg|count|sum|max|min)\(\s*(.*)\s*\)(\s*as\s*(.+)?)?/i', $column, $matches)) + { + if ( count($matches) == 3) + { + return $matches[1].'('.$this->escape_column($matches[2]).')'; + } + else if ( count($matches) == 5) + { + return $matches[1].'('.$this->escape_column($matches[2]).') AS '.$this->escape_column($matches[2]); + } + } + // This matches any modifiers we support to SELECT. if ( ! preg_match('/\b(?:rand|all|distinct(?:row)?|high_priority|sql_(?:small_result|b(?:ig_result|uffer_result)|no_cache|ca(?:che|lc_found_rows)))\s/i', $column)) { @@ -251,7 +264,7 @@ class Database_Mssql_Driver extends Database_Driver return preg_replace($characters, $replace, $str); } - public function list_tables(Database $db) + public function list_tables() { $sql = 'SHOW TABLES FROM ['.$this->db_config['connection']['database'].']'; $result = $this->query($sql)->result(FALSE, MSSQL_ASSOC); @@ -272,36 +285,22 @@ class Database_Mssql_Driver extends Database_Driver public function list_fields($table) { - static $tables; + $result = array(); - if (empty($tables[$table])) + foreach ($this->field_data($table) as $row) { - foreach ($this->field_data($table) as $row) - { - // Make an associative array - $tables[$table][$row->Field] = $this->sql_type($row->Type); - } + // Make an associative array + $result[$row->Field] = $this->sql_type($row->Type); } - return $tables[$table]; + return $result; } public function field_data($table) { - $columns = array(); - - if ($query = MSSQL_query('SHOW COLUMNS FROM '.$this->escape_table($table), $this->link)) - { - if (MSSQL_num_rows($query) > 0) - { - while ($row = MSSQL_fetch_object($query)) - { - $columns[] = $row; - } - } - } + $query = $this->query('SHOW COLUMNS FROM '.$this->escape_table($table), $this->link); - return $columns; + return $query->result_array(TRUE); } } @@ -460,4 +459,4 @@ class Mssql_Result extends Database_Result { return mssql_data_seek($this->result, $offset); } -} // End mssql_Result Class
\ No newline at end of file +} // End mssql_Result Class diff --git a/kohana/libraries/drivers/Database/Mysql.php b/kohana/libraries/drivers/Database/Mysql.php index 9315ed1f..978de459 100644 --- a/kohana/libraries/drivers/Database/Mysql.php +++ b/kohana/libraries/drivers/Database/Mysql.php @@ -22,12 +22,6 @@ class Database_Mysql_Driver extends Database_Driver { protected $db_config; /** - * Performance caches. - */ - private $tables_cache; - private $fields_cache; - - /** * Sets the config for the class. * * @param array database configuration @@ -35,8 +29,6 @@ class Database_Mysql_Driver extends Database_Driver { public function __construct($config) { $this->db_config = $config; - $this->tables_cache = array(); - $this->fields_cache = array(); Kohana::log('debug', 'MySQL Database Driver Initialized'); } @@ -85,23 +77,23 @@ class Database_Mysql_Driver extends Database_Driver { public function query($sql) { // Only cache if it's turned on, and only cache if it's not a write statement - if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET)\b#i', $sql)) + if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET|DELETE|TRUNCATE)\b#i', $sql)) { $hash = $this->query_hash($sql); - if ( ! isset(self::$query_cache[$hash])) + if ( ! isset($this->query_cache[$hash])) { // Set the cached object - self::$query_cache[$hash] = new Mysql_Result(mysql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql); + $this->query_cache[$hash] = new Mysql_Result(mysql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql); } else { // Rewind cached result - self::$query_cache[$hash]->rewind(); + $this->query_cache[$hash]->rewind(); } // Return the cached query - return self::$query_cache[$hash]; + return $this->query_cache[$hash]; } return new Mysql_Result(mysql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql); @@ -136,9 +128,22 @@ class Database_Mysql_Driver extends Database_Driver { if (!$this->db_config['escape']) return $column; - if (strtolower($column) == 'count(*)' OR $column == '*') + if ($column == '*') return $column; + // This matches any functions we support to SELECT. + if ( preg_match('/(avg|count|sum|max|min)\(\s*(.*)\s*\)(\s*as\s*(.+)?)?/i', $column, $matches)) + { + if ( count($matches) == 3) + { + return $matches[1].'('.$this->escape_column($matches[2]).')'; + } + else if ( count($matches) == 5) + { + return $matches[1].'('.$this->escape_column($matches[2]).') AS '.$this->escape_column($matches[2]); + } + } + // This matches any modifiers we support to SELECT. if ( ! preg_match('/\b(?:rand|all|distinct(?:row)?|high_priority|sql_(?:small_result|b(?:ig_result|uffer_result)|no_cache|ca(?:che|lc_found_rows)))\s/i', $column)) { @@ -217,8 +222,8 @@ class Database_Mysql_Driver extends Database_Driver { { $froms[] = $this->escape_column($from); } - $sql .= "\nFROM "; - $sql .= implode(', ', $froms); + $sql .= "\nFROM ("; + $sql .= implode(', ', $froms).")"; } if (count($database['join']) > 0) @@ -273,11 +278,11 @@ class Database_Mysql_Driver extends Database_Driver { return mysql_real_escape_string($str, $this->link); } - public function list_tables(Database $db) + public function list_tables() { - $tables =& $this->tables_cache; + $tables = array(); - if (empty($tables) AND $query = $db->query('SHOW TABLES FROM '.$this->escape_table($this->db_config['connection']['database']))) + if ($query = $this->query('SHOW TABLES FROM '.$this->escape_table($this->db_config['connection']['database']))) { foreach ($query->result(FALSE) as $row) { @@ -295,63 +300,37 @@ class Database_Mysql_Driver extends Database_Driver { public function list_fields($table) { - $tables =& $this->fields_cache; + $result = NULL; - if (empty($tables[$table])) + foreach ($this->field_data($table) as $row) { - foreach ($this->field_data($table) as $row) + // Make an associative array + $result[$row->Field] = $this->sql_type($row->Type); + + if ($row->Key === 'PRI' AND $row->Extra === 'auto_increment') + { + // For sequenced (AUTO_INCREMENT) tables + $result[$row->Field]['sequenced'] = TRUE; + } + + if ($row->Null === 'YES') { - // Make an associative array - $tables[$table][$row->Field] = $this->sql_type($row->Type); - - if ($row->Key === 'PRI' AND $row->Extra === 'auto_increment') - { - // For sequenced (AUTO_INCREMENT) tables - $tables[$table][$row->Field]['sequenced'] = TRUE; - } - - if ($row->Null === 'YES') - { - // Set NULL status - $tables[$table][$row->Field]['null'] = TRUE; - } + // Set NULL status + $result[$row->Field]['null'] = TRUE; } } - if (!isset($tables[$table])) + if (!isset($result)) throw new Kohana_Database_Exception('database.table_not_found', $table); - return $tables[$table]; + return $result; } public function field_data($table) { - $columns = array(); - - if ($query = mysql_query('SHOW COLUMNS FROM '.$this->escape_table($table), $this->link)) - { - if (mysql_num_rows($query)) - { - while ($row = mysql_fetch_object($query)) - { - $columns[] = $row; - } - } - } + $result = $this->query('SHOW COLUMNS FROM '.$this->escape_table($table)); - return $columns; - } - - /** - * Clears the internal query cache. - * - * @param string SQL query - */ - public function clear_cache($sql = NULL) - { - parent::clear_cache($sql); - $this->tables_cache = array(); - $this->fields_cache = array(); + return $result->result_array(TRUE); } } // End Database_Mysql_Driver Class @@ -514,4 +493,4 @@ class Mysql_Result extends Database_Result { } } -} // End Mysql_Result Class
\ No newline at end of file +} // End Mysql_Result Class diff --git a/kohana/libraries/drivers/Database/Mysqli.php b/kohana/libraries/drivers/Database/Mysqli.php index 13a81281..f15e4283 100644 --- a/kohana/libraries/drivers/Database/Mysqli.php +++ b/kohana/libraries/drivers/Database/Mysqli.php @@ -68,23 +68,23 @@ class Database_Mysqli_Driver extends Database_Mysql_Driver { public function query($sql) { // Only cache if it's turned on, and only cache if it's not a write statement - if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET)\b#i', $sql)) + if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET|DELETE|TRUNCATE)\b#i', $sql)) { $hash = $this->query_hash($sql); - if ( ! isset(self::$query_cache[$hash])) + if ( ! isset($this->query_cache[$hash])) { // Set the cached object - self::$query_cache[$hash] = new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql); + $this->query_cache[$hash] = new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql); } else { // Rewind cached result - self::$query_cache[$hash]->rewind(); + $this->query_cache[$hash]->rewind(); } // Return the cached query - return self::$query_cache[$hash]; + return $this->query_cache[$hash]; } return new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql); @@ -111,22 +111,6 @@ class Database_Mysqli_Driver extends Database_Mysql_Driver { return $this->link->error; } - public function field_data($table) - { - $columns = array(); - $query = $this->link->query('SHOW COLUMNS FROM '.$this->escape_table($table)); - - if (is_object($query)) - { - while ($row = $query->fetch_object()) - { - $columns[] = $row; - } - } - - return $columns; - } - } // End Database_Mysqli_Driver Class /** @@ -299,12 +283,15 @@ class Kohana_Mysqli_Result extends Database_Result { public function seek($offset) { - if ( ! $this->offsetExists($offset)) - return FALSE; + if ($this->offsetExists($offset) AND $this->result->data_seek($offset)) + { + // Set the current row to the offset + $this->current_row = $offset; - $this->result->data_seek($offset); + return TRUE; + } - return TRUE; + return FALSE; } public function offsetGet($offset) @@ -368,4 +355,4 @@ class Kohana_Mysqli_Statement { $this->stmt->execute(); return $this->stmt; } -}
\ No newline at end of file +} diff --git a/kohana/libraries/drivers/Database/Pdosqlite.php b/kohana/libraries/drivers/Database/Pdosqlite.php index 5a512877..c2d1bb21 100644 --- a/kohana/libraries/drivers/Database/Pdosqlite.php +++ b/kohana/libraries/drivers/Database/Pdosqlite.php @@ -43,7 +43,7 @@ class Database_Pdosqlite_Driver extends Database_Driver { array(PDO::ATTR_PERSISTENT => $this->db_config['persistent'])); $this->link->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); - $this->link->query('PRAGMA count_changes=1;'); + //$this->link->query('PRAGMA count_changes=1;'); if ($charset = $this->db_config['character_set']) { @@ -63,7 +63,7 @@ class Database_Pdosqlite_Driver extends Database_Driver { public function query($sql) { - try + try { $sth = $this->link->prepare($sql); } @@ -92,9 +92,22 @@ class Database_Pdosqlite_Driver extends Database_Driver { if ( ! $this->db_config['escape']) return $column; - if (strtolower($column) == 'count(*)' OR $column == '*') + if ($column == '*') return $column; + // This matches any functions we support to SELECT. + if ( preg_match('/(avg|count|sum|max|min)\(\s*(.*)\s*\)(\s*as\s*(.+)?)?/i', $column, $matches)) + { + if ( count($matches) == 3) + { + return $matches[1].'('.$this->escape_column($matches[2]).')'; + } + else if ( count($matches) == 5) + { + return $matches[1].'('.$this->escape_column($matches[2]).') AS '.$this->escape_column($matches[2]); + } + } + // This matches any modifiers we support to SELECT. if ( ! preg_match('/\b(?:rand|all|distinct(?:row)?|high_priority|sql_(?:small_result|b(?:ig_result|uffer_result)|no_cache|ca(?:che|lc_found_rows)))\s/i', $column)) { @@ -205,12 +218,12 @@ class Database_Pdosqlite_Driver extends Database_Driver { return $res; } - public function list_tables(Database $db) + public function list_tables() { $sql = "SELECT `name` FROM `sqlite_master` WHERE `type`='table' ORDER BY `name`;"; try { - $result = $db->query($sql)->result(FALSE, PDO::FETCH_ASSOC); + $result = $this->query($sql)->result(FALSE, PDO::FETCH_ASSOC); $tables = array(); foreach ($result as $row) { @@ -298,14 +311,12 @@ class Pdosqlite_Result extends Database_Result { { if (is_object($result) OR $result = $link->prepare($sql)) { - // run the query - try + // run the query. Return true if success, false otherwise + if( ! $result->execute()) { - $result->execute(); - } - catch (PDOException $e) - { - throw new Kohana_Database_Exception('database.error', $e->getMessage()); + // Throw Kohana Exception with error message. See PDOStatement errorInfo() method + $arr_infos = $result->errorInfo(); + throw new Kohana_Database_Exception('database.error', $arr_infos[2]); } if (preg_match('/^SELECT|PRAGMA|EXPLAIN/i', $sql)) @@ -320,6 +331,8 @@ class Pdosqlite_Result extends Database_Result { elseif (preg_match('/^DELETE|INSERT|UPDATE/i', $sql)) { $this->insert_id = $link->lastInsertId(); + + $this->total_rows = $result->rowCount(); } } else diff --git a/kohana/libraries/drivers/Database/Pgsql.php b/kohana/libraries/drivers/Database/Pgsql.php index 62a33ad6..c8a7d819 100644 --- a/kohana/libraries/drivers/Database/Pgsql.php +++ b/kohana/libraries/drivers/Database/Pgsql.php @@ -68,18 +68,18 @@ class Database_Pgsql_Driver extends Database_Driver { { $hash = $this->query_hash($sql); - if ( ! isset(self::$query_cache[$hash])) + if ( ! isset($this->query_cache[$hash])) { // Set the cached object - self::$query_cache[$hash] = new Pgsql_Result(pg_query($this->link, $sql), $this->link, $this->db_config['object'], $sql); + $this->query_cache[$hash] = new Pgsql_Result(pg_query($this->link, $sql), $this->link, $this->db_config['object'], $sql); } else { // Rewind cached result - self::$query_cache[$hash]->rewind(); + $this->query_cache[$hash]->rewind(); } - return self::$query_cache[$hash]; + return $this->query_cache[$hash]; } // Suppress warning triggered when a database error occurs (e.g., a constraint violation) @@ -104,9 +104,22 @@ class Database_Pgsql_Driver extends Database_Driver { if (!$this->db_config['escape']) return $column; - if (strtolower($column) == 'count(*)' OR $column == '*') + if ($column == '*') return $column; + // This matches any functions we support to SELECT. + if ( preg_match('/(avg|count|sum|max|min)\(\s*(.*)\s*\)(\s*as\s*(.+)?)?/i', $column, $matches)) + { + if ( count($matches) == 3) + { + return $matches[1].'('.$this->escape_column($matches[2]).')'; + } + else if ( count($matches) == 5) + { + return $matches[1].'('.$this->escape_column($matches[2]).') AS '.$this->escape_column($matches[2]); + } + } + // This matches any modifiers we support to SELECT. if ( ! preg_match('/\b(?:all|distinct)\s/i', $column)) { @@ -147,14 +160,14 @@ class Database_Pgsql_Driver extends Database_Driver { { $prefix = ($num_regexs == 0) ? '' : $type; - return $prefix.' '.$this->escape_column($field).' REGEXP \''.$this->escape_str($match).'\''; + return $prefix.' '.$this->escape_column($field).' ~* \''.$this->escape_str($match).'\''; } public function notregex($field, $match, $type, $num_regexs) { $prefix = $num_regexs == 0 ? '' : $type; - return $prefix.' '.$this->escape_column($field).' NOT REGEXP \''.$this->escape_str($match) . '\''; + return $prefix.' '.$this->escape_column($field).' !~* \''.$this->escape_str($match) . '\''; } public function limit($limit, $offset = 0) @@ -225,10 +238,10 @@ class Database_Pgsql_Driver extends Database_Driver { return pg_escape_string($this->link, $str); } - public function list_tables(Database $db) + public function list_tables() { $sql = 'SELECT table_schema || \'.\' || table_name FROM information_schema.tables WHERE table_schema NOT IN (\'pg_catalog\', \'information_schema\')'; - $result = $db->query($sql)->result(FALSE, PGSQL_ASSOC); + $result = $this->query($sql)->result(FALSE, PGSQL_ASSOC); $retval = array(); foreach ($result as $row) @@ -246,39 +259,34 @@ class Database_Pgsql_Driver extends Database_Driver { public function list_fields($table) { - static $tables; + $result = NULL; - if (empty($tables[$table])) + foreach ($this->field_data($table) as $row) { - foreach ($this->field_data($table) as $row) + // Make an associative array + $result[$row->column_name] = $this->sql_type($row->data_type); + + if (!strncmp($row->column_default, 'nextval(', 8)) + { + $result[$row->column_name]['sequenced'] = TRUE; + } + + if ($row->is_nullable === 'YES') { - // Make an associative array - $tables[$table][$row->column_name] = $this->sql_type($row->data_type); - - if (!strncmp($row->column_default, 'nextval(', 8)) - { - $tables[$table][$row->column_name]['sequenced'] = TRUE; - } - - if ($row->is_nullable === 'YES') - { - $tables[$table][$row->column_name]['null'] = TRUE; - } + $result[$row->column_name]['null'] = TRUE; } } - if (!isset($tables[$table])) + if (!isset($result)) throw new Kohana_Database_Exception('database.table_not_found', $table); - return $tables[$table]; + return $result; } public function field_data($table) { - $columns = array(); - // http://www.postgresql.org/docs/8.3/static/infoschema-columns.html - $result = pg_query($this->link, ' + $result = $this->query(' SELECT column_name, column_default, is_nullable, data_type, udt_name, character_maximum_length, numeric_precision, numeric_precision_radix, numeric_scale FROM information_schema.columns @@ -286,15 +294,7 @@ class Database_Pgsql_Driver extends Database_Driver { ORDER BY ordinal_position '); - if ($result) - { - while ($row = pg_fetch_object($result)) - { - $columns[] = $row; - } - } - - return $columns; + return $result->result_array(TRUE); } } // End Database_Pgsql_Driver Class @@ -318,6 +318,7 @@ class Pgsql_Result extends Database_Result { */ public function __construct($result, $link, $object = TRUE, $sql) { + $this->link = $link; $this->result = $result; // If the query is a resource, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query @@ -418,9 +419,14 @@ class Pgsql_Result extends Database_Result { } } - while ($row = $fetch($this->result, NULL, $type)) + if ($this->total_rows) { - $rows[] = $row; + pg_result_seek($this->result, 0); + + while ($row = $fetch($this->result, NULL, $type)) + { + $rows[] = $row; + } } return $rows; @@ -450,10 +456,15 @@ class Pgsql_Result extends Database_Result { public function seek($offset) { - if ( ! $this->offsetExists($offset)) - return FALSE; + if ($this->offsetExists($offset) AND pg_result_seek($this->result, $offset)) + { + // Set the current row to the offset + $this->current_row = $offset; - return pg_result_seek($this->result, $offset); + return TRUE; + } + + return FALSE; } public function list_fields() @@ -524,4 +535,4 @@ class Kohana_Pgsql_Statement { { return $this; } -}
\ No newline at end of file +} |