DB.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. use CDbException;
  3. use DbCriteria;
  4. use Yii;
  5. class DB
  6. {
  7. /**
  8. * 批量插入
  9. *
  10. * @param $table
  11. * @param array $data
  12. * @param array $header 可以不传 但 $data 必须是关联数组集合
  13. * @return int
  14. * @throws CDbException
  15. */
  16. public static function safeBatchInsert($table, array $data, array $header = [])
  17. {
  18. $params = [];
  19. if (!$data) {
  20. return false;
  21. }
  22. if (!$header) {
  23. $header = array_keys($data[0]);
  24. }
  25. $headerStr = implode(',', $header);
  26. $sql = "INSERT INTO {$table} ({$headerStr}) values ";
  27. foreach ($data as $k => $datum) {
  28. $tempValues = [];
  29. $i = 0;
  30. foreach ($datum as $ik => $item) {
  31. $paramKey = ':'.$header[$i].$k;
  32. $paramValue = is_numeric($item) ? $item : "{$item}";
  33. $params[$paramKey] = $paramValue;
  34. $tempValues[] = $paramKey;
  35. $i++;
  36. }
  37. $valueStr = implode(',', $tempValues);
  38. $sql .= "({$valueStr}),";
  39. }
  40. return Yii::app()->db->createCommand(rtrim($sql, ','))->execute($params);
  41. }
  42. /**
  43. * 获取sql操作command对象
  44. *
  45. * @param string $dbName
  46. * @return \CDbCommand
  47. */
  48. public static function getDbCommand($dbName = 'db')
  49. {
  50. $command = \Yii::app()->$dbName->createCommand();
  51. $command->reset();
  52. return $command;
  53. }
  54. /**
  55. * 从数据库获取数据的通用方法
  56. *
  57. * @param string $tableName 表名
  58. * @param array $filters 筛选条件
  59. * @param string $fields 字段
  60. * @param string $group
  61. * @param string $order
  62. * @param integer $page 分页,如果为0,就不分页
  63. * @param integer $pageSize
  64. * @param string $index 强制指定的索引
  65. * @param string $dbName 数据库名
  66. * @return array
  67. */
  68. public static function getDataByCondition(
  69. $tableName,
  70. $filters,
  71. $fields = '*',
  72. $group = '',
  73. $order = '',
  74. $page = 0,
  75. $pageSize = 0,
  76. $index = '',
  77. $dbName = 'db'
  78. ): array {
  79. $db = self::getDbCommand($dbName);
  80. if (!empty($index)) {
  81. $tableName .= " FORCE INDEX (`{$index}`)";
  82. }
  83. $criteria = new \CDbCriteria();
  84. if (!empty($filters)) {
  85. foreach ($filters as $key => $value) {
  86. if (is_array($value) && 'like' === reset($value)) {
  87. $criteria->compare($key, array_pop($value), true);
  88. } elseif (is_array($value) && 'condition' === reset($value)) {
  89. $criteria->addCondition(array_pop($value));
  90. } elseif (is_array($value) && 'betweenCondition' === reset($value)) {
  91. $criteria->addBetweenCondition($key, $value[1], $value[2]);
  92. } elseif (is_array($value) && $value === array()) {
  93. $criteria->addInCondition($key, $value);
  94. } elseif (empty($value)) {
  95. $criteria->addCondition("{$key} = :{$key}");
  96. $criteria->params[':'.$key] = $value;
  97. } else {
  98. $criteria->compare($key, $value);
  99. }
  100. }
  101. }
  102. $build = $db->select($fields)
  103. ->from($tableName)
  104. ->where($criteria->condition, $criteria->params);
  105. !empty($group) && $build->group($group);
  106. !empty($order) && $build->order($order);
  107. if ($page > 0) {
  108. $page = intval($page);
  109. $pageSize = intval($pageSize);
  110. $offset = $pageSize * ($page - 1);
  111. $build->limit($pageSize, $offset);
  112. }
  113. /* if ($isLock) {
  114. $sql = $build->getText() . " FOR UPDATE";
  115. return $this->queryAllBySql($sql, $build->params);
  116. } */
  117. return $build->queryAll();
  118. }
  119. /**
  120. * 从数据库获取数据的通用方法
  121. *
  122. * @param string $tableName 表名
  123. * @param array $filters 筛选条件
  124. * @param string $fields 字段
  125. * @param string $group
  126. * @param string $order
  127. * @param integer $page 分页,如果为0,就不分页
  128. * @param integer $pageSize
  129. * @param string $index 强制指定的索引
  130. * @param string $dbName 数据库名
  131. * @return array
  132. */
  133. public static function updateData($tableName, $filters, $dbName = 'db')
  134. {
  135. $db = self::getDbCommand($dbName);
  136. if (!isset($filters['id'])) {
  137. return false;
  138. }
  139. $id = $filters['id'];
  140. unset($filters['id']);
  141. return $db->update($tableName, $filters, "id=:id", array(':id' => $id));
  142. }
  143. public static function updateById($tableName, $info, $id)
  144. {
  145. $id = intval($id);
  146. if ($id <= 0) {
  147. return false;
  148. }
  149. $db = self::getDbCommand();
  150. return $db->update(self::formTableName($tableName), $info, "id=:id", [':id' => $id]);
  151. }
  152. public static function getInfoById($tableName, $id, $fields = '*')
  153. {
  154. $id = intval($id);
  155. if ($id <= 0) {
  156. return [];
  157. }
  158. return self::getInfoWithCriteria($tableName, DbCriteria::simpleCompare(['id' => $id])->setSelect($fields));
  159. }
  160. public static function deleteById($tableName, $id)
  161. {
  162. $id = intval($id);
  163. if ($id <= 0) {
  164. return false;
  165. }
  166. $db = self::getDbCommand();
  167. return $db->delete(self::formTableName($tableName), "id=:id", [':id' => $id]);
  168. }
  169. public static function formTableName($tableName)
  170. {
  171. return 'wx_' . trim($tableName, 'wx_');
  172. }
  173. /**
  174. * 从数据库获取数据的通用方法,与上面的方法相比,就是多了分页的内容
  175. *
  176. * @param string $tableName 表名
  177. * @param array $filters 筛选条件
  178. * @param string $fields 字段
  179. * @param string $group
  180. * @param string $order
  181. * @param integer $page 分页,如果为0,就不分页
  182. * @param integer $pageSize
  183. * @param string $index 强制指定的索引
  184. * @param string $dbName 数据库名
  185. * @return array
  186. */
  187. public static function getWebDataByCondition(
  188. $tableName,
  189. $filters,
  190. $fields = '*',
  191. $group = '',
  192. $order = '',
  193. $page = 0,
  194. $pageSize = 0,
  195. $index = '',
  196. $dbName = 'db'
  197. ) {
  198. $return = [
  199. 'page' => $page,
  200. 'pageSize' => $pageSize,
  201. 'totalPage' => -1,
  202. 'counts' => -1,
  203. 'records' => [],
  204. ];
  205. $db = self::getDbCommand($dbName);
  206. if (!empty($index)) {
  207. $tableName .= " FORCE INDEX (`{$index}`)";
  208. }
  209. $criteria = new \CDbCriteria();
  210. if (!empty($filters)) {
  211. foreach ($filters as $key => $value) {
  212. if (is_array($value) && 'like' === reset($value)) {
  213. $criteria->compare($key, array_pop($value), true);
  214. } elseif (is_array($value) && 'condition' === reset($value)) {
  215. $criteria->addCondition(array_pop($value));
  216. } elseif (is_array($value) && 'betweenCondition' === reset($value)) {
  217. $criteria->addBetweenCondition($key, $value[1], $value[2]);
  218. } elseif (is_array($value) && $value === array()) {
  219. $criteria->addInCondition($key, $value);
  220. } elseif (empty($value)) {
  221. $criteria->addCondition("{$key} = :{$key}");
  222. $criteria->params[':'.$key] = $value;
  223. } else {
  224. $criteria->compare($key, $value);
  225. }
  226. }
  227. }
  228. // 如果有分页,则查询总记录数
  229. $counts = -1;
  230. if ($page > 0) {
  231. if (empty($group)) {
  232. $counts = $db->select("count(*) as total")
  233. ->from($tableName)
  234. ->where($criteria->condition, $criteria->params)
  235. ->queryScalar();
  236. $db->reset();
  237. } else {
  238. $count_data = $db->select("{$group}")
  239. ->from($tableName)
  240. ->where($criteria->condition, $criteria->params)
  241. ->group($group)
  242. ->queryAll();
  243. $db->reset();
  244. $counts = count($count_data);
  245. }
  246. $counts = intval($counts);
  247. if ($counts < 1) {
  248. return $return; // 数据为空
  249. }
  250. }
  251. $build = $db->select($fields)
  252. ->from($tableName)
  253. ->where($criteria->condition, $criteria->params);
  254. !empty($group) && $build->group($group);
  255. !empty($order) && $build->order($order);
  256. // 如果有分页,则对查询条件做处理
  257. $totalPages = -1;
  258. if ($pageSize > 0) {
  259. //计算总页数
  260. $totalPages = ceil($counts / $pageSize);
  261. $offset = $pageSize * ($page - 1);
  262. $build->limit($pageSize, $offset);
  263. $return['totalPage'] = $totalPages;
  264. $return['counts'] = $counts;
  265. }
  266. $return['records'] = $build->queryAll();
  267. return $return;
  268. }
  269. /**
  270. * 从数据库获取数据的通用方法,只获取一条数据
  271. *
  272. * @param string $tableName 表名
  273. * @param array $filters 筛选条件
  274. * @param string $fields 字段
  275. * @param string $group
  276. * @param string $order
  277. * @param integer $page 分页,如果为0,就不分页
  278. * @param integer $pageSize
  279. * @param string $index 强制指定的索引
  280. * @param string $dbName 数据库名
  281. * @return array
  282. */
  283. public static function getOneByCondition(
  284. $tableName,
  285. $filters,
  286. $fields = '*',
  287. $group = '',
  288. $index = '',
  289. $dbName = 'db'
  290. ) {
  291. $data = self::getDataByCondition($tableName, $filters, $fields, $group, '', 0, 0, $index, $dbName);
  292. return !empty($data) ? reset($data) : [];
  293. }
  294. /**
  295. * 向数据库插入数据
  296. *
  297. * @param string $tableName 表名
  298. * @param array $info
  299. * @param string $dbName
  300. * @return int
  301. */
  302. public static function addData($tableName, $info, $dbName = 'db')
  303. {
  304. if (empty($info)) {
  305. return false;
  306. }
  307. $command = self::getDbCommand($dbName);
  308. $ret = $command->insert(self::formTableName($tableName), $info);
  309. if ($ret) {
  310. return $command->getConnection()->getLastInsertID();
  311. }
  312. return false;
  313. }
  314. /**
  315. * 根据条件删除数据库内容
  316. *
  317. * @param string $tableName
  318. * @param array $filters
  319. * @param string $dbName
  320. * @return int
  321. */
  322. public static function deleteByCondition($tableName, $filters, $dbName = 'db')
  323. {
  324. if (empty($filters)) {
  325. return false;
  326. }
  327. $command = self::getDbCommand($dbName);
  328. $condition = '';
  329. $params = [];
  330. foreach ($filters as $key => $value) {
  331. $condition .= " {$key}=:{$key} AND";
  332. $params[':'.$key] = $value;
  333. }
  334. $condition = trim($condition, 'AND');
  335. $condition = trim($condition);
  336. return $command->delete($tableName, $condition, $params);
  337. }
  338. /**
  339. * 获取model的错误信息
  340. *
  341. * @param object $model
  342. * @return string
  343. */
  344. public static function getModelErrorMsg($model)
  345. {
  346. $str = '';
  347. if ($model->hasErrors()) {
  348. $errors = [];
  349. foreach ($model->getErrors() as $error) {
  350. $errors += $error;
  351. }
  352. $str = implode("<br/>", $errors);
  353. }
  354. return $str;
  355. }
  356. /**
  357. * 通过 DbCriteria 来搜索
  358. * @param string $table_name 表名
  359. * @param DbCriteria $criteria 分页通过 setPage 设置,否则不会查询分页信息
  360. * @return array|\CDbDataReader 格式: ['page'=>1, 'pageSize'=>1, 'totalPage'=>1, 'counts'=>1, 'records'=>[]]
  361. * @throws \CException
  362. */
  363. public static function getListWithCriteria(string $table_name, DbCriteria $criteria)
  364. {
  365. // 数据返回格式
  366. $retData = [
  367. 'current' => $criteria->getPage(),
  368. 'size' => $criteria->getPageSize(),
  369. 'totalPage' => 0,
  370. 'total' => 0,
  371. 'records' => [],
  372. ];
  373. $table_name = self::formTableName($table_name);
  374. if ($criteria->alias) {
  375. $table_name .= ' as '.$criteria->alias;
  376. }
  377. // 指定索引
  378. if (!empty($criteria->forceIndex)) {
  379. $table_name .= " FORCE INDEX (`{$criteria->forceIndex}`)";
  380. }
  381. // 根据 DbCriteria 构建查询
  382. $build = self::getDbCommand()->select($criteria->select)
  383. ->from($table_name)
  384. ->where($criteria->condition, $criteria->params);
  385. !empty($criteria->group) && $build->group($criteria->group);
  386. !empty($criteria->having) && $build->having($criteria->having);
  387. !empty($criteria->join) && $build->setJoin($criteria->join);
  388. // 有分页需要先查询总数 然后再恢复 builder
  389. if ($criteria->isFenye()) {
  390. if (!empty($criteria->group)) {
  391. // 使用group以后 需要子查询来统计总条数
  392. $subQuery = $build->select($criteria->getCountSelectStr())->getText();
  393. $totalNum = self::getDbCommand()->select('count(*)')->from("($subQuery) t")->queryScalar(
  394. $criteria->params
  395. );
  396. } else {
  397. $totalNum = $build->select($criteria->getCountSelectStr())->queryScalar();
  398. }
  399. $retData['total'] = $totalNum;
  400. $retData['totalPage'] = ceil($totalNum / $criteria->getPageSize());
  401. $build = self::getDbCommand()->select($criteria->select)
  402. ->from($table_name)
  403. ->where($criteria->condition, $criteria->params);
  404. !empty($criteria->group) && $build->group($criteria->group);
  405. !empty($criteria->having) && $build->having($criteria->having);
  406. !empty($criteria->join) && $build->setJoin($criteria->join);
  407. $build->limit($criteria->limit, $criteria->offset);
  408. } elseif ($criteria->limit > 0) {
  409. // 不做分页 单纯控制偏移及数量
  410. $build->limit($criteria->limit, $criteria->offset);
  411. }
  412. !empty($criteria->order) && $build->order($criteria->order);
  413. $retData['records'] = $build->queryAll() ?: [];
  414. // debug
  415. if ($criteria->getDebugMode()) {
  416. Logger::info(
  417. json_encode(
  418. [
  419. 'DbCriteriaDebug' => $criteria->getSql($build->getText(), $criteria->params),
  420. 'tag' => $criteria->getDebugTag(),
  421. ],
  422. JSON_UNESCAPED_UNICODE
  423. )
  424. );
  425. }
  426. return $retData;
  427. }
  428. /**
  429. * 通过 DbCriteria 来搜索
  430. * @param string $table_name 表名
  431. * @param DbCriteria $criteria
  432. * @return array|\CDbDataReader
  433. * @throws \CException
  434. */
  435. public static function getInfoWithCriteria(string $table_name, DbCriteria $criteria)
  436. {
  437. $table_name = self::formTableName($table_name);
  438. // 指定索引
  439. if (!empty($criteria->forceIndex)) {
  440. $table_name .= " FORCE INDEX (`{$criteria->forceIndex}`)";
  441. }
  442. // 根据 DbCriteria 构建查询
  443. $build = self::getDbCommand()->select($criteria->select)
  444. ->from($table_name)
  445. ->limit(1)
  446. ->where($criteria->condition, $criteria->params);
  447. !empty($criteria->group) && $build->group($criteria->group);
  448. !empty($criteria->having) && $build->having($criteria->having);
  449. !empty($criteria->order) && $build->order($criteria->order);
  450. // debug
  451. if ($criteria->getDebugMode()) {
  452. Logger::info(
  453. json_encode(
  454. [
  455. 'DbCriteriaDebug' => $criteria->getSql($build->getText(), $criteria->params),
  456. 'tag' => $criteria->getDebugTag(),
  457. ],
  458. JSON_UNESCAPED_UNICODE
  459. )
  460. );
  461. }
  462. return $build->queryRow();
  463. }
  464. public static function getScalerWithCriteria(string $table_name, DbCriteria $criteria)
  465. {
  466. $data = self::getInfoWithCriteria($table_name, $criteria);
  467. return $data ? reset($data) : '';
  468. }
  469. }