Search in sources :

Example 1 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class BindDataSourceSubProcessor method createSQLEntityFromDao.

/**
 * <p>
 * Create bean's definition for each dao definition contained in dataSource
 * </p>
 *
 * @param dataSource
 * @param daoName
 */
private boolean createSQLEntityFromDao(final SQLiteDatabaseSchema schema, Element dataSource, String daoName) {
    TypeElement daoElement = globalDaoElements.get(daoName);
    if (daoElement == null) {
        String msg = String.format("Data source %s references a DAO %s without @BindDao annotation", dataSource.toString(), daoName);
        throw (new InvalidNameException(msg));
    }
    ModelProperty property;
    String beanName = AnnotationUtility.extractAsClassName(daoElement, BindDao.class, AnnotationAttributeType.VALUE);
    if (!StringUtils.hasText(beanName)) {
        return false;
    }
    final TypeElement beanElement = globalBeanElements.get(beanName);
    // this.isGeneratedEntity(beanName);
    AssertKripton.asserTrueOrMissedAnnotationOnClassException(beanElement != null, daoElement, beanName);
    // create equivalent entity in the domain of bind processor
    final BindEntity bindEntity = BindEntityBuilder.parse(null, beanElement);
    // assert: bean is present
    final SQLiteEntity currentEntity = new SQLiteEntity(schema, bindEntity);
    if (schema.contains(currentEntity.getName())) {
        // bean already defined in datasource
        return true;
    }
    final boolean bindAllFields = AnnotationUtility.getAnnotationAttributeAsBoolean(currentEntity, BindType.class, AnnotationAttributeType.ALL_FIELDS, Boolean.TRUE);
    {
        PropertyUtility.buildProperties(elementUtils, currentEntity, new PropertyFactory<SQLiteEntity, SQLProperty>() {

            @Override
            public SQLProperty createProperty(SQLiteEntity entity, Element propertyElement) {
                return new SQLProperty(entity, propertyElement, AnnotationUtility.buildAnnotationList(propertyElement));
            }
        }, propertyAnnotationFilter, new PropertyCreatedListener<SQLiteEntity, SQLProperty>() {

            @Override
            public boolean onProperty(SQLiteEntity entity, SQLProperty property) {
                if (property.hasAnnotation(BindDisabled.class)) {
                    if (bindAllFields) {
                        return false;
                    } else {
                        throw new InvalidDefinition("@BindDisabled can not be used with @BindType(allField=false)");
                    }
                }
                ModelAnnotation annotationBindColumn = property.getAnnotation(BindColumn.class);
                if (annotationBindColumn != null && AnnotationUtility.extractAsBoolean(property, annotationBindColumn, AnnotationAttributeType.ENABLED) == false) {
                    return false;
                }
                if (!bindAllFields && annotationBindColumn == null) {
                    return false;
                }
                if (annotationBindColumn != null) {
                    property.setNullable(AnnotationUtility.extractAsBoolean(property, annotationBindColumn, AnnotationAttributeType.NULLABLE));
                    ColumnType columnType = ColumnType.valueOf(AnnotationUtility.extractAsEnumerationValue(property, annotationBindColumn, AnnotationAttributeType.COLUMN_TYPE));
                    property.columnType = columnType;
                    property.setPrimaryKey(columnType == ColumnType.PRIMARY_KEY);
                    String foreignClassName = annotationBindColumn.getAttributeAsClassName(AnnotationAttributeType.FOREIGN_KEY);
                    property.foreignClassName = foreignClassName;
                    if (property.hasForeignKeyClassName() && property.columnType == ColumnType.PRIMARY_KEY) {
                        AssertKripton.failIncompatibleAttributesInAnnotationException("In class '%s' property '%s' can not be defined as PRIMARY KEY and FOREIGN KEY", bindEntity.getElement().asType(), property.getName());
                    }
                    ForeignKeyAction onDeleteAction = ForeignKeyAction.valueOf(AnnotationUtility.extractAsEnumerationValue(property, annotationBindColumn, AnnotationAttributeType.ON_DELETE));
                    ForeignKeyAction onUpdateAction = ForeignKeyAction.valueOf(AnnotationUtility.extractAsEnumerationValue(property, annotationBindColumn, AnnotationAttributeType.ON_UPDATE));
                    if (!property.hasForeignKeyClassName() && onDeleteAction != ForeignKeyAction.NO_ACTION) {
                        String msg = String.format("In class '%s', property '%s' defines 'onDelete' attribute but it is not a foreign key", bindEntity.getElement().asType(), property.getName());
                        AssertKripton.failIncompatibleAttributesInAnnotationException(msg);
                    }
                    if (!property.hasForeignKeyClassName() && onUpdateAction != ForeignKeyAction.NO_ACTION) {
                        String msg = String.format("In class '%s', property '%s' defines 'onUpdate' attribute but it is not a foreign key", bindEntity.getElement().asType(), property.getName());
                        AssertKripton.failIncompatibleAttributesInAnnotationException(msg);
                    }
                    property.onDeleteAction = onDeleteAction;
                    property.onUpdateAction = onUpdateAction;
                } else {
                    // primary key is set in other places
                    property.setNullable(true);
                    // ColumnType columnType = ColumnType.STANDARD;
                    property.columnType = ColumnType.STANDARD;
                }
                if (bindEntity.contains(property.getName())) {
                    BindProperty bindProperty = bindEntity.get(property.getName());
                    if (bindProperty.isBindedArray() || bindProperty.isBindedCollection() || bindProperty.isBindedMap() || bindProperty.isBindedObject()) {
                        property.bindProperty = bindProperty;
                    }
                } else {
                    throw (new KriptonRuntimeException(String.format("In class '%s' property '%s' has a wrong definition for create SQLite DataSource", bindEntity.getElement().asType(), property.getName())));
                }
                String columnName = null;
                if (annotationBindColumn != null) {
                    columnName = annotationBindColumn.getAttribute(AnnotationAttributeType.VALUE);
                }
                if (!StringUtils.hasText(columnName)) {
                    columnName = property.getName();
                }
                // convert column typeName from field typeName to table:
                // fieldName
                // to field_name
                property.columnName = schema.columnNameConverter.convert(columnName);
                return true;
            }
        });
    }
    // just to fix that property id can be the default PK without
    // annotation.
    // this operation force primary key flag for property
    SQLProperty primaryKey = currentEntity.getPrimaryKey();
    if (primaryKey != null) {
        primaryKey.setPrimaryKey(true);
        primaryKey.columnType = ColumnType.PRIMARY_KEY;
        primaryKey.setNullable(false);
    }
    if (currentEntity.getCollection().size() == 0) {
        String msg = String.format("Class '%s', used in %s database definition, has no property!", currentEntity.getName(), dataSource.getSimpleName().toString());
        throw (new PropertyNotFoundException(msg));
    }
    if (currentEntity.countPrimaryKeys() > 1) {
        throw (new TooManySQLPrimaryKeyFoundException(currentEntity));
    }
    // check primary key
    property = currentEntity.getPrimaryKey();
    if (property == null)
        throw (new SQLPrimaryKeyNotFoundException(currentEntity));
    if (!property.isType(Long.TYPE, Long.class))
        throw (new SQLPrimaryKeyNotValidTypeException(currentEntity, property));
    // add entity to schema after properties definition!
    schema.addEntity(currentEntity);
    return true;
}
Also used : PropertyCreatedListener(com.abubusoft.kripton.processor.core.reflect.PropertyUtility.PropertyCreatedListener) ColumnType(com.abubusoft.kripton.android.ColumnType) PropertyNotFoundException(com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException) TypeElement(javax.lang.model.element.TypeElement) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException) TooManySQLPrimaryKeyFoundException(com.abubusoft.kripton.processor.exceptions.TooManySQLPrimaryKeyFoundException) BindEntity(com.abubusoft.kripton.processor.bind.model.BindEntity) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) InvalidNameException(com.abubusoft.kripton.processor.exceptions.InvalidNameException) InvalidDefinition(com.abubusoft.kripton.processor.exceptions.InvalidDefinition) PropertyFactory(com.abubusoft.kripton.processor.core.reflect.PropertyFactory) ForeignKeyAction(com.abubusoft.kripton.android.sqlite.ForeignKeyAction) ModelProperty(com.abubusoft.kripton.processor.core.ModelProperty) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLPrimaryKeyNotValidTypeException(com.abubusoft.kripton.processor.exceptions.SQLPrimaryKeyNotValidTypeException) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) SQLPrimaryKeyNotFoundException(com.abubusoft.kripton.processor.exceptions.SQLPrimaryKeyNotFoundException)

Example 2 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class ModifyRawHelper method generateJavaDoc.

/**
 * @param daoDefinition
 * @param method
 * @param methodBuilder
 * @param updateMode
 * @param whereCondition
 * @param where
 * @param methodParams
 * @param updateableParams
 *
 * @return sql generated
 */
private void generateJavaDoc(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder, boolean updateMode, String whereCondition, Pair<String, List<Pair<String, TypeName>>> where, List<Pair<String, TypeName>> methodParams) {
    final List<SQLProperty> updatedProperties = new ArrayList<>();
    final One<Boolean> onWhereStatement = new One<Boolean>(false);
    String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public void onWhereStatementBegin(Where_stmtContext ctx) {
            onWhereStatement.value0 = true;
        }

        @Override
        public void onWhereStatementEnd(Where_stmtContext ctx) {
            onWhereStatement.value0 = false;
        }

        @Override
        public String onColumnNameToUpdate(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            updatedProperties.add(tempProperty);
            return tempProperty.columnName;
        }

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
            AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
            if (onWhereStatement.value0) {
                return "${" + bindParameterName + "}";
            } else {
                return ":" + bindParameterName;
            }
        }
    });
    if (updateMode) {
        methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n");
        // list of updated fields
        methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
        methodBuilder.addJavadoc("<ul>\n");
        for (SQLProperty property : updatedProperties) {
            methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
        // methodBuilder.addJavadoc("<dd>is binded to query's parameter
        // <strong>$L</strong> and method's parameter
        // <strong>$L</strong></dd>\n", "${" + resolvedName + "}",
        // property.value0);
        }
        methodBuilder.addJavadoc("</ul>");
        methodBuilder.addJavadoc("\n\n");
    } else {
        methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n\n");
    }
    // list of where parameter
    methodBuilder.addJavadoc("<h2>Where parameters:</h2>\n");
    methodBuilder.addJavadoc("<dl>\n");
    for (Pair<String, TypeName> property : where.value1) {
        String rawName = method.findParameterNameByAlias(property.value0);
        methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
        methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
    }
    methodBuilder.addJavadoc("</dl>");
    methodBuilder.addJavadoc("\n\n");
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<dl>\n");
        methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        methodBuilder.addJavadoc("\n</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // dynamic conditions
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // method parameters
    if (methodParams.size() > 0) {
        for (Pair<String, TypeName> param : methodParams) {
            String resolvedName = method.findParameterAliasByName(param.value0);
            methodBuilder.addJavadoc("@param $L", param.value0);
            if (method.isThisDynamicWhereConditionsName(param.value0)) {
                methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
            } else if (where.value1.contains(new Pair<>(resolvedName, param.value1))) {
                methodBuilder.addJavadoc("\n\tis used as where parameter <strong>$L</strong>\n", "${" + resolvedName + "}");
            } else {
                methodBuilder.addJavadoc("\n\tis used as updated field <strong>$L</strong>\n", resolvedName);
            }
        }
    }
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    // define return value
    if (returnType == TypeName.VOID) {
    } else {
        methodBuilder.addJavadoc("\n");
        if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
            } else {
                methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
            }
        // methodBuilder.addCode("return result!=0;\n");
        } else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return number of updated records");
            } else {
                methodBuilder.addJavadoc("@return number of deleted records");
            }
        // methodBuilder.addCode("return result;\n");
        } else {
            // more than one listener found
            throw (new InvalidMethodSignException(method, "invalid return type"));
        }
        methodBuilder.addJavadoc("\n");
    }
}
Also used : Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) TypeName(com.squareup.javapoet.TypeName) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Pair(com.abubusoft.kripton.common.Pair)

