3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Adapter\Platform
;
12 use Zend\Db\Adapter\Driver\DriverInterface
;
13 use Zend\Db\Adapter\Driver\Mysqli
;
14 use Zend\Db\Adapter\Driver\Pdo
;
15 use Zend\Db\Adapter\Exception
;
17 class Mysql
implements PlatformInterface
19 /** @var \mysqli|\PDO */
20 protected $resource = null;
22 public function __construct($driver = null)
25 $this->setDriver($driver);
30 * @param \Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo||\mysqli|\PDO $driver
31 * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
34 public function setDriver($driver)
36 // handle Zend\Db drivers
37 if ($driver instanceof Mysqli\Mysqli
38 ||
($driver instanceof Pdo\Pdo
&& $driver->getDatabasePlatformName() == 'Mysql')
39 ||
($driver instanceof \mysqli
)
40 ||
($driver instanceof \PDO
&& $driver->getAttribute(\PDO
::ATTR_DRIVER_NAME
) == 'mysql')
42 $this->resource = $driver;
46 throw new Exception\
InvalidArgumentException('$driver must be a Mysqli or Mysql PDO Zend\Db\Adapter\Driver, Mysqli instance or MySQL PDO instance');
54 public function getName()
60 * Get quote identifier symbol
64 public function getQuoteIdentifierSymbol()
72 * @param string $identifier
75 public function quoteIdentifier($identifier)
77 return '`' . str_replace('`', '``', $identifier) . '`';
81 * Quote identifier chain
83 * @param string|string[] $identifierChain
86 public function quoteIdentifierChain($identifierChain)
88 $identifierChain = str_replace('`', '``', $identifierChain);
89 if (is_array($identifierChain)) {
90 $identifierChain = implode('`.`', $identifierChain);
92 return '`' . $identifierChain . '`';
96 * Get quote value symbol
100 public function getQuoteValueSymbol()
108 * @param string $value
111 public function quoteValue($value)
113 if ($this->resource instanceof DriverInterface
) {
114 $this->resource = $this->resource->getConnection()->getResource();
116 if ($this->resource instanceof \mysqli
) {
117 return '\'' . $this->resource->real_escape_string($value) . '\'';
119 if ($this->resource instanceof \PDO
) {
120 return $this->resource->quote($value);
123 'Attempting to quote a value in ' . __CLASS__
. ' without extension/driver support '
124 . 'can introduce security vulnerabilities in a production environment.'
126 return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
130 * Quote Trusted Value
132 * The ability to quote values without notices
137 public function quoteTrustedValue($value)
139 if ($this->resource instanceof DriverInterface
) {
140 $this->resource = $this->resource->getConnection()->getResource();
142 if ($this->resource instanceof \mysqli
) {
143 return '\'' . $this->resource->real_escape_string($value) . '\'';
145 if ($this->resource instanceof \PDO
) {
146 return $this->resource->quote($value);
148 return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
154 * @param string|string[] $valueList
157 public function quoteValueList($valueList)
159 if (!is_array($valueList)) {
160 return $this->quoteValue($valueList);
163 $value = reset($valueList);
165 $valueList[key($valueList)] = $this->quoteValue($value);
166 } while ($value = next($valueList));
167 return implode(', ', $valueList);
171 * Get identifier separator
175 public function getIdentifierSeparator()
181 * Quote identifier in fragment
183 * @param string $identifier
184 * @param array $safeWords
187 public function quoteIdentifierInFragment($identifier, array $safeWords = array())
189 // regex taken from @link http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
190 $parts = preg_split('#([^0-9,a-z,A-Z$_])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
192 $safeWords = array_flip($safeWords);
193 $safeWords = array_change_key_case($safeWords, CASE_LOWER
);
195 foreach ($parts as $i => $part) {
196 if ($safeWords && isset($safeWords[strtolower($part)])) {
209 $parts[$i] = '`' . str_replace('`', '``', $part) . '`';
212 return implode('', $parts);