use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class InsertTest method test_fastInsert_rollback_jdbc.
/**
* Github Issue #131, 这个证明了 JDBC 驱动支持事务
*/
@Test
public void test_fastInsert_rollback_jdbc() {
dao.create(Pet.class, true);
try {
Trans.exec(new Molecule<Object>() {
public void run() {
dao.run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
PreparedStatement ps = conn.prepareStatement("INSERT INTO t_pet(name) VALUES(?)");
for (int i = 0; i < 100; i++) {
ps.setString(1, "XXXXX" + i);
ps.addBatch();
}
ps.execute();
}
});
throw new RuntimeException();
}
});
} catch (Throwable e) {
}
assertEquals(0, dao.count(Pet.class));
}
use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class EntityHolder method makeEntity.
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T extends Map<String, ?>> Entity<T> makeEntity(String tableName, T map) {
final NutEntity<T> en = new NutEntity(map.getClass());
en.setTableName(tableName);
en.setViewName(tableName);
boolean check = false;
for (Entry<String, ?> entry : map.entrySet()) {
String key = entry.getKey();
// 是实体补充描述吗?
if (key.startsWith("#")) {
en.getMetas().put(key.substring(1), entry.getValue().toString());
continue;
} else // 以 "." 开头的字段,不是实体字段
if (key.startsWith(".")) {
continue;
}
// 是实体字段
Object value = entry.getValue();
Mirror<?> mirror = Mirror.me(value);
NutMappingField ef = new NutMappingField(en);
if (key.startsWith("+")) {
ef.setAsAutoIncreasement();
if (mirror != null && mirror.isIntLike())
ef.setAsId();
key = key.substring(1);
}
if (key.startsWith("!")) {
ef.setAsNotNull();
key = key.substring(1);
}
if (key.startsWith("*")) {
key = key.substring(1);
if (mirror != null && mirror.isIntLike())
ef.setAsId();
else
ef.setAsName();
}
ef.setName(key);
ef.setType(null == value ? Object.class : value.getClass());
ef.setColumnName(key);
// 猜测一下数据库类型
Jdbcs.guessEntityFieldColumnType(ef);
ef.setAdaptor(expert.getAdaptor(ef));
if (mirror != null)
ef.setType(mirror.getType());
// 这里比较纠结,回设的时候应该用什么呢?
ef.setInjecting(new InjectToMap(key));
ef.setEjecting(new EjectFromMap(entry.getKey()));
if (ef.isAutoIncreasement() && ef.isId() && expert.isSupportAutoIncrement() && !expert.isSupportGeneratedKeys()) {
en.addAfterInsertMacro(expert.fetchPojoId(en, ef));
}
en.addMappingField(ef);
if (mirror != null && !check)
check = mirror.isEnum();
}
en.checkCompositeFields(null);
// 最后在数据库中验证一下实体各个字段
if (check)
connCallback.invoke(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
expert.setupEntityField(conn, en);
}
});
// 搞定返回
return en;
}
use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class NutDao method exists.
public boolean exists(final String tableName) {
final boolean[] ee = { false };
this.run(new ConnCallback() {
public void invoke(Connection conn) {
Statement stat = null;
ResultSet rs = null;
try {
stat = conn.createStatement();
// 增加不等式,减少sql执行时间
String sql = "SELECT COUNT(1) FROM " + tableName + " where 1!=1";
rs = stat.executeQuery(sql);
if (rs.next())
ee[0] = true;
} catch (SQLException e) {
} finally {
Daos.safeClose(stat, rs);
}
}
});
return ee[0];
}
use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class ExtDaoInvocationHandler method checkTableColumn.
/**
* 检查分表中是否有字段变化 提示
*
* @param dao
* Dao实例
* @param tableName
* 动态表名上下文
* @param clsType
* 映射Pojo
*/
public static void checkTableColumn(Dao dao, Object tableName, final Class<?> clsType) {
final NutDao d = (NutDao) dao;
final JdbcExpert expert = d.getJdbcExpert();
ext(d, tableName).run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
Entity<?> en = d.getEntity(clsType);
expert.setupEntityField(conn, en);
}
});
}
use of org.nutz.dao.ConnCallback 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];
}
Aggregations