me.icymint.libra.jdbc.model.Database Maven / Gradle / Ivy
package me.icymint.libra.jdbc.model;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
/**
* 数据库。
*
* @author Daniel Yu
* @since 2013-3-12
*
*/
public class Database extends SqlObject {
private class C implements Comparator {
@Override
public int compare(Table o1, Table o2) {
return o2.level() - o1.level();
}
}
private final LinkedHashMap lhm = new LinkedHashMap();
private final SqlFactory sf;
private String schema = null;
private C c = new C();
protected Database(SqlFactory sf) {
super();
this.sf = sf;
}
public void addTable(Table t) throws SQLException {
Table tt = lhm.get(t.getName());
if (tt == null) {
synchronized (lhm) {
tt = lhm.get(t.getName());
if (tt == null) {
lhm.put(t.getName(), t);
t.setParent(this);
return;
}
}
}
throw new SQLException("名称为" + t.getName() + "的表格已经存在于SQL仓库中。");
}
public void delTable(Table t) {
if (lhm.containsValue(t)) {
synchronized (lhm) {
if (lhm.containsValue(t)) {
lhm.remove(t.getName());
t.setParent(this);
}
}
}
}
public SqlFactory getFactory() {
return sf;
}
@Override
public String getId() {
return this.getInfo().getId();
}
@Override
public String getName() {
return this.getInfo().getName();
}
public String getSchema() {
return schema;
}
public Table getTable(String name) {
return lhm.get(name);
}
public Collection getTables() {
return lhm.values();
}
public void rebuild() {
List list = new LinkedList(this.getTables());
Collections.sort(list, c);
for (Table t : list) {
logger.info("正在删除表格 " + t.toString() + " ...");
if (t.exists())
t.drop();
}
Collections.reverse(list);
for (Table t : list) {
logger.info("正在创建表格 " + t.toString() + " ...");
t.create();
}
}
public void setSchema(String schema) {
this.schema = schema;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy