use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext in project kripton by xcesco.
the class JQLBuilder method buildJQLUpdate.
/**
* <pre>
* UPDATE bean01 SET text=${text} WHERE id=${id}
* </pre>
*
* @param method
* @param preparedJql
* @return
*/
private static JQL buildJQLUpdate(final SQLiteModelMethod method, final JQL result, Map<JQLDynamicStatementType, String> dynamicReplace, String preparedJql) {
final Class<? extends Annotation> annotation = BindSqlUpdate.class;
if (StringUtils.hasText(preparedJql)) {
result.value = preparedJql;
// UPDATE can contains bind parameter in column values and select
// statement
final One<Boolean> inWhereCondition = new One<Boolean>(false);
final One<Boolean> inColumnsToUpdate = new One<Boolean>(false);
JQLChecker.getInstance().analyze(method, result, new JqlBaseListener() {
@Override
public void enterProjected_columns(Projected_columnsContext ctx) {
if (inColumnsToUpdate.value0) {
result.containsSelectOperation = true;
}
}
@Override
public void enterConflict_algorithm(Conflict_algorithmContext ctx) {
result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(ctx.getText().toUpperCase());
}
@Override
public void enterWhere_stmt(Where_stmtContext ctx) {
inWhereCondition.value0 = true;
}
@Override
public void exitWhere_stmt_clauses(Where_stmt_clausesContext ctx) {
inWhereCondition.value0 = false;
}
@Override
public void enterBind_parameter(Bind_parameterContext ctx) {
if (inWhereCondition.value0) {
result.bindParameterOnWhereStatementCounter++;
} else {
result.bindParameterAsColumnValueCounter++;
}
}
@Override
public void enterColumns_to_update(Columns_to_updateContext ctx) {
inColumnsToUpdate.value0 = true;
}
@Override
public void exitColumns_to_update(Columns_to_updateContext ctx) {
inColumnsToUpdate.value0 = false;
}
});
JQLChecker.getInstance().replaceVariableStatements(method, preparedJql, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onWhere(String statement) {
result.annotatedWhere = true;
result.staticWhereConditions = true;
return null;
}
});
if (result.containsSelectOperation) {
AssertKripton.assertTrueOrInvalidMethodSignException(method.getReturnClass().equals(TypeName.VOID), method, "defined JQL requires that method's return type is void");
}
} else {
final SQLiteDaoDefinition dao = method.getParent();
Set<String> fields;
ModifyType modifyType = SqlModifyBuilder.detectModifyType(method, JQLType.UPDATE);
if (modifyType == ModifyType.UPDATE_BEAN) {
fields = extractFieldsFromAnnotation(method, annotation, false);
} else {
fields = extractFieldsFromMethodParameters(method, annotation);
}
AssertKripton.assertTrueOrInvalidMethodSignException(fields.size() > 0, method, "no field was specified for update");
result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(AnnotationUtility.extractAsEnumerationValue(method.getElement(), annotation, AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE));
StringBuilder builder = new StringBuilder();
builder.append(UPDATE_KEYWORD);
builder.append(" " + result.conflictAlgorithmType.getSqlForInsert());
// entity name
builder.append(dao.getEntitySimplyClassName());
// recreate fields
final One<String> prefix = new One<>("");
if (result.hasParamBean()) {
prefix.value0 = result.paramBean + ".";
}
builder.append(" " + SET_KEYWORD + " ");
builder.append(forEachFields(fields, new OnFieldListener() {
@Override
public String onField(String item) {
return item + "=${" + prefix.value0 + item + "}";
}
}));
builder.append(defineWhereStatement(method, result, annotation, dynamicReplace));
result.value = builder.toString();
}
result.operationType = JQLType.UPDATE;
result.dynamicReplace = dynamicReplace;
return result;
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext in project kripton by xcesco.
the class JQLBuilder method buildJQLInsert.
/**
* <pre>
*
* INSERT INTO person (name, surname, birth_city, birth_day) VALUES (${name}, ${surname}, ${birthCity}, ${birthDay})
* </pre>
*
* @param method
* @param preparedJql
* @return
*/
private static JQL buildJQLInsert(SQLiteModelMethod method, final JQL result, String preparedJql) {
if (StringUtils.hasText(preparedJql)) {
result.value = preparedJql;
// INSERT can contains bind parameter in column values and select
// statement
final One<Boolean> inColumnValueSet = new One<Boolean>(false);
final One<Boolean> inWhereStatement = new One<Boolean>(false);
JQLChecker.getInstance().analyze(method, result, new JqlBaseListener() {
@Override
public void enterConflict_algorithm(Conflict_algorithmContext ctx) {
result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(ctx.getText().toUpperCase());
}
@Override
public void enterProjected_columns(Projected_columnsContext ctx) {
result.containsSelectOperation = true;
}
@Override
public void enterWhere_stmt(Where_stmtContext ctx) {
inWhereStatement.value0 = true;
}
@Override
public void exitWhere_stmt(Where_stmtContext ctx) {
inWhereStatement.value0 = false;
}
@Override
public void enterColumn_value_set(Column_value_setContext ctx) {
inColumnValueSet.value0 = true;
}
@Override
public void exitColumn_value_set(Column_value_setContext ctx) {
inColumnValueSet.value0 = false;
}
@Override
public void enterBind_parameter(Bind_parameterContext ctx) {
if (inWhereStatement.value0) {
result.bindParameterOnWhereStatementCounter++;
} else if (inColumnValueSet.value0) {
result.bindParameterAsColumnValueCounter++;
}
AssertKripton.assertTrue(inWhereStatement.value0 || inColumnValueSet.value0, "unknown situation!");
}
});
if (result.containsSelectOperation) {
AssertKripton.assertTrueOrInvalidMethodSignException(method.getReturnClass().equals(TypeName.VOID), method, "defined JQL requires that method's return type is void");
}
// ASSERT: a INSERT-SELECT SQL can not contains parameters on values
// section.
} else {
// use annotation's attribute value and exclude and bean definition
// to
final Class<? extends Annotation> annotation = BindSqlInsert.class;
final SQLiteDaoDefinition dao = method.getParent();
final boolean includePrimaryKey = AnnotationUtility.extractAsBoolean(method.getElement(), annotation, AnnotationAttributeType.INCLUDE_PRIMARY_KEY);
// define field list
// every method parameter can be used only as insert field
InsertType insertResultType = SqlInsertBuilder.detectInsertType(method);
Set<String> fields;
if (insertResultType == InsertType.INSERT_BEAN) {
fields = extractFieldsFromAnnotation(method, annotation, includePrimaryKey);
} else {
fields = extractFieldsFromMethodParameters(method, annotation);
}
result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(AnnotationUtility.extractAsEnumerationValue(method.getElement(), annotation, AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE));
StringBuilder builder = new StringBuilder();
builder.append(INSERT_KEYWORD);
builder.append(" " + result.conflictAlgorithmType.getSqlForInsert());
builder.append(INTO_KEYWORD);
builder.append(" " + dao.getEntitySimplyClassName());
builder.append(" (");
builder.append(forEachFields(fields, new OnFieldListener() {
@Override
public String onField(String item) {
return item;
}
}));
builder.append(") ");
builder.append(VALUES_KEYWORD);
final One<String> prefix = new One<>("");
if (result.hasParamBean()) {
prefix.value0 = result.paramBean + ".";
}
builder.append(" (");
builder.append(forEachFields(fields, new OnFieldListener() {
@Override
public String onField(String item) {
return "${" + prefix.value0 + item + "}";
}
}));
builder.append(")");
result.value = builder.toString();
}
result.operationType = JQLType.INSERT;
result.dynamicReplace = new HashMap<>();
return result;
}
Aggregations