Author: Mike Naberezny (mike@maintainable.com)
Author: Derek DeVries (derek@maintainable.com)
Author: Chuck Hagenbuch (chuck@horde.org)
Author: Jan Schneider (jan@horde.org)
Exemple #1
0
 /**
  * Returns an expression using the specified operator.
  *
  * @param string $lhs    The column or expression to test.
  * @param string $op     The operator.
  * @param string $rhs    The comparison value.
  * @param boolean $bind  If true, the method returns the query and a list
  *                       of values suitable for binding as an array.
  * @param array $params  Any additional parameters for the operator.
  *
  * @return string|array  The SQL test fragment, or an array containing the
  *                       query and a list of values if $bind is true.
  */
 public function buildClause($lhs, $op, $rhs, $bind = false, $params = array())
 {
     $lhs = $this->_escapePrepare($lhs);
     switch ($op) {
         case '|':
         case '&':
             /* Only PgSQL 7.3+ understands SQL99 'SIMILAR TO'; use ~ for
              * greater backwards compatibility. */
             $query = 'CASE WHEN CAST(%s AS VARCHAR) ~ \'^-?[0-9]+$\' THEN (CAST(%s AS INTEGER) %s %s) ELSE 0 END';
             if ($bind) {
                 return array(sprintf($query, $lhs, $lhs, $op, '?'), array((int) $rhs));
             } else {
                 return sprintf($query, $lhs, $lhs, $op, (int) $rhs);
             }
         case 'LIKE':
             $query = '%s ILIKE %s';
             if ($bind) {
                 if (empty($params['begin'])) {
                     return array(sprintf($query, $lhs, '?'), array('%' . $rhs . '%'));
                 }
                 return array(sprintf('(' . $query . ' OR ' . $query . ')', $lhs, '?', $lhs, '?'), array($rhs . '%', '% ' . $rhs . '%'));
             }
             if (empty($params['begin'])) {
                 return sprintf($query, $lhs, $this->_escapePrepare($this->quote('%' . $rhs . '%')));
             }
             return sprintf('(' . $query . ' OR ' . $query . ')', $lhs, $this->_escapePrepare($this->quote($rhs . '%')), $lhs, $this->_escapePrepare($this->quote('% ' . $rhs . '%')));
     }
     return parent::buildClause($lhs, $op, $rhs, $bind, $params);
 }
Exemple #2
0
 /**
  * Clears the cache for tables when altering them.
  *
  * @param string $tableName  A table name.
  */
 protected function _clearTableCache($tableName)
 {
     parent::_clearTableCache($tableName);
     $this->cacheWrite('tables/primarykeys/' . $tableName, '');
 }
Exemple #3
0
 /**
  * Unserialize callback.
  */
 public function __wakeup()
 {
     $this->_schema->setAdapter($this);
     $this->connect();
 }
Exemple #4
0
 /**
  * Adds a new column to a table.
  *
  * @param string $tableName   A table name.
  * @param string $columnName  A column name.
  * @param string $type        A data type.
  * @param array $options      Column options. See
  *                            Horde_Db_Adapter_Base_TableDefinition#column()
  *                            for details.
  */
 public function addColumn($tableName, $columnName, $type, $options = array())
 {
     if ($this->transactionStarted()) {
         throw new Horde_Db_Exception('Cannot add columns to a SQLite database while inside a transaction');
     }
     if ($type == 'autoincrementKey') {
         $this->_alterTable($tableName, array(), create_function('$definition', sprintf('$definition->column("%s", "%s", %s);', $columnName, $type, var_export($options, true))));
     } else {
         parent::addColumn($tableName, $columnName, $type, $options);
     }
     // See last paragraph on http://www.sqlite.org/lang_altertable.html
     $this->execute('VACUUM');
 }
Exemple #5
0
 /**
  * Returns an expression using the specified operator.
  *
  * @param string $lhs    The column or expression to test.
  * @param string $op     The operator.
  * @param string $rhs    The comparison value.
  * @param boolean $bind  If true, the method returns the query and a list
  *                       of values suitable for binding as an array.
  * @param array $params  Any additional parameters for the operator.
  *
  * @return string|array  The SQL test fragment, or an array containing the
  *                       query and a list of values if $bind is true.
  */
 public function buildClause($lhs, $op, $rhs, $bind = false, $params = array())
 {
     switch ($op) {
         case '~':
             if ($bind) {
                 return array($lhs . ' REGEXP ?', array($rhs));
             } else {
                 return $lhs . ' REGEXP ' . $rhs;
             }
     }
     return parent::buildClause($lhs, $op, $rhs, $bind, $params);
 }
Exemple #6
0
 /**
  * Adds a new column to a table.
  *
  * @param string $tableName   A table name.
  * @param string $columnName  A column name.
  * @param string $type        A data type.
  * @param array $options      Column options. See
  *                            Horde_Db_Adapter_Base_TableDefinition#column()
  *                            for details.
  */
 public function addColumn($tableName, $columnName, $type, $options = array())
 {
     /* Ignore ':autoincrement' - it is handled automatically by SQLite
      * for any 'INTEGER PRIMARY KEY' column. */
     if ($this->transactionStarted()) {
         throw new Horde_Db_Exception('Cannot add columns to a SQLite database while inside a transaction');
     }
     parent::addColumn($tableName, $columnName, $type, $options);
     // See last paragraph on http://www.sqlite.org/lang_altertable.html
     $this->execute('VACUUM');
 }