⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableconfig.java

📁 TestDataBuilder是一个采用Java编写的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public String getDestroy() {
		return destroy;
	}

	public void setDestroy(String destroy) {
		this.destroy = destroy;
	}

	public String getInit() {
		return init;
	}

	public void setInit(String init) {
		this.init = init;
	}

	public String getOnError() {
		return onError;
	}

	public void setOnError(String onError) {
		this.onError = onError;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
    
    /**
     * 得到所有用到的role名称.
     * <p><code>getAllUsedRoles</code></p>
     * @return
     * @author LiuXiaojie 2007-6-18
     */
    public List<String> getAllRoles(){
        List<String> roleNames = new LinkedList<String>();
        for(int i=0;i < this.getColumnConfigs().size(); i++){
            ColumnConfig columnConfig = (ColumnConfig) this.getColumnConfigs().get(i);
            String roleName = columnConfig.getRefRoleName();
            if(!roleNames.contains(roleName)){
                roleNames.add(roleName);
            }
        }
        return roleNames;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final TableConfig other = (TableConfig) obj;
        if (tableName == null) {
            if (other.tableName != null) return false;
        } else if (!tableName.equals(other.tableName)) return false;
        return true;
    }

    public TableConfig(String tableName){
    	this.tableName = tableName;
    }
    
    public TableConfig(){
    	
    }

    public RoleFactory getRoleFactory() {
        return roleFactory;
    }

    public void setRoleFactory(RoleFactory roleFactory) {
        this.roleFactory = roleFactory;
        this.roleFactory.setTableConfig(this);
    }

    public Boolean getIsGenerate() {
        return isGenerate;
    }

    public void setIsGenerate(Boolean isGenerate) {
        this.isGenerate = isGenerate;
    }
    
    /**
	 * 根据参照的role生成数据.
	 * <p><code>getValueByRole</code></p>
	 * @return
	 * @throws BaseException
	 * @throws SQLException
	 * @author LiuXiaojie 2008-9-5
     * @throws BaseException 
     * @throws SQLException 
	 */
	private Object getValueByRole(ColumnConfig columnConfig) throws BaseException, SQLException {
        Object retValue = null;
		String ref = columnConfig.getRef();
		if (ref != null) {
			if (columnConfig.isRefComplexType()) { 
				String roleName = ref.substring(0, ref.indexOf("."));
				String valueKey = ref.substring(ref.indexOf(".") + 1);
				ComplexObj complexObj = getComplexRole(roleName);
                if(complexObj != null){
                    retValue = complexObj.getValue(valueKey);
                }else{
                    retValue = null;
                }
			} else {
                if(retValue == null){
                    retValue = getRoleFactory().getValueByRole(ref);
                }
			}
		} 
        
		return retValue;
	}
	
	/**
	 * 从RoleFactory取出一个复合数据类型
	 * <p>
	 * <code>getComplexRole</code>
	 * </p>
	 * 
	 * @param roleName
	 * @return
	 * @author LiuXiaojie 2007-6-17
	 * @throws BaseException 
	 * @throws SQLException 
	 * @throws SQLException 
	 */
	public ComplexObj getComplexRole(String roleName) throws BaseException, SQLException {
		Object complexObj = (Object) complexTypes.get(roleName);
		if (complexObj == null) {
            Object tempObj = null;
            if(complexObj == null){
                tempObj = getRoleFactory().getValueByRole(roleName);
            }
            
            if(tempObj != null){
                try{
                    complexObj = (Object)tempObj;
                }catch(Throwable ex){
                    throw new BaseException(String.format("获取的复合类型[%s]转换出错", roleName), ex);
                }
            }else{
            	complexObj = new NullObject();
            }
			complexTypes.put(roleName, complexObj);
		}
		if(complexObj instanceof NullObject){
			return null;
		}
		return (ComplexObj)complexObj;
	}

	private Map<String, Object> valueCache = new HashMap<String, Object>();
	
	private Map<String,Object> complexTypes = new HashMap<String,Object>();
	
	public void clearCache(){
		this.complexTypes.clear();
		this.valueCache.clear();
	}
	
	/**
	 * 根据配置的规则,生成值,并将取的值,存入cache中.
	 * <p><code>getValue</code></p>
	 * @param columnConfig
	 * @return
	 * @throws BaseException
	 * @author LiuXiaojie 2008-9-5
	 * @throws SQLException 
	 */
	public Object getValue(ColumnConfig columnConfig) throws BaseException, SQLException{
		Object value = valueCache.get(columnConfig.getName());
		if(value == null){
			value = this.getValueByRole(columnConfig);
			if(value == null){
				value = new NullObject();
			}
			valueCache.put(columnConfig.getName(), value);
		}
		if(value instanceof NullObject){
			value = null;
		}
		return value;
	}

	/**
	 * 获取当前表当前行的一个字段的值。
	 * @param columnName 列名。
	 * @return 返回列值。
	 * @throws BaseException
	 * @throws SQLException
	 */
	public Object getValue(String columnName) throws BaseException, SQLException{
		ColumnConfig columnConfig = getColumnConfig(columnName);
		if(columnConfig == null){
			throw new BaseException("column [" + columnName + "] not exist");
		}
		return getValue(columnConfig);
	}
	
	/**
	 * 根据给定的字段,获取参照表。
	 * @param fields 字段(这些字段必须是一个外键中的多个字段)
	 * @return 方法会根据给出的fields,获取对应的值,并在参照表中找到对应的行。
	 * @throws BaseException
	 * @throws SQLException
	 */
    public TableConfig getReferencer(String ... fields ) throws BaseException, SQLException{
    	WorkspaceDataCache workspaceDataCache = WorkspaceDataCache.getInstance();
    	Database database = workspaceDataCache.getDatabase();
    	Table table = database.getTable(this.getTableName());
    	ForeignKey foreignKey = table.getForeignKeyByField(fields[0]);
    	String refTable = foreignKey.getRefTable();
    	TableConfig retTableConfig = workspaceDataCache.getTableConfig(refTable);
    	
    	StringBuilder sql = new StringBuilder("select * from ").append(refTable);
    	sql.append(" where 1=1 ");
    	List<Object> args = new LinkedList<Object>();
    	for(String field : fields){
    		Object value = this.getValue(field);
    		args.add(value);
    		String refField = foreignKey.getRefFiled(field);
    		sql.append(" and ").append(refField).append("=? ");
    	}
       Connection conn = workspaceDataCache.getTestDataConfig().getDefConnection();
       PreparedStatement statement = conn.prepareStatement(sql.toString());
       for (int i = 0; i < args.size(); i++) {
    	   if(args.get(i) == null){
    		   statement.setString(i+1,null);
    	   }else{
    		   statement.setObject(i+1, args.get(i));
       		}
       }
       ResultSet rs =  statement.executeQuery();
      
       if(rs.next()){
    	   ResultSetMetaData metaData = rs.getMetaData();
           int columnCount = metaData.getColumnCount();
           for(int i=0;i < columnCount;i++){
        	   String columnName = metaData.getColumnName(i+1);
        	   Object value = rs.getObject(columnName);
        	   if(value == null){
        		   value = new NullObject();
        	   }
        	   retTableConfig.valueCache.put(columnName, value);
           }           
       }
       
    	return retTableConfig;
    }
    
	public ColumnConfig getColumnConfig(String columnName){
		ColumnConfig columnConfig = null;
		List<ColumnConfig> columnConfigs = this.getColumnConfigs();
		if(columnConfigs != null){
			for(ColumnConfig tmpColumnConfig : columnConfigs){
				if(tmpColumnConfig.getName().equalsIgnoreCase(columnName)){
					columnConfig = tmpColumnConfig;
					break;
				}
			}
		}
		return columnConfig;
	}
	
	private String insertSQLFormat = null;
	private PreparedStatement insertStatement = null;
    
	/**
     * 得到插入语句.
     * <p><code>getInsertStatement</code></p>
     * @return
     * @throws DrmException
     * @author LiuXiaojie 2007-4-25
     * @throws SQLException 
     */
    public PreparedStatement getInsertStatement(Connection conn) {
        if (insertStatement == null) {
            if (this.getColumnConfigs().size() > 0) {
                StringBuffer strFields = new StringBuffer("insert into " + this.getTableName() + " (");
                StringBuffer values_ = new StringBuffer(" values (");
                
                List columnConfigs = this.getColumnConfigs();
                for (int i = 0; i < columnConfigs.size(); i++) {
                	ColumnConfig columnConfig = (ColumnConfig) columnConfigs.get(i);
                	
                    if (i == 0) {
                        strFields.append(columnConfig.getName());
                      
                    } else {
                        strFields.append(", " + columnConfig.getName());
                        values_.append(", ");
                    }
                	
                    Role role = this.getRoleFactory().getRole(columnConfig.getRef());
                	if(role != null && role.isSQLFuncMethod()){
                    	values_.append( role.getSQL());
                    }else{
                    	values_.append("?");
                    }
                }

                strFields.append(")");
                values_.append(")");
                try {
                    insertSQLFormat = strFields.toString() + values_.toString();
                    this.insertStatement = conn.prepareStatement(insertSQLFormat);
                    insertSQLFormat = insertSQLFormat.replace("?", "%s");
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                logger.info("INSERT SQL:" + strFields.toString() + values_.toString());
            }else{
                logger.error("*************** no any insert fields! *****************");
            }
        }
        return insertStatement;
    }
   
	public void destroyStates() {
		if(this.insertStatement != null){
			try {
				this.insertStatement.close();
				this.insertSQLFormat = null;
			} catch (SQLException ex) {				
                logger.error("destroy", ex);
			}
			this.insertStatement = null;
			
		}
	}

	public String getInsertSQLFormat() {
		return insertSQLFormat;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -