Skip to content

Commit

Permalink
增加Service\DB\Table,数据库表对象
Browse files Browse the repository at this point in the history
  • Loading branch information
yeaha committed May 30, 2016
1 parent a9970f3 commit 411adbc
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 32 deletions.
66 changes: 35 additions & 31 deletions src/Service/DB/Adapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Owl\Service\DB;

use Owl\Application as App;
Expand All @@ -24,34 +23,34 @@ abstract public function getTables();
* @param string $table
*
* @return [
* (string) => [
* 'primary_key' => (boolean),
* 'type' => (string),
* 'sql_type' => (string),
* 'character_max_length' => (integer),
* 'numeric_precision' => (integer),
* 'numeric_scale' => (integer),
* 'default_value' => (mixed),
* 'not_null' => (boolean),
* 'comment' => (string),
* ],
* ...
* ]
* (string) => [
* 'primary_key' => (boolean),
* 'type' => (string),
* 'sql_type' => (string),
* 'character_max_length' => (integer),
* 'numeric_precision' => (integer),
* 'numeric_scale' => (integer),
* 'default_value' => (mixed),
* 'not_null' => (boolean),
* 'comment' => (string),
* ],
* ...
* ]
*/
abstract public function getColumns($table);

/**
* @param string $table
*
* @return [
* [
* 'name' => (string),
* 'columns' => (array),
* 'is_primary' => (boolean),
* 'is_unique' => (boolean),
* ],
* ...
* ]
* [
* 'name' => (string),
* 'columns' => [(string), ...],
* 'is_primary' => (boolean),
* 'is_unique' => (boolean),
* ],
* ...
* ]
*/
abstract public function getIndexes($table);

Expand All @@ -78,8 +77,8 @@ public function __sleep()
public function __call($method, array $args)
{
return $args
? call_user_func_array([$this->connect(), $method], $args)
: $this->connect()->$method();
? call_user_func_array([$this->connect(), $method], $args)
: $this->connect()->$method();
}

public function isConnected()
Expand Down Expand Up @@ -185,8 +184,8 @@ public function inTransaction()
public function execute($sql, $params = null)
{
$params = $params === null
? []
: is_array($params) ? $params : array_slice(func_get_args(), 1);
? []
: is_array($params) ? $params : array_slice(func_get_args(), 1);

App::log('debug', 'database execute', [
'sql' => ($sql instanceof \PDOStatement) ? $sql->queryString : $sql,
Expand Down Expand Up @@ -255,6 +254,11 @@ public function select($table)
return new \Owl\Service\DB\Select($this, $table);
}

public function getTable($table)
{
return new \Owl\Service\DB\Table($this, $table);
}

public function insert($table, array $row)
{
$params = [];
Expand All @@ -272,8 +276,8 @@ public function insert($table, array $row)
public function update($table, array $row, $where = null, $params = null)
{
$where_params = ($where === null || $params === null)
? []
: is_array($params) ? $params : array_slice(func_get_args(), 3);
? []
: is_array($params) ? $params : array_slice(func_get_args(), 3);

$params = [];
foreach ($row as $value) {
Expand All @@ -294,8 +298,8 @@ public function update($table, array $row, $where = null, $params = null)
public function delete($table, $where = null, $params = null)
{
$params = ($where === null || $params === null)
? []
: is_array($params) ? $params : array_slice(func_get_args(), 2);
? []
: is_array($params) ? $params : array_slice(func_get_args(), 2);

$sth = $this->prepareDelete($table, $where);

Expand Down Expand Up @@ -365,7 +369,7 @@ public function prepareDelete($table, $where = null)

protected function rollbackAll()
{
$max = 9; // 最多9次,避免死循环
$max = 9; // 最多9次,避免死循环
while ($this->in_transaction && $max-- > 0) {
$this->rollback();
}
Expand Down
7 changes: 6 additions & 1 deletion src/Service/DB/Sqlite/Adapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Owl\Service\DB\Sqlite;

if (!extension_loaded('pdo_sqlite')) {
Expand All @@ -17,13 +16,19 @@ public function lastID($table = null, $column = null)

public function getTables()
{
// @FIXME
throw new \Exception('Sqlite\Adapter::getTables() not implement');
}

public function getColumns($table)
{
// @FIXME
throw new \Exception('Sqlite\Adapter::getColumns() not implement');
}

public function getIndexes($table)
{
// @FIXME
throw new \Exception('Sqlite\Adapter::getIndexes() not implement');
}
}
167 changes: 167 additions & 0 deletions src/Service/DB/Table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
namespace Owl\Service\DB;

class Table
{
protected $adapter;
protected $table_name;
protected $columns;
protected $indexes;

public function __construct(Adapter $adapter, $table_name)
{
$this->adapter = $adapter;
$this->table_name = $table_name;
}

/**
* 获得表名
*
* @return string
*/
public function getName()
{
return $this->table_name;
}

/**
* 获得数据库连接对象
*
* @return \Owl\Service\DB\Adapter
*/
public function getAdapter()
{
return $this->adapter;
}

/**
* 获得字段信息
*
* @see \Owl\Service\DB\Adapter
* @return array
*/
public function getColumns()
{
if ($this->columns === null) {
$this->columns = $this->adapter->getColumns($this->table_name);
}

return $this->columns;
}

/**
* 检查字段是否存在
*
* @param string $column_name
* @return bool
*/
public function hasColumn($column_name)
{
$columns = $this->getColumns();

return isset($columns[$column_name]);
}

/**
* 获得索引信息
*
* @return array
*/
public function getIndexes()
{
if ($this->indexes === null) {
$this->indexes = $this->adapter->getIndexes($this->table_name);
}

return $this->indexes;
}

/**
* 检查索引是否存在
*
* @param string $index_name
* @return bool
*/
public function hasIndex($index_name)
{
foreach ($this->getIndexes() as $index) {
if ($index['name'] === $index_name) {
return true;
}
}

return false;
}

/**
* 获得指定字段上的索引
*
* @param string $column_name
* @return array
*/
public function getIndexesOfColumn($column_name)
{
$result = [];

foreach ($this->getIndexes() as $index) {
if (in_array($column_name, $index['columns'])) {
$result[] = $index;
}
}

return $result;
}

/**
* 创建查询对象
*
* @return \Owl\Service\DB\Select
*/
public function select()
{
return $this->adapter->select($this->table_name);
}

/**
* 插入一条记录,返回插入的行数
*
* @param array $row
* @return int
*/
public function insert(array $row)
{
return $this->adapter->insert($this->table_name, $row);
}

/**
* 更新记录,允许指定条件
* 返回被更新的行数
*
* @param array $row
* @param string $where
* @param mixed $parameters
* @return int
*/
public function update(array $row/*, $where = null, $parameters = null*/)
{
$args = func_get_args();
array_unshift($args, $this->table_name);

return call_user_func_array([$this->adapter, 'update'], $args);
}

/**
* 删除记录,允许指定条件
*
* @param string $where
* @param mixed $parameters
* @return int
*/
public function delete( /*$where = null, $parameters = null*/)
{
$args = func_get_args();
array_unshift($args, $this->table_name);

return call_user_func_array([$this->adapter, 'delete'], $args);
}
}

0 comments on commit 411adbc

Please sign in to comment.