Buffer for SQL statements that can be used to create
java.sql.PreparedStatements.
| Method from org.apache.openjpa.jdbc.sql.SQLBuffer Detail: |
public void addCastForParam(String oper,
Val val) {
if (_sql.charAt(_sql.length() - 1) == '?") {
String castString = _dict.addCastAsType(oper, val);
if (castString != null)
_sql.replace(_sql.length() - 1, _sql.length(), castString);
}
}
Replace SQL '?' with CAST string if required by DB platform |
public SQLBuffer append(SQLBuffer buf) {
append(buf, _sql.length(), (_params == null) ? 0 : _params.size(),
true);
return this;
}
Append all SQL and parameters of the given buffer. |
public SQLBuffer append(Table table) {
_sql.append(_dict.getFullName(table, false));
return this;
}
|
public SQLBuffer append(Sequence seq) {
_sql.append(_dict.getFullName(seq));
return this;
}
|
public SQLBuffer append(Column col) {
_sql.append(col.getName());
return this;
}
|
public SQLBuffer append(String s) {
_sql.append(s);
return this;
}
|
public SQLBuffer append(Select sel,
JDBCFetchConfiguration fetch) {
return append(sel, fetch, false);
}
Append a subselect. This delays resolution of the select SQL. |
public SQLBuffer appendCount(Select sel,
JDBCFetchConfiguration fetch) {
return append(sel, fetch, true);
}
Append a subselect count. This delays resolution of the select SQL. |
public SQLBuffer appendValue(Object o) {
return appendValue(o, null);
}
Append a parameter value. |
public SQLBuffer appendValue(boolean b) {
return appendValue(b, null);
}
Append a parameter value. |
public SQLBuffer appendValue(byte b) {
return appendValue(b, null);
}
Append a parameter value. |
public SQLBuffer appendValue(char c) {
return appendValue(c, null);
}
Append a parameter value. |
public SQLBuffer appendValue(double d) {
return appendValue(d, null);
}
Append a parameter value. |
public SQLBuffer appendValue(float f) {
return appendValue(f, null);
}
Append a parameter value. |
public SQLBuffer appendValue(int i) {
return appendValue(i, null);
}
Append a parameter value. |
public SQLBuffer appendValue(long l) {
return appendValue(l, null);
}
Append a parameter value. |
public SQLBuffer appendValue(short s) {
return appendValue(s, null);
}
Append a parameter value. |
public SQLBuffer appendValue(Object o,
Column col) {
if (o == null)
_sql.append("NULL");
else if (o instanceof Raw)
_sql.append(o.toString());
else {
_sql.append(PARAMETER_TOKEN);
// initialize param and col lists; we hold off on col list until
// we get the first non-null col
if (_params == null)
_params = new ArrayList();
if (col != null && _cols == null) {
_cols = new ArrayList();
while (_cols.size() < _params.size())
_cols.add(null);
}
_params.add(o);
if (_cols != null)
_cols.add(col);
}
return this;
}
Append a parameter value for a specific column. |
public SQLBuffer appendValue(boolean b,
Column col) {
return appendValue((b) ? Boolean.TRUE : Boolean.FALSE, col);
}
Append a parameter value. |
public SQLBuffer appendValue(byte b,
Column col) {
return appendValue(new Byte(b), col);
}
Append a parameter value. |
public SQLBuffer appendValue(char c,
Column col) {
return appendValue(new Character(c), col);
}
Append a parameter value. |
public SQLBuffer appendValue(double d,
Column col) {
return appendValue(new Double(d), col);
}
Append a parameter value. |
public SQLBuffer appendValue(float f,
Column col) {
return appendValue(new Float(f), col);
}
Append a parameter value. |
public SQLBuffer appendValue(int i,
Column col) {
return appendValue(Numbers.valueOf(i), col);
}
Append a parameter value. |
public SQLBuffer appendValue(long l,
Column col) {
return appendValue(Numbers.valueOf(l), col);
}
Append a parameter value. |
public SQLBuffer appendValue(short s,
Column col) {
return appendValue(new Short(s), col);
}
Append a parameter value. |
public Object clone() {
return new SQLBuffer(this);
}
Perform a shallow clone of this SQL Buffer. |
public boolean equals(Object other) {
if (other == this)
return true;
if (!(other instanceof SQLBuffer))
return false;
SQLBuffer buf = (SQLBuffer) other;
return _sql.equals(buf._sql)
&& ObjectUtils.equals(_params, buf._params);
}
|
public List getColumns() {
return _cols;
}
|
public List getParameters() {
return (_params == null) ? Collections.EMPTY_LIST : _params;
}
Return the list of parameter values. |
public String getSQL() {
return getSQL(false);
}
Return the SQL for this buffer. |
public String getSQL(boolean replaceParams) {
resolveSubselects();
String sql = _sql.toString();
if (!replaceParams || _params == null || _params.isEmpty())
return sql;
StringBuffer buf = new StringBuffer();
Iterator pi = _params.iterator();
for (int i = 0; i < sql.length(); i++) {
if (sql.charAt(i) != '?") {
buf.append(sql.charAt(i));
continue;
}
Object param = pi.hasNext() ? pi.next() : null;
if (param == null)
buf.append("NULL");
else if (param instanceof Number || param instanceof Boolean)
buf.append(param);
else if (param instanceof String || param instanceof Character)
buf.append("'").append(param).append("'");
else
buf.append("?");
}
return buf.toString();
}
Returns the SQL for this buffer. |
public int hashCode() {
int hash = _sql.hashCode();
return (_params == null) ? hash : hash ^ _params.hashCode();
}
|
public boolean isEmpty() {
return _sql.length() == 0;
}
Return true if the buffer is emtpy. |
public CallableStatement prepareCall(Connection conn) throws SQLException {
return prepareCall(conn, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}
Create and populate the parameters of a prepared statement using
the SQL in this buffer. |
public CallableStatement prepareCall(Connection conn,
int rsType,
int rsConcur) throws SQLException {
return prepareCall(conn, null, rsType, rsConcur);
}
Create and populate the parameters of a prepared statement using
the SQL in this buffer. |
public CallableStatement prepareCall(Connection conn,
JDBCFetchConfiguration fetch,
int rsType,
int rsConcur) throws SQLException {
if (rsType == -1 && fetch == null)
rsType = ResultSet.TYPE_FORWARD_ONLY;
else if (rsType == -1)
rsType = fetch.getResultSetType();
if (rsConcur == -1)
rsConcur = ResultSet.CONCUR_READ_ONLY;
CallableStatement stmnt;
if (rsType == ResultSet.TYPE_FORWARD_ONLY
&& rsConcur == ResultSet.CONCUR_READ_ONLY)
stmnt = conn.prepareCall(getSQL());
else
stmnt = conn.prepareCall(getSQL(), rsType, rsConcur);
try {
setParameters(stmnt);
if (fetch != null) {
if (fetch.getFetchBatchSize() > 0)
stmnt.setFetchSize(fetch.getFetchBatchSize());
if (rsType != ResultSet.TYPE_FORWARD_ONLY
&& fetch.getFetchDirection() != ResultSet.FETCH_FORWARD)
stmnt.setFetchDirection(fetch.getFetchDirection());
}
return stmnt;
} catch (SQLException se) {
try {
stmnt.close();
} catch (SQLException se2) {
}
throw se;
}
}
Create and populate the parameters of a prepred statement using the
SQL in this buffer and the given fetch configuration. |
public PreparedStatement prepareStatement(Connection conn) throws SQLException {
return prepareStatement(conn, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}
Create and populate the parameters of a prepared statement using
the SQL in this buffer. |
public PreparedStatement prepareStatement(Connection conn,
int rsType,
int rsConcur) throws SQLException {
return prepareStatement(conn, null, rsType, rsConcur);
}
Create and populate the parameters of a prepared statement using
the SQL in this buffer. |
public PreparedStatement prepareStatement(Connection conn,
JDBCFetchConfiguration fetch,
int rsType,
int rsConcur) throws SQLException {
if (rsType == -1 && fetch == null)
rsType = ResultSet.TYPE_FORWARD_ONLY;
else if (rsType == -1)
rsType = fetch.getResultSetType();
if (rsConcur == -1)
rsConcur = ResultSet.CONCUR_READ_ONLY;
PreparedStatement stmnt;
if (rsType == ResultSet.TYPE_FORWARD_ONLY
&& rsConcur == ResultSet.CONCUR_READ_ONLY)
stmnt = conn.prepareStatement(getSQL());
else
stmnt = conn.prepareStatement(getSQL(), rsType, rsConcur);
try {
setParameters(stmnt);
if (fetch != null) {
if (fetch.getFetchBatchSize() > 0)
stmnt.setFetchSize(fetch.getFetchBatchSize());
if (rsType != ResultSet.TYPE_FORWARD_ONLY
&& fetch.getFetchDirection() != ResultSet.FETCH_FORWARD)
stmnt.setFetchDirection(fetch.getFetchDirection());
}
return stmnt;
} catch (SQLException se) {
try {
stmnt.close();
} catch (SQLException se2) {
}
throw se;
}
}
Create and populate the parameters of a prepred statement using the
SQL in this buffer and the given fetch configuration. |
public boolean replace(Select old,
Select sel) {
if (_subsels == null)
return false;
Subselect sub;
for (int i = 0; i < _subsels.size(); i++) {
sub = (Subselect) _subsels.get(i);
if (sub.select == old) {
sub.select = sel;
return true;
}
}
return false;
}
|
public void replaceSqlString(int start,
int end,
String newString) {
_sql.replace(start, end, newString);
}
Replace current buffer string with the new string |
public void setParameters(PreparedStatement ps) throws SQLException {
if (_params == null)
return;
Column col;
for (int i = 0; i < _params.size(); i++) {
col = (_cols == null) ? null : (Column) _cols.get(i);
_dict.setUnknown(ps, i + 1, _params.get(i), col);
}
}
Populate the parameters of an existing PreparedStatement
with values from this buffer. |
public void setParameters(List params) {
_params = params;
}
|
public boolean sqlEquals(String sql) {
return _sql.toString().equals(sql);
}
Compare internal SQL without resolving subselects or stringifying
parameters. |