-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSelect.php
More file actions
79 lines (62 loc) · 1.82 KB
/
Select.php
File metadata and controls
79 lines (62 loc) · 1.82 KB
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
<?php
namespace Nulpunkt\Yesql\Statement;
class Select implements Statement
{
private $sql;
private $modline;
private $rowFunc;
private $rowClass;
private $stmt;
public function __construct($sql, $modline)
{
$this->sql = $sql;
$this->modline = $modline;
$this->rowFunc = $this->getRowFunc();
$this->rowClass = $this->getRowClass();
}
public function execute($db, $args)
{
if (!$this->stmt) {
$this->stmt = $db->prepare($this->sql);
}
$this->stmt->execute($args);
if ($this->rowClass) {
$this->stmt->setFetchMode(\PDO::FETCH_CLASS, $this->rowClass);
} else {
$this->stmt->setFetchMode(\PDO::FETCH_ASSOC);
}
$res = array_map([$this, 'prepareElement'], $this->stmt->fetchAll());
return $this->oneOrMany() == 'one' ? @$res[0] : $res;
}
private function oneOrMany()
{
preg_match("/\boneOrMany:\s*(one|many)/", $this->modline, $m);
return isset($m[1]) ? $m[1] : "many";
}
private function getRowClass()
{
preg_match('/rowClass:\s*(\S+)/', $this->modline, $m);
$c = @$m[1];
if ($c && !class_exists($c)) {
throw new \Nulpunkt\Yesql\Exception\ClassNotFound("{$c} is not a class");
}
return $c;
}
private function getRowFunc()
{
preg_match('/rowFunc:\s*(\S+)/', $this->modline, $m);
$f = isset($m[1]) ? $m[1] : [$this, 'identity'];
if ($f && !is_callable($f)) {
throw new \Nulpunkt\Yesql\Exception\MethodMissing("{$f} is not callable");
}
return $f;
}
private function prepareElement($res)
{
return call_user_func($this->rowFunc, $res);
}
private function identity($e)
{
return $e;
}
}