123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace JinDouYun\Dao;
- use Mall\Framework\Core\SqlHelper;
- use Mall\Framework\Factory;
- use JinDouYun\Cache\AllTableNameCache;
- class BaseDao extends SqlHelper
- {
- /**
- * @var \Mall\Framework\SearchClient\Client
- */
- private $search;
- /**
- * 当前操作的数据库配置标识
- * @var $serviceDB
- */
- private $serviceDB;
- /**
- * 参考文章: https://www.php.cn/php-weizijiaocheng-403412.html
- * 开启事务数量统计
- * BaseDao constructor.
- * @param string $serviceDB
- */
- private $transactions;
- public function __construct($serviceDB = 'default')
- {
- $this->serviceDB = $serviceDB;
- parent::__construct($serviceDB);
- //$this->setSearchIndex($serviceDB);
- }
- /**
- * 设置搜索引擎配置项
- *
- * @param string $serviceDB
- *
- * @throws \Exception
- * @return \Mall\Framework\SearchClient\Client
- */
- public function setSearchIndex($serviceDB)
- {
- $this->search = Factory::search($serviceDB);
- return $this->search;
- }
- /**
- * 计算分表表名
- *
- * @param string $prefix 表名前缀
- * @param int $id 索引表id
- * @param int $pNumber 分割数量
- *
- * @return string
- */
- public function getTableName($prefix, $id, $pNumber = 500000)
- {
- $prefix = trim($prefix, '_') . '_';
- $tableName = strtolower($prefix . ceil($id / $pNumber));
- return $tableName;
- }
- /**
- * 判断表是否存在
- * @param $tableName
- * @return bool
- */
- public function existsTable($tableName)
- {
- $databaseName = Factory::config()->get('db')[$this->serviceDB]['dbname'];
- if(AllTableNameCache::TableIsExists($databaseName, $tableName)) {
- return true;
- }else{
- return false;
- }
- }
- /**
- * 切换Dao层操作的表
- * 主要用作切换分表使用
- *
- * @param $tableName
- *
- * @throws \Exception
- */
- public function setTable($tableName)
- {
- $this->_table = $tableName;
- $databaseName = Factory::config()->get('db')[$this->serviceDB]['dbname'];
- // 当前使用库所有表名缓存如果不存在自动更新
- if (!AllTableNameCache::allTableNameCacheIsExists($databaseName)) {
- $tables = $this->db->select("SELECT TABLE_NAME FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = '{$databaseName}';") ?: [];
- if (!empty($tables)) {
- AllTableNameCache::allTableNameCache($databaseName, $tables);
- }
- }
- // 判断切换的表是否存在,不存在自动创建
- if (!AllTableNameCache::TableIsExists($databaseName, $tableName)) {
- $result = explode('_', $tableName);
- if ($result && !empty($result)) {
- $tablePrefix = '';
- for ($i = 0, $c = count($result); $i < ($c - 1); $i++) {
- if(!is_numeric($result[$i])){
- $tablePrefix .= $result[$i] . '_';
- }
- }
- $dbresult = $this->db->query("CREATE TABLE IF NOT EXISTS {$tableName} LIKE {$tablePrefix}1");
- if ($dbresult === false) {
- throw new \Exception($tableName . '分表创建错误. ErrorInfo: ' . var_export($this->db->error(), true));
- }
- } else {
- throw new \Exception($tableName . '不是一个正确得表名');
- }
- AllTableNameCache::addNewTableName($databaseName, $tableName);
- }
- }
- /**
- * 添加数据
- *
- * @param array $params
- *
- * @return bool|int
- */
- public function insert($params = array(), $multiple = false)
- {
- return parent::insert($params, $multiple);
- }
- /**
- * replace方式添加数据
- *
- * @param array $params
- *
- * @return bool|int
- */
- public function replace($params = array(), $multiple = false)
- {
- return parent::replace($params, $multiple);
- }
- /**
- * 更新数据
- *
- * @param array $data
- * @param null $where
- * @param null $limit
- * @param null $order
- *
- * @return int
- */
- public function update($data = array(), $where = null, $limit = null, $order = null)
- {
- return parent::update($data, $where, $limit, $order);
- }
- /**
- * 删除数据
- *
- * @param null $where
- * @param null $limit
- * @param null $order
- * @param array $data
- *
- * @return int
- */
- public function delete($where = null, $limit = null, $order = null, $data = array())
- {
- return parent::delete($where, $limit, $order, $data);
- }
- /**
- * 数据库自增自减操作
- * @param $fieldName 字段名称
- * @param $id 修改数据id
- * @param $action 操作 "+"/"-"
- * @param $step 修改数据步长
- * @return mixed 影响行数
- *
- */
- public function autodecrement($fieldName, $id, $action, $step)
- {
- $tableName = parent::get_Table();
- $sql = 'call autodecrement("'.$tableName.'", "'.$fieldName.'", '.$id.', "'.$action.'", '.$step.', @result);';
- $dbResult = parent::query($sql);
- if ($dbResult === false) {
- return false;
- }
- $sql = 'select @result as result;';
- $dbResult = parent::query($sql);
- if ($dbResult === false) {
- return false;
- }
- return $dbResult[0]['result'];
- }
- /**
- * 执行sql
- */
- public function query($sql)
- {
- return parent::query($sql);
- }
- /**
- * 执行sql
- */
- public function exportQuery($sql)
- {
- return parent::exportQuery($sql);
- }
- /**
- * 获取Sql操作错误
- *
- * @return string
- */
- public function error()
- {
- return parent::error();
- }
- /**
- * 过虑字段
- *
- * @param array $params 要筛选的数据
- *
- * @return array
- */
- public function getTablesFields($params)
- {
- $fields = [];
- foreach ($this->_fields as $field) {
- if (array_key_exists($field, $params)) {
- $fields[$field] = $params[$field];
- }
- }
- return $fields;
- }
- /**
- * 开启事务
- * @param bool $foreign_key_checks
- * @return mixed
- */
- public function beginTransaction($foreign_key_checks = false)
- {
- ++$this->transactions;
- if ($this->transactions == 1){
- return parent::beginTransaction($foreign_key_checks = false);
- }
- }
- /**
- * 提交事务
- * @return mixed
- */
- public function commit()
- {
- if($this->transactions == 1){
- return parent::commit();
- }
- --$this->transactions;
- }
- /**
- * 回滚事务
- * @return mixed
- */
- public function rollBack()
- {
- if($this->transactions == 1){
- $this->transactions = 0;
- return parent::rollBack();
- }else{
- --$this->transactions;
- }
- }
- /**
- * 根据DSL查询文档
- *
- * @param $query
- *
- * @return array
- */
- public function getSearchQueryDsl($query)
- {
- return $this->search->search($query);
- }
- /**
- * 根据DSL查询文档 滚动查询
- * @param $query
- * @param string $scroll
- * @param int $size
- * @return mixed
- */
- public function getScrollSearchQueryDsl($query, $scroll = '10s', $size = 2000)
- {
- $result = [];
- $scrollResult = $this->search->scrollSearch($query, $scroll, $size);
- while (isset($scrollResult['hits']['hits']) && count($scrollResult['hits']['hits']) > 0) {
- $result = array_merge($result, $scrollResult['hits']['hits']);
- $scroll_id = $scrollResult['_scroll_id'];
- $scrollResult = $this->search->request('/_search/scroll','POST',[
- "scroll_id" => $scroll_id,
- "scroll" => $scroll
- ],
- false,
- false
- );
- }
- if(!empty($result)) {
- $scrollResult['hits']['hits'] = $result;
- }
- return $scrollResult;
- }
- /**
- * 根据索引的主ID查询文档
- *
- * @param int $id 索引的DocumentId
- *
- * @return array
- */
- public function getSearchIndexDocument($id)
- {
- //$id = intval($id);
- return $this->search->get($id);
- }
- /**
- * 添加或更新ES索引
- *
- * @param array $data 创建索引数据
- * @param int $id 创建索引的DocumentId
- *
- * @return array
- */
- public function addUpSearchIndexDocument($data, $id)
- {
- return $this->search->index($data, $id);
- }
- /**
- * 局部更新索引内容
- * @param array $data 要更新的数据
- * @param int $id 要更新的文档下的数据id
- * @return array
- */
- public function esupdateTypeFieldVaule($data, $id)
- {
- return $this->search->updateFieldVaule($data, $id);
- }
- /**
- * 批量更新索引内容
- * @param $query
- * @param $data
- * @return array
- */
- public function esBatchUpdateTypeFieldVaule($data, $query)
- {
- return $this->search->batchUpdateFieldVaule($data, $query);
- }
- /**
- * 删除索引下面的指定文档
- */
- public function esdeleteTypeDocument($id)
- {
- return $this->search->delete($id);
- }
- }
|