<?php defined('SYSPATH') or die('No direct script access.');
protected static $_current_databases = array(); protected static $_set_names;
public function connect() {
if ($this->_connection) return;
if (Database_MySQL::$_set_names === NULL) { Database_MySQL::$_set_names = ! function_exists('mysql_set_charset');}
extract($this->_config['connection'] + array('database' => 'klavarog', 'username' => 'bill', 'password' => 'gates'));
unset($this->_config['connection']['username'], $this->_config['connection']['password']);
try { if (empty($persistent)) { $this->_connection = mysql_connect($hostname, $username, $password, TRUE); } else { $this->_connection = mysql_pconnect($hostname, $username, $password);}} catch (ErrorException $e) { $this->_connection = NULL; throw $e; }
$this->_connection_id = sha1($hostname.'_'.$username.'_'.$password); $this->_select_db($database);
if ( ! empty($this->_config['charset'])) {$this->set_charset($this->_config['charset']);}}
protected function _select_db($database) {
if ( ! mysql_select_db($database, $this->_connection)) { throw new Database_Exception(':error', array(':error' => mysql_error($this->_connection)), mysql_errno($this->_connection)); }
Database_MySQL::$_current_databases[$this->_connection_id] = $database; }
public function disconnect() {
try { $status = TRUE; if (is_resource($this->_connection)) { $status = mysql_close($this->_connection); }} catch (Exception $e) { $status = is_resource($this->_connection); }
return $status; }
public function set_charset($charset) {
$this->_connection or $this->connect();
if (Database_MySQL::$_set_names === TRUE) { $status = (bool) mysql_query('SET NAMES '.$this->quote($charset), $this->_connection); } else { $status = mysql_set_charset($charset, $this->_connection); }
if ($status === FALSE) { throw new Database_Exception(':error', array(':error' => mysql_error($this->_connection)), mysql_errno($this->_connection)); }}
public function query($type, $sql, $as_object) {
$this->_connection or $this->connect();
if ( ! empty($this->_config['profiling'])) { $benchmark = Profiler::start("Database ({$this->_instance})", $sql); }
if ( ! empty($this->_config['connection']['persistent']) AND $this->_config['connection']['database'] !== Database_MySQL::$_current_databases[$this->_connection_id]) { $this->_select_db($this->_config['connection']['database']); }
if (($result = mysql_query($sql, $this->_connection)) === FALSE) {
if (isset($benchmark)) { Profiler::delete($benchmark); }
throw new Database_Exception(':error [ :query ]', array(':error' => mysql_error($this->_connection), ':query' => $sql), mysql_errno($this->_connection)); }
if (isset($benchmark)) { Profiler::stop($benchmark); } $this->last_query = $sql;
if ($type === Database::SELECT) { return new Database_MySQL_Result($result, $sql, $as_object); } elseif ($type === Database::INSERT) { return array(mysql_insert_id($this->_connection), mysql_affected_rows($this->_connection), ); } else { return mysql_affected_rows($this->_connection); }}
public function datatype($type) {
static $types = array('blob' => array('type' => 'string', 'binary' => TRUE, 'character_maximum_length' => '65535'), 'bool' => array('type' => 'bool'), 'year' => array('type' => 'string'));
$type = str_replace(' zerofill', '', $type); if (isset($types[$type])) return $types[$type];
return parent::datatype($type); }
public function list_tables($like = NULL) { if (is_string($like)) { $result = $this->query(Database::SELECT, 'SHOW TABLES LIKE '.$this->quote($like), FALSE); } else { $result = $this->query(Database::SELECT, 'SHOW TABLES', FALSE); }
$tables = array(); foreach ($result as $row) { $tables[] = reset($row); } return $tables; }
public function list_columns($table, $like = NULL) {
$table = $this->quote_table($table); if (is_string($like)) { $result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table.' LIKE '.$this->quote($like), FALSE); } else { $result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table, FALSE); } $count = 0; $columns = array();
foreach ($result as $row) {
list($type, $length) = $this->_parse_type($row['Type']);
$column = $this->datatype($type); $column['column_name'] = $row['Field']; $column['column_default'] = $row['Default'];
switch ($column['type']) {
case 'float': if (isset($length)) { list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length); } break;
case 'varchar': $column['character_maximum_length'] = $length; case 'text': break; }
$column['comment'] = $row['Comment']; $column['extra'] = $row['Extra']; $columns[$row['Field']] = $column; } return $columns; }
public function escape($value) {
$this->_connection or $this->connect();
if (($value = mysql_real_escape_string((string) $value, $this->_connection)) === FALSE) { throw new Database_Exception(':error', array(':error' => mysql_errno($this->_connection)), mysql_error($this->_connection)); }
return "'$value'"; }
Зарегистрируйтесь, чтобы начать пользоваться.
` ~
1 !
2 @
3 #
4 $
5 %
6 ^
7 &
8 *
9 (
0 )
- _
= +
Bsp
Shift
Z
X
C
V
B
N
M
, <
. >
/ ?
Shift