This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
Sql.php
174 lines (155 loc) · 5.25 KB
/
Sql.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Db\Sql;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Platform\PlatformInterface;
class Sql
{
/** @var AdapterInterface */
protected $adapter = null;
/** @var string|array|TableIdentifier */
protected $table = null;
/** @var Platform\Platform */
protected $sqlPlatform = null;
/**
* @param AdapterInterface $adapter
* @param null|string|array|TableIdentifier $table
* @param null|Platform\AbstractPlatform $sqlPlatform @deprecated since version 3.0
*/
public function __construct(AdapterInterface $adapter, $table = null, Platform\AbstractPlatform $sqlPlatform = null)
{
$this->adapter = $adapter;
if ($table) {
$this->setTable($table);
}
$this->sqlPlatform = $sqlPlatform ?: new Platform\Platform($adapter);
}
/**
* @return null|\Zend\Db\Adapter\AdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
public function hasTable()
{
return ($this->table !== null);
}
/**
* @param string|array|TableIdentifier $table
* @return self Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function setTable($table)
{
if (is_string($table) || is_array($table) || $table instanceof TableIdentifier) {
$this->table = $table;
} else {
throw new Exception\InvalidArgumentException(
'Table must be a string, array or instance of TableIdentifier.'
);
}
return $this;
}
public function getTable()
{
return $this->table;
}
public function getSqlPlatform()
{
return $this->sqlPlatform;
}
public function select($table = null)
{
if ($this->table !== null && $table !== null) {
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);
}
public function insert($table = null)
{
if ($this->table !== null && $table !== null) {
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 update($table = null)
{
if ($this->table !== null && $table !== null) {
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);
}
public function delete($table = null)
{
if ($this->table !== null && $table !== null) {
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);
}
/**
* @param PreparableSqlInterface $sqlObject
* @param StatementInterface $statement
* @param AdapterInterface $adapter
*
* @return StatementInterface
*/
public function prepareStatementForSqlObject(
PreparableSqlInterface $sqlObject,
StatementInterface $statement = null,
AdapterInterface $adapter = null
) {
$adapter = $adapter ?: $this->adapter;
$statement = $statement ?: $adapter->getDriver()->createStatement();
return $this->sqlPlatform->setSubject($sqlObject)->prepareStatement($adapter, $statement);
}
/**
* Get sql string using platform or sql object
*
* @param SqlInterface $sqlObject
* @param PlatformInterface|null $platform
*
* @return string
*
* @deprecated Deprecated in 2.4. Use buildSqlString() instead
*/
public function getSqlStringForSqlObject(SqlInterface $sqlObject, PlatformInterface $platform = null)
{
$platform = ($platform) ?: $this->adapter->getPlatform();
return $this->sqlPlatform->setSubject($sqlObject)->getSqlString($platform);
}
/**
* @param SqlInterface $sqlObject
* @param AdapterInterface $adapter
*
* @return string
*
* @throws Exception\InvalidArgumentException
*/
public function buildSqlString(SqlInterface $sqlObject, AdapterInterface $adapter = null)
{
return $this
->sqlPlatform
->setSubject($sqlObject)
->getSqlString($adapter ? $adapter->getPlatform() : $this->adapter->getPlatform());
}
}