123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace KIF\Dao;
- use KIF\Verify;
- use KIF\Exception\ParamsException;
- use KIF\Data\ResultWrapper;
- abstract class DBAgileDev extends AbstractDao {
-
- protected $other_field = array();
-
- private $real_field;
-
- public function __construct($master_flag = 'default') {
- parent::__construct($master_flag);
-
- $fixed_field = array(
- $this->primaryKey,
- 'extend',
- 'create_time',
- 'update_time',
- 'cas_token',
- );
- $this->real_field = array_merge($fixed_field, $this->other_field);
- }
-
- public function add(array $info, $actionType = parent::PARAM_CREATE_ACTION_INSERT, array $onDuplicate = array()) {
- if (!isset($info['create_time']) || !Verify::unsignedInt($info['create_time'])) {
- $info['create_time'] = time();
- }
- if (!isset($info['update_time']) || !Verify::unsignedInt($info['update_time'])) {
- $info['update_time'] = time();
- }
- $extend = array();
- foreach ($info as $tmpK => $tmpV) {
- if (!in_array($tmpK, $this->real_field)) {
- $extend[$tmpK] = $tmpV;
- unset($info[$tmpK]);
- }
- }
-
- $info['cas_token'] = 0;
- $tableInfo = $info;
- $tableInfo['extend'] = serialize($extend);
- switch ($actionType) {
- case parent::PARAM_CREATE_ACTION_INSERT:
- $id = $this->create($tableInfo);
- break;
- case parent::PARAM_CREATE_ACTION_INSERT_IGNORE:
- $id = $this->insertIgnore($tableInfo);
- break;
- case parent::PARAM_CREATE_ACTION_REPLACE:
- $id = $this->replace($tableInfo);
- break;
- case parent::PARAM_CREATE_ACTION_ONDUPLICATE:
- if (empty($onDuplicate)) {
- $onDuplicate = $tableInfo;
- unset($onDuplicate['create_time']);
- unset($onDuplicate['cas_token']);
- } else {
-
- foreach ($onDuplicate as $tmpK => $tmpV) {
- if (!in_array($tmpK, $this->real_field)) {
- unset($onDuplicate[$tmpK]);
- }
- }
- }
- $onDuplicate['update_time'] = $info['update_time'];
- $onDuplicate['cas_token'] = SqlHelper::wrapperNoQuote("`cas_token` + 1");
- $id = $this->insertDuplicate($tableInfo, $onDuplicate);
- break;
- default:
- throw new ParamsException("invalid actionType");
- }
- return $id;
- }
-
- public function modify(array $info, array $condition = null, $cas_token = null) {
- if (!isset($info[$this->primaryKey])) {
- if (!isset($condition[$this->primaryKey])) {
- return ResultWrapper::fail("请指定要修改的记录");
- }
- $info[$this->primaryKey] = $condition[$this->primaryKey];
- }
- if (!Verify::unsignedInt($info[$this->primaryKey])) {
- return ResultWrapper::fail("请指定有效的记录");
- }
- if (!isset($condition)) {
- $condition = array();
- }
- $useMasterFlag = $this->db->beginUseMaster();
- $oldSpecialSale = parent::get($info[$this->primaryKey]);
- $this->db->restore($useMasterFlag);
- if (!$oldSpecialSale) {
- return ResultWrapper::fail("不存在的记录:{$info[$this->primaryKey]}");;
- }
-
- $extend = unserialize($oldSpecialSale['extend']);
- foreach ($info as $tmpK => $tmpV) {
- if (!in_array($tmpK, $this->real_field)) {
- $extend[$tmpK] = $tmpV;
- unset($info[$tmpK]);
- }
- }
- $info['cas_token'] = SqlHelper::wrapperNoQuote('`cas_token` + 1');
- $tableInfo = $info;
- $tableInfo['extend'] = serialize($extend);
- $tableInfo['update_time'] = time();
- $condition[$this->primaryKey] = $info[$this->primaryKey];
- if (Verify::naturalNumber($cas_token)) {
- $condition['cas_token'] = $cas_token;
- }
- $result = $this->update($tableInfo, $condition);
- if (!$result) {
- return ResultWrapper::fail("数据库update操作失败");
- }
- $affectedRows = $this->db->affectedRows();
- if (!Verify::unsignedInt($affectedRows)) {
- if (Verify::naturalNumber($cas_token)) {
- return ResultWrapper::fail("CAS_TOKEN_NOT_MATCH");
- } else {
- return ResultWrapper::fail("NOT_MATCH");
- }
- }
- return ResultWrapper::success();
- }
-
- public function get($id, & $cas_token = null) {
- if (!Verify::unsignedInt($id)) {
- return false;
- }
- $result = $this->gets(array($id));
- if (!$result) {
- return false;
- }
- $tmpResult = array_pop($result);
- $cas_token = $tmpResult['cas_token'];
- return $tmpResult;
- }
-
- public function gets(array $ids, array & $cas_tokens = null) {
- $result = parent::gets($ids);
- if (!$result) {
- return $result;
- }
- foreach ($result as & $tmpV) {
- $tmpExtend = array();
- $tmpExtend = unserialize($tmpV['extend']);
- unset($tmpV['extend']);
-
- if (is_array($tmpExtend)) foreach ($tmpExtend as $tmpKK => $tmpVV) {
- if (!in_array($tmpKK, $this->real_field)) {
- $tmpV[$tmpKK] = $tmpVV;
- }
- }
- $cas_tokens[$tmpV[$this->primaryKey]] = $tmpV['cas_token'];
- }
- return $result;
- }
-
- public function getsIdsAll($order = null, $limit = null) {
- $ids = parent::getsAllIds($order, $limit);
- return $ids;
- }
- public function fetchAll($condition = null, $limit = 10, $order = null, $selectCols = '*') {
- $result = parent::findBy($condition, null, $limit, $selectCols, $order);
- if (!$result) {
- return false;
- }
- foreach ($result as & $tmpV) {
- $tmpExtend = array();
- $tmpExtend = unserialize($tmpV['extend']);
- unset($tmpV['extend']);
-
- if (is_array($tmpExtend)) foreach ($tmpExtend as $tmpKK => $tmpVV) {
- if (!in_array($tmpKK, $this->real_field)) {
- $tmpV[$tmpKK] = $tmpVV;
- }
- }
- }
- return $result;
- }
-
- public function getsLatestIdsByUpdateTime($update_time, $limit = null) {
- $order = 'update_time ASC';
- $condition = array(
- 'update_time' => SqlHelper::addCompareOperator('>=', $update_time),
- );
- return $this->findIdsBy($condition, $limit, $order);
- }
- }
|