/** * @return Row */ public function current() { if (array_key_exists($this->position, $this->cache)) { return $this->cache[$this->position]; } else { return $this->cache[$this->position] = $this->processor->loadDataObject($this->result->fetch(\PDO::FETCH_ASSOC)); } }
/** * {@inheritdoc} */ public function rewind() { if ($this->statement) { throw new InvalidMethodCallException('Cannot rewind a PDOStatement'); } $this->statement = $this->connection->prepare($this->query); $this->statement->execute($this->parameters); $this->next(); }
/** * common used bindValue. this is very useful while create query for count first and query again to get all data. * * @param \Doctrine\DBAL\Driver\Statement $stmt Doctrine statement object. * @param array $options options for bindValue. */ private function bindValues($stmt, array $options = []) { if (isset($options['module_enable'])) { $stmt->bindValue('module_enable', $options['module_enable'], \PDO::PARAM_INT); } if (isset($options['site_id'])) { $stmt->bindValue('site_id', $options['site_id'], \PDO::PARAM_INT); } if (isset($options['search'])) { $stmt->bindValue('search', '%' . $options['search'] . '%', \PDO::PARAM_STR); } }
protected function assertTableKeys($expected, Statement $query, $keyName) { $query->execute(); $users = array(); while ($row = $query->fetch()) { $users[] = $row[$keyName]; } $query->closeCursor(); $users = array_unique($users); sort($users); sort($expected); $this->assertEquals($expected, $users); }
/** * Return an object property or a field of the current row * * @param string $strKey The field name * * @return mixed|null The field value or null */ public function __get($strKey) { switch ($strKey) { case 'query': return $this->strQuery; break; case 'numRows': return $this->count(); break; case 'numFields': return $this->resResult->columnCount(); break; case 'isModified': return $this->blnModified; break; default: if (empty($this->arrCache)) { $this->next(); } if (isset($this->arrCache[$strKey])) { return $this->arrCache[$strKey]; } break; } return null; }
/** * Load data if it hasn't been loaded yet */ protected function loadData() { if (null === $this->data) { $this->stmt->execute(); $this->data = $this->stmt->fetchAll(\PDO::FETCH_ASSOC); } }
/** * Executes one-time cleanup tasks at the end of a hydration that was initiated * through {@link hydrateAll} or {@link iterate()}. * * @return void */ protected function cleanup() { $this->_stmt->closeCursor(); $this->_stmt = null; $this->_rsm = null; $this->_cache = array(); $this->_metadataCache = array(); }
/** * Return an object property * * @param string $strKey The property name * * @return mixed|null The property value or null */ public function __get($strKey) { switch ($strKey) { case 'query': return $this->strQuery; break; case 'error': $info = $this->statement->errorInfo(); return 'SQLSTATE ' . $info[0] . ': error ' . $info[1] . ': ' . $info[2]; break; case 'affectedRows': return $this->statement->rowCount(); break; case 'insertId': return $this->resConnection->lastInsertId(); break; } return null; }
/** * Hydrates a single row returned by the current statement instance during * row-by-row hydration with {@link iterate()}. * * @return mixed */ public function hydrateRow() { $row = $this->_stmt->fetch(PDO::FETCH_ASSOC); if (!$row) { $this->cleanup(); return false; } $result = array(); $this->hydrateRowData($row, $result); return $result; }
/** * Implementation of Iterator. * * @link http://php.net/manual/en/class.iterator.php */ public function valid() { if (isset($this->results[$this->position])) { return true; } $result = $this->stmt->fetch(PDO::FETCH_ASSOC); if ($result) { $this->results[$this->position] = $result; return true; } return false; }
/** * @param string $sql Consulta SQL a la base de datos * @param array $criteria Criterios de la consulta SQL adoptados en el WHERE * * @return Statement Retorna el objeto de DoctrineORM Statement * @throws DBALException Error de DoctrineORM * @throws Exception Error en el SQL */ protected function _query($sql = "", array $criteria = array()) { try { // Preparar el SQL $this->_stmt = $this->_connection->prepare($sql); // Agregar los parametros foreach ($criteria as $param => $value) { if (is_integer($param)) { $this->_stmt->bindValue($param + 1, $value); } if (is_string($param)) { $this->_stmt->bindParam($param, $value); } } // Ejecutar el SQL $this->_stmt->execute(); } catch (DBALException $dbalException) { #throw $dbalException; } if (in_array($this->_stmt->errorCode(), array_keys($this->_errors))) { throw new Exception($this->_errors[$this->_stmt->errorCode()] . " SQL: {$sql}"); } return $this->_stmt; }
/** * Performs a DELETE or UPDATE query to the database. * @param \Doctrine\DBAL\Driver\Statement $query * @param array $parameters * @return bool true if at least one row was modified, false otherwise */ protected function modify($query, $parameters) { $result = $query->execute($parameters); return $result === true && $query->rowCount() > 0; }
/** * Bind parameters to statement * * @param Statement $stmt * @param ArrayCollection $parameters */ public function bindParameters(Statement $stmt, ArrayCollection $parameters) { $values = []; $types = []; foreach ($parameters as $parameter) { /* @var $parameter Parameter */ $values[] = $parameter->getValue(); $types[] = $parameter->getType(); } $typeOffset = array_key_exists(0, $types) ? -1 : 0; $bindIndex = 1; foreach ($values as $value) { $typeIndex = $bindIndex + $typeOffset; if (isset($types[$typeIndex])) { $type = $types[$typeIndex]; $stmt->bindValue($bindIndex, $value, $type); } else { $stmt->bindValue($bindIndex, $value); } ++$bindIndex; } }
/** * Executes one-time cleanup tasks at the end of a hydration that was initiated * through {@link hydrateAll} or {@link iterate()}. * * @return void */ protected function cleanup() { $this->_rsm = null; $this->_stmt->closeCursor(); $this->_stmt = null; }
public function rowCount() { return $this->stmt->rowCount(); }
public function rowCount() { return $this->wrapped->rowCount(); }
/** * Returns an array containing all of the result set rows. * * @param int|null $fetchMode Controls how the next row will be returned to the caller. * The value must be one of the PDO::FETCH_* constants, * defaulting to PDO::FETCH_BOTH. * * @return \Narrowspark\Collection\Collection * * @see PDO::FETCH_* constants. */ public function fetchAll(int $fetchMode = null) : Collection { return new Collection($this->statement->fetch($fetchMode)); }
/** * rowCount * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement * executed by the corresponding object. * * If the last SQL statement executed by the associated Statement object was a SELECT statement, * some databases may return the number of rows returned by that statement. However, * this behaviour is not guaranteed for all databases and should not be * relied on for portable applications. * * @return integer Returns the number of rows. */ public function rowCount() { return $this->statement->rowCount(); }
/** * Bind params to SQL statement. * * @param Statement $statement * @param array $params */ private function bindParams($statement, $params) { foreach ($params as $param) { $statement->bindValue($param[0], $param[1], $param[2]); } }
/** * This method is called to hydrate ACLs and ACEs. * * This method was designed for performance; thus, a lot of code has been * inlined at the cost of readability, and maintainability. * * Keep in mind that changes to this method might severely reduce the * performance of the entire ACL system. * * @param Statement $stmt * @param array $oidLookup * @param array $sids * @throws \RuntimeException * @return \SplObjectStorage */ protected function hydrateObjectIdentities(Statement $stmt, array $oidLookup, array $sids) { $parentIdToFill = new \SplObjectStorage(); $acls = $aces = $emptyArray = array(); $oidCache = $oidLookup; $result = new \SplObjectStorage(); $loadedAces =& $this->loadedAces; $loadedAcls =& $this->loadedAcls; $permissionGrantingStrategy = $this->permissionGrantingStrategy; // we need these to set protected properties on hydrated objects $aclReflection = new \ReflectionClass('Symfony\\Component\\Security\\Acl\\Domain\\Acl'); $aclClassAcesProperty = $aclReflection->getProperty('classAces'); $aclClassAcesProperty->setAccessible(true); $aclClassFieldAcesProperty = $aclReflection->getProperty('classFieldAces'); $aclClassFieldAcesProperty->setAccessible(true); $aclObjectAcesProperty = $aclReflection->getProperty('objectAces'); $aclObjectAcesProperty->setAccessible(true); $aclObjectFieldAcesProperty = $aclReflection->getProperty('objectFieldAces'); $aclObjectFieldAcesProperty->setAccessible(true); $aclParentAclProperty = $aclReflection->getProperty('parentAcl'); $aclParentAclProperty->setAccessible(true); // fetchAll() consumes more memory than consecutive calls to fetch(), // but it is faster foreach ($stmt->fetchAll(\PDO::FETCH_NUM) as $data) { list($aclId, $objectIdentifier, $parentObjectIdentityId, $entriesInheriting, $classType, $aceId, $objectIdentityId, $fieldName, $aceOrder, $mask, $granting, $grantingStrategy, $auditSuccess, $auditFailure, $username, $securityIdentifier) = $data; // has the ACL been hydrated during this hydration cycle? if (isset($acls[$aclId])) { $acl = $acls[$aclId]; } else { if (isset($loadedAcls[$classType][$objectIdentifier])) { $acl = $loadedAcls[$classType][$objectIdentifier]; // keep reference in local array (saves us some hash calculations) $acls[$aclId] = $acl; // attach ACL to the result set; even though we do not enforce that every // object identity has only one instance, we must make sure to maintain // referential equality with the oids passed to findAcls() if (!isset($oidCache[$objectIdentifier . $classType])) { $oidCache[$objectIdentifier . $classType] = $acl->getObjectIdentity(); } $result->attach($oidCache[$objectIdentifier . $classType], $acl); } else { // create object identity if we haven't done so yet $oidLookupKey = $objectIdentifier . $classType; if (!isset($oidCache[$oidLookupKey])) { $oidCache[$oidLookupKey] = new ObjectIdentity($objectIdentifier, $classType); } $acl = new Acl((int) $aclId, $oidCache[$oidLookupKey], $permissionGrantingStrategy, $emptyArray, !!$entriesInheriting); // keep a local, and global reference to this ACL $loadedAcls[$classType][$objectIdentifier] = $acl; $acls[$aclId] = $acl; // try to fill in parent ACL, or defer until all ACLs have been hydrated if (null !== $parentObjectIdentityId) { if (isset($acls[$parentObjectIdentityId])) { $aclParentAclProperty->setValue($acl, $acls[$parentObjectIdentityId]); } else { $parentIdToFill->attach($acl, $parentObjectIdentityId); } } $result->attach($oidCache[$oidLookupKey], $acl); } } // check if this row contains an ACE record if (null !== $aceId) { // have we already hydrated ACEs for this ACL? if (!isset($aces[$aclId])) { $aces[$aclId] = array($emptyArray, $emptyArray, $emptyArray, $emptyArray); } // has this ACE already been hydrated during a previous cycle, or // possible been loaded from cache? // It is important to only ever have one ACE instance per actual row since // some ACEs are shared between ACL instances if (!isset($loadedAces[$aceId])) { if (!isset($sids[$key = ($username ? '1' : '0') . $securityIdentifier])) { if ($username) { $sids[$key] = new UserSecurityIdentity(substr($securityIdentifier, 1 + ($pos = strpos($securityIdentifier, '-'))), substr($securityIdentifier, 0, $pos)); } else { $sids[$key] = new RoleSecurityIdentity($securityIdentifier); } } if (null === $fieldName) { $loadedAces[$aceId] = new Entry((int) $aceId, $acl, $sids[$key], $grantingStrategy, (int) $mask, !!$granting, !!$auditFailure, !!$auditSuccess); } else { $loadedAces[$aceId] = new FieldEntry((int) $aceId, $acl, $fieldName, $sids[$key], $grantingStrategy, (int) $mask, !!$granting, !!$auditFailure, !!$auditSuccess); } } $ace = $loadedAces[$aceId]; // assign ACE to the correct property if (null === $objectIdentityId) { if (null === $fieldName) { $aces[$aclId][0][$aceOrder] = $ace; } else { $aces[$aclId][1][$fieldName][$aceOrder] = $ace; } } else { if (null === $fieldName) { $aces[$aclId][2][$aceOrder] = $ace; } else { $aces[$aclId][3][$fieldName][$aceOrder] = $ace; } } } } // We do not sort on database level since we only want certain subsets to be sorted, // and we are going to read the entire result set anyway. // Sorting on DB level increases query time by an order of magnitude while it is // almost negligible when we use PHPs array sort functions. foreach ($aces as $aclId => $aceData) { $acl = $acls[$aclId]; ksort($aceData[0]); $aclClassAcesProperty->setValue($acl, $aceData[0]); foreach (array_keys($aceData[1]) as $fieldName) { ksort($aceData[1][$fieldName]); } $aclClassFieldAcesProperty->setValue($acl, $aceData[1]); ksort($aceData[2]); $aclObjectAcesProperty->setValue($acl, $aceData[2]); foreach (array_keys($aceData[3]) as $fieldName) { ksort($aceData[3][$fieldName]); } $aclObjectFieldAcesProperty->setValue($acl, $aceData[3]); } // fill-in parent ACLs where this hasn't been done yet cause the parent ACL was not // yet available $processed = 0; foreach ($parentIdToFill as $acl) { $parentId = $parentIdToFill->offsetGet($acl); // let's see if we have already hydrated this if (isset($acls[$parentId])) { $aclParentAclProperty->setValue($acl, $acls[$parentId]); $processed += 1; continue; } } // reset reflection changes $aclClassAcesProperty->setAccessible(false); $aclClassFieldAcesProperty->setAccessible(false); $aclObjectAcesProperty->setAccessible(false); $aclObjectFieldAcesProperty->setAccessible(false); $aclParentAclProperty->setAccessible(false); // this should never be true if the database integrity hasn't been compromised if ($processed < count($parentIdToFill)) { throw new \RuntimeException('Not all parent ids were populated. This implies an integrity problem.'); } return $result; }
/** * @brief Writes debug output including additional SQL error info to the ownCloud log. * * @param string $output The string appended to ownCloud log. * @param \Doctrine\DBAL\Driver\Statement $query The query whose information * will be added to the debug output. * @param int $level Log level of debug output. Default is \OCP\Util::ERROR. */ public static function writeLogDbError($output, $query, $level = \OCP\Util::ERROR) { $output = $output . " SQLSTATE: " . $query->errorCode() . ". Error info: " . var_export($query->errorInfo(), true); self::writeLog($output, $level); }
public function testRowCount() { $rowCount = 666; $this->wrappedStmt->expects($this->once())->method('rowCount')->will($this->returnValue($rowCount)); $this->assertSame($rowCount, $this->stmt->rowCount()); }
function it_counts_complete_products_per_channels(Statement $statement) { $statement->execute()->willReturn(null); $statement->fetchAll()->willReturn(array(array('locale' => 'en_US', 'label' => 'ECommerce', 'total' => 0), array('locale' => 'fr_FR', 'label' => 'ECommerce', 'total' => 1), array('locale' => 'en_US', 'label' => 'Mobile', 'total' => 2))); $this->getCompleteProductsCountPerChannels()->shouldReturn(array(array('locale' => 'en_US', 'label' => 'ECommerce', 'total' => 0), array('locale' => 'fr_FR', 'label' => 'ECommerce', 'total' => 1), array('locale' => 'en_US', 'label' => 'Mobile', 'total' => 2))); }
/** * Binds values to the prepared statement * * @param Statement $stmt statement * @param array $valuesForBind values to bind in following structure: * [ * "key" name of parameter in statement * => [ * "value" value of the parameter * "type" type of the parameter (optional) * ] * ] */ private function bindValuesToStatement(Statement $stmt, $valuesForBind) { foreach ($valuesForBind as $key => $data) { if (isset($data['type'])) { $stmt->bindValue($key, $data['value'], $data['type']); } else { $stmt->bindValue($key, $data['value']); } } }
/** * Stores a query result into an array. * * @author Olivier Brouckaert * @param Statement $result - the return value of the query * @param string $option BOTH, ASSOC, or NUM * * @return array - the value returned by the query */ public static function store_result(Statement $result, $option = 'BOTH') { return $result->fetchAll(self::customOptionToDoctrineOption($option)); }
/** * Returns the list of items based on the input SQL result pointer * For each result row the internal var, $this->internal['currentRow'], is set with the row returned. * $this->pi_list_header() makes the header row for the list * $this->pi_list_row() is used for rendering each row * Notice that these two functions are typically ALWAYS defined in the extension class of the plugin since they are directly concerned with the specific layout for that plugins purpose. * * @param Statement $statement Result pointer to a SQL result which can be traversed. * @param string $tableParams Attributes for the table tag which is wrapped around the table rows containing the list * @return string Output HTML, wrapped in <div>-tags with a class attribute * @see pi_list_row(), pi_list_header() */ public function pi_list_makelist($statement, $tableParams = '') { // Make list table header: $tRows = []; $this->internal['currentRow'] = ''; $tRows[] = $this->pi_list_header(); // Make list table rows $c = 0; while ($this->internal['currentRow'] = $statement->fetch()) { $tRows[] = $this->pi_list_row($c); $c++; } $out = ' <!-- Record list: --> <div' . $this->pi_classParam('listrow') . '> <' . rtrim('table ' . $tableParams) . '> ' . implode('', $tRows) . ' </table> </div>'; return $out; }
/** * Binds a set of parameters, some or all of which are typed with a PDO binding type * or DBAL mapping type, to a given statement. * * @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to. * @param array $params The map/list of named/positional parameters. * @param array $types The parameter types (PDO binding types or DBAL mapping types). * * @return void * * @internal Duck-typing used on the $stmt parameter to support driver statements as well as * raw PDOStatement instances. */ private function _bindTypedValues($stmt, array $params, array $types) { // Check whether parameters are positional or named. Mixing is not allowed, just like in PDO. if (is_int(key($params))) { // Positional parameters $typeOffset = array_key_exists(0, $types) ? -1 : 0; $bindIndex = 1; foreach ($params as $value) { $typeIndex = $bindIndex + $typeOffset; if (isset($types[$typeIndex])) { $type = $types[$typeIndex]; list($value, $bindingType) = $this->getBindingInfo($value, $type); $stmt->bindValue($bindIndex, $value, $bindingType); } else { $stmt->bindValue($bindIndex, $value); } ++$bindIndex; } } else { // Named parameters foreach ($params as $name => $value) { if (isset($types[$name])) { $type = $types[$name]; list($value, $bindingType) = $this->getBindingInfo($value, $type); $stmt->bindValue($name, $value, $bindingType); } else { $stmt->bindValue($name, $value); } } } }
/** * Stores a query result into an array. * @param Statement $result - the return value of the query * @param option BOTH, ASSOC, or NUM * @return array - the value returned by the query */ public static function store_result(Statement $result, $option = 'BOTH') { return $result->fetchAll(self::customOptionToDoctrineOption($option)); /* var_dump($a ); $array = array(); if ($result !== false) { // For isolation from database engine's behaviour. while ($row = self::fetch_array($result, $option)) { $array[] = $row; } } return $array;*/ }
/** * Binds a PHP variable to a corresponding named or question mark placeholder in the * SQL statement that was use to prepare the statement. * * @param mixed $column Either the placeholder name or the 1-indexed placeholder index * @param mixed $variable The variable to bind * @param integer|null $type one of the PDO::PARAM_* constants * @param integer|null $length max length when using an OUT bind * @return boolean */ public function bindParam($column, &$variable, $type = null, $length = null) { return $this->statement->bindParam($column, $variable, $type, $length); }
/** * fetch the next row of a result set * * @param Statement $result as returned by query * * @return mixed false on error */ private function fetch(Statement $result) { return $result->fetch(\PDO::FETCH_ASSOC); }