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);
}
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());
}
}
}
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);
}
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;
}
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());
}
Aggregations