use of ru.curs.celesta.score.SequenceElement in project celesta by CourseOrchestra.
the class FirebirdAdaptor method getCurrentIdent.
@Override
public int getCurrentIdent(Connection conn, BasicTable t) {
IntegerColumn idColumn = t.getPrimaryKey().values().stream().filter(c -> c instanceof IntegerColumn).map(c -> (IntegerColumn) c).filter(ic -> ic.getSequence() != null).findFirst().get();
final SequenceElement s = idColumn.getSequence();
String curValueProcName = sequenceCurValueProcString(s.getGrain().getName(), s.getName());
String sql = String.format("EXECUTE PROCEDURE %s(null)", curValueProcName);
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
throw new CelestaException(e.getMessage());
}
}
use of ru.curs.celesta.score.SequenceElement in project celesta by CourseOrchestra.
the class CursorGenerator method buildConstructors.
private static List<MethodSpec> buildConstructors(GrainElement ge, TypeName columnsClassType) {
List<MethodSpec> results = new ArrayList<>();
ParameterSpec contextParam = ParameterSpec.builder(CallContext.class, "context").build();
ParameterSpec fieldsParam = ParameterSpec.builder(ParameterizedTypeName.get(Set.class, String.class), "fields").build();
ParameterSpec columnsParam = ParameterSpec.builder(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(ColumnMeta.class), WildcardTypeName.subtypeOf(Object.class))), "columns").build();
ParameterSpec parametersParam = ParameterSpec.builder(ParameterizedTypeName.get(Map.class, String.class, Object.class), "parameters").build();
Supplier<MethodSpec.Builder> msp = () -> MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(contextParam);
// Common constructor
MethodSpec.Builder builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(parametersParam);
builder.addStatement("super(context, parameters)");
} else {
builder.addStatement("super(context)");
}
results.add(builder.build());
if (ge instanceof SequenceElement) {
return results;
}
// Constructor with columns limitation
builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(parametersParam);
builder.addParameter(columnsParam).varargs();
builder.addStatement("super(context, parameters, columns)");
} else {
builder.addParameter(columnsParam).varargs();
builder.addStatement("super(context, columns)");
}
results.add(builder.build());
// Deprecated constructor with fields limitation
builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(fieldsParam);
builder.addParameter(parametersParam);
builder.addStatement("super(context, fields, parameters)");
} else {
builder.addParameter(fieldsParam);
builder.addStatement("super(context, fields)");
}
results.add(builder.addAnnotation(Deprecated.class).build());
// ParameterizedView constructors
if (ge instanceof ParameterizedView) {
ParameterizedView pv = (ParameterizedView) ge;
builder = msp.get();
for (Parameter parameter : pv.getParameters().values()) {
builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
}
String spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "))";
builder.addStatement(spec, pv.getParameters().keySet().toArray());
results.add(builder.build());
builder = msp.get();
for (Parameter parameter : pv.getParameters().values()) {
builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
}
builder.addParameter(columnsParam).varargs();
spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "), columns)";
builder.addStatement(spec, pv.getParameters().keySet().toArray());
results.add(builder.build());
results.add(getParameterizedViewTypedConstructorHelper(pv));
}
return results;
}
use of ru.curs.celesta.score.SequenceElement in project celesta by CourseOrchestra.
the class AbstractAdaptorTest method testCreateAndAlterSequence.
@Test
void testCreateAndAlterSequence() throws Exception {
Grain g = score.getGrain(GRAIN_NAME);
SequenceElement sequence = g.getElement("testSequence", SequenceElement.class);
if (dba.sequenceExists(conn, g.getName(), sequence.getName()))
dba.dropSequence(conn, sequence);
// Sequence not exists
assertFalse(dba.sequenceExists(conn, g.getName(), sequence.getName()));
dba.createSequence(conn, sequence);
assertTrue(dba.sequenceExists(conn, g.getName(), sequence.getName()));
DbSequenceInfo sequenceInfo = dba.getSequenceInfo(conn, sequence);
assertFalse(sequenceInfo.reflects(sequence));
assertAll(() -> assertEquals(1L, sequenceInfo.getIncrementBy()), () -> assertEquals(5L, sequenceInfo.getMinValue()), () -> assertEquals(Long.MAX_VALUE, sequenceInfo.getMaxValue()), () -> assertEquals(false, sequenceInfo.isCycle()));
assertEquals(5, dba.nextSequenceValue(conn, sequence));
assertEquals(6, dba.nextSequenceValue(conn, sequence));
// Modifying of increment by
sequence.getArguments().put(SequenceElement.Argument.INCREMENT_BY, 2L);
assertTrue(sequenceInfo.reflects(sequence));
dba.alterSequence(conn, sequence);
DbSequenceInfo sequenceInfo2 = dba.getSequenceInfo(conn, sequence);
assertFalse(sequenceInfo2.reflects(sequence));
assertAll(() -> assertEquals(2L, sequenceInfo2.getIncrementBy()), () -> assertEquals(5L, sequenceInfo2.getMinValue()), () -> assertEquals(Long.MAX_VALUE, sequenceInfo2.getMaxValue()), () -> assertEquals(false, sequenceInfo2.isCycle()));
// Altering to short cycle
sequence.getArguments().put(SequenceElement.Argument.INCREMENT_BY, 1L);
sequence.getArguments().put(SequenceElement.Argument.MINVALUE, 5L);
sequence.getArguments().put(SequenceElement.Argument.MAXVALUE, 7L);
sequence.getArguments().put(SequenceElement.Argument.CYCLE, true);
assertTrue(sequenceInfo.reflects(sequence));
dba.alterSequence(conn, sequence);
DbSequenceInfo sequenceInfo3 = dba.getSequenceInfo(conn, sequence);
assertFalse(sequenceInfo3.reflects(sequence));
assertAll(() -> assertEquals(1L, sequenceInfo3.getIncrementBy()), () -> assertEquals(5L, sequenceInfo3.getMinValue()), () -> assertEquals(7L, sequenceInfo3.getMaxValue()), () -> assertEquals(true, sequenceInfo3.isCycle()));
dba.dropSequence(conn, sequence);
}
use of ru.curs.celesta.score.SequenceElement in project celesta by CourseOrchestra.
the class AbstractAdaptorTest method testColumnUpdateWithDefaultSequence.
@Test
void testColumnUpdateWithDefaultSequence() throws Exception {
Grain g = score.getGrain(GRAIN_NAME);
SequenceElement sequence = g.getElement("testSequence", SequenceElement.class);
SequenceElement sequence2 = g.getElement("testSequence2", SequenceElement.class);
BasicTable table = g.getElement("tableForTestSequence", BasicTable.class);
try {
if (dba.tableExists(conn, g.getName(), table.getName()))
dba.dropTable(conn, table);
if (dba.sequenceExists(conn, g.getName(), sequence.getName()))
dba.dropSequence(conn, sequence);
if (dba.sequenceExists(conn, g.getName(), sequence2.getName()))
dba.dropSequence(conn, sequence2);
dba.createSequence(conn, sequence);
dba.createSequence(conn, sequence2);
dba.createTable(conn, table);
IntegerColumn id = (IntegerColumn) table.getColumn("id");
final DbColumnInfo idInfo1 = dba.getColumnInfo(conn, table.getColumn("id"));
assertAll(() -> assertTrue(idInfo1.reflects(id)), () -> assertEquals(IntegerColumn.class, idInfo1.getType()), () -> assertEquals("NEXTVAL(testSequence)".toLowerCase(), idInfo1.getDefaultValue().toLowerCase()));
id.setNullableAndDefault(false, "NEXTVAL(" + sequence2.getName() + ")");
assertFalse(idInfo1.reflects(id));
dba.updateColumn(conn, id, idInfo1);
final DbColumnInfo idInfo2 = dba.getColumnInfo(conn, table.getColumn("id"));
assertAll(() -> assertTrue(idInfo2.reflects(id)), () -> assertEquals(IntegerColumn.class, idInfo2.getType()), () -> assertEquals("NEXTVAL(testSequence2)".toLowerCase(), idInfo2.getDefaultValue().toLowerCase()));
id.setNullableAndDefault(false, "5");
assertFalse(idInfo2.reflects(id));
dba.updateColumn(conn, id, idInfo2);
final DbColumnInfo idInfo3 = dba.getColumnInfo(conn, table.getColumn("id"));
assertAll(() -> assertTrue(idInfo3.reflects(id)), () -> assertEquals(IntegerColumn.class, idInfo3.getType()), () -> assertEquals("5".toLowerCase(), idInfo3.getDefaultValue().toLowerCase()));
id.setNullableAndDefault(false, "NEXTVAL(" + sequence.getName() + ")");
assertFalse(idInfo3.reflects(id));
dba.updateColumn(conn, id, idInfo3);
final DbColumnInfo idInfo4 = dba.getColumnInfo(conn, table.getColumn("id"));
assertAll(() -> assertTrue(idInfo4.reflects(id)), () -> assertEquals(IntegerColumn.class, idInfo4.getType()), () -> assertEquals("NEXTVAL(testSequence)".toLowerCase(), idInfo4.getDefaultValue().toLowerCase()));
IntegerColumn numb = (IntegerColumn) table.getColumn("numb");
final DbColumnInfo numbInfo1 = dba.getColumnInfo(conn, numb);
assertAll(() -> assertTrue(numbInfo1.reflects(numb)), () -> assertEquals(IntegerColumn.class, numbInfo1.getType()), () -> assertTrue(numbInfo1.getDefaultValue().isEmpty()));
numb.setNullableAndDefault(false, "NEXTVAL(" + sequence2.getName() + ")");
assertFalse(numbInfo1.reflects(numb));
dba.updateColumn(conn, numb, numbInfo1);
final DbColumnInfo numbInfo2 = dba.getColumnInfo(conn, numb);
assertAll(() -> assertTrue(numbInfo2.reflects(numb)), () -> assertEquals(IntegerColumn.class, numbInfo2.getType()), () -> assertEquals("NEXTVAL(testSequence2)".toLowerCase(), numbInfo2.getDefaultValue().toLowerCase()));
} finally {
if (dba.tableExists(conn, g.getName(), table.getName()))
dba.dropTable(conn, table);
if (dba.sequenceExists(conn, g.getName(), sequence.getName()))
dba.dropSequence(conn, sequence);
if (dba.sequenceExists(conn, g.getName(), sequence2.getName()))
dba.dropSequence(conn, sequence2);
}
}
use of ru.curs.celesta.score.SequenceElement in project celesta by CourseOrchestra.
the class AbstractAdaptorTest method testInitDataForMaterializedView.
@Test
public void testInitDataForMaterializedView() throws Exception {
Grain g = score.getGrain(GRAIN_NAME);
SequenceElement ts = g.getElement("tableForInitMvData_id", SequenceElement.class);
BasicTable t = g.getElement("tableForInitMvData", BasicTable.class);
MaterializedView mv = g.getElement("mViewForInit", MaterializedView.class);
try {
// Могли остаться от незавершившегося теста
try {
dba.dropTable(conn, t);
} catch (CelestaException e) {
conn.rollback();
}
try {
dba.dropSequence(conn, ts);
} catch (CelestaException e) {
conn.rollback();
}
try {
dba.dropTable(conn, mv);
} catch (CelestaException e) {
conn.rollback();
}
dba.createSequence(conn, ts);
dba.createTable(conn, t);
dba.createTable(conn, mv);
boolean[] nullsMask = { true, false, false, false };
LocalDateTime d = LocalDateTime.now();
Object[] rowData = { null, "A", 5, Date.from(d.toInstant(ZoneOffset.UTC)) };
List<ParameterSetter> program = new ArrayList<>();
try (PreparedStatement pstmt = dba.getInsertRecordStatement(conn, t, nullsMask, program)) {
int i = 1;
for (ParameterSetter ps : program) {
ps.execute(pstmt, i++, rowData, 0);
}
pstmt.execute();
d = d.plusSeconds(1);
Object[] rowData2 = { null, "A", 5, Date.from(d.toInstant(ZoneOffset.UTC)) };
i = 1;
for (ParameterSetter ps : program) {
ps.execute(pstmt, i++, rowData2, 0);
}
pstmt.execute();
assertEquals(0, getCount(conn, mv));
Object[] secondRowData = { null, "B", 3, d };
i = 1;
for (ParameterSetter ps : program) {
ps.execute(pstmt, i++, secondRowData, 0);
}
pstmt.execute();
pstmt.close();
assertEquals(0, getCount(conn, mv));
dba.initDataForMaterializedView(conn, mv);
assertEquals(2, getCount(conn, mv));
dba.initDataForMaterializedView(conn, mv);
assertEquals(2, getCount(conn, mv));
}
FromClause from = new FromClause();
from.setGe(mv);
from.setExpression(dba.tableString(mv.getGrain().getName(), mv.getName()));
try (PreparedStatement pstmt = dba.getRecordSetStatement(conn, from, "", "\"var\"", 0, 0, Collections.emptySet())) {
ResultSet rs = pstmt.executeQuery();
rs.next();
assertEquals("A", rs.getString("var"));
assertEquals(10, rs.getInt("s"));
assertEquals(2, rs.getInt(MaterializedView.SURROGATE_COUNT));
rs.next();
assertEquals("B", rs.getString("var"));
assertEquals(3, rs.getInt("s"));
assertEquals(1, rs.getInt(MaterializedView.SURROGATE_COUNT));
}
} catch (Exception e) {
throw e;
} finally {
if (dba.tableExists(conn, g.getName(), mv.getName())) {
dba.dropTable(conn, mv);
}
if (dba.tableExists(conn, g.getName(), t.getName())) {
dba.dropTable(conn, t);
}
if (dba.sequenceExists(conn, g.getName(), ts.getName())) {
dba.dropSequence(conn, ts);
}
}
}
Aggregations