/**
  * Special handling for PDO query().
  * All bind parameter names must begin with ':'
  *
  * @param string|\libDb\Select $sql The SQL statement with placeholders.
  * @param array $bind An array of data to bind to the placeholders.
  * @return Pdo
  * @throws \libDb\Statement\Exception To re-throw PDOException.
  */
 public function query($sql, $bind = array())
 {
     if (empty($bind) && $sql instanceof \libDb\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 \libDb\Statement\Exception($e->getMessage(), $e->getCode(), $e);
     }
 }
Exemple #2
0
 /**
  * Performs a validation on the select query before passing back to the parent class.
  * Ensures that only columns from the primary \libDb\Table 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(\libDb\Table\Select::COLUMNS);
     $primary = $this->_info[AbstractTable::NAME];
     $schema = $this->_info[AbstractTable::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(\libDb\Table\Select::COLUMNS);
         }
         $from = $this->getPart(\libDb\Table\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 \libDb\Table\Select\Exception('Select query cannot join with another table');
                     }
                 }
             }
         }
     }
     return parent::assemble();
 }
 /**
  * Generate ORDER clause from user-supplied string or array
  *
  * @param  string|array $order  OPTIONAL An SQL ORDER clause.
  * @return Select
  */
 protected function _order(Select $select, $order)
 {
     if (!is_array($order)) {
         $order = array($order);
     }
     foreach ($order as $val) {
         $select->order($val);
     }
     return $select;
 }