diff options
Diffstat (limited to 'kohana/libraries')
-rw-r--r-- | kohana/libraries/Database.php | 79 | ||||
-rw-r--r-- | kohana/libraries/Input.php | 21 | ||||
-rw-r--r-- | kohana/libraries/ORM.php | 58 | ||||
-rw-r--r-- | kohana/libraries/Validation.php | 10 | ||||
-rw-r--r-- | kohana/libraries/drivers/Cache/Memcache.php | 65 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Mysqli.php | 6 | ||||
-rw-r--r-- | kohana/libraries/drivers/Database/Pgsql.php | 9 |
7 files changed, 120 insertions, 128 deletions
diff --git a/kohana/libraries/Database.php b/kohana/libraries/Database.php index c98faffb..49241675 100644 --- a/kohana/libraries/Database.php +++ b/kohana/libraries/Database.php @@ -462,7 +462,18 @@ class Database_Core { public function where($key, $value = NULL, $quote = TRUE) { $quote = (func_num_args() < 2 AND ! is_array($key)) ? -1 : $quote; - $keys = is_array($key) ? $key : array($key => $value); + if (is_object($key)) + { + $keys = array((string) $key => ''); + } + elseif ( ! is_array($key)) + { + $keys = array($key => $value); + } + else + { + $keys = $key; + } foreach ($keys as $key => $value) { @@ -484,7 +495,18 @@ class Database_Core { public function orwhere($key, $value = NULL, $quote = TRUE) { $quote = (func_num_args() < 2 AND ! is_array($key)) ? -1 : $quote; - $keys = is_array($key) ? $key : array($key => $value); + if (is_object($key)) + { + $keys = array((string) $key => ''); + } + elseif ( ! is_array($key)) + { + $keys = array($key => $value); + } + else + { + $keys = $key; + } foreach ($keys as $key => $value) { @@ -1172,11 +1194,15 @@ class Database_Core { * See if a table exists in the database. * * @param string table name + * @param boolean True to attach table prefix * @return boolean */ - public function table_exists($table_name) + public function table_exists($table_name, $prefix = TRUE) { - return in_array($this->config['table_prefix'].$table_name, $this->list_tables()); + if ($prefix) + return in_array($this->config['table_prefix'].$table_name, $this->list_tables()); + else + return in_array($table_name, $this->list_tables()); } /** @@ -1313,17 +1339,6 @@ class Database_Core { } /** - * Create a prepared statement (experimental). - * - * @param string SQL query - * @return object - */ - public function stmt_prepare($sql) - { - return $this->driver->stmt_prepare($sql, $this->config); - } - - /** * Pushes existing query space onto the query stack. Use push * and pop to prevent queries from clashing before they are * executed @@ -1381,6 +1396,40 @@ class Database_Core { return $this; } + /** + * Count the number of records in the last query, without LIMIT or OFFSET applied. + * + * @return integer + */ + public function count_last_query() + { + if ($sql = $this->last_query()) + { + if (stripos($sql, 'LIMIT') !== FALSE) + { + // Remove LIMIT from the SQL + $sql = preg_replace('/\sLIMIT\s+[^a-z]+/i', ' ', $sql); + } + + if (stripos($sql, 'OFFSET') !== FALSE) + { + // Remove OFFSET from the SQL + $sql = preg_replace('/\sOFFSET\s+\d+/i', '', $sql); + } + + // Get the total rows from the last query executed + $result = $this->query + ( + 'SELECT COUNT(*) AS '.$this->escape_column('total_rows').' '. + 'FROM ('.trim($sql).') AS '.$this->escape_table('counted_results') + ); + + // Return the total number of rows from the query + return (int) $result->current()->total_rows; + } + + return FALSE; + } } // End Database Class diff --git a/kohana/libraries/Input.php b/kohana/libraries/Input.php index 04e8ee4d..4549bf4a 100644 --- a/kohana/libraries/Input.php +++ b/kohana/libraries/Input.php @@ -237,17 +237,18 @@ class Input_Core { if ($this->ip_address !== NULL) return $this->ip_address; - if ($ip = $this->server('HTTP_CLIENT_IP')) - { - $this->ip_address = $ip; - } - elseif ($ip = $this->server('REMOTE_ADDR')) - { - $this->ip_address = $ip; - } - elseif ($ip = $this->server('HTTP_X_FORWARDED_FOR')) + // Server keys that could contain the client IP address + $keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'); + + foreach ($keys as $key) { - $this->ip_address = $ip; + if ($ip = $this->server($key)) + { + $this->ip_address = $ip; + + // An IP address has been found + break; + } } if ($comma = strrpos($this->ip_address, ',') !== FALSE) diff --git a/kohana/libraries/ORM.php b/kohana/libraries/ORM.php index aa176089..4cbec50a 100644 --- a/kohana/libraries/ORM.php +++ b/kohana/libraries/ORM.php @@ -220,8 +220,16 @@ class ORM_Core { switch ($num_args) { case 0: - // Support for things like reset_select, reset_write, list_tables - return $this->db->$method(); + if (in_array($method, array('open_paren', 'close_paren', 'enable_cache', 'disable_cache'))) + { + // Should return ORM, not Database + $this->db->$method(); + } + else + { + // Support for things like reset_select, reset_write, list_tables + return $this->db->$method(); + } break; case 1: $this->db->$method($args[0]); @@ -1011,41 +1019,6 @@ class ORM_Core { } /** - * Count the number of records in the last query, without LIMIT or OFFSET applied. - * - * @return integer - */ - public function count_last_query() - { - if ($sql = $this->db->last_query()) - { - if (stripos($sql, 'LIMIT') !== FALSE) - { - // Remove LIMIT from the SQL - $sql = preg_replace('/\sLIMIT\s+[^a-z]+/i', ' ', $sql); - } - - if (stripos($sql, 'OFFSET') !== FALSE) - { - // Remove OFFSET from the SQL - $sql = preg_replace('/\sOFFSET\s+\d+/i', '', $sql); - } - - // Get the total rows from the last query executed - $result = $this->db->query - ( - 'SELECT COUNT(*) AS '.$this->db->escape_column('total_rows').' '. - 'FROM ('.trim($sql).') AS '.$this->db->escape_table('counted_results') - ); - - // Return the total number of rows from the query - return (int) $result->current()->total_rows; - } - - return FALSE; - } - - /** * Proxy method to Database list_fields. * * @param string table name @@ -1070,17 +1043,6 @@ class ORM_Core { } /** - * Proxy method to Database last_query. - * - * @return string - */ - public function last_query() - { - // Proxy to database - return $this->db->last_query(); - } - - /** * Proxy method to Database field_data. * * @chainable diff --git a/kohana/libraries/Validation.php b/kohana/libraries/Validation.php index 473425db..e2c8ff17 100644 --- a/kohana/libraries/Validation.php +++ b/kohana/libraries/Validation.php @@ -108,12 +108,12 @@ class Validation_Core extends ArrayObject { public function field_names() { // All the fields that are being validated - $fields = array_unique(array_merge + $fields = array_keys(array_merge ( - array_keys($this->pre_filters), - array_keys($this->rules), - array_keys($this->callbacks), - array_keys($this->post_filters) + $this->pre_filters, + $this->rules, + $this->callbacks, + $this->post_filters )); // Remove wildcard fields diff --git a/kohana/libraries/drivers/Cache/Memcache.php b/kohana/libraries/drivers/Cache/Memcache.php index f443b997..acd4c850 100644 --- a/kohana/libraries/drivers/Cache/Memcache.php +++ b/kohana/libraries/drivers/Cache/Memcache.php @@ -17,14 +17,11 @@ class Cache_Memcache_Driver implements Cache_Driver { protected $backend; protected $flags; - // The persistent lifetime value for expirations of 0 - protected $persistent_lifetime; - // Tags array - protected $tags; + protected static $tags; // Have the tags been changed? - protected $tags_changed = FALSE; + protected static $tags_changed = FALSE; public function __construct() { @@ -46,34 +43,34 @@ class Cache_Memcache_Driver implements Cache_Driver { or Kohana::log('error', 'Cache: Connection failed: '.$server['host']); } - // Set "persistent lifetime" value to one year - $this->persistent_lifetime = strtotime('now +1 year'); - // Load tags - $this->tags = $this->backend->get(self::TAGS_KEY); + self::$tags = $this->backend->get(self::TAGS_KEY); - if ( ! is_array($this->tags)) + if ( ! is_array(self::$tags)) { // Create a new tags array - $this->tags = array(); + self::$tags = array(); // Tags have been created - $this->tags_changed = TRUE; + self::$tags_changed = TRUE; } } public function __destruct() { - if ($this->tags_changed === TRUE) + if (self::$tags_changed === TRUE) { // Save the tags - $this->backend->set(self::TAGS_KEY, $this->tags, $this->flags, $this->persistent_lifetime); + $this->backend->set(self::TAGS_KEY, self::$tags, $this->flags, 0); + + // Tags are now unchanged + self::$tags_changed = FALSE; } } public function find($tag) { - if (isset($this->tags[$tag]) AND $results = $this->backend->get($this->tags[$tag])) + if (isset(self::$tags[$tag]) AND $results = $this->backend->get(self::$tags[$tag])) { // Return all the found caches return $results; @@ -95,22 +92,16 @@ class Cache_Memcache_Driver implements Cache_Driver { if ( ! empty($tags)) { // Tags will be changed - $this->tags_changed = TRUE; + self::$tags_changed = TRUE; foreach ($tags as $tag) { // Add the id to each tag - $this->tags[$tag][$id] = $id; + self::$tags[$tag][$id] = $id; } } - if ($lifetime === 0) - { - // Using an expiration of zero is unreliable, as memcache may delete - // it without warning. @see http://php.net/memcache_set - $lifetime = $this->persistent_lifetime; - } - else + if ($lifetime !== 0) { // Memcache driver expects unix timestamp $lifetime += time(); @@ -123,14 +114,14 @@ class Cache_Memcache_Driver implements Cache_Driver { public function delete($id, $tag = FALSE) { // Tags will be changed - $this->tags_changed = TRUE; + self::$tags_changed = TRUE; if ($id === TRUE) { if ($status = $this->backend->flush()) { // Remove all tags, all items have been deleted - $this->tags = array(); + self::$tags = array(); // We must sleep after flushing, or overwriting will not work! // @see http://php.net/manual/en/function.memcache-flush.php#81420 @@ -141,28 +132,28 @@ class Cache_Memcache_Driver implements Cache_Driver { } elseif ($tag === TRUE) { - if (isset($this->tags[$id])) + if (isset(self::$tags[$id])) { - foreach ($this->tags[$id] as $_id) + foreach (self::$tags[$id] as $_id) { // Delete each id in the tag $this->backend->delete($_id); } // Delete the tag - unset($this->tags[$id]); + unset(self::$tags[$id]); } return TRUE; } else { - foreach ($this->tags as $tag => $_ids) + foreach (self::$tags as $tag => $_ids) { - if (isset($this->tags[$tag][$id])) + if (isset(self::$tags[$tag][$id])) { // Remove the id from the tags - unset($this->tags[$tag][$id]); + unset(self::$tags[$tag][$id]); } } @@ -173,23 +164,23 @@ class Cache_Memcache_Driver implements Cache_Driver { public function delete_expired() { // Tags will be changed - $this->tags_changed = TRUE; + self::$tags_changed = TRUE; - foreach ($this->tags as $tag => $_ids) + foreach (self::$tags as $tag => $_ids) { foreach ($_ids as $id) { if ( ! $this->backend->get($id)) { // This id has disappeared, delete it from the tags - unset($this->tags[$tag][$id]); + unset(self::$tags[$tag][$id]); } } - if (empty($this->tags[$tag])) + if (empty(self::$tags[$tag])) { // The tag no longer has any valid ids - unset($this->tags[$tag]); + unset(self::$tags[$tag]); } } diff --git a/kohana/libraries/drivers/Database/Mysqli.php b/kohana/libraries/drivers/Database/Mysqli.php index 3d6bbfbf..13a81281 100644 --- a/kohana/libraries/drivers/Database/Mysqli.php +++ b/kohana/libraries/drivers/Database/Mysqli.php @@ -96,12 +96,6 @@ class Database_Mysqli_Driver extends Database_Mysql_Driver { throw new Kohana_Database_Exception('database.error', $this->show_error()); } - public function stmt_prepare($sql = '') - { - is_object($this->link) or $this->connect(); - return new Kohana_Mysqli_Statement($sql, $this->link); - } - public function escape_str($str) { if (!$this->db_config['escape']) diff --git a/kohana/libraries/drivers/Database/Pgsql.php b/kohana/libraries/drivers/Database/Pgsql.php index 9758de3c..62a33ad6 100644 --- a/kohana/libraries/drivers/Database/Pgsql.php +++ b/kohana/libraries/drivers/Database/Pgsql.php @@ -82,7 +82,8 @@ class Database_Pgsql_Driver extends Database_Driver { return self::$query_cache[$hash]; } - return new Pgsql_Result(pg_query($this->link, $sql), $this->link, $this->db_config['object'], $sql); + // Suppress warning triggered when a database error occurs (e.g., a constraint violation) + return new Pgsql_Result(@pg_query($this->link, $sql), $this->link, $this->db_config['object'], $sql); } public function set_charset($charset) @@ -161,12 +162,6 @@ class Database_Pgsql_Driver extends Database_Driver { return 'LIMIT '.$limit.' OFFSET '.$offset; } - public function stmt_prepare($sql = '') - { - is_object($this->link) or $this->connect(); - return new Kohana_Mysqli_Statement($sql, $this->link); - } - public function compile_select($database) { $sql = ($database['distinct'] == TRUE) ? 'SELECT DISTINCT ' : 'SELECT '; |