This repository was archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathActiveQuery.php
More file actions
143 lines (122 loc) · 3.31 KB
/
ActiveQuery.php
File metadata and controls
143 lines (122 loc) · 3.31 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
declare(strict_types=1);
namespace Spiral\Database\Query;
use Spiral\Database\Driver\DriverInterface;
use Spiral\Database\Exception\BuilderException;
use Spiral\Database\Exception\StatementException;
use Throwable;
/**
* QueryBuilder classes generate set of control tokens for query compilers, this is query level
* abstraction.
*/
abstract class ActiveQuery implements QueryInterface
{
/** @var DriverInterface */
protected $driver;
/** @var string|null */
protected $prefix;
/**
* @return string
*/
public function __toString()
{
$parameters = new QueryParameters();
return Interpolator::interpolate(
$this->sqlStatement($parameters),
$parameters->getParameters()
);
}
/**
* @return array
*/
public function __debugInfo()
{
$parameters = new QueryParameters();
try {
$queryString = $this->sqlStatement($parameters);
} catch (Throwable $e) {
$queryString = "[ERROR: {$e->getMessage()}]";
}
return [
'queryString' => Interpolator::interpolate($queryString, $parameters->getParameters()),
'parameters' => $parameters->getParameters(),
'driver' => $this->driver
];
}
/**
* @param DriverInterface $driver
* @param string|null $prefix
* @return QueryInterface|$this
*/
public function withDriver(DriverInterface $driver, string $prefix = null): QueryInterface
{
$query = clone $this;
$query->driver = $driver;
$query->prefix = $prefix;
return $query;
}
/**
* @return DriverInterface|null
*/
public function getDriver(): ?DriverInterface
{
return $this->driver;
}
/**
* @return string|null
*/
public function getPrefix(): ?string
{
return $this->prefix;
}
/**
* Generate SQL query, must have associated driver instance.
*
* @param QueryParameters|null $parameters
* @return string
*/
public function sqlStatement(QueryParameters $parameters = null): string
{
if ($this->driver === null) {
throw new BuilderException('Unable to build query without associated driver');
}
return $this->driver->getQueryCompiler()->compile(
$parameters ?? new QueryParameters(),
$this->prefix ?? '',
$this
);
}
/**
* Compile and run query.
*
* @return mixed
*
* @throws BuilderException
* @throws StatementException
*/
abstract public function run();
/**
* Helper methods used to correctly fetch and split identifiers provided by function
* parameters. Example: fI(['name, email']) => 'name', 'email'
*
* @param array $identifiers
*
* @return array
*/
protected function fetchIdentifiers(array $identifiers): array
{
if (count($identifiers) === 1 && is_string($identifiers[0])) {
return array_map('trim', explode(',', $identifiers[0]));
}
if (count($identifiers) === 1 && is_array($identifiers[0])) {
return $identifiers[0];
}
return $identifiers;
}
}