Builder.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\db;
  12. use PDO;
  13. use think\Exception;
  14. abstract class Builder
  15. {
  16. // connection对象实例
  17. protected $connection;
  18. // 查询表达式映射
  19. protected $exp = ['EQ' => '=', 'NEQ' => '<>', 'GT' => '>', 'EGT' => '>=', 'LT' => '<', 'ELT' => '<=', 'NOTLIKE' => 'NOT LIKE', 'NOTIN' => 'NOT IN', 'NOTBETWEEN' => 'NOT BETWEEN', 'NOTEXISTS' => 'NOT EXISTS', 'NOTNULL' => 'NOT NULL', 'NOTBETWEEN TIME' => 'NOT BETWEEN TIME'];
  20. // 查询表达式解析
  21. protected $parser = [
  22. 'parseCompare' => ['=', '<>', '>', '>=', '<', '<='],
  23. 'parseLike' => ['LIKE', 'NOT LIKE'],
  24. 'parseBetween' => ['NOT BETWEEN', 'BETWEEN'],
  25. 'parseIn' => ['NOT IN', 'IN'],
  26. 'parseExp' => ['EXP'],
  27. 'parseNull' => ['NOT NULL', 'NULL'],
  28. 'parseBetweenTime' => ['BETWEEN TIME', 'NOT BETWEEN TIME'],
  29. 'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'],
  30. 'parseExists' => ['NOT EXISTS', 'EXISTS'],
  31. 'parseColumn' => ['COLUMN'],
  32. ];
  33. // SQL表达式
  34. protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%UNION%%ORDER%%LIMIT% %LOCK%%COMMENT%';
  35. protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
  36. protected $insertAllSql = '%INSERT% INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
  37. protected $updateSql = 'UPDATE %TABLE% SET %SET%%JOIN%%WHERE%%ORDER%%LIMIT% %LOCK%%COMMENT%';
  38. protected $deleteSql = 'DELETE FROM %TABLE%%USING%%JOIN%%WHERE%%ORDER%%LIMIT% %LOCK%%COMMENT%';
  39. /**
  40. * 架构函数
  41. * @access public
  42. * @param Connection $connection 数据库连接对象实例
  43. */
  44. public function __construct(Connection $connection)
  45. {
  46. $this->connection = $connection;
  47. }
  48. /**
  49. * 获取当前的连接对象实例
  50. * @access public
  51. * @return Connection
  52. */
  53. public function getConnection()
  54. {
  55. return $this->connection;
  56. }
  57. /**
  58. * 注册查询表达式解析
  59. * @access public
  60. * @param string $name 解析方法
  61. * @param array $parser 匹配表达式数据
  62. * @return $this
  63. */
  64. public function bindParser($name, $parser)
  65. {
  66. $this->parser[$name] = $parser;
  67. return $this;
  68. }
  69. /**
  70. * 数据分析
  71. * @access protected
  72. * @param Query $query 查询对象
  73. * @param array $data 数据
  74. * @param array $fields 字段信息
  75. * @param array $bind 参数绑定
  76. * @return array
  77. */
  78. protected function parseData(Query $query, $data = [], $fields = [], $bind = [])
  79. {
  80. if (empty($data)) {
  81. return [];
  82. }
  83. $options = $query->getOptions();
  84. // 获取绑定信息
  85. if (empty($bind)) {
  86. $bind = $this->connection->getFieldsBind($options['table']);
  87. }
  88. if (empty($fields)) {
  89. if ('*' == $options['field']) {
  90. $fields = array_keys($bind);
  91. } else {
  92. $fields = $options['field'];
  93. }
  94. }
  95. $result = [];
  96. foreach ($data as $key => $val) {
  97. if ('*' != $options['field'] && !in_array($key, $fields, true)) {
  98. continue;
  99. }
  100. $item = $this->parseKey($query, $key, true);
  101. if ($val instanceof Expression) {
  102. $result[$item] = $val->getValue();
  103. continue;
  104. } elseif (!is_scalar($val) && (in_array($key, (array) $query->getOptions('json')) || 'json' == $this->connection->getFieldsType($options['table'], $key))) {
  105. $val = json_encode($val, JSON_UNESCAPED_UNICODE);
  106. } elseif (is_object($val) && method_exists($val, '__toString')) {
  107. // 对象数据写入
  108. $val = $val->__toString();
  109. }
  110. if (false !== strpos($key, '->')) {
  111. list($key, $name) = explode('->', $key);
  112. $item = $this->parseKey($query, $key);
  113. $result[$item] = 'json_set(' . $item . ', \'$.' . $name . '\', ' . $this->parseDataBind($query, $key, $val, $bind) . ')';
  114. } elseif ('*' == $options['field'] && false === strpos($key, '.') && !in_array($key, $fields, true)) {
  115. if ($options['strict']) {
  116. throw new Exception('fields not exists:[' . $key . ']');
  117. }
  118. } elseif (is_null($val)) {
  119. $result[$item] = 'NULL';
  120. } elseif (is_array($val) && !empty($val)) {
  121. switch (strtoupper($val[0])) {
  122. case 'INC':
  123. $result[$item] = $item . ' + ' . floatval($val[1]);
  124. break;
  125. case 'DEC':
  126. $result[$item] = $item . ' - ' . floatval($val[1]);
  127. break;
  128. case 'EXP':
  129. throw new Exception('not support data:[' . $val[0] . ']');
  130. }
  131. } elseif (is_scalar($val)) {
  132. // 过滤非标量数据
  133. $result[$item] = $this->parseDataBind($query, $key, $val, $bind);
  134. }
  135. }
  136. return $result;
  137. }
  138. /**
  139. * 数据绑定处理
  140. * @access protected
  141. * @param Query $query 查询对象
  142. * @param string $key 字段名
  143. * @param mixed $data 数据
  144. * @param array $bind 绑定数据
  145. * @return string
  146. */
  147. protected function parseDataBind(Query $query, $key, $data, $bind = [])
  148. {
  149. if ($data instanceof Expression) {
  150. return $data->getValue();
  151. }
  152. $name = $query->bind($data, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
  153. return ':' . $name;
  154. }
  155. /**
  156. * 字段名分析
  157. * @access public
  158. * @param Query $query 查询对象
  159. * @param mixed $key 字段名
  160. * @param bool $strict 严格检测
  161. * @return string
  162. */
  163. public function parseKey(Query $query, $key, $strict = false)
  164. {
  165. return $key instanceof Expression ? $key->getValue() : $key;
  166. }
  167. /**
  168. * field分析
  169. * @access protected
  170. * @param Query $query 查询对象
  171. * @param mixed $fields 字段名
  172. * @return string
  173. */
  174. protected function parseField(Query $query, $fields)
  175. {
  176. if ('*' == $fields || empty($fields)) {
  177. $fieldsStr = '*';
  178. } elseif (is_array($fields)) {
  179. // 支持 'field1'=>'field2' 这样的字段别名定义
  180. $array = [];
  181. foreach ($fields as $key => $field) {
  182. if (!is_numeric($key)) {
  183. $array[] = $this->parseKey($query, $key) . ' AS ' . $this->parseKey($query, $field, true);
  184. } else {
  185. $array[] = $this->parseKey($query, $field);
  186. }
  187. }
  188. $fieldsStr = implode(',', $array);
  189. }
  190. return $fieldsStr;
  191. }
  192. /**
  193. * table分析
  194. * @access protected
  195. * @param Query $query 查询对象
  196. * @param mixed $tables 表名
  197. * @return string
  198. */
  199. protected function parseTable(Query $query, $tables)
  200. {
  201. $item = [];
  202. $options = $query->getOptions();
  203. foreach ((array) $tables as $key => $table) {
  204. if (!is_numeric($key)) {
  205. $key = $this->connection->parseSqlTable($key);
  206. $item[] = $this->parseKey($query, $key) . ' ' . $this->parseKey($query, $table);
  207. } else {
  208. $table = $this->connection->parseSqlTable($table);
  209. if (isset($options['alias'][$table])) {
  210. $item[] = $this->parseKey($query, $table) . ' ' . $this->parseKey($query, $options['alias'][$table]);
  211. } else {
  212. $item[] = $this->parseKey($query, $table);
  213. }
  214. }
  215. }
  216. return implode(',', $item);
  217. }
  218. /**
  219. * where分析
  220. * @access protected
  221. * @param Query $query 查询对象
  222. * @param mixed $where 查询条件
  223. * @return string
  224. */
  225. protected function parseWhere(Query $query, $where)
  226. {
  227. $options = $query->getOptions();
  228. $whereStr = $this->buildWhere($query, $where);
  229. if (!empty($options['soft_delete'])) {
  230. // 附加软删除条件
  231. list($field, $condition) = $options['soft_delete'];
  232. $binds = $this->connection->getFieldsBind($options['table']);
  233. $whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : '';
  234. $whereStr = $whereStr . $this->parseWhereItem($query, $field, $condition, '', $binds);
  235. }
  236. return empty($whereStr) ? '' : ' WHERE ' . $whereStr;
  237. }
  238. /**
  239. * 生成查询条件SQL
  240. * @access public
  241. * @param Query $query 查询对象
  242. * @param mixed $where 查询条件
  243. * @return string
  244. */
  245. public function buildWhere(Query $query, $where)
  246. {
  247. if (empty($where)) {
  248. $where = [];
  249. }
  250. $whereStr = '';
  251. $binds = $this->connection->getFieldsBind($query->getOptions('table'));
  252. foreach ($where as $logic => $val) {
  253. $str = [];
  254. foreach ($val as $value) {
  255. if ($value instanceof Expression) {
  256. $str[] = ' ' . $logic . ' ( ' . $value->getValue() . ' )';
  257. continue;
  258. }
  259. if (is_array($value)) {
  260. if (key($value) !== 0) {
  261. throw new Exception('where express error:' . var_export($value, true));
  262. }
  263. $field = array_shift($value);
  264. } elseif (!($value instanceof \Closure)) {
  265. throw new Exception('where express error:' . var_export($value, true));
  266. }
  267. if ($value instanceof \Closure) {
  268. // 使用闭包查询
  269. $newQuery = $query->newQuery()->setConnection($this->connection);
  270. $value($newQuery);
  271. $whereClause = $this->buildWhere($newQuery, $newQuery->getOptions('where'));
  272. if (!empty($whereClause)) {
  273. $query->bind($newQuery->getBind(false));
  274. $str[] = ' ' . $logic . ' ( ' . $whereClause . ' )';
  275. }
  276. } elseif (is_array($field)) {
  277. array_unshift($value, $field);
  278. $str2 = [];
  279. foreach ($value as $item) {
  280. $str2[] = $this->parseWhereItem($query, array_shift($item), $item, $logic, $binds);
  281. }
  282. $str[] = ' ' . $logic . ' ( ' . implode(' AND ', $str2) . ' )';
  283. } elseif (strpos($field, '|')) {
  284. // 不同字段使用相同查询条件(OR)
  285. $array = explode('|', $field);
  286. $item = [];
  287. foreach ($array as $k) {
  288. $item[] = $this->parseWhereItem($query, $k, $value, '', $binds);
  289. }
  290. $str[] = ' ' . $logic . ' ( ' . implode(' OR ', $item) . ' )';
  291. } elseif (strpos($field, '&')) {
  292. // 不同字段使用相同查询条件(AND)
  293. $array = explode('&', $field);
  294. $item = [];
  295. foreach ($array as $k) {
  296. $item[] = $this->parseWhereItem($query, $k, $value, '', $binds);
  297. }
  298. $str[] = ' ' . $logic . ' ( ' . implode(' AND ', $item) . ' )';
  299. } else {
  300. // 对字段使用表达式查询
  301. $field = is_string($field) ? $field : '';
  302. $str[] = ' ' . $logic . ' ' . $this->parseWhereItem($query, $field, $value, $logic, $binds);
  303. }
  304. }
  305. $whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($logic) + 1) : implode(' ', $str);
  306. }
  307. return $whereStr;
  308. }
  309. // where子单元分析
  310. protected function parseWhereItem(Query $query, $field, $val, $rule = '', $binds = [])
  311. {
  312. // 字段分析
  313. $key = $field ? $this->parseKey($query, $field, true) : '';
  314. // 查询规则和条件
  315. if (!is_array($val)) {
  316. $val = is_null($val) ? ['NULL', ''] : ['=', $val];
  317. }
  318. list($exp, $value) = $val;
  319. // 对一个字段使用多个查询条件
  320. if (is_array($exp)) {
  321. $item = array_pop($val);
  322. // 传入 or 或者 and
  323. if (is_string($item) && in_array($item, ['AND', 'and', 'OR', 'or'])) {
  324. $rule = $item;
  325. } else {
  326. array_push($val, $item);
  327. }
  328. foreach ($val as $k => $item) {
  329. $str[] = $this->parseWhereItem($query, $field, $item, $rule, $binds);
  330. }
  331. return '( ' . implode(' ' . $rule . ' ', $str) . ' )';
  332. }
  333. // 检测操作符
  334. $exp = strtoupper($exp);
  335. if (isset($this->exp[$exp])) {
  336. $exp = $this->exp[$exp];
  337. }
  338. if ($value instanceof Expression) {
  339. } elseif (is_object($value) && method_exists($value, '__toString')) {
  340. // 对象数据写入
  341. $value = $value->__toString();
  342. }
  343. if (strpos($field, '->')) {
  344. $jsonType = $query->getJsonFieldType($field);
  345. $bindType = $this->connection->getFieldBindType($jsonType);
  346. } else {
  347. $bindType = isset($binds[$field]) && 'LIKE' != $exp ? $binds[$field] : PDO::PARAM_STR;
  348. }
  349. if (is_scalar($value) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) {
  350. if (0 === strpos($value, ':') && $query->isBind(substr($value, 1))) {
  351. } else {
  352. $name = $query->bind($value, $bindType);
  353. $value = ':' . $name;
  354. }
  355. }
  356. // 解析查询表达式
  357. foreach ($this->parser as $fun => $parse) {
  358. if (in_array($exp, $parse)) {
  359. $whereStr = $this->$fun($query, $key, $exp, $value, $field, $bindType, isset($val[2]) ? $val[2] : 'AND');
  360. break;
  361. }
  362. }
  363. if (!isset($whereStr)) {
  364. throw new Exception('where express error:' . $exp);
  365. }
  366. return $whereStr;
  367. }
  368. /**
  369. * 模糊查询
  370. * @access protected
  371. * @param Query $query 查询对象
  372. * @param string $key
  373. * @param string $exp
  374. * @param mixed $value
  375. * @param string $field
  376. * @param integer $bindType
  377. * @param string $logic
  378. * @return string
  379. */
  380. protected function parseLike(Query $query, $key, $exp, $value, $field, $bindType, $logic)
  381. {
  382. // 模糊匹配
  383. if (is_array($value)) {
  384. foreach ($value as $item) {
  385. $name = $query->bind($item, PDO::PARAM_STR);
  386. $array[] = $key . ' ' . $exp . ' :' . $name;
  387. }
  388. $whereStr = '(' . implode(' ' . strtoupper($logic) . ' ', $array) . ')';
  389. } else {
  390. $whereStr = $key . ' ' . $exp . ' ' . $value;
  391. }
  392. return $whereStr;
  393. }
  394. /**
  395. * 表达式查询
  396. * @access protected
  397. * @param Query $query 查询对象
  398. * @param string $key
  399. * @param string $exp
  400. * @param array $value
  401. * @param string $field
  402. * @param integer $bindType
  403. * @return string
  404. */
  405. protected function parseColumn(Query $query, $key, $exp, array $value, $field, $bindType)
  406. {
  407. // 字段比较查询
  408. list($op, $field2) = $value;
  409. if (!in_array($op, ['=', '<>', '>', '>=', '<', '<='])) {
  410. throw new Exception('where express error:' . var_export($value, true));
  411. }
  412. return '( ' . $key . ' ' . $op . ' ' . $this->parseKey($query, $field2, true) . ' )';
  413. }
  414. /**
  415. * 表达式查询
  416. * @access protected
  417. * @param Query $query 查询对象
  418. * @param string $key
  419. * @param string $exp
  420. * @param Expression $value
  421. * @param string $field
  422. * @param integer $bindType
  423. * @return string
  424. */
  425. protected function parseExp(Query $query, $key, $exp, Expression $value, $field, $bindType)
  426. {
  427. // 表达式查询
  428. return '( ' . $key . ' ' . $value->getValue() . ' )';
  429. }
  430. /**
  431. * Null查询
  432. * @access protected
  433. * @param Query $query 查询对象
  434. * @param string $key
  435. * @param string $exp
  436. * @param mixed $value
  437. * @param string $field
  438. * @param integer $bindType
  439. * @return string
  440. */
  441. protected function parseNull(Query $query, $key, $exp, $value, $field, $bindType)
  442. {
  443. // NULL 查询
  444. return $key . ' IS ' . $exp;
  445. }
  446. /**
  447. * 范围查询
  448. * @access protected
  449. * @param Query $query 查询对象
  450. * @param string $key
  451. * @param string $exp
  452. * @param mixed $value
  453. * @param string $field
  454. * @param integer $bindType
  455. * @return string
  456. */
  457. protected function parseBetween(Query $query, $key, $exp, $value, $field, $bindType)
  458. {
  459. // BETWEEN 查询
  460. $data = is_array($value) ? $value : explode(',', $value);
  461. $min = $query->bind($data[0], $bindType);
  462. $max = $query->bind($data[1], $bindType);
  463. return $key . ' ' . $exp . ' :' . $min . ' AND :' . $max . ' ';
  464. }
  465. /**
  466. * Exists查询
  467. * @access protected
  468. * @param Query $query 查询对象
  469. * @param string $key
  470. * @param string $exp
  471. * @param mixed $value
  472. * @param string $field
  473. * @param integer $bindType
  474. * @return string
  475. */
  476. protected function parseExists(Query $query, $key, $exp, $value, $field, $bindType)
  477. {
  478. // EXISTS 查询
  479. if ($value instanceof \Closure) {
  480. $value = $this->parseClosure($query, $value, false);
  481. } elseif ($value instanceof Expression) {
  482. $value = $value->getValue();
  483. } else {
  484. throw new Exception('where express error:' . $value);
  485. }
  486. return $exp . ' (' . $value . ')';
  487. }
  488. /**
  489. * 时间比较查询
  490. * @access protected
  491. * @param Query $query 查询对象
  492. * @param string $key
  493. * @param string $exp
  494. * @param mixed $value
  495. * @param string $field
  496. * @param integer $bindType
  497. * @return string
  498. */
  499. protected function parseTime(Query $query, $key, $exp, $value, $field, $bindType)
  500. {
  501. return $key . ' ' . substr($exp, 0, 2) . ' ' . $this->parseDateTime($query, $value, $field, $bindType);
  502. }
  503. /**
  504. * 大小比较查询
  505. * @access protected
  506. * @param Query $query 查询对象
  507. * @param string $key
  508. * @param string $exp
  509. * @param mixed $value
  510. * @param string $field
  511. * @param integer $bindType
  512. * @return string
  513. */
  514. protected function parseCompare(Query $query, $key, $exp, $value, $field, $bindType)
  515. {
  516. if (is_array($value)) {
  517. throw new Exception('where express error:' . $exp . var_export($value, true));
  518. }
  519. // 比较运算
  520. if ($value instanceof \Closure) {
  521. $value = $this->parseClosure($query, $value);
  522. }
  523. if ('=' == $exp && is_null($value)) {
  524. return $key . ' IS NULL';
  525. }
  526. return $key . ' ' . $exp . ' ' . $value;
  527. }
  528. /**
  529. * 时间范围查询
  530. * @access protected
  531. * @param Query $query 查询对象
  532. * @param string $key
  533. * @param string $exp
  534. * @param mixed $value
  535. * @param string $field
  536. * @param integer $bindType
  537. * @return string
  538. */
  539. protected function parseBetweenTime(Query $query, $key, $exp, $value, $field, $bindType)
  540. {
  541. if (is_string($value)) {
  542. $value = explode(',', $value);
  543. }
  544. return $key . ' ' . substr($exp, 0, -4)
  545. . $this->parseDateTime($query, $value[0], $field, $bindType)
  546. . ' AND '
  547. . $this->parseDateTime($query, $value[1], $field, $bindType);
  548. }
  549. /**
  550. * IN查询
  551. * @access protected
  552. * @param Query $query 查询对象
  553. * @param string $key
  554. * @param string $exp
  555. * @param mixed $value
  556. * @param string $field
  557. * @param integer $bindType
  558. * @return string
  559. */
  560. protected function parseIn(Query $query, $key, $exp, $value, $field, $bindType)
  561. {
  562. // IN 查询
  563. if ($value instanceof \Closure) {
  564. $value = $this->parseClosure($query, $value, false);
  565. } elseif ($value instanceof Expression) {
  566. $value = $value->getValue();
  567. } else {
  568. $value = array_unique(is_array($value) ? $value : explode(',', $value));
  569. $array = [];
  570. foreach ($value as $k => $v) {
  571. $name = $query->bind($v, $bindType);
  572. $array[] = ':' . $name;
  573. }
  574. if (count($array) == 1) {
  575. return $key . ('IN' == $exp ? ' = ' : ' <> ') . $array[0];
  576. } else {
  577. $zone = implode(',', $array);
  578. $value = empty($zone) ? "''" : $zone;
  579. }
  580. }
  581. return $key . ' ' . $exp . ' (' . $value . ')';
  582. }
  583. /**
  584. * 闭包子查询
  585. * @access protected
  586. * @param Query $query 查询对象
  587. * @param \Closure $call
  588. * @param bool $show
  589. * @return string
  590. */
  591. protected function parseClosure(Query $query, $call, $show = true)
  592. {
  593. $newQuery = $query->newQuery()->removeOption();
  594. $call($newQuery);
  595. return $newQuery->buildSql($show);
  596. }
  597. /**
  598. * 日期时间条件解析
  599. * @access protected
  600. * @param Query $query 查询对象
  601. * @param string $value
  602. * @param string $key
  603. * @param integer $bindType
  604. * @return string
  605. */
  606. protected function parseDateTime(Query $query, $value, $key, $bindType = null)
  607. {
  608. $options = $query->getOptions();
  609. // 获取时间字段类型
  610. if (strpos($key, '.')) {
  611. list($table, $key) = explode('.', $key);
  612. if (isset($options['alias']) && $pos = array_search($table, $options['alias'])) {
  613. $table = $pos;
  614. }
  615. } else {
  616. $table = $options['table'];
  617. }
  618. $type = $this->connection->getTableInfo($table, 'type');
  619. if (isset($type[$key])) {
  620. $info = $type[$key];
  621. }
  622. if (isset($info)) {
  623. if (is_string($value)) {
  624. $value = strtotime($value) ?: $value;
  625. }
  626. if (preg_match('/(datetime|timestamp)/is', $info)) {
  627. // 日期及时间戳类型
  628. $value = date('Y-m-d H:i:s', $value);
  629. } elseif (preg_match('/(date)/is', $info)) {
  630. // 日期及时间戳类型
  631. $value = date('Y-m-d', $value);
  632. }
  633. }
  634. $name = $query->bind($value, $bindType);
  635. return ':' . $name;
  636. }
  637. /**
  638. * limit分析
  639. * @access protected
  640. * @param Query $query 查询对象
  641. * @param mixed $limit
  642. * @return string
  643. */
  644. protected function parseLimit(Query $query, $limit)
  645. {
  646. return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';
  647. }
  648. /**
  649. * join分析
  650. * @access protected
  651. * @param Query $query 查询对象
  652. * @param array $join
  653. * @return string
  654. */
  655. protected function parseJoin(Query $query, $join)
  656. {
  657. $joinStr = '';
  658. if (!empty($join)) {
  659. foreach ($join as $item) {
  660. list($table, $type, $on) = $item;
  661. $condition = [];
  662. foreach ((array) $on as $val) {
  663. if ($val instanceof Expression) {
  664. $condition[] = $val->getValue();
  665. } elseif (strpos($val, '=')) {
  666. list($val1, $val2) = explode('=', $val, 2);
  667. $condition[] = $this->parseKey($query, $val1) . '=' . $this->parseKey($query, $val2);
  668. } else {
  669. $condition[] = $val;
  670. }
  671. }
  672. $table = $this->parseTable($query, $table);
  673. $joinStr .= ' ' . $type . ' JOIN ' . $table . ' ON ' . implode(' AND ', $condition);
  674. }
  675. }
  676. return $joinStr;
  677. }
  678. /**
  679. * order分析
  680. * @access protected
  681. * @param Query $query 查询对象
  682. * @param mixed $order
  683. * @return string
  684. */
  685. protected function parseOrder(Query $query, $order)
  686. {
  687. foreach ($order as $key => $val) {
  688. if ($val instanceof Expression) {
  689. $array[] = $val->getValue();
  690. } elseif (is_array($val) && preg_match('/^[\w\.]+$/', $key)) {
  691. $array[] = $this->parseOrderField($query, $key, $val);
  692. } elseif ('[rand]' == $val) {
  693. $array[] = $this->parseRand($query);
  694. } elseif (is_string($val)) {
  695. if (is_numeric($key)) {
  696. list($key, $sort) = explode(' ', strpos($val, ' ') ? $val : $val . ' ');
  697. } else {
  698. $sort = $val;
  699. }
  700. if (preg_match('/^[\w\.]+$/', $key)) {
  701. $sort = strtoupper($sort);
  702. $sort = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : '';
  703. $array[] = $this->parseKey($query, $key, true) . $sort;
  704. } else {
  705. throw new Exception('order express error:' . $key);
  706. }
  707. }
  708. }
  709. return empty($array) ? '' : ' ORDER BY ' . implode(',', $array);
  710. }
  711. /**
  712. * orderField分析
  713. * @access protected
  714. * @param Query $query 查询对象
  715. * @param mixed $key
  716. * @param array $val
  717. * @return string
  718. */
  719. protected function parseOrderField($query, $key, $val)
  720. {
  721. if (isset($val['sort'])) {
  722. $sort = $val['sort'];
  723. unset($val['sort']);
  724. } else {
  725. $sort = '';
  726. }
  727. $sort = strtoupper($sort);
  728. $sort = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : '';
  729. $options = $query->getOptions();
  730. $bind = $this->connection->getFieldsBind($options['table']);
  731. foreach ($val as $k => $item) {
  732. $val[$k] = $this->parseDataBind($query, $key, $item, $bind);
  733. }
  734. return 'field(' . $this->parseKey($query, $key, true) . ',' . implode(',', $val) . ')' . $sort;
  735. }
  736. /**
  737. * group分析
  738. * @access protected
  739. * @param Query $query 查询对象
  740. * @param mixed $group
  741. * @return string
  742. */
  743. protected function parseGroup(Query $query, $group)
  744. {
  745. if (empty($group)) {
  746. return '';
  747. }
  748. if (is_string($group)) {
  749. $group = explode(',', $group);
  750. }
  751. foreach ($group as $key) {
  752. $val[] = $this->parseKey($query, $key);
  753. }
  754. return ' GROUP BY ' . implode(',', $val);
  755. }
  756. /**
  757. * having分析
  758. * @access protected
  759. * @param Query $query 查询对象
  760. * @param string $having
  761. * @return string
  762. */
  763. protected function parseHaving(Query $query, $having)
  764. {
  765. return !empty($having) ? ' HAVING ' . $having : '';
  766. }
  767. /**
  768. * comment分析
  769. * @access protected
  770. * @param Query $query 查询对象
  771. * @param string $comment
  772. * @return string
  773. */
  774. protected function parseComment(Query $query, $comment)
  775. {
  776. if (false !== strpos($comment, '*/')) {
  777. $comment = strstr($comment, '*/', true);
  778. }
  779. return !empty($comment) ? ' /* ' . $comment . ' */' : '';
  780. }
  781. /**
  782. * distinct分析
  783. * @access protected
  784. * @param Query $query 查询对象
  785. * @param mixed $distinct
  786. * @return string
  787. */
  788. protected function parseDistinct(Query $query, $distinct)
  789. {
  790. return !empty($distinct) ? ' DISTINCT ' : '';
  791. }
  792. /**
  793. * union分析
  794. * @access protected
  795. * @param Query $query 查询对象
  796. * @param mixed $union
  797. * @return string
  798. */
  799. protected function parseUnion(Query $query, $union)
  800. {
  801. if (empty($union)) {
  802. return '';
  803. }
  804. $type = $union['type'];
  805. unset($union['type']);
  806. foreach ($union as $u) {
  807. if ($u instanceof \Closure) {
  808. $sql[] = $type . ' ' . $this->parseClosure($query, $u);
  809. } elseif (is_string($u)) {
  810. $sql[] = $type . ' ( ' . $this->connection->parseSqlTable($u) . ' )';
  811. }
  812. }
  813. return ' ' . implode(' ', $sql);
  814. }
  815. /**
  816. * index分析,可在操作链中指定需要强制使用的索引
  817. * @access protected
  818. * @param Query $query 查询对象
  819. * @param mixed $index
  820. * @return string
  821. */
  822. protected function parseForce(Query $query, $index)
  823. {
  824. if (empty($index)) {
  825. return '';
  826. }
  827. return sprintf(" FORCE INDEX ( %s ) ", is_array($index) ? implode(',', $index) : $index);
  828. }
  829. /**
  830. * 设置锁机制
  831. * @access protected
  832. * @param Query $query 查询对象
  833. * @param bool|string $lock
  834. * @return string
  835. */
  836. protected function parseLock(Query $query, $lock = false)
  837. {
  838. if (is_bool($lock)) {
  839. return $lock ? ' FOR UPDATE ' : '';
  840. } elseif (is_string($lock) && !empty($lock)) {
  841. return ' ' . trim($lock) . ' ';
  842. }
  843. }
  844. /**
  845. * 生成查询SQL
  846. * @access public
  847. * @param Query $query 查询对象
  848. * @return string
  849. */
  850. public function select(Query $query)
  851. {
  852. $options = $query->getOptions();
  853. return str_replace(
  854. ['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
  855. [
  856. $this->parseTable($query, $options['table']),
  857. $this->parseDistinct($query, $options['distinct']),
  858. $this->parseField($query, $options['field']),
  859. $this->parseJoin($query, $options['join']),
  860. $this->parseWhere($query, $options['where']),
  861. $this->parseGroup($query, $options['group']),
  862. $this->parseHaving($query, $options['having']),
  863. $this->parseOrder($query, $options['order']),
  864. $this->parseLimit($query, $options['limit']),
  865. $this->parseUnion($query, $options['union']),
  866. $this->parseLock($query, $options['lock']),
  867. $this->parseComment($query, $options['comment']),
  868. $this->parseForce($query, $options['force']),
  869. ],
  870. $this->selectSql);
  871. }
  872. /**
  873. * 生成Insert SQL
  874. * @access public
  875. * @param Query $query 查询对象
  876. * @param bool $replace 是否replace
  877. * @return string
  878. */
  879. public function insert(Query $query, $replace = false)
  880. {
  881. $options = $query->getOptions();
  882. // 分析并处理数据
  883. $data = $this->parseData($query, $options['data']);
  884. if (empty($data)) {
  885. return '';
  886. }
  887. $fields = array_keys($data);
  888. $values = array_values($data);
  889. return str_replace(
  890. ['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
  891. [
  892. $replace ? 'REPLACE' : 'INSERT',
  893. $this->parseTable($query, $options['table']),
  894. implode(' , ', $fields),
  895. implode(' , ', $values),
  896. $this->parseComment($query, $options['comment']),
  897. ],
  898. $this->insertSql);
  899. }
  900. /**
  901. * 生成insertall SQL
  902. * @access public
  903. * @param Query $query 查询对象
  904. * @param array $dataSet 数据集
  905. * @param bool $replace 是否replace
  906. * @return string
  907. */
  908. public function insertAll(Query $query, $dataSet, $replace = false)
  909. {
  910. $options = $query->getOptions();
  911. // 获取合法的字段
  912. if ('*' == $options['field']) {
  913. $allowFields = $this->connection->getTableFields($options['table']);
  914. } else {
  915. $allowFields = $options['field'];
  916. }
  917. // 获取绑定信息
  918. $bind = $this->connection->getFieldsBind($options['table']);
  919. foreach ($dataSet as $data) {
  920. $data = $this->parseData($query, $data, $allowFields, $bind);
  921. $values[] = 'SELECT ' . implode(',', array_values($data));
  922. if (!isset($insertFields)) {
  923. $insertFields = array_keys($data);
  924. }
  925. }
  926. $fields = [];
  927. foreach ($insertFields as $field) {
  928. $fields[] = $this->parseKey($query, $field);
  929. }
  930. return str_replace(
  931. ['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
  932. [
  933. $replace ? 'REPLACE' : 'INSERT',
  934. $this->parseTable($query, $options['table']),
  935. implode(' , ', $fields),
  936. implode(' UNION ALL ', $values),
  937. $this->parseComment($query, $options['comment']),
  938. ],
  939. $this->insertAllSql);
  940. }
  941. /**
  942. * 生成slect insert SQL
  943. * @access public
  944. * @param Query $query 查询对象
  945. * @param array $fields 数据
  946. * @param string $table 数据表
  947. * @return string
  948. */
  949. public function selectInsert(Query $query, $fields, $table)
  950. {
  951. if (is_string($fields)) {
  952. $fields = explode(',', $fields);
  953. }
  954. foreach ($fields as &$field) {
  955. $field = $this->parseKey($query, $field, true);
  956. }
  957. return 'INSERT INTO ' . $this->parseTable($query, $table) . ' (' . implode(',', $fields) . ') ' . $this->select($query);
  958. }
  959. /**
  960. * 生成update SQL
  961. * @access public
  962. * @param Query $query 查询对象
  963. * @return string
  964. */
  965. public function update(Query $query)
  966. {
  967. $options = $query->getOptions();
  968. $data = $this->parseData($query, $options['data']);
  969. if (empty($data)) {
  970. return '';
  971. }
  972. foreach ($data as $key => $val) {
  973. $set[] = $key . ' = ' . $val;
  974. }
  975. return str_replace(
  976. ['%TABLE%', '%SET%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
  977. [
  978. $this->parseTable($query, $options['table']),
  979. implode(' , ', $set),
  980. $this->parseJoin($query, $options['join']),
  981. $this->parseWhere($query, $options['where']),
  982. $this->parseOrder($query, $options['order']),
  983. $this->parseLimit($query, $options['limit']),
  984. $this->parseLock($query, $options['lock']),
  985. $this->parseComment($query, $options['comment']),
  986. ],
  987. $this->updateSql);
  988. }
  989. /**
  990. * 生成delete SQL
  991. * @access public
  992. * @param Query $query 查询对象
  993. * @return string
  994. */
  995. public function delete(Query $query)
  996. {
  997. $options = $query->getOptions();
  998. return str_replace(
  999. ['%TABLE%', '%USING%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
  1000. [
  1001. $this->parseTable($query, $options['table']),
  1002. !empty($options['using']) ? ' USING ' . $this->parseTable($query, $options['using']) . ' ' : '',
  1003. $this->parseJoin($query, $options['join']),
  1004. $this->parseWhere($query, $options['where']),
  1005. $this->parseOrder($query, $options['order']),
  1006. $this->parseLimit($query, $options['limit']),
  1007. $this->parseLock($query, $options['lock']),
  1008. $this->parseComment($query, $options['comment']),
  1009. ],
  1010. $this->deleteSql);
  1011. }
  1012. }