use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class ExtDaoInvocationHandler method migration.
/**
* 为数据表自动增减字段
*
* @param dao
* Dao实例
* @param klass
* 映射Pojo
* @param add
* 是否允许添加字段
* @param del
* 是否允许删除字段
* @param checkIndex
* 是否检查索引
* @param tableName
* 动态表名上下文
*/
public static void migration(Dao dao, final Class<?> klass, final boolean add, final boolean del, final boolean checkIndex, final Object tableName) {
final JdbcExpert expert = dao.getJdbcExpert();
if (tableName != null && Strings.isNotBlank(tableName.toString())) {
dao = ext(dao, tableName);
}
final Entity<?> en = dao.getEntity(klass);
if (!dao.exists(klass))
return;
final List<Sql> sqls = new ArrayList<Sql>();
final Set<String> _indexs = new HashSet<String>();
dao.run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
expert.setupEntityField(conn, en);
Statement stat = null;
ResultSet rs = null;
ResultSetMetaData meta = null;
try {
// 获取数据库元信息
stat = conn.createStatement();
rs = stat.executeQuery("select * from " + en.getTableName() + " where 1 != 1");
meta = rs.getMetaData();
Set<String> columnNames = new HashSet<String>();
int columnCount = meta.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnNames.add(meta.getColumnName(i).toLowerCase());
}
for (MappingField mf : en.getMappingFields()) {
if (mf.isReadonly())
continue;
String colName = mf.getColumnName();
if (columnNames.contains(colName.toLowerCase())) {
columnNames.remove(colName.toLowerCase());
continue;
}
if (add) {
log.infof("add column[%s] to table[%s]", mf.getColumnName(), en.getTableName());
sqls.add(expert.createAddColumnSql(en, mf));
}
}
if (del) {
for (String colName : columnNames) {
log.infof("del column[%s] from table[%s]", colName, en.getTableName());
Sql sql = Sqls.create("ALTER table $table DROP column $name");
sql.vars().set("table", en.getTableName());
sql.vars().set("name", colName);
sqls.add(sql);
}
}
// show index from mytable;
if (checkIndex)
_indexs.addAll(expert.getIndexNames(en, conn));
} catch (SQLException e) {
if (log.isDebugEnabled())
log.debugf("migration Table '%s' fail!", en.getTableName(), e);
} finally // Close ResultSet and Statement
{
Daos.safeClose(stat, rs);
}
}
});
// 创建索引
UpdateIndexSql indexSqls = createIndexs(dao, en, _indexs, tableName);
if (checkIndex) {
// 因为已删除的字段的索引是没办法删除的 所以要先处理索引 再处理字段
Sql[] delIndexSqls = indexSqls.getSqlsDel();
if (!Lang.isEmptyArray(delIndexSqls)) {
dao.execute(delIndexSqls);
}
}
for (Sql sql : sqls) {
dao.execute(sql);
}
if (checkIndex) {
Sql[] addIndexSqls = indexSqls.getSqlsAdd();
if (!Lang.isEmptyArray(addIndexSqls)) {
dao.execute(addIndexSqls);
}
}
// 创建关联表
dao.getJdbcExpert().createRelation(dao, en);
}
use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class DaoSupport method setDataSource.
public void setDataSource(DataSource ds, boolean isLazy) {
if (null != dataSource)
if (log.isWarnEnabled())
log.warn("Replaced a running dataSource!");
dataSource = ds;
if (expert == null)
expert = Jdbcs.getExpert(ds);
log.debug("select expert : " + expert.getClass().getName());
pojoMaker = new NutPojoMaker(expert);
meta = new DatabaseMeta();
final Set<String> keywords = new HashSet<String>(Daos.sql2003Keywords());
run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
try {
DatabaseMetaData dmd = conn.getMetaData();
meta.setProductName(dmd.getDatabaseProductName());
meta.setVersion(dmd.getDatabaseProductVersion());
log.debug("JDBC Driver --> " + dmd.getDriverVersion());
log.debug("JDBC Name --> " + dmd.getDriverName());
if (!Strings.isBlank(dmd.getURL()))
log.debug("JDBC URL --> " + dmd.getURL());
if (dmd.getDriverName().contains("mariadb") || dmd.getDriverName().contains("sqlite")) {
log.warn("Auto-select fetch size to Integer.MIN_VALUE, enable for ResultSet Streaming");
SqlContext.DEFAULT_FETCH_SIZE = Integer.MIN_VALUE;
}
String tmp = dmd.getSQLKeywords();
if (tmp != null) {
for (String keyword : tmp.split(",")) {
keywords.add(keyword.toUpperCase());
}
}
expert.checkDataSource(conn);
} catch (Exception e) {
log.info("something wrong when checking DataSource", e);
}
}
});
if (log.isDebugEnabled())
log.debug("Database info --> " + meta);
expert.setKeywords(keywords);
if (!isLazy) {
holder = new EntityHolder(this.expert, dataSource);
holder.maker = createEntityMaker();
}
setRunner(runner);
setExecutor(executor);
}
use of org.nutz.dao.ConnCallback in project nutz by nutzam.
the class NutDao method create.
public synchronized <T> Entity<T> create(Class<T> classOfT, boolean dropIfExists) {
Entity<T> en = holder.getEntity(classOfT);
if (exists(en.getTableName())) {
if (dropIfExists) {
expert.dropEntity(this, en);
} else {
expert.createRelation(this, en);
return en;
}
}
holder.remove(classOfT.getName());
final Entity<T> _en = holder.getEntity(classOfT);
expert.createEntity(this, _en);
// 最后在数据库中验证一下实体各个字段
run(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 create.
public synchronized <T> Entity<T> create(final Entity<T> en, boolean dropIfExists) {
if (exists(en.getTableName())) {
if (dropIfExists) {
expert.dropEntity(this, en);
} else {
expert.createRelation(this, en);
return en;
}
}
expert.createEntity(this, en);
// 最后在数据库中验证一下实体各个字段
run(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 create.
public synchronized <T extends Map<String, ?>> Entity<T> create(String tableName, T map, boolean dropIfExists) {
final Entity<T> en = holder.makeEntity(tableName, map);
if (exists(en.getTableName())) {
if (dropIfExists) {
expert.dropEntity(this, en);
} else {
expert.createRelation(this, en);
return en;
}
}
expert.createEntity(this, en);
// 最后在数据库中验证一下实体各个字段
run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
expert.setupEntityField(conn, en);
}
});
return en;
}
Aggregations