|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\sql\model; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\sql\framework\Executable; |
|
6
|
|
|
use mindplay\sql\framework\Statement; |
|
7
|
|
|
use mindplay\sql\framework\TypeProvider; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract base-class for all types of SQL query models. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Query implements Executable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var TypeProvider |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $types; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array map where placeholder name => mixed value types |
|
21
|
|
|
*/ |
|
22
|
|
|
private $params = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Type[] map where placeholder name => Type instance |
|
26
|
|
|
*/ |
|
27
|
|
|
private $param_types = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param TypeProvider $types |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function __construct(TypeProvider $types) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->types = $types; |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Bind an individual placeholder name to a given value. |
|
39
|
|
|
* |
|
40
|
|
|
* The `$type` argument is optional for scalar types (string, int, float, bool, null) and arrays of scalar values. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name placeholder name |
|
43
|
|
|
* @param mixed $value |
|
44
|
|
|
* @param Type|string|null $type Type instance, or Type class-name (or NULL for scalar types) |
|
45
|
|
|
* |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function bind($name, $value, $type = null) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->params[$name] = $value; |
|
51
|
|
|
|
|
52
|
1 |
|
$this->param_types[$name] = is_string($type) |
|
53
|
1 |
|
? $this->types->getType($type) |
|
54
|
1 |
|
: $type; // assumes Type instance (or NULL) |
|
55
|
|
|
|
|
56
|
1 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getParams() |
|
63
|
|
|
{ |
|
64
|
1 |
|
$params = []; |
|
65
|
|
|
|
|
66
|
1 |
|
foreach ($this->params as $name => $value) { |
|
67
|
1 |
|
$params[$name] = isset($this->param_types[$name]) |
|
68
|
1 |
|
? $this->param_types[$name]->convertToSQL($value) |
|
69
|
1 |
|
: $value; // assume scalar value (or array of scalar values) |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $params; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Internally creates a full Type-map for all Columns in a given Table |
|
77
|
|
|
* |
|
78
|
|
|
* @param Table $table |
|
79
|
|
|
* |
|
80
|
|
|
* @return Type[] map where Column Alias maps to Type |
|
81
|
|
|
*/ |
|
82
|
1 |
|
protected function createTypeMap(Table $table) |
|
83
|
|
|
{ |
|
84
|
1 |
|
$type_map = []; |
|
85
|
|
|
|
|
86
|
1 |
|
foreach ($table->listColumns() as $column) { |
|
87
|
1 |
|
$type_map[$column->getAlias() ?: $column->getName()] = $column->getType(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $type_map; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|