/** * 获取表的某一列 * * @param dsId 数据源id * @param tableName 表名 * @param columnName 列名 * @return MdRdbColumn * @author Kevice * @time 2012-12-18 下午3:21:37 */ public static MdRdbColumn getColumn(String dsId, String tableName, String columnName) { if (StringTool.isBlank(tableName) || StringTool.isBlank(columnName)) { return null; } if (StringTool.isBlank(dsId)) { dsId = JoyProperties.DB_DATASOURCEID; } Map<String, MdRdbColumn> columns = getColumns(dsId, tableName); return columns.get(columnName); }
/** * 根据数据源ID、对象名从缓存中取得该对象的元数据信息 * * @param dsId 数据源ID(为空取系统默认数据源id) * @param name 对象名(为空返回null) * @return MdDbTable * @author Kevice * @throws CacheException * @time 2012-12-14 下午4:54:03 * @since 1.0.0 */ public static MdRdbTable getRelationalObject(String dsId, String name) { if (StringTool.isBlank(name)) { return null; } if (StringTool.isBlank(dsId)) { dsId = JoyProperties.DB_DATASOURCEID; } RdbConnection connection = new RdbConnection(dsId); try { return getRelationalObject(connection, name); } finally { JdbcTool.closeConnection(connection.getConnection()); } }
/** * 获取主键 * * @param dsId 数据源id * @param tableName 表名 * @return 主键对象 * @author Kevice * @time 2012-12-18 下午3:37:42 * @since 1.0.0 */ public static MdRdbPrimaryKey getPrimaryKey(String dsId, String tableName) { if (StringTool.isBlank(tableName)) { return null; } if (StringTool.isBlank(dsId)) { dsId = JoyProperties.DB_DATASOURCEID; } RdbConnection connection = new RdbConnection(dsId); try { return getPrimaryKey(connection, tableName); } finally { JdbcTool.closeConnection(connection.getConnection()); } }
/** * 根据数据源ID、对象类型,从缓存中取得对应表的元数据信息 * * @param dsId 数据源ID(为空取系统默认数据源id) * @param objTypes 对象类型数组 * @return Map<String, MdRdbTable> * @throws CacheException * @since 1.0.0 * @author Kevice * @time 2012-12-14 下午5:26:01 */ public static Map<String, MdRdbTable> getRelationalObjects( String dsId, RdbObjectType... objTypes) { if (StringTool.isBlank(dsId)) { dsId = JoyProperties.DB_DATASOURCEID; } RdbConnection connection = new RdbConnection(dsId); try { return getRelationalObjects(connection, objTypes); } finally { JdbcTool.closeConnection(connection.getConnection()); } }
/** * 返回指定数据源id的数据库元数据信息 * * @param dsId 数据源id * @return 数据库元数据信息 * @since 1.0.0 * @author Kevice * @time 2013年11月16日 上午1:05:04 */ public static DbMetaData getDbMetaData(String dsId) { if (StringTool.isBlank(dsId)) { dsId = JoyProperties.DB_DATASOURCEID; } Connection conn = JdbcTool.getConnectionByDsId(dsId); DbMetaData dbMetaData = null; try { dbMetaData = getDbMetaData(conn); } finally { JdbcTool.closeConnection(conn); } return dbMetaData; }
private List<CodeRecord> searchCodes(String codeTableId, TSysCodeTable codeDic) { List<CodeRecord> codes = new ArrayList<CodeRecord>(); Connection conn = null; TSysDataSrc dataSrc = codeDic.getDataSrc(); String sqlPattern = "SELECT * FROM {0} WHERE {1} ORDER BY {2} ASC"; String codeFieldName = codeDic.getCodeField(); String parentCodeField = codeDic.getParentField(); String orderByField = StringTool.isBlank(codeDic.getOrderField()) ? codeFieldName : codeDic.getOrderField(); String codeCategory = null; String groupingCommentField = codeDic.getGroupingCommentField(); String where = "1=1"; String activeField = codeDic.getActiveField(); if (StringTool.isNotBlank(activeField)) { where += " AND " + activeField + " = '1'"; } String deletedField = codeDic.getDeletedField(); if (StringTool.isNotBlank(deletedField)) { where += " AND " + deletedField + " = '0'"; } if (codeDic.getId().equals(codeTableId) == false) { String groupingField = codeDic.getGroupingField(); where += " AND " + groupingField + " = '" + codeTableId + "'"; } else { codeCategory = codeDic.getTableComment(); } String sql = MessageFormat.format(sqlPattern, codeDic.getTableName(), where, orderByField); PreparedStatement statement = null; try { conn = JdbcTool.getConnection(dataSrc); statement = conn.prepareStatement(sql); String transFieldName = getTransFieldName(codeDic, conn); ResultSet rs = statement.executeQuery(); while (rs.next()) { String code = rs.getString(codeFieldName); String trans = rs.getString(transFieldName); String parentCode = StringTool.isBlank(parentCodeField) ? null : rs.getString(parentCodeField); if (codeCategory == null && StringTool.isNotBlank(groupingCommentField)) { codeCategory = rs.getString(groupingCommentField); } String order = rs.getString(orderByField); CodeRecord codeRecord = new CodeRecord(code, trans, parentCode, order, codeCategory); codes.add(codeRecord); } if (codes.isEmpty() == false || codeDic.getId().equals(codeTableId)) { logger.info("加载代码表#{0}数据共{1}条。", codeTableId, codes.size()); } } catch (SQLException e) { logger.error(e, "代码表#{0}数据加载失败!", codeTableId); } finally { if (statement != null) { try { statement.close(); } catch (SQLException ex) { logger.error(ex.getMessage(), ex); } } JdbcTool.closeConnection(conn); } return codes; }