/
MysqlAdapter.php
207 lines (187 loc) · 5.6 KB
/
MysqlAdapter.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Runtime\Adapter\Pdo;
use Propel\Runtime\Adapter\AdapterInterface;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Connection\StatementInterface;
use Propel\Runtime\Map\ColumnMap;
/**
* This is used in order to connect to a MySQL database.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @author Jon S. Stevens <jon@clearink.com> (Torque)
* @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
* @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
*/
class MysqlAdapter extends PdoAdapter implements AdapterInterface
{
/**
* Returns SQL which concatenates the second string to the first.
*
* @param string $s1 String to concatenate.
* @param string $s2 String to append.
*
* @return string
*/
public function concatString($s1, $s2)
{
return "CONCAT($s1, $s2)";
}
/**
* Returns SQL which extracts a substring.
*
* @param string $s String to extract from.
* @param integer $pos Offset to start from.
* @param integer $len Number of characters to extract.
*
* @return string
*/
public function subString($s, $pos, $len)
{
return "SUBSTRING($s, $pos, $len)";
}
/**
* Returns SQL which calculates the length (in chars) of a string.
*
* @param string $s String to calculate length of.
* @return string
*/
public function strLength($s)
{
return "CHAR_LENGTH($s)";
}
/**
* Locks the specified table.
*
* @param ConnectionInterface $con The Propel connection to use.
* @param string $table The name of the table to lock.
*
* @throws \PDOException No Statement could be created or executed.
*/
public function lockTable($con, $table)
{
$con->exec("LOCK TABLE $table WRITE");
}
/**
* Unlocks the specified table.
*
* @param ConnectionInterface $con The Propel connection to use.
* @param string $table The name of the table to unlock.
*
* @throws \PDOException No Statement could be created or executed.
*/
public function unlockTable($con, $table)
{
$con->exec('UNLOCK TABLES');
}
/**
* @see AdapterInterface::quoteIdentifier()
*
* @param string $text
* @return string
*/
public function quoteIdentifier($text)
{
return '`' . $text . '`';
}
/**
* @see AdapterInterface::quoteIdentifierTable()
*
* @param string $table
* @return string
*/
public function quoteIdentifierTable($table)
{
// e.g. 'database.table alias' should be escaped as '`database`.`table` `alias`'
return '`' . strtr($table, array('.' => '`.`', ' ' => '` `')) . '`';
}
/**
* @see AdapterInterface::useQuoteIdentifier()
*
* @return boolean
*/
public function useQuoteIdentifier()
{
return true;
}
/**
* @see AdapterInterface::applyLimit()
*
* @param string $sql
* @param integer $offset
* @param integer $limit
*/
public function applyLimit(&$sql, $offset, $limit)
{
if ($limit > 0) {
$sql .= ' LIMIT ' . ($offset > 0 ? $offset . ', ' : '') . $limit;
} elseif ($offset > 0) {
$sql .= ' LIMIT ' . $offset . ', 18446744073709551615';
}
}
/**
* @see AdapterInterface::random()
*
* @param string $seed
* @return string
*/
public function random($seed = null)
{
return 'rand('.((int) $seed).')';
}
/**
* @see AdapterInterface::bindValue()
*
* @param StatementInterface $stmt
* @param string $parameter
* @param mixed $value
* @param ColumnMap $cMap
* @param null|integer $position
*
* @return boolean
*/
public function bindValue(StatementInterface $stmt, $parameter, $value, ColumnMap $cMap, $position = null)
{
$pdoType = $cMap->getPdoType();
// FIXME - This is a temporary hack to get around apparent bugs w/ PDO+MYSQL
// See http://pecl.php.net/bugs/bug.php?id=9919
if (\PDO::PARAM_BOOL === $pdoType) {
$value = (int) $value;
$pdoType = \PDO::PARAM_INT;
return $stmt->bindValue($parameter, $value, $pdoType);
}
if ($cMap->isTemporal()) {
$value = $this->formatTemporalValue($value, $cMap);
} elseif (is_resource($value) && $cMap->isLob()) {
// we always need to make sure that the stream is rewound, otherwise nothing will
// get written to database.
rewind($value);
}
return $stmt->bindValue($parameter, $value, $pdoType);
}
/**
* Prepare the parameters for a PDO connection.
* Protects MySQL from charset injection risk.
* @see http://www.propelorm.org/ticket/1360
*
* @param array the connection parameters from the configuration
*
* @return array the modified parameters
*/
protected function prepareParams($params)
{
if (isset($params['settings']['charset'])) {
if (false === strpos($params['dsn'], ';charset=')) {
$params['dsn'] .= ';charset=' . $params['settings']['charset'];
unset($params['settings']['charset']);
}
}
return parent::prepareParams($params);
}
}