Search in sources :

Example 1 with ColumnMeta

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

the class CursorGenerator method buildCurrentValues.

private MethodSpec buildCurrentValues(Map<String, ? extends ColumnMeta<?>> columns) {
    ArrayTypeName resultType = ArrayTypeName.of(Object.class);
    MethodSpec.Builder builder = MethodSpec.methodBuilder("_currentValues").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(resultType);
    String spec = "return new Object[] {" + columns.values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "}";
    builder.addStatement(spec, columns.values().stream().map(ColumnMeta::getName).map(this::camelize).toArray());
    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) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 2 with ColumnMeta

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

the class CursorGenerator method buildClearBuffer.

private MethodSpec buildClearBuffer(Map<String, ? extends ColumnMeta<?>> columns, Set<Column<?>> pk) {
    ParameterSpec param = ParameterSpec.builder(boolean.class, "withKeys").build();
    MethodSpec.Builder builder = MethodSpec.methodBuilder("_clearBuffer").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(param);
    if (!pk.isEmpty()) {
        builder.beginControlFlow("if ($N)", param.name);
        pk.stream().forEach(c -> builder.addStatement("this.$N = null", camelize(c.getName())));
        builder.endControlFlow();
    }
    columns.entrySet().stream().filter(e -> !pk.contains(e.getValue())).forEach(e -> builder.addStatement("this.$N = null", camelize(e.getKey())));
    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) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 3 with ColumnMeta

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

the class CursorGenerator method buildSetAutoIncrement.

private MethodSpec buildSetAutoIncrement(Map<String, ? extends ColumnMeta<?>> columns) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("_setAutoIncrement").addModifiers(Modifier.PROTECTED).addAnnotation(Override.class);
    ParameterSpec param = ParameterSpec.builder(int.class, "val").build();
    builder.addParameter(param);
    columns.entrySet().stream().filter(e -> e.getValue() instanceof IntegerColumn).filter(e -> ((IntegerColumn) e.getValue()).getSequence() != null).findAny().ifPresent(e -> builder.addStatement("this.$N = $N", camelize(e.getKey()), param.name));
    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) IntegerColumn(ru.curs.celesta.score.IntegerColumn) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 4 with ColumnMeta

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

the class BasicCursor method orderBy.

/**
 * Sets sorting.
 *
 * @param names array of fields for sorting
 */
@Deprecated
public final void orderBy(String... names) {
    ColumnMeta<?>[] columns = new ColumnMeta<?>[names.length];
    for (int i = 0; i < names.length; i++) {
        final String name = names[i];
        Matcher m = COLUMN_NAME.matcher(name);
        if (!m.matches()) {
            throw new CelestaException("orderby() argument '%s' should match pattern <column name> [ASC|DESC]", name);
        }
        final String colName = m.group(1);
        final String colOrdering = Optional.ofNullable(m.group(2)).map(String::trim).orElse(null);
        ColumnMeta<?> column = validateColumnName(colName);
        if ("asc".equalsIgnoreCase(colOrdering)) {
            column = column.asc();
        } else if ("desc".equalsIgnoreCase(colOrdering)) {
            column = column.desc();
        }
        columns[i] = column;
    }
    orderBy(columns);
}
Also used : ColumnMeta(ru.curs.celesta.score.ColumnMeta) Matcher(java.util.regex.Matcher) CelestaException(ru.curs.celesta.CelestaException)

Example 5 with ColumnMeta

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

the class NavigationQueriesMakerTest method test6.

@Test
public void test6() {
    Params p = new Params(a("A"), a(false), a(true));
    p.setNullsFirst(false);
    WhereTermsMaker c = new WhereTermsMaker(p);
    assertEquals("((\"A\" > ?) or (\"A\" is null))", c.getWhereTerm('>').getWhere());
    Map<String, AbstractFilter> filters = new HashMap<>();
    filters.put("A", new SingleValue(4));
    p.setFilters(filters);
    // Range filter must makes the system treat field as not nullable!
    assertEquals("((\"A\" = ?) and (\"A\" > ?))", c.getWhereTerm('>').getWhere());
    filters.put("A", new Range(4, 5));
    assertEquals("((\"A\" between ? and ?) and (\"A\" > ?))", c.getWhereTerm('>').getWhere());
    filters.put("A", new Filter("null|'foo'", new ColumnMeta<Void>() {

        @Override
        public String getName() {
            return null;
        }

        @Override
        public String jdbcGetterName() {
            return null;
        }

        @Override
        public String getCelestaType() {
            return StringColumn.VARCHAR;
        }

        @Override
        public Class<?> getJavaClass() {
            return String.class;
        }

        @Override
        public boolean isNullable() {
            return true;
        }

        @Override
        public String getCelestaDoc() {
            return "";
        }
    }));
    filters.put("B", new SingleValue(1));
    assertEquals("((\"B\" = ?) and (\"A\" is null or \"A\" = 'foo') and ((\"A\" > ?) or (\"A\" is null)))", c.getWhereTerm('>').getWhere());
}
Also used : SingleValue(ru.curs.celesta.dbutils.filter.SingleValue) WhereTermsMaker(ru.curs.celesta.dbutils.term.WhereTermsMaker) AbstractFilter(ru.curs.celesta.dbutils.filter.AbstractFilter) ColumnMeta(ru.curs.celesta.score.ColumnMeta) HashMap(java.util.HashMap) AbstractFilter(ru.curs.celesta.dbutils.filter.AbstractFilter) Filter(ru.curs.celesta.dbutils.filter.Filter) Range(ru.curs.celesta.dbutils.filter.Range) Test(org.junit.jupiter.api.Test)

Aggregations

ColumnMeta (ru.curs.celesta.score.ColumnMeta)5 HashMap (java.util.HashMap)4 CelestaException (ru.curs.celesta.CelestaException)4 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)3 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)3 ClassName (com.squareup.javapoet.ClassName)3 CodeBlock (com.squareup.javapoet.CodeBlock)3 FieldSpec (com.squareup.javapoet.FieldSpec)3 JavaFile (com.squareup.javapoet.JavaFile)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 ParameterSpec (com.squareup.javapoet.ParameterSpec)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)3 File (java.io.File)3 IOException (java.io.IOException)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Timestamp (java.sql.Timestamp)3