123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- <?php
- include_once DRUPAL_ROOT . '/includes/database/prefetch.inc';
- class DatabaseConnection_sqlite extends DatabaseConnection {
-
- protected $savepointSupport = FALSE;
-
- protected $willRollback;
-
- protected $attachedDatabases = array();
-
- var $tableDropped = FALSE;
- public function __construct(array $connection_options = array()) {
-
- $this->statementClass = NULL;
-
- $this->transactionSupport = $this->transactionalDDLSupport = !isset($connection_options['transactions']) || $connection_options['transactions'] !== FALSE;
- $this->connectionOptions = $connection_options;
-
- $connection_options += array(
- 'pdo' => array(),
- );
- $connection_options['pdo'] += array(
-
- PDO::ATTR_STRINGIFY_FETCHES => TRUE,
- );
- parent::__construct('sqlite:' . $connection_options['database'], '', '', $connection_options['pdo']);
-
- $prefixes = $this->prefixes;
- foreach ($prefixes as $table => &$prefix) {
-
- if (!empty($prefix)) {
-
- if (!isset($this->attachedDatabases[$prefix])) {
- $this->attachedDatabases[$prefix] = $prefix;
- $this->query('ATTACH DATABASE :database AS :prefix', array(':database' => $connection_options['database'] . '-' . $prefix, ':prefix' => $prefix));
- }
-
-
- $prefix .= '.';
- }
- }
-
- $this->setPrefix($prefixes);
-
- $version = $this->query('SELECT sqlite_version()')->fetchField();
- $this->savepointSupport = (version_compare($version, '3.6.8') >= 0);
-
- $this->sqliteCreateFunction('if', array($this, 'sqlFunctionIf'));
- $this->sqliteCreateFunction('greatest', array($this, 'sqlFunctionGreatest'));
- $this->sqliteCreateFunction('pow', 'pow', 2);
- $this->sqliteCreateFunction('length', 'strlen', 1);
- $this->sqliteCreateFunction('md5', 'md5', 1);
- $this->sqliteCreateFunction('concat', array($this, 'sqlFunctionConcat'));
- $this->sqliteCreateFunction('substring', array($this, 'sqlFunctionSubstring'), 3);
- $this->sqliteCreateFunction('substring_index', array($this, 'sqlFunctionSubstringIndex'), 3);
- $this->sqliteCreateFunction('rand', array($this, 'sqlFunctionRand'));
-
- if (isset($connection_options['init_commands'])) {
- $this->exec(implode('; ', $connection_options['init_commands']));
- }
- }
-
- public function __destruct() {
- if ($this->tableDropped && !empty($this->attachedDatabases)) {
- foreach ($this->attachedDatabases as $prefix) {
-
- try {
- $count = $this->query('SELECT COUNT(*) FROM ' . $prefix . '.sqlite_master WHERE type = :type AND name NOT LIKE :pattern', array(':type' => 'table', ':pattern' => 'sqlite_%'))->fetchField();
-
- if ($count == 0) {
-
- $this->query('DETACH DATABASE :schema', array(':schema' => $prefix));
-
- unlink($this->connectionOptions['database'] . '-' . $prefix);
- }
- }
- catch (Exception $e) {
-
-
- }
- }
- }
- }
-
- public function sqlFunctionIf($condition, $expr1, $expr2 = NULL) {
- return $condition ? $expr1 : $expr2;
- }
-
- public function sqlFunctionGreatest() {
- $args = func_get_args();
- foreach ($args as $k => $v) {
- if (!isset($v)) {
- unset($args);
- }
- }
- if (count($args)) {
- return max($args);
- }
- else {
- return NULL;
- }
- }
-
- public function sqlFunctionConcat() {
- $args = func_get_args();
- return implode('', $args);
- }
-
- public function sqlFunctionSubstring($string, $from, $length) {
- return substr($string, $from - 1, $length);
- }
-
- public function sqlFunctionSubstringIndex($string, $delimiter, $count) {
-
- if (empty($string)) {
- return '';
- }
- $end = 0;
- for ($i = 0; $i < $count; $i++) {
- $end = strpos($string, $delimiter, $end + 1);
- if ($end === FALSE) {
- $end = strlen($string);
- }
- }
- return substr($string, 0, $end);
- }
-
- public function sqlFunctionRand($seed = NULL) {
- if (isset($seed)) {
- mt_srand($seed);
- }
- return mt_rand() / mt_getrandmax();
- }
-
- public function prepare($query, $options = array()) {
- return new DatabaseStatement_sqlite($this, $query, $options);
- }
-
- public function PDOPrepare($query, array $options = array()) {
- return parent::prepare($query, $options);
- }
- public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
- return $this->query($query . ' LIMIT ' . (int) $from . ', ' . (int) $count, $args, $options);
- }
- public function queryTemporary($query, array $args = array(), array $options = array()) {
-
-
- $tablename = $this->generateTemporaryTableName();
- $prefixes = $this->prefixes;
- $prefixes[$tablename] = '';
- $this->setPrefix($prefixes);
- $this->query('CREATE TEMPORARY TABLE ' . $tablename . ' AS ' . $query, $args, $options);
- return $tablename;
- }
- public function driver() {
- return 'sqlite';
- }
- public function databaseType() {
- return 'sqlite';
- }
- public function mapConditionOperator($operator) {
-
- static $specials = array(
- 'LIKE' => array('postfix' => " ESCAPE '\\'"),
- 'NOT LIKE' => array('postfix' => " ESCAPE '\\'"),
- );
- return isset($specials[$operator]) ? $specials[$operator] : NULL;
- }
- public function prepareQuery($query) {
- return $this->prepare($this->prefixTables($query));
- }
- public function nextId($existing_id = 0) {
- $transaction = $this->startTransaction();
-
-
-
-
-
-
- $stmt = $this->query('UPDATE {sequences} SET value = GREATEST(value, :existing_id) + 1', array(
- ':existing_id' => $existing_id,
- ));
- if (!$stmt->rowCount()) {
- $this->query('INSERT INTO {sequences} (value) VALUES (:existing_id + 1)', array(
- ':existing_id' => $existing_id,
- ));
- }
-
-
- return $this->query('SELECT value FROM {sequences}')->fetchField();
- }
- public function rollback($savepoint_name = 'drupal_transaction') {
- if ($this->savepointSupport) {
- return parent::rollBack($savepoint_name);
- }
- if (!$this->inTransaction()) {
- throw new DatabaseTransactionNoActiveException();
- }
-
-
- if (!in_array($savepoint_name, $this->transactionLayers)) {
- return;
- }
-
-
- while ($savepoint = array_pop($this->transactionLayers)) {
- if ($savepoint == $savepoint_name) {
-
- $this->willRollback = TRUE;
-
-
-
- if (empty($this->transactionLayers)) {
- break;
- }
- return;
- }
- }
- if ($this->supportsTransactions()) {
- PDO::rollBack();
- }
- }
- public function pushTransaction($name) {
- if ($this->savepointSupport) {
- return parent::pushTransaction($name);
- }
- if (!$this->supportsTransactions()) {
- return;
- }
- if (isset($this->transactionLayers[$name])) {
- throw new DatabaseTransactionNameNonUniqueException($name . " is already in use.");
- }
- if (!$this->inTransaction()) {
- PDO::beginTransaction();
- }
- $this->transactionLayers[$name] = $name;
- }
- public function popTransaction($name) {
- if ($this->savepointSupport) {
- return parent::popTransaction($name);
- }
- if (!$this->supportsTransactions()) {
- return;
- }
- if (!$this->inTransaction()) {
- throw new DatabaseTransactionNoActiveException();
- }
-
- while($savepoint = array_pop($this->transactionLayers)) {
- if ($savepoint != $name) continue;
-
- if (empty($this->transactionLayers)) {
-
- if ($this->willRollback) {
- $this->willRollback = FALSE;
- PDO::rollBack();
- }
- elseif (!PDO::commit()) {
- throw new DatabaseTransactionCommitFailedException();
- }
- }
- else {
- break;
- }
- }
- }
- }
- class DatabaseStatement_sqlite extends DatabaseStatementPrefetch implements Iterator, DatabaseStatementInterface {
-
- protected function getStatement($query, &$args = array()) {
- if (count($args)) {
-
- if (range(0, count($args) - 1) === array_keys($args)) {
-
- $count = 0;
- $new_args = array();
- foreach ($args as $value) {
- if (is_float($value) || is_int($value)) {
- if (is_float($value)) {
-
-
- $value = sprintf('%F', $value);
- }
- $query = substr_replace($query, $value, strpos($query, '?'), 1);
- }
- else {
- $placeholder = ':db_statement_placeholder_' . $count++;
- $query = substr_replace($query, $placeholder, strpos($query, '?'), 1);
- $new_args[$placeholder] = $value;
- }
- }
- $args = $new_args;
- }
- else {
-
- foreach ($args as $placeholder => $value) {
- if (is_float($value) || is_int($value)) {
- if (is_float($value)) {
-
-
- $value = sprintf('%F', $value);
- }
-
-
-
- unset($args[$placeholder]);
-
-
-
- if ($placeholder[0] != ':') {
- $placeholder = ":$placeholder";
- }
-
-
-
- $query = preg_replace('/' . preg_quote($placeholder) . '\b/', $value, $query);
- }
- }
- }
- }
- return $this->dbh->PDOPrepare($query);
- }
- public function execute($args = array(), $options = array()) {
- try {
- $return = parent::execute($args, $options);
- }
- catch (PDOException $e) {
- if (!empty($e->errorInfo[1]) && $e->errorInfo[1] === 17) {
-
- $return = parent::execute($args, $options);
- }
- else {
-
- throw $e;
- }
- }
-
-
-
- $rename_columns = array();
- foreach ($this->columnNames as $k => $column) {
-
-
- if (preg_match("/^\((.*)\)$/", $column, $matches)) {
- $rename_columns[$column] = $matches[1];
- $this->columnNames[$k] = $matches[1];
- $column = $matches[1];
- }
-
- if (preg_match("/^.*\.(.*)$/", $column, $matches)) {
- $rename_columns[$column] = $matches[1];
- $this->columnNames[$k] = $matches[1];
- }
- }
- if ($rename_columns) {
-
-
- if (isset($this->currentRow)) {
- $this->data[0] = &$this->currentRow;
- }
-
- foreach ($this->data as $k => $row) {
- foreach ($rename_columns as $old_column => $new_column) {
- $this->data[$k][$new_column] = $this->data[$k][$old_column];
- unset($this->data[$k][$old_column]);
- }
- }
-
- $this->currentRow = $this->data[0];
- unset($this->data[0]);
- }
- return $return;
- }
- }
|