-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from mnbjhu/bug_fix
Call and foreach
- Loading branch information
Showing
18 changed files
with
189 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package uk.gibby.neo4k.clauses | ||
|
||
import uk.gibby.neo4k.core.QueryScope | ||
import uk.gibby.neo4k.queries.Query | ||
import uk.gibby.neo4k.returns.empty.EmptyReturn | ||
|
||
class Call(private val innerQuery: Query<Unit, EmptyReturn, *, *>): Clause(){ | ||
override fun getString(): String { | ||
return "CALL { WITH * ${innerQuery.query} }" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package uk.gibby.neo4k.clauses | ||
|
||
import uk.gibby.neo4k.core.NameCounter | ||
import uk.gibby.neo4k.core.QueryScope | ||
import uk.gibby.neo4k.returns.ReturnValue | ||
import uk.gibby.neo4k.returns.empty.EmptyReturn | ||
import uk.gibby.neo4k.returns.empty.EmptyReturnInstance | ||
import uk.gibby.neo4k.returns.generic.ArrayReturn | ||
|
||
class ForEach<T, U: ReturnValue<T>>(private val array: ArrayReturn<T, U>, private val query: QueryScope.(U) -> Unit): Clause(){ | ||
private val element = array.inner.createReference(NameCounter.next()) as U | ||
override fun getString(): String { | ||
val scope = QueryScope() | ||
scope.query(element) | ||
return "FOREACH(${element.getString()} IN ${array.getString()}|${scope.getString()})" | ||
} | ||
companion object{ | ||
fun <T, U: ReturnValue<T>>QueryScope.forEach(array: ArrayReturn<T, U>, action: QueryScope.(U) -> Unit): EmptyReturn { | ||
addStatement(ForEach(array, action)) | ||
return EmptyReturnInstance | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package uk.gibby.neo4k.clauses | ||
|
||
import uk.gibby.neo4k.core.Creatable | ||
import uk.gibby.neo4k.core.QueryScope | ||
|
||
|
||
/** | ||
* Create | ||
* | ||
* Represents the 'CREATE' clause from the CypherQL | ||
* | ||
* [Neo4j Cypher Manual](https://neo4j.com/docs/cypher-manual/current/clauses/create/) | ||
* | ||
* @property creatable The nodes or paths to create | ||
* @constructor Creates a 'CREATE' clause | ||
*/ | ||
class Merge(private vararg val creatable: Creatable<*>): Clause(){ | ||
override fun getString() = "MERGE ${creatable.joinToString { it.getCreateString() }}" | ||
companion object{ | ||
/** | ||
* Create | ||
* | ||
* Used to create nodes or paths as part of a graph query | ||
* | ||
* [Neo4j Cypher Manual](https://neo4j.com/docs/cypher-manual/current/clauses/merge/) | ||
* | ||
* @param T Type of the reference produced | ||
* @param U Type of the [Creatable] | ||
* @param creatable Describes one node or path to merge | ||
* @receiver The scope of the query | ||
* @return The references to the produced nodes or paths | ||
* @see [QueryScope] | ||
*/ | ||
fun <T, U : Creatable<T>> QueryScope.merge(creatable: U): T{ | ||
addStatement(Create(creatable)) | ||
return creatable.getReference() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
package uk.gibby.neo4k.core | ||
|
||
import uk.gibby.neo4k.clauses.Clause | ||
import uk.gibby.neo4k.clauses.* | ||
import uk.gibby.neo4k.queries.QueryBuilder | ||
import uk.gibby.neo4k.returns.ReturnValue | ||
import uk.gibby.neo4k.returns.empty.EmptyReturn | ||
import uk.gibby.neo4k.returns.primitives.LongReturn | ||
|
||
class QueryScope { | ||
private val clauses = mutableListOf<Clause>() | ||
private val afterClauses = mutableListOf<Clause>() | ||
fun addStatement(statement: Clause){ clauses.add(statement) } | ||
fun addStatementAfterReturn(statement: Clause){ afterClauses.add(statement) } | ||
|
||
|
||
fun getString() = clauses.joinToString(" "){it.getString()} | ||
fun getAfterString() = afterClauses.joinToString(" ") { it.getString() } | ||
|
||
fun <T, U: ReturnValue<T>>QueryBuilder<Unit, EmptyReturn, T, U>.call(): U { | ||
val query = _build() | ||
addStatement(Call(query)) | ||
return query.returnValue | ||
} | ||
infix fun <U: ReturnValue<*>>U.limit(count: LongReturn): U = this.also { addStatementAfterReturn(Limit(count)) } | ||
infix fun <U: ReturnValue<*>>U.limit(count: Long): U = this.also { addStatementAfterReturn(Limit(count)) } | ||
infix fun <U: ReturnValue<*>> U.orderBy(result: ReturnValue<*>): U { | ||
addStatementAfterReturn(OrderBy(result)) | ||
return this | ||
} | ||
infix fun <U: ReturnValue<*>>U.orderByDesc(result: ReturnValue<*>): U{ | ||
addStatementAfterReturn(OrderBy(result, true)) | ||
return this | ||
} | ||
infix fun <U: ReturnValue<*>>U.skip(count: LongReturn) = this.also { addStatementAfterReturn(Skip(count)) } | ||
infix fun <U: ReturnValue<*>>U.skip(count: Long) = this.also { addStatementAfterReturn(Skip(count)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package e2e.clauses | ||
|
||
import e2e.schemas.ActedIn | ||
import e2e.schemas.Actor | ||
import e2e.schemas.Movie | ||
import org.amshove.kluent.`should contain same` | ||
import org.junit.jupiter.api.Test | ||
import uk.gibby.neo4k.clauses.Create.Companion.create | ||
import uk.gibby.neo4k.clauses.ForEach.Companion.forEach | ||
import uk.gibby.neo4k.clauses.Match.Companion.match | ||
import uk.gibby.neo4k.core.array | ||
import uk.gibby.neo4k.core.invoke | ||
import uk.gibby.neo4k.core.of | ||
import uk.gibby.neo4k.paths.`o-→` | ||
import uk.gibby.neo4k.queries.query | ||
import uk.gibby.neo4k.returns.primitives.StringReturn | ||
import util.GraphTest | ||
|
||
class Call : GraphTest(){ | ||
@Test | ||
fun forEachTest(){ | ||
val myList = listOf("something1", "something2") | ||
graph.query { forEach(array(::StringReturn) of myList) { name -> create(::Actor{ it[this.name] = name }) } } | ||
graph.query { | ||
val actors = match(::Actor) | ||
actors.name | ||
} `should contain same` myList | ||
} | ||
|
||
@Test | ||
fun callTest(){ | ||
val myList = listOf("something1", "something2", "something3") | ||
graph.query { forEach(array(::StringReturn) of myList) { name -> create(::Actor{ it[this.name] = name }) } } | ||
graph.query { forEach(array(::StringReturn) of myList) { name -> create(::Movie{ it[title] = name; it[releaseYear] = 1234}) } } | ||
graph.query { | ||
val actor = match(::Actor) | ||
query { | ||
val movie = match(::Movie) | ||
create(actor `o-→` ::ActedIn `o-→` movie) | ||
movie | ||
}.call() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters