use of ru.curs.celesta.score.ReadOnlyTable 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.ReadOnlyTable in project celesta by CourseOrchestra.
the class CreateCursorTest method createReadOnlyCursorTest.
@Test
public void createReadOnlyCursorTest() throws ParseException {
ReadOnlyTable roTable = g.getTable("roTable", ReadOnlyTable.class);
assertNotNull(roTable);
ReadOnlyTableCursor rotCursor = ReadOnlyTableCursor.create(roTable, cc());
assertNotNull(rotCursor);
assertEquals(0, rotCursor.count());
assertFalse(new CursorIterator<>(rotCursor).hasNext());
}
Aggregations