use of com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException 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.exceptions.InvalidMethodSignException 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"));
}
}
}
}
Aggregations