Repository navigation
Expand file tree
/
Copy pathSql.php
More file actions
144 lines (115 loc) · 4.17 KB
/
Copy pathSql.php
File metadata and controls
144 lines (115 loc) · 4.17 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
144
<?php
declare(strict_types=1);
namespace PhpDb\Sql;
use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\StatementInterface;
use function sprintf;
class Sql
{
protected AdapterInterface $adapter;
protected TableIdentifier|string|array|null $table;
protected Platform\PlatformDecoratorInterface $sqlPlatform;
public function __construct(
AdapterInterface $adapter,
array|string|TableIdentifier|null $table = null,
) {
$this->table = $table;
$this->adapter = $adapter;
$this->sqlPlatform = $adapter->getPlatform()->getSqlPlatformDecorator();
}
/**
* @throws Exception\InvalidArgumentException
*/
public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null): string
{
if (! $this->sqlPlatform instanceof SqlInterface) {
throw new Exception\RuntimeException(
'The subject does not implement SqlInterface',
);
}
$this->sqlPlatform->setSubject($sqlObject);
return $this->sqlPlatform->getSqlString(
$adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform(),
);
}
public function delete(string|TableIdentifier|null $table = null): Delete
{
if (null !== $this->table && null !== $table) {
throw new Exception\InvalidArgumentException(sprintf(
'This Sql object is intended to work with only the table "%s" provided at construction time.',
$this->table,
));
}
return new Delete($table ?: $this->table);
}
public function getAdapter(): ?AdapterInterface
{
return $this->adapter;
}
public function getSqlPlatform(): ?Platform\PlatformDecoratorInterface
{
return $this->sqlPlatform;
}
public function getTable(): array|string|TableIdentifier|null
{
return $this->table;
}
public function hasTable(): bool
{
return null !== $this->table;
}
public function insert(string|TableIdentifier|null $table = null): Insert
{
if (null !== $this->table && null !== $table) {
throw new Exception\InvalidArgumentException(sprintf(
'This Sql object is intended to work with only the table "%s" provided at construction time.',
$this->table,
));
}
return new Insert($table ?: $this->table);
}
public function prepareStatementForSqlObject(
PreparableSqlInterface $sqlObject,
?StatementInterface $statement = null,
?AdapterInterface $adapter = null,
): StatementInterface {
if (! $this->sqlPlatform instanceof PreparableSqlInterface) {
throw new Exception\RuntimeException(
'The subject does not implement PreparableSqlInterface',
);
}
$adapter ??= $this->adapter;
$statement ??= $adapter->getDriver()->createStatement();
$this->sqlPlatform->setSubject($sqlObject);
$this->sqlPlatform->prepareStatement($adapter, $statement);
return $statement;
}
public function select(string|TableIdentifier|null $table = null): Select
{
if (null !== $this->table && null !== $table) {
throw new Exception\InvalidArgumentException(sprintf(
'This Sql object is intended to work with only the table "%s" provided at construction time.',
$this->table,
));
}
return new Select($table ?: $this->table);
}
/**
* @throws Exception\InvalidArgumentException
*/
public function setTable(array|string|TableIdentifier $table): self
{
$this->table = $table;
return $this;
}
public function update(string|TableIdentifier|null $table = null): Update
{
if (null !== $this->table && null !== $table) {
throw new Exception\InvalidArgumentException(sprintf(
'This Sql object is intended to work with only the table "%s" provided at construction time.',
$this->table,
));
}
return new Update($table ?: $this->table);
}
}