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

org.tinygroup.database.table.impl.TableProcessorImpl Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * --------------------------------------------------------------------------
 *  版权 (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  本开源软件遵循 GPL 3.0 协议;
 *  如果您不遵循此协议,则不被允许使用此文件。
 *  你可以从下面的地址获取完整的协议文本
 *
 *       http://www.gnu.org/licenses/gpl.html
 */
package org.tinygroup.database.table.impl;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tinygroup.database.ProcessorManager;
import org.tinygroup.database.config.table.Table;
import org.tinygroup.database.config.table.Tables;
import org.tinygroup.database.table.TableProcessor;
import org.tinygroup.database.table.TableSqlProcessor;
import org.tinygroup.database.util.DataBaseUtil;
import org.tinygroup.metadata.util.MetadataUtil;
import org.tinygroup.springutil.SpringUtil;

public class TableProcessorImpl implements TableProcessor {
	private  Map> tableMap = new HashMap>();

	public void addTables(Tables tables) {
		String packageName = MetadataUtil.passNull(tables.getPackageName());
		if(!tableMap.containsKey(packageName)){
			tableMap.put(packageName, new HashMap());
		}
		Map nameMap = tableMap.get(packageName);
		for (Table table : tables.getTableList()) {
			nameMap.put(table.getName(), table);
		}
	}
	
	public void addTable(Table table) {
		String packageName = MetadataUtil.passNull(table.getPackageName());
		if(!tableMap.containsKey(packageName)){
			tableMap.put(packageName, new HashMap());
		}
		Map nameMap = tableMap.get(packageName);
	    nameMap.put(table.getName(), table);
	}

	public Table getTable(String packageName, String name) {
		if (packageName != null) {
			Map packageMap = tableMap.get(packageName);
			if (packageMap != null) {
				Table table = packageMap.get(name);
				if (table != null) {
					return table;
				}
			} else {
				//如果当前包中没有找到,则从所有的包中去找
//				throw new RuntimeException("未找到package:" + packageName
//						+ ",name:" + name + "的表格");
			}
		}
		for (String pkgName : tableMap.keySet()) {
			Map packageMap = tableMap.get(pkgName);
			if (packageMap != null) {
				Table table = packageMap.get(name);
				if (table != null) {
					return table;
				}
			}
		}
		throw new RuntimeException("未找到package:" + packageName + ",name:"
				+ name + "的表格");
	}

	public Table getTable(String name) {
		return getTable(null, name);
	}

	public List getCreateSql(String name, String language) {
		return getCreateSql(name, null, language);
	}

	public List getCreateSql(String name, String packageName,
			String language) {
		Table table = getTable(packageName, name);
		return getCreateSql(table, packageName, language);
	}

	public List getCreateSql(Table table, String packageName,
			String language) {
		TableSqlProcessor sqlProcessor = getSqlProcessor(language);
		return sqlProcessor.getCreateSql(table, packageName);
	}

	public List getCreateSql(Table table, String language) {
		return getCreateSql(table, null, language);
	}

	public Table getTableById(String id) {
		for (Map tables : tableMap.values()) {
			for (Table table : tables.values()) {
				if (table.getId().equals(id)) {
					return table;
				}
			}
		}
		throw new RuntimeException("未找到ID:" + id + "的表格");
	}

	public List getTables() {
		List
tableList = new ArrayList
(); for (Map tables : tableMap.values()) { tableList.addAll(tables.values()); } return tableList; } public List getUpdateSql(String name, String packageName, DatabaseMetaData metadata,String catalog, String language) { Table table = getTable(packageName, name); return getUpdateSql(table, packageName, metadata,catalog, language); } public List getUpdateSql(Table table, String packageName, DatabaseMetaData metadata,String catalog, String language) { TableSqlProcessor sqlProcessor = getSqlProcessor(language); return sqlProcessor.getUpdateSql(table, packageName, metadata, catalog); } public String getDropSql(String name, String packageName, String language) { Table table = getTable(packageName, name); return getDropSql(table, packageName, language); } public String getDropSql(Table table, String packageName, String language) { TableSqlProcessor sqlProcessor = getSqlProcessor(language); return sqlProcessor.getDropSql(table, packageName); } public boolean checkTableExist(Table table, String catalog, DatabaseMetaData metadata, String language) { TableSqlProcessor sqlProcessor = getSqlProcessor(language); return sqlProcessor.checkTableExist(table, catalog, metadata); } private TableSqlProcessor getSqlProcessor(String language) { ProcessorManager processorManager = SpringUtil .getBean(DataBaseUtil.PROCESSORMANAGER_BEAN); TableSqlProcessor sqlProcessor = (TableSqlProcessor) processorManager .getProcessor(language, "table"); return sqlProcessor; } }