Package org.dalesbred.query
Class QueryBuilder
- java.lang.Object
-
- org.dalesbred.query.QueryBuilder
-
public final class QueryBuilder extends java.lang.ObjectA very simple builder that can be used to build queries dynamically. Operates on a slightly higher level than StringBuilder in that it tracks the query-variables and can be used as a building block for higher level abstractions.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringPLACEHOLDERPlaceholder to be used in queries for values
-
Constructor Summary
Constructors Constructor Description QueryBuilder()Constructs a new empty QueryBuilder.QueryBuilder(@NotNull java.lang.String sql)Constructs a QueryBuilder with given initial SQL-fragment.QueryBuilder(@NotNull java.lang.String sql, java.lang.Object... arguments)Constructs a QueryBuilder with given initial SQL-fragment and arguments.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull QueryBuilderaddArgument(@Nullable java.lang.Object argument)Adds an argument to this query.@NotNull QueryBuilderaddArguments(@NotNull java.util.Collection<?> args)Adds given arguments to this query.@NotNull QueryBuilderaddArguments(java.lang.Object... args)Adds given arguments to this query.@NotNull QueryBuilderappend(@NotNull java.lang.String sql)Appends given fragment to this query.@NotNull QueryBuilderappend(@NotNull java.lang.String sql, @NotNull java.util.Collection<?> args)Appends given fragment and arguments to this query.@NotNull QueryBuilderappend(@NotNull java.lang.String sql, java.lang.Object... args)Appends given fragment and arguments to this query.@NotNull QueryBuilderappend(@NotNull SqlQuery query)Appends given query and its arguments to this query.@NotNull QueryBuilderappendPlaceholders(int count)Adds a given amount of comma-separated place-holders.@NotNull QueryBuilderappendPlaceholders(@NotNull java.util.Collection<?> args)Adds placeholders for all elements of collection and then adds then values of collection itself.@NotNull SqlQuerybuild()Builds an SQL query from the current state of this builder.booleanhasArguments()Does this query have any arguments?booleanisEmpty()Is the query string empty?
-
-
-
Field Detail
-
PLACEHOLDER
public static final java.lang.String PLACEHOLDER
Placeholder to be used in queries for values- See Also:
- Constant Field Values
-
-
Constructor Detail
-
QueryBuilder
public QueryBuilder()
Constructs a new empty QueryBuilder.
-
QueryBuilder
public QueryBuilder(@NotNull @NotNull java.lang.String sql)Constructs a QueryBuilder with given initial SQL-fragment.
-
QueryBuilder
public QueryBuilder(@NotNull @NotNull java.lang.String sql, java.lang.Object... arguments)Constructs a QueryBuilder with given initial SQL-fragment and arguments.
-
-
Method Detail
-
append
@NotNull public @NotNull QueryBuilder append(@NotNull @NotNull java.lang.String sql)
Appends given fragment to this query.
-
append
@NotNull public @NotNull QueryBuilder append(@NotNull @NotNull java.lang.String sql, java.lang.Object... args)
Appends given fragment and arguments to this query.
-
append
@NotNull public @NotNull QueryBuilder append(@NotNull @NotNull java.lang.String sql, @NotNull @NotNull java.util.Collection<?> args)
Appends given fragment and arguments to this query.
-
append
@NotNull public @NotNull QueryBuilder append(@NotNull @NotNull SqlQuery query)
Appends given query and its arguments to this query.
-
appendPlaceholders
@NotNull public @NotNull QueryBuilder appendPlaceholders(int count)
Adds a given amount of comma-separated place-holders. The amount must be at last 1.
-
appendPlaceholders
@NotNull public @NotNull QueryBuilder appendPlaceholders(@NotNull @NotNull java.util.Collection<?> args)
Adds placeholders for all elements of collection and then adds then values of collection itself.
-
isEmpty
public boolean isEmpty()
Is the query string empty?
-
hasArguments
public boolean hasArguments()
Does this query have any arguments?
-
addArgument
@NotNull public @NotNull QueryBuilder addArgument(@Nullable @Nullable java.lang.Object argument)
Adds an argument to this query.
-
addArguments
@NotNull public @NotNull QueryBuilder addArguments(java.lang.Object... args)
Adds given arguments to this query.
-
addArguments
@NotNull public @NotNull QueryBuilder addArguments(@NotNull @NotNull java.util.Collection<?> args)
Adds given arguments to this query.
-
build
@NotNull public @NotNull SqlQuery build()
Builds an SQL query from the current state of this builder.- Throws:
java.lang.IllegalStateException- if the builder is empty
-
-