Example 3 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class ModifyRawHelper method generateJavaDoc.

private void generateJavaDoc(final SQLiteModelMethod method, Builder methodBuilder, boolean updateMode) {
    List<Pair<String, TypeName>> methodParams = method.getParameters();
    final List<SQLProperty> updatedProperties = new ArrayList<>();
    final List<Pair<String, TypeName>> methodParamsUsedAsParameter = new ArrayList<>();
    // new
    String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnNameToUpdate(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            updatedProperties.add(tempProperty);
            return tempProperty.columnName;
        }

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
            AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
            methodParamsUsedAsParameter.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
            return "${" + bindParameterName + "}";
        }
    });
    if (updateMode) {
        methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n");
        // list of updated fields
        methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
        methodBuilder.addJavadoc("<ul>\n");
        for (SQLProperty property : updatedProperties) {
            methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
        }
        methodBuilder.addJavadoc("</ul>");
        methodBuilder.addJavadoc("\n\n");
    } else {
        methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n\n");
    }
    // list of where parameter
    methodBuilder.addJavadoc("<h2>Parameters:</h2>\n");
    methodBuilder.addJavadoc("<dl>\n");
    for (Pair<String, TypeName> property : methodParamsUsedAsParameter) {
        String rawName = method.findParameterNameByAlias(property.value0);
        methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
        methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
    }
    methodBuilder.addJavadoc("</dl>");
    methodBuilder.addJavadoc("\n\n");
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<dl>\n");
        methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        methodBuilder.addJavadoc("\n</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // dynamic conditions
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // method parameters
    if (methodParams.size() > 0) {
        for (Pair<String, TypeName> param : methodParams) {
            String resolvedName = method.findParameterAliasByName(param.value0);
            methodBuilder.addJavadoc("@param $L", param.value0);
            if (method.isThisDynamicWhereConditionsName(param.value0)) {
                methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
            } else {
                methodBuilder.addJavadoc("\n\tis used as for parameter <strong>$L</strong>\n", resolvedName);
            }
        }
    }
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    // define return value
    if (returnType == TypeName.VOID) {
    } else {
        methodBuilder.addJavadoc("\n");
        if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
            } else {
                methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
            }
            methodBuilder.addCode("return result!=0;\n");
        } else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return number of updated records");
            } else {
                methodBuilder.addJavadoc("@return number of deleted records");
            }
        // methodBuilder.addCode("return result;\n");
        } else {
            // more than one listener found
            throw (new InvalidMethodSignException(method, "invalid return type"));
        }
        methodBuilder.addJavadoc("\n");
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayList(java.util.ArrayList) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Pair(com.abubusoft.kripton.common.Pair)

Example 4 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class SelectBeanHelper method generateSpecializedPart.

@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    // List<SQLProperty> fields = fieldList.value1;
    // TypeName collectionClass;
    TypeName entityClass = typeName(entity.getElement());
    methodBuilder.addCode("\n");
    methodBuilder.addCode("$T resultBean=null;\n", entityClass);
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
    // generate index from columns
    methodBuilder.addCode("\n");
    {
        int i = 0;
        for (JQLProjection a : fieldList) {
            SQLProperty item = a.property;
            methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
            if (item.hasTypeAdapter()) {
                methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
            }
        }
    }
    methodBuilder.addCode("\n");
    methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
    // generate mapping
    int i = 0;
    for (JQLProjection a : fieldList) {
        SQLProperty item = a.property;
        if (item.isNullable()) {
            methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
        }
        SQLTransformer.cursor2Java(methodBuilder, entityClass, item, "resultBean", "_cursor", "index" + i + "");
        methodBuilder.addCode(";");
        if (item.isNullable()) {
            methodBuilder.addCode(" }");
        }
        methodBuilder.addCode("\n");
        i++;
    }
    methodBuilder.addCode("\n");
    methodBuilder.endControlFlow();
    methodBuilder.addCode("return resultBean;\n");
    // close try { open cursor
    methodBuilder.endControlFlow();
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 5 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class SelectPaginatedResultHelper method generateSpecializedPart.

@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    // List<SQLProperty> fields = fieldList.value1;
    TypeName entityClass = typeName(entity.getElement());
    methodBuilder.addCode("\n");
    methodBuilder.addStatement("$T<$T> resultList=new $T<$T>(_cursor.getCount())", List.class, entityClass, ArrayList.class, entityClass);
    methodBuilder.addStatement("$T resultBean=null", entityClass);
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
    // generate index from columns
    methodBuilder.addCode("\n");
    {
        int i = 0;
        for (JQLProjection a : fieldList) {
            SQLProperty item = a.property;
            methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
            if (item.hasTypeAdapter()) {
                methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
            }
        }
    }
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("do\n");
    methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
    // generate mapping
    int i = 0;
    for (JQLProjection a : fieldList) {
        SQLProperty item = a.property;
        if (item.isNullable()) {
            methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
        }
        SQLTransformer.cursor2Java(methodBuilder, typeName(entity.getElement()), item, "resultBean", "_cursor", "index" + i + "");
        methodBuilder.addCode(";");
        if (item.isNullable()) {
            methodBuilder.addCode(" }");
        }
        methodBuilder.addCode("\n");
        i++;
    }
    methodBuilder.addCode("\n");
    methodBuilder.addCode("resultList.add(resultBean);\n");
    methodBuilder.endControlFlow("while (_cursor.moveToNext())");
    methodBuilder.endControlFlow();
    methodBuilder.addCode("\n");
    methodBuilder.addCode("return resultList;\n");
    methodBuilder.endControlFlow();
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Aggregations

SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)41 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)23 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)22 TypeName (com.squareup.javapoet.TypeName)18 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)14 ArrayList (java.util.ArrayList)12 One (com.abubusoft.kripton.common.One)10 Pair (com.abubusoft.kripton.common.Pair)9 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)7 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)7 InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)6 ModelProperty (com.abubusoft.kripton.processor.core.ModelProperty)5 LinkedHashSet (java.util.LinkedHashSet)5 SQLiteStatement (android.database.sqlite.SQLiteStatement)4 SQLTypeAdapterUtils (com.abubusoft.kripton.common.SQLTypeAdapterUtils)4 PropertyNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException)4 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)4 BaseProcessorTest (base.BaseProcessorTest)3 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)3 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)3