public function doSimpleQuery(Where $where = null) { if ($where === null) { $where = new Where(); } $sql = strtr("select :fields from :relation where :condition", [':fields' => $this->createProjection()->formatFieldsWithFieldAlias(), ':relation' => $this->getStructure()->getRelation(), ':condition' => (string) $where]); return $this->query($sql, $where->getValues()); }
/** * {@inheritdoc} */ public function getDigestHash($realm, $username) { $where = Where::create('username_canonical = $*', [$username]); $users = $this->manager->findWhere('public', 'users', $where); if ($users->count() == 0) { return null; } return $users->get(0)->password_digesta; }
public function getAccounts(array $role = []) { $personModel = $this->getSession()->getModel('\\GermBundle\\Model\\Germ\\PersonSchema\\PersonModel'); $sql = <<<SQL select {projection} from {account} a inner join {person} p on (p.id = a.person_id) where {where} SQL; $projection = $this->createProjection()->setField("person_name", "concat(p.lastname, ' ', p.firstname) as person_name", "varchar")->setField("account_id", "a.id as account_id", "varchar"); $where = new Where(); if ($role) { $where->andWhereIn('role', $role); } $sql = strtr($sql, ['{account}' => $this->structure->getRelation(), '{person}' => $personModel->getStructure()->getRelation(), '{projection}' => $projection->formatFields('a'), '{where}' => $where]); return $this->query($sql, [], $projection); }
/** * @param string $path * * @return array */ public function getPrincipalByPath($path) { $where = Where::create('uri = $*', [$path]); $principals = $this->manager->findWhere('public', 'principal', $where); if ($principals->count() == 0) { return []; } $principal = $principals->get(0); $ret = ['id' => $principal->id, 'uri' => $principal->uri]; foreach ($this->fieldMap as $key => $value) { $ret[$key] = $principal->{$value}['dbField']; } return $ret; }
/** * getTableFieldInformationName * * A short description here * * @param string $schema * @param string $name * @return ConvertedResultIterator */ public function getTableFieldInformationName($schema, $name) { return $this->getTableFieldInformationWhere(Where::create("cla.relname = \$*", [$name])->andWhere("clns.nspname = \$*", [$schema])); }
public function testGetSchemas() { $i = $this->getInspector(); $this->array($i->getSchemas(Where::create("n.nspname = \$*", ['inspector_test']))->current())->hasKeys(['name', 'oid', 'comment', 'relations', 'owner'])->integer($i->getSchemas(Where::create("n.nspname = \$*", ['whatever']))->count())->isEqualTo(0); }
public function testGetTableFieldInformation() { $this->assert("getTableFieldInformation() returns fields info upon OID.")->given($relation_oid = $this->getInspector()->getRelationsInSchema('inspector_test', Where::create("cl.relname = \$*", ['with_simple_pk']))->slice('oid')[0])->given($relation_info = $this->getInspector()->getTableFieldInformation($relation_oid))->object($relation_info)->isInstanceOf(ConvertedResultIterator::class)->array($relation_info->slice('name'))->isIdenticalTo(['with_simple_pk_id', 'a_patron', 'some_timestamps'])->array($relation_info->slice('type'))->isIdenticalTo(['int4', 'inspector_test._someone', '_timestamptz']); }
/** * @param string $slug * * @return RedirectResponse */ public function calendarDeleteAction($slug) { $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!'); $usr = $this->get('security.token_storage')->getToken()->getUser(); $where = Where::create('slug = $*', [$slug]); $calendars = $this->get('pmanager')->findWhere('public', 'calendar', $where); if ($calendars->count() == 0) { return $this->redirectToRoute('calendar_home'); } $calendar = $calendars->get(0); if ($calendar->principaluri != 'principals/' . $usr->getUsernameCanonical()) { $this->addFlash('danger', 'Ce calendrier ne vous appartient pas.'); return $this->redirectToRoute('calendar_read', ['slug' => $slug]); } $calendarBackend = new Calendar($this->get('pmanager')); $calendarBackend->deleteCalendar($calendar->uid); $this->addFlash('success', 'Le calendrier "' . $calendar->displayName . '" a bien été supprimé.'); return $this->redirectToRoute('calendar_home'); }
/** * @param Request $request * @return Response * @throws \Exception * * @ApiDoc( * description="Create a new event", * requirements={ * { * "name"="calendar_uri", * "dataType"="string", * "description"="The uri of the calendar", * }, * { * "name"="event_data", * "dataType"="array", * "description"="All the fields of the new event" * } * } * ) */ public function createEventAction(Request $request) { $params = array(); $content = $request->getContent(); if (!empty($content)) { $params = json_decode($content, true); } if (!isset($params['calendar_uri']) || !isset($params['event_data'])) { return $this->buildError(400, 'Missing parameters'); } $calendarBackend = new CalendarBackend($this->get('pmanager'), $this->generateUrl('event_read', [], true), $this->get('slugify')); $calendarUri = $params['calendar_uri']; $where = Where::create('uri = $*', [$calendarUri]); $rawCalendars = $this->get('pmanager')->findWhere('public', 'calendar', $where); if ($rawCalendars->count() == 0) { return $this->buildError('400', 'CalendarUri given does not correspond to any calendar in the database'); } $calendar = $rawCalendars->get(0); $calendarId = $calendar->uid; $event = new Event(); foreach ($params['event_data'] as $name => $value) { $event->{$name} = $value; } $vevent = $event->getVObject(); $calendarBackend->createCalendarObject($calendarId, $vevent->VEVENT->UID . '.ics', $vevent->serialize()); return $this->buildResponse(['created' => $vevent->VEVENT->UID->getValue()]); }
/** * {@inheritdoc} */ public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { // Current synctoken $calendar = $this->manager->findById('public', 'calendar', $calendarId); $currentToken = $calendar->synctoken; if (is_null($currentToken)) { return; } $result = ['syncToken' => $currentToken, 'added' => [], 'modified' => [], 'deleted' => []]; if ($syncToken) { $where = Where::create('synctoken >= $*', [$syncToken])->andWhere('synctoken < $*', [$currentToken])->andWhere('calendarid = $*', [$calendarId]); // Fetching all changes $calendarChanges = $this->manager->findWhere('public', 'calendarchange', $where, 'ORDER BY synctoken'); $changes = []; // This loop ensures that any duplicates are overwritten, only the // last change on a node is relevant. foreach ($calendarChanges as $change) { $changes[$change->uri] = $change->operation; } foreach ($changes as $uri => $operation) { switch ($operation) { case 1: $result['added'][] = $uri; break; case 2: $result['modified'][] = $uri; break; case 3: $result['deleted'][] = $uri; break; } } } else { // No synctoken supplied, this is the initial sync. $objects = $this->manager->findAll('public', 'calendarobject'); foreach ($objects as $object) { $result['added'] = $object->uri; } } return $result; }
/** * getUserSchemas * * Return a list of user schema (not pg_* nor information_schema). * * @param Where $where * @return ConvertedResultIterator */ public function getUserSchemas(Where $where = null) { $condition = Where::create()->andWhere($where)->andwhere("n.nspname !~ \$*", ['^pg_'])->andWhere("n.nspname <> \$*", ['information_schema']); return $this->getSchemas($condition); }
/** * getCompositeInformation * * Return the structure of a composite row. * * @access public * @param int $oid * @return \PommProject\Foundation\ConvertedResultIterator */ public function getCompositeInformation($oid) { $sql = <<<SQL select a.attname as "name", t.typname as "type" from pg_type orig join pg_catalog.pg_class c on orig.typrelid = c.oid join pg_catalog.pg_attribute a on a.attrelid = c.oid and a.attnum > 0 join pg_catalog.pg_type t on t.oid = a.atttypid where :condition SQL; return $this->executeSql($sql, Where::create('orig.oid = $*', [$oid])); }
/** * getTypesInSchema * * Return types defined in the given schema. * * @param string $schema * @return ConvertedResultIterator */ public function getTypesInSchema($schema, Where $where = null) { $schema_where = Where::create("ns.nspname = \$*", [$schema]); $where = $where === null ? $schema_where : $where->andWhere($schema_where); return $this->getTypes($where); }
/** * getWhereFrom * * Build a condition on given values. * * @access protected * @param array $values * @return Where */ protected function getWhereFrom(array $values) { $where = new Where(); foreach ($values as $field => $value) { $where->andWhere(sprintf("%s = \$*::%s", $this->escapeIdentifier($field), $this->getStructure()->getTypeFor($field)), [$value]); } return $where; }
public function testCreateWhereNotIn() { $where = PommWhere::createWhereNotIn('(a, b)', [[1, 2], [3, 4]]); $this->object($where)->isInstanceOf('\\PommProject\\Foundation\\Where')->string($where->__toString())->isEqualTo('(a, b) NOT IN (($*, $*), ($*, $*))'); }
protected function createWhereByCriteria($criteria = array()) { $where = new Where(); foreach ($criteria as $colname => $value) { $colname = Inflector::underscore($colname); $element = sprintf('%s = $*', $colname); $subWhere = new Where($element, array($value)); $where->andWhere($subWhere); } return $where; }
/** * {@inheritdoc} */ public function updateUser(UserInterface $user) { $this->updatePassword($user); if ($user->getId() == null) { $this->createPrincipals($user); $ret = $this->manager->insertOne('public', 'users', $user->jsonSerialize()); $user->setId($ret->id); } else { $where = Where::create('id = $*', [$user->getId()]); $dbUser = $this->manager->findWhere('public', 'users', $where)->get(0); $data = $user->jsonSerialize(); foreach ($data as $name => $value) { $dbUser->{$name} = $value; } $this->manager->updateOne('public', 'users', $dbUser, array_keys($data)); } }
public function testGetTypesInSchema() { $iterator = $this->getInspector()->getTypesInSchema('inspector_test', Where::create("t.typname ~* \$*", ['someone|sponsor'])); $this->assert('getTypesInSchema returns an iterator on results.')->object($iterator)->isInstanceOf(ConvertedResultIterator::class)->integer($iterator->count())->isEqualTo(4)->assert('getTypesInSchema returns the correct types.')->given($type = $iterator->get(0))->string($type['name'])->isEqualTo('someone')->string($type['category'])->isEqualTo('composite')->given($type = $iterator->get(2))->string($type['name'])->isEqualTo('sponsor_rating')->string($type['category'])->isEqualTo('enumerated'); }
public function testDeleteWhere() { $model = $this->getWriteFixtureModel($this->buildSession()); $entity1 = $model->createAndSave(['a_varchar' => 'qwerty', 'a_boolean' => false]); $entity2 = $model->createAndSave(['a_varchar' => 'qwertz', 'a_boolean' => true]); $deleted_entities = $model->deleteWhere('a_varchar = $*::varchar', ['qwertz']); $this->object($deleted_entities)->isInstanceOf('\\PommProject\\ModelManager\\Model\\CollectionIterator')->integer($deleted_entities->count())->isEqualTo(1)->object($deleted_entities->get(0))->isInstanceOf('\\PommProject\\ModelManager\\Test\\Fixture\\SimpleFixture')->isEqualTo($entity2)->integer($deleted_entities->get(0)->status())->isEqualTo(FlexibleEntityInterface::STATUS_NONE); $deleted_entities2 = $model->deleteWhere('a_varchar = $*::varchar', ['qwertz']); $this->object($deleted_entities2)->isInstanceOf('\\PommProject\\ModelManager\\Model\\CollectionIterator')->integer($deleted_entities2->count())->isEqualTo(0); $deleted_entities3 = $model->deleteWhere(Where::create('a_boolean = $*::boolean', [false])); $this->object($deleted_entities3)->isInstanceOf('\\PommProject\\ModelManager\\Model\\CollectionIterator')->integer($deleted_entities3->count())->isEqualTo(1)->object($deleted_entities3->get(0))->isInstanceOf('\\PommProject\\ModelManager\\Test\\Fixture\\SimpleFixture')->isEqualTo($entity1); }
/** * deleteWhere * * Delete records by a given condition. A collection of all deleted entries is returned. * * @param $where * @param array $values * @return CollectionIterator */ public function deleteWhere($where, array $values = []) { if (!$where instanceof Where) { $where = new Where($where, $values); } $sql = strtr("delete from :relation where :condition returning :projection", [':relation' => $this->getStructure()->getRelation(), ':condition' => (string) $where, ':projection' => $this->createProjection()->formatFieldsWithFieldAlias()]); $collection = $this->query($sql, $where->getValues()); foreach ($collection as $entity) { $entity->status(FlexibleEntityInterface::STATUS_NONE); } $collection->rewind(); return $collection; }