diff --git a/src/builder/DM.php b/src/builder/DM.php index 933403d..3436d37 100644 --- a/src/builder/DM.php +++ b/src/builder/DM.php @@ -3,8 +3,10 @@ namespace bingher\db\builder; +use Exception; use think\db\Builder; use think\db\BaseQuery as Query; +use think\db\Raw; /** * 达梦数据库驱动 @@ -64,14 +66,45 @@ protected function parseLock(Query $query, $lock = false): string * * @return string */ - public function parseKey(Query $query, $key, bool $strict = false): string + public function parseKey(Query $query, string|int|Raw $key, bool $strict = false): string { + if (is_int($key)) { + return (string) $key; + } elseif ($key instanceof Raw) { + return $this->parseRaw($query, $key); + } + $key = trim($key); - if (strpos($key, '->') && false === strpos($key, '(')) { + if (str_contains($key, '->') && !str_contains($key, '(')) { // JSON字段支持 - list($field, $name) = explode($key, '->'); - $key = $field . '."' . $name . '"'; + [$field, $name] = explode($key, '->'); + $key = $field . '."' . $name . '"'; + } elseif (str_contains($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) { + [$table, $key] = explode('.', $key, 2); + + $alias = $query->getOptions('alias'); + + if ('__TABLE__' == $table) { + $table = $query->getOptions('table'); + $table = is_array($table) ? array_shift($table) : $table; + } + + if (isset($alias[$table])) { + $table = $alias[$table]; + } + } + + if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) { + throw new Exception('not support data:' . $key); + } + + if ('*' != $key && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) { + $key = '"' . $key . '"'; + } + + if (isset($table)) { + $key = '"' . $table . '".' . $key; } return $key; diff --git a/tests/DmTest.php b/tests/DmTest.php index 1e45f9f..09c3402 100644 --- a/tests/DmTest.php +++ b/tests/DmTest.php @@ -451,4 +451,19 @@ public function testUpdate() dump($res); $this->assertTrue($res == 1); } + + public function testKeyword() + { + $db = Db::connect(); + $data = [ + "mysqlid" => "backup_database", + "ip" => "192.168.103.38", + "port" => 3306, + "user" => "root1", + "pwd" => "xmhymake1", + "state" => 1 + ]; + $res = $db->table('hy_mysql')->where('mysqlid', $data['mysqlid'])->update($data); + $this->assertEquals($res, 1); + } }