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