Search in sources :

Example 1 with StringColumn

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

the class FirebirdDdlGenerator method createParameterizedView.

@Override
List<String> createParameterizedView(ParameterizedView pv) {
    SQLGenerator gen = getViewSQLGenerator();
    StringWriter sw = new StringWriter();
    PrintWriter bw = new PrintWriter(sw);
    try {
        pv.selectScript(bw, gen);
    } catch (IOException e) {
        throw new CelestaException(e);
    }
    bw.flush();
    // Calculating of max available varchar length for input params
    Map<String, String> textParamMap = new ParameterizedViewTypeResolver<>(pv, ViewColumnType.TEXT, StringColumn.class, sc -> sc.isMax() ? 0 : sc.getLength(), (oldLength, newLength) -> {
        if (oldLength == 0 || newLength == 0) {
            return 0;
        } else {
            return Math.max(oldLength, newLength);
        }
    }, length -> {
        if (length == 0) {
            return "blob sub_type text";
        } else {
            return String.format("varchar(%d)", length);
        }
    }).resolveTypes();
    final class ScaleAndPrecision {

        private int precision;

        private int scale;

        private ScaleAndPrecision(int precision, int scale) {
            this.precision = precision;
            this.scale = scale;
        }
    }
    // Calculating of max available varchar length for input params
    Map<String, String> decimalParamMap = new ParameterizedViewTypeResolver<>(pv, ViewColumnType.DECIMAL, DecimalColumn.class, dc -> new ScaleAndPrecision(dc.getPrecision(), dc.getScale()), (oldValue, newValue) -> new ScaleAndPrecision(Math.max(oldValue.precision, newValue.precision), Math.max(oldValue.scale, newValue.scale)), scaleAndPrecision -> String.format("%s(%s,%s)", ColumnDefinerFactory.getColumnDefiner(getType(), DecimalColumn.class).dbFieldType(), scaleAndPrecision.precision, scaleAndPrecision.scale)).resolveTypes();
    String inParams = pv.getParameters().entrySet().stream().map(e -> {
        final String type;
        ViewColumnType viewColumnType = e.getValue().getType();
        if (ViewColumnType.TEXT == viewColumnType) {
            type = textParamMap.get(e.getKey());
        } else if (ViewColumnType.DECIMAL == viewColumnType) {
            type = decimalParamMap.get(e.getKey());
        } else {
            type = ColumnDefinerFactory.getColumnDefiner(getType(), CELESTA_TYPES_COLUMN_CLASSES.get(e.getValue().getType().getCelestaType())).dbFieldType();
        }
        return e.getKey() + " " + type;
    }).collect(Collectors.joining(", "));
    String outParams = pv.getColumns().entrySet().stream().map(e -> {
        final String type;
        ViewColumnMeta<?> viewColumnMeta = e.getValue();
        if (ViewColumnType.TEXT == viewColumnMeta.getColumnType()) {
            StringColumn sc = (StringColumn) pv.getColumnRef(viewColumnMeta.getName());
            if (sc.isMax()) {
                type = "blob sub_type text";
            } else {
                type = String.format("varchar(%d)", sc.getLength());
            }
        } else if (ViewColumnType.DECIMAL == viewColumnMeta.getColumnType()) {
            DecimalColumn dc = (DecimalColumn) pv.getColumnRef(viewColumnMeta.getName());
            if (dc != null) {
                type = String.format("%s(%s,%s)", ColumnDefinerFactory.getColumnDefiner(getType(), DecimalColumn.class).dbFieldType(), dc.getPrecision(), dc.getScale());
            } else {
                Sum sum = (Sum) pv.getAggregateColumns().get(viewColumnMeta.getName());
                BinaryTermOp binaryTermOp = (BinaryTermOp) sum.getTerm();
                List<DecimalColumn> decimalColumns = binaryTermOp.getOperands().stream().filter(op -> op instanceof FieldRef).map(FieldRef.class::cast).filter(fr -> DecimalColumn.class.equals(fr.getColumn().getClass())).map(FieldRef::getColumn).map(DecimalColumn.class::cast).collect(Collectors.toList());
                int maxPrecision = decimalColumns.stream().mapToInt(DecimalColumn::getPrecision).max().getAsInt();
                int maxScale = decimalColumns.stream().mapToInt(DecimalColumn::getScale).max().getAsInt();
                type = String.format("%s(%s,%s)", ColumnDefinerFactory.getColumnDefiner(getType(), DecimalColumn.class).dbFieldType(), maxPrecision, maxScale);
            }
        } else {
            type = ColumnDefinerFactory.getColumnDefiner(getType(), CELESTA_TYPES_COLUMN_CLASSES.get(e.getValue().getCelestaType())).dbFieldType();
        }
        return String.format("\"%s\" %s", e.getKey(), type);
    }).collect(Collectors.joining(", "));
    String intoList = pv.getColumns().keySet().stream().map(c -> String.format("\"%s\"", c)).map(":"::concat).collect(Collectors.joining(", "));
    String selectSql = sw.toString();
    String sql = String.format("CREATE PROCEDURE " + tableString(pv.getGrain().getName(), pv.getName()) + "(%s)%n" + "  RETURNS (%s)%n" + "  AS%n" + "  BEGIN%n" + "  FOR %s%n" + "  INTO %s%n" + "    DO BEGIN%n" + "      SUSPEND;%n" + "    END%n" + "  END", inParams, outParams, selectSql, intoList);
    return Collections.singletonList(sql);
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) DateTimeColumn(ru.curs.celesta.score.DateTimeColumn) CelestaException(ru.curs.celesta.CelestaException) SequenceElement(ru.curs.celesta.score.SequenceElement) TableRef(ru.curs.celesta.score.TableRef) Grain(ru.curs.celesta.score.Grain) Matcher(java.util.regex.Matcher) DbIndexInfo(ru.curs.celesta.dbutils.meta.DbIndexInfo) AbstractView(ru.curs.celesta.score.AbstractView) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) StringColumn(ru.curs.celesta.score.StringColumn) Map(java.util.Map) ForeignKey(ru.curs.celesta.score.ForeignKey) BigInteger(java.math.BigInteger) ViewColumnMeta(ru.curs.celesta.score.ViewColumnMeta) PrintWriter(java.io.PrintWriter) DBType(ru.curs.celesta.DBType) SQLGenerator(ru.curs.celesta.score.SQLGenerator) Set(java.util.Set) BooleanColumn(ru.curs.celesta.score.BooleanColumn) Column(ru.curs.celesta.score.Column) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) ParameterizedViewSelectStmt(ru.curs.celesta.score.ParameterizedViewSelectStmt) ColumnDefinerFactory(ru.curs.celesta.dbutils.adaptors.column.ColumnDefinerFactory) List(java.util.List) Stream(java.util.stream.Stream) TriggerType(ru.curs.celesta.event.TriggerType) BasicTable(ru.curs.celesta.score.BasicTable) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) IntegerColumn(ru.curs.celesta.score.IntegerColumn) FieldRef(ru.curs.celesta.score.FieldRef) FirebirdAdaptor(ru.curs.celesta.dbutils.adaptors.FirebirdAdaptor) ParameterRef(ru.curs.celesta.score.ParameterRef) ViewColumnType(ru.curs.celesta.score.ViewColumnType) TableElement(ru.curs.celesta.score.TableElement) DecimalColumn(ru.curs.celesta.score.DecimalColumn) HashMap(java.util.HashMap) TriggerQuery(ru.curs.celesta.event.TriggerQuery) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ALTER_TABLE(ru.curs.celesta.dbutils.adaptors.constants.CommonConstants.ALTER_TABLE) Count(ru.curs.celesta.score.Count) DBAdaptor(ru.curs.celesta.dbutils.adaptors.DBAdaptor) ParameterizedView(ru.curs.celesta.score.ParameterizedView) LinkedList(java.util.LinkedList) LogicValuedExpr(ru.curs.celesta.score.LogicValuedExpr) Expr(ru.curs.celesta.score.Expr) SchemalessFunctions.getVersionCheckTriggerName(ru.curs.celesta.dbutils.adaptors.function.SchemalessFunctions.getVersionCheckTriggerName) StringWriter(java.io.StringWriter) IOException(java.io.IOException) MaterializedView(ru.curs.celesta.score.MaterializedView) BinaryTermOp(ru.curs.celesta.score.BinaryTermOp) AbstractMap(java.util.AbstractMap) VersionedElement(ru.curs.celesta.score.VersionedElement) Sum(ru.curs.celesta.score.Sum) Collections(java.util.Collections) Index(ru.curs.celesta.score.Index) SchemalessFunctions.generateSequenceTriggerName(ru.curs.celesta.dbutils.adaptors.function.SchemalessFunctions.generateSequenceTriggerName) StringColumn(ru.curs.celesta.score.StringColumn) ViewColumnType(ru.curs.celesta.score.ViewColumnType) FieldRef(ru.curs.celesta.score.FieldRef) BinaryTermOp(ru.curs.celesta.score.BinaryTermOp) DecimalColumn(ru.curs.celesta.score.DecimalColumn) SQLGenerator(ru.curs.celesta.score.SQLGenerator) Sum(ru.curs.celesta.score.Sum) IOException(java.io.IOException) CelestaException(ru.curs.celesta.CelestaException) StringWriter(java.io.StringWriter) ViewColumnMeta(ru.curs.celesta.score.ViewColumnMeta) PrintWriter(java.io.PrintWriter)

