Skip to content

Commit 3b4c779

Browse files
authored
Merge pull request ArcadeData#94 from ArcadeData/daily
Studio: added drop property and index. Fixed reload of schema (now th…
2 parents 41f1dad + 6564be6 commit 3b4c779

7 files changed

Lines changed: 169 additions & 82 deletions

File tree

engine/src/main/grammar/SQLGrammar.jjt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4557,7 +4557,7 @@ ExportDatabaseStatement ExportDatabaseStatement():
45574557
(
45584558
<EXPORT> <DATABASE>
45594559
jjtThis.url = Url()
4560-
[ <FORMAT> jjtThis.format = PString() ]
4560+
[ <FORMAT> jjtThis.format = Identifier() ]
45614561
[ <OVERWRITE> ( <TRUE> { jjtThis.overwrite = BooleanExpression.TRUE;} | <FALSE> { jjtThis.overwrite = BooleanExpression.FALSE;} ) ]
45624562
)
45634563
{return jjtThis; }

engine/src/main/java/com/arcadedb/query/sql/executor/FetchFromSchemaTypesStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ else if (type.getType() == Edge.RECORD_TYPE)
9191
propRes.setProperty("name", typeIndexInternal.getName());
9292
propRes.setProperty("typeName", typeIndexInternal.getTypeName());
9393
propRes.setProperty("type", typeIndexInternal.getType());
94-
propRes.setProperty("properties", Arrays.asList(typeIndexInternal.getPropertyNames()));
94+
propRes.setProperty("properties", typeIndexInternal.getPropertyNames());
9595
propRes.setProperty("automatic", typeIndexInternal.isAutomatic());
9696
propRes.setProperty("unique", typeIndexInternal.isUnique());
9797
return propRes;

engine/src/main/java/com/arcadedb/query/sql/parser/DropPropertyStatement.java

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@
1717
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=true,NODE_PREFIX=O,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_USERTYPE_VISIBILITY_PUBLIC=true */
1818
package com.arcadedb.query.sql.parser;
1919

20+
import com.arcadedb.database.Database;
21+
import com.arcadedb.exception.CommandExecutionException;
22+
import com.arcadedb.index.Index;
23+
import com.arcadedb.index.IndexInternal;
24+
import com.arcadedb.index.TypeIndex;
2025
import com.arcadedb.query.sql.executor.CommandContext;
26+
import com.arcadedb.query.sql.executor.InternalResultSet;
27+
import com.arcadedb.query.sql.executor.ResultInternal;
2128
import com.arcadedb.query.sql.executor.ResultSet;
29+
import com.arcadedb.schema.DocumentType;
2230

2331
import java.util.*;
2432

@@ -27,7 +35,7 @@ public class DropPropertyStatement extends DDLStatement {
2735
protected Identifier typeName;
2836
protected Identifier propertyName;
2937
protected boolean ifExists = false;
30-
protected boolean force = false;
38+
protected boolean force = false;
3139

3240
public DropPropertyStatement(int id) {
3341
super(id);
@@ -37,85 +45,75 @@ public DropPropertyStatement(SqlParser p, int id) {
3745
super(p, id);
3846
}
3947

40-
@Override public ResultSet executeDDL(CommandContext ctx) {
41-
42-
throw new UnsupportedOperationException();
43-
// InternalResultSet rs = new InternalResultSet();
44-
// final Database database = ctx.getDatabase();
45-
// final OClassImpl sourceClass = (OClassImpl) database.getMetadata().getSchema().getClass(className.getStringValue());
46-
// if (sourceClass == null)
47-
// throw new PCommandExecutionException("Source class '" + className + "' not found");
48-
//
49-
// if (sourceClass.getProperty(propertyName.getStringValue()) == null) {
50-
// if(ifExists){
51-
// return rs;
52-
// }
53-
// throw new PCommandExecutionException("Property '" + propertyName + "' not found on class " + className);
54-
// }
55-
// final List<OIndex<?>> indexes = relatedIndexes(propertyName.getStringValue(), database);
56-
// if (!indexes.isEmpty()) {
57-
// if (force) {
58-
// for (final OIndex<?> index : indexes) {
59-
// index.delete();
60-
// OResultInternal result = new OResultInternal();
61-
// result.setProperty("operation", "cascade drop index");
62-
// result.setProperty("indexName", index.getName());
63-
// rs.add(result);
64-
// }
65-
// } else {
66-
// final StringBuilder indexNames = new StringBuilder();
67-
//
68-
// boolean first = true;
69-
// for (final OIndex<?> index : sourceClass.getClassInvolvedIndexes(propertyName.getStringValue())) {
70-
// if (!first) {
71-
// indexNames.append(", ");
72-
// } else {
73-
// first = false;
74-
// }
75-
// indexNames.append(index.getName());
76-
// }
77-
//
78-
// throw new PCommandExecutionException("Property used in indexes (" + indexNames.toString()
79-
// + "). Please drop these indexes before removing property or use FORCE parameter.");
80-
// }
81-
// }
82-
//
83-
// // REMOVE THE PROPERTY
84-
// sourceClass.dropProperty(propertyName.getStringValue());
85-
//
86-
// OResultInternal result = new OResultInternal();
87-
// result.setProperty("operation", "drop property");
88-
// result.setProperty("typeName", className.getStringValue());
89-
// result.setProperty("propertyname", propertyName.getStringValue());
90-
// rs.add(result);
91-
// return rs;
48+
@Override
49+
public ResultSet executeDDL(CommandContext ctx) {
50+
InternalResultSet rs = new InternalResultSet();
51+
final Database database = ctx.getDatabase();
52+
final DocumentType sourceClass = database.getSchema().getType(typeName.getStringValue());
53+
if (sourceClass == null)
54+
throw new CommandExecutionException("Source class '" + typeName + "' not found");
55+
56+
if (sourceClass.getProperty(propertyName.getStringValue()) == null) {
57+
if (ifExists) {
58+
return rs;
59+
}
60+
throw new CommandExecutionException("Property '" + propertyName + "' not found on class " + typeName);
61+
}
62+
final List<TypeIndex> indexes = sourceClass.getIndexesByProperty(propertyName.getStringValue());
63+
if (!indexes.isEmpty()) {
64+
if (force) {
65+
for (final Index index : indexes) {
66+
((IndexInternal) index).drop();
67+
ResultInternal result = new ResultInternal();
68+
result.setProperty("operation", "cascade drop index");
69+
result.setProperty("indexName", index.getName());
70+
rs.add(result);
71+
}
72+
} else {
73+
final StringBuilder indexNames = new StringBuilder();
74+
75+
boolean first = true;
76+
for (final TypeIndex index : indexes) {
77+
if (!first) {
78+
indexNames.append(", ");
79+
} else {
80+
first = false;
81+
}
82+
indexNames.append(index.getName());
83+
}
84+
85+
throw new CommandExecutionException(
86+
"Property used in indexes (" + indexNames + "). Please drop these indexes before removing property or use FORCE parameter.");
87+
}
88+
}
89+
90+
// REMOVE THE PROPERTY
91+
sourceClass.dropProperty(propertyName.getStringValue());
92+
93+
ResultInternal result = new ResultInternal();
94+
result.setProperty("operation", "drop property");
95+
result.setProperty("typeName", typeName.getStringValue());
96+
result.setProperty("propertyName", propertyName.getStringValue());
97+
rs.add(result);
98+
return rs;
9299
}
93100

94-
// private List<OIndex<?>> relatedIndexes(final String fieldName, ODatabase database) {
95-
// final List<OIndex<?>> result = new ArrayList<OIndex<?>>();
96-
// for (final OIndex<?> oIndex : database.getMetadata().getIndexManager().getClassIndexes(className.getStringValue())) {
97-
// if (OCollections.indexOf(oIndex.getDefinition().getFields(), fieldName, new OCaseInsentiveComparator()) > -1) {
98-
// result.add(oIndex);
99-
// }
100-
// }
101-
//
102-
// return result;
103-
// }
104-
105-
@Override public void toString(Map<String, Object> params, StringBuilder builder) {
101+
@Override
102+
public void toString(Map<String, Object> params, StringBuilder builder) {
106103
builder.append("DROP PROPERTY ");
107104
typeName.toString(params, builder);
108105
builder.append(".");
109106
propertyName.toString(params, builder);
110-
if(ifExists){
107+
if (ifExists) {
111108
builder.append(" IF EXISTS");
112109
}
113-
if(force){
110+
if (force) {
114111
builder.append(" FORCE");
115112
}
116113
}
117114

118-
@Override public DropPropertyStatement copy() {
115+
@Override
116+
public DropPropertyStatement copy() {
119117
DropPropertyStatement result = new DropPropertyStatement(-1);
120118
result.typeName = typeName == null ? null : typeName.copy();
121119
result.propertyName = propertyName == null ? null : propertyName.copy();
@@ -124,7 +122,8 @@ public DropPropertyStatement(SqlParser p, int id) {
124122
return result;
125123
}
126124

127-
@Override public boolean equals(Object o) {
125+
@Override
126+
public boolean equals(Object o) {
128127
if (this == o)
129128
return true;
130129
if (o == null || getClass() != o.getClass())
@@ -134,15 +133,16 @@ public DropPropertyStatement(SqlParser p, int id) {
134133

135134
if (force != that.force)
136135
return false;
137-
if(ifExists!=that.ifExists){
136+
if (ifExists != that.ifExists) {
138137
return false;
139138
}
140139
if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null)
141140
return false;
142141
return propertyName != null ? propertyName.equals(that.propertyName) : that.propertyName == null;
143142
}
144143

145-
@Override public int hashCode() {
144+
@Override
145+
public int hashCode() {
146146
int result = typeName != null ? typeName.hashCode() : 0;
147147
result = 31 * result + (propertyName != null ? propertyName.hashCode() : 0);
148148
result = 31 * result + (force ? 1 : 0);

engine/src/main/java/com/arcadedb/query/sql/parser/ExportDatabaseStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class ExportDatabaseStatement extends SimpleExecStatement {
3131

3232
protected Url url;
33-
protected String format = "jsonl";
33+
protected Identifier format = new Identifier("jsonl");
3434
protected BooleanExpression overwrite = BooleanExpression.FALSE;
3535

3636
public ExportDatabaseStatement(int id) {
@@ -58,7 +58,7 @@ public ResultSet executeSimple(CommandContext ctx) {
5858
final Class<?> clazz = Class.forName("com.arcadedb.integration.exporter.Exporter");
5959
final Object exporter = clazz.getConstructor(Database.class, String.class).newInstance(ctx.getDatabase(), fileName);
6060

61-
String formatExport = format;
61+
String formatExport = format.getStringValue();
6262
if ((formatExport.startsWith("'") && formatExport.endsWith("'")) ||//
6363
formatExport.startsWith("\"") && formatExport.endsWith("\"")) {
6464
formatExport = formatExport.substring(1, formatExport.length() - 1);

engine/src/main/java/com/arcadedb/query/sql/parser/SqlParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17672,7 +17672,7 @@ final public ExportDatabaseStatement ExportDatabaseStatement() throws ParseExcep
1767217672
switch (jj_ntk == -1 ? jj_ntk_f() : jj_ntk) {
1767317673
case FORMAT:{
1767417674
jj_consume_token(FORMAT);
17675-
jjtn000.format = PString();
17675+
jjtn000.format = Identifier();
1767617676
break;
1767717677
}
1767817678
default:
@@ -21278,7 +21278,7 @@ private boolean jj_3_88()
2127821278
private boolean jj_3R_706()
2127921279
{
2128021280
if (jj_scan_token(FORMAT)) return true;
21281-
if (jj_3R_490()) return true;
21281+
if (jj_3R_128()) return true;
2128221282
return false;
2128321283
}
2128421284

engine/src/main/java/com/arcadedb/schema/DocumentType.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,21 @@ public List<Index> getPolymorphicBucketIndexByBucketId(final int bucketId) {
310310
return result;
311311
}
312312

313+
public List<TypeIndex> getIndexesByProperty(final String property) {
314+
final List<TypeIndex> result = new ArrayList<>();
315+
316+
for (Map.Entry<List<String>, TypeIndex> entry : indexesByProperties.entrySet()) {
317+
for (String prop : entry.getKey()) {
318+
if (property.equals(prop)) {
319+
result.add(entry.getValue());
320+
break;
321+
}
322+
}
323+
}
324+
325+
return result;
326+
}
327+
313328
public TypeIndex getPolymorphicIndexByProperties(final String... properties) {
314329
return getPolymorphicIndexByProperties(Arrays.asList(properties));
315330
}

0 commit comments

Comments
 (0)