use of com.orientechnologies.orient.core.sql.parser.OUpdateStatement in project orientdb by orientechnologies.
the class OCommandExecutorSQLUpdate method parse.
@SuppressWarnings("unchecked")
public OCommandExecutorSQLUpdate parse(final OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
String originalQuery = queryText;
try {
queryText = preParse(queryText, iRequest);
if (isUpdateEdge()) {
// work-around to use UPDATE syntax without having to
queryText = queryText.replaceFirst("EDGE ", "");
}
textRequest.setText(queryText);
final ODatabaseDocument database = getDatabase();
init((OCommandRequestText) iRequest);
setEntries.clear();
addEntries.clear();
putEntries.clear();
removeEntries.clear();
incrementEntries.clear();
content = null;
merge = null;
query = null;
parserRequiredKeyword(KEYWORD_UPDATE);
subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
if (subjectName == null) {
throwSyntaxErrorException("Invalid subject name. Expected cluster, class, index or sub-query");
}
if (subjectName.equalsIgnoreCase("EDGE")) {
updateEdge = true;
subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
}
clazz = extractClassFromTarget(subjectName);
String word = parserNextWord(true);
if (parserIsEnded() || (!word.equals(KEYWORD_SET) && !word.equals(KEYWORD_ADD) && !word.equals(KEYWORD_PUT) && !word.equals(KEYWORD_REMOVE) && !word.equals(KEYWORD_INCREMENT) && !word.equals(KEYWORD_CONTENT) && !word.equals(KEYWORD_MERGE) && !word.equals(KEYWORD_LOCK) && !word.equals(KEYWORD_RETURN) && !word.equals(KEYWORD_UPSERT) && !word.equals(KEYWORD_EDGE)))
throwSyntaxErrorException("Expected keyword " + KEYWORD_SET + "," + KEYWORD_ADD + "," + KEYWORD_CONTENT + "," + KEYWORD_MERGE + "," + KEYWORD_PUT + "," + KEYWORD_REMOVE + "," + KEYWORD_INCREMENT + "," + KEYWORD_LOCK + " or " + KEYWORD_RETURN + " or " + KEYWORD_UPSERT + " or " + KEYWORD_EDGE);
while ((!parserIsEnded() && !parserGetLastWord().equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE)) || parserGetLastWord().equals(KEYWORD_UPSERT)) {
word = parserGetLastWord();
if (word.equals(KEYWORD_CONTENT))
parseContent();
else if (word.equals(KEYWORD_MERGE))
parseMerge();
else if (word.equals(KEYWORD_SET))
parseSetFields(clazz, setEntries);
else if (word.equals(KEYWORD_ADD))
parseAddFields(clazz);
else if (word.equals(KEYWORD_PUT))
parsePutFields();
else if (word.equals(KEYWORD_REMOVE))
parseRemoveFields();
else if (word.equals(KEYWORD_INCREMENT))
parseIncrementFields();
else if (word.equals(KEYWORD_LOCK))
lockStrategy = parseLock();
else if (word.equals(KEYWORD_UPSERT))
upsertMode = true;
else if (word.equals(KEYWORD_RETURN))
parseReturn();
else if (word.equals(KEYWORD_RETRY)) {
OLogManager.instance().warn(this, "RETRY keyword will be ignored in " + originalQuery);
parseRetry();
} else
break;
parserNextWord(true);
}
final String additionalStatement = parserGetLastWord();
if (subjectName.startsWith("(")) {
subjectName = subjectName.trim();
query = database.command(new OSQLAsynchQuery<ODocument>(subjectName.substring(1, subjectName.length() - 1), this).setContext(context));
if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT))
compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(), KEYWORD_WHERE);
} else if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LET) || additionalStatement.equals(KEYWORD_LOCK)) {
if (this.preParsedStatement != null) {
Map<Object, Object> params = ((OCommandRequestText) iRequest).getParameters();
OUpdateStatement updateStm = (OUpdateStatement) preParsedStatement;
StringBuilder selectString = new StringBuilder();
selectString.append("select from ");
updateStm.target.toString(params, selectString);
if (updateStm.let != null) {
selectString.append(" ");
updateStm.let.toString(params, selectString);
}
if (updateStm.whereClause != null) {
selectString.append(" WHERE ");
updateStm.whereClause.toString(params, selectString);
}
if (updateStm.limit != null) {
selectString.append(" ");
updateStm.limit.toString(params, selectString);
}
if (updateStm.timeout != null) {
selectString.append(" ");
updateStm.timeout.toString(params, selectString);
}
if (updateStm.lockRecord != null) {
selectString.append(" LOCK ");
switch(updateStm.lockRecord) {
case DEFAULT:
selectString.append("DEFAULT");
break;
case EXCLUSIVE_LOCK:
selectString.append("RECORD");
break;
case SHARED_LOCK:
selectString.append("SHARED");
break;
case NONE:
selectString.append("NONE");
break;
}
}
query = new OSQLAsynchQuery<ODocument>(selectString.toString(), this);
} else {
query = new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget() + " " + additionalStatement + " " + parserText.substring(parserGetCurrentPosition()), this);
}
isUpsertAllowed = (((OMetadataInternal) getDatabase().getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName) != null);
} else if (!additionalStatement.isEmpty())
throwSyntaxErrorException("Invalid keyword " + additionalStatement);
else
query = new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget(), this);
if (upsertMode && !isUpsertAllowed)
throwSyntaxErrorException("Upsert only works with class names ");
if (upsertMode && !additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE))
throwSyntaxErrorException("Upsert only works with WHERE keyword");
if (upsertMode && updateEdge)
throwSyntaxErrorException("Upsert is not supported with UPDATE EDGE");
} finally {
textRequest.setText(originalQuery);
}
return this;
}
Aggregations