Class Sql
- java.lang.Object
-
- de.ufinke.cubaja.sql.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 Summary
Constructors Constructor Description Sql()Default constructor.Sql(Reader reader)Constructor which reads initial SQL text from a reader.Sql(Class<?> packageClass, String sqlResource)Constructor which reads initial SQL text from a resource.Sql(String sql)Constructor with initial SQL text.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Sqlappend(Reader reader)Appends lines from a reader.Sqlappend(Class<?> packageClass, String sqlResource)Appends lines from a resource.Sqlappend(String line)Appends a line.SqlappendInsert(String... variables)Generates SQL code forinsertstatements.SqlappendList(int[] value)Appends a list of integer values separated by comma.SqlappendList(Object[] value)Appends a list of values separated by comma.SqlappendList(Collection<Object> value)Appends a list of values separated by comma.SqlappendUpdate(String... variables)Generates SQL code forupdatestatements.protected StringgetSingleStatement()protected List<String>getStatements()Sqlresolve(String name, int[] value)Sets a property to a generated collection value.Sqlresolve(String name, Object[] value)Sets a property to a generated collection value.Sqlresolve(String name, String value)Sets a property to a value.Sqlresolve(String name, Collection<Object> value)Sets a property to a generated collection value.StringtoString()Returns the formatted SQL.
-
-
-
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). ThesqlResourceparameter contains only the plain file name without extension and without path.- Parameters:
packageClass- class in the same package as the resource filesqlResource- name of resource file within the package- Throws:
IOException- I/O problem
-
-
Method Detail
-
getSingleStatement
protected String getSingleStatement() throws SQLException
- Throws:
SQLException
-
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
packageClassparameter 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). ThesqlResourceparameter 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 thepackageClass. The resource is loaded following the rules defined bygetResourceAsStream.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 filesqlResource- 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 ofNumber, the value will be enclosed by apostrophs.Useful for
INpredicates. 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 ofNumber, the value will be enclosed by apostrophs.Useful for
INpredicates. 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
INpredicates. The parantheses are not generated automatically.- Parameters:
value- array with values- Returns:
- this
-
appendUpdate
public Sql appendUpdate(String... variables)
Generates SQL code forupdatestatements. The variables in the list are expanded toset var1 = :var1, var2 = :var2 ....- Parameters:
variables- field names- Returns:
- this
-
appendInsert
public Sql appendInsert(String... variables)
Generates SQL code forinsertstatements. 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 propertyvalue- 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 inappendList, thenresolveis called.- Parameters:
name- name of propertyvalue- 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 inappendList, thenresolveis called.- Parameters:
name- name of propertyvalue- 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 inappendList, thenresolveis called.- Parameters:
name- name of propertyvalue- array of values which replace the properties as a comma separated list- Returns:
- this
-
-