Search in sources :

Example 1 with ReadOnlyTable

use of ru.curs.celesta.score.ReadOnlyTable 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 ReadOnlyTable

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

the class CreateCursorTest method createReadOnlyCursorTest.

@Test
public void createReadOnlyCursorTest() throws ParseException {
    ReadOnlyTable roTable = g.getTable("roTable", ReadOnlyTable.class);
    assertNotNull(roTable);
    ReadOnlyTableCursor rotCursor = ReadOnlyTableCursor.create(roTable, cc());
    assertNotNull(rotCursor);
    assertEquals(0, rotCursor.count());
    assertFalse(new CursorIterator<>(rotCursor).hasNext());
}
Also used : ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) Test(org.junit.jupiter.api.Test) AbstractCelestaTest(ru.curs.celesta.AbstractCelestaTest)

Aggregations

ReadOnlyTable (ru.curs.celesta.score.ReadOnlyTable)2 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 IOException (java.io.IOException)1 Test (org.junit.jupiter.api.Test)1 AbstractCelestaTest (ru.curs.celesta.AbstractCelestaTest)1 CelestaException (ru.curs.celesta.CelestaException)1 BasicTable (ru.curs.celesta.score.BasicTable)1 BinaryColumn (ru.curs.celesta.score.BinaryColumn)1 Column (ru.curs.celesta.score.Column)1 DataGrainElement (ru.curs.celesta.score.DataGrainElement)1 IntegerColumn (ru.curs.celesta.score.IntegerColumn)1 StringColumn (ru.curs.celesta.score.StringColumn)1 Table (ru.curs.celesta.score.Table)1 TableElement (ru.curs.celesta.score.TableElement)1 VersionedElement (ru.curs.celesta.score.VersionedElement)1 ZonedDateTimeColumn (ru.curs.celesta.score.ZonedDateTimeColumn)1