Class Sql


  • public class Sql
    extends Object
    Collector and interpreter of SQL text. The various methods keep track of line breaks, separators, comments and variables. The result is formatted executable SQL.

    By default, this class splits consecutive statements (separated by semicolon) into a list of statements. Text between line comments --{beginStatement} and --{endStatement} are treated as a single SQL statement. These comments are necessary to process stored procedure definitions or proprietary SQL scripts.

    Author:
    Uwe Finke
    • Constructor Detail

      • Sql

        public Sql()
        Default constructor.
      • Sql

        public Sql​(String sql)
        Constructor with initial SQL text.
        Parameters:
        sql - initial SQL text
      • Sql

        public Sql​(Reader reader)
            throws IOException
        Constructor which reads initial SQL text from a reader.
        Parameters:
        reader - reader
        Throws:
        IOException - I/O problem
      • Sql

        public Sql​(Class<?> packageClass,
                   String sqlResource)
            throws IOException
        Constructor which reads initial SQL text from a resource.

        The SQL must be written in a separate file within a java source package (usually the package where the class which uses the SQL belongs to). We have to specify a class within that package as parameter. This may be any class, but usually it will be the class which uses the SQL. The file names extension must be sql (lower case). The sqlResource parameter contains only the plain file name without extension and without path.

        Parameters:
        packageClass - class in the same package as the resource file
        sqlResource - name of resource file within the package
        Throws:
        IOException - I/O problem
    • Method Detail

      • toString

        public String toString()
        Returns the formatted SQL.
        Overrides:
        toString in class Object
      • getStatements

        protected List<String> getStatements()
      • append

        public Sql append​(String line)
        Appends a line.
        Parameters:
        line - another line of the SQL statement
        Returns:
        this
      • append

        public Sql append​(Reader reader)
                   throws IOException
        Appends lines from a reader.
        Parameters:
        reader - reader
        Returns:
        this
        Throws:
        IOException - I/O problem
      • append

        public Sql append​(Class<?> packageClass,
                          String sqlResource)
                   throws IOException
        Appends lines from a resource.

        The SQL must be written in a separate file within a java source package. The packageClass parameter specifies a class which is located in the same package as the SQL resource. This may be any class, but usually it will be the class which uses the SQL (then the parameter simply is 'getClass()').

        The file names extension must be sql (lower case). The sqlResource parameter contains the file name without extension (the extension is appended by this method automatically). There is no prefix or path required when the resource resides in the same package as the packageClass. The resource is loaded following the rules defined by getResourceAsStream.

        To be platform independent on runtime, the resource must be coded in UTF-8. Don't forget to customize your development editor.

        Parameters:
        packageClass - class in the same package as the resource file
        sqlResource - name of resource file within the package
        Returns:
        this
        Throws:
        IOException - I/O problem
      • appendList

        public Sql appendList​(Object[] value)
        Appends a list of values separated by comma. If an object type is not a subtype of Number, the value will be enclosed by apostrophs.

        Useful for IN predicates. The parantheses are not generated automatically.

        Parameters:
        value - array with values
        Returns:
        this
      • appendList

        public Sql appendList​(Collection<Object> value)
        Appends a list of values separated by comma. If an object type is not a subtype of Number, the value will be enclosed by apostrophs.

        Useful for IN predicates. The parantheses are not generated automatically.

        Parameters:
        value - collection with values
        Returns:
        this
      • appendList

        public Sql appendList​(int[] value)
        Appends a list of integer values separated by comma.

        Useful for IN predicates. The parantheses are not generated automatically.

        Parameters:
        value - array with values
        Returns:
        this
      • appendUpdate

        public Sql appendUpdate​(String... variables)
        Generates SQL code for update statements. The variables in the list are expanded to set var1 = :var1, var2 = :var2 ....
        Parameters:
        variables - field names
        Returns:
        this
      • appendInsert

        public Sql appendInsert​(String... variables)
        Generates SQL code for insert statements. The variables in the list are expanded to (var1, var2, ...) values (:var1, :var2 ...).
        Parameters:
        variables - field names
        Returns:
        this
      • resolve

        public Sql resolve​(String name,
                           String value)
        Sets a property to a value. Properties like '${name} are replaced by the value before the formatted statement is retrieved.
        Parameters:
        name - name of property
        value - value which replaces the property
        Returns:
        this
      • resolve

        public Sql resolve​(String name,
                           Object[] value)
        Sets a property to a generated collection value. The array elements are formatted as in appendList, then resolve is called.
        Parameters:
        name - name of property
        value - array with values which replace the properties as a comma separated list
        Returns:
        this
      • resolve

        public Sql resolve​(String name,
                           Collection<Object> value)
        Sets a property to a generated collection value. The collection elements are formatted as in appendList, then resolve is called.
        Parameters:
        name - name of property
        value - collection of values which replace the properties as a comma separated list
        Returns:
        this
      • resolve

        public Sql resolve​(String name,
                           int[] value)
        Sets a property to a generated collection value. The collection elements are formatted as in appendList, then resolve is called.
        Parameters:
        name - name of property
        value - array of values which replace the properties as a comma separated list
        Returns:
        this