When writing an adapter, you need to override these abstract methods:
{{code: php
abstract protected function _fetchTableList();
abstract protected function _fetchTableCols($table);
abstract protected function _createSequence($name, $start = 1);
abstract protected function _dropSequence($name);
abstract protected function _nextSequence($name);
abstract protected function _dropIndex($table, $name);
abstract protected function _modAutoincPrimary(&$coldef, $autoinc, $primary);
}}
If the backend needs identifier deconfliction (e.g., PostgreSQL), you will
want to override _modIndexName() and _modSequenceName(). Most times this
will not be necessary.
If the backend does not have explicit "LIMIT ... OFFSET" support,
you will want to override _modSelect($stmt, $parts) to rewrite the query
in order to emulate limit/select behavior. This is particularly necessary
for Microsoft SQL and Oracle.
/** * * Updates an existing session-data row in the database. * * @param string $id The session ID. * * @param string $data The serialized session data. * * @return bool * * @todo Should we log caught exceptions? * */ protected function _update($id, $data) { $cols = array($this->_config['updated_col'] => date('Y-m-d H:i:s'), $this->_config['data_col'] => $data); $where = array("{$this->_config['id_col']} = ?" => $id); try { $this->_sql->update($this->_config['table'], $cols, $where); return true; } catch (Solar_Sql_Exception $e) { // @todo log this somehow? return false; } }
/** * * Creates the table and indexes in the database using $this->_table_cols * and $this->_index. * * @return void * */ protected function _createTableAndIndexes() { /** * Create the table. */ $this->_sql->createTable($this->_table_name, $this->_table_cols); /** * Create the indexes. */ foreach ($this->_index as $name => $info) { try { // create this index $this->_sql->createIndex($this->_table_name, $info['name'], $info['type'] == 'unique', $info['cols']); } catch (Exception $e) { // cancel the whole deal. $this->_sql->dropTable($this->_table_name); throw $e; } } }
/** * * Extend base adapter function to add stringify to the calls. * * After connection, set various connection attributes. * * @return void * */ protected function _postConnect() { parent::_postConnect(); $alter = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'"; $this->query($alter); $this->_pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); }
/** * * Builds a CREATE TABLE command string. * * @param string $name The table name to create. * * @param string $cols The column definitions. * * @return string A CREATE TABLE command string. * */ protected function _sqlCreateTable($name, $cols) { $stmt = parent::_sqlCreateTable($name, $cols); $stmt .= " TYPE=InnoDB"; // for transactions $stmt .= " DEFAULT CHARSET=utf8 COLLATE=utf8_bin"; // for UTF8 return $stmt; }
/** * * Safely quotes a value for an SQL statement; unlike the main adapter, * the SQLite adapter **does not** quote numeric values. * * If an array is passed as the value, the array values are quoted * and then returned as a comma-separated string; this is useful * for generating IN() lists. * * {{code: php * $sql = Solar::factory('Solar_Sql'); * * $safe = $sql->quote('foo"bar"'); * // $safe == "'foo\"bar\"'" * * $safe = $sql->quote(array('one', 'two', 'three')); * // $safe == "'one', 'two', 'three'" * }} * * @param mixed $val The value to quote. * * @return string An SQL-safe quoted value (or a string of * separated-and-quoted values). * */ public function quote($val) { if (is_numeric($val)) { return $val; } else { return parent::quote($val); } }