Skip to content

Commit 8371616

Browse files
committed
isValidName added
1 parent 763fa33 commit 8371616

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

src/ClickHouseSQLParser.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,22 @@ public function tableCreaterFnParser($sql) {
732732
return $ret;
733733
}
734734

735+
/**
736+
* Check if the name is valid id
737+
*
738+
* @param string $sql
739+
* @param boolean $return_name
740+
* @return boolean|string
741+
*/
742+
public function isValidName($sql, $return_name = false)
743+
{
744+
$idpat = "[a-z_][0-9a-z_]*";
745+
$pattern = "#^(`)?(?<name>(?(1)[^`]+|$idpat))(?(1)`)$#is";
746+
$matches = [];
747+
$result = \preg_match($pattern, $sql, $matches);
748+
return ($return_name) ? $matches['name'] : ($result == 1);
749+
}
750+
735751
/**
736752
* Parse last part of create-table-sql request like "ENGINE = MergeTree(...)"
737753
*
@@ -780,12 +796,11 @@ public function parseCreateEngineSql($create_engine)
780796
break;
781797
}
782798
}
783-
if (empty($primary_key) ||
784-
(\substr($primary_key, 0, 1) !=='(') ||
785-
(\substr($primary_key, -1) !==')')) {
786-
return "Primary key undefined or illegal";
799+
if ((\substr($primary_key, 0, 1) ==='(') && (\substr($primary_key, -1) ===')')) {
800+
$primary_key = $this->divParts(\substr($primary_key, 1, -1));
801+
} elseif ($this->isValidName($primary_key)) {
802+
$primary_key = [$primary_key];
787803
}
788-
$primary_key = $this->divParts(\substr($primary_key, 1, -1));
789804
if (!\is_array($primary_key) || empty($primary_key[0])) {
790805
return "Can't parse primary key";
791806
}

tests/src/CreaterTableParserTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ protected function setUp()
2727
public function createEngineProvider()
2828
{
2929
return [
30+
#0
31+
["Engine = MergeTree(a, b, 8192)",
32+
['engine' => 'MergeTree',
33+
'engine_param' => ['a', 'b', 8192],
34+
'merge_tree_fam' => true,
35+
'date_field' => 'a',
36+
'sampl' => false,
37+
'primary_key' => 'b',
38+
'granul' => 8192,
39+
'ver' => false
40+
]],
41+
42+
#1
3043
["ENGINE = MergeTree(dt, (id, dt), 8192, ver)",
3144
['engine' => 'MergeTree',
3245
'engine_param' => ['dt', '(id, dt)', 8192, 'ver'],
@@ -38,6 +51,7 @@ public function createEngineProvider()
3851
'ver' => 'ver'
3952
]],
4053

54+
#2
4155
["ENGINE = MergeTree(dt, (id, dt), 8192)",
4256
['engine' => 'MergeTree',
4357
'engine_param' => ['dt', '(id, dt)', 8192],
@@ -49,6 +63,7 @@ public function createEngineProvider()
4963
'ver' => ''
5064
]],
5165

66+
#3
5267
["ENGINE = MergeTree(dt, sampl, (id, dt), 8192)",
5368
['engine' => 'MergeTree',
5469
'engine_param' => ['dt', 'sampl', '(id, dt)', 8192],
@@ -60,6 +75,7 @@ public function createEngineProvider()
6075
'ver' => ''
6176
]],
6277

78+
#4
6379
["ENGINE = Log",
6480
['engine' => 'Log',
6581
'engine_param' => [''],
@@ -71,11 +87,14 @@ public function createEngineProvider()
7187
'ver' => ''
7288
]],
7389

90+
#5
7491
["ENGINE = Log(", false],
92+
93+
#6
7594
["IMAGINE = Log", false],
76-
["Engine = MergeTree(a, b, 8192)", false],
77-
["Engine = MergeTree(a, (), 8192)", false],
7895

96+
#7
97+
["Engine = MergeTree(a, (), 8192)", false],
7998
];
8099
}
81100

@@ -101,6 +120,9 @@ public function testParseCreateEngineSql($sql, $regpars)
101120
}
102121
}
103122
} else {
123+
if (\is_array($ans)) {
124+
print_r($ans);
125+
}
104126
$this->assertFalse(\is_array($ans));
105127
}
106128
}
@@ -707,6 +729,32 @@ public function testMakeFieldCreater($name, $type, $default, $creater)
707729
$this->assertEquals($creater, $ans);
708730
}
709731

732+
/**
733+
* Data provider for testIsValidName
734+
*
735+
* @return array of name, isValid, ret_name
736+
*/
737+
public function isValidNameProvider()
738+
{
739+
return [
740+
['id', true, false],
741+
['id', 'id', true],
742+
['', false, false],
743+
];
744+
}
745+
746+
/**
747+
* @dataProvider isValidNameProvider
748+
* @covers ierusalim\ClickHouse\ClickHouseSQLParser::isValidName
749+
* @todo Implement testMakeFieldCreater().
750+
*/
751+
public function testIsValidName($name, $isValid, $ret_name)
752+
{
753+
$ch = $this->object;
754+
$ans = $ch->isValidName($name, $ret_name);
755+
$this->assertEquals($isValid, $ans);
756+
}
757+
710758
/**
711759
* Data provider for testParseCreateFields
712760
*

0 commit comments

Comments
 (0)