Mysql.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 (isset($options['alias'][$table])) {
  36. $table = $options['alias'][$table];
  37. }
  38. }
  39. if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
  40. $key = '`' . $key . '`';
  41. }
  42. if (isset($table)) {
  43. $key = '`' . $table . '`.' . $key;
  44. }
  45. return $key;
  46. }
  47. /**
  48. * 随机排序
  49. * @access protected
  50. * @return string
  51. */
  52. protected function parseRand()
  53. {
  54. return 'rand()';
  55. }
  56. }