Class QueryBuilder


  • public final class QueryBuilder
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String PLACEHOLDER
      Placeholder 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 QueryBuilder addArgument​(@Nullable java.lang.Object argument)
      Adds an argument to this query.
      @NotNull QueryBuilder addArguments​(@NotNull java.util.Collection<?> args)
      Adds given arguments to this query.
      @NotNull QueryBuilder addArguments​(java.lang.Object... args)
      Adds given arguments to this query.
      @NotNull QueryBuilder append​(@NotNull java.lang.String sql)
      Appends given fragment to this query.
      @NotNull QueryBuilder append​(@NotNull java.lang.String sql, @NotNull java.util.Collection<?> args)
      Appends given fragment and arguments to this query.
      @NotNull QueryBuilder append​(@NotNull java.lang.String sql, java.lang.Object... args)
      Appends given fragment and arguments to this query.
      @NotNull QueryBuilder append​(@NotNull SqlQuery query)
      Appends given query and its arguments to this query.
      @NotNull QueryBuilder appendPlaceholders​(int count)
      Adds a given amount of comma-separated place-holders.
      @NotNull QueryBuilder appendPlaceholders​(@NotNull java.util.Collection<?> args)
      Adds placeholders for all elements of collection and then adds then values of collection itself.
      @NotNull SqlQuery build()
      Builds an SQL query from the current state of this builder.
      boolean hasArguments()
      Does this query have any arguments?
      boolean isEmpty()
      Is the query string empty?
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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