5353import org .jooq .Record ;
5454import org .jooq .RecordType ;
5555import org .jooq .Result ;
56+ import org .jooq .SQLDialect ;
5657import org .jooq .SelectJoinStep ;
5758import org .jooq .SelectQuery ;
5859import org .jooq .Table ;
5960import org .jooq .impl .DSL ;
6061
62+ import static org .apache .polygene .entitystore .sql .MixinTable .INDEX_COLUMN_NAME ;
63+ import static org .apache .polygene .entitystore .sql .MixinTable .NAME_COLUMN_NAME ;
64+ import static org .apache .polygene .entitystore .sql .MixinTable .REFERENCE_COLUMN_NAME ;
65+ import static org .apache .polygene .entitystore .sql .SqlType .makeField ;
66+
6167/**
6268 * This class handles all the dealing with the main table, called the Entities Table.
6369 * <p>
64- * This is the table that holds the {@link BaseEntity} rows, and joins with the different so called
65- * {@link MixinTable}, which handles one table per Mixin type.
70+ * This is the table that holds the {@link BaseEntity} rows, and joins with the different so called
71+ * {@link MixinTable}, which handles one table per Mixin type.
6672 * </p>
6773 */
6874@ SuppressWarnings ( "WeakerAccess" )
6975public class EntitiesTable
70- implements TableFields
7176{
77+ private static final String VALUEID_COLUMN_NAME = "_value_id" ;
78+ private static final String TYPE_COLUMN_NAME = "_type" ;
79+ private static final String VERSION_COLUMN_NAME = "_version" ;
80+ private static final String APPLICATIONVERSION_COLUMN_NAME = "_app_version" ;
81+
7282 private static final Predicate <? super Class <?>> NOT_COMPOSITE = type -> !( type .equals ( Composite .class ) || type .equals ( EntityComposite .class ) );
7383 private static final Predicate <? super Class <?>> NOT_HASIDENTITY = type -> !( type .equals ( HasIdentity .class ) );
84+ private final SQLDialect dialect ;
7485 private Map <EntityCompositeType , Set <Class <?>>> mixinTypeCache = new ConcurrentHashMap <>();
7586 private Map <Class <?>, MixinTable > mixinTablesCache = new ConcurrentHashMap <>();
7687
7788 private final Table <Record > entitiesTable ;
78- private JooqDslContext dsl ;
89+
90+ private Field <String > valueIdentityColumn ;
91+ private Field <String > typeNameColumn ;
92+ private Field <String > versionColumn ;
93+ private Field <String > applicationVersionColumn ;
94+
95+ private final JooqDslContext dsl ;
7996 private final TypesTable types ;
80- private String applicationVersion ;
81- private Serialization serialization ;
97+ private final String applicationVersion ;
98+ private final Serialization serialization ;
8299 private boolean replacementStrategy = false ; // Figure out later if we should support both and if so, how.
83100
84- EntitiesTable ( JooqDslContext dsl , TypesTable types , String applicationVersion , String entitiesTableName , Serialization serialization )
101+ EntitiesTable ( JooqDslContext dsl ,
102+ SQLDialect dialect ,
103+ TypesTable types ,
104+ String applicationVersion ,
105+ String entitiesTableName ,
106+ Serialization serialization )
85107 {
86108 this .dsl = dsl ;
109+ this .dialect = dialect ;
87110 this .types = types ;
88111 this .applicationVersion = applicationVersion ;
89112 this .serialization = serialization ;
90113 entitiesTable = dsl .tableOf ( entitiesTableName );
114+ valueIdentityColumn = makeField ( VALUEID_COLUMN_NAME , String .class , dialect );
115+ typeNameColumn = makeField ( TYPE_COLUMN_NAME , String .class , dialect );
116+ versionColumn = makeField ( VERSION_COLUMN_NAME , String .class , dialect );
117+ applicationVersionColumn = makeField ( APPLICATIONVERSION_COLUMN_NAME , String .class , dialect );
91118 }
92119
93120 public BaseEntity fetchEntity ( EntityReference reference , ModuleDescriptor module )
94121 {
95122 Result <Record > baseEntityResult = dsl
96123 .selectFrom ( entitiesTable )
97- .where ( identityColumn .eq ( reference .identity ().toString () ) )
124+ .where ( types . identityColumn () .eq ( reference .identity ().toString () ) )
98125 .fetch ();
99126
100127 if ( baseEntityResult .isEmpty () )
@@ -112,10 +139,10 @@ protected BaseEntity toBaseEntity( Record row, ModuleDescriptor module )
112139 result .type = findEntityDescriptor ( typeName , module );
113140 result .version = row .field ( versionColumn ).get ( row );
114141 result .applicationVersion = row .field ( applicationVersionColumn ).get ( row );
115- result .identity = StringIdentity .identityOf ( row .field ( identityColumn ).get ( row ) );
142+ result .identity = StringIdentity .identityOf ( row .field ( types . identityColumn () ).get ( row ) );
116143 result .currentValueIdentity = EntityReference .parseEntityReference ( row .field ( valueIdentityColumn ).get ( row ) ).identity ();
117- result .modifedAt = Instant .ofEpochMilli ( row .field ( modifiedColumn ).get ( row ).getTime () );
118- result .createdAt = Instant .ofEpochMilli ( row .field ( createdColumn ).get ( row ).getTime () );
144+ result .modifedAt = Instant .ofEpochMilli ( row .field ( types . modifiedColumn () ).get ( row ).getTime () );
145+ result .createdAt = Instant .ofEpochMilli ( row .field ( types . createdColumn () ).get ( row ).getTime () );
119146 return result ;
120147 }
121148
@@ -172,7 +199,7 @@ void modifyEntity( DefaultEntityState state, BaseEntity baseEntity, EntityStoreU
172199
173200 private MixinTable findMixinTable ( Class <?> type , EntityDescriptor entityDescriptor )
174201 {
175- return mixinTablesCache .computeIfAbsent ( type , t -> new MixinTable ( dsl , types , type , entityDescriptor , serialization ) );
202+ return mixinTablesCache .computeIfAbsent ( type , t -> new MixinTable ( dsl , dialect , types , type , entityDescriptor , serialization ) );
176203 }
177204
178205 private Set <Class <?>> mixinsOf ( Stream <? extends AssociationDescriptor > stream )
@@ -216,9 +243,9 @@ BaseEntity createNewBaseEntity( EntityReference reference, EntityDescriptor desc
216243 String typeName = descriptor .primaryType ().getName ();
217244 Instant currentTime = uow .currentTime ();
218245 dsl .insertInto ( entitiesTable )
219- .set ( identityColumn , reference .identity ().toString () )
220- .set ( createdColumn , new Timestamp ( currentTime .toEpochMilli () ))
221- .set ( modifiedColumn , new Timestamp ( currentTime .toEpochMilli ()) )
246+ .set ( types . identityColumn () , reference .identity ().toString () )
247+ .set ( types . createdColumn () , new Timestamp ( currentTime .toEpochMilli () ) )
248+ .set ( types . modifiedColumn () , new Timestamp ( currentTime .toEpochMilli () ) )
222249 .set ( valueIdentityColumn , valueIdentity )
223250 .set ( typeNameColumn , typeName )
224251 .set ( versionColumn , "1" )
@@ -243,11 +270,11 @@ private void updateBaseEntity( BaseEntity entity, EntityStoreUnitOfWork uow )
243270 entity .currentValueIdentity = StringIdentity .identityOf ( UUID .randomUUID ().toString () );
244271 }
245272 dsl .update ( entitiesTable )
246- .set ( modifiedColumn , new Timestamp ( uow .currentTime ().toEpochMilli () ) )
273+ .set ( types . modifiedColumn () , new Timestamp ( uow .currentTime ().toEpochMilli () ) )
247274 .set ( valueIdentityColumn , entity .currentValueIdentity .toString () )
248275 .set ( versionColumn , entity .version )
249276 .set ( applicationVersionColumn , applicationVersion )
250- .where ( identityColumn .eq ( entity .identity .toString () ) )
277+ .where ( types . identityColumn () .eq ( entity .identity .toString () ) )
251278 .execute ();
252279 }
253280
@@ -321,32 +348,32 @@ public void fetchAssociations( BaseEntity entity, EntityDescriptor entityDescrip
321348 SelectQuery <Record > query = from .where ( identityColumnOf ( entitiesTable ).eq ( reference ) ).getQuery ();
322349 Result <Record > result = query .fetch ();
323350 RecordType <Record > recordType = result .recordType ();
324- result .forEach ( record ->
325- {
326- AssociationValue value = new AssociationValue ();
327- if ( recordType . indexOf ( referenceColumn ) >= 0 )
328- {
329- // some many-to-many association found.
330- if ( recordType . indexOf ( nameColumn ) >= 0 )
331- {
332- // NamedAssociations found.
333- value . name = QualifiedName . fromClass ( entityDescriptor .primaryType (), record . getValue ( nameColumn ) );
334- }
335- if ( recordType .indexOf ( indexColumn ) >= 0 )
336- {
337- // ManyAssociations found.
338- value .position = record .getValue ( indexColumn );
339- }
340- value .reference = record .getValue ( referenceColumn );
341- }
342- consume . accept ( value );
343- } );
351+ result .forEach ( record -> {
352+ AssociationValue value = new AssociationValue ();
353+ if ( recordType . indexOf ( MixinTable . REFERENCE_COLUMN_NAME ) >= 0 )
354+ {
355+ // some many-to-many association found.
356+ if ( recordType . indexOf ( NAME_COLUMN_NAME ) >= 0 )
357+ {
358+ // NamedAssociations found.
359+ value . name = QualifiedName . fromClass ( entityDescriptor . primaryType (), ( String ) record . get ( NAME_COLUMN_NAME ) );
360+ EntityCompositeType valueType = entityDescriptor .valueType ( );
361+ }
362+ if ( recordType .indexOf ( INDEX_COLUMN_NAME ) >= 0 )
363+ {
364+ // ManyAssociations found.
365+ value .position = ( String ) record .get ( INDEX_COLUMN_NAME );
366+ }
367+ value .reference = ( String ) record .get ( REFERENCE_COLUMN_NAME );
368+ consume . accept ( value );
369+ }
370+ } );
344371 }
345372
346373 private Field <String > identityColumnOf ( Table <Record > joinedTable )
347374 {
348375 String name = joinedTable .getName ();
349- String identity = identityColumn .getName ();
376+ String identity = types . identityColumn () .getName ();
350377 return DSL .field ( DSL .name ( name , identity ), String .class );
351378 }
352379
@@ -395,4 +422,18 @@ public void removeEntity( EntityReference entityReference, EntityDescriptor desc
395422 assocTables .forEach ( table -> dsl .delete ( table ).where ( identityColumnOf ( table ).eq ( valueId ) ).execute () );
396423 }
397424 }
425+
426+ void create ()
427+ {
428+ dsl .createTableIfNotExists ( entitiesTable )
429+ .column ( types .identityColumn () )
430+ .column ( types .createdColumn () )
431+ .column ( typeNameColumn )
432+ .column ( applicationVersionColumn )
433+ .column ( versionColumn )
434+ .column ( types .modifiedColumn () )
435+ .column ( valueIdentityColumn )
436+ .constraint ( DSL .primaryKey ( types .identityColumn () ) )
437+ .execute ();
438+ }
398439}
0 commit comments