use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class SqlBuilderHelper method forEachColumnInContentValue.
/**
* <p>
* Generate column check
* </p>
*
* @param methodBuilder
* @param method
* @param columnSetString
* @param generateColumnNameCheck
* @param listener
*/
static void forEachColumnInContentValue(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method, String columnSetString, boolean generateColumnNameCheck, OnColumnListener listener) {
SQLiteDaoDefinition daoDefinition = method.getParent();
methodBuilder.beginControlFlow("for (String columnName:$L)", columnSetString);
if (generateColumnNameCheck) {
methodBuilder.beginControlFlow("if (!$L.contains(columnName))", method.contentProviderMethodName + "ColumnSet");
methodBuilder.addStatement("throw new $T(String.format(\"For URI '$L', column '%s' does not exists in table '%s' or can not be defined in this $L operation\", columnName, $S ))", KriptonRuntimeException.class, method.contentProviderUriTemplate, method.jql.operationType, daoDefinition.getEntity().getTableName());
methodBuilder.endControlFlow();
}
if (listener != null)
listener.onColumnCheck(methodBuilder, "columnName");
methodBuilder.endControlFlow();
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class SqlModifyBuilder method detectModifyType.
/**
* Detect method type
*
* @param method
* @param jqlType
* jql type is necessary because method.jql can be not properly
* initialized
* @return
*/
public static ModifyType detectModifyType(SQLiteModelMethod method, JQLType jqlType) {
// Elements elementUtils = BaseProcessor.elementUtils;
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
ModifyType updateResultType = null;
// check type of arguments
int count = 0;
for (Pair<String, TypeName> param : method.getParameters()) {
if (method.isThisDynamicWhereConditionsName(param.value0)) {
// if current parameter is dynamic where, skip it
continue;
}
if (TypeUtility.isEquals(param.value1, typeName(entity.getElement()))) {
count++;
}
}
if (count == 0) {
// method to update raw data: no bean is used
updateResultType = jqlType == JQLType.UPDATE ? ModifyType.UPDATE_RAW : ModifyType.DELETE_RAW;
ModelAnnotation annotation;
if (jqlType == JQLType.UPDATE) {
annotation = method.getAnnotation(BindSqlUpdate.class);
AssertKripton.assertTrueOrInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.FIELDS).size() == 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.FIELDS.getValue());
AssertKripton.assertTrueOrInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.EXCLUDED_FIELDS).size() == 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.EXCLUDED_FIELDS.getValue());
} else {
annotation = method.getAnnotation(BindSqlDelete.class);
}
// check if there is only one parameter
AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() > 1 && TypeUtility.isEquals(method.getParameters().get(0).value1, daoDefinition.getEntityClassName()), method);
} else if (count == 1) {
updateResultType = jqlType == JQLType.UPDATE ? ModifyType.UPDATE_BEAN : ModifyType.DELETE_BEAN;
// with dynamic where conditions, we have to add 1 to parameter
// check size (one parameter is a bean and one is the where
// conditions)
AssertKripton.assertTrueOrInvalidMethodSignException(method.getParameters().size() == 1 + (method.hasDynamicWhereConditions() ? 1 : 0) + (method.hasDynamicWhereArgs() ? 1 : 0), method, " expected only one parameter of %s type", daoDefinition.getEntityClassName());
} else {
throw (new InvalidMethodSignException(method, "only one parameter of type " + typeName(entity.getElement()) + " can be used"));
}
return updateResultType;
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition in project kripton by xcesco.
the class JQLBuilder method buildJQLSelect.
private static JQL buildJQLSelect(final SQLiteModelMethod method, final JQL result, Map<JQLDynamicStatementType, String> dynamicReplace, String preparedJql) {
final Class<? extends Annotation> annotation = BindSqlSelect.class;
final SQLiteDaoDefinition dao = method.getParent();
if (StringUtils.hasText(preparedJql)) {
result.value = preparedJql;
// in SELECT SQL only where statement can contains bind parameter
JQLChecker.getInstance().analyze(method, result, new JqlBaseListener() {
@Override
public void enterBind_parameter(Bind_parameterContext ctx) {
result.bindParameterOnWhereStatementCounter++;
}
});
JQLChecker.getInstance().replaceVariableStatements(method, preparedJql, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onWhere(String statement) {
result.annotatedWhere = true;
result.staticWhereConditions = true;
return null;
}
@Override
public String onOrderBy(String statement) {
result.annotatedOrderBy = true;
result.staticOrderBy = true;
return null;
}
@Override
public String onOffset(String statement) {
result.annotatedOffset = true;
return null;
}
@Override
public String onLimit(String statement) {
result.annotatedLimit = true;
return null;
}
@Override
public String onHaving(String statement) {
result.annotatedHaving = true;
return null;
}
@Override
public String onGroup(String statement) {
result.annotatedGroupBy = true;
return null;
}
});
} else {
// extract some informaction from method and bean
// use annotation's attribute value and exclude and bean definition
// to
// define field list
final Set<String> fields = extractFieldsFromAnnotation(method, BindSqlSelect.class, true);
boolean distinct = AnnotationUtility.extractAsBoolean(method.getElement(), annotation, AnnotationAttributeType.DISTINCT);
String annotatedGroupBy = AnnotationUtility.extractAsString(method.getElement(), annotation, AnnotationAttributeType.GROUP_BY);
String annotatedHaving = AnnotationUtility.extractAsString(method.getElement(), annotation, AnnotationAttributeType.HAVING);
StringBuilder builder = new StringBuilder();
builder.append(SELECT_KEYWORD + " ");
//
if (distinct) {
builder.append(DISTINCT_KEYWORD + " ");
}
// recreate fields
builder.append(forEachFields(fields, new OnFieldListener() {
@Override
public String onField(String item) {
return item;
}
}));
// entity name
builder.append(" " + FROM_KEYWORD + " " + dao.getEntitySimplyClassName());
// where
builder.append(defineWhereStatement(method, result, annotation, dynamicReplace));
// group by
if (StringUtils.hasText(annotatedGroupBy)) {
result.annotatedGroupBy = true;
builder.append(" " + GROUP_BY_KEYWORD + " " + annotatedGroupBy);
}
// having
if (StringUtils.hasText(annotatedHaving)) {
result.annotatedHaving = true;
builder.append(" " + HAVING_KEYWORD + " " + annotatedHaving);
}
// order by
builder.append(defineOrderByStatement(method, result, annotation, dynamicReplace));
// limit
builder.append(defineLimitStatement(method, result, annotation, dynamicReplace));
result.value = builder.toString();
}
result.operationType = JQLType.SELECT;
result.dynamicReplace = dynamicReplace;
return result;
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition 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.model.SQLiteDaoDefinition in project kripton by xcesco.
the class JQLBuilder method buildJQL.
public static JQL buildJQL(SQLiteModelMethod method, String preparedJql) {
final SQLiteDaoDefinition dao = method.getParent();
Map<JQLDynamicStatementType, String> dynamicReplace = new HashMap<>();
final JQL result = new JQL();
// for each method's parameter
forEachParameter(method, new OnMethodParameterListener() {
@Override
public void onMethodParameter(VariableElement item) {
if (dao.getEntity().getElement().asType().equals(item.asType())) {
result.paramBean = item.getSimpleName().toString();
}
}
});
// defined how jql is defined
result.declarationType = JQLDeclarationType.JQL_COMPACT;
if (StringUtils.hasText(preparedJql)) {
result.declarationType = JQLDeclarationType.JQL_EXPLICIT;
}
if (method.hasAnnotation(BindSqlSelect.class)) {
checkFieldsDefinitions(method, BindSqlSelect.class);
return buildJQLSelect(method, result, dynamicReplace, preparedJql);
} else if (method.hasAnnotation(BindSqlInsert.class)) {
checkFieldsDefinitions(method, BindSqlInsert.class);
return buildJQLInsert(method, result, preparedJql);
} else if (method.hasAnnotation(BindSqlUpdate.class)) {
checkFieldsDefinitions(method, BindSqlUpdate.class);
return buildJQLUpdate(method, result, dynamicReplace, preparedJql);
} else if (method.hasAnnotation(BindSqlDelete.class)) {
return buildJQLDelete(method, result, dynamicReplace, preparedJql);
}
return null;
}
Aggregations