Mysql.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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\builder;
  12. use think\db\Builder;
  13. /**
  14. * mysql数据库驱动
  15. */
  16. class Mysql extends Builder
  17. {
  18. protected $updateSql = 'UPDATE %TABLE% %JOIN% SET %SET% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
  19. /**
  20. * 字段和表名处理
  21. * @access protected
  22. * @param string $key
  23. * @param array $options
  24. * @return string
  25. */
  26. protected function parseKey($key, $options = [])
  27. {
  28. $key = trim($key);
  29. if (strpos($key, '$.') && false === strpos($key, '(')) {
  30. // JSON字段支持
  31. list($field, $name) = explode('$.', $key);
  32. $key = 'json_extract(' . $field . ', \'$.' . $name . '\')';
  33. } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
  34. list($table, $key) = explode('.', $key, 2);
  35. if ('__TABLE__' == $table) {
  36. $table = $this->query->getTable();
  37. }
  38. if (isset($options['alias'][$table])) {
  39. $table = $options['alias'][$table];
  40. }
  41. }
  42. if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
  43. $key = '`' . $key . '`';
  44. }
  45. if (isset($table)) {
  46. if (strpos($table, '.')) {
  47. $table = str_replace('.', '`.`', $table);
  48. }
  49. $key = '`' . $table . '`.' . $key;
  50. }
  51. return $key;
  52. }
  53. /**
  54. * 随机排序
  55. * @access protected
  56. * @return string
  57. */
  58. protected function parseRand()
  59. {
  60. return 'rand()';
  61. }
  62. }