/** * @return Fluent * @throws InvalidStateException */ public function createFluent() { if ($this->clauses->from === null or empty($this->clauses->select)) { throw new InvalidStateException(); } $statement = $this->connection->command(); foreach (array_keys($this->clauses->select) as $alias) { // SELECT $statement->select($this->queryHelper->formatSelect($this->domainQueryHelper->getReflection($this->aliases->getEntityClass($alias)), $alias)); if (array_key_exists($alias, $this->relationshipTables)) { call_user_func_array(array($statement, 'select'), array_merge(array('%n.%n AS %n, %n.%n AS %n, %n.%n AS %n'), $this->relationshipTables[$alias])); } } $statement->from(array($this->clauses->from['table'] => $this->clauses->from['alias'])); // FROM foreach ($this->clauses->join as $join) { // JOIN call_user_func_array(array($statement, $join['type']), array_merge(array('%n AS %n'), $join['joinParameters'])); call_user_func_array(array($statement, 'on'), array_merge(array('%n.%n = %n.%n'), $join['onParameters'])); } if (!empty($this->clauses->where)) { // WHERE call_user_func_array(array($statement, 'where'), $this->clauses->where); } foreach ($this->clauses->orderBy as $orderBy) { // ORDER BY $statement->orderBy('%n.%n', $orderBy[0], $orderBy[1]); if ($orderBy[2] === self::ORDER_DESC) { $statement->desc(); } } return $statement; }
public function preloadEdges(ResultProxy $resultProxy) { $nodesIds = array(); foreach ($resultProxy as $node) { $nodesIds[$node['id']] = true; } $edges = $this->connection->select('*')->from('edge')->where('[source] IN %in OR [target] IN %in', $ids = array_keys($nodesIds), $ids)->orderBy('type')->fetchAll(); $referencing = Result::createInstance(array(), 'edge', $this->connection, $this->mapper); foreach ($edges as $edge) { if (isset($nodesIds[$edge['source']]) || isset($nodesIds[$edge['target']])) { $edge = $edge->toArray(); $edge['related_node'] = $edge['source']; $referencing->addDataEntry($edge); if ($edge['target'] !== $edge['source'] and $edge['target'] !== null) { $edge['related_node'] = $edge['target']; $referencing->addDataEntry($edge); } } } $referencing->cleanAddedAndRemovedMeta(); $resultProxy->setReferencingResult($referencing, 'edge', 'related_node'); }
/** * @param $username * @param $email * @return \App\Model\Entities\User * @throws DibiException */ public function generateUser($username, $email = null) { $values = ['username' => $username, 'password' => \Nette\Security\Passwords::hash('abcd'), 'ip' => '127.0.0.1', 'lastLogin' => '2015-05-01 06:00:00', 'lastIP' => '127.0.0.1']; if ($email === null) { $email = $username . '@' . $username . '.abc'; } $values['email'] = $email; $this->connection->insert('user', $values)->execute(); $id = $this->connection->getInsertId(); $user = new \App\Model\Entities\User($values['username'], $values['password'], $values['email'], $values['ip']); $this->makeEntityAlive($user, $id); return $user; }
/** * @param Fluent $statement * @param Filtering|null $filtering * @throws InvalidArgumentException */ private function applyFiltering(Fluent $statement, Filtering $filtering) { $targetedArgs = $filtering->getTargetedArgs(); foreach ($filtering->getFilters() as $filter) { $args = array($filter); if (!$filter instanceof Closure) { foreach (str_split($this->connection->getWiringSchema($filter)) as $autowiredArg) { if ($autowiredArg === 'e') { $args[] = $filtering->getEntity(); } elseif ($autowiredArg === 'p') { $args[] = $filtering->getProperty(); } } if (isset($targetedArgs[$filter])) { $args = array_merge($args, $targetedArgs[$filter]); } } $args = array_merge($args, $filtering->getArgs()); call_user_func_array(array($statement, 'applyFilter'), $args); } }
/** * Persists changes in M:N relationships * * @param Entity $entity */ protected function persistHasManyChanges(Entity $entity) { $primaryKey = $this->mapper->getPrimaryKey($this->getTable()); $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey); foreach ($entity->getHasManyRowDifferences() as $key => $difference) { list($columnReferencingSourceTable, $relationshipTable, $columnReferencingTargetTable) = explode(':', $key); $multiInsert = []; foreach ($difference as $value => $count) { if ($count > 0) { for ($i = 0; $i < $count; $i++) { $multiInsert[] = [$columnReferencingSourceTable => $entity->{$idField}, $columnReferencingTargetTable => $value]; } } else { $this->connection->query('DELETE FROM %n WHERE %n = ? AND %n = ? %lmt', $relationshipTable, $columnReferencingSourceTable, $entity->{$idField}, $columnReferencingTargetTable, $value, -$count); } } if (!empty($multiInsert)) { $this->connection->query('INSERT INTO %n %ex', $relationshipTable, $multiInsert); } } }
/** * @param Fluent $statement * @param Filtering|null $filtering * @return FilteringResult|null * @throws InvalidArgumentException */ private function applyFiltering(Fluent $statement, Filtering $filtering) { $targetedArgs = $filtering->getTargetedArgs(); foreach ($filtering->getFilters() as $filter) { $baseArgs = []; if (!$filter instanceof Closure) { foreach (str_split($this->connection->getWiringSchema($filter)) as $autowiredArg) { if ($autowiredArg === 'e') { $baseArgs[] = $filtering->getEntity(); } elseif ($autowiredArg === 'p') { $baseArgs[] = $filtering->getProperty(); } } if (isset($targetedArgs[$filter])) { $baseArgs = array_merge($baseArgs, $targetedArgs[$filter]); } } $result = call_user_func_array([$statement, 'applyFilter'], array_merge([$filter], $baseArgs, $filtering->getArgs())); if ($result instanceof FilteringResult) { return new FilteringResultDecorator($result, $baseArgs); } } }
/** * * @param string|null $savepoint */ public function rollback($savepoint = NULL) { $this->connection->rollback($savepoint); }