1: <?php
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: class Horde_Db_Adapter_Pdo_Sqlite extends Horde_Db_Adapter_Pdo_Base
27: {
28: 29: 30:
31: protected $_schemaClass = 'Horde_Db_Adapter_Sqlite_Schema';
32:
33: 34: 35: 36:
37: protected $_sqliteVersion;
38:
39: 40: 41:
42: public function adapterName()
43: {
44: return 'PDO_SQLite';
45: }
46:
47: 48: 49:
50: public function supportsMigrations()
51: {
52: return true;
53: }
54:
55: 56: 57: 58: 59: 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: 74:
75:
76: 77: 78: 79: 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: 112:
113:
114: 115: 116: 117: 118: 119: 120:
121: public function execute($sql, $arg1=null, $arg2=null)
122: {
123: return $this->_catchSchemaChanges('execute', array($sql, $arg1, $arg2));
124: }
125:
126: 127: 128:
129: public function beginDbTransaction()
130: {
131: return $this->_catchSchemaChanges('beginDbTransaction');
132: }
133:
134: 135: 136:
137: public function commitDbTransaction()
138: {
139: return $this->_catchSchemaChanges('commitDbTransaction');
140: }
141:
142: 143: 144: 145:
146: public function rollbackDbTransaction()
147: {
148: return $this->_catchSchemaChanges('rollbackDbTransaction');
149: }
150:
151: 152: 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: 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: 189: 190: 191: 192:
193: protected function _parseConfig()
194: {
195:
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:
202: $dsnOpts = $this->_config;
203: unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
204:
205:
206: return array($this->_buildDsnString($this->_normalizeConfig($dsnOpts)), '', '');
207: }
208:
209: }
210: