method query documentation in bbn\Db\Languages\Sql

Executes a writing statement and return the number of affected rows or return a query object for the reading * statement

function($statement) { if (!$this->pdo) { return null; } $args = \func_get_args(); // If fancy is false we just use the regular PDO query function if (!$this->_fancy) { return $this->pdo->query(...$args); } // The function can be called directly with func_get_args() while ((\count($args) === 1) && \is_array($args[0])){ $args = $args[0]; } if (!empty($args[0]) && \is_string($args[0])) { // The first argument is the statement $statement = trim(array_shift($args)); // Sending a hash as second argument from helper functions will bind it to the saved statement if (count($args) && \is_string($args[0]) && isset($this->queries[$args[0]]) ) { $hash = is_string($this->queries[$args[0]]) ? $this->queries[$args[0]] : $args[0]; $hash_sent = array_shift($args); } else { $hash = $this->makeHash($statement); } $driver_options = []; if (count($args) && \is_array($args[0]) ) { // Case where drivers are arguments if (!array_key_exists(0, $args[0])) { $driver_options = array_shift($args); } // Case where values are in a single argument elseif (\count($args) === 1) { $args = $args[0]; } } /** @var array $params Will become the property last_params each time a query is executed */ $params = [ 'statement' => $statement, 'values' => [], 'last' => microtime(true) ]; $num_values = 0; foreach ($args as $i => $arg){ if (!\is_array($arg)) { $params['values'][] = $arg; $num_values++; } elseif (isset($arg[2])) { $params['values'][] = $arg[2]; $num_values++; } } if (!isset($this->queries[$hash])) { /** @var int $placeholders The number of placeholders in the statement */ $placeholders = 0; if ($sequences = $this->parseQuery($statement)) { /* Or looking for question marks */ $sequences = array_keys($sequences); preg_match_all('/(\?)/', $statement, $exp); $placeholders = isset($exp[1]) && \is_array($exp[1]) ? \count($exp[1]) : 0; while ($sequences[0] === 'OPTIONS'){ array_shift($sequences); } $params['kind'] = $sequences[0]; $params['union'] = isset($sequences['UNION']); $params['write'] = \in_array($params['kind'], self::$write_kinds, true); $params['structure'] = \in_array($params['kind'], self::$structure_kinds, true); } elseif (($this->getEngine() === 'sqlite') && (strpos($statement, 'PRAGMA') === 0)) { $params['kind'] = 'PRAGMA'; } else { throw new \Exception( \defined('BBN_IS_DEV') && BBN_IS_DEV ? "Impossible to parse the query $statement" : 'Impossible to parse the query' ); } // This will add to the queries array $this->_add_query( $hash, $statement, $params['kind'], $placeholders, $driver_options ); if (!empty($hash_sent)) { $this->queries[$hash_sent] = $hash; } } // The hash of the hash for retrieving a query based on the helper's config's hash elseif (\is_string($this->queries[$hash])) { $hash = $this->queries[$hash]; } $this->_update_query($hash); $q =& $this->queries[$hash]; /* If the number of values is inferior to the number of placeholders we fill the values with the last given value */ if (!empty($params['values']) && ($num_values < $q['placeholders'])) { $params['values'] = array_merge( $params['values'], array_fill($num_values, $q['placeholders'] - $num_values, end($params['values'])) ); $num_values = \count($params['values']); } /* The number of values must match the number of placeholders to bind */ if ($num_values !== $q['placeholders']) { $this->error( 'Incorrect arguments count (your values: '.$num_values.', in the statement: '.$q['placeholders'].")\n\n" .$statement."\n\n".'start of values'.print_r($params['values'], 1).'Arguments:' .print_r(\func_get_args(), true) .print_r($q, true) ); exit; } if ($q['exe_time'] === 0) { $time = $q['last']; } // That will always contain the parameters of the last query done $this->addStatement($q['sql'], $params); // If the statement is a structure modifier we need to clear the cache if ($q['structure']) { $tmp = $q; $this->queries = [$hash => $tmp]; $this->list_queries = [[ 'hash' => $hash, 'last' => $tmp['last'] ]]; unset($tmp); /** @todo Clear the cache */ } try{ // This is a writing statement, it will execute the statement and return the number of affected rows if ($q['write']) { // A prepared query already exists for the writing /** @var \bbn\Db\Query */ if ($q['prepared']) { $r = $q['prepared']->init($params['values'])->execute(); } // If there are no values we can assume the statement doesn't need to be prepared and is just executed elseif ($num_values === 0) { // Native PDO function which returns the number of affected rows $r = $this->pdo->exec($q['sql']); } // Preparing the query else{ // Native PDO function which will use Db\Query as base class /** @var \bbn\Db\Query */ $q['prepared'] = $this->pdo->prepare($q['sql'], $q['options']); $r = $q['prepared']->execute(); } } // This is a reading statement, it will prepare the statement and return a query object else{ if (!$q['prepared']) { // Native PDO function which will use Db\Query as base class $q['prepared'] = $this->pdo->prepare($q['sql'], $driver_options); } else{ // Returns the same Db\Query object $q['prepared']->init($params['values']); } } if (!empty($time) && ($q['exe_time'] === 0)) { $q['exe_time'] = microtime(true) - $time; } } catch (\PDOException $e) { $this->error($e); } if ($this->check()) { // So if read statement returns the query object if (!$q['write']) { return $q['prepared']; } // If it is a write statement returns the number of affected rows if ($q['prepared'] && $q['write']) { $r = $q['prepared']->rowCount(); } // If it is an insert statement we (try to) set the last inserted ID if (($q['kind'] === 'INSERT') && $r) { $this->setLastInsertId(); } return $r ?: false; } } }

Executes a writing statement and return the number of affected rows or return a query object for the reading * statement BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.