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 */
1818package 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 ;
2025import com .arcadedb .query .sql .executor .CommandContext ;
26+ import com .arcadedb .query .sql .executor .InternalResultSet ;
27+ import com .arcadedb .query .sql .executor .ResultInternal ;
2128import com .arcadedb .query .sql .executor .ResultSet ;
29+ import com .arcadedb .schema .DocumentType ;
2230
2331import 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 );
0 commit comments