mirror of
https://github.com/maxkratz/edgedb-java.git
synced 2024-09-16 16:27:58 +00:00
Restructure examples (#7)
* restructure examples * remove hardcoded path and clean examples * for comprehensions
This commit is contained in:
parent
485cf29335
commit
b9ddbbbb55
38 changed files with 452 additions and 162 deletions
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -4,6 +4,11 @@ build/
|
|||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
target/
|
||||
.bsp/
|
||||
examples/scala-examples/lib/*
|
||||
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/*
|
||||
*.iws
|
||||
|
@ -36,4 +41,4 @@ bin/
|
|||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
|
|
25
examples/java-examples/build.gradle
Normal file
25
examples/java-examples/build.gradle
Normal file
|
@ -0,0 +1,25 @@
|
|||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group 'com.edgedb'
|
||||
version '0.0.1-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':src:driver')
|
||||
|
||||
implementation 'ch.qos.logback:logback-classic:1.4.5'
|
||||
implementation 'ch.qos.logback:logback-core:1.4.5'
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
|
||||
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
|
@ -43,4 +43,4 @@ public class AbstractTypes implements Example {
|
|||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,4 +18,4 @@ public final class BasicQueryFunctions implements Example {
|
|||
.thenCompose(v -> client.queryRequiredSingle(String.class, "select 'Hello, Java!'"))
|
||||
.thenAccept(result -> logger.info("queryRequiredSingle result: {}", result));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -35,4 +35,4 @@ public final class CustomDeserializer implements Example {
|
|||
.thenCompose(v -> client.queryRequiredSingle(Person.class, "select Person { name, age } filter .name = 'Example'"))
|
||||
.thenAccept(person -> logger.info("Person result: {person}"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,4 +6,4 @@ import java.util.concurrent.CompletionStage;
|
|||
|
||||
public interface Example {
|
||||
CompletionStage<Void> run(EdgeDBClient client);
|
||||
}
|
||||
}
|
|
@ -25,4 +25,4 @@ public final class GlobalsAndConfig implements Example {
|
|||
return configuredClient.queryRequiredSingle(UUID.class, "select global current_user_id")
|
||||
.thenAccept(result -> logger.info("current_user_id global: {}", result));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -33,4 +33,4 @@ public final class JsonResults implements Example {
|
|||
})
|
||||
.thenAccept(person -> logger.info("People deserialized from JSON: {}", (Object[]) person));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,9 +21,9 @@ public final class LinkProperties implements Example {
|
|||
|
||||
private static final String INSERT_QUERY =
|
||||
"with a := (insert Person { name := 'Person A', age := 20 } unless conflict on .name)," +
|
||||
"b := (insert Person { name := 'Person B', age := 21 } unless conflict on .name)," +
|
||||
"c := (insert Person { name := 'Person C', age := 22, friends := b } unless conflict on .name)" +
|
||||
"insert Person { name := 'Person D', age := 23, friends := { a, b, c }, best_friend := c } unless conflict on .name";
|
||||
"b := (insert Person { name := 'Person B', age := 21 } unless conflict on .name)," +
|
||||
"c := (insert Person { name := 'Person C', age := 22, friends := b } unless conflict on .name)" +
|
||||
"insert Person { name := 'Person D', age := 23, friends := { a, b, c }, best_friend := c } unless conflict on .name";
|
||||
|
||||
@Override
|
||||
public CompletionStage<Void> run(EdgeDBClient client) {
|
||||
|
@ -35,4 +35,4 @@ public final class LinkProperties implements Example {
|
|||
))
|
||||
.thenAccept(result -> logger.info("Person with links: {}", result));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,31 +9,19 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class Main {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Main.class);
|
||||
|
||||
public static void main(String[] args) throws IOException, EdgeDBException {
|
||||
var argsList = Arrays.asList(args);
|
||||
|
||||
var client = new EdgeDBClient(EdgeDBClientConfig.builder()
|
||||
.withNamingStrategy(NamingStrategy.snakeCase())
|
||||
.useFieldSetters(true)
|
||||
.build()
|
||||
);
|
||||
).withModule("examples");
|
||||
|
||||
// use the example module
|
||||
var exampleClient = client.withModule("examples");
|
||||
|
||||
if(argsList.contains("java")) {
|
||||
runJavaExamples(exampleClient);
|
||||
}
|
||||
|
||||
if(argsList.contains("kotlin")) {
|
||||
com.edgedb.examples.KotlinMain.Companion.runExamples(exampleClient);
|
||||
}
|
||||
runJavaExamples(client);
|
||||
|
||||
logger.info("Examples complete");
|
||||
}
|
|
@ -29,4 +29,4 @@ public final class QueryResults implements Example {
|
|||
.thenCompose(v -> client.queryRequiredSingle(Person.class, "select Person { name, age } filter .name = 'Example'"))
|
||||
.thenAccept(result -> logger.info("Person returned from query: {}", result));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,4 +18,4 @@ public final class Transactions implements Example {
|
|||
logger.info("Result from transaction: {}", result);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,32 +4,20 @@ plugins {
|
|||
}
|
||||
|
||||
group 'com.edgedb'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceSets.main {
|
||||
java.srcDirs("src/main/java", "src/main/kotlin")
|
||||
}
|
||||
version '0.0.1-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven {
|
||||
url "https://oss.sonatype.org/content/repositories/snapshots"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':src:driver')
|
||||
implementation 'ch.qos.logback:logback-classic:1.4.5'
|
||||
implementation 'ch.qos.logback:logback-core:1.4.5'
|
||||
implementation 'org.jooq:joou:0.9.4'
|
||||
implementation 'org.reflections:reflections:0.10.2'
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
|
||||
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
|
||||
implementation 'ch.qos.logback:logback-classic:1.4.5'
|
||||
implementation 'ch.qos.logback:logback-core:1.4.5'
|
||||
}
|
||||
|
||||
jar.duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
|
@ -39,7 +27,6 @@ test {
|
|||
useJUnitPlatform()
|
||||
}
|
||||
compileKotlin {
|
||||
destinationDirectory = compileJava.destinationDirectory
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
|
@ -4,11 +4,10 @@ import com.edgedb.driver.EdgeDBClient
|
|||
import com.edgedb.driver.annotations.EdgeDBType
|
||||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.concurrent.CompletionStage
|
||||
|
||||
class KotlinAbstractTypes : KotlinExample {
|
||||
class AbstractTypes : Example {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinExample::class.java)!!
|
||||
private val logger = LoggerFactory.getLogger(AbstractTypes::class.java)!!
|
||||
}
|
||||
|
||||
@EdgeDBType
|
||||
|
@ -28,7 +27,7 @@ class KotlinAbstractTypes : KotlinExample {
|
|||
|
||||
override suspend fun runAsync(client: EdgeDBClient) {
|
||||
client.execute(
|
||||
"""insert Movie {
|
||||
"""insert Movie {
|
||||
title := "The Matrix",
|
||||
release_year := 1999
|
||||
} unless conflict on .title;
|
||||
|
@ -39,8 +38,8 @@ class KotlinAbstractTypes : KotlinExample {
|
|||
).await()
|
||||
|
||||
val results = client.query(
|
||||
Media::class.java,
|
||||
"""select Media {
|
||||
Media::class.java,
|
||||
"""select Media {
|
||||
title,
|
||||
[is Movie].release_year,
|
||||
[is Show].seasons
|
||||
|
@ -50,13 +49,13 @@ class KotlinAbstractTypes : KotlinExample {
|
|||
for(result in results) {
|
||||
if(result is Show) {
|
||||
logger.info(
|
||||
"Got show: title: {}, seasons: {}",
|
||||
result.title, result.seasons
|
||||
"Got show: title: {}, seasons: {}",
|
||||
result.title, result.seasons
|
||||
)
|
||||
} else if(result is Movie) {
|
||||
logger.info(
|
||||
"Got movie: title: {}, release year: {}",
|
||||
result.title, result.releaseYear
|
||||
"Got movie: title: {}, release year: {}",
|
||||
result.title, result.releaseYear
|
||||
)
|
||||
}
|
||||
}
|
|
@ -4,9 +4,9 @@ import com.edgedb.driver.EdgeDBClient
|
|||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class KotlinBasicQueries : KotlinExample {
|
||||
class BasicQueries : Example {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinBasicQueries::class.java)!!
|
||||
private val logger = LoggerFactory.getLogger(BasicQueries::class.java)!!
|
||||
}
|
||||
|
||||
override suspend fun runAsync(client: EdgeDBClient) {
|
|
@ -7,23 +7,23 @@ import com.edgedb.driver.annotations.EdgeDBType
|
|||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class KotlinCustomDeserializer : KotlinExample {
|
||||
class CustomDeserializer : Example {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinCustomDeserializer::class.java)!!
|
||||
private val logger = LoggerFactory.getLogger(CustomDeserializer::class.java)!!
|
||||
}
|
||||
|
||||
@EdgeDBType
|
||||
data class Person
|
||||
@EdgeDBDeserializer
|
||||
constructor (
|
||||
@EdgeDBName("name")
|
||||
val name: String,
|
||||
@EdgeDBName("age")
|
||||
val age: Long
|
||||
) {
|
||||
init {
|
||||
logger.info("Custom deserializer called with: name: {}, age: {}", name, age)
|
||||
}
|
||||
@EdgeDBDeserializer
|
||||
constructor (
|
||||
@EdgeDBName("name")
|
||||
val name: String,
|
||||
@EdgeDBName("age")
|
||||
val age: Long
|
||||
) {
|
||||
init {
|
||||
logger.info("Custom deserializer called with: name: {}, age: {}", name, age)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun runAsync(client: EdgeDBClient) {
|
|
@ -2,6 +2,6 @@ package com.edgedb.examples
|
|||
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
|
||||
interface KotlinExample {
|
||||
interface Example {
|
||||
suspend fun runAsync(client: EdgeDBClient)
|
||||
}
|
|
@ -6,19 +6,19 @@ import org.slf4j.LoggerFactory
|
|||
import java.time.Duration
|
||||
import java.util.*
|
||||
|
||||
class KotlinGlobalsAndConfig : KotlinExample {
|
||||
class GlobalsAndConfig : Example {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinExample::class.java)!!
|
||||
private val logger = LoggerFactory.getLogger(GlobalsAndConfig::class.java)!!
|
||||
}
|
||||
|
||||
override suspend fun runAsync(client: EdgeDBClient) {
|
||||
val configuredClient = client
|
||||
.withConfig { config -> config
|
||||
.withIdleTransactionTimeout(Duration.ZERO)
|
||||
.applyAccessPolicies(true)
|
||||
.withIdleTransactionTimeout(Duration.ZERO)
|
||||
.applyAccessPolicies(true)
|
||||
}
|
||||
.withGlobals(mapOf(
|
||||
"current_user_id" to UUID.randomUUID()
|
||||
"current_user_id" to UUID.randomUUID()
|
||||
))
|
||||
|
||||
val currentUserId = configuredClient.queryRequiredSingle(
|
|
@ -6,10 +6,9 @@ import com.edgedb.driver.annotations.EdgeDBType
|
|||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class KotlinLinkProperties : KotlinExample {
|
||||
|
||||
class LinkProperties : Example {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinLinkProperties::class.java)
|
||||
private val logger = LoggerFactory.getLogger(LinkProperties::class.java)
|
||||
private const val INSERT_QUERY = """
|
||||
with
|
||||
a := (insert Person { name := 'Person A', age := 20 } unless conflict on .name),
|
|
@ -0,0 +1,41 @@
|
|||
package com.edgedb.examples
|
||||
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import com.edgedb.driver.EdgeDBClientConfig
|
||||
import com.edgedb.driver.namingstrategies.NamingStrategy
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
object Main {
|
||||
private val logger = LoggerFactory.getLogger(Main::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val client = EdgeDBClient(EdgeDBClientConfig.builder()
|
||||
.withNamingStrategy(NamingStrategy.snakeCase())
|
||||
.useFieldSetters(true)
|
||||
.build()
|
||||
).withModule("examples")
|
||||
|
||||
val examples = listOf(
|
||||
AbstractTypes(),
|
||||
BasicQueries(),
|
||||
CustomDeserializer(),
|
||||
GlobalsAndConfig(),
|
||||
LinkProperties(),
|
||||
Transactions()
|
||||
)
|
||||
|
||||
runBlocking {
|
||||
for (example in examples) {
|
||||
logger.info("Running Kotlin example {}...", example)
|
||||
try {
|
||||
example.runAsync(client)
|
||||
logger.info("Kotlin example {} complete!", example)
|
||||
} catch (x: Exception) {
|
||||
logger.error("Failed to run Kotlin example {}", example, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,9 +4,9 @@ import com.edgedb.driver.EdgeDBClient
|
|||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class KotlinTransactions : KotlinExample {
|
||||
class Transactions : Example {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinTransactions::class.java)
|
||||
private val logger = LoggerFactory.getLogger(Transactions::class.java)
|
||||
}
|
||||
|
||||
override suspend fun runAsync(client: EdgeDBClient) {
|
21
examples/scala-examples/build.sbt
Normal file
21
examples/scala-examples/build.sbt
Normal file
|
@ -0,0 +1,21 @@
|
|||
ThisBuild / version := "0.1.0-SNAPSHOT"
|
||||
|
||||
ThisBuild / scalaVersion := "3.1.3"
|
||||
|
||||
//resolvers += Resolver.file("my-test-repo", file("test"))
|
||||
|
||||
libraryDependencies ++= Seq(
|
||||
"com.edgedb" % "driver" % "0.0.1" from "file:///" + System.getProperty("user.dir") + "/lib/com.edgedb.driver-0.0.1-SNAPSHOT.jar",
|
||||
"ch.qos.logback" % "logback-classic" % "1.4.7",
|
||||
"ch.qos.logback" % "logback-core" % "1.4.7",
|
||||
"com.fasterxml.jackson.core" % "jackson-databind" % "2.15.1",
|
||||
"io.netty" % "netty-all" % "4.1.89.Final",
|
||||
"org.jooq" % "joou" % "0.9.4",
|
||||
"org.reflections" % "reflections" % "0.10.2"
|
||||
)
|
||||
|
||||
lazy val root = (project in file("."))
|
||||
.settings(
|
||||
name := "scala-examples",
|
||||
idePackagePrefix := Some("com.edgedb.examples")
|
||||
)
|
1
examples/scala-examples/project/build.properties
Normal file
1
examples/scala-examples/project/build.properties
Normal file
|
@ -0,0 +1 @@
|
|||
sbt.version = 1.9.0
|
1
examples/scala-examples/project/plugins.sbt
Normal file
1
examples/scala-examples/project/plugins.sbt
Normal file
|
@ -0,0 +1 @@
|
|||
addSbtPlugin("org.jetbrains.scala" % "sbt-ide-settings" % "1.1.1")
|
62
examples/scala-examples/src/main/scala/AbstractTypes.scala
Normal file
62
examples/scala-examples/src/main/scala/AbstractTypes.scala
Normal file
|
@ -0,0 +1,62 @@
|
|||
package com.edgedb.examples
|
||||
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import com.edgedb.driver.annotations.EdgeDBType
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
import scala.concurrent.{Await, ExecutionContext, Future}
|
||||
|
||||
object AbstractTypes:
|
||||
@EdgeDBType
|
||||
abstract class Media:
|
||||
var title: String = _
|
||||
|
||||
@EdgeDBType
|
||||
class Movie extends Media:
|
||||
var releaseYear: Long = _
|
||||
|
||||
@EdgeDBType
|
||||
class Show extends Media:
|
||||
var seasons: Long = _
|
||||
|
||||
class AbstractTypes extends Example:
|
||||
private val logger = LoggerFactory.getLogger(classOf[AbstractTypes])
|
||||
|
||||
import scala.jdk.FutureConverters._
|
||||
import AbstractTypes._
|
||||
|
||||
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
for {
|
||||
result <- client.query(
|
||||
classOf[Media],
|
||||
"""
|
||||
| insert Movie {
|
||||
| title := "The Matrix",
|
||||
| release_year := 1999
|
||||
| } unless conflict on .title;
|
||||
| insert Show {
|
||||
| title := "The Office",
|
||||
| seasons := 9
|
||||
| } unless conflict on .title;
|
||||
| select Media {
|
||||
| title,
|
||||
| [is Movie].release_year,
|
||||
| [is Show].seasons
|
||||
|}""".stripMargin).asScala
|
||||
} yield {
|
||||
result.forEach {
|
||||
case movie: Movie =>
|
||||
logger.info(
|
||||
"Got movie: title: {}, release year: {}",
|
||||
movie.title, movie.releaseYear
|
||||
)
|
||||
case show: Show =>
|
||||
logger.info(
|
||||
"Got show: title: {}, seasons: {}",
|
||||
show.title, show.seasons
|
||||
)
|
||||
case unknown => logger.warn("Got unknown result type: {}", unknown)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.edgedb.examples
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.jdk.FutureConverters.*
|
||||
class BasicQueryFunctions extends Example:
|
||||
private val logger = LoggerFactory.getLogger(classOf[BasicQueryFunctions])
|
||||
|
||||
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
for {
|
||||
queryResult <- client.query(classOf[String], "SELECT 'Hello, Scala!'").asScala
|
||||
querySingleResult <- client.querySingle(classOf[String], "SELECT 'Hello, Scala!'").asScala
|
||||
queryRequiredSingleResult <- client.queryRequiredSingle(classOf[String], "SELECT 'Hello, Scala!'").asScala
|
||||
}
|
||||
yield (queryResult, querySingleResult, queryRequiredSingleResult) {
|
||||
logger.info("'query' method result: {}", queryResult)
|
||||
logger.info("'querySingle' method result: {}", querySingleResult)
|
||||
logger.info("'queryRequiredSingle' method result: {}", queryRequiredSingleResult)
|
||||
0
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.edgedb.examples
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import com.edgedb.driver.annotations.{EdgeDBDeserializer, EdgeDBName, EdgeDBType}
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.jdk.FutureConverters.*
|
||||
|
||||
object CustomDeserializer:
|
||||
private val logger = LoggerFactory.getLogger(classOf[CustomDeserializer])
|
||||
|
||||
@EdgeDBType
|
||||
class Person @EdgeDBDeserializer()
|
||||
(
|
||||
@EdgeDBName("name")
|
||||
val name: String,
|
||||
@EdgeDBName("age")
|
||||
val age: Long
|
||||
) {
|
||||
logger.info("Custom deserializer called")
|
||||
}
|
||||
|
||||
class CustomDeserializer extends Example:
|
||||
import CustomDeserializer._
|
||||
|
||||
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
for(
|
||||
result <- client.queryRequiredSingle(
|
||||
classOf[Person],
|
||||
"""
|
||||
| insert Person { name := 'Example', age := 123 } unless conflict on .name;
|
||||
| select Person { name, age } filter .name = 'Example'
|
||||
|""".stripMargin
|
||||
).asScala
|
||||
) yield {
|
||||
logger.info("Got person: name: {}, age: {}", result.name, result.age)
|
||||
}
|
||||
}
|
8
examples/scala-examples/src/main/scala/Example.scala
Normal file
8
examples/scala-examples/src/main/scala/Example.scala
Normal file
|
@ -0,0 +1,8 @@
|
|||
package com.edgedb.examples
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
|
||||
trait Example {
|
||||
def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit];
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.edgedb.examples
|
||||
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer
|
||||
import com.edgedb.driver.state.Config
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
import java.time.Duration
|
||||
import java.util.UUID
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.jdk.CollectionConverters.MapHasAsJava
|
||||
import scala.jdk.FutureConverters.*
|
||||
|
||||
class GlobalsAndConfig extends Example:
|
||||
private val logger = LoggerFactory.getLogger(classOf[GlobalsAndConfig])
|
||||
|
||||
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
val configuredClient = client.withConfig(Config.builder()
|
||||
.withQueryExecutionTimeout(Duration.ZERO)
|
||||
.applyAccessPolicies(true)
|
||||
.build()
|
||||
).withGlobals(
|
||||
Map[String, Object](
|
||||
"current_user_id" -> UUID.randomUUID()
|
||||
).asJava
|
||||
)
|
||||
|
||||
for(
|
||||
result <- configuredClient.queryRequiredSingle(
|
||||
classOf[UUID],
|
||||
"SELECT GLOBAL current_user_id"
|
||||
).asScala
|
||||
) yield {
|
||||
logger.info("Current user ID: {}", result)
|
||||
}
|
||||
}
|
62
examples/scala-examples/src/main/scala/LinkProperties.scala
Normal file
62
examples/scala-examples/src/main/scala/LinkProperties.scala
Normal file
|
@ -0,0 +1,62 @@
|
|||
package com.edgedb.examples
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import com.edgedb.driver.annotations.{EdgeDBLinkType, EdgeDBType}
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
import java.util
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.jdk.FutureConverters.*
|
||||
|
||||
object LinkProperties:
|
||||
@EdgeDBType
|
||||
class Person:
|
||||
var name: String = _
|
||||
var age: Long = _
|
||||
var bestFriend: Person = _
|
||||
|
||||
@EdgeDBLinkType(classOf[Person])
|
||||
var friends: util.Collection[Person] = _
|
||||
|
||||
class LinkProperties extends Example:
|
||||
private val logger = LoggerFactory.getLogger(classOf[LinkProperties])
|
||||
import LinkProperties.Person
|
||||
|
||||
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
for(
|
||||
result <- client.queryRequiredSingle(
|
||||
classOf[Person],
|
||||
"""
|
||||
| with
|
||||
| a := (insert Person { name := 'Person A', age := 20 } unless conflict on .name),
|
||||
| b := (insert Person { name := 'Person B', age := 21 } unless conflict on .name),
|
||||
| c := (insert Person { name := 'Person C', age := 22, friends := b } unless conflict on .name)
|
||||
| insert Person {
|
||||
| name := 'Person D',
|
||||
| age := 23,
|
||||
| friends := {
|
||||
| a,
|
||||
| b,
|
||||
| c
|
||||
| },
|
||||
| best_friend := c
|
||||
| } unless conflict on .name;
|
||||
| select Person {
|
||||
| name,
|
||||
| age,
|
||||
| friends: {
|
||||
| name,
|
||||
| age,
|
||||
| friends
|
||||
| },
|
||||
| best_friend: {
|
||||
| name,
|
||||
| age,
|
||||
| friends
|
||||
| }
|
||||
| } filter .name = 'Person D'
|
||||
|""".stripMargin
|
||||
).asScala
|
||||
) yield {
|
||||
logger.info("Person with links: {}", result)
|
||||
}
|
||||
}
|
48
examples/scala-examples/src/main/scala/Main.scala
Normal file
48
examples/scala-examples/src/main/scala/Main.scala
Normal file
|
@ -0,0 +1,48 @@
|
|||
package com.edgedb.examples
|
||||
|
||||
import com.edgedb.driver.namingstrategies.NamingStrategy
|
||||
import com.edgedb.driver.{EdgeDBClient, EdgeDBClientConfig, Transaction}
|
||||
import org.slf4j.{Logger, LoggerFactory}
|
||||
|
||||
import scala.concurrent.duration.Duration
|
||||
import scala.concurrent.{Await, ExecutionContext, Future, blocking}
|
||||
import scala.util.control.NonFatal
|
||||
import ExecutionContext.Implicits.global
|
||||
|
||||
@main
|
||||
def main(): Unit = {
|
||||
val logger = LoggerFactory.getLogger("Main")
|
||||
|
||||
val client = new EdgeDBClient(EdgeDBClientConfig.builder
|
||||
.withNamingStrategy(NamingStrategy.snakeCase)
|
||||
.useFieldSetters(true)
|
||||
.build
|
||||
).withModule("examples")
|
||||
|
||||
val examples = List(
|
||||
AbstractTypes(),
|
||||
BasicQueryFunctions(),
|
||||
CustomDeserializer(),
|
||||
GlobalsAndConfig(),
|
||||
LinkProperties(),
|
||||
Transactions()
|
||||
)
|
||||
|
||||
for (example <- examples)
|
||||
Await.ready(runExample(logger, client, example), Duration.Inf)
|
||||
|
||||
logger.info("Examples complete!")
|
||||
}
|
||||
|
||||
private def runExample(logger: Logger, client: EdgeDBClient, example: Example)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
logger.info("Running Scala example {}...", example)
|
||||
example.run(client)
|
||||
.map({ * =>
|
||||
logger.info(s"Scala example {} complete!", example)
|
||||
})
|
||||
.recoverWith { e =>
|
||||
logger.error(s"Failed to run Scala example {}", example, e)
|
||||
|
||||
Future {}
|
||||
}
|
||||
}
|
18
examples/scala-examples/src/main/scala/Transactions.scala
Normal file
18
examples/scala-examples/src/main/scala/Transactions.scala
Normal file
|
@ -0,0 +1,18 @@
|
|||
package com.edgedb.examples
|
||||
import com.edgedb.driver.{EdgeDBClient, Transaction}
|
||||
import org.slf4j.LoggerFactory
|
||||
import scala.jdk.FutureConverters.*
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
class Transactions extends Example {
|
||||
private val logger = LoggerFactory.getLogger(classOf[Transactions])
|
||||
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
|
||||
client.transaction((tx: Transaction) => {
|
||||
logger.info("In transaction")
|
||||
tx.queryRequiredSingle(classOf[String], "select 'Result from Transaction'")
|
||||
}).asScala.map{ result =>
|
||||
logger.info("Result from transaction: {}", result)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
module com.edgedb.examples {
|
||||
requires com.edgedb.driver;
|
||||
requires org.slf4j;
|
||||
requires org.jooq.joou;
|
||||
requires org.reflections;
|
||||
requires com.fasterxml.jackson.core;
|
||||
requires com.fasterxml.jackson.databind;
|
||||
|
||||
requires kotlin.stdlib;
|
||||
requires kotlinx.coroutines.core;
|
||||
|
||||
exports com.edgedb.examples;
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package com.edgedb.examples
|
||||
|
||||
import com.edgedb.driver.EdgeDBClient
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class KotlinMain {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(KotlinMain::class.java)
|
||||
|
||||
fun runExamples(client: EdgeDBClient) {
|
||||
val examples = listOf(
|
||||
KotlinAbstractTypes(),
|
||||
KotlinBasicQueries(),
|
||||
KotlinCustomDeserializer(),
|
||||
KotlinGlobalsAndConfig(),
|
||||
KotlinLinkProperties(),
|
||||
KotlinTransactions()
|
||||
)
|
||||
|
||||
runBlocking {
|
||||
for(example in examples) {
|
||||
logger.info("Running Kotlin example {}...", example)
|
||||
try {
|
||||
example.runAsync(client)
|
||||
logger.info("Kotlin example {} complete!", example)
|
||||
} catch (x: Exception) {
|
||||
logger.error("Failed to run Kotlin example {}", example, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!-- Log level: OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL -->
|
||||
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>%d{HH:mm:ss} [%thread] %class{0} %M : %msg%n</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
<appender name="APPEND" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="STDOUT" />
|
||||
<includeCallerData>true</includeCallerData>
|
||||
</appender>
|
||||
|
||||
<logger name="nio2.ssl.SSLEngineAutomat" level="ALL" additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
<logger name="nio2.ssl.SSLAsynchronousSocketChannel" level="ALL" additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
<logger name="nio2.ssl.SSLAsynchronousSocketChannelLayer" level="ALL" additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
<logger name="nio2.ssl.SSLAsynchronousSocketChannelImpl" level="ALL" additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
<logger name="nio2.ssl.SSLAsynchronousChannelGroup" level="ALL" additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
<root level="ALL">
|
||||
<appender-ref ref="APPEND" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,6 +1,7 @@
|
|||
rootProject.name = 'edgedb-java'
|
||||
include 'examples'
|
||||
include 'src:driver'
|
||||
include 'tools'
|
||||
include 'tools:testgen'
|
||||
include 'examples:java-examples'
|
||||
include 'examples:kotlin-examples'
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import java.nio.file.Paths
|
||||
|
||||
dependencies {
|
||||
api "com.fasterxml.jackson.core:jackson-core:$jackson_version"
|
||||
api "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
|
||||
|
@ -30,6 +32,14 @@ jar {
|
|||
'Automatic-Module-Name': 'com.edgedb.driver'
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register('copyJarToBin') {
|
||||
copy {
|
||||
from jar
|
||||
into Paths.get(project.rootDir.toString(), 'examples', 'scala-examples', 'lib')
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
|
|
|
@ -342,7 +342,6 @@ public class TypeDeserializerInfo<T> {
|
|||
|
||||
// if there's a set method that isn't ignored, with the same type, use it.
|
||||
var setMethod = setters.get(field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1));
|
||||
|
||||
if(setMethod == null) {
|
||||
setMethod = setters.get(field.getName());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue