Overview

Packages

  • Db
    • Adapter
    • Migration

Classes

  • Horde_Db_Adapter_Base
  • Horde_Db_Adapter_Base_Column
  • Horde_Db_Adapter_Base_ColumnDefinition
  • Horde_Db_Adapter_Base_Index
  • Horde_Db_Adapter_Base_Schema
  • Horde_Db_Adapter_Base_Table
  • Horde_Db_Adapter_Base_TableDefinition
  • Horde_Db_Adapter_Mysql
  • Horde_Db_Adapter_Mysql_Column
  • Horde_Db_Adapter_Mysql_Result
  • Horde_Db_Adapter_Mysql_Schema
  • Horde_Db_Adapter_Mysqli
  • Horde_Db_Adapter_Mysqli_Result
  • Horde_Db_Adapter_Pdo_Base
  • Horde_Db_Adapter_Pdo_Mysql
  • Horde_Db_Adapter_Pdo_Pgsql
  • Horde_Db_Adapter_Pdo_Sqlite
  • Horde_Db_Adapter_Postgresql_Column
  • Horde_Db_Adapter_Postgresql_Schema
  • Horde_Db_Adapter_SplitRead
  • Horde_Db_Adapter_Sqlite_Column
  • Horde_Db_Adapter_Sqlite_Schema

Interfaces

  • Horde_Db_Adapter
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007 Maintainable Software, LLC
  4:  * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  5:  *
  6:  * @author     Mike Naberezny <mike@maintainable.com>
  7:  * @author     Derek DeVries <derek@maintainable.com>
  8:  * @author     Chuck Hagenbuch <chuck@horde.org>
  9:  * @license    http://www.horde.org/licenses/bsd
 10:  * @category   Horde
 11:  * @package    Db
 12:  * @subpackage Adapter
 13:  */
 14: 
 15: /**
 16:  * PDO_SQLite Horde_Db_Adapter
 17:  *
 18:  * @author     Mike Naberezny <mike@maintainable.com>
 19:  * @author     Derek DeVries <derek@maintainable.com>
 20:  * @author     Chuck Hagenbuch <chuck@horde.org>
 21:  * @license    http://www.horde.org/licenses/bsd
 22:  * @category   Horde
 23:  * @package    Db
 24:  * @subpackage Adapter
 25:  */
 26: class Horde_Db_Adapter_Pdo_Sqlite extends Horde_Db_Adapter_Pdo_Base
 27: {
 28:     /**
 29:      * @var string
 30:      */
 31:     protected $_schemaClass = 'Horde_Db_Adapter_Sqlite_Schema';
 32: 
 33:     /**
 34:      * SQLite version number
 35:      * @var integer
 36:      */
 37:     protected $_sqliteVersion;
 38: 
 39:     /**
 40:      * @return  string
 41:      */
 42:     public function adapterName()
 43:     {
 44:         return 'PDO_SQLite';
 45:     }
 46: 
 47:     /**
 48:      * @return  boolean
 49:      */
 50:     public function supportsMigrations()
 51:     {
 52:         return true;
 53:     }
 54: 
 55:     /**
 56:      * Does this adapter support using DISTINCT within COUNT?  This is +true+
 57:      * for all adapters except sqlite.
 58:      *
 59:      * @return  boolean
 60:      */
 61:     public function supportsCountDistinct()
 62:     {
 63:         return $this->_sqliteVersion >= '3.2.6';
 64:     }
 65: 
 66:     public function supportsAutoIncrement()
 67:     {
 68:         return $this->_sqliteVersion >= '3.1.0';
 69:     }
 70: 
 71: 
 72:     /*##########################################################################
 73:     # Connection Management
 74:     ##########################################################################*/
 75: 
 76:     /**
 77:      * Connect to the db.
 78:      *
 79:      * @throws Horde_Db_Exception
 80:      */
 81:     public function connect()
 82:     {
 83:         if ($this->_active) {
 84:             return;
 85:         }
 86: 
 87:         parent::connect();
 88: 
 89:         $this->_connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
 90: 
 91:         $this->_lastQuery = $sql = 'PRAGMA full_column_names=0';
 92:         $retval = $this->_connection->exec($sql);
 93:         if ($retval === false) {
 94:             $error = $this->_connection->errorInfo();
 95:             throw new Horde_Db_Exception($error[2]);
 96:         }
 97: 
 98:         $this->_lastQuery = $sql = 'PRAGMA short_column_names=1';
 99:         $retval = $this->_connection->exec($sql);
100:         if ($retval === false) {
101:             $error = $this->_connection->errorInfo();
102:             throw new Horde_Db_Exception($error[2]);
103:         }
104: 
105:         $this->_lastQuery = $sql = 'SELECT sqlite_version(*)';
106:         $this->_sqliteVersion = $this->selectValue($sql);
107:     }
108: 
109: 
110:     /*##########################################################################
111:     # Database Statements
112:     ##########################################################################*/
113: 
114:     /**
115:      * Executes the SQL statement in the context of this connection.
116:      *
117:      * @param   string  $sql
118:      * @param   mixed   $arg1  Either an array of bound parameters or a query name.
119:      * @param   string  $arg2  If $arg1 contains bound parameters, the query name.
120:      */
121:     public function execute($sql, $arg1=null, $arg2=null)
122:     {
123:         return $this->_catchSchemaChanges('execute', array($sql, $arg1, $arg2));
124:     }
125: 
126:     /**
127:      * Begins the transaction (and turns off auto-committing).
128:      */
129:     public function beginDbTransaction()
130:     {
131:         return $this->_catchSchemaChanges('beginDbTransaction');
132:     }
133: 
134:     /**
135:      * Commits the transaction (and turns on auto-committing).
136:      */
137:     public function commitDbTransaction()
138:     {
139:         return $this->_catchSchemaChanges('commitDbTransaction');
140:     }
141: 
142:     /**
143:      * Rolls back the transaction (and turns on auto-committing). Must be
144:      * done if the transaction block raises an exception or returns false.
145:      */
146:     public function rollbackDbTransaction()
147:     {
148:         return $this->_catchSchemaChanges('rollbackDbTransaction');
149:     }
150: 
151:     /**
152:      * SELECT ... FOR UPDATE is redundant since the table is locked.
153:      */
154:     public function addLock(&$sql, array $options = array())
155:     {
156:     }
157: 
158:     public function emptyInsertStatement($tableName)
159:     {
160:         return 'INSERT INTO '.$this->quoteTableName($tableName).' VALUES(NULL)';
161:     }
162: 
163: 
164:     /*##########################################################################
165:     # Protected
166:     ##########################################################################*/
167: 
168:     protected function _catchSchemaChanges($method, $args = array())
169:     {
170:         try {
171:             return call_user_func_array(array($this, "parent::$method"), $args);
172:         } catch (Exception $e) {
173:             if (preg_match('/database schema has changed/i', $e->getMessage())) {
174:                 $this->reconnect();
175:                 return call_user_func_array(array($this, "parent::$method"), $args);
176:             } else {
177:                 throw $e;
178:             }
179:         }
180:     }
181: 
182:     protected function _buildDsnString($params)
183:     {
184:         return 'sqlite:' . $params['dbname'];
185:     }
186: 
187:     /**
188:      * Parse configuration array into options for PDO constructor
189:      *
190:      * @throws  Horde_Db_Exception
191:      * @return  array  [dsn, username, password]
192:      */
193:     protected function _parseConfig()
194:     {
195:         // check required config keys are present
196:         if (empty($this->_config['database']) && empty($this->_config['dbname'])) {
197:             $msg = 'Either dbname or database is required';
198:             throw new Horde_Db_Exception($msg);
199:         }
200: 
201:         // collect options to build PDO Data Source Name (DSN) string
202:         $dsnOpts = $this->_config;
203:         unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
204: 
205:         // return DSN and dummy user/pass for connection
206:         return array($this->_buildDsnString($this->_normalizeConfig($dsnOpts)), '', '');
207:     }
208: 
209: }
210: 
API documentation generated by ApiGen