Package org.apache.openjpa.jdbc.sql
Class SQLBuffer
- java.lang.Object
-
- org.apache.openjpa.jdbc.sql.SQLBuffer
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public final class SQLBuffer extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable
Buffer for SQL statements that can be used to create java.sql.PreparedStatements. This buffer holds the SQL statement parameters and their corresponding columns. The parameters introduced by the runtime system are distinguished from the parameters set by the user.- Since:
- 0.2.4
- Author:
- Marc Prud'hommeaux, Abe White, Pinaki Poddar
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SQLBuffer(DBDictionary dict)
Default constructor.SQLBuffer(SQLBuffer buf)
Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCastForParam(java.lang.String oper, Val val)
Replace SQL '?'SQLBuffer
append(java.lang.String s)
SQLBuffer
append(DBIdentifier name)
SQLBuffer
append(Column col)
SQLBuffer
append(Sequence seq)
SQLBuffer
append(Table table)
SQLBuffer
append(Select sel, JDBCFetchConfiguration fetch)
Append a subselect.SQLBuffer
append(SQLBuffer buf)
Append all SQL and parameters of the given buffer.SQLBuffer
appendCount(Select sel, JDBCFetchConfiguration fetch)
Append a subselect count.SQLBuffer
appendParamOnly(SQLBuffer buf)
Append parameters only if the given buffer at the given positions.SQLBuffer
appendValue(java.lang.Object o)
Append a parameter value.SQLBuffer
appendValue(java.lang.Object o, Column col)
Append a system inserted parameter value for a specific column.SQLBuffer
appendValue(java.lang.Object o, Column col, Parameter userParam)
Append a user parameter value for a specific column.SQLBuffer
appendValue(java.lang.Object o, Column col, Parameter userParam, boolean useParamToken)
java.lang.Object
clone()
Perform a shallow clone of this SQL Buffer.boolean
equals(java.lang.Object other)
java.util.List
getColumns()
java.util.List
getParameters()
Return the list of parameter values.java.lang.String
getSQL()
Return the SQL for this buffer.java.lang.String
getSQL(boolean replaceParams)
Returns the SQL for this buffer.java.util.List
getUserParameters()
Get the user parameter positions in the list of parameters.int
hashCode()
boolean
isEmpty()
Return true if the buffer is emtpy.java.sql.CallableStatement
prepareCall(java.sql.Connection conn)
Create and populate the parameters of a prepared statement using the SQL in this buffer.java.sql.CallableStatement
prepareCall(java.sql.Connection conn, int rsType, int rsConcur)
Create and populate the parameters of a prepared statement using the SQL in this buffer.java.sql.CallableStatement
prepareCall(java.sql.Connection conn, JDBCFetchConfiguration fetch, int rsType, int rsConcur)
Create and populate the parameters of a prepred statement using the SQL in this buffer and the given fetch configuration.java.sql.PreparedStatement
prepareStatement(java.sql.Connection conn)
Create and populate the parameters of a prepared statement using the SQL in this buffer.java.sql.PreparedStatement
prepareStatement(java.sql.Connection conn, int rsType, int rsConcur)
Create and populate the parameters of a prepared statement using the SQL in this buffer.java.sql.PreparedStatement
prepareStatement(java.sql.Connection conn, JDBCFetchConfiguration fetch, int rsType, int rsConcur)
Create and populate the parameters of a prepred statement using the SQL in this buffer and the given fetch configuration.boolean
replace(Select old, Select sel)
Replace a subselect.void
replaceSqlString(int start, int end, java.lang.String newString)
Replace current buffer string with the new stringvoid
setParameters(java.sql.PreparedStatement ps)
Populate the parameters of an existing PreparedStatement with values from this buffer.void
setParameters(java.util.List params)
boolean
sqlEquals(java.lang.String sql)
Compare internal SQL without resolving subselects or stringifying parameters.
-
-
-
Constructor Detail
-
SQLBuffer
public SQLBuffer(DBDictionary dict)
Default constructor.
-
SQLBuffer
public SQLBuffer(SQLBuffer buf)
Copy constructor.
-
-
Method Detail
-
clone
public java.lang.Object clone()
Perform a shallow clone of this SQL Buffer.- Overrides:
clone
in classjava.lang.Object
-
isEmpty
public boolean isEmpty()
Return true if the buffer is emtpy.
-
appendParamOnly
public SQLBuffer appendParamOnly(SQLBuffer buf)
Append parameters only if the given buffer at the given positions.
-
append
public SQLBuffer append(DBIdentifier name)
-
append
public SQLBuffer append(java.lang.String s)
-
append
public SQLBuffer append(Select sel, JDBCFetchConfiguration fetch)
Append a subselect. This delays resolution of the select SQL.
-
appendCount
public SQLBuffer appendCount(Select sel, JDBCFetchConfiguration fetch)
Append a subselect count. This delays resolution of the select SQL.
-
appendValue
public SQLBuffer appendValue(java.lang.Object o)
Append a parameter value.
-
appendValue
public SQLBuffer appendValue(java.lang.Object o, Column col)
Append a system inserted parameter value for a specific column.
-
appendValue
public SQLBuffer appendValue(java.lang.Object o, Column col, Parameter userParam)
Append a user parameter value for a specific column. User parameters are marked as opposed to the parameters inserted by the internal runtime system. This helps to reuse the buffer by reparmeterizing it with new set of user parameters while the 'internal' parameters remain unchanged.- Parameters:
userParam
- if non-null, designates a 'user' parameter.
-
appendValue
public SQLBuffer appendValue(java.lang.Object o, Column col, Parameter userParam, boolean useParamToken)
-
getParameters
public java.util.List getParameters()
Return the list of parameter values.
-
getUserParameters
public java.util.List getUserParameters()
Get the user parameter positions in the list of parameters. The odd element of the returned list contains an integer index that refers to the position in thegetParameters()
list. The even element of the returned list refers to the user parameter key. This structure is preferred over a normal map because a user parameter may occur more than one in the parameters.
-
getSQL
public java.lang.String getSQL()
Return the SQL for this buffer.
-
getSQL
public java.lang.String getSQL(boolean replaceParams)
Returns the SQL for this buffer.- Parameters:
replaceParams
- if true, then replace parameters with the actual parameter values
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.sql.Connection conn) throws java.sql.SQLException
Create and populate the parameters of a prepared statement using the SQL in this buffer.- Throws:
java.sql.SQLException
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.sql.Connection conn, int rsType, int rsConcur) throws java.sql.SQLException
Create and populate the parameters of a prepared statement using the SQL in this buffer.- Throws:
java.sql.SQLException
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.sql.Connection conn, JDBCFetchConfiguration fetch, int rsType, int rsConcur) throws java.sql.SQLException
Create and populate the parameters of a prepred statement using the SQL in this buffer and the given fetch configuration.- Throws:
java.sql.SQLException
-
prepareCall
public java.sql.CallableStatement prepareCall(java.sql.Connection conn) throws java.sql.SQLException
Create and populate the parameters of a prepared statement using the SQL in this buffer.- Throws:
java.sql.SQLException
-
prepareCall
public java.sql.CallableStatement prepareCall(java.sql.Connection conn, int rsType, int rsConcur) throws java.sql.SQLException
Create and populate the parameters of a prepared statement using the SQL in this buffer.- Throws:
java.sql.SQLException
-
prepareCall
public java.sql.CallableStatement prepareCall(java.sql.Connection conn, JDBCFetchConfiguration fetch, int rsType, int rsConcur) throws java.sql.SQLException
Create and populate the parameters of a prepred statement using the SQL in this buffer and the given fetch configuration.- Throws:
java.sql.SQLException
-
setParameters
public void setParameters(java.sql.PreparedStatement ps) throws java.sql.SQLException
Populate the parameters of an existing PreparedStatement with values from this buffer.- Throws:
java.sql.SQLException
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
sqlEquals
public boolean sqlEquals(java.lang.String sql)
Compare internal SQL without resolving subselects or stringifying parameters.
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
addCastForParam
public void addCastForParam(java.lang.String oper, Val val)
Replace SQL '?' with CAST string if required by DB platform- Parameters:
oper
-val
-
-
replaceSqlString
public void replaceSqlString(int start, int end, java.lang.String newString)
Replace current buffer string with the new string- Parameters:
start
- replace start positionend
- replace end positionnewString
-
-
setParameters
public void setParameters(java.util.List params)
-
getColumns
public java.util.List getColumns()
-
-