Class EntitySqlGenerator

java.lang.Object
org.fastnate.generator.EntitySqlGenerator
All Implemented Interfaces:
Closeable, AutoCloseable

public class EntitySqlGenerator extends Object implements Closeable
Creates SQL statements for a set of entities using their JPA annotations. Known limitations:
Author:
Tobias Liefke
  • Field Details

    • context

      private final GeneratorContext context
      Used to keep the state of indices and to store any configuration.
    • writer

      private final StatementsWriter writer
      The target of any generated SQL statement, e.g. a file or database.
  • Constructor Details

    • EntitySqlGenerator

      public EntitySqlGenerator(GeneratorContext context, Connection connection) throws SQLException
      Creates a new instance for a database connection.
      Parameters:
      context - the current context that stores any indices and configuration
      connection - the database connection
      Throws:
      SQLException - if the database is not accessible
    • EntitySqlGenerator

      public EntitySqlGenerator(GeneratorContext context, Writer writer)
      Creates a new instance for a output writer.
      Parameters:
      context - the current context that stores any indices and configuration
      writer - the stream for the generated file
    • EntitySqlGenerator

      public EntitySqlGenerator(GeneratorContext context, StatementsWriter writer)
      Creates a new EntitySqlGenerator instance.
      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

      private static <E> boolean isPostponedInsert(List<Object> postInsertEntities, E entity)
    • close

      public void close() throws IOException
      Writes any missing SQL and closes any open resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if the target writer throws one
    • findEntity

      protected <E> boolean findEntity(E entity) throws IOException
      Tries to find an entity in an online store. Usefull for writing online updates.
      Parameters:
      entity - the entity to check
      Returns:
      true if the entity was found and has an id now
      Throws:
      IOException - if something wents wrong
    • flush

      public void flush() throws IOException
      Writes any open and alignment statements.
      Throws:
      IOException - if the writer throws one
    • markExistingEntities

      public <E> void markExistingEntities(Iterable<E> entities) throws IOException
      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 during write(Object)
      Throws:
      IOException - if the writer throws one
    • markExistingEntity

      public <E> void markExistingEntity(E entity, Number id)
      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 to markExistingEntities(Iterable), we know the id for the given entity.
      Parameters:
      entity - the entity to ignore during write(Object)
      id - the id of the entity in
    • write

      public <E> void write(E entity) throws IOException
      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 one
      IllegalArgumentException - if the entity is invalid
    • write

      private <E> void write(E entity, List<Object> postponedEntities) throws IOException
      Creates the Import-SQL for an entity. If the entity was already written, nothing happens.
      Parameters:
      entity - the entity to create the SQL for
      postponedEntities - contains entities that will be written later and can be ignored
      Throws:
      IOException - if the writer throws one
    • write

      public <E> void write(Iterable<? extends E> entities) throws IOException
      Creates the SQL for the given entities.
      Parameters:
      entities - the entities for SQL creation
      Throws:
      IOException - if the writer throws one
    • writeAlignmentStatements

      public void writeAlignmentStatements() throws IOException
      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

      public void writeComment(String comment) throws IOException
      Writes a SQL comment to the target writer. Shortcut for getWriter().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

      public void writePlainStatement(String statement) throws IOException
      Writes a plain SQL statement to the target writer. Shortcut for getWriter().writePlainStatement(getContext().getDialect(), statement).
      Parameters:
      statement - the statement to write
      Throws:
      IOException - if the writer throws one
    • writeSectionSeparator

      public void writeSectionSeparator() throws IOException
      Writes a new line to the target to separate different sections in the SQL file. Shortcut for getWriter().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

      public GeneratorContext getContext()
      Used to keep the state of indices and to store any configuration.
    • getWriter

      public StatementsWriter getWriter()
      The target of any generated SQL statement, e.g. a file or database.