Mysql.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 mixed $key
  77. * @param array $options
  78. * @return string
  79. */
  80. protected function parseKey($key, $options = [], $strict = false)
  81. {
  82. if (is_numeric($key)) {
  83. return $key;
  84. } elseif ($key instanceof Expression) {
  85. return $key->getValue();
  86. }
  87. $key = trim($key);
  88. if (strpos($key, '$.') && false === strpos($key, '(')) {
  89. // JSON字段支持
  90. list($field, $name) = explode('$.', $key);
  91. return 'json_extract(' . $field . ', \'$.' . $name . '\')';
  92. } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
  93. list($table, $key) = explode('.', $key, 2);
  94. if ('__TABLE__' == $table) {
  95. $table = $this->query->getTable();
  96. }
  97. if (isset($options['alias'][$table])) {
  98. $table = $options['alias'][$table];
  99. }
  100. }
  101. if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
  102. throw new Exception('not support data:' . $key);
  103. }
  104. if ('*' != $key && ($strict || !preg_match('/[,\'\"\*\(\)`.\s]/', $key))) {
  105. $key = '`' . $key . '`';
  106. }
  107. if (isset($table)) {
  108. if (strpos($table, '.')) {
  109. $table = str_replace('.', '`.`', $table);
  110. }
  111. $key = '`' . $table . '`.' . $key;
  112. }
  113. return $key;
  114. }
  115. /**
  116. * 随机排序
  117. * @access protected
  118. * @return string
  119. */
  120. protected function parseRand()
  121. {
  122. return 'rand()';
  123. }
  124. }