Package org.fastnate.generator
Class EntitySqlGenerator
java.lang.Object
org.fastnate.generator.EntitySqlGenerator
- All Implemented Interfaces:
Closeable,AutoCloseable
Creates SQL statements for a set of entities using their JPA annotations.
Known limitations:
- Not all databases covered, see
GeneratorDialect. - Only tested with Hibernate.
- Author:
- Tobias Liefke
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final GeneratorContextUsed to keep the state of indices and to store any configuration.private final StatementsWriterThe target of any generated SQL statement, e.g. a file or database. -
Constructor Summary
ConstructorsConstructorDescriptionEntitySqlGenerator(GeneratorContext context, Writer writer) Creates a new instance for a output writer.EntitySqlGenerator(GeneratorContext context, Connection connection) Creates a new instance for a database connection.EntitySqlGenerator(GeneratorContext context, StatementsWriter writer) Creates a newEntitySqlGeneratorinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Writes any missing SQL and closes any open resources.protected <E> booleanfindEntity(E entity) Tries to find an entity in an online store.voidflush()Writes any open and alignment statements.Used to keep the state of indices and to store any configuration.The target of any generated SQL statement, e.g. a file or database.private static <E> booleanisPostponedInsert(List<Object> postInsertEntities, E entity) <E> voidmarkExistingEntities(Iterable<E> entities) Marks a set of entity references, where we don't know the ID in the database.<E> voidmarkExistingEntity(E entity, Number id) Marks an entity reference, where we know the ID in the database.<E> voidwrite(E entity) Creates the Import-SQL for an entity.private <E> voidCreates the Import-SQL for an entity.<E> voidCreates the SQL for the given entities.voidWrites all statements that are necessary to align ID generators in the database with the current IDs.voidwriteComment(String comment) Writes a SQL comment to the target writer.private <E> voidwriteInserts(E entity, List<Object> postponedEntities, EntityClass<E> classDescription, ColumnExpression discriminator) voidwritePlainStatement(String statement) Writes a plain SQL statement to the target writer.voidWrites a new line to the target to separate different sections in the SQL file.private <E,T> void writeTableEntities(E entity, List<Object> postponedEntities, Collection<Property<? super E, ?>> properties)
-
Field Details
-
context
Used to keep the state of indices and to store any configuration. -
writer
The target of any generated SQL statement, e.g. a file or database.
-
-
Constructor Details
-
EntitySqlGenerator
Creates a new instance for a database connection.- Parameters:
context- the current context that stores any indices and configurationconnection- the database connection- Throws:
SQLException- if the database is not accessible
-
EntitySqlGenerator
Creates a new instance for a output writer.- Parameters:
context- the current context that stores any indices and configurationwriter- the stream for the generated file
-
EntitySqlGenerator
Creates a newEntitySqlGeneratorinstance.- Parameters:
context- Used to keep the state of indices and to store any configuration.writer- The target of any generated SQL statement, e.g. a file or database.
-
-
Method Details
-
isPostponedInsert
-
close
Writes any missing SQL and closes any open resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if the target writer throws one
-
findEntity
Tries to find an entity in an online store. Usefull for writing online updates.- Parameters:
entity- the entity to check- Returns:
trueif the entity was found and has an id now- Throws:
IOException- if something wents wrong
-
flush
Writes any open and alignment statements.- Throws:
IOException- if the writer throws one
-
markExistingEntities
Marks a set of entity references, where we don't know the ID in the database. As we write every unknown entity to the SQL file, if it is referenced by one that is just written, we need to mark entities that exist already in the database (and where we possibly have no knowledge of the id of the entity).- Parameters:
entities- the entity to ignore duringwrite(Object)- Throws:
IOException- if the writer throws one
-
markExistingEntity
Marks an entity reference, where we know the ID in the database. As we write every unknown entity to the SQL file, if it is referenced by one that is just written, we need to mark entities that exist already in the database. In difference tomarkExistingEntities(Iterable), we know the id for the given entity.- Parameters:
entity- the entity to ignore duringwrite(Object)id- the id of the entity in
-
write
Creates the Import-SQL for an entity. If the entity was already written (has an id), it is updated (if nessecary).- Parameters:
entity- the entity to create the SQL for- Throws:
IOException- if the writer throws oneIllegalArgumentException- if the entity is invalid
-
write
Creates the Import-SQL for an entity. If the entity was already written, nothing happens.- Parameters:
entity- the entity to create the SQL forpostponedEntities- contains entities that will be written later and can be ignored- Throws:
IOException- if the writer throws one
-
write
Creates the SQL for the given entities.- Parameters:
entities- the entities for SQL creation- Throws:
IOException- if the writer throws one
-
writeAlignmentStatements
Writes all statements that are necessary to align ID generators in the database with the current IDs.- Throws:
IOException- if the target writer throws one
-
writeComment
Writes a SQL comment to the target writer. Shortcut forgetWriter().writeComment(comment).- Parameters:
comment- the comment to write- Throws:
IOException- if the writer throws one
-
writeInserts
private <E> void writeInserts(E entity, List<Object> postponedEntities, EntityClass<E> classDescription, ColumnExpression discriminator) throws IOException - Throws:
IOException
-
writePlainStatement
Writes a plain SQL statement to the target writer. Shortcut forgetWriter().writePlainStatement(getContext().getDialect(), statement).- Parameters:
statement- the statement to write- Throws:
IOException- if the writer throws one
-
writeSectionSeparator
Writes a new line to the target to separate different sections in the SQL file. Shortcut forgetWriter().writeSectionSeparator().- Throws:
IOException- if the writer throws such an exception
-
writeTableEntities
private <E,T> void writeTableEntities(E entity, List<Object> postponedEntities, Collection<Property<? super E, ?>> properties) throws IOException- Throws:
IOException
-
getContext
Used to keep the state of indices and to store any configuration. -
getWriter
The target of any generated SQL statement, e.g. a file or database.
-