Exemple #1
0
 /**
  * @param $str
  * @param array $parameters
  * @return string
  */
 public static function bind($str, $parameters)
 {
     if (!is_array($parameters)) {
         $parameters = (array) $parameters;
     }
     foreach ($parameters as &$param) {
         if (is_array($param)) {
             $param = implode(', ', self::quote($param));
         } else {
             $param = self::quote($param);
         }
     }
     if (($questionMarkCount = substr_count($str, '?')) > ($paramCount = count($parameters))) {
         $last = end($parameters);
         $parameters = array_merge($parameters, array_fill(0, $questionMarkCount - $paramCount, $last));
     }
     $str = str_replace('?', '%s', $str);
     if ($questionMarkCount == 1 && !is_array(current($parameters))) {
         $parameters = implode(', ', $parameters);
     }
     static::$lastQuery = vsprintf($str, $parameters);
     return static::$lastQuery;
 }
 /**
  * Callback for the DatabaseDriver's setQuery method.
  *
  * @param   string  $query  The query.
  *
  * @return  void
  *
  * @since   1.0
  */
 public static function mockSetQuery($query)
 {
     static::$lastQuery = $query;
     return static::$dbo;
 }
Exemple #3
0
 /**
  * Perform a SQL Insert
  *
  * Creates and performs a SQL insert for this model instance. For an insert all
  * columns are listed with their corresponding values.
  *
  * @throws    \Nerd\DB\Exception  If the query cannot be executed
  *
  * @return    array     Common SQL result array
  */
 public function insert($replace = false)
 {
     $model = $this;
     $params = [];
     self::$columns->each(function ($column) use(&$model, &$params) {
         $params[':' . $column->field] = isset($model->_values[$column->field]) ? $model->_values[$column->field] : null;
     });
     $sql = 'INSERT INTO ' . '`' . static::$table . '` ' . '(' . static::listColumns() . ') ' . 'VALUES (' . join(', ', array_keys($params)) . ');';
     if ($replace) {
         $sql = str_replace('INSERT INTO', 'REPLACE INTO', $sql);
     }
     static::$lastQuery = $sql;
     try {
         $statement = static::connection()->prepare($sql);
         $success = $statement->execute($params);
     } catch (\PDOException $e) {
         throw new \Nerd\DB\Exception($e);
     }
     return [$success, $statement->rowCount(), $statement];
 }
Exemple #4
0
 /**
  * Query influxDB
  *
  * @param  string $database
  * @param  string $query
  * @param  array  $parameters
  *
  * @return ResultSet
  * @throws Exception
  */
 public function query($database, $query, $parameters = [])
 {
     if (!$this->driver instanceof QueryDriverInterface) {
         throw new Exception('The currently configured driver does not support query operations');
     }
     if ($database) {
         $parameters['db'] = $database;
     }
     $driver = $this->getDriver();
     $parameters = ['url' => 'query?' . http_build_query(array_merge(['q' => $query], $parameters)), 'database' => $database, 'method' => 'get'];
     // add authentication to the driver if needed
     if (!empty($this->username) && !empty($this->password)) {
         $parameters += ['auth' => [$this->username, $this->password]];
     }
     $driver->setParameters($parameters);
     try {
         // store the last query sent
         static::$lastQuery = $query;
         // perform the query and return the resultset
         return $driver->query();
     } catch (DriverException $e) {
         throw new Exception('Query has failed', $e->getCode(), $e);
     }
 }