Class DBMigration<M extends DBRow>
- java.lang.Object
-
- nz.co.gregs.dbvolution.query.RowDefinition
-
- nz.co.gregs.dbvolution.DBMigration<M>
-
- All Implemented Interfaces:
Serializable
public class DBMigration<M extends DBRow> extends RowDefinition implements Serializable
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAsOptionalTables(DBRow... examples)
Add the rows as optional tables in the query.DBMigration<M>
addTablesAndExpressions(DBQuery query)
DBMigration<M>
copy()
Long
count(DBRow... rows)
Count the rows on the database without retrieving the rows.void
createAllRows(DBRow... extraExamples)
List<M>
getAllRows(DBRow... extraExamples)
Gets all the migrated rows using conditions in the DBMigration and the supplied examples.boolean
getBlankQueryAllowed()
boolean
getCartesianJoinsAllowed()
M
getMapper()
List<DBRow>
getOptionalTables()
List<M>
getRows(DBRow... rows)
Gets all the report rows of the migration limited by the supplied example rows.List<M>
getRowsHaving(DBRow[] rows, BooleanExpression... conditions)
Gets all the report rows of this DBMigration limited by the supplied example rows but reduce the result to only those that match the conditions.SortProvider[]
getSortColumns()
Returns the list of sort columnsString
getSQLForCount(DBRow... rows)
Returns the SQL query that will used to count the rows returned for the supplied DBReportString
getSQLForInsert(DBRow... rows)
Generates and returns the actual SQL to be used by this DBQueryInsert to insert the queried rows.String
getSQLForQuery(DBRow... rows)
Generates and returns the actual SQL to be used by this DBQueryInsert to select the rows to insert.DBMigration<M>
setBlankQueryAllowed(Boolean setting)
Change the Default Setting of Disallowing Blank QueriesDBMigration<M>
setCartesianJoinAllowed(Boolean setting)
Suppresses Cartesian join error protection.String
toString()
static <MAPPER extends DBRow>
DBMigration<MAPPER>using(DBDatabase database, MAPPER migrationMapper)
Create a migration that uses the mapper provided.DBMigrationValidation.Results
validateAllRows(DBRow... extraExamples)
Perform a migration validation using DBMigrationValidation.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from class nz.co.gregs.dbvolution.query.RowDefinition
column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, column, getAutoFillingPropertyWrappers, getColumnPropertyWrapperDefinitions, getColumnPropertyWrappers, getColumnQueryableDatatypes, getFieldNames, getFieldValues, getPropertyWrapperOf, getPropertyWrapperOf, getReturnColumns, getTableVariantAlias, getWrapper, setReturnColumns
-
-
-
-
Method Detail
-
copy
public DBMigration<M> copy()
-
using
public static <MAPPER extends DBRow> DBMigration<MAPPER> using(DBDatabase database, MAPPER migrationMapper)
Create a migration that uses the mapper provided.Mapper uses a subclass of a particular DBRow to create rows for that DBRow and insert them into the table.
Central to this concept is using other existing data in table, represented by their own DBRow classes, transformed using DBvolution expressions
public static class MigrateToFight extends Fight { public Villain baddy = new Villain(); public Hero goody = new Hero(); { baddy.name.permittedPattern("Dr%"); hero = goody.column(goody.name).asExpressionColumn(); villain = baddy.column(baddy.name).asExpressionColumn(); } } database.createTable(new Fight()); DBMigration migration = DBMigration.using(new MigrateToFight()); migration.createAllRows(database);
- Type Parameters:
MAPPER
- the transformation to apply- Parameters:
database
- the database to work withmigrationMapper
- the transformation class- Returns:
- a DBMigration object
-
addTablesAndExpressions
public DBMigration<M> addTablesAndExpressions(DBQuery query)
-
getAllRows
public List<M> getAllRows(DBRow... extraExamples) throws SQLException, AccidentalCartesianJoinException, AccidentalBlankQueryException
Gets all the migrated rows using conditions in the DBMigration and the supplied examples.- Parameters:
extraExamples
- extra rows defining additional criteria- Returns:
- a list of DBReport instances representing the results of the report query. 1 Database exceptions may be thrown
- Throws:
SQLException
- java.sql.SQLExceptionAccidentalBlankQueryException
- thrown if no conditions have been set and blank queries have not been explicitly allowedAccidentalCartesianJoinException
-
toString
public String toString()
- Overrides:
toString
in classRowDefinition
-
getRows
public List<M> getRows(DBRow... rows) throws SQLException, AccidentalCartesianJoinException, AccidentalBlankQueryException
Gets all the report rows of the migration limited by the supplied example rows.All supplied rows should be using a DBRow subclass that is included in the report.
Builtin report limitation will be used, the example rows supply further details for constraining the report.
This method allows you to create generic reports and apply dynamic limitations such as date ranges, department name, and other highly variable parameters.
- Parameters:
rows
- rows- Returns:
- a list of DBReport instances representing the results of the report query. 1 Database exceptions may be thrown
- Throws:
SQLException
- java.sql.SQLExceptionAccidentalBlankQueryException
- thrown if no conditions have been set and blank queries have not been explicitly allowedAccidentalCartesianJoinException
-
getRowsHaving
public List<M> getRowsHaving(DBRow[] rows, BooleanExpression... conditions) throws SQLException, AccidentalCartesianJoinException, AccidentalBlankQueryException
Gets all the report rows of this DBMigration limited by the supplied example rows but reduce the result to only those that match the conditions.All conditions should only reference the fields/column of the DBMigration.
All supplied rows should be using a DBRow subclass that is included in the report.
Built-in report limitation will be used, the example rows supply further details for constraining the report.
This method allows you to create generic reports and apply dynamic limitations such as date ranges, department name, and other highly variable parameters.
- Parameters:
rows
- rows example rows that provide extra criteriaconditions
- the conditions that will be supplied to the WHERE or HAVING clause of the query- Returns:
- a list of DBReport instances representing the results of the report query
- Throws:
SQLException
- Database exceptions may be thrownAccidentalBlankQueryException
- thrown if no conditions have been set and blank queries have not been explicitly allowedAccidentalCartesianJoinException
-
getSQLForQuery
public String getSQLForQuery(DBRow... rows)
Generates and returns the actual SQL to be used by this DBQueryInsert to select the rows to insert.Good for debugging and great for DBAs, this is how you find out what DBvolution is really doing.
Generates the SQL query for retrieving the objects but does not execute the SQL. Use
the getAllRows method
to retrieve the rows.- Parameters:
rows
- additional conditions to apply to the report.- Returns:
- a String of the SQL that will be used by this DBQuery. 1 Database exceptions may be thrown
-
getSQLForInsert
public String getSQLForInsert(DBRow... rows)
Generates and returns the actual SQL to be used by this DBQueryInsert to insert the queried rows.Good for debugging and great for DBAs, this is how you find out what DBvolution is really doing.
Generates the SQL query for retrieving the objects but does not execute the SQL. Use
the getAllRows method
to retrieve the rows.- Parameters:
rows
- additional conditions to apply to the report.- Returns:
- a String of the SQL that will be used by this DBQuery. 1 Database exceptions may be thrown
-
getSQLForCount
public String getSQLForCount(DBRow... rows) throws SQLException
Returns the SQL query that will used to count the rows returned for the supplied DBReportUse this method to check the SQL that will be executed during
the count method
- Parameters:
rows
- additional conditions to be applied.- Returns:
- a String of the SQL query that will be used to count the rows returned by this report 1 Database exceptions may be thrown
- Throws:
SQLException
- java.sql.SQLException
-
count
public Long count(DBRow... rows) throws SQLException, AccidentalCartesianJoinException, AccidentalBlankQueryException
Count the rows on the database without retrieving the rows.Creates a
count query
for the report and conditions and retrieves the number of rows that would have been returned hadgetAllRows method
been called.- Parameters:
rows
- additional conditions for the query.- Returns:
- the number of rows that have or will be retrieved. 1 Database exceptions may be thrown
- Throws:
SQLException
- java.sql.SQLExceptionAccidentalCartesianJoinException
AccidentalBlankQueryException
-
addAsOptionalTables
public void addAsOptionalTables(DBRow... examples)
Add the rows as optional tables in the query.Optional tables are joined to the query using an outer join and results are returned regardless of whether there is a matching row in the optional table.
Optional rows may contain only DBNull values, that is there getValue() method will return NULL and isDBNull() will be true.
- Parameters:
examples
- additional tables to include in the query if possible.
-
getSortColumns
public SortProvider[] getSortColumns()
Returns the list of sort columns- Returns:
- the sortColumns
-
setCartesianJoinAllowed
public DBMigration<M> setCartesianJoinAllowed(Boolean setting)
Suppresses Cartesian join error protection.DBvolution protects you using accidental Cartesian joins but use this function if a Cartesian is required.
Cartesian joins occur when there is no connection between 2 (or more) tables. Normally all tables are connect by a chain of relationships, usually primary key to foreign key.
Sometimes a connection is missed: for instance 2 unrelated tables are being compared by price, but the price relating expression has not been added. In this case DBvolution will throw an
AccidentalCartesianJoinException
and abort the query. This exception avoids creating a probably massive dataset that will reduce database and network performance significantly.However there are valid cases for a Cartesian join: finding all possible combinations of cake and coffee for instance.
If you are sure you need a Cartesian join, use this method to avoid the error-checking and the
AccidentalCartesianJoinException
- Parameters:
setting
- True if you need a Cartesian join in this DBQueryInsert.- Returns:
- this DBQueryInsert object
-
setBlankQueryAllowed
public DBMigration<M> setBlankQueryAllowed(Boolean setting)
Change the Default Setting of Disallowing Blank QueriesA common mistake is creating a query without supplying criteria and accidentally retrieving a huge number of rows.
DBvolution detects this situation and, by default, throws a
AccidentalBlankQueryException
when it happens.To change this behaviour, and allow blank queries, call
setBlankQueriesAllowed(true)
.- Parameters:
setting
- - TRUE to allow blank queries, FALSE to return it to the default setting.- Returns:
- this DBQueryInsert instance
-
createAllRows
public void createAllRows(DBRow... extraExamples) throws SQLException
- Throws:
SQLException
-
validateAllRows
public DBMigrationValidation.Results validateAllRows(DBRow... extraExamples) throws SQLException
Perform a migration validation using DBMigrationValidation.Validation will process all the available rows and try migrate them or to identify why they were not migrated. FAILURE TO MIGRATE MAY BE CORRECT BEHAVIOUR. Carefully check the results to determine if there are issues with the migration or the data.
- Parameters:
extraExamples
- extra examples- Returns:
- result from the migration validation
- Throws:
SQLException
- if the database has an issue with the validation script
-
getCartesianJoinsAllowed
public boolean getCartesianJoinsAllowed()
-
getBlankQueryAllowed
public boolean getBlankQueryAllowed()
-
getMapper
public M getMapper()
-
-