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

me.icymint.libra.jdbc.dialect.AbstractDialect Maven / Gradle / Ivy

package me.icymint.libra.jdbc.dialect;

import java.util.Collection;

import me.icymint.libra.jdbc.model.Column;
import me.icymint.libra.jdbc.model.ForeignKey;
import me.icymint.libra.jdbc.model.Index;
import me.icymint.libra.jdbc.model.Table;
import me.icymint.libra.jdbc.model.Unique;

/**
 * 方言汇总,其预定义了一些通用SQL方法。
 * 
 * @author Daniel Yu
 * @since 2013-3-14
 * 
 */
public abstract class AbstractDialect implements Dialect {
	@Override
	public String deleteTable(Table t) {
		return "DELETE FROM " + tableName(t);
	}

	@Override
	public String dropTable(Table t) {
		return "DROP TABLE " + tableName(t);
	}

	/**
	 * 外键生成方案。
	 * 
	 * @param sb
	 * @param foreignKeys
	 * @param t
	 */
	protected void genFKeys(StringBuilder sb,
			Collection foreignKeys, Table t) {
		for (ForeignKey fk : foreignKeys) {
			Table ft = t.getParent().getTable(fk.foreignTable());
			Column l = t.getColumn(fk.getLocalColumn());
			Column f = ft.getColumn(fk.getForeignColumn());
			sb.append("CONSTRAINT FK_").append(f.toString())
					.append(" FOREIGN KEY (").append(l.toString())
					.append(") REFERENCES ").append(tableName(ft)).append("(")
					.append(f.toString()).append("),\n");
		}
	}

	/**
	 * 索引生成方案。
	 * 
	 * @param sb
	 * @param inds
	 * @param t
	 */
	protected void genIndexes(StringBuilder sb, Collection inds, Table t) {
		if (inds.size() > 0) {
			for (Index x : inds) {
				sb.append(";\nCREATE INDEX ").append(x.getName())
						.append(" ON ").append(tableName(t)).append("(");
				String[] cols = x.getColumns();
				Column c = t.getColumn(cols[0]);
				sb.append(c.toString());
				for (int i = 1; i < cols.length; i++) {
					sb.append(",").append(t.getColumn(cols[i]));
				}
				sb.append(") ");
			}
		}
	}

	/**
	 * 主键生成方案。
	 * 
	 * @param sb
	 * @param ks
	 * @param t
	 */
	protected void genKeys(StringBuilder sb, Collection ks, Table t) {
		if (ks.size() > 0) {
			sb.append("CONSTRAINT PK_" + t.toString() + " PRIMARY KEY (");
			for (Column k : ks) {
				sb.append(k.toString()).append(",");
			}
			sb.deleteCharAt(sb.length() - 1).append("),\n");
		}
	}

	/**
	 * Unique生成方案。
	 * 
	 * @param sb
	 * @param us
	 * @param t
	 */
	protected void genUniques(StringBuilder sb, Collection us, Table t) {
		if (us.size() > 0) {
			for (Unique u : us) {
				sb.append("CONSTRAINT UC_").append(u.getName())
						.append(" UNIQUE (");
				String[] cols = u.getColumns();
				Column c = t.getColumn(cols[0]);
				sb.append(c.toString());
				for (int i = 1; i < cols.length; i++) {
					sb.append(",").append(t.getColumn(cols[i]));
				}
				sb.append("),\n");
			}
		}
	}

	@Override
	public String insertTable(Table t, Column[] cols) {
		StringBuilder sb = new StringBuilder("INSERT INTO ")
				.append(tableName(t)).append(" (").append(cols[0].toString());
		for (int i = 1; i < cols.length; i++) {
			sb.append(",").append(cols[i].toString());
		}
		sb.append(") VALUES (?");
		for (int i = 1; i < cols.length; i++) {
			sb.append(",?");
		}
		return sb.append(")").toString();
	}

	/**
	 * 获取合适的表名,为数据表添加schema前缀。
	 * 

* 例如:SqlServer数据库以dbo.为前缀;Db2表则需要添加特定的schema。 * * @param t */ protected String tableName(Table t) { return t.toString(); } @Override public String truncTable(Table t) { return "TRUNCATE TABLE " + tableName(t); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy