use of com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity in project kripton by xcesco.
the class GenericSQLHelper method generateGenericExecSQL.
/**
* @param methodBuilder
* @param method
* @param daoDefinition
* @param schema
*/
public static void generateGenericExecSQL(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
boolean nullable;
final List<String> paramsList = new ArrayList<String>();
final List<String> contentValueList = new ArrayList<String>();
final One<Boolean> columnsToUpdate = new One<Boolean>(true);
String sql = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
super.onWhereStatementBegin(ctx);
columnsToUpdate.value0 = false;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
super.onWhereStatementEnd(ctx);
columnsToUpdate.value0 = true;
}
@Override
public String onColumnName(String columnName) {
String resolvedName = currentSchema.findColumnNameByPropertyName(method, columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(resolvedName != null, method, columnName);
return resolvedName;
}
@Override
public String onBindParameter(String bindParameterName) {
String propertyName = method.findParameterAliasByName(bindParameterName);
if (columnsToUpdate.value0) {
contentValueList.add(propertyName);
} else {
paramsList.add(propertyName);
}
return "?";
}
});
// update/insert columns
final SQLiteEntity entity = daoDefinition.getEntity();
for (String item : contentValueList) {
// ASSERT: property is always in entity
String propertyName = method.findParameterNameByAlias(item);
TypeName paramType = method.findParameterTypeByAliasOrName(item);
SQLProperty property = entity.get(item);
if (propertyName == null)
throw (new PropertyNotFoundException(method, propertyName, paramType));
Pair<String, TypeName> methodParam = new Pair<String, TypeName>(propertyName, paramType);
// check same type
TypeUtility.checkTypeCompatibility(method, methodParam, property);
if (method.isLogEnabled()) {
methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
} else {
methodBuilder.addCode("_contentValues.put(");
}
// it does not need to be converted in string
SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, methodParam.value0, methodParam.value1, property);
methodBuilder.addCode(");\n");
// if (nullable) {
// methodBuilder.nextControlFlow("else");
//
// if (method.isLogEnabled()) {
// methodBuilder.addStatement("_contentValues.putNull($S)", property.columnName);
// } else {
// methodBuilder.addStatement("_contentValues.putNull()");
// }
//
// methodBuilder.endControlFlow();
// }
}
// where condition
methodBuilder.addComment("build where condition");
{
// String separator = "";
TypeName paramType;
String realName;
for (String item : paramsList) {
methodBuilder.addCode("_contentValues.addWhereArgs(");
paramType = method.findParameterTypeByAliasOrName(item);
realName = method.findParameterNameByAlias(item);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(paramType != null, method, item);
// code for query arguments
nullable = TypeUtility.isNullable(paramType);
if (nullable) {
methodBuilder.addCode("($L==null?\"\":", realName);
}
// check for string conversion
TypeUtility.beginStringConversion(methodBuilder, paramType);
SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, realName, paramType, null);
// check for string conversion
TypeUtility.endStringConversion(methodBuilder, paramType);
if (nullable) {
methodBuilder.addCode(")");
}
methodBuilder.addCode(");\n");
}
}
// log for where parames
SqlBuilderHelper.generateLog(method, methodBuilder);
// log
methodBuilder.addCode("\n");
methodBuilder.addStatement("database().execSQL($S, _contentValues.whereArgsAsArray())", sql);
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity in project kripton by xcesco.
the class InsertRawHelper method generateJavaDoc.
/**
* @param methodBuilder
* @param method
* @param returnType
* @return string sql
*/
public String generateJavaDoc(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method, TypeName returnType) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
final SQLiteEntity entity = daoDefinition.getEntity();
final One<Boolean> inColumnValues = new One<Boolean>(false);
final List<Pair<String, TypeName>> methodParamsUsedAsColumnValue = new ArrayList<>();
final List<Pair<String, TypeName>> methodParamsUsedAsParameter = new ArrayList<>();
// transform JQL to SQL
String sqlInsert = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public void onColumnValueSetBegin(Column_value_setContext ctx) {
inColumnValues.value0 = true;
}
@Override
public void onColumnValueSetEnd(Column_value_setContext ctx) {
inColumnValues.value0 = false;
}
@Override
public String onColumnName(String columnName) {
Set<SQLProperty> property = currentSchema.getPropertyBySimpleName(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(property != null, method, columnName);
SQLProperty tempProperty = property.iterator().next();
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
@Override
public String onBindParameter(String bindParameterName) {
String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
if (inColumnValues.value0) {
methodParamsUsedAsColumnValue.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
} else {
methodParamsUsedAsParameter.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
}
return "${" + resolvedParamName + "}";
}
});
methodBuilder.addJavadoc("<h2>SQL insert</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlInsert);
methodBuilder.addJavadoc("\n");
if (methodParamsUsedAsColumnValue.size() > 0) {
// list of inserted fields
methodBuilder.addJavadoc("<h2>Inserted columns:</strong></h2>\n");
methodBuilder.addJavadoc("<dl>\n");
for (Pair<String, TypeName> property : methodParamsUsedAsColumnValue) {
// String resolvedName = method.findParameterAliasByName(property.value0);
String resolvedName = method.findParameterNameByAlias(property.value0);
/*SQLProperty prop = entity.get(resolvedName);
if (prop == null)
throw (new PropertyNotFoundException(method, property.value0, property.value1));*/
methodBuilder.addJavadoc("\t<dt>$L</dt>", property.value0);
methodBuilder.addJavadoc("<dd>is binded to query's parameter <strong>$L</strong> and method's parameter <strong>$L</strong></dd>\n", "${" + property.value0 + "}", resolvedName);
}
methodBuilder.addJavadoc("</dl>\n\n");
}
// list of parameters
if (methodParamsUsedAsParameter.size() > 0) {
methodBuilder.addJavadoc("<h2>Method parameters used as sql parameters</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
for (Pair<String, TypeName> property : methodParamsUsedAsParameter) {
String resolvedName = method.findParameterNameByAlias(property.value0);
methodBuilder.addJavadoc("\t<dt>$L</dt>", resolvedName);
methodBuilder.addJavadoc("<dd>is binded to query's parameter <strong>$${$L}</strong></dd>\n", property.value0);
}
methodBuilder.addJavadoc("</dl>\n\n");
}
for (Pair<String, TypeName> param : method.getParameters()) {
if (methodParamsUsedAsColumnValue.contains(param)) {
methodBuilder.addJavadoc("@param $L\n", param.value0);
if (entity.get(method.findParameterAliasByName(param.value0)) != null) {
methodBuilder.addJavadoc("\tis binded to column value <strong>$L</strong>\n", entity.get(method.findParameterAliasByName(param.value0)).columnName);
} else {
// in case of JQL explicit, you can declare name of parameter
methodBuilder.addJavadoc("\tis binded to query parameter <strong>$L</strong>\n", param.value0);
}
}
if (methodParamsUsedAsParameter.contains(param)) {
methodBuilder.addJavadoc("@param $L\n", param.value0);
methodBuilder.addJavadoc("\tis used as parameter\n");
}
}
generateJavaDocReturnType(methodBuilder, returnType);
return sqlInsert;
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity in project kripton by xcesco.
the class InsertRawHelper method generate.
@Override
public void generate(TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, boolean mapFields, final SQLiteModelMethod method, TypeName returnType) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
final SQLiteEntity entity = daoDefinition.getEntity();
// boolean nullable;
// generate javadoc
generateJavaDoc(methodBuilder, method, returnType);
// standard INSERT
if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate()", KriptonContentValues.class);
} else {
String psName = method.buildPreparedStatementName();
// generate SQL for insert
classBuilder.addField(FieldSpec.builder(TypeName.get(SQLiteStatement.class), psName, Modifier.PRIVATE, Modifier.STATIC).build());
methodBuilder.beginControlFlow("if ($L==null)", psName);
SqlBuilderHelper.generateSQLForStaticQuery(method, methodBuilder);
methodBuilder.addStatement("$L = $T.compile(_context, _sql)", psName, KriptonDatabaseWrapper.class);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate($L)", KriptonContentValues.class, psName);
}
if (method.jql.containsSelectOperation) {
// INSERT-SELECT
GenericSQLHelper.generateGenericExecSQL(methodBuilder, method);
} else {
methodBuilder.addCode("\n");
List<Pair<String, TypeName>> fieldsToUpdate = method.getParameters();
fieldsToUpdate = SqlBuilderHelper.orderContentValues(method, fieldsToUpdate);
for (Pair<String, TypeName> item : fieldsToUpdate) {
String propertyName = method.findParameterAliasByName(item.value0);
SQLProperty property = entity.get(propertyName);
if (property == null)
throw (new PropertyNotFoundException(method, propertyName, item.value1));
// check same type
TypeUtility.checkTypeCompatibility(method, item, property);
if (method.isLogEnabled()) {
methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
} else {
methodBuilder.addCode("_contentValues.put(");
}
// it does not need to be converted in string
SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, item.value0, item.value1, property);
methodBuilder.addCode(");\n");
}
methodBuilder.addCode("\n");
SqlBuilderHelper.generateLog(method, methodBuilder);
methodBuilder.addComment("insert operation");
if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
// does not memorize compiled statement, it can vary every time generate SQL for insert
SqlBuilderHelper.generateSQLForInsertDynamic(method, methodBuilder);
methodBuilder.addStatement("long result = $T.insert(_context, _sql, _contentValues)", KriptonDatabaseWrapper.class);
} else {
String psName = method.buildPreparedStatementName();
methodBuilder.addStatement("long result = $T.insert($L, _contentValues)", KriptonDatabaseWrapper.class, psName);
}
if (daoDefinition.getParent().generateRx) {
GenericSQLHelper.generateSubjectNext(methodBuilder, SubjectType.INSERT);
}
// support for livedata
if (daoDefinition.hasLiveData()) {
methodBuilder.addComment("support for livedata");
methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result)");
}
// define return value
if (returnType == TypeName.VOID) {
} else if (TypeUtility.isTypeIncludedIn(returnType, Boolean.TYPE, Boolean.class)) {
methodBuilder.addCode("return result!=-1;\n");
} else if (TypeUtility.isTypeIncludedIn(returnType, Long.TYPE, Long.class)) {
methodBuilder.addCode("return result;\n");
} else if (TypeUtility.isTypeIncludedIn(returnType, Integer.TYPE, Integer.class)) {
methodBuilder.addCode("return (int)result;\n");
} else {
// more than one listener found
throw (new InvalidMethodSignException(method, "invalid return type"));
}
}
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity in project kripton by xcesco.
the class ModifyRawHelper method generate.
@Override
public void generate(TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, boolean updateMode, SQLiteModelMethod method, TypeName returnType) {
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
// separate params used for update bean and params used in
// whereCondition
// analyze whereCondition
String whereCondition = extractWhereConditions(updateMode, method);
// this method is invoked to check if every parameter is binded to an method param
SqlUtility.extractParametersFromString(method.jql.value, method, entity);
Pair<String, List<Pair<String, TypeName>>> where = SqlUtility.extractParametersFromString(whereCondition, method, entity);
// defines which parameter is used like update field and which is used
// in where condition.
List<Pair<String, TypeName>> methodParams = method.getParameters();
List<Pair<String, TypeName>> updateableParams = new ArrayList<Pair<String, TypeName>>();
List<Pair<String, TypeName>> whereParams = new ArrayList<Pair<String, TypeName>>();
String name;
for (Pair<String, TypeName> param : methodParams) {
name = method.findParameterAliasByName(param.value0);
if (method.isThisDynamicWhereConditionsName(name)) {
// skip for dynamic where
continue;
}
if (method.isThisDynamicWhereArgsName(name)) {
// skip for dynamic where
continue;
}
if (where.value1.contains(new Pair<>(name, param.value1))) {
whereParams.add(param);
} else {
updateableParams.add(param);
}
}
// clear contentValues
if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate()", KriptonContentValues.class);
} else {
String psName = method.buildPreparedStatementName();
// generate SQL for insert
classBuilder.addField(FieldSpec.builder(TypeName.get(SQLiteStatement.class), psName, Modifier.PRIVATE, Modifier.STATIC).build());
methodBuilder.beginControlFlow("if ($L==null)", psName);
SqlBuilderHelper.generateSQLForStaticQuery(method, methodBuilder);
methodBuilder.addStatement("$L = $T.compile(_context, _sql)", psName, KriptonDatabaseWrapper.class);
methodBuilder.endControlFlow();
methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate($L)", KriptonContentValues.class, psName);
}
if (method.jql.containsSelectOperation) {
generateJavaDoc(method, methodBuilder, updateMode);
GenericSQLHelper.generateGenericExecSQL(methodBuilder, method);
} else {
// generate javadoc
generateJavaDoc(method, methodBuilder, updateMode, whereCondition, where, methodParams);
if (updateMode) {
AssertKripton.assertTrueOrInvalidMethodSignException(updateableParams.size() > 0, method, "no column was selected for update");
// order item for content values
updateableParams = SqlBuilderHelper.orderContentValues(method, updateableParams);
for (Pair<String, TypeName> item : updateableParams) {
String resolvedParamName = method.findParameterAliasByName(item.value0);
SQLProperty property = entity.get(resolvedParamName);
if (property == null)
throw (new PropertyNotFoundException(method, resolvedParamName, item.value1));
// check same type
TypeUtility.checkTypeCompatibility(method, item, property);
// here it needed raw parameter typeName
if (method.isLogEnabled()) {
methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
} else {
methodBuilder.addCode("_contentValues.put(");
}
SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, item.value0, TypeUtility.typeName(property.getElement()), property);
methodBuilder.addCode(");\n");
// if (nullable) {
// methodBuilder.nextControlFlow("else");
//
// if (method.isLogEnabled()) {
// methodBuilder.addStatement("_contentValues.putNull($S)", property.columnName);
// } else {
// methodBuilder.addStatement("_contentValues.putNull()");
// }
//
// methodBuilder.endControlFlow();
// }
}
methodBuilder.addCode("\n");
} else {
if (updateableParams.size() > 0) {
String separator = "";
StringBuilder buffer = new StringBuilder();
for (Pair<String, TypeName> item : updateableParams) {
String resolvedParamName = method.findParameterAliasByName(item.value0);
buffer.append(separator + resolvedParamName);
separator = ", ";
}
// in DELETE can not be updated fields
if (updateableParams.size() > 1) {
throw (new InvalidMethodSignException(method, " parameters " + buffer.toString() + " are not used in where conditions"));
} else {
throw (new InvalidMethodSignException(method, " parameter " + buffer.toString() + " is not used in where conditions"));
}
}
}
// build where condition
generateWhereCondition(methodBuilder, method, where);
methodBuilder.addCode("\n");
ModifyBeanHelper.generateModifyQueryCommonPart(method, classBuilder, methodBuilder);
// support for livedata
if (daoDefinition.hasLiveData()) {
methodBuilder.addComment("support for livedata");
methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result)");
}
// if true, field must be associate to ben attributes
if (returnType == TypeName.VOID) {
} else {
if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
methodBuilder.addStatement("return result!=0");
} else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
methodBuilder.addStatement("return result");
} else {
// more than one listener found
throw (new InvalidMethodSignException(method, "invalid return type"));
}
}
}
}
use of com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity in project kripton by xcesco.
the class JQLBuilder method extractFieldsFromAnnotation.
/**
* @param method
* @param annotationClazz
* @param includePrimaryKey
* @param dao
* @param entity
* @return
*/
private static <A extends Annotation> LinkedHashSet<String> extractFieldsFromAnnotation(final SQLiteModelMethod method, Class<A> annotationClazz, final boolean includePrimaryKey) {
final SQLiteDaoDefinition dao = method.getParent();
final SQLiteEntity entity = method.getParent().getEntity();
List<String> annotatedFieldValues = AnnotationUtility.extractAsStringArray(method.getElement(), annotationClazz, AnnotationAttributeType.FIELDS);
List<String> annotatedExcludedFieldValues = AnnotationUtility.extractAsStringArray(method.getElement(), annotationClazz, AnnotationAttributeType.EXCLUDED_FIELDS);
CollectionUtils.trim(annotatedFieldValues);
CollectionUtils.trim(annotatedExcludedFieldValues);
final One<Integer> count = new One<>(0);
// extract properties from managed bean
final Set<String> allFields = new LinkedHashSet<>();
forEachFields(dao, new OnPropertyListener() {
@Override
public void onProperty(SQLProperty item) {
if (!item.isPrimaryKey() || (item.isPrimaryKey() && includePrimaryKey)) {
allFields.add(item.getName());
}
if (TypeUtility.isEquals(item.getPropertyType().getTypeName(), typeName(entity.getElement()))) {
count.value0++;
}
}
});
// properties from selected set.
if (annotatedFieldValues.size() == 0 && annotatedExcludedFieldValues.size() == 0) {
// if no fields was selected, select all
annotatedFieldValues.clear();
annotatedFieldValues.addAll(allFields);
} else if (annotatedExcludedFieldValues.size() > 0) {
for (String fieldName : annotatedExcludedFieldValues) {
if (!entity.contains(fieldName)) {
AssertKripton.failUnknownPropertyInJQLException(method, annotationClazz, AnnotationAttributeType.EXCLUDED_FIELDS, fieldName);
}
}
allFields.removeAll(annotatedExcludedFieldValues);
annotatedFieldValues.clear();
annotatedFieldValues.addAll(allFields);
}
LinkedHashSet<String> result = new LinkedHashSet<>();
result.addAll(annotatedFieldValues);
return result;
}
Aggregations