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\Pdo
;
14 use Zend\Db\Adapter\Exception
;
16 class Sqlite
implements PlatformInterface
20 protected $resource = null;
22 public function __construct($driver = null)
25 $this->setDriver($driver);
30 * @param \Zend\Db\Adapter\Driver\Pdo\Pdo||\PDO $driver
31 * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
34 public function setDriver($driver)
36 if (($driver instanceof \PDO
&& $driver->getAttribute(\PDO
::ATTR_DRIVER_NAME
) == 'sqlite')
37 ||
($driver instanceof Pdo\Pdo
&& $driver->getDatabasePlatformName() == 'Sqlite')
39 $this->resource = $driver;
43 throw new Exception\
InvalidArgumentException('$driver must be a Sqlite PDO Zend\Db\Adapter\Driver, Sqlite PDO instance');
51 public function getName()
57 * Get quote identifier symbol
61 public function getQuoteIdentifierSymbol()
69 * @param string $identifier
72 public function quoteIdentifier($identifier)
74 return '"' . str_replace('"', '\\' . '"', $identifier) . '"';
78 * Quote identifier chain
80 * @param string|string[] $identifierChain
83 public function quoteIdentifierChain($identifierChain)
85 $identifierChain = str_replace('"', '\\"', $identifierChain);
86 if (is_array($identifierChain)) {
87 $identifierChain = implode('"."', $identifierChain);
89 return '"' . $identifierChain . '"';
93 * Get quote value symbol
97 public function getQuoteValueSymbol()
105 * @param string $value
108 public function quoteValue($value)
110 if ($this->resource instanceof DriverInterface
) {
111 $this->resource = $this->resource->getConnection()->getResource();
113 if ($this->resource instanceof \PDO
) {
114 return $this->resource->quote($value);
117 'Attempting to quote a value in ' . __CLASS__
. ' without extension/driver support '
118 . 'can introduce security vulnerabilities in a production environment.'
120 return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
124 * Quote Trusted Value
126 * The ability to quote values without notices
131 public function quoteTrustedValue($value)
133 if ($this->resource instanceof DriverInterface
) {
134 $this->resource = $this->resource->getConnection()->getResource();
136 if ($this->resource instanceof \PDO
) {
137 return $this->resource->quote($value);
139 return '\'' . addcslashes($value, "\x00\n\r\\'\"\x1a") . '\'';
145 * @param string|string[] $valueList
148 public function quoteValueList($valueList)
150 if (!is_array($valueList)) {
151 return $this->quoteValue($valueList);
153 $value = reset($valueList);
155 $valueList[key($valueList)] = $this->quoteValue($value);
156 } while ($value = next($valueList));
157 return implode(', ', $valueList);
161 * Get identifier separator
165 public function getIdentifierSeparator()
171 * Quote identifier in fragment
173 * @param string $identifier
174 * @param array $safeWords
177 public function quoteIdentifierInFragment($identifier, array $safeWords = array())
179 $parts = preg_split('#([\.\s\W])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
181 $safeWords = array_flip($safeWords);
182 $safeWords = array_change_key_case($safeWords, CASE_LOWER
);
184 foreach ($parts as $i => $part) {
185 if ($safeWords && isset($safeWords[strtolower($part)])) {
198 $parts[$i] = '"' . str_replace('"', '\\' . '"', $part) . '"';
201 return implode('', $parts);