use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.
the class NutDaoExecutor method _runExec.
// 执行存储过程,简单实现
protected void _runExec(Connection conn, DaoStatement st) throws SQLException {
if (st.getContext().getPager() != null) {
throw Lang.makeThrow(DaoException.class, "NOT Pageable : " + st);
}
// 打印调试信息
String sql = st.toPreparedStatement();
if (log.isDebugEnabled())
log.debug(sql);
Object[][] paramMatrix = st.getParamMatrix();
CallableStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareCall(sql);
ValueAdaptor[] adaptors = st.getAdaptors();
HashMap<Integer, OutParam> outParams = new HashMap<Integer, OutParam>();
if (st instanceof Sql) {
VarIndex varIndex = ((Sql) st).paramIndex();
VarSet varSet = ((Sql) st).params();
for (int i = 0; i < varIndex.size(); i++) {
String name = varIndex.getOrderName(i);
if (name.startsWith("OUT") && varSet.get(name).getClass() == Integer.class) {
Integer t = (Integer) varSet.get(name);
outParams.put(i, new OutParam(name, t));
}
}
}
// 创建语句并设置参数
if (paramMatrix != null && paramMatrix.length > 0) {
PreparedStatement pst = (PreparedStatement) stmt;
Object[] pm = paramMatrix[0];
for (int i = 0; i < pm.length; i++) {
OutParam outParam = outParams.get(i);
if (outParam == null)
adaptors[i].set(pst, pm[i], i + 1);
else
stmt.registerOutParameter(i + 1, outParam.jdbcType);
}
}
stmt.execute();
if (outParams.size() > 0) {
Record r = Record.create();
for (Entry<Integer, OutParam> en : outParams.entrySet()) {
OutParam outParam = en.getValue();
int jdbcIndex = en.getKey() + 1;
Object value;
switch(outParam.jdbcType) {
case Types.INTEGER:
value = stmt.getInt(jdbcIndex);
break;
case Types.TIMESTAMP:
value = stmt.getTimestamp(jdbcIndex);
break;
case Types.CLOB:
value = stmt.getString(jdbcIndex);
break;
case Types.DATE:
value = stmt.getDate(jdbcIndex);
break;
default:
value = stmt.getObject(jdbcIndex);
break;
}
r.set(outParam.name.substring(3), value);
}
st.getContext().attr("OUT", r);
}
// 先尝试读取第一个,并调用一次回调
rs = stmt.getResultSet();
try {
st.onAfter(conn, rs, null);
} finally {
if (rs != null)
rs.close();
}
while (true) {
if (stmt.getMoreResults()) {
rs = stmt.getResultSet();
try {
if (rs != null)
st.onAfter(conn, rs, null);
} finally {
if (rs != null)
rs.close();
}
}
break;
}
} finally {
if (stmt != null)
stmt.close();
}
}
use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.
the class ExtDaoInvocationHandler method updateBySpecialChain.
/**
* 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
*
* @see org.nutz.dao.Chain#addSpecial(String, Object)
*/
@SuppressWarnings({ "rawtypes" })
public static int updateBySpecialChain(Dao dao, Entity en, String tableName, Chain chain, Condition cnd) {
if (en != null)
tableName = en.getTableName();
if (tableName == null)
throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
final StringBuilder sql = new StringBuilder("UPDATE ").append(tableName).append(" SET ");
Chain head = chain.head();
final List<Object> values = new ArrayList<Object>();
final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
while (head != null) {
MappingField mf = null;
if (en != null)
mf = en.getField(head.name());
String colName = head.name();
if (mf != null)
colName = mf.getColumnNameInSql();
sql.append(colName).append("=");
if (head.special()) {
if (head.value() != null && head.value() instanceof String) {
String str = (String) head.value();
if (str.length() > 0) {
switch(str.charAt(0)) {
case '+':
case '-':
case '*':
case '/':
case '%':
case '&':
case '^':
case '|':
sql.append(colName);
break;
}
}
}
sql.append(head.value());
} else {
sql.append("?");
values.add(head.value());
ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value());
if (mf != null && mf.getAdaptor() != null)
adaptor = mf.getAdaptor();
adaptors.add(adaptor);
}
sql.append(" ");
head = head.next();
if (head != null)
sql.append(", ");
}
if (cnd != null)
sql.append(" ").append(cnd.toSql(en));
if (log.isDebugEnabled())
log.debug(sql);
final int[] ints = new int[1];
dao.run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
PreparedStatement ps = conn.prepareStatement(sql.toString());
try {
for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1);
ints[0] = ps.executeUpdate();
} finally {
Daos.safeClose(ps);
}
}
});
return ints[0];
}
use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.
the class ExtDaoInvocationHandler method insertBySpecialChain.
/**
* 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
*
* @see org.nutz.dao.Chain#addSpecial(String, Object)
*/
@SuppressWarnings({ "rawtypes" })
public static void insertBySpecialChain(Dao dao, Entity en, String tableName, Chain chain) {
if (en != null) {
tableName = en.getTableName();
}
if (tableName == null) {
throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
}
final StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append(" (");
StringBuilder _value_places = new StringBuilder(" VALUES(");
final List<Object> values = new ArrayList<Object>();
final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
Chain head = chain.head();
while (head != null) {
String colName = head.name();
MappingField mf = null;
if (en != null) {
mf = en.getField(colName);
if (mf != null) {
colName = mf.getColumnNameInSql();
}
}
sql.append(colName);
if (head.special()) {
_value_places.append(head.value());
} else {
if (en != null) {
mf = en.getField(head.name());
}
_value_places.append("?");
values.add(head.value());
ValueAdaptor adaptor = head.adaptor();
if (adaptor == null) {
if (mf != null && mf.getAdaptor() != null) {
adaptor = mf.getAdaptor();
} else {
adaptor = Jdbcs.getAdaptorBy(head.value());
}
}
adaptors.add(adaptor);
}
head = head.next();
if (head != null) {
sql.append(", ");
_value_places.append(", ");
}
}
sql.append(")");
_value_places.append(")");
sql.append(_value_places);
if (log.isDebugEnabled()) {
log.debug(sql);
}
dao.run(new ConnCallback() {
@Override
public void invoke(Connection conn) throws Exception {
PreparedStatement ps = conn.prepareStatement(sql.toString());
try {
for (int i = 0; i < values.size(); i++) {
adaptors.get(i).set(ps, values.get(i), i + 1);
}
ps.execute();
} finally {
Daos.safeClose(ps);
}
}
});
}
use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.
the class SqlValueRange method joinAdaptor.
public int joinAdaptor(Entity<?> en, ValueAdaptor[] adaptors, int off) {
if (size == 0)
return off;
MappingField mf = _field(en);
ValueAdaptor adaptor = null;
if (mf == null) {
for (Object object : values) {
if (object != null) {
adaptor = Jdbcs.getAdaptorBy(object);
break;
}
}
if (adaptor == null)
adaptor = Jdbcs.Adaptor.asNull;
} else {
adaptor = mf.getAdaptor();
}
for (int i = off; i < off + size; i++) {
adaptors[i] = adaptor;
}
return 0;
}
use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.
the class SimpleDaoTest method test_issue_xxx.
// 这个TestCase的意义何在? 删掉了
// @Test
public void test_issue_xxx() {
final Object[] re = new Object[1];
ValueAdaptor va = new ValueAdaptor() {
@Override
public void set(PreparedStatement stat, Object obj, int index) throws SQLException {
re[0] = obj;
stat.setString(index, "ABC");
}
@Override
public Object get(ResultSet rs, String colName) throws SQLException {
// TODO Auto-generated method stub
return null;
}
};
List<String> name = Arrays.asList("wendal");
Sql sql = Sqls.create("select * from t_pet where name=@name");
sql.setParam("name", name);
sql.setValueAdaptor("name", va);
dao.execute(sql);
assertEquals(name, re[0]);
}
Aggregations