Inheritance: implements GraphAware\Neo4j\Client\StackInterface
 public function prepareQuery(NodeInterface $input, Recommendation $recommendation, CypherAwarePostProcessor $postProcessor)
 {
     $query = 'MATCH (input), (reco) WHERE id(input) = {idInput} AND id(reco) = {idReco}' . PHP_EOL;
     $query .= $postProcessor->query();
     $parameters = ['idInput' => $input->identity(), 'idReco' => $recommendation->item()->identity()];
     $tag = sprintf('post_process_%s_%d', $postProcessor->name(), $recommendation->item()->identity());
     $this->stack->push($query, $parameters, $tag);
 }
 private function createLabeledNodesCreationStack(array $byLabelsMap)
 {
     $stack = Stack::create(self::TAG_NODES_CREATE);
     $statements = [];
     foreach ($byLabelsMap as $label => $entities) {
         foreach ($entities as $entity) {
             $query = sprintf('UNWIND {nodes} as node
             CREATE (n:`%s`) SET n += node.props', $label);
             $metadata = $this->em->getClassMetadataFor(get_class($entity));
             $oid = spl_object_hash($entity);
             $labeledProperties = $metadata->getLabeledPropertiesToBeSet($entity);
             $lblKey = sprintf('%s_%s', $metadata->getLabel(), implode('_', array_map(function (LabeledPropertyMetadata $labeledPropertyMetadata) {
                 return $labeledPropertyMetadata->getLabelName();
             }, $labeledProperties)));
             foreach ($labeledProperties as $labeledPropertyMetadata) {
                 $query .= sprintf(' SET n:`%s`', $labeledPropertyMetadata->getLabelName());
             }
             $query .= ' RETURN id(n) as id, node.oid as oid';
             $statements[$lblKey]['query'] = $query;
             $statements[$lblKey]['nodes'][] = ['props' => $metadata->getPropertyValuesArray($entity), 'oid' => $oid];
         }
     }
     foreach ($statements as $key => $statement) {
         $stack->push($statement['query'], ['nodes' => $statement['nodes']], $key);
     }
     return $stack;
 }
 public function testStatsAreMergedWithBolt()
 {
     $this->emptyDb();
     $stack = Stack::create(null, 'bolt');
     $stack->push('CREATE (n:Node)');
     $stack->push('CREATE (n:Node)');
     $results = $this->client->runStack($stack);
     $this->assertEquals(2, $results->updateStatistics()->nodesCreated());
     $this->assertEquals(2, $results->updateStatistics()->labelsAdded());
 }
Example #4
0
 /**
  * @param string|null $tag
  * @param string|null $connectionAlias
  *
  * @return StackInterface
  */
 public function stack($tag = null, $connectionAlias = null)
 {
     return Stack::create($tag, $connectionAlias);
 }
Example #5
0
 public function flush()
 {
     //preFlush
     if ($this->eventManager->hasListeners(Events::PRE_FLUSH)) {
         $this->eventManager->dispatchEvent(Events::PRE_FLUSH, new Event\PreFlushEventArgs($this->entityManager));
     }
     //Detect changes
     $this->detectRelationshipReferenceChanges();
     $this->detectRelationshipEntityChanges();
     $this->detectEntityChanges();
     $statements = [];
     //onFlush
     if ($this->eventManager->hasListeners(Events::ON_FLUSH)) {
         $this->eventManager->dispatchEvent(Events::ON_FLUSH, new Event\OnFlushEventArgs($this->entityManager));
     }
     foreach ($this->nodesScheduledForCreate as $nodeToCreate) {
         $this->traverseRelationshipEntities($nodeToCreate);
         $class = get_class($nodeToCreate);
         $persister = $this->getPersister($class);
         $statements[] = $persister->getCreateQuery($nodeToCreate);
     }
     $tx = $this->entityManager->getDatabaseDriver()->transaction();
     $tx->begin();
     $nodesCreationStack = $this->flushOperationProcessor->processNodesCreationJob($this->nodesScheduledForCreate);
     $results = $tx->runStack($nodesCreationStack);
     foreach ($results as $result) {
         foreach ($result->records() as $record) {
             $oid = $record->get('oid');
             $gid = $record->get('id');
             $this->hydrateGraphId($oid, $gid);
             $this->entitiesById[$gid] = $this->nodesScheduledForCreate[$oid];
             $this->entityIds[$oid] = $gid;
             $this->entityStates[$oid] = self::STATE_MANAGED;
             $this->manageEntityReference($oid);
         }
     }
     $relStack = $this->entityManager->getDatabaseDriver()->stack('rel_create_schedule');
     foreach ($this->relationshipsScheduledForCreated as $relationship) {
         $statement = $this->relationshipPersister->getRelationshipQuery($this->entityIds[spl_object_hash($relationship[0])], $relationship[1], $this->entityIds[spl_object_hash($relationship[2])]);
         $relStack->push($statement->text(), $statement->parameters());
     }
     if (count($this->relationshipsScheduledForDelete) > 0) {
         foreach ($this->relationshipsScheduledForDelete as $toDelete) {
             $statement = $this->relationshipPersister->getDeleteRelationshipQuery($toDelete[0], $toDelete[1], $toDelete[2]);
             $relStack->push($statement->text(), $statement->parameters());
         }
     }
     $tx->runStack($relStack);
     $reStack = Stack::create('rel_entity_create');
     foreach ($this->relEntitiesScheduledForCreate as $oid => $info) {
         $rePersister = $this->getRelationshipEntityPersister(get_class($info[0]));
         $statement = $rePersister->getCreateQuery($info[0], $info[1]);
         $reStack->push($statement->text(), $statement->parameters());
     }
     foreach ($this->relEntitesScheduledForUpdate as $oid => $entity) {
         $rePersister = $this->getRelationshipEntityPersister(get_class($entity));
         $statement = $rePersister->getUpdateQuery($entity);
         $reStack->push($statement->text(), $statement->parameters());
     }
     foreach ($this->relEntitesScheduledForDelete as $o) {
         $statement = $this->getRelationshipEntityPersister(get_class($o))->getDeleteQuery($o);
         $reStack->push($statement->text(), $statement->parameters());
     }
     $tx->runStack($reStack);
     $updateNodeStack = Stack::create('update_nodes');
     foreach ($this->nodesScheduledForUpdate as $entity) {
         $this->traverseRelationshipEntities($entity);
         $statement = $this->getPersister(get_class($entity))->getUpdateQuery($entity);
         $updateNodeStack->push($statement->text(), $statement->parameters());
     }
     $tx->pushStack($updateNodeStack);
     $deleteNodeStack = Stack::create('delete_nodes');
     $possiblyDeleted = [];
     foreach ($this->nodesScheduledForDelete as $entity) {
         $statement = $this->getPersister(get_class($entity))->getDeleteQuery($entity);
         $deleteNodeStack->push($statement->text(), $statement->parameters());
         $possiblyDeleted[] = spl_object_hash($entity);
     }
     $tx->pushStack($deleteNodeStack);
     $tx->commit();
     foreach ($this->relationshipsScheduledForCreated as $rel) {
         $aoid = spl_object_hash($rel[0]);
         $boid = spl_object_hash($rel[2]);
         $field = $rel[3];
         $this->managedRelationshipReferences[$aoid][$field][] = ['entity' => $aoid, 'target' => $boid, 'rel' => $rel[1]];
     }
     foreach ($possiblyDeleted as $oid) {
         $this->entityStates[$oid] = self::STATE_DELETED;
     }
     //postFlush
     if ($this->eventManager->hasListeners(Events::POST_FLUSH)) {
         $this->eventManager->dispatchEvent(Events::POST_FLUSH, new Event\PostFlushEventArgs($this->entityManager));
     }
     $this->nodesScheduledForCreate = $this->nodesScheduledForUpdate = $this->nodesScheduledForDelete = $this->relationshipsScheduledForCreated = $this->relationshipsScheduledForDelete = $this->relEntitesScheduledForUpdate = $this->relEntitiesScheduledForCreate = $this->relEntitesScheduledForDelete = [];
 }