Thanks to visit codestin.com
Credit goes to github.com

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
48 changes: 46 additions & 2 deletions e2e/src/test/java/com/arcadedb/e2e/RemoteDatabaseJavaApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.remote.RemoteDatabase;
import com.arcadedb.schema.Schema;
import com.arcadedb.utility.CollectionUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -136,9 +137,11 @@ void createTypesAndDataWithSqlScripts() {

database.command("sqlscript",
"""
BEGIN;LET photo = CREATE VERTEX Photos SET id = "p5555", name = "download3.jpg";
BEGIN;
LET photo = CREATE VERTEX Photos SET id = "p5555", name = "download3.jpg";
LET user = SELECT FROM Users WHERE id = "u1111";
LET userEdge = CREATE EDGE HasUploaded FROM $user TO $photo SET kind = "User_Photos";
LET edgeType = 'HasUploaded';
LET userEdge = CREATE EDGE $edgeType FROM $user TO $photo SET kind = "User_Photos";
COMMIT RETRY 30;
RETURN $photo;""");

Expand All @@ -164,6 +167,47 @@ void renameTypeAndAliases() {

}

@Test
void createSchemaWithDynamicSqlScript() {
database.command("sqlscript", """
BEGIN;

LET vTypes = ['V1', 'V2', 'V3'];
FOREACH ($vType IN $vTypes) {
CREATE VERTEX TYPE $vType;
}

LET eTypes = ['E1', 'E2', 'E3'];
FOREACH ($eType IN $eTypes) {
CREATE EDGE TYPE $eType;
}

LET dTypes = ['D1', 'D2', 'D3'];
FOREACH ($dType IN $dTypes) {
CREATE DOCUMENT TYPE $dType ;
}

LET types = ['V1', 'V2', 'V3','E1', 'E2', 'E3','D1', 'D2', 'D3'];
FOREACH ($type IN $types) {
CREATE PROPERTY $type.id STRING;
CREATE INDEX ON $type (id) UNIQUE NULL_STRATEGY SKIP;
}
COMMIT;
""");

Schema schema = database.getSchema();
System.out.println("schema.toString() = " + schema.toString());
assertThat(schema.existsType("V1")).isTrue();
assertThat(schema.existsType("V2")).isTrue();
assertThat(schema.existsType("V3")).isTrue();
assertThat(schema.existsType("D1")).isTrue();
assertThat(schema.existsType("D2")).isTrue();
assertThat(schema.existsType("D3")).isTrue();
assertThat(schema.existsType("E1")).isTrue();
assertThat(schema.existsType("E2")).isTrue();
assertThat(schema.existsType("E3")).isTrue();
}

@Test
@Disabled
void testMultipleInsert() throws SQLException, ClassNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ private ResultSet executeInternal(final List<Statement> statements, final Comman
throw new CommandSQLParsingException("Found COMMIT statement without a BEGIN");
}

if (stm instanceof LetStatement letStatement)
if (stm instanceof LetStatement letStatement) {
scriptContext.declareScriptVariable(letStatement.getVariableName().getStringValue());
}
}

