Mysql.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\builder;
  12. use think\db\Builder;
  13. use think\Exception;
  14. /**
  15. * mysql数据库驱动
  16. */
  17. class Mysql extends Builder
  18. {
  19. protected $insertAllSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES %DATA% %COMMENT%';
  20. protected $updateSql = 'UPDATE %TABLE% %JOIN% SET %SET% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
  21. /**
  22. * 生成insertall SQL
  23. * @access public
  24. * @param array $dataSet 数据集
  25. * @param array $options 表达式
  26. * @param bool $replace 是否replace
  27. * @return string
  28. * @throws Exception
  29. */
  30. public function insertAll($dataSet, $options = [], $replace = false)
  31. {
  32. // 获取合法的字段
  33. if ('*' == $options['field']) {
  34. $fields = array_keys($this->query->getFieldsType($options['table']));
  35. } else {
  36. $fields = $options['field'];
  37. }
  38. foreach ($dataSet as $data) {
  39. foreach ($data as $key => $val) {
  40. if (!in_array($key, $fields, true)) {
  41. if ($options['strict']) {
  42. throw new Exception('fields not exists:[' . $key . ']');
  43. }
  44. unset($data[$key]);
  45. } elseif (is_null($val)) {
  46. $data[$key] = 'NULL';
  47. } elseif (is_scalar($val)) {
  48. $data[$key] = $this->parseValue($val, $key);
  49. } elseif (is_object($val) && method_exists($val, '__toString')) {
  50. // 对象数据写入
  51. $data[$key] = $val->__toString();
  52. } else {
  53. // 过滤掉非标量数据
  54. unset($data[$key]);
  55. }
  56. }
  57. $value = array_values($data);
  58. $values[] = '( ' . implode(',', $value) . ' )';
  59. if (!isset($insertFields)) {
  60. $insertFields = array_map([$this, 'parseKey'], array_keys($data));
  61. }
  62. }
  63. return str_replace(
  64. ['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
  65. [
  66. $replace ? 'REPLACE' : 'INSERT',
  67. $this->parseTable($options['table'], $options),
  68. implode(' , ', $insertFields),
  69. implode(' , ', $values),
  70. $this->parseComment($options['comment']),
  71. ], $this->insertAllSql);
  72. }
  73. /**
  74. * 字段和表名处理
  75. * @access protected
  76. * @param string $key
  77. * @param array $options
  78. * @return string
  79. */
  80. protected function parseKey($key, $options = [])
  81. {
  82. $key = trim($key);
  83. if (strpos($key, '$.') && false === strpos($key, '(')) {
  84. // JSON字段支持
  85. list($field, $name) = explode('$.', $key);
  86. $key = 'json_extract(' . $field . ', \'$.' . $name . '\')';
  87. } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
  88. list($table, $key) = explode('.', $key, 2);
  89. if ('__TABLE__' == $table) {
  90. $table = $this->query->getTable();
  91. }
  92. if (isset($options['alias'][$table])) {
  93. $table = $options['alias'][$table];
  94. }
  95. }
  96. if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
  97. $key = '`' . $key . '`';
  98. }
  99. if (isset($table)) {
  100. if (strpos($table, '.')) {
  101. $table = str_replace('.', '`.`', $table);
  102. }
  103. $key = '`' . $table . '`.' . $key;
  104. }
  105. return $key;
  106. }
  107. /**
  108. * 随机排序
  109. * @access protected
  110. * @return string
  111. */
  112. protected function parseRand()
  113. {
  114. return 'rand()';
  115. }
  116. }