Class SQLSelect<M extends Model>
- Type Parameters:
M
- Model class
- All Implemented Interfaces:
SQLRequest
Provides a convenient way to build select SQL queries, reactive-style, for a provided model class.
SQL Select provides multiple ways to create SQL queries.
Table name is directly inferred the from model class and column names are either gathered as defined by the model inputs.
SQLSelect supports conditioning using where clauses.
Nesting where clauses is directly supported as a byproduct of using Where
under the hood.
For this, several methods are provided.
Where should be started by calling SQLWhereQuery.where(Where)
. While SQLWhereQuery.andWhere(Where)
and SQLWhereQuery.orWhere(Where)
can be used to start, it is strongly discouraged and not supported.
Where clauses can then be chained using either SQLWhereQuery.andWhere(Where)
and SQLWhereQuery.orWhere(Where)
, depending on the desired operator.
Each SQLWhereQuery.where(Where)
, SQLWhereQuery.andWhere(Where)
and SQLWhereQuery.orWhere(Where)
have shortcut methods, respectively SQLWhereQuery.where(String, String, Object)
, SQLWhereQuery.andWhere(String, String, Object)
and SQLWhereQuery.orWhere(String, String, Object)
, to avoid creating new unnecessary Where
clauses.
Similarly, SQLSelect supports order by clauses.
Again, several methods are provided.
Order By should be started by calling orderBy(OrderBy)
. While andOrderBy(OrderBy)
can be used to start, it is strongly discouraged and not supported.
Order By clauses can then be chained using andOrderBy(OrderBy)
.
orderBy(String, Order)
and andOrderBy(OrderBy)
have shortcut methods, respectively orderBy(String, Order)
and andOrderBy(String, Order)
, to avoid creating new unnecessary OrderBy
clauses.
SQLSelect supports Reactive-style programming to create more compact code.
Instead of creating a new object and calling methods on it repeatedly, line by line, calls can be chained directly, eliminating the need to ceate a temprary object and significantly shorting the code.
SQLSelect<Model> s = new SQLSelect<>( Model.class ); s.where( "col1", "=", "val1" ); s.andWhere( "col2", "<", "56" ); s.orderBy( "col3", Order.DESC ); Model m = s.findUnique();
is strictly equivalent to
Model m = ( new SQLSelect<Model>( Model.class ) ).where( "col1", "=", "val1" ).andWhere( "col2", "<", "56" ).orderBy( "col3", Order.DESC ).findUnique();
Let us see some example usage.
For this, let us consider the following models:
public class ExampleModel extends Model { @Id @Column( name = 'id' ) private Long exampleId; public ExampleModel() {} public Long getExampleId() { return exampleId; } public void setExampleId( Long v ) { exampleId = v; } }
public class InheritedExampleModel extends ExampleModel { @Column( name = 'num' ) private Long number; public InheritedExampleModel() {} public Long getNumber() { return number; } public void setNumber( Long v ) { number = v; } }
The simplest query is built by just passing a model class.
SQLSelect<ExampleModel> s = new SQLSelect<>( ExampleModel.class );
will generate : SELECT id as exampleId FROM examplemodel;
Column names are automatically retrieved and mapped accordingly.
Multiple classes can be passed to create a NATURAL JOIN
request between them. The most complete model should be passed as generic type.
SQLSelect<InheritedExampleModel> s = new SQLSelect<>( new Class[]{ InheritedExampleModel.class, ExampleModel.class } );
will generate : SELECT id as exampleId, num as number FROM inheritedexamplemodel NATURAL JOIN examplemodel;
If NATURAL JOIN
is not the desired join clause, custom join clauses and conditions can be passed as parameters.
There should be exactly one less join clauses and conditions than passed model classes.
SQLSelect<InheritedExampleModel> s = new SQLSelect<>( new Class[]{ InheritedExampleModel.class, ExampleModel.class }, new String[]{ 'LEFT OUTER JOIN' }, new String[]{ 'ON inheritedexamplemodel.id = examplemodel.id' } );
will generate : SELECT id as exampleId, num as number FROM inheritedexamplemodel LEFT INNER JOIN examplemodel ON inheritedexamplemodel.id = examplemodel.id;
For each possible combinations, it is possible to pass field names as last parameters or a column name String array to select specific columns.
SQLSelect<ExampleModel> s = new SQLSelect<>( ExampleModel.class, "exampleId" );
will generate : SELECT id as exampleId FROM examplemodel;
SQLSelect<InheritedExampleModel> s = new SQLSelect<>( new Class[]{ InheritedExampleModel.class, ExampleModel.class }, "number" );
will generate : SELECT num as number FROM inheritedexamplemodel NATURAL JOIN examplemodel;
It is also possible to tell the class not to process the passed selected fields and insert them as-is.
- Author:
- Virgile, Nicolas, Louis-Félix
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new helper for a model classCreates a new helper for multiple model classes, assuming they are joined by NATURAL JOIN.SQLSelect
(Class<? extends Model>[] modelClasses, boolean rawSelectedFields, String... selectedFields) Creates a new helper for multiple model classes, assuming they are joined by NATURAL JOIN, retrieving only the provided columns.Creates a new helper for multiple model classes, assuming they are joined by NATURAL JOIN, retrieving only the provided columns.Creates a new helper for multiple model classes, joining them with the provided join clause and using the provided join condition.SQLSelect
(Class<? extends Model>[] modelClasses, String[] joinClause, String[] joinCondition, boolean rawSelectedFields, String... selectedFields) Creates a new helper for multiple model classes, joining them with the provided join clause and using the provided join condition, retrieving only the provided columns.SQLSelect
(Class<? extends Model>[] modelClasses, String[] joinClause, String[] joinCondition, String... selectedFields) Creates a new helper for multiple model classes, joining them with the provided join clause and using the provided join condition, retrieving only the provided columns.Creates a new helper for a model class, retrieving only the provided columns.Creates a new helper for a model class, retrieving only the provided columns. -
Method Summary
Modifier and TypeMethodDescriptionandOrderBy
(OrderBy condition) Chain another Order By condition using the provided values.andOrderBy
(String column, Order order) Chain another Order By condition using the provided values.findList()
Execute the built query and retrieve a list of model instances.findRaw()
Execute the built query and return a raw ResultSetExecute the built query and retrieve a unique, directly usable model instance.boolean
Execute the built query and return if there is at least one result.Init a new Order By clause.Init a new Order By clause using the provided values.setSelectedFields
(String... selectedFields) Sets new selected columns.toString()
Generate the SQL query for this request helper.
-
Constructor Details
-
SQLSelect
Creates a new helper for a model class- Parameters:
modelClass
- Model class
-
SQLSelect
Creates a new helper for a model class, retrieving only the provided columns.- Parameters:
modelClass
- Model classselectedFields
- Fields to retrieve
-
SQLSelect
public SQLSelect(Class<? extends Model> modelClass, boolean rawSelectedFields, String... selectedFields) Creates a new helper for a model class, retrieving only the provided columns.- Parameters:
modelClass
- Model classrawSelectedFields
-true
do not attempt to automatically process selected fieldsselectedFields
- Fields to retrieve
-
SQLSelect
Creates a new helper for multiple model classes, assuming they are joined by NATURAL JOIN.- Parameters:
modelClasses
- Model classes
-
SQLSelect
Creates a new helper for multiple model classes, assuming they are joined by NATURAL JOIN, retrieving only the provided columns.- Parameters:
modelClasses
- Model classesselectedFields
- Fields to retrieve
-
SQLSelect
public SQLSelect(Class<? extends Model>[] modelClasses, boolean rawSelectedFields, String... selectedFields) Creates a new helper for multiple model classes, assuming they are joined by NATURAL JOIN, retrieving only the provided columns.- Parameters:
modelClasses
- Model classesrawSelectedFields
-true
do not attempt to automatically process selected fieldsselectedFields
- Fields to retrieve
-
SQLSelect
public SQLSelect(Class<? extends Model>[] modelClasses, String[] joinClause, String[] joinCondition) Creates a new helper for multiple model classes, joining them with the provided join clause and using the provided join condition.- Parameters:
modelClasses
- Model classesjoinClause
- Join clausejoinCondition
- Join condition
-
SQLSelect
public SQLSelect(Class<? extends Model>[] modelClasses, String[] joinClause, String[] joinCondition, String... selectedFields) throws IllegalArgumentException Creates a new helper for multiple model classes, joining them with the provided join clause and using the provided join condition, retrieving only the provided columns.- Parameters:
modelClasses
- Model classesjoinClause
- Join clausejoinCondition
- Join conditionselectedFields
- Fields to retrieve- Throws:
IllegalArgumentException
- Join clause or condition is malformed
-
SQLSelect
public SQLSelect(Class<? extends Model>[] modelClasses, String[] joinClause, String[] joinCondition, boolean rawSelectedFields, String... selectedFields) throws IllegalArgumentException Creates a new helper for multiple model classes, joining them with the provided join clause and using the provided join condition, retrieving only the provided columns.- Parameters:
modelClasses
- Model classesjoinClause
- Join clausejoinCondition
- Join conditionrawSelectedFields
-true
do not attempt to automatically process selected fieldsselectedFields
- Fields to retrieve- Throws:
IllegalArgumentException
- Join clause or condition is malformed
-
-
Method Details
-
getModelClass
-
setSelectedFields
Sets new selected columns.- Parameters:
selectedFields
- New selected columns.- Returns:
- This SQL select helper
-
orderBy
Init a new Order By clause.- Parameters:
condition
- Order By clause to use- Returns:
- This SQL select helper
-
andOrderBy
Chain another Order By condition using the provided values.- Parameters:
condition
- Order By clause to chain- Returns:
- This SQL select helper
-
orderBy
Init a new Order By clause using the provided values.- Parameters:
column
- Column to targetorder
- Order to target- Returns:
- This SQL select helper
-
andOrderBy
Chain another Order By condition using the provided values.- Parameters:
column
- Column to targetorder
- Order to target- Returns:
- This SQL select helper
-
findUnique
Execute the built query and retrieve a unique, directly usable model instance.- Returns:
- Model instance
- Throws:
DatabaseException
- Database error
-
findList
Execute the built query and retrieve a list of model instances.- Returns:
- Model instances
- Throws:
DatabaseException
- Database error
-
hasAtLeastOne
Execute the built query and return if there is at least one result.- Returns:
true
if has at least one result- Throws:
DatabaseException
- Database error
-
findRaw
Execute the built query and return a raw ResultSet- Returns:
- ResultSet
- Throws:
DatabaseException
- Database error
-
toString
Description copied from interface:SQLRequest
Generate the SQL query for this request helper.- Specified by:
toString
in interfaceSQLRequest
- Overrides:
toString
in classObject
- Returns:
- Generated SQL query
-