Class SelectAdapter

  • Direct Known Subclasses:
    FromSubQueryAdapter

    public class SelectAdapter
    extends java.lang.Object
    An abstract representation of a select statement which can be translated into an executable select statement. Keeps track of bind variables (parameter markers) and hopefully is a bit easier to use (and is more reliable) than constructing SQL with StringBuilder. The goal is to support two main use-cases (which drive the API design):
    • 1. Simplify construction of hand-written SQL statements
    • 2. Support code-generated SQL where a statement is constructed from another model (e.g. FHIR search queries).

    The goal isn't to enforce building a syntactically perfect SQL statement - checking that is the role of the RDBMS SQL parser. But hopefully this makes things a bit easier, less error-prone and therefore quicker. It also helps to standardize the SQL statement building process across the project.

    • Constructor Summary

      Constructors 
      Constructor Description
      SelectAdapter​(boolean distinct, java.lang.String... columns)  
      SelectAdapter​(java.lang.String... columns)
      Public constructor taking a collection of string column names
      SelectAdapter​(Select select)
      Adapter this select statement
    • Constructor Detail

      • SelectAdapter

        public SelectAdapter​(Select select)
        Adapter this select statement
        Parameters:
        select -
      • SelectAdapter

        public SelectAdapter​(java.lang.String... columns)
        Public constructor taking a collection of string column names
        Parameters:
        columns -
      • SelectAdapter

        public SelectAdapter​(boolean distinct,
                             java.lang.String... columns)
    • Method Detail

      • column

        public SelectAdapter column​(java.lang.String source,
                                    java.lang.String name)
      • addColumn

        public SelectAdapter addColumn​(java.lang.String source,
                                       java.lang.String name,
                                       Alias alias)
      • addColumn

        public SelectAdapter addColumn​(java.lang.String columnValue,
                                       Alias alias)
        Add a column value with a given alias. Can be used to add literals in the select list
        Parameters:
        source -
        name -
        alias -
        Returns:
      • with

        public SelectAdapter with​(Select selectClause,
                                  Alias alias)
        Add a WITH x AS (SELECT ...) statement to the beginning of the select
        Parameters:
        selectClause -
        alias -
        Returns:
      • from

        public FromAdapter from​(java.lang.String tableName,
                                Alias alias)
        Create a from clause for this select statement
        Returns:
      • from

        public FromAdapter from​(Select sub,
                                Alias alias)
        Add the sub-query select to the FROM clause
        Parameters:
        sub - the sub-query select statement
        alias -
        Returns:
      • from

        public FromAdapter from​(java.lang.String table)
        Create a from clause for this select statement
        Returns:
      • from

        public FromAdapter from()
        Create a FromAdapter associated with the current select statement. No row sources are added to the query. The FROM will only be added to the statement if rows sources are subsequently added to the returned FromAdapter.
        Returns:
      • getSelect

        public Select getSelect()
        Getter for the select statement we are managing
        Returns:
      • pagination

        public void pagination​(int offset,
                               int rowsPerPage)
        Add pagination offset/limit to the query
        Parameters:
        translator -
        offset -
        rowsPerPage -
      • union

        public void union​(Select unionSelect)
        Add a select via UNION
        Parameters:
        unionSelect - the select to be UNION'd to this select statement
      • unionAll

        public void unionAll​(Select unionAllSelect)
        Add a select via UNION ALL
        Parameters:
        unionAllSelect - the select to be UNION ALL'd to this select statement
      • build

        public Select build()
        Get the statement we've been constructing
        Returns: