use of com.orientechnologies.orient.core.sql.parser.OAlterPropertyStatement in project orientdb by orientechnologies.
the class OCommandExecutorSQLAlterProperty method parse.
public OCommandExecutorSQLAlterProperty parse(final OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
String originalQuery = queryText;
try {
queryText = preParse(queryText, iRequest);
textRequest.setText(queryText);
init((OCommandRequestText) iRequest);
StringBuilder word = new StringBuilder();
int oldPos = 0;
int pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
if (pos == -1 || !word.toString().equals(KEYWORD_ALTER))
throw new OCommandSQLParsingException("Keyword " + KEYWORD_ALTER + " not found. Use " + getSyntax(), parserText, oldPos);
oldPos = pos;
pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
if (pos == -1 || !word.toString().equals(KEYWORD_PROPERTY))
throw new OCommandSQLParsingException("Keyword " + KEYWORD_PROPERTY + " not found. Use " + getSyntax(), parserText, oldPos);
oldPos = pos;
pos = nextWord(parserText, parserTextUpperCase, oldPos, word, false);
if (pos == -1)
throw new OCommandSQLParsingException("Expected <class>.<property>. Use " + getSyntax(), parserText, oldPos);
String[] parts = word.toString().split("\\.");
if (parts.length != 2) {
if (parts[1].startsWith("`") && parts[parts.length - 1].endsWith("`")) {
StringBuilder fullName = new StringBuilder();
for (int i = 1; i < parts.length; i++) {
if (i > 1) {
fullName.append(".");
}
fullName.append(parts[i]);
}
parts = new String[] { parts[0], fullName.toString() };
} else {
throw new OCommandSQLParsingException("Expected <class>.<property>. Use " + getSyntax(), parserText, oldPos);
}
}
className = decodeClassName(parts[0]);
if (className == null)
throw new OCommandSQLParsingException("Class not found", parserText, oldPos);
fieldName = decodeClassName(parts[1]);
oldPos = pos;
pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
if (pos == -1)
throw new OCommandSQLParsingException("Missing property attribute to change. Use " + getSyntax(), parserText, oldPos);
final String attributeAsString = word.toString();
try {
attribute = OProperty.ATTRIBUTES.valueOf(attributeAsString.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
throw new OCommandSQLParsingException("Unknown property attribute '" + attributeAsString + "'. Supported attributes are: " + Arrays.toString(OProperty.ATTRIBUTES.values()), parserText, oldPos);
}
value = parserText.substring(pos + 1).trim();
if (attribute.equals(ATTRIBUTES.NAME) || attribute.equals(ATTRIBUTES.LINKEDCLASS)) {
value = decodeClassName(value);
}
if (value.length() == 0) {
throw new OCommandSQLParsingException("Missing property value to change for attribute '" + attribute + "'. Use " + getSyntax(), parserText, oldPos);
}
if (preParsedStatement != null) {
OAlterPropertyStatement stm = (OAlterPropertyStatement) preParsedStatement;
OExpression settingExp = stm.settingValue;
if (settingExp != null) {
Object expValue = settingExp.execute(null, context);
if (expValue == null) {
expValue = settingExp.toString();
}
if (expValue != null) {
if (expValue instanceof Date) {
value = ODateHelper.getDateTimeFormatInstance().format((Date) expValue);
} else
value = expValue.toString();
} else
value = null;
if (attribute.equals(ATTRIBUTES.NAME) || attribute.equals(ATTRIBUTES.LINKEDCLASS)) {
value = decodeClassName(value);
}
} else if (stm.customPropertyName != null) {
value = "" + stm.customPropertyName.getStringValue() + "=" + stm.customPropertyValue.toString();
} else if (stm.clearCustom) {
value = "clear";
}
} else {
if (value.equalsIgnoreCase("null")) {
value = null;
}
if (value != null && isQuoted(value)) {
value = removeQuotes(value);
}
}
} finally {
textRequest.setText(originalQuery);
}
return this;
}
Aggregations