Skip to content

Commit

Permalink
builder使用Oracle对应的Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hbh112233abc committed Apr 11, 2024
1 parent bcce262 commit 213cd01
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/builder/DM.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace bingher\db\builder;

use Exception;
use think\db\Builder;
use think\db\BaseQuery as Query;
use think\db\Raw;

/**
* 达梦数据库驱动
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tests/DmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 213cd01

Please sign in to comment.