All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.icymint.libra.jdbc.model.Table Maven / Gradle / Ivy

package me.icymint.libra.jdbc.model;

import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;

/**
 * 数据库中表格。
 * 
 * @author Daniel Yu
 * @since 2013-3-12
 * 
 */
public class Table extends SqlObject {
	private class TableHolder {
		private final Column[] columns;
		private final int[] types;

		private TableHolder() {
			LinkedList cols = new LinkedList();
			for (Column c : Table.this.getColumns()) {
				if (!c.isAutoIncrement()) {
					cols.add(c);
				}
			}
			columns = cols.toArray(new Column[] {});
			types = new int[cols.size()];
			for (int i = 0; i < types.length; i++) {
				types[i] = columns[i].getTypes();
			}
		}
	}

	private TableHolder th = null;

	private final LinkedHashMap lhm = new LinkedHashMap();

	private final LinkedHashMap indexes = new LinkedHashMap();
	private final LinkedHashMap fks = new LinkedHashMap();
	private final LinkedHashSet keys = new LinkedHashSet();
	private Database parent = null;
	private final LinkedHashMap uniques = new LinkedHashMap();

	public void addForeignKey(ForeignKey fk) throws SQLException {
		Table t = this.getParent().getTable(fk.foreignTable());
		if (t == null)
			throw new SQLException("外键配置不准确,无法找到名称为" + fk.foreignTable()
					+ "的表格");
		if (this.getColumn(fk.getLocalColumn()) == null)
			throw new SQLException("外键配置不准确,无法找到名称为" + fk.getLocalColumn()
					+ "的字段");
		if (t.getColumn(fk.getForeignColumn()) == null)
			throw new SQLException("外键配置不准确,无法找到名称为" + fk.getForeignColumn()
					+ "的字段");

		ForeignKey ifk = fks.get(fk.foreignTable());
		if (ifk == null) {
			synchronized (fks) {
				ifk = fks.get(fk.foreignTable());
				if (ifk == null) {
					fks.put(fk.foreignTable(), fk);
					return;
				}
			}
		}
		throw new SQLException("外表" + fk.foreignTable() + "对应的外键配置已经存在!");
	}

	public void addIndex(Index index) throws SQLException {
		for (String c : index.getColumns()) {
			if (this.getColumn(c) == null)
				throw new SQLException("索引字段" + c + "不存在!");
		}
		Index ind = indexes.get(index.getName());
		if (ind == null) {
			synchronized (indexes) {
				ind = indexes.get(index.getName());
				if (ind == null) {
					indexes.put(index.getName(), index);
					return;
				}
			}
		}
		throw new SQLException("名称为" + index.getName() + "的索引已经存在!");
	}

	public void addUnique(Unique unique) throws SQLException {
		for (String c : unique.getColumns()) {
			if (this.getColumn(c) == null)
				throw new SQLException("索引字段" + c + "不存在!");
		}
		Unique ind = uniques.get(unique.getName());
		if (ind == null) {
			synchronized (uniques) {
				ind = uniques.get(unique.getName());
				if (ind == null) {
					uniques.put(unique.getName(), unique);
					return;
				}
			}
		}
		throw new SQLException("名称为" + unique.getName() + "的Unique已经存在!");
	}

	public void create() {
		// logger.debug(this.getTemplate().getDialect().createTable(this));
		this.getTemplate().execute(
				this.getTemplate().getDialect().createTable(this));
	}

	public void deletes() {
		this.getTemplate().execute(
				this.getTemplate().getDialect().deleteTable(this));
	}

	public void drop() {
		this.getTemplate().execute(
				this.getTemplate().getDialect().dropTable(this));
	}

	public boolean exists() {
		return this.getTemplate()
				.query(this.getTemplate().getDialect().existsTable(this),
						new int[] {}) != null;
	}

	private TableHolder fetch() {
		if (th == null) {
			th = new TableHolder();
		}
		return th;
	}

	public Column getColumn(String name) {
		return lhm.get(name);
	}

	public Column getColumnAtIndex(int index) throws SQLException {
		int i = 0;
		for (Column col : lhm.values()) {
			if (i++ == index)
				return col;
		}
		throw new SQLException("无法找到序号为" + index + "的字段。");
	}

	public Collection getColumns() {
		return lhm.values();
	}

	public Collection getForeignKeys() {
		return fks.values();
	}

	public Collection getIndexes() {
		return indexes.values();
	}

	public Collection getKeys() {
		return keys;
	}

	public Database getParent() {
		return parent;
	}

	public Collection getUniques() {
		return uniques.values();
	}

	public void insert(Object... objs) {
		this.getTemplate().execute(
				this.getTemplate().getDialect()
						.insertTable(this, this.fetch().columns),
				this.fetch().types, objs);
	}

	public void insertAll(Collection objs) {
		this.getTemplate().executeBatch(
				this.getTemplate().getDialect()
						.insertTable(this, this.fetch().columns),
				this.fetch().types, objs);
	}

	protected int level() {
		int level = 0;
		for (ForeignKey fk : this.getForeignKeys()) {
			level = Math.max(level, this.getParent()
					.getTable(fk.foreignTable()).level() + 1);
		}
		return level;
	}

	protected void setColumns(Column[] cols) {
		this.lhm.clear();
		for (Column col : cols)
			lhm.put(col.getName(), col);
		th = null;
	}

	protected void setKeys(String[] keys) throws SQLException {
		this.keys.clear();
		for (String key : keys) {
			Column k = lhm.get(key);
			if (k != null)
				this.keys.add(k);
			else
				throw new SQLException("名称为" + key + "的字段不存在!");
		}
	}

	protected void setParent(Database database) {
		parent = database;
	}

	public void trunc() {
		this.getTemplate().execute(
				this.getTemplate().getDialect().truncTable(this));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy