Пример #1
0
 /**
  * Connect to database.
  *
  * @since 1.0.0
  * @access public
  *
  * @param string $host Database address.
  * @param string $database Database name.
  * @param string $username Access username.
  * @param string $password Access password.
  * @return self
  *
  * @throws Freyja\Exceptions\InvalidArgumentException if one of the arguments
  * isn't a string.
  * @throws Freyja\Exceptions\RuntimeException if the connection has an error.
  */
 public function connect($host, $database, $username, $password)
 {
     foreach (array('host', 'database', 'username', 'password') as $arg) {
         if (!is_string(${$arg})) {
             throw InvalidArgumentException::typeMismatch($arg, ${$arg}, 'String');
         }
     }
     $connection = new mysqli($host, $username, $password, $database);
     // Handle connection errors.
     if ($connection->connect_error) {
         throw new RuntimeException(sprintf('Error while connecting to MySql Server (%d): %s', $connection->connect_errno, $connection->connect_error));
     }
     // Connection successful.
     $this->connection = $connection;
     return $this;
 }
Пример #2
0
 /**
  * Process WHERE clauses.
  *
  * Process `WHERE ... IN ...` and `WHERE ... NOT IN ...` clauses.
  *
  * @since 1.0.0
  * @access private
  *
  * @param string $field Field name.
  * @param array $values Values to check with.
  * @param string $method Name of the method that called this one.
  * @return self
  *
  * @throws Freyja\Exceptions\RuntimeException if $value elements aren't in the
  * correct form.
  * @throws Freyja\Exceptions\InvalidArgumentException if $field isn't a
  * string.
  */
 private function processWhereIn($field, array $values, $method = 'whereIn')
 {
     if (!is_string($field)) {
         throw InvalidArgumentException::typeMismatch('field', $field, 'String');
     }
     $where = $this->where;
     $correct = array_map(array($this, 'correctValue'), $values, array_fill(0, count($values), $method));
     $where .= sprintf('%1$s %2$s %3$sIN(%4$s)', empty($where) ? 'WHERE' : ' AND', $field, $method == 'whereNotIn' ? 'NOT ' : '', join(', ', $correct));
     $this->where = $where;
 }
Пример #3
0
 /**
  * Set default value.
  *
  * Set default value for this field. If you want to set NULL as default value
  * pass `Field::NULL` to this method.
  *
  * @since 1.0.0
  * @access public
  *
  * @param mixed $value Optional. Default value for the field.
  * Default `self::NULL`.
  * @return self
  *
  * @throws Freyja\Exceptions\LogicException if 'NULL' is passed as default
  * value, and the field is set to NOT NULL.
  * @throws Freyja\Exceptions\InvalidArgumentException if $value isn't a
  * scalar.
  */
 public function setDefault($value = self::NULL)
 {
     if ($value == self::NULL && $this->nullable == false) {
         throw new LogicException('Cannot set default value to NULL if the field is NOT NULL');
     }
     if (!is_scalar($value)) {
         throw InvalidArgumentException::typeMismatch('value', $value, 'Scalar');
     }
     if (is_string($value) && $value != self::NULL) {
         $this->default = "'" . $value . "'";
     } elseif (is_bool($value)) {
         $this->default = $value == true ? 'TRUE' : 'FALSE';
     } else {
         $this->default = $value;
     }
     return $this;
 }
Пример #4
0
 /**
  * Check table existence.
  *
  * @since 1.0.0
  * @access public
  *
  * @param string|Table $table Freyja\Database\Schema\Table object or string.
  * @return boolean Whether the table exists in the schema or not.
  *
  * @throws Freyja\Exceptions\InvalidArgumentException if $table isn't a string
  * or a Freyja\Database\Schema\Table object.
  */
 public function hasTable($table)
 {
     if (!is_string($table) && !is_a($table, 'Freyja\\Database\\Schema\\Table')) {
         throw InvalidArgumentException::typeMismatch('table', $table, 'String or Freyja\\Database\\Schema\\Table');
     }
     if (!is_string($table)) {
         $table = $table->getName();
     }
     return isset($this->schema['tables'][$table]);
 }
Пример #5
0
 /**
  * Alter table.
  *
  * @since 1.0.0
  * @access private
  *
  * @param array $fields Fields to be altered.
  * @param string $type Alter type. Value can be 'add' or 'drop'.
  *
  * @return self
  *
  * @throws Freyja\Exceptions\InvalidArgumentException if some element in
  * $fields isn't a Freyja\Database\Schema\Field object.
  * @throws Freyja\Exceptions\LogicException if $fields is an empty array.
  */
 private function alter(array $fields, $type)
 {
     if (empty($fields)) {
         throw new LogicException('It\'s required at least one column to alter the table');
     }
     foreach ($fields as $field) {
         if (!is_a($field, 'Freyja\\Database\\Schema\\Field')) {
             throw InvalidArgumentException::typeMismatch('field', $field, 'Freyja\\Database\\Schema\\Field');
         }
     }
     $this->type = 'alter';
     $this->alter_fields[$type] = array_merge($this->alter_fields[$type], $fields);
     return $this;
 }