package some;


import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

public class TableInfo {

	private TreeMap<String, ColumnInfo> columnInfoMap = new TreeMap<String, ColumnInfo>(new ComparatorString2Int()) ;		// テーブル固有のカラムを格納
	private TreeMap<String, ColumnInfo> primaryKeyMap = new TreeMap<String, ColumnInfo>(new ComparatorString2Int()) ;		// プライマリーキー（論理キー）を格納

	public String toString() {
		StringBuffer sBuff = new StringBuffer();
		sBuff.append( "[TableName=" + getTableName() + "]");
		sBuff.append( "[TableNameKnj=" + getTableNameKnj() + "]");
		sBuff.append( "[TableType=" + getTableType() + "]");

		return sBuff.toString();
	}

//	------------------------------< 変数設定 >
	private String tableName = "";
	private String tableNameKnj = "";
	private String tableType = "";

//	------------------------------< setter定義 >
	public void setTableName(String sVal){ this.tableName=sVal; }
	public void setTableNameKnj(String sVal){ this.tableNameKnj=sVal; }
	public void setTableType(String sVal){ this.tableType=sVal; }

//	------------------------------< getter定義 >
	public String getTableName(){ return this.tableName; }
	public String getTableNameKnj(){ return this.tableNameKnj; }
	public String getTableType(){ return this.tableType; }

	public TreeMap<String, ColumnInfo> getPrimaryKeyMap() {
		return primaryKeyMap;
	}
	public TreeMap<String, ColumnInfo> getColumnMap() {
		return columnInfoMap ;
	}

	private List<ColumnInfo> columnList;
	public List<ColumnInfo> getColumnList() {
		return this.columnList;
	}
	public void setColumnList(List<ColumnInfo> columnList) {
		this.columnList = columnList;
		columnInfoMap.clear();
		for (ColumnInfo columnInfo : columnList) {
			columnInfoMap.put(String.valueOf(columnInfo.getColumnId()), columnInfo);

			// プライマリーキー（論理キー）の場合
			if (columnInfo.getPrimaryKeyNo() > 0) {
				primaryKeyMap.put(String.valueOf(columnInfo.getPrimaryKeyNo()), columnInfo);
			}
		}
	}

	public ColumnInfo getColumn(String columnName) {
		ColumnInfo retCol = null ;

		for (String key : columnInfoMap.keySet()) {
			ColumnInfo column = columnInfoMap.get(key);

			if (column.getColumnName().equalsIgnoreCase(columnName)) {
				retCol = column;
				break ;
			}
		}
		return retCol;
	}

	private class ComparatorString2Int implements Comparator<Object> {

		public ComparatorString2Int() {
			super();
		}

		public int compare(Object arg0, Object arg1) {
			String sArg0 = (String)arg0;
			String sArg1 = (String)arg1;

			int nArg0 = Integer.parseInt((String)arg0);
			int nArg1 = Integer.parseInt((String)arg1);

			return (nArg0 - nArg1);
		}

}

}