return new LocalResultSet(plan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import com.arcadedb.database.DatabaseInternal;
import com.arcadedb.database.Document;

import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Basic implementation of CommandContext interface that stores variables in a map. Supports parent/child context to build a tree
Expand Down Expand Up @@ -109,47 +112,62 @@ public Object getVariable(final String name) {
return getVariable(name, null);
}

public Object getVariable(String name, final Object iDefault) {
public Object getVariable(String name, final Object defaultValue) {
if (name == null)
return iDefault;
return defaultValue;

final Object result;
Object result;

if (name.startsWith("$"))
name = name.substring(1);

final String firstPart;
firstPart = name;
String fieldName = null;
if (name.contains(".")) {
final String[] parts = name.split("\\.");
if (parts.length > 2) {
throw new com.arcadedb.exception.CommandSQLParsingException(
"Nested property access is not supported in this context: " + name);
}
name = parts[0];
if (parts.length > 1) {
fieldName = parts[1];
}
}

if (firstPart.equalsIgnoreCase("CONTEXT"))
if (name.equalsIgnoreCase("CONTEXT"))
result = getVariables();
else if (firstPart.equalsIgnoreCase("PARENT"))
else if (name.equalsIgnoreCase("PARENT"))
result = parent;
else if (firstPart.equalsIgnoreCase("ROOT")) {
else if (name.equalsIgnoreCase("ROOT")) {
CommandContext p = this;
while (p.getParent() != null)
p = p.getParent();
result = p;

} else {
if (variables != null && variables.containsKey(firstPart))
result = variables.get(firstPart);
if (variables != null && variables.containsKey(name))
result = variables.get(name);
else {
if (child != null)
result = child.getVariable(firstPart);
result = child.getVariable(name);
else
result = getVariableFromParentHierarchy(firstPart);
result = getVariableFromParentHierarchy(name);
}
}

return result != null ? result : iDefault;
if (fieldName != null) {
if (result instanceof Result result1)
result = result1.getProperty(fieldName);
}
return result != null ? result : defaultValue;
}

protected Object getVariableFromParentHierarchy(final String varName) {
if (this.variables != null && variables.containsKey(varName)) {
return variables.get(varName);
protected Object getVariableFromParentHierarchy(final String name) {
if (this.variables != null && variables.containsKey(name)) {
return variables.get(name);
}
if (parent != null && parent instanceof BasicCommandContext context) {
return context.getVariableFromParentHierarchy(varName);
return context.getVariableFromParentHierarchy(name);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public interface CommandContext {

Object getVariablePath(String name, Object iDefault);

Object getVariable(String iName);
Object getVariable(String name);

Object getVariable(String iName, Object iDefaultValue);
Object getVariable(String name, Object iDefaultValue);

CommandContext setVariable(String iName, Object iValue);
CommandContext setVariable(String name, Object iValue);

CommandContext incrementVariable(String getNeighbors);

Map<String, Object> getVariables();

CommandContext getParent();

CommandContext setParent(CommandContext iParentContext);
CommandContext setParent(CommandContext parentContext);

CommandContext setChild(CommandContext context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.arcadedb.database.Database;
import com.arcadedb.exception.CommandSQLParsingException;
import com.arcadedb.index.TypeIndex;
import com.arcadedb.query.sql.parser.CreateEdgeStatement;
import com.arcadedb.query.sql.parser.Expression;
import com.arcadedb.query.sql.parser.Identifier;
Expand All @@ -29,7 +28,6 @@
import com.arcadedb.query.sql.parser.JsonArray;
import com.arcadedb.query.sql.parser.UpdateItem;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.EdgeType;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,7 +37,7 @@
*/
public class CreateEdgeExecutionPlanner {

protected Identifier targetClass;
protected Identifier targetType;
protected final Identifier targetBucketName;
protected final Expression leftExpression;
protected final Expression rightExpression;
Expand All @@ -48,7 +46,7 @@ public class CreateEdgeExecutionPlanner {
protected final InsertBody body;

public CreateEdgeExecutionPlanner(final CreateEdgeStatement statement) {
this.targetClass = statement.getTargetType() == null ? null : statement.getTargetType().copy();
this.targetType = statement.getTargetType() == null ? null : statement.getTargetType().copy();
this.targetBucketName = statement.getTargetBucketName() == null ? null : statement.getTargetBucketName().copy();
this.leftExpression = statement.getLeftExpression() == null ? null : statement.getLeftExpression().copy();
this.rightExpression = statement.getRightExpression() == null ? null : statement.getRightExpression().copy();
Expand All @@ -65,15 +63,20 @@ public CreateEdgeExecutionPlanner(final CreateEdgeStatement statement) {

public InsertExecutionPlan createExecutionPlan(final CommandContext context) {

if (targetClass == null) {
if (targetType.getStringValue().startsWith("$")) {
String variable = (String) context.getVariable(targetType.getStringValue());
targetType = new Identifier(variable);
}

if (targetType == null) {
if (targetBucketName == null) {
throw new CommandSQLParsingException("Missing target");
} else {
final Database db = context.getDatabase();
final DocumentType typez = db.getSchema()
.getTypeByBucketId((db.getSchema().getBucketByName(targetBucketName.getStringValue()).getFileId()));
if (typez != null) {
targetClass = new Identifier(typez.getName());
targetType = new Identifier(typez.getName());
} else {
throw new CommandSQLParsingException("Missing target");
}
Expand All @@ -100,10 +103,10 @@ public InsertExecutionPlan createExecutionPlan(final CommandContext context) {
// .findFirst()
// .orElse(null);
// } else
uniqueIndexName = null;
uniqueIndexName = null;

result.chain(
new CreateEdgesStep(targetClass, targetBucketName, uniqueIndexName, new Identifier("$__ARCADEDB_CREATE_EDGE_fromV"),
new CreateEdgesStep(targetType, targetBucketName, uniqueIndexName, new Identifier("$__ARCADEDB_CREATE_EDGE_fromV"),
new Identifier("$__ARCADEDB_CREATE_EDGE_toV"), unidirectional, ifNotExists, context));

handleSetFields(result, body, context);
Expand All @@ -118,8 +121,8 @@ private void handleGlobalLet(final InsertExecutionPlan result, final Identifier
}

private void handleCheckType(final InsertExecutionPlan result, final CommandContext context) {
if (targetClass != null) {
result.chain(new CheckIsEdgeTypeStep(targetClass.getStringValue(), context));
if (targetType != null) {
result.chain(new CheckIsEdgeTypeStep(targetType.getStringValue(), context));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

import com.arcadedb.exception.CommandSQLParsingException;
import com.arcadedb.query.sql.parser.CreateVertexStatement;
import com.arcadedb.query.sql.parser.Identifier;

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

/**
* @author Luigi Dell'Aquila (luigi.dellaquila-(at)-gmail.com)
*/
public class CreateVertexExecutionPlanner extends InsertExecutionPlanner {

public CreateVertexExecutionPlanner(final CreateVertexStatement statement) {
this.targetType = statement.getTargetType() == null ? null : statement.getTargetType().copy();
this.targetBucketName = statement.getTargetBucketName() == null ? null : statement.getTargetBucketName().copy();
Expand All @@ -40,6 +43,7 @@ public CreateVertexExecutionPlanner(final CreateVertexStatement statement) {

@Override
public InsertExecutionPlan createExecutionPlan(final CommandContext context) {

final InsertExecutionPlan prev = super.createExecutionPlan(context);
final List<ExecutionStep> steps = new ArrayList<>(prev.getSteps());
final InsertExecutionPlan result = new InsertExecutionPlan(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ private void handleLimit(final UpdateExecutionPlan plan, final CommandContext co
plan.chain(new LimitExecutionStep(limit, context));
}

private void handleTarget(final UpdateExecutionPlan result, final CommandContext context, final FromClause target,
private void handleTarget(final UpdateExecutionPlan result,
final CommandContext context,
final FromClause fromClause,
final WhereClause whereClause) {
final SelectStatement sourceStatement = new SelectStatement(-1);
sourceStatement.setTarget(target);
sourceStatement.setTarget(fromClause);
sourceStatement.setWhereClause(whereClause);
final SelectExecutionPlanner planner = new SelectExecutionPlanner(sourceStatement);
result.chain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import com.arcadedb.query.sql.parser.SelectStatement;
import com.arcadedb.query.sql.parser.UpdateItem;

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

/**
* Created by luigidellaquila on 08/08/16.
Expand All @@ -55,14 +56,20 @@ public InsertExecutionPlanner(final InsertStatement statement) {
}

public InsertExecutionPlan createExecutionPlan(final CommandContext context) {

if (targetType != null && targetType.getStringValue().startsWith("$")) {
String variable = (String) context.getVariable(targetType.getStringValue());
targetType = new Identifier(variable);
}

final InsertExecutionPlan result = new InsertExecutionPlan(context);

if (selectStatement != null) {
handleInsertSelect(result, this.selectStatement, context);
} else {
handleCreateRecord(result, this.insertBody, context);
}
handleTargetClass(result, targetType, context);
handleTargetType(result, targetType, context);
handleSetFields(result, insertBody, context);
if (targetBucket != null) {
String name = targetBucket.getBucketName();
Expand Down Expand Up @@ -113,9 +120,9 @@ private void handleSetFields(final InsertExecutionPlan result, final InsertBody
}
}

private void handleTargetClass(final InsertExecutionPlan result, final Identifier targetClass, final CommandContext context) {
if (targetClass != null)
result.chain(new SetDocumentStepStep(targetClass, context));
private void handleTargetType(final InsertExecutionPlan result, final Identifier targetType, final CommandContext context) {
if (targetType != null)
result.chain(new SetDocumentStepStep(targetType, context));
}

private void handleCreateRecord(final InsertExecutionPlan result, final InsertBody body, final CommandContext context) {
Expand Down
Loading
Loading