📄 sqlstatement.java
字号:
package pl.sql;
import java.util.*;
import java.sql.*;
import pl.*;
/**
* This class contains an information about sql statement.
*
* @author: Artyom Rudoy
*/
public class SqlStatement implements Cloneable
{
private StringBuffer sqlString = new StringBuffer();
private Vector parameters = new Vector();
private java.sql.Statement statement = null;
/**
* Creates SqlStatement.
*/
public SqlStatement()
{
super();
}
/**
* Adds a parameter for this statement.
*
* @param obj a parameter
*/
public void addParameter(Object obj, int plType)
{
parameters.add(new Parameter(obj, plType));
}
/**
* Adds an sql clause to this statement.
*
* @param sqlClause an sql clause
*/
public void addSqlClause(String sqlClause)
{
sqlString.append(sqlClause);
}
/**
* Adds an sql statement to this statement.
*
* @param statement a statement to add
*/
public void addSqlStatement(SqlStatement statement)
{
addSqlClause(statement.getSqlString());
for(int i = 0; i < statement.getSize(); i++)
{
parameters.add(statement.parameters.get(i));
}
}
/**
* Creates a copy of this sql statement.
*
* @return java.lang.Object
*/
public Object clone()
{
try
{
SqlStatement cloneObject = (SqlStatement)super.clone();
cloneObject.parameters = (Vector)parameters.clone();
cloneObject.statement = null;
return cloneObject;
}
catch(CloneNotSupportedException e)
{
return null;
}
}
/**
* Releases resources of the corresponding java.sql.Statement object.
*/
public void close() throws PlException
{
if(statement != null)
{
try
{
statement.close();
statement = null;
}
catch(SQLException e)
{
throw new PlException(e);
}
}
}
/**
* Insert the method's description here.
* Creation date: (09.12.2000 23:32:07)
* @exception java.lang.Throwable The exception description.
*/
public void finalize() throws java.lang.Throwable
{
close();
}
/**
* Returns a parameter with the given index.
*
* @return java.lang.Object
* @param index index of the parameter
*/
public Parameter getParameter(int index)
{
return (Parameter)parameters.get(index);
}
/**
* Returns number of parameters.
*
* @return int
*/
public int getSize()
{
return parameters.size();
}
/**
* Returns the sql string for this statement.
*
* @return java.lang.String
*/
public String getSqlString()
{
return sqlString.toString();
}
/**
* Returns corresponding java.sql.Statement object or null
* if it does not exist.
*
* @return java.sql.Statement
*/
public java.sql.Statement getStatement()
{
return statement;
}
/**
* Sets corresponding java.sql.Statement object.
*
* @param statement corresponding java.sql.Statement object
*/
public void setStatement(java.sql.Statement statement)
{
this.statement = statement;
}
/**
* Returns the string representation of this sql statement.
*
* @return java.lang.String
*/
public String toString()
{
String s = getSqlString();
s = this.getSqlByAllParameters(s);
return s;
}
private static final String[] dispartStringWithTokens(String stream, String delim) {
int count = 0; // 记数
for (int index = 0; index < stream.length() && index != -1; count++) {
index = stream.indexOf(delim, index);
if (index == -1) continue; // 最后一项不带分隔符
index += delim.length();
}
String[] result = new String[count];
for (int i = 0, begin = 0, end = 0; i < count; i++) {
end = stream.indexOf(delim, begin);
if (end == -1) end = stream.length(); // 最后一项不带分隔符
result[i] = new String(stream.substring(begin, end));
begin = end + delim.length();
}
return result;
}
public String getSqlByAllParameters(String orignalSql) {
String[] s2 = dispartStringWithTokens(orignalSql, "?");
String[] s3 = new String[s2.length];
StringBuffer stb = new StringBuffer();
try {
for (int i = 0; i < getSize(); i++) {
SqlStatement.Parameter parameter = getParameter(i);
//PlTypes.setParameter(pst, parameter.getValue(), parameter.getPlType(), i + 1);
s3[i] = parameter.getValue().toString();
//System.out.println(" *** i,s3[i]="+i+","+s3[i]);
}
// for(int i=0;i<s2.length;i++)
// {
// // System.out.println(" *** i,s2[i]="+i+","+s2[i]);
//
//
// }
for (int i = 0; i < getSize(); i++) {
stb.append(s2[i]);
if (s3[i] == null || s3[i].trim().equals("")) {
stb.append("?");
//System.out.println(" *** 1 i ,append="+i+","+s3[i]);
}
else {
stb.append(s3[i]);
//System.out.println(" *** 2 i ,append="+i+","+s3[i]);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return stb.toString();
}
public static class Parameter
{
private Object value = null;
private int plType = PlTypes.UNDEFINED;
public Parameter(Object value, int plType)
{
this.value = value;
this.plType = plType;
}
public Object getValue()
{
return value;
}
public int getPlType()
{
return plType;
}
}
public static void main(String[] argv)
{
String sql = "SELECT person.id ,person.name FROM person WHERE person.id=? AND person.name=?";
SqlStatement st = new SqlStatement();
st.addSqlClause(sql);
st.addParameter("1",-1);
st.addParameter("name",1);
// String s = st.getSqlByAllParameters(sql);
System.out.println("sql = "+st.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -