Exemplo n.º 1
0
 /**
  * @param CM_Db_Client      $client
  * @param string            $table
  * @param string|array|null $where Associative array field=>value OR string
  */
 public function __construct(CM_Db_Client $client, $table, $where = null)
 {
     parent::__construct($client);
     $this->_addSql('SELECT COUNT(*)');
     $this->_addSql('FROM ' . $this->_getClient()->quoteIdentifier($table));
     $this->_addWhere($where);
 }
Exemplo n.º 2
0
 /**
  * @param CM_Db_Client $client
  * @param string       $table
  * @param string|null  $column
  */
 public function __construct(CM_Db_Client $client, $table, $column = null)
 {
     parent::__construct($client);
     $this->_addSql('DESCRIBE ' . $this->_getClient()->quoteIdentifier($table));
     if (null !== $column) {
         $this->_addSql($this->_getClient()->quoteIdentifier($column));
     }
 }
Exemplo n.º 3
0
 /**
  * @param CM_Db_Client $client
  * @param string       $table
  * @param string       $field
  * @param int          $direction
  * @param array|null   $where Associative array field=>value
  * @param int          $lowerBound
  * @param int          $upperBound
  */
 public function __construct($client, $table, $field, $direction, $where, $lowerBound, $upperBound)
 {
     parent::__construct($client);
     $this->_addSql('UPDATE ' . $this->_getClient()->quoteIdentifier($table));
     $this->_addSql('SET ' . $this->_getClient()->quoteIdentifier($field) . ' = ' . $this->_getClient()->quoteIdentifier($field) . ' + ?');
     $this->_addParameters($direction);
     $this->_addWhere($where);
     $combinationStatement = empty($where) ? 'WHERE' : 'AND';
     $this->_addSql($combinationStatement . ' ' . $this->_getClient()->quoteIdentifier($field) . ' BETWEEN ? AND ?');
     $this->_addParameters(array($lowerBound, $upperBound));
 }
Exemplo n.º 4
0
 /**
  * @param CM_Db_Client      $client
  * @param string            $table
  * @param string|array      $fields Column-name OR Column-names array OR associative field=>value pair
  * @param string|array|null $values Column-value OR Column-values array OR Multiple Column-values array(array)
  * @param array|null        $onDuplicateKeyValues
  * @param string            $statement
  * @throws CM_Exception
  */
 public function __construct(CM_Db_Client $client, $table, $fields, $values = null, array $onDuplicateKeyValues = null, $statement = 'INSERT')
 {
     parent::__construct($client);
     if ($values === null && is_array($fields)) {
         $values = array_values($fields);
         $fields = array_keys($fields);
     }
     $valueList = (array) $values;
     $fieldList = (array) $fields;
     if (!is_array(reset($valueList))) {
         if (count($fieldList) == 1) {
             foreach ($valueList as &$values) {
                 $values = array($values);
             }
         } else {
             $valueList = array($valueList);
         }
     }
     foreach ($fieldList as &$fields) {
         $fields = $this->_getClient()->quoteIdentifier($fields);
     }
     $rowsList = array();
     foreach ($valueList as &$values) {
         $row = (array) $values;
         if (count($row) != count($fieldList)) {
             throw new CM_Exception('Row size does not match number of fields');
         }
         $rowList = array();
         foreach ($row as $rowValue) {
             if ($rowValue === null) {
                 $rowList[] = 'NULL';
             } else {
                 $rowList[] = '?';
                 $this->_addParameters($rowValue);
             }
         }
         $rowsList[] = '(' . implode(',', $rowList) . ')';
     }
     $this->_addSql($statement . ' INTO ' . $this->_getClient()->quoteIdentifier($table));
     $this->_addSql('(' . implode(',', $fieldList) . ')');
     $this->_addSql('VALUES ' . implode(',', $rowsList));
     if ($onDuplicateKeyValues) {
         $valuesList = array();
         foreach ($onDuplicateKeyValues as $fields => $values) {
             if ($values === null) {
                 $valuesList[] = $this->_getClient()->quoteIdentifier($fields) . ' = NULL';
             } else {
                 $valuesList[] = $this->_getClient()->quoteIdentifier($fields) . ' = ?';
                 $this->_addParameters($values);
             }
         }
         $this->_addSql('ON DUPLICATE KEY UPDATE ' . implode(',', $valuesList));
     }
 }
Exemplo n.º 5
0
 /**
  * @param CM_Db_Client      $client
  * @param string            $table
  * @param string|array      $values Associative array field=>value
  * @param string|array|null $where  Associative array field=>value OR string
  * @param string            $statement
  */
 public function __construct(CM_Db_Client $client, $table, array $values, $where = null, $statement = 'UPDATE')
 {
     parent::__construct($client);
     $sqlParts = array();
     foreach ($values as $field => $value) {
         if (null === $value) {
             $sqlParts[] = $this->_getClient()->quoteIdentifier($field) . ' = NULL';
         } else {
             $sqlParts[] = $this->_getClient()->quoteIdentifier($field) . ' = ?';
             $this->_addParameters($value);
         }
     }
     $this->_addSql($statement . ' ' . $this->_getClient()->quoteIdentifier($table));
     $this->_addSql('SET ' . implode(', ', $sqlParts));
     $this->_addWhere($where);
 }
Exemplo n.º 6
0
 /**
  * @param CM_Db_Client      $client
  * @param string            $table
  * @param string|array      $fields Column-name OR Column-names array
  * @param string|array|null $where  Associative array field=>value OR string
  * @param string|array|null $order
  */
 public function __construct(CM_Db_Client $client, $table, $fields, $where = null, $order = null)
 {
     parent::__construct($client);
     $fields = (array) $fields;
     foreach ($fields as &$field) {
         if ($field == '*') {
             $field = '*';
         } else {
             $field = $this->_getClient()->quoteIdentifier($field);
         }
     }
     $this->_addSql('SELECT ' . implode(',', $fields));
     $this->_addSql('FROM ' . $this->_getClient()->quoteIdentifier($table));
     $this->_addWhere($where);
     $this->_addOrderBy($order);
 }
Exemplo n.º 7
0
 public function __construct(CM_Db_Client $client, $orderBy)
 {
     parent::__construct($client);
     $this->_addOrderBy($orderBy);
 }
Exemplo n.º 8
0
 /**
  * @param CM_Db_Client      $client
  * @param string            $table
  * @param string|array      $fields    Column-name OR Column-names array
  * @param array[]           $whereList Outer array-entries are combined using OR, inner arrays using AND
  * @param string|array|null $order
  */
 public function __construct(CM_Db_Client $client, $table, $fields, array $whereList, $order = null)
 {
     parent::__construct($client);
     $fields = (array) $fields;
     foreach ($fields as &$field) {
         if ($field == '*') {
             $field = '*';
         } else {
             $field = $this->_getClient()->quoteIdentifier($field);
         }
     }
     $this->_addSql('SELECT ' . implode(',', $fields));
     $this->_addSql('FROM ' . $this->_getClient()->quoteIdentifier($table));
     if (!empty($whereList)) {
         $wherePartCommon = array();
         if (count($whereList) >= 2) {
             $wherePartCommon = reset($whereList);
             foreach ($whereList as $wherePart) {
                 $wherePartCommon = array_uintersect_assoc($wherePartCommon, $wherePart, function ($a, $b) {
                     return $a !== $b;
                 });
             }
         }
         $whereParts = array();
         foreach ($whereList as $wherePart) {
             $sqlParts = array();
             $wherePartFiltered = array_udiff_assoc($wherePart, $wherePartCommon, function ($a, $b) {
                 return $a !== $b;
             });
             if (!empty($wherePartFiltered)) {
                 foreach ($wherePartFiltered as $field => $value) {
                     if (null === $value) {
                         $sqlParts[] = $this->_getClient()->quoteIdentifier($field) . ' IS NULL';
                     } else {
                         $sqlParts[] = $this->_getClient()->quoteIdentifier($field) . ' = ?';
                         $this->_addParameters($value);
                     }
                 }
                 $whereParts[] = implode(' AND ', $sqlParts);
             }
         }
         if (empty($wherePartCommon)) {
             $this->_addSql('WHERE ' . implode(' OR ', $whereParts));
         } else {
             if (!empty($whereParts)) {
                 $this->_addSql('WHERE (' . implode(' OR ', $whereParts) . ') AND');
             } else {
                 $this->_addSql('WHERE');
             }
             $sqlParts = array();
             foreach ($wherePartCommon as $field => $value) {
                 if (null === $value) {
                     $sqlParts[] = $this->_getClient()->quoteIdentifier($field) . ' IS NULL';
                 } else {
                     $sqlParts[] = $this->_getClient()->quoteIdentifier($field) . ' = ?';
                     $this->_addParameters($value);
                 }
             }
             $this->_addSql(implode(' AND ', $sqlParts));
         }
     }
     $this->_addOrderBy($order);
 }
Exemplo n.º 9
0
 /**
  * @param CM_Db_Client $client
  * @param string       $table
  */
 public function __construct(CM_Db_Client $client, $table)
 {
     parent::__construct($client);
     $this->_addSql('TRUNCATE TABLE ' . $this->_getClient()->quoteIdentifier($table));
 }