Skip to content

Commit

Permalink
数据库类库增加recovery_last_builder()方法,可以恢复上一次被reset()时的builder
Browse files Browse the repository at this point in the history
reset()方法可以重置单个builder,比如reset('select')
  • Loading branch information
breath-co2 committed Aug 7, 2013
1 parent c7c5a53 commit 11dca0b
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 80 deletions.
70 changes: 40 additions & 30 deletions core/classes/database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function close_connect()
*/
public function query($sql, $as_object = false, $use_master = null)
{
if ( null === $use_master && true === $this->is_auto_use_master )
if (null === $use_master && true === $this->is_auto_use_master)
{
$use_master = true;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public function table_prefix()
* @param boolean $use_master 当$type=select此参数有效,设置true则使用主数据库,设置false则使用从数据库,不设置则使用默认
* @return string
*/
public function compile($type = 'select', $use_master=null)
public function compile($type = 'select', $use_master = null)
{
if ( $type=='select' && null === $use_master && true === $this->is_auto_use_master )
{
Expand All @@ -330,7 +330,7 @@ public function compile($type = 'select', $use_master=null)
*/
public function get($as_object = false, $use_master = null)
{
return $this->query($this->compile('select',$use_master), $as_object, $use_master);
return $this->query($this->compile('select', $use_master), $as_object, $use_master);
}

/**
Expand All @@ -342,7 +342,7 @@ public function get($as_object = false, $use_master = null)
*/
public function get_one($as_object = false, $use_master = null)
{
return $this->get($as_object,$use_master)->current();
return $this->get($as_object, $use_master)->current();
}

/**
Expand All @@ -365,15 +365,15 @@ public function last_query()
*/
public function update($table = null, $value = null, $where = null)
{
if ( $table )
if ($table)
{
$this->table($table);
}
if ( $value )
if ($value)
{
$this->set($value);
}
if ( $where )
if ($where)
{
$this->where($where);
}
Expand All @@ -392,11 +392,11 @@ public function update($table = null, $value = null, $where = null)
*/
public function insert($table = null, $value = null)
{
if ( $table )
if ($table)
{
$this->table($table);
}
if ( $value )
if ($value)
{
$this->columns(array_keys($value));
$this->values($value);
Expand All @@ -415,11 +415,11 @@ public function insert($table = null, $value = null)
*/
public function delete($table = null, $where = null)
{
if ( $table )
if ($table)
{
$this->table($table);
}
if ( $where )
if ($where)
{
$this->where($where);
}
Expand All @@ -436,17 +436,26 @@ public function delete($table = null, $where = null)
*/
public function count_records($table = null, $where = null)
{
if ( $table )
if ($table)
{
$this->from($table);
}
if ( $where )
if ($where)
{
$this->where($where);
}

// 记录当前builder信息
$builder = $this->_builder;

$this->select($this->expr_value('COUNT(1) AS `total_row_count`'));

return (int)$this->query($this->compile('select'), false)->get('total_row_count');
$count = (int)$this->query($this->compile('select'), false)->get('total_row_count');

// 将之前获取的builder信息放倒_builder_bak上,以便可使用->recovery_last_builder()方法恢复前一个builder
$this->_builder_bak = $builder;

return $count;
}

/**
Expand All @@ -472,21 +481,22 @@ public function replace($table = null, $value = null, $where = null)
*/
public function merge($table = null, $value = null, $where = null)
{
if ( $table )
if ($table)
{
$this->table($table);
}
if ( $value )
if ($value)
{
$this->columns(array_keys($value));
$this->values($value);
}
if ( $where )
if ($where)
{
$this->where($where);
}

$sql = $this->compile('replace');
return $this->query($sql, false , true);
return $this->query($sql, false, true);
}

/**
Expand Down Expand Up @@ -532,11 +542,11 @@ public function is_auto_use_master()
* @return boolean
* @throws Exception
*/
public function create_database( $database, $charset = null, $collate=null )
public function create_database($database, $charset = null, $collate=null)
{
if (method_exists($this->driver, 'create_database'))
{
return $this->driver->create_database($database,$charset,$collate);
return $this->driver->create_database($database, $charset, $collate);
}
else
{
Expand Down Expand Up @@ -565,9 +575,9 @@ public static function parse_dsn($dsn)
);

// Get the protocol and arguments
list ( $db['type'], $connection ) = explode('://', $dsn, 2);
list ($db['type'], $connection) = explode('://', $dsn, 2);

if ( $connection[0] === '/' )
if ($connection[0] === '/')
{
// Strip leading slash
$db['database'] = substr($connection, 1);
Expand All @@ -576,34 +586,34 @@ public static function parse_dsn($dsn)
{
$connection = parse_url('http://' . $connection);

if ( isset($connection['user']) )
if (isset($connection['user']))
{
$db['username'] = $connection['user'];
}

if ( isset($connection['pass']) )
if (isset($connection['pass']))
{
$db['password'] = $connection['pass'];
}

if ( isset($connection['port']) )
if (isset($connection['port']))
{
$db['port'] = $connection['port'];
}

if ( isset($connection['host']) )
if (isset($connection['host']))
{
if ( $connection['host'] === 'unix(' )
if ($connection['host'] === 'unix(')
{
list ( $db['persistent'], $connection['path'] ) = explode(')', $connection['path'], 2);
list ($db['persistent'], $connection['path']) = explode(')', $connection['path'], 2);
}
else
{
$db['hostname'] = $connection['host'];
}
}

if ( isset($connection['path']) && $connection['path'] )
if (isset($connection['path']) && $connection['path'])
{
// Strip leading slash
$db['database'] = trim(trim(substr($connection['path'], 1),'/'));
Expand All @@ -622,7 +632,7 @@ public static function close_all_connect()

foreach ( Database::$instances as $database )
{
if ( $database instanceof Database )
if ($database instanceof Database)
{
$database->close_connect();
}
Expand Down
Loading

0 comments on commit 11dca0b

Please sign in to comment.