public function getPair($record = null, $showEmpty = false) { if (!empty($this->setting)) { if ($this->setting[0] == 'getPair') { $where = $this->setting[4]; if (!empty($where)) { $where = json_decode($where, true); foreach ($where as $k => $v) { if ($v[0] == '{' && substr($v, -1) == '}') { $v = substr($v, 1, -1); $where[$k] = $record[$v]; } } } $r = \Sooh\DB\Broker::getInstance($this->setting[1])->getPair($this->setting[2], $this->setting[3], $this->setting[4], $where); } else { throw new \ErrorException('unknown getPairMethod'); } } else { $r = $this->arr; } if ($showEmpty && $this->captForEmpty) { $r[''] = $this->captForEmpty; } $this->pairVals = $r; }
protected function getDB() { $dbid = 'default'; // $pre = \Sooh\Base\Ini::getInstance()->get('dbConf.'.$dbid); // $pre = $pre['dbEnums']['default']; $pre = 'db_monitor'; $this->tbname = $pre . '.tb_crond_log'; return \Sooh\DB\Broker::getInstance($dbid); }
public function write($logData) { if ($this->rpc !== null) { return $this->rpc->initArgs(array('logData' => $logData))->send(__FUNCTION__); } else { $dt = \Sooh\Base\Time::getInstance(); \Sooh\DB\Broker::getInstance()->addLog('db_monitor.tb_error_log', array('ymd' => $dt->YmdFull, 'hhiiss' => $dt->his, 'msg' => $logData)); return 'done'; } }
/** * 循环遍历用的所有的数据库连接标识和表(不包括cache) * 回调函数接受两个参数:dbBroker和表名 * @param type $callback_dbid_fulltbname */ protected function loop($callback_dbid_fulltbname, $searchInCache = false) { $bak = $this->pkey_val; try { $numSplit = $this->numToSplit[0]; if (is_callable($callback_dbid_fulltbname)) { for ($i = 0; $i < $numSplit; $i++) { $this->pkey_val = $i; $dbid = $this->_allHosts[$searchInCache ? 1 : 0][$i % $numSplit]; $tbname = $this->tbname($searchInCache); $callback_dbid_fulltbname(\Sooh\DB\Broker::getInstance($dbid), $tbname); } } else { for ($i = 0; $i < $numSplit; $i++) { $this->pkey_val = $i; $dbid = $this->_allHosts[$searchInCache ? 1 : 0][$i % $numSplit]; $tbname = $this->tbname($searchInCache); call_user_func($callback_dbid_fulltbname, \Sooh\DB\Broker::getInstance($dbid), $tbname); } } } catch (\ErrorException $e) { error_log("error found when " . get_called_class() . '->loop():' . $e->getMessage() . "\n" . $e->getTraceAsString()); } $this->pkey_val = $bak; }
/** * 删除过期日志 * @param int $dayExpired (默认删除190天前) */ public function remoreCrondLogExpired($dayExpired = 190) { $dt = \Sooh\Base\Time::getInstance()->getInstance()->timestamp(-$dayExpired); \Sooh\DB\Broker::getInstance($this->dbConfID)->delRecords($this->tbName, array('ymdh<' => date('YmdH', $dt))); }
/** * * @param string $type [idfa|imei|md5|mac] * @param string $sn * @param string $phone * @param string $userIdentifier * @param string $contractId * @param array $extraData * @return \PrjLib\DataDig\Log\Device */ public static function ensureOne($type, $sn, $phone = null, $userIdentifier = null, $contractId = null, $extraData = null) { $dt = \Sooh\Base\Time::getInstance(); $deviceId = self::guidMaker($type, $sn); $ddd = \Yaf_Dispatcher::getInstance()->getRequest(); error_log("trace device->ensure(" . $ddd->getModuleName() . '/' . $ddd->getControllerName() . '/' . $ddd->getActionName() . ") " . $deviceId . " phone:{$phone} user:{$userIdentifier}"); $sys = parent::getCopy(array('deviceId' => $deviceId)); try { \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists); $sys->load(); } catch (\ErrorException $e) { if (\Sooh\DB\Broker::errorIs($e, \Sooh\DB\Error::tableNotExists)) { $sys->createTable(); } } $fields = array('ip' => \Sooh\Base\Tools::remoteIP(), 'ymd' => $dt->YmdFull, 'hhiiss' => $dt->his); try { if ($sys->exists() === false) { foreach ($fields as $k => $v) { $sys->setField($k, $v); } $sys->setField('phone', empty($phone) ? '0' : $phone); $sys->setField('userIdentifier', empty($userIdentifier) ? '' : $userIdentifier); $sys->setField('extraData', empty($extraData) ? '' : json_encode($extraData)); $sys->setField('extraRet', ''); $sys->setField('contractId', empty($contractId) ? '0' : $contractId); try { \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::duplicateKey); $sys->update(); $sys->flgNewCreate = true; return $sys; } catch (\ErrorException $e) { $sys->reload(); if ($sys->exists() === false) { error_log('error create new device log:' . $e->getMessage() . "\n" . $e->getTraceAsString()); return $sys; } } } $oldPhone = $sys->getField('phone', true); $oldUser = $sys->getField('userIdentifier', true); $oldContractId = $sys->getField('contractId', true); if (!empty($phone) && !empty($oldPhone) && $phone != $oldPhone || !empty($oldUser) && !empty($userIdentifier) && $oldUser != $userIdentifier || !empty($oldContractId) && !empty($contractId) && $oldContractId != $contractId) { $extraDataOld = $sys->getField('extraData', true); $extraRetOld = $sys->getField('extraRet', true); $extraRetOld = is_scalar($extraRetOld) === false ? json_encode($extraRetOld) : $extraRetOld; \Sooh\DB\Broker::getInstance(\PrjLib\Tbname::db_rpt)->addRecord(\PrjLib\Tbname::tb_device_log, array('deviceId' => $deviceId, 'dtChange' => $dt->YmdFull . sprintf('%06d', $dt->his), 'phoneOld' => $oldPhone, 'userIdentifierOld' => $oldUser, 'extraDataOld' => is_scalar($extraDataOld) === false ? json_encode($extraDataOld) : $extraDataOld, 'extraRetOld' => $extraRetOld, 'contractIdOld' => $oldContractId, 'phoneNew' => $phone, 'userIdentifierNew' => $userIdentifier, 'extraDataNew' => empty($extraData) ? '' : json_encode($extraData), 'extraRetNew' => $extraRetOld, 'contractIdNew' => empty($contractId) ? '0' : $contractId, 'ipOld' => $sys->getField('ip', true), 'ipNew' => $fields['ip'])); } foreach ($fields as $k => $v) { $sys->setField($k, $v); } if (!empty($extraData)) { $sys->setField('extraData', empty($extraData) ? '' : json_encode($extraData)); } if (!empty($phone)) { $sys->setField('phone', $phone); } if (!empty($userIdentifier)) { $sys->setField('userIdentifier', $userIdentifier); } //$sys->setField('extraRet', ''); if (!empty($contractId)) { $sys->setField('contractId', $contractId); } $sys->update(); //var_log($sys->dump(),'======================log->filled for '.$sys->tbname()); \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists); } catch (\ErrorException $e) { error_log("error: on ensure-device:" . $e->getMessage() . "\n" . $e->getTraceAsString()); } return $sys; }
/** * * @return \Soob\DB\Interfaces\All */ protected function getDB() { return \Sooh\DB\Broker::getInstance($this->driver); }
/** * * @return \Soob\DB\Interfaces\All */ protected function getDB($dbid) { return \Sooh\DB\Broker::getInstance($dbid); }
protected static function getDBAndTbNameById(&$tbnameToSet, $splitedId, $isCache = false) { $dbByObj = static::idFor_dbByObj_InConf($isCache); $ini = sooh_ini::getInstance(); $dbId = $ini->get('dbByObj.' . $dbByObj); if (is_array($dbId)) { $i = $splitedId % (sizeof($dbId) - 1); $confIDStr = 'dbConf.' . $dbId[$i + 1]; } elseif (!empty($dbId)) { $confIDStr = 'dbConf.' . $dbId; } else { $tmp = $ini->get('dbByObj.default'); if (empty($tmp)) { $confIDStr = 'dbConf.default'; } else { if (is_array($tmp)) { $i = $splitedId % (sizeof($tmp) - 1); $confIDStr = 'dbConf.' . $tmp[$i + 1]; } else { $confIDStr = 'dbConf.' . $tmp; } } } $conf = $ini->get($confIDStr); if (empty($conf)) { error_log('try find ' . $confIDStr . ' in dbConf failed for ' . $dbByObj); return null; } $db = sooh_dbBroker::getInstance($conf, $dbByObj); if (isset($conf['dbEnums'][$dbByObj])) { $dbname = $conf['dbEnums'][$dbByObj]; } else { $dbname = $conf['dbEnums']['default']; } $tbnameToSet = $dbname . '.' . static::splitedTbName($splitedId, $isCache); return $db; }