Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

import com.arcadedb.database.DatabaseInternal;
import com.arcadedb.query.cypher.CypherQueryEngine;
import com.arcadedb.query.graphql.GraphQLQueryEngine;
import com.arcadedb.query.gremlin.GremlinQueryEngine;
import com.arcadedb.query.mongo.MongoQueryEngine;
import com.arcadedb.query.sql.SQLQueryEngine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class QueryEngineManager {
private final Map<String, QueryEngine.QueryEngineFactory> implementations = new HashMap<>();
Expand All @@ -34,6 +32,7 @@ public QueryEngineManager() {
register(new GremlinQueryEngine.GremlinQueryEngineFactory());
register(new CypherQueryEngine.CypherQueryEngineFactory());
register(new MongoQueryEngine.MongoQueryEngineFactory());
register(new GraphQLQueryEngine.GraphQLQueryEngineFactory());
}

public void register(final QueryEngine.QueryEngineFactory impl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arcadedb.query.graphql;

import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseInternal;
import com.arcadedb.exception.CommandExecutionException;
import com.arcadedb.exception.QueryParsingException;
import com.arcadedb.log.LogManager;
import com.arcadedb.query.QueryEngine;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.utility.FileUtils;

import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;

public class GraphQLQueryEngine implements QueryEngine {
private static final String ENGINE_NAME = "graphql-engine";
private final Object graphQLSchema;

public static class GraphQLQueryEngineFactory implements QueryEngineFactory {
private static Boolean available = null;
private static Class<?> graphQLSchemaClass;

@Override
public boolean isAvailable() {
if (available == null) {
try {
graphQLSchemaClass = Class.forName("com.arcadedb.graphql.schema.GraphQLSchema");
available = true;
} catch (ClassNotFoundException e) {
available = false;
}
}
return available;
}

@Override
public String getLanguage() {
return "graphql";
}

@Override
public QueryEngine getInstance(final DatabaseInternal database) {
Object engine = database.getWrappers().get(ENGINE_NAME);
if (engine != null)
return (GraphQLQueryEngine) engine;

try {
engine = new GraphQLQueryEngine(graphQLSchemaClass.getConstructor(Database.class).newInstance(database));
database.setWrapper(ENGINE_NAME, engine);
return (GraphQLQueryEngine) engine;

} catch (Exception e) {
LogManager.instance().log(this, Level.SEVERE, "Error on initializing GraphQL query engine", e);
throw new QueryParsingException("Error on initializing GraphQL query engine", e);
}
}
}

protected GraphQLQueryEngine(final Object graphQLSchema) {
this.graphQLSchema = graphQLSchema;
}

@Override
public AnalyzedQuery analyze(String query) {
return new AnalyzedQuery() {
@Override
public boolean isIdempotent() {
return false;
}

@Override
public boolean isDDL() {
return false;
}
};
}

@Override
public ResultSet query(final String query, final Map<String, Object> parameters) {
return command(query, parameters);
}

@Override
public ResultSet query(final String query, final Object... parameters) {
return command(query, parameters);
}

@Override
public ResultSet command(final String query, final Map<String, Object> parameters) {
try {
final ResultSet resultSet = (ResultSet) GraphQLQueryEngineFactory.graphQLSchemaClass.getMethod("execute", String.class).invoke(graphQLSchema, query);

return resultSet;
} catch (InvocationTargetException e) {
throw new CommandExecutionException("Error on executing GraphQL command:\n" + FileUtils.printWithLineNumbers(query), e.getTargetException());
} catch (Exception e) {
throw new QueryParsingException("Error on executing GraphQL query:\n" + FileUtils.printWithLineNumbers(query), e);
}
}

@Override
public ResultSet command(final String query, final Object... parameters) {
final Map<String, Object> map = new HashMap<>(parameters.length / 2);
for (int i = 0; i < parameters.length; i += 2)
map.put((String) parameters[i], parameters[i + 1]);
return command(query, map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public Optional<Document> getElement() {

@Override
public Map<String, Object> toMap() {
return content;
return element != null ? element.toMap() : content;
}

@Override
Expand Down
31 changes: 31 additions & 0 deletions engine/src/main/java/com/arcadedb/utility/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,35 @@ public static String escapeHTML(final String s) {
}
return out.toString();
}

public static String printWithLineNumbers(final String text) {
// COUNT TOTAL LINES FIRST
int totalLines = 1;
for (int i = 0; i < text.length(); i++) {
final Character current = text.charAt(i);
if (current == '\n')
++totalLines;
}

final int maxLineDigits = String.valueOf(totalLines).length();

final StringBuilder result = new StringBuilder("1: ");

int line = 1;
for (int i = 0; i < text.length(); i++) {
final Character current = text.charAt(i);
final Character next = i + 1 < text.length() ? text.charAt(i) : null;
if (current == '\n') {
++line;
result.append(String.format("\n%-" + maxLineDigits + "d: ", line));
} else if (current == '\r' && (next == null || next == '\n')) {
++line;
result.append(String.format("\n%-" + maxLineDigits + "d: ", line));
++i;
} else
result.append(current);
}

return result.toString();
}
}
81 changes: 81 additions & 0 deletions graphql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.arcadedb</groupId>
<artifactId>arcadedb-parent</artifactId>
<version>21.12.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>arcadedb-graphql</artifactId>
<packaging>jar</packaging>

<properties>
<ph-javacc-maven-plugin.version>4.1.4</ph-javacc-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>com.arcadedb</groupId>
<artifactId>arcadedb-server</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.arcadedb</groupId>
<artifactId>arcadedb-server</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.helger.maven</groupId>
<artifactId>ph-javacc-maven-plugin</artifactId>
<version>${ph-javacc-maven-plugin.version}</version>
<executions>
<execution>
<id>jjc1</id>
<phase>generate-sources</phase>
<goals>
<goal>jjtree-javacc</goal>
<!--<goal>jjdoc</goal>-->

</goals>
<configuration>
<jdkVersion>1.8</jdkVersion>
<sourceDirectory>${basedir}/src/main/grammar</sourceDirectory>
<interimDirectory>${basedir}/src/main/java</interimDirectory>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
</configuration>
</execution>

</executions>
</plugin>
</plugins>
</build>

</project>
Loading