Ejemplo n.º 1
0
 /**
  * Special handling for PDO query().
  * All bind parameter names must begin with ':'
  *
  * @param string|\Micro\Database\Select $sql The SQL statement with placeholders.
  * @param array $bind An array of data to bind to the placeholders.
  * @return \Micro\Database\Statement\Pdo
  * @throws \Micro\Database\Statement\Exception To re-throw \PDOException.
  */
 public function query($sql, $bind = array())
 {
     if (empty($bind) && $sql instanceof Select) {
         $bind = $sql->getBind();
     }
     if (is_array($bind)) {
         foreach ($bind as $name => $value) {
             if (!is_int($name) && !preg_match('/^:/', $name)) {
                 $newName = ":{$name}";
                 unset($bind[$name]);
                 $bind[$newName] = $value;
             }
         }
     }
     try {
         return parent::query($sql, $bind);
     } catch (\PDOException $e) {
         throw new StatementException($e->getMessage(), $e->getCode(), $e);
     }
 }
Ejemplo n.º 2
0
 public function applyWhereConditions(Select $select)
 {
     foreach ($this->where as $field => $condition) {
         if ($condition instanceof Expr) {
             $select->where($condition);
             continue;
         }
         if (is_array($condition)) {
             $select->where($field . ' IN (?)', $condition);
         } elseif (is_string($condition && preg_match('/(^%|%$)/i', $condition))) {
             $select->where($field . ' LIKE ?', $condition);
         } else {
             $select->where($field . ' = ?', $condition);
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Returns an array of items for a page.
  *
  * @param  integer $offset Page offset
  * @param  integer $itemCountPerPage Number of items per page
  * @return array
  */
 public function getItems($offset = \null, $itemCountPerPage = \null)
 {
     $this->_select->limit($itemCountPerPage, $offset);
     return $this->_select->query()->fetchAll();
 }
Ejemplo n.º 4
0
 /**
  * Performs a validation on the select query before passing back to the parent class.
  * Ensures that only columns from the primary \Micro\Database\Table\TableAbstract are returned in the result.
  *
  * @return string|null This object as a SELECT string (or null if a string cannot be produced)
  */
 public function assemble()
 {
     $fields = $this->getPart(Select::COLUMNS);
     $primary = $this->_info[TableAbstract::NAME];
     $schema = $this->_info[TableAbstract::SCHEMA];
     if (count($this->_parts[self::UNION]) == 0) {
         // If no fields are specified we assume all fields from primary table
         if (!count($fields)) {
             $this->from($primary, self::SQL_WILDCARD, $schema);
             $fields = $this->getPart(Select::COLUMNS);
         }
         $from = $this->getPart(Select::FROM);
         if ($this->_integrityCheck !== false) {
             foreach ($fields as $columnEntry) {
                 list($table, $column) = $columnEntry;
                 // Check each column to ensure it only references the primary table
                 if ($column) {
                     if (!isset($from[$table]) || $from[$table]['tableName'] != $primary) {
                         throw new Exception('Select query cannot join with another table');
                     }
                 }
             }
         }
     }
     return parent::assemble();
 }