Dm.php 4.4 KB

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