-
Notifications
You must be signed in to change notification settings - Fork 1
/
BiscuitChangeQuery.php
66 lines (58 loc) · 1.59 KB
/
BiscuitChangeQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/*
* EXAMPLES
* CHANGE users { add(join_date:date) };
* CHANGE users { remove(join_date) };
* CHANGE users { rename(join_date:join) };
*/
class BiscuitChangeQuery extends BiscuitQuery
{
private $clause_type;
private $arguments;
public function __construct($query)
{
parent::__construct($query);
$this->setTableName(explode(' ', $query)[1]);
// Get clause type
preg_match("/(\w+)\(.*?\)/", $query, $results);
$clause = $results[0];
if (!BiscuitChangeQuery::validateClauseType($results[1])) {
$this->clause_type = 'ndf'; // Set to NDF if is not valid
} else {
$this->clause_type = $results[1];
}
// Generate the arguments
$this->arguments = $this->genArguments($clause);
}
public function getClauseType()
{
return $this->clause_type;
}
public function getArguments()
{
return $this->arguments;
}
private function genArguments($clause)
{
preg_match("/\w+\((.*?)\)/", $clause, $results);
$results = explode(':', $results[1]);
$arguments_array = array(
'column' => $results[0]
);
if (count($results) > 1) {
$arguments_array['type'] = $results[1];
}
return $arguments_array;
}
public static function validateClauseType($type)
{
$valid_types = array('add', 'remove', 'rename');
foreach ($valid_types as $valid) {
if ($type == $valid) {
return true;
}
}
return false;
}
}
?>