-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuilder.php
123 lines (110 loc) · 3.62 KB
/
Builder.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace YepSQL;
use PDO;
class Builder
{
private $pdo;
private $queries = [];
/* Map PHP types to PDO types */
private $type_map = [
'is_bool' => PDO::PARAM_BOOL,
'is_float' => PDO::PARAM_STR,
'is_int' => PDO::PARAM_INT,
'is_null' => PDO::PARAM_NULL,
'is_string' => PDO::PARAM_STR,
];
/**
* @param object $pdo instance of PDO
* @param string $filepath the path to file with queries
*/
function __construct(PDO $pdo, $filepath = null)
{
$this->pdo = $pdo;
if (!is_null($filepath)) {
$this->loadFromFile($filepath);
}
}
/**
* Query call: $yepsql_instance->query_name($args);
* @param string $name - query name
* @param array $args - query params
*/
public function __call($name, array $args = [])
{
if (!isset($this->queries[$name])) {
throw new BuilderException('Query "'. $name .'" does not exist', 4);
}
$r = $this->pdo->prepare($this->queries[$name]);
$args = empty($args) ? $args : $args[0];
foreach ($args as $argk => $argv) {
foreach($this->type_map as $php_type => $pdo_type) {
if ($php_type($argv)) {
$r->bindValue($argk, $argv, $pdo_type);
break;
}
/* Try to bind it anyway if we didn't find a match in the type
map */
$r->bindValue($argk, $argv);
}
}
$r->execute();
return $r;
}
/**
* Creates a query from a file
* @param string $path - path to file
*/
public function loadFromFile($filepath)
{
if (!file_exists($filepath)) {
throw new BuilderException("File not exists", 1);
}
$data = explode("\n", file_get_contents($filepath));
$tag = [];
foreach ($data as $line => $string) {
// remove extra characters (windows \r)
$string = str_replace("\r", '', $string);
if (preg_match("/^\s*--\s*name:\s*([a-zA-Z0-9_-]+)/", $string, $name)) {
// complete previous query
if (!empty($tag)) $this->createQueryWithTags($tag);
// new query
$tag = [
'name' => strtr($name[1], '-', '_'),
'query' => null
];
} elseif (preg_match('/^\s*--/', $string) || empty($string)) {
// sql comment or empty line
// do nothing
} elseif (empty($tag) && !preg_match('/^\s*--/', $string)) {
// invalid file:
throw new BuilderException('Parse error: the query definition without a "name:" line '.$line, 2);
} else {
// new query line
$tag['query'] .= $string."\n";
}
}
$this->createQueryWithTags($tag);
return $this->queries;
}
/**
* Create new query
* @param array $tag - request [name, query]
* @return array - new empty array
*/
private function createQueryWithTags(array $tag)
{
if (empty($tag['query'])) {
throw new BuilderException('Query "'.$tag['name'].'" is empty!', 3);
}
$this->queries[$tag['name']] = $tag['query'];
}
/**
* return query body
* @param string $name - request name
* @return string $query - request code
*/
public function getQuery($name)
{
return isset($this->queries[$name]) ? $this->queries[$name] : false;
}
}