Class QueryBuilder

java.lang.Object
org.dalesbred.query.QueryBuilder

public final class QueryBuilder extends Object
A 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 Details

  • Constructor Details

    • QueryBuilder

      public QueryBuilder()
      Constructs a new empty QueryBuilder.
    • QueryBuilder

      public QueryBuilder(@NotNull @NotNull String sql)
      Constructs a QueryBuilder with given initial SQL-fragment.
    • QueryBuilder

      public QueryBuilder(@NotNull @NotNull String sql, Object... arguments)
      Constructs a QueryBuilder with given initial SQL-fragment and arguments.
  • Method Details

    • append

      @NotNull public @NotNull QueryBuilder append(@NotNull @NotNull String sql)
      Appends given fragment to this query.
    • append

      @NotNull public @NotNull QueryBuilder append(@NotNull @NotNull String sql, Object... args)
      Appends given fragment and arguments to this query.
    • append

      @NotNull public @NotNull QueryBuilder append(@NotNull @NotNull String sql, @NotNull @NotNull 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 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 Object argument)
      Adds an argument to this query.
    • addArguments

      @NotNull public @NotNull QueryBuilder addArguments(Object... args)
      Adds given arguments to this query.
    • addArguments

      @NotNull public @NotNull QueryBuilder addArguments(@NotNull @NotNull 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:
      IllegalStateException - if the builder is empty