Query类增加分表规则方法partition

This commit is contained in:
thinkphp
2016-05-29 11:49:08 +08:00
parent f4e3a3466c
commit 14de14a1eb

View File

@@ -39,8 +39,6 @@ class Query
protected $options = []; protected $options = [];
// 参数绑定 // 参数绑定
protected $bind = []; protected $bind = [];
// 分表规则
protected $partition = [];
/** /**
* 架构函数 * 架构函数
@@ -262,42 +260,44 @@ class Query
* 得到分表的的数据表名 * 得到分表的的数据表名
* @access public * @access public
* @param array $data 操作的数据 * @param array $data 操作的数据
* @param string $field 分表依据的字段
* @param array $rule 分表规则
* @return string * @return string
*/ */
public function getPartitionTableName($data = []) public function getPartitionTableName($data, $field, $rule = [])
{ {
// 对数据表进行分区 // 对数据表进行分区
if (isset($data[$this->partition['field']])) { if ($field && isset($data[$field])) {
$field = $data[$this->partition['field']]; $value = $data[$field];
switch ($this->partition['type']) { $type = $rule['type'];
switch ($type) {
case 'id': case 'id':
// 按照id范围分表 // 按照id范围分表
$step = $this->partition['expr']; $step = $rule['expr'];
$seq = floor($field / $step) + 1; $seq = floor($value / $step) + 1;
break; break;
case 'year': case 'year':
// 按照年份分表 // 按照年份分表
if (!is_numeric($field)) { if (!is_numeric($value)) {
$field = strtotime($field); $value = strtotime($value);
} }
$seq = date('Y', $field) - $this->partition['expr'] + 1; $seq = date('Y', $value) - $rule['expr'] + 1;
break; break;
case 'mod': case 'mod':
// 按照id的模数分表 // 按照id的模数分表
$seq = ($field % $this->partition['num']) + 1; $seq = ($value % $rule['num']) + 1;
break; break;
case 'md5': case 'md5':
// 按照md5的序列分表 // 按照md5的序列分表
$seq = (ord(substr(md5($field), 0, 1)) % $this->partition['num']) + 1; $seq = (ord(substr(md5($value), 0, 1)) % $rule['num']) + 1;
break; break;
default: default:
if (function_exists($this->partition['type'])) { if (function_exists($type)) {
// 支持指定函数哈希 // 支持指定函数哈希
$fun = $this->partition['type']; $seq = (ord(substr($type($value), 0, 1)) % $rule['num']) + 1;
$seq = (ord(substr($fun($field), 0, 1)) % $this->partition['num']) + 1;
} else { } else {
// 按照字段的首字母的值分表 // 按照字段的首字母的值分表
$seq = (ord($field{0}) % $this->partition['num']) + 1; $seq = (ord($value{0}) % $rule['num']) + 1;
} }
} }
return $this->getTable() . '_' . $seq; return $this->getTable() . '_' . $seq;
@@ -305,7 +305,7 @@ class Query
// 当设置的分表字段不在查询条件或者数据中 // 当设置的分表字段不在查询条件或者数据中
// 进行联合查询,必须设定 partition['num'] // 进行联合查询,必须设定 partition['num']
$tableName = []; $tableName = [];
for ($i = 0; $i < $this->partition['num']; $i++) { for ($i = 0; $i < $rule['num']; $i++) {
$tableName[] = 'SELECT * FROM ' . $this->getTable() . '_' . ($i + 1); $tableName[] = 'SELECT * FROM ' . $this->getTable() . '_' . ($i + 1);
} }
@@ -751,6 +751,20 @@ class Query
return $this; return $this;
} }
/**
* 设置分表规则
* @access public
* @param array $data 操作的数据
* @param string $field 分表依据的字段
* @param array $rule 分表规则
* @return $this
*/
public function partition($data, $field, $rule = [])
{
$this->options['table'] = $this->getPartitionTableName($data, $field, $rule);
return $this;
}
/** /**
* 指定查询条件 * 指定查询条件
* @access public * @access public