|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\sql\framework; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\sql\model\Type; |
|
6
|
|
|
use UnexpectedValueException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Abstract base-class for all types of SQL Query models. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Query implements Executable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var TypeProvider |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $types; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array map where placeholder name => mixed value types |
|
20
|
|
|
*/ |
|
21
|
|
|
private $params = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Type[] map where placeholder name => Type instance |
|
25
|
|
|
*/ |
|
26
|
|
|
private $param_types = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param TypeProvider $types |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function __construct(TypeProvider $types) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->types = $types; |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Bind an individual placeholder name to a given value. |
|
38
|
|
|
* |
|
39
|
|
|
* The `$type` argument is optional for scalar types (string, int, float, bool, null) and arrays of scalar values. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name placeholder name |
|
42
|
|
|
* @param mixed $value |
|
43
|
|
|
* @param Type|string|null $type Type instance, or Type class-name (or NULL for scalar types) |
|
44
|
|
|
* |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function bind($name, $value, $type = null) |
|
48
|
|
|
{ |
|
49
|
1 |
|
static $SCALAR_TYPES = [ |
|
50
|
|
|
'integer' => true, |
|
51
|
|
|
'double' => true, |
|
52
|
|
|
'string' => true, |
|
53
|
|
|
'boolean' => true, |
|
54
|
|
|
'NULL' => true, |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
1 |
|
$value_type = gettype($value); |
|
58
|
|
|
|
|
59
|
1 |
|
if ($value_type === 'array') { |
|
60
|
1 |
|
foreach ($value as $item) { |
|
61
|
1 |
|
$item_type = gettype($item); |
|
62
|
|
|
|
|
63
|
1 |
|
if (! isset($SCALAR_TYPES[$item_type])) { |
|
64
|
1 |
|
throw new UnexpectedValueException("unexpected item type in array: {$item_type}"); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
1 |
|
if (! isset($SCALAR_TYPES[$value_type])) { |
|
69
|
1 |
|
throw new UnexpectedValueException("unexpected value type: {$value_type}"); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$this->params[$name] = $value; |
|
74
|
|
|
|
|
75
|
1 |
|
$this->param_types[$name] = is_string($type) |
|
76
|
1 |
|
? $this->types->getType($type) |
|
77
|
1 |
|
: $type; // assumes Type instance (or NULL) |
|
78
|
|
|
|
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Applies a set of placeholder name/value pairs and binds them to individual placeholders. |
|
84
|
|
|
* |
|
85
|
|
|
* This works for scalar values only (string, int, float, bool, null) and arrays of scalar values - to |
|
86
|
|
|
* bind values with `Type`-support, use the `bind()` method. |
|
87
|
|
|
* |
|
88
|
|
|
* @see bind() |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $params placeholder name/value pairs |
|
91
|
|
|
* |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function apply(array $params) |
|
95
|
|
|
{ |
|
96
|
1 |
|
foreach ($params as $name => $value) { |
|
97
|
1 |
|
$this->bind($name, $value); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @inheritdoc |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getParams() |
|
107
|
|
|
{ |
|
108
|
1 |
|
$params = []; |
|
109
|
|
|
|
|
110
|
1 |
|
foreach ($this->params as $name => $value) { |
|
111
|
1 |
|
$params[$name] = isset($this->param_types[$name]) |
|
112
|
1 |
|
? $this->param_types[$name]->convertToSQL($value) |
|
113
|
1 |
|
: $value; // assume scalar value (or array of scalar values) |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
return $params; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|