Newer
Older
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
<?php
namespace Breyta;
use Breyta\Model\Statement;
class BasicAdapter implements AdapterInterface
{
/** @var callable */
protected $executor;
public function __construct(callable $executor)
{
$this->executor = $executor;
}
public function exec(string $sql)
{
$statement = $this->getStatement($sql);
call_user_func($this->executor, $statement);
return $statement->result;
}
public function getStatement(string $sql): Statement
{
$statement = new Statement;
$statement->raw = $sql;
$sql = preg_replace('/\s+/', ' ', $sql);
$statement->teaser = substr($sql, 0, 50);
$delimPattern = '(?>`|")?'; // optional delimiter pattern
$namePattern = $delimPattern . '(?>[a-z0-9_]+' . $delimPattern . '\.' . $delimPattern . ')?' . // schema
'[a-z0-9_]+' . $delimPattern;
if (preg_match(
'/^(alter|create|drop) ' . // action
'(?>[a-z=]+ )*?' . // something between like 'OR REPLACE', 'DEFINER = user' etc...
'(?>(table|index|function|trigger|view|procedure) )' . // type
'(' . $namePattern . ')' . // name
' /i',
$statement,
$match
)) {
$statement->teaser = implode(' ', [strtoupper($match[1]), strtoupper($match[2]), $match[3]]);
$statement->action = strtolower($match[1]);
$statement->type = strtolower($match[2]);
$statement->name = str_replace(['"', '`'], '', $match[3]);
} elseif (preg_match(
'/^(update|delete) ' . // action
'(' . $namePattern . ')' . // name
' /i',
$statement,
$match
)) {
$statement->teaser = implode(' ', [strtoupper($match[1]), $match[2]]);
$statement->action = strtolower($match[1]);
$statement->name = str_replace(['"', '`'], '', $match[2]);
}
return $statement;
}
}