DB.php 17 KB

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