Java tutorial
package org.dbmfs; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.sql.PreparedStatement; import java.sql.ParameterMetaData; import java.io.Serializable; import java.lang.reflect.Method; import com.jolbox.bonecp.BoneCP; import com.jolbox.bonecp.BoneCPConfig; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.MapListHandler; /** * DB?.<br> * TODO:MySQL????????Interface????<br> * * @author okuyamaoo * @license Apache License */ public class DatabaseAccessor { public static String tableNameSep = "___"; // ?? public static String primaryKeySep = "_#_"; // ?? public static String tableMetaInfoKey = "__DBMFS_TABLE_META_INFOMATION"; public static String tableMetaInfoPKeyKey = "__PKEY_MAP"; public static String tableMetaInfoColumnSep = "____"; public static String tableMetaInfoColumnMetaSep = ","; public static CacheFolder tableListCacheFolder = new CacheFolder(); public static CacheFolder tableExsistCacheFolder = new CacheFolder(); public static CacheFolder pKeyColumnNameCacheFolder = new CacheFolder(); public static CacheFolder allColumnMetaCacheFolder = new CacheFolder(); public static CacheFolder dataCacheFolder = new CacheFolder(); private static BoneCP connectionPool = null; private Connection injectConn = null; private Map<String, Integer> sqlTypeMap = null; /** * */ public DatabaseAccessor() { } /** * */ public DatabaseAccessor(Connection conn) { injectConn = conn; } protected void initSqlTypeMap() { sqlTypeMap = new HashMap(); } public static void initDatabaseAccessor() throws Exception { BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl(DatabaseFilesystem.databaseUrl); config.setUsername(DatabaseFilesystem.user); config.setPassword(DatabaseFilesystem.password); config.setMinConnectionsPerPartition(3); config.setMaxConnectionsPerPartition(8); config.setPartitionCount(1); config.setDefaultAutoCommit(true); config.setDefaultReadOnly(false); config.setDefaultTransactionIsolation("READ_COMMITTED"); connectionPool = new BoneCP(config); } public static void poolShutdown() { if (connectionPool != null) connectionPool.shutdown(); } /** * ??.<br> * * @return ??? */ public void createTable(DDLFolder folder, String tableName) throws Exception { try { QueryRunner qr = new QueryRunner(); int createRet = qr.update(injectConn, folder.getCreateSQL(tableName)); } catch (SQLException se) { se.printStackTrace(); throw se; } catch (Exception e) { e.printStackTrace(); throw e; } } /** * ??.<br> * * @return ??? */ public List<String> getTableList() throws Exception { if (tableListCacheFolder.containsKey("tablelist")) return (List<String>) tableListCacheFolder.get("tablelist"); List<String> tableNameList = new ArrayList(); try { String table = "%"; String types[] = { "TABLE", "VIEW", "SYSTEM TABLE" }; DatabaseMetaData dbmd = injectConn.getMetaData(); ResultSet rs = dbmd.getTables(null, null, table, types); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); tableNameList.add(tableName); } tableListCacheFolder.put("tablelist", tableNameList); rs.close(); } catch (Exception e) { e.printStackTrace(); throw e; } return tableNameList; } /** * ???????.<br> * * @param targetTableName ?? * @return */ public boolean exsistTable(String targetTableName) throws Exception { if (tableExsistCacheFolder.containsKey(targetTableName)) return true; boolean ret = false; try { String table = "%"; String types[] = { "TABLE", "VIEW", "SYSTEM TABLE" }; DatabaseMetaData dbmd = injectConn.getMetaData(); ResultSet rs = dbmd.getTables(null, null, table, types); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); if (targetTableName.equals(tableName)) { ret = true; tableExsistCacheFolder.put(targetTableName, "true"); break; } } rs.close(); } catch (Exception e) { e.printStackTrace(); throw e; } return ret; } /** * ???????????.<br> * * @param targetTableName ?? * @param pKeyConcatStr (?) * @retrun */ public boolean exsistData(String targetTableName, String pKeyConcatStr) throws Exception { boolean ret = false; try { // ? List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(targetTableName); Map<String, Map<String, Object>> allColumnMeta = getAllColumnMeta(targetTableName, true); if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) return false; // ? pKeyConcatStr = pKeyConcatStr.replace(".json", ""); String[] keyStrSplit = pKeyConcatStr.split(primaryKeySep); if (keyStrSplit.length != primaryKeyColumnNames.size()) return false; // ?? StringBuilder queryBuf = new StringBuilder(); queryBuf.append("select count(*) as cnt from "); queryBuf.append(targetTableName); queryBuf.append(" where "); // ()? Object[] params = new Object[primaryKeyColumnNames.size()]; String whereSep = ""; for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { Map<String, Object> meta = allColumnMeta.get(primaryKeyColumnNames.get(idx)); params[idx] = DbmfsUtil.deserializeType(keyStrSplit[idx], (String) meta.get("javaTypeName")); queryBuf.append(whereSep); queryBuf.append(primaryKeyColumnNames.get(idx)); queryBuf.append(" = ? "); whereSep = " and "; } ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // //System.out.println(queryBuf.toString()); //System.out.println(params[0]); List<Map<String, Object>> queryResult = (List<Map<String, Object>>) qr.query(injectConn, queryBuf.toString(), resultSetHandler, params); Map countRet = queryResult.get(0); Long count = (Long) countRet.get("cnt"); if (count.longValue() == 1) ret = true; } catch (Exception e) { e.printStackTrace(); throw e; } return ret; } /** * ??????Map????.<br> * ??????null?<br> * * @param targetTableName ?? * @param pKeyConcatStr (?) * @return ? */ public List<Map<String, Object>> getDataList(String targetTableName, String pKeyConcatStr) throws Exception { List<Map<String, Object>> queryResult = null; try { // ? StringBuilder cacheKeyBuf = new StringBuilder(50); cacheKeyBuf.append(targetTableName).append(tableNameSep).append(pKeyConcatStr); Object cacheDataMap = dataCacheFolder.get(cacheKeyBuf.toString()); if (cacheDataMap != null) { if (cacheDataMap instanceof List) { return (List<Map<String, Object>>) cacheDataMap; } else if (cacheDataMap instanceof Map) { List<Map<String, Object>> cacheList = new ArrayList(); cacheList.add((Map<String, Object>) cacheDataMap); return cacheList; } } // ? List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(targetTableName); Map<String, Map<String, Object>> allColumnMeta = getAllColumnMeta(targetTableName, true); if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) return null; // ? String[] keyStrSplit = pKeyConcatStr.split(primaryKeySep); // ?????????????? if (primaryKeyColumnNames.size() != keyStrSplit.length) return null; // ?? StringBuilder queryBuf = new StringBuilder(); queryBuf.append("select * from "); queryBuf.append(targetTableName); queryBuf.append(" where "); // ()? Object[] params = new Object[primaryKeyColumnNames.size()]; String whereSep = ""; for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { Map<String, Object> meta = allColumnMeta.get(primaryKeyColumnNames.get(idx)); params[idx] = DbmfsUtil.deserializeType(keyStrSplit[idx], (String) meta.get("javaTypeName")); queryBuf.append(whereSep); queryBuf.append(primaryKeyColumnNames.get(idx)); queryBuf.append(" = ? "); whereSep = " and "; } ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // queryResult = (List<Map<String, Object>>) qr.query(injectConn, queryBuf.toString(), resultSetHandler, params); if (queryResult == null || queryResult.size() < 1) return null; // ????? String metaSerializeString = serializeMetaInfomation(allColumnMeta); for (Map resultData : queryResult) { resultData.put(tableMetaInfoKey, metaSerializeString); } dataCacheFolder.put(cacheKeyBuf.toString(), queryResult); } catch (Exception e) { e.printStackTrace(); throw e; } return queryResult; } /** * ??Map????.<br> * ??????null?<br> * * @param targetTableName ?? * @param pKeyConcatStr (?) * @return ? */ public List<Map<String, Object>> getDataList(String query, List<String> primaryKeyColumnNames, String pKeyConcatStr) throws Exception { List<Map<String, Object>> queryResult = null; try { // ? String[] keyStrSplit = pKeyConcatStr.split(primaryKeySep); if (keyStrSplit.length != primaryKeyColumnNames.size()) return null; // ?? StringBuilder queryBuf = new StringBuilder(); queryBuf.append("select * from ("); queryBuf.append(query); queryBuf.append(") t1 where "); // ()? Object[] params = new Object[primaryKeyColumnNames.size()]; String whereSep = ""; // ???bindquery?????????String?? // (TODONo1)??select???? for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { params[idx] = keyStrSplit[idx]; queryBuf.append(whereSep); queryBuf.append("t1."); queryBuf.append(primaryKeyColumnNames.get(idx)); queryBuf.append(" = ? "); whereSep = " and "; } ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // queryResult = (List<Map<String, Object>>) qr.query(injectConn, queryBuf.toString(), resultSetHandler, params); if (queryResult == null || queryResult.size() < 1) return null; for (Map resultData : queryResult) { resultData.put(tableMetaInfoKey, ""); } } catch (Exception e) { e.printStackTrace(); throw e; } return queryResult; } // ??????? private List<String> getPrimaryKeyColumnNames(String tableName) throws Exception { if (pKeyColumnNameCacheFolder.containsKey(tableName)) return (List<String>) pKeyColumnNameCacheFolder.get(tableName); List<String> primaryKeyColumnNames = null; try { primaryKeyColumnNames = new ArrayList(); DatabaseMetaData dbmd = injectConn.getMetaData(); // ? ResultSet rs = dbmd.getPrimaryKeys(null, null, tableName); while (rs.next()) { primaryKeyColumnNames.add(rs.getString("COLUMN_NAME")); } Collections.sort(primaryKeyColumnNames); pKeyColumnNameCacheFolder.put(tableName, primaryKeyColumnNames); rs.close(); } catch (Exception e) { e.printStackTrace(); throw e; } return primaryKeyColumnNames; } public Map<String, Map<String, Object>> getAllColumnMeta(String tableName, boolean includePKeyMeta) throws Exception { Map<String, Map<String, Object>> columnMeta = getAllColumnMeta(tableName); if (!includePKeyMeta) { if (columnMeta != null) { Map<String, Map<String, Object>> convertMap = new LinkedHashMap(); for (Map.Entry<String, Map<String, Object>> entry : columnMeta.entrySet()) { String key = entry.getKey(); Map<String, Object> val = entry.getValue(); if (!key.equals(DatabaseAccessor.tableMetaInfoPKeyKey)) { convertMap.put(key, val); } } return convertMap; } } return columnMeta; } private Map<String, Map<String, Object>> getAllColumnMeta(String tableName) throws Exception { Map<String, Map<String, Object>> allColumnMeta = null; try { if (allColumnMetaCacheFolder.containsKey(tableName)) return (Map<String, Map<String, Object>>) allColumnMetaCacheFolder.get(tableName); allColumnMeta = new LinkedHashMap(); DatabaseMetaData dbmd = injectConn.getMetaData(); // ? ResultSet rs = dbmd.getColumns(null, null, tableName, "%"); while (rs.next()) { Map<String, Object> columMeta = new LinkedHashMap(); columMeta.put("name", rs.getString("COLUMN_NAME")); columMeta.put("type", rs.getInt("DATA_TYPE")); columMeta.put("type_name", rs.getString("TYPE_NAME")); columMeta.put("column_size", rs.getInt("COLUMN_SIZE")); columMeta.put("null_type", rs.getString("IS_NULLABLE")); columMeta.put("seq_type", rs.getString("IS_AUTOINCREMENT")); columMeta.put("javaTypeName", getJavaTypeName(rs.getString("TYPE_NAME"))); allColumnMeta.put((String) columMeta.get("name"), columMeta); } Map<String, Object> pKeyNameMap = new LinkedHashMap(); try { List<String> pkeyList = getPrimaryKeyColumnNames(tableName); int no = 1; for (String pkeyName : pkeyList) { pKeyNameMap.put(pkeyName, no); no++; } } catch (SQLException se) { } allColumnMeta.put(DatabaseAccessor.tableMetaInfoPKeyKey, pKeyNameMap); allColumnMetaCacheFolder.put(tableName, allColumnMeta); rs.close(); } catch (Exception e) { e.printStackTrace(); throw e; } return allColumnMeta; } /** * ????????.<br> * * @param tableName ?? * @return ?????? */ public List<String> getRecordKeyList(String tableName, int offset, int limit) throws Exception { List<String> resultList = new ArrayList(); try { // ? List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(tableName); // ???????? if (primaryKeyColumnNames.size() == 0) return resultList; // ??select??????? // TODO:??????????? // SQL?? String query = createAllColumnQuery(tableName, offset, limit); // String query = createPrimaryKeyQuery(tableName, primaryKeyColumnNames); ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // List<Map<String, Object>> queryResult = (List<Map<String, Object>>) qr.query(injectConn, query, resultSetHandler); // ????? Map<String, Map<String, Object>> allColumnMeta = getAllColumnMeta(tableName, true); String metaSerializeString = serializeMetaInfomation(allColumnMeta); // ?????? for (int idx = 0; idx < queryResult.size(); idx++) { Map data = queryResult.get(idx); StringBuilder queryDataStrBuf = new StringBuilder(40); String pKeyStrSep = ""; for (int pIdx = 0; pIdx < primaryKeyColumnNames.size(); pIdx++) { queryDataStrBuf.append(pKeyStrSep); queryDataStrBuf.append(data.get(primaryKeyColumnNames.get(pIdx))); pKeyStrSep = DatabaseAccessor.primaryKeySep; } data.put(tableMetaInfoKey, metaSerializeString); List cachePutList = new ArrayList(); cachePutList.add(data); dataCacheFolder.put(tableName + tableNameSep + queryDataStrBuf.toString(), cachePutList); resultList.add(queryDataStrBuf.toString()); } } catch (Exception e) { e.printStackTrace(); throw e; } return resultList; } /** * ?????????????.<br> * * @param query (Select?) * @param primaryKeyColumnNames ????? * @return ?????? */ public List<String> getRecordKeyList(String query, List<String> primaryKeyColumnNames, int offset, int limit) throws Exception { List<String> resultList = new ArrayList(); try { ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // ???limit offset String executeQuery = createAllColumnQuery("(" + query + ")", offset, limit); // List<Map<String, Object>> queryResult = (List<Map<String, Object>>) qr.query(injectConn, query, resultSetHandler); // ?????? for (int idx = 0; idx < queryResult.size(); idx++) { Map data = queryResult.get(idx); StringBuilder queryDataStrBuf = new StringBuilder(40); String pKeyStrSep = ""; for (int pIdx = 0; pIdx < primaryKeyColumnNames.size(); pIdx++) { queryDataStrBuf.append(pKeyStrSep); queryDataStrBuf.append(data.get(primaryKeyColumnNames.get(pIdx))); // TODO???data.get????queryprimaryKeyColumnNames.get(pIdx)??? // ?(TODONo1)?? pKeyStrSep = DatabaseAccessor.primaryKeySep; } /* List cachePutList = new ArrayList(); cachePutList.add(data); dataCacheFolder.put(tableName + tableNameSep + queryDataStrBuf.toString(), cachePutList);*/ resultList.add(queryDataStrBuf.toString()); } } catch (Exception e) { e.printStackTrace(); throw e; } return resultList; } public boolean saveData(String tableName, String pKeyConcatStr, Map<String, Object> dataObject) throws Exception { try { // ?? if (exsistData(tableName, pKeyConcatStr)) { // ?? deleteData(tableName, pKeyConcatStr); return insertData(tableName, dataObject); } else { // ???? return insertData(tableName, dataObject); } } catch (Exception e) { throw e; } } public boolean insertData(String tableName, Map<String, Object> dataObject) throws Exception { try { Map<String, Map<String, Object>> allColumnMeta = getAllColumnMeta(tableName, false); // QueryParameter List queryParams = new ArrayList(); List<Integer> queryParamTypes = new ArrayList(); // ?? StringBuilder queryBuf = new StringBuilder(); StringBuilder valuesBuf = new StringBuilder(); queryBuf.append("insert into "); queryBuf.append(tableName); queryBuf.append(" ( "); valuesBuf.append(" values("); String sep = ""; for (Map.Entry<String, Map<String, Object>> ent : allColumnMeta.entrySet()) { valuesBuf.append(sep); queryBuf.append(sep); String columnName = ent.getKey(); Map columnMeta = ent.getValue(); queryBuf.append(columnName); Object columnData = dataObject.get(columnName); if (columnData != null) { queryParams.add(columnData); queryParamTypes.add((Integer) columnMeta.get("type")); valuesBuf.append("?"); } else { valuesBuf.append("null"); } sep = ","; } queryBuf.append(" ) "); valuesBuf.append(" ) "); queryBuf.append(valuesBuf.toString()); QueryRunner qr = new QueryRunner(); int insertCount = qr.update(injectConn, queryBuf.toString(), queryParams.toArray(new Object[0])); } catch (SQLException se) { se.printStackTrace(); throw se; } catch (Exception e) { e.printStackTrace(); throw e; } return true; } /** * DB??Update? * */ public boolean updateData(String tableName, Map<String, Object> dataObject) throws Exception { try { // ???? List<String> pKeyNameList = getPrimaryKeyColumnNames(tableName); Map<String, Object> pKeyObjectMap = new LinkedHashMap(); // ???Object??? for (String pKeyName : pKeyNameList) { Object pKeyValue = dataObject.remove(pKeyName); if (pKeyValue != null) { pKeyObjectMap.put(pKeyName, pKeyValue); } } // ?? if (pKeyObjectMap.size() == 0) return false; // QueryParameter List setParams = new ArrayList(); List whereParams = new ArrayList(); // ?? StringBuilder setBuf = new StringBuilder(); StringBuilder whereBuf = new StringBuilder(); setBuf.append("update "); setBuf.append(tableName); setBuf.append(" set "); whereBuf.append(" where "); String setSep = ""; String whereSep = ""; // Update?Set? for (Map.Entry<String, Object> ent : dataObject.entrySet()) { String columnName = ent.getKey(); Object columnValue = ent.getValue(); setBuf.append(setSep); // Set?? setBuf.append(columnName); setBuf.append(" = ? "); setParams.add(columnValue); setSep = ","; } // Update?where? for (Map.Entry<String, Object> ent : pKeyObjectMap.entrySet()) { String columnName = ent.getKey(); Object columnValue = ent.getValue(); // where?? whereBuf.append(whereSep); if (columnValue == null) { whereBuf.append(columnName); whereBuf.append(" is null "); setParams.add(columnValue); } else { whereBuf.append(columnName); whereBuf.append(" = ? "); whereParams.add(columnValue); } whereSep = " and "; } // ?? setBuf.append(whereBuf.toString()); // ?? setParams.addAll(whereParams); QueryRunner qr = new QueryRunner(); int updateCount = qr.update(injectConn, setBuf.toString(), setParams.toArray(new Object[0])); if (updateCount != 1) { return false; } else { removeDataCache(tableName, pKeyObjectMap); return true; } } catch (Exception e) { e.printStackTrace(); throw e; } } /** * DB??Delete? * */ public boolean deleteData(String tableName, String pKeyConcatStr) throws Exception { boolean ret = false; try { // ? List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(tableName); if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) return false; // ? String[] keyStrSplit = pKeyConcatStr.split(primaryKeySep); if (keyStrSplit.length != primaryKeyColumnNames.size()) return false; // ?? StringBuilder queryBuf = new StringBuilder(); queryBuf.append("delete from "); queryBuf.append(tableName); queryBuf.append(" where "); // ()? Object[] params = new Object[primaryKeyColumnNames.size()]; String whereSep = ""; for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { params[idx] = keyStrSplit[idx]; queryBuf.append(whereSep); queryBuf.append(primaryKeyColumnNames.get(idx)); queryBuf.append(" = ? "); whereSep = " and "; } QueryRunner qr = new QueryRunner(); int updateCount = qr.update(injectConn, queryBuf.toString(), params); if (updateCount != 1) { return false; } else { // removeDataCache(tableName, pKeyConcatStr); return true; } } catch (Exception e) { e.printStackTrace(); throw e; } } /** * ?select?????? * */ private String createPrimaryKeyQuery(String tableName, List<String> primaryKeyColumnNames) { StringBuilder queryBuf = new StringBuilder(); String sep = ""; queryBuf.append("select "); for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { queryBuf.append(sep); queryBuf.append(primaryKeyColumnNames.get(idx)); sep = ","; } queryBuf.append(" from "); queryBuf.append(tableName); return queryBuf.toString(); } /** * ?select?????? * */ private String createAllColumnQuery(String targetDataQuery, int offset, int limit) { StringBuilder queryBuf = new StringBuilder(); String sep = ""; queryBuf.append("select *"); queryBuf.append(" from "); queryBuf.append(targetDataQuery); queryBuf.append(" limit "); queryBuf.append(limit); queryBuf.append(" offset "); queryBuf.append(offset); return queryBuf.toString(); } public void removeDataCache(String tableName, String pKeyConcatStr) { StringBuilder cacheKeyBuf = new StringBuilder(30); cacheKeyBuf.append(tableName); cacheKeyBuf.append(tableNameSep); cacheKeyBuf.append(pKeyConcatStr); dataCacheFolder.remove(cacheKeyBuf.toString()); } public void removeDataCache(String tableName, Map<String, Object> pKeyDataMap) { StringBuilder cacheKeyBuf = new StringBuilder(30); cacheKeyBuf.append(tableName); cacheKeyBuf.append(tableNameSep); String sep = ""; // ?? for (Map.Entry<String, Object> ent : pKeyDataMap.entrySet()) { Object columnValue = ent.getValue(); cacheKeyBuf.append(sep); cacheKeyBuf.append(columnValue); sep = primaryKeySep; } dataCacheFolder.remove(cacheKeyBuf.toString()); } public static Connection getOriginalConnection() throws Exception { Connection conn = connectionPool.getConnection(); conn.setAutoCommit(false); return conn; } public static void returnOriginalConnection(Connection conn) throws Exception { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } protected String serializeMetaInfomation(Map<String, Map<String, Object>> allColumnMeta) { StringBuilder strBuf = new StringBuilder(); String sep = ""; for (Map.Entry<String, Map<String, Object>> ent : allColumnMeta.entrySet()) { String mapKey = ent.getKey(); if (!mapKey.equals(DatabaseAccessor.tableMetaInfoPKeyKey)) { Map<String, Object> columnMeta = ent.getValue(); strBuf.append(sep); strBuf.append("column_name:"); strBuf.append(mapKey); strBuf.append(tableMetaInfoColumnMetaSep); strBuf.append("type_name:"); strBuf.append((String) columnMeta.get("type_name")); strBuf.append(tableMetaInfoColumnMetaSep); strBuf.append("column_size:"); strBuf.append((Integer) columnMeta.get("column_size")); strBuf.append(tableMetaInfoColumnMetaSep); strBuf.append("null_type:"); strBuf.append((String) columnMeta.get("null_type")); strBuf.append(tableMetaInfoColumnMetaSep); strBuf.append("seq_type:"); strBuf.append((String) columnMeta.get("seq_type")); strBuf.append(tableMetaInfoColumnMetaSep); strBuf.append("javaTypeName:"); strBuf.append((String) columnMeta.get("javaTypeName")); sep = tableMetaInfoColumnSep; } } // PKey?????? StringBuilder pKeyNames = new StringBuilder(); String pKeyNamesSep = ""; Map<String, Object> pkeyMap = (Map<String, Object>) allColumnMeta .get(DatabaseAccessor.tableMetaInfoPKeyKey); for (String pkeyName : pkeyMap.keySet()) { pKeyNames.append(pKeyNamesSep); pKeyNames.append(pkeyName); pKeyNamesSep = ","; } strBuf.append(sep); strBuf.append("pkey_columns_name:"); strBuf.append(pKeyNames.toString()); return strBuf.toString(); } // MySQL?DB????Java???? protected String getJavaTypeName(String dbTypeName) { if (dbTypeName.equals("BIT") || dbTypeName.toLowerCase().equals("bit")) { return "java.lang.Boolean"; } else if (dbTypeName.equals("BOOL") || dbTypeName.toLowerCase().equals("bool")) { return "java.lang.Boolean"; } else if (dbTypeName.equals("BOOLEAN") || dbTypeName.toLowerCase().equals("boolean")) { return "java.lang.Boolean"; } else if (dbTypeName.equals("TINYINT") || dbTypeName.toLowerCase().equals("tinyint")) { return "java.lang.Integer"; } else if (dbTypeName.indexOf("SMALLINT") != -1 || dbTypeName.toLowerCase().indexOf("smallint") != -1) { return "java.lang.Integer"; } else if (dbTypeName.equals("MEDIUMINT") || dbTypeName.toLowerCase().equals("numeric")) { return "java.lang.Integer"; } else if (dbTypeName.equals("INT") || dbTypeName.toLowerCase().equals("int4") || dbTypeName.toLowerCase().equals("int") || dbTypeName.toLowerCase().equals("int2")) { return "java.lang.Integer"; } else if (dbTypeName.equals("INTEGER") || dbTypeName.toLowerCase().equals("integer")) { return "java.lang.Integer"; } else if (dbTypeName.equals("BIGINT") || dbTypeName.toLowerCase().equals("int8")) { return "java.lang.Long"; } else if (dbTypeName.equals("FLOAT") || dbTypeName.toLowerCase().equals("float4")) { return "java.lang.Float"; } else if (dbTypeName.equals("DOUBLE") || dbTypeName.toLowerCase().equals("float8")) { return "java.lang.Double"; } else if (dbTypeName.equals("DECIMAL") || dbTypeName.toLowerCase().equals("decimal")) { return "java.math.BigDecimal"; } else if (dbTypeName.equals("DATE") || dbTypeName.toLowerCase().equals("date")) { return "java.sql.Date"; } else if (dbTypeName.equals("DATETIME") || dbTypeName.toLowerCase().equals("datetime")) { return "java.sql.Timestamp"; } else if (dbTypeName.equals("TIMESTAMP") || dbTypeName.toLowerCase().equals("timestamp") || dbTypeName.toLowerCase().equals("timestamptz")) { return "java.sql.Timestamp"; } else if (dbTypeName.equals("TIME") || dbTypeName.toLowerCase().equals("time") || dbTypeName.toLowerCase().equals("timetz")) { return "java.sql.Time"; } else if (dbTypeName.equals("CHAR") || dbTypeName.toLowerCase().equals("char")) { return "java.lang.String"; } else if (dbTypeName.equals("VARCHAR") || dbTypeName.toLowerCase().equals("varchar")) { return "java.lang.String"; } else if (dbTypeName.equals("BINARY")) { return "byte[]"; } else if (dbTypeName.equals("VARBINARY")) { return "byte[]"; } else if (dbTypeName.equals("TINYBLOB") || dbTypeName.equals("bytea")) { return "byte[]"; } else if (dbTypeName.equals("BLOB")) { return "byte[]"; } else if (dbTypeName.equals("MEDIUMBLOB")) { return "byte[]"; } else if (dbTypeName.equals("LONGBLOB")) { return "byte[]"; } return null; } }