Search in sources :

Example 1 with DataGrainElement

use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.

the class CursorGenerator method generateCursor.

/**
 * Generate code for schema (grain) element.
 * @param ge Schema (grain) element
 * @param scorePath path to CelestaSQL file
 */
public void generateCursor(GrainElement ge, String scorePath) {
    final String sourcePackage = calcSourcePackage(ge, scorePath);
    if (sourcePackage.isEmpty()) {
        throw new CelestaException("Couldn't generate class file for %s.%s without package", ge.getGrain().getName(), ge.getName());
    }
    final String className = calcClassName(ge);
    final String columnsClassName = "Columns";
    boolean isVersionedGe = ge instanceof VersionedElement && ((VersionedElement) ge).isVersioned();
    ClassName classType = ClassName.bestGuess(className);
    TypeSpec.Builder cursorClass = buildClassDefinition(ge, classType);
    ClassName columnsClassType = classType.nestedClass(columnsClassName);
    cursorClass.addFields(buildMetaFields(ge));
    cursorClass.addMethods(buildConstructors(ge, columnsClassType));
    // FIELDS
    if (ge instanceof DataGrainElement) {
        DataGrainElement dge = (DataGrainElement) ge;
        FieldSpec columnsField = buildColumnsField(columnsClassType);
        cursorClass.addField(columnsField);
        cursorClass.addInitializerBlock(buildColumnsFiledInitializer(columnsField));
        List<FieldSpec> fieldSpecs = buildDataFields(dge);
        cursorClass.addFields(fieldSpecs);
        cursorClass.addMethods(generateGettersAndSetters(fieldSpecs, classType));
        cursorClass.addMethod(buildGetFieldValue(dge.getColumns()));
        cursorClass.addMethod(buildSetFieldValue(dge.getColumns()));
        StringBuilder parseResultOverridingMethodNameBuilder = new StringBuilder("_parseResult");
        Set<Column<?>> pk = Collections.emptySet();
        if (dge instanceof TableElement && !(dge instanceof ReadOnlyTable)) {
            TableElement te = (TableElement) dge;
            pk = new LinkedHashSet<>(te.getPrimaryKey().values());
            cursorClass.addMethod(buildCurrentKeyValues(pk));
            cursorClass.addMethod(buildTryGet(pk));
            cursorClass.addMethod(buildGet(pk));
            if (te instanceof Table) {
                parseResultOverridingMethodNameBuilder.append("Internal");
            }
        }
        final Map<String, ? extends ColumnMeta<?>> columns = dge.getColumns();
        MethodSpec buildParseResultMethod = buildParseResult(columns, parseResultOverridingMethodNameBuilder.toString(), isVersionedGe);
        cursorClass.addMethod(buildParseResultMethod);
        cursorClass.addMethod(buildClearBuffer(columns, pk));
        cursorClass.addMethod(buildCurrentValues(columns));
        cursorClass.addType(buildCursorColumnsAsInnerStaticClass(dge, columnsClassType));
        if (dge instanceof BasicTable) {
            BasicTable t = (BasicTable) dge;
            if (t instanceof Table) {
                cursorClass.addMethods(buildCalcBlobs(columns, className));
                cursorClass.addMethod(buildSetAutoIncrement(columns));
                cursorClass.addMethods(buildTriggerRegistration(classType));
            }
            cursorClass.addTypes(buildOptionFieldsAsInnerStaticClasses(t.getColumns().values()));
        }
        cursorClass.addMethods(buildCompileCopying(ge, classType, columns.keySet(), isVersionedGe));
        cursorClass.addMethod(buildIterator(classType));
    }
    cursorClass.addMethods(buildGrainNameAndObjectName(ge));
    JavaFile javaFile = JavaFile.builder(sourcePackage, cursorClass.build()).skipJavaLangImports(true).indent("    ").build();
    try {
        javaFile.writeTo(srcDir);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) BasicTable(ru.curs.celesta.score.BasicTable) ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) Table(ru.curs.celesta.score.Table) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) CelestaException(ru.curs.celesta.CelestaException) DataGrainElement(ru.curs.celesta.score.DataGrainElement) FieldSpec(com.squareup.javapoet.FieldSpec) TableElement(ru.curs.celesta.score.TableElement) BasicTable(ru.curs.celesta.score.BasicTable) VersionedElement(ru.curs.celesta.score.VersionedElement) StringColumn(ru.curs.celesta.score.StringColumn) Column(ru.curs.celesta.score.Column) IntegerColumn(ru.curs.celesta.score.IntegerColumn) ZonedDateTimeColumn(ru.curs.celesta.score.ZonedDateTimeColumn) BinaryColumn(ru.curs.celesta.score.BinaryColumn) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with DataGrainElement

use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.

the class CursorGenerator method buildCursorColumnsAsInnerStaticClass.

private TypeSpec buildCursorColumnsAsInnerStaticClass(DataGrainElement dge, ClassName columnsClassType) {
    TypeSpec.Builder builder = TypeSpec.classBuilder(columnsClassType).addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build()).addAnnotation(buildGeneratedAnnotation()).addAnnotation(CelestaGenerated.class);
    FieldSpec elementField = FieldSpec.builder(dge.getClass(), "element", Modifier.PRIVATE, Modifier.FINAL).build();
    builder.addField(elementField);
    builder.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(ICelesta.class, "celesta").addStatement("this.$N = celesta.getScore().getGrains().get($L).getElements($T.class).get($L)", elementField, GRAIN_FIELD_NAME, elementField.type, OBJECT_FIELD_NAME).build());
    dge.getColumns().entrySet().stream().filter(e -> !BinaryColumn.CELESTA_TYPE.equals(e.getValue().getCelestaType())).map(e -> {
        final String columnName = e.getKey();
        final TypeName columnType = ParameterizedTypeName.get(ColumnMeta.class, e.getValue().getJavaClass());
        return MethodSpec.methodBuilder(camelize(columnName)).addModifiers(Modifier.PUBLIC).returns(columnType).addStatement("return ($T) this.$N.getColumns().get($S)", columnType, elementField, columnName).build();
    }).forEach(builder::addMethod);
    return builder.build();
}
Also used : BasicCursor(ru.curs.celesta.dbutils.BasicCursor) Arrays(java.util.Arrays) Modifier(javax.lang.model.element.Modifier) CelestaException(ru.curs.celesta.CelestaException) ZonedDateTime(java.time.ZonedDateTime) ClassName(com.squareup.javapoet.ClassName) SequenceElement(ru.curs.celesta.score.SequenceElement) Grain(ru.curs.celesta.score.Grain) Parameter(ru.curs.celesta.score.Parameter) ColumnMeta(ru.curs.celesta.score.ColumnMeta) Generated(javax.annotation.Generated) StringColumn(ru.curs.celesta.score.StringColumn) ResultSet(java.sql.ResultSet) Map(java.util.Map) ICelesta(ru.curs.celesta.ICelesta) ZoneOffset(java.time.ZoneOffset) DataGrainElement(ru.curs.celesta.score.DataGrainElement) CelestaGenerated(ru.curs.celesta.dbutils.CelestaGenerated) ParameterSpec(com.squareup.javapoet.ParameterSpec) CallContext(ru.curs.celesta.CallContext) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Set(java.util.Set) Column(ru.curs.celesta.score.Column) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) Objects(java.util.Objects) List(java.util.List) ViewCursor(ru.curs.celesta.dbutils.ViewCursor) TriggerType(ru.curs.celesta.event.TriggerType) BasicTable(ru.curs.celesta.score.BasicTable) GrainElement(ru.curs.celesta.score.GrainElement) BasicDataAccessor(ru.curs.celesta.dbutils.BasicDataAccessor) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) IntegerColumn(ru.curs.celesta.score.IntegerColumn) MaterializedViewCursor(ru.curs.celesta.dbutils.MaterializedViewCursor) ZonedDateTimeColumn(ru.curs.celesta.score.ZonedDateTimeColumn) FieldSpec(com.squareup.javapoet.FieldSpec) TableElement(ru.curs.celesta.score.TableElement) LocalDateTime(java.time.LocalDateTime) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) Cursor(ru.curs.celesta.dbutils.Cursor) HashMap(java.util.HashMap) View(ru.curs.celesta.score.View) ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Sequence(ru.curs.celesta.dbutils.Sequence) BinaryColumn(ru.curs.celesta.score.BinaryColumn) ParameterizedView(ru.curs.celesta.score.ParameterizedView) CodeBlock(com.squareup.javapoet.CodeBlock) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) CursorIterator(ru.curs.celesta.dbutils.CursorIterator) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedViewCursor(ru.curs.celesta.dbutils.ParameterizedViewCursor) MaterializedView(ru.curs.celesta.score.MaterializedView) Table(ru.curs.celesta.score.Table) File(java.io.File) Consumer(java.util.function.Consumer) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) VersionedElement(ru.curs.celesta.score.VersionedElement) DateTimeFormatter(java.time.format.DateTimeFormatter) NamedElement(ru.curs.celesta.score.NamedElement) FileResource(ru.curs.celesta.score.io.FileResource) ReadOnlyTableCursor(ru.curs.celesta.dbutils.ReadOnlyTableCursor) Collections(java.util.Collections) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ColumnMeta(ru.curs.celesta.score.ColumnMeta) FieldSpec(com.squareup.javapoet.FieldSpec) ICelesta(ru.curs.celesta.ICelesta) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 3 with DataGrainElement

use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.

the class BasicCursor method getFrom.

// TODO:Must be refactored by new util class FromClauseGenerator
protected FromClause getFrom() {
    FromClause result = new FromClause();
    DataGrainElement ge = meta();
    result.setGe(ge);
    result.setExpression(db().tableString(ge.getGrain().getName(), ge.getName()));
    return result;
}
Also used : FromClause(ru.curs.celesta.dbutils.query.FromClause) DataGrainElement(ru.curs.celesta.score.DataGrainElement)

Example 4 with DataGrainElement

use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.

the class ParameterizedViewCursor method getFrom.

@Override
protected FromClause getFrom() {
    FromClause result = new FromClause();
    DataGrainElement ge = meta();
    result.setGe(ge);
    result.setExpression(db().getCallFunctionSql(meta));
    List<Object> paramValues = meta().getParameters().keySet().stream().map(pName -> parameters.get(pName)).collect(Collectors.toList());
    result.setParameters(paramValues);
    return result;
}
Also used : ColumnMeta(ru.curs.celesta.score.ColumnMeta) Arrays(java.util.Arrays) List(java.util.List) CallContext(ru.curs.celesta.CallContext) CelestaException(ru.curs.celesta.CelestaException) FromClause(ru.curs.celesta.dbutils.query.FromClause) Map(java.util.Map) Set(java.util.Set) DataGrainElement(ru.curs.celesta.score.DataGrainElement) ParameterizedView(ru.curs.celesta.score.ParameterizedView) Collectors(java.util.stream.Collectors) ParseException(ru.curs.celesta.score.ParseException) FromClause(ru.curs.celesta.dbutils.query.FromClause) DataGrainElement(ru.curs.celesta.score.DataGrainElement)

Aggregations

DataGrainElement (ru.curs.celesta.score.DataGrainElement)4 CelestaException (ru.curs.celesta.CelestaException)3 ClassName (com.squareup.javapoet.ClassName)2 FieldSpec (com.squareup.javapoet.FieldSpec)2 JavaFile (com.squareup.javapoet.JavaFile)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 CallContext (ru.curs.celesta.CallContext)2 FromClause (ru.curs.celesta.dbutils.query.FromClause)2 BasicTable (ru.curs.celesta.score.BasicTable)2 BinaryColumn (ru.curs.celesta.score.BinaryColumn)2 Column (ru.curs.celesta.score.Column)2 ColumnMeta (ru.curs.celesta.score.ColumnMeta)2 IntegerColumn (ru.curs.celesta.score.IntegerColumn)2