Example 2 with StringColumn

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

the class PostgresDdlGenerator method updateColType.

@Override
void updateColType(Column<?> c, DbColumnInfo actual, List<String> sqlList) {
    @SuppressWarnings("unchecked") final Class<? extends Column<?>> cClass = (Class<Column<?>>) c.getClass();
    String colType;
    if (c.getClass() == StringColumn.class) {
        StringColumn sc = (StringColumn) c;
        colType = sc.isMax() ? "text" : String.format("%s(%s)", ColumnDefinerFactory.getColumnDefiner(getType(), cClass).dbFieldType(), sc.getLength());
    } else if (c.getClass() == DecimalColumn.class) {
        DecimalColumn dc = (DecimalColumn) c;
        colType = String.format("%s(%s,%s)", ColumnDefinerFactory.getColumnDefiner(getType(), cClass).dbFieldType(), dc.getPrecision(), dc.getScale());
    } else {
        colType = ColumnDefinerFactory.getColumnDefiner(getType(), cClass).dbFieldType();
    }
    StringBuilder alterSql = new StringBuilder(String.format(ALTER_TABLE + tableString(c.getParentTable().getGrain().getName(), c.getParentTable().getName()) + " ALTER COLUMN \"%s\" TYPE %s", c.getName(), colType));
    // If type doesn't match
    if (c.getClass() != actual.getType()) {
        if (c.getClass() == IntegerColumn.class) {
            alterSql.append(String.format(" USING (%s::integer);", c.getQuotedName()));
        } else if (c.getClass() == BooleanColumn.class) {
            alterSql.append(String.format(" USING (%s::boolean);", c.getQuotedName()));
        }
        sqlList.add(alterSql.toString());
    } else if (c.getClass() == StringColumn.class) {
        StringColumn sc = (StringColumn) c;
        if (sc.isMax() != actual.isMax() || sc.getLength() != actual.getLength()) {
            sqlList.add(alterSql.toString());
        }
    } else if (c.getClass() == DecimalColumn.class) {
        DecimalColumn dc = (DecimalColumn) c;
        if (dc.getPrecision() != actual.getLength() || dc.getScale() != dc.getScale()) {
            sqlList.add(alterSql.toString());
        }
    }
}
Also used : StringColumn(ru.curs.celesta.score.StringColumn) BooleanColumn(ru.curs.celesta.score.BooleanColumn) DecimalColumn(ru.curs.celesta.score.DecimalColumn)

Example 3 with StringColumn

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

the class OraZonedDateTimeColumnDefiner method getInternalDefinition.

@Override
public String getInternalDefinition(Column<?> c) {
    StringColumn ic = (StringColumn) c;
    String fieldType = ic.isMax() ? "nclob" : String.format("%s(%s)", dbFieldType(), ic.getLength());
    return join(c.getQuotedName(), fieldType);
}
Also used : StringColumn(ru.curs.celesta.score.StringColumn)

Example 4 with StringColumn

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

the class FireBirdZonedDateTimeColumnDefiner method getDefaultDefinition.

@Override
public String getDefaultDefinition(Column c) {
    StringColumn ic = (StringColumn) c;
    String defaultStr = "";
    if (ic.getDefaultValue() != null) {
        defaultStr = DEFAULT + StringColumn.quoteString(ic.getDefaultValue());
    }
    return defaultStr;
}
Also used : StringColumn(ru.curs.celesta.score.StringColumn)

Example 5 with StringColumn

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

the class AbstractAdaptorTest method updateColumn.

@Test
public void updateColumn() throws ParseException, IOException, SQLException {
    // NULL/NOT NULL и DEFAULT (простые)
    DbColumnInfo c;
    Column<?> col;
    // To test transforms on non-empty table
    insertRow(conn, t, 17);
    col = t.getColumn("attrInt");
    c = dba.getColumnInfo(conn, col);
    assertEquals("attrInt", c.getName());
    assertSame(IntegerColumn.class, c.getType());
    assertEquals(true, c.isNullable());
    assertEquals("3", c.getDefaultValue());
    col.setNullableAndDefault(false, "55");
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("attrInt", c.getName());
    assertSame(IntegerColumn.class, c.getType());
    assertEquals(false, c.isNullable());
    assertEquals("55", c.getDefaultValue());
    col.setNullableAndDefault(false, null);
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("", c.getDefaultValue());
    // f6 varchar(MAX) not null default 'abc',
    col = t.getColumn("f6");
    c = dba.getColumnInfo(conn, col);
    assertEquals("f6", c.getName());
    assertSame(StringColumn.class, c.getType());
    assertEquals(false, c.isNullable());
    assertEquals("'abc'", c.getDefaultValue());
    assertEquals(true, c.isMax());
    StringColumn scol = (StringColumn) col;
    scol.setLength("234");
    scol.setNullableAndDefault(true, "'eee'");
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("f6", c.getName());
    assertSame(StringColumn.class, c.getType());
    assertEquals(true, c.isNullable());
    assertEquals("'eee'", c.getDefaultValue());
    assertEquals(false, c.isMax());
    assertEquals(234, c.getLength());
    // f8 datetime default '20130401',
    col = t.getColumn("f8");
    DateTimeColumn dcol = (DateTimeColumn) col;
    assertFalse(dcol.isGetdate());
    c = dba.getColumnInfo(conn, col);
    assertEquals("f8", c.getName());
    assertSame(DateTimeColumn.class, c.getType());
    assertEquals(true, c.isNullable());
    assertEquals("'20130401'", c.getDefaultValue());
    col.setNullableAndDefault(true, "getdate");
    assertTrue(dcol.isGetdate());
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("f8", c.getName());
    assertSame(DateTimeColumn.class, c.getType());
    assertEquals(true, c.isNullable());
    assertEquals("GETDATE()", c.getDefaultValue());
    col.setNullableAndDefault(true, null);
    assertFalse(dcol.isGetdate());
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("f8", c.getName());
    assertSame(DateTimeColumn.class, c.getType());
    assertEquals(true, c.isNullable());
    assertEquals("", c.getDefaultValue());
    col.setNullableAndDefault(false, "getdate");
    assertTrue(dcol.isGetdate());
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("f8", c.getName());
    assertSame(DateTimeColumn.class, c.getType());
    assertEquals(false, c.isNullable());
    assertEquals("GETDATE()", c.getDefaultValue());
    // f10 image default 0xFFAAFFAAFF
    col = t.getColumn("f10");
    c = dba.getColumnInfo(conn, col);
    assertTrue(col.isNullable());
    assertTrue(c.isNullable());
    assertEquals("0xFFAAFFAAFF", c.getDefaultValue());
    col.setNullableAndDefault(false, "0xBBCC");
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("0xBBCC", c.getDefaultValue());
    assertFalse(c.isNullable());
    // f12 decimal(11, 7)
    col = t.getColumn("f12");
    c = dba.getColumnInfo(conn, col);
    assertTrue(col.isNullable());
    assertTrue(c.isNullable());
    assertEquals("", c.getDefaultValue());
    assertEquals(11, c.getLength());
    assertEquals(7, c.getScale());
    col.setNullableAndDefault(false, "155.0216");
    DecimalColumn dc = (DecimalColumn) col;
    dc.setPrecision(7);
    dc.setScale(4);
    dba.updateColumn(conn, col, c);
    c = dba.getColumnInfo(conn, col);
    assertEquals("155.0216", c.getDefaultValue());
    assertEquals(7, c.getLength());
    assertEquals(4, c.getScale());
    assertFalse(c.isNullable());
}
Also used : StringColumn(ru.curs.celesta.score.StringColumn) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) DecimalColumn(ru.curs.celesta.score.DecimalColumn) DateTimeColumn(ru.curs.celesta.score.DateTimeColumn) ZonedDateTimeColumn(ru.curs.celesta.score.ZonedDateTimeColumn) Test(org.junit.jupiter.api.Test)

Aggregations

StringColumn (ru.curs.celesta.score.StringColumn)10 DecimalColumn (ru.curs.celesta.score.DecimalColumn)5 DbColumnInfo (ru.curs.celesta.dbutils.meta.DbColumnInfo)4 BooleanColumn (ru.curs.celesta.score.BooleanColumn)4 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 DateTimeColumn (ru.curs.celesta.score.DateTimeColumn)2 IntegerColumn (ru.curs.celesta.score.IntegerColumn)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 BigInteger (java.math.BigInteger)1 Connection (java.sql.Connection)1 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1