Search in sources :

Example 1 with Table

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

the class DbUpdater method updateTable.

boolean updateTable(BasicTable t, List<DbFkInfo> dbFKeys) {
    // If table was compiled with option NO AUTOUPDATE then nothing is to be done
    if (!t.isAutoUpdate()) {
        return false;
    }
    final Connection conn = schemaCursor.callContext().getConn();
    if (!dbAdaptor.tableExists(conn, t.getGrain().getName(), t.getName())) {
        // Table doesn't exist in the DB, create it from scratch.
        dbAdaptor.createTable(conn, t);
        return true;
    }
    DbPkInfo pkInfo;
    Set<String> dbColumns = dbAdaptor.getColumns(conn, t);
    boolean modified = updateColumns(t, conn, dbColumns, dbFKeys);
    // For versioned tables synchronize 'recversion' field
    if (t instanceof Table) {
        Table tab = (Table) t;
        if (tab.isVersioned()) {
            if (dbColumns.contains(VersionedElement.REC_VERSION)) {
                DbColumnInfo ci = dbAdaptor.getColumnInfo(conn, tab.getRecVersionField());
                if (!ci.reflects(tab.getRecVersionField())) {
                    dbAdaptor.updateColumn(conn, tab.getRecVersionField(), ci);
                    modified = true;
                }
            } else {
                dbAdaptor.createColumn(conn, tab.getRecVersionField());
                modified = true;
            }
        }
    }
    // Once again check the primary key, and if needed (in case it doesn't exist or
    // had been dropped) create it.
    pkInfo = dbAdaptor.getPKInfo(conn, t);
    if (pkInfo.isEmpty()) {
        dbAdaptor.createPK(conn, t);
    }
    dbAdaptor.updateVersioningTrigger(conn, t);
    return modified;
}
Also used : Table(ru.curs.celesta.score.Table) BasicTable(ru.curs.celesta.score.BasicTable) Connection(java.sql.Connection) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) DbPkInfo(ru.curs.celesta.dbutils.meta.DbPkInfo) VersionString(ru.curs.celesta.score.VersionString)

Example 2 with Table

use of ru.curs.celesta.score.Table 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 3 with Table

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

the class FieldsLookup method validate.

public void validate() {
    if (filtered instanceof Table && filtering instanceof Table) {
        Set<List<Integer>> columnOrdersInIndicesSet = getColumnOrdersInIndicesSet(fields, (BasicTable) filtered);
        Set<List<Integer>> otherColumnOrdersInIndicesSet = getColumnOrdersInIndicesSet(otherFields, (BasicTable) filtering);
        columnOrdersInIndicesSet.retainAll(otherColumnOrdersInIndicesSet);
        columnOrdersInIndicesSet.stream().forEach(Collections::sort);
        Optional<List<Integer>> match = columnOrdersInIndicesSet.stream().filter(l -> l.equals(IntStream.range(0, l.size()).boxed().collect(Collectors.toList()))).findAny();
        match.orElseThrow(() -> new CelestaException("'In' filter validation failed. Fields matched for the filter for tables %s.%s and %s.%s" + " are not covered by pks or indices on these tables.", filtered.getGrain().getName(), filtered.getName(), filtering.getGrain().getName(), filtering.getName()));
    }
}
Also used : BasicCursor(ru.curs.celesta.dbutils.BasicCursor) IntStream(java.util.stream.IntStream) CelestaException(ru.curs.celesta.CelestaException) Set(java.util.Set) Cursor(ru.curs.celesta.dbutils.Cursor) View(ru.curs.celesta.score.View) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Table(ru.curs.celesta.score.Table) ParseException(ru.curs.celesta.score.ParseException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ColumnMeta(ru.curs.celesta.score.ColumnMeta) List(java.util.List) ViewCursor(ru.curs.celesta.dbutils.ViewCursor) BasicTable(ru.curs.celesta.score.BasicTable) Optional(java.util.Optional) DataGrainElement(ru.curs.celesta.score.DataGrainElement) Collections(java.util.Collections) Index(ru.curs.celesta.score.Index) Table(ru.curs.celesta.score.Table) BasicTable(ru.curs.celesta.score.BasicTable) ArrayList(java.util.ArrayList) List(java.util.List) Collections(java.util.Collections) CelestaException(ru.curs.celesta.CelestaException)

Example 4 with Table

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

the class CreateCursorTest method createCursorTest.

@Test
public void createCursorTest() throws ParseException {
    Table wtable = g.getTable("wtable", Table.class);
    assertNotNull(wtable);
    Cursor wtCursor = Cursor.create(wtable, cc());
    assertNotNull(wtCursor);
    wtCursor.deleteAll();
    wtCursor.setValue("data", "test");
    wtCursor.insert();
    wtCursor = Cursor.create(wtable, cc());
    assertEquals("test", new CursorIterator<>(wtCursor).next().getValue("data"));
    try (WtableCursor cursor = new WtableCursor(cc())) {
        assertEquals("test", cursor.iterator().next().getData());
    }
}
Also used : ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) Table(ru.curs.celesta.score.Table) WtableCursor(createcursors.WtableCursor) WtableCursor(createcursors.WtableCursor) PvtableCursor(createcursors.PvtableCursor) MvtableCursor(createcursors.MvtableCursor) Test(org.junit.jupiter.api.Test) AbstractCelestaTest(ru.curs.celesta.AbstractCelestaTest)

Aggregations

Table (ru.curs.celesta.score.Table)4 BasicTable (ru.curs.celesta.score.BasicTable)3 CelestaException (ru.curs.celesta.CelestaException)2 DataGrainElement (ru.curs.celesta.score.DataGrainElement)2 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 MvtableCursor (createcursors.MvtableCursor)1 PvtableCursor (createcursors.PvtableCursor)1 WtableCursor (createcursors.WtableCursor)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1