diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index 6f92ea224..72df537ac 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -147,7 +147,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return false; } - protected List prepareInsertEntityStatements(SourceConnection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { + protected List prepareInsertEntityStatements(JdbcConnection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; final List prestmts = new ArrayList<>(); for (Map.Entry> en : prepareInfos.entrySet()) { @@ -164,7 +164,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmts; } - protected PreparedStatement prepareInsertEntityStatement(SourceConnection conn, String sql, EntityInfo info, T... entitys) throws SQLException { + protected PreparedStatement prepareInsertEntityStatement(JdbcConnection conn, String sql, EntityInfo info, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; final PreparedStatement prestmt = info.isAutoGenerated() ? conn.prepareUpdateStatement(sql, Statement.RETURN_GENERATED_KEYS) @@ -176,7 +176,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmt; } - protected List prepareUpdateEntityStatements(SourceConnection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { + protected List prepareUpdateEntityStatements(JdbcConnection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; final List prestmts = new ArrayList<>(); @@ -193,7 +193,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmts; } - protected PreparedStatement prepareUpdateEntityStatement(SourceConnection conn, String prepareSQL, EntityInfo info, T... entitys) throws SQLException { + protected PreparedStatement prepareUpdateEntityStatement(JdbcConnection conn, String prepareSQL, EntityInfo info, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; final PreparedStatement prestmt = conn.prepareUpdateStatement(prepareSQL); @@ -205,7 +205,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmt; } - protected int bindStatementParameters(SourceConnection conn, PreparedStatement prestmt, EntityInfo info, Attribute[] attrs, T entity) throws SQLException { + protected int bindStatementParameters(JdbcConnection conn, PreparedStatement prestmt, EntityInfo info, Attribute[] attrs, T entity) throws SQLException { int i = 0; for (Attribute attr : attrs) { Object val = getEntityAttrValue(info, attr, entity); @@ -234,7 +234,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return 0; } int c = 0; - SourceConnection conn = null; + JdbcConnection conn = null; List stmtsRef = new ArrayList<>(); try { conn = writePool.pollTransConnection(); @@ -308,9 +308,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.rollback(stmtsRef); throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerTransConnection(conn); - } + writePool.offerTransConnection(conn); } } @@ -326,7 +324,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected int insertDB(EntityInfo info, T... entitys) { - SourceConnection conn = null; + JdbcConnection conn = null; List stmtsRef = new ArrayList<>(); try { conn = writePool.pollConnection(); @@ -337,13 +335,11 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (SQLException e) { throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } - private int insertDBStatement(List stmtsRef, final SourceConnection conn, final EntityInfo info, T... entitys) throws SQLException { + private int insertDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, T... entitys) throws SQLException { final long s = System.currentTimeMillis(); int c = 0; String presql = null; @@ -657,7 +653,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected int deleteDB(EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, String... sqls) { - SourceConnection conn = null; + JdbcConnection conn = null; List stmtsRef = new ArrayList<>(); try { conn = writePool.pollConnection(); @@ -669,13 +665,11 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.rollback(stmtsRef); throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } - private int deleteDBStatement(List stmtsRef, final SourceConnection conn, final EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, String... sqls) throws SQLException { + private int deleteDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, String... sqls) throws SQLException { final long s = System.currentTimeMillis(); Statement stmt = null; try { @@ -774,7 +768,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected int clearTableDB(EntityInfo info, String[] tables, FilterNode node, String... sqls) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); Statement stmt = null; try { @@ -847,9 +841,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } @@ -865,7 +857,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected int createTableDB(EntityInfo info, String copyTableSql, Serializable pk, String... sqls) { - SourceConnection conn = null; + JdbcConnection conn = null; Statement stmt = null; final long s = System.currentTimeMillis(); try { @@ -970,15 +962,13 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (SQLException e) { throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } @Override protected int dropTableDB(EntityInfo info, String[] tables, FilterNode node, String... sqls) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); Statement stmt = null; try { @@ -1048,9 +1038,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } @@ -1061,7 +1049,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected int updateEntityDB(EntityInfo info, T... entitys) { - SourceConnection conn = null; + JdbcConnection conn = null; List stmtsRef = new ArrayList<>(); try { conn = writePool.pollConnection(); @@ -1073,13 +1061,11 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.rollback(stmtsRef); throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } - private int updateEntityDBStatement(List stmtsRef, final SourceConnection conn, final EntityInfo info, T... entitys) throws SQLException { + private int updateEntityDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, T... entitys) throws SQLException { final long s = System.currentTimeMillis(); String presql = null; PreparedStatement prestmt = null; @@ -1242,7 +1228,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected int updateColumnDB(EntityInfo info, Flipper flipper, UpdateSqlInfo sql) { - SourceConnection conn = null; + JdbcConnection conn = null; List stmtsRef = new ArrayList<>(); try { conn = writePool.pollConnection(); @@ -1254,13 +1240,11 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.rollback(stmtsRef); throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } - private int updateColumnDBStatement(List stmtsRef, final SourceConnection conn, final EntityInfo info, Flipper flipper, UpdateSqlInfo sql) throws SQLException { //String sql, boolean prepared, Object... blobs) { + private int updateColumnDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, Flipper flipper, UpdateSqlInfo sql) throws SQLException { //String sql, boolean prepared, Object... blobs) { final long s = System.currentTimeMillis(); int c = -1; String firstTable = null; @@ -1418,7 +1402,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected Map getNumberMapDB(EntityInfo info, String[] tables, String sql, FilterNode node, FilterFuncColumn... columns) { - SourceConnection conn = null; + JdbcConnection conn = null; final Map map = new HashMap<>(); final long s = System.currentTimeMillis(); Statement stmt = null; @@ -1509,9 +1493,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -1522,7 +1504,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected Number getNumberResultDB(EntityInfo info, String[] tables, String sql, FilterFunc func, Number defVal, String column, FilterNode node) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); Statement stmt = null; try { @@ -1599,9 +1581,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -1612,7 +1592,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected Map queryColumnMapDB(EntityInfo info, String[] tables, String sql, String keyColumn, FilterFunc func, String funcColumn, FilterNode node) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); Map rs = new LinkedHashMap<>(); Statement stmt = null; @@ -1687,9 +1667,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -1700,7 +1678,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected Map queryColumnMapDB(EntityInfo info, String[] tables, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { - SourceConnection conn = null; + JdbcConnection conn = null; Map rs = new LinkedHashMap<>(); final long s = System.currentTimeMillis(); Statement stmt = null; @@ -1790,9 +1768,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -1815,7 +1791,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } protected T findDB(EntityInfo info, Serializable pk) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); PreparedStatement prestmt = null; try { @@ -1824,9 +1800,9 @@ public class DataJdbcSource extends AbstractDataSqlSource { String prepareSQL = info.getFindQuestionPrepareSQL(pk); prestmt = conn.prepareQueryStatement(prepareSQL); prestmt.setObject(1, pk); - final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); - T rs = set.next() ? info.getBuilder().getFullEntityValue(set) : null; - set.close(); + ResultSet rr = prestmt.executeQuery(); + T rs = rr.next() ? info.getBuilder().getFullEntityValue(createDataResultSet(info, rr)) : null; + rr.close(); conn.offerQueryStatement(prestmt); slowLog(s, prepareSQL); return rs; @@ -1836,9 +1812,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -1849,7 +1823,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected T findDB(EntityInfo info, String[] tables, String sql, boolean onlypk, SelectColumn selects, Serializable pk, FilterNode node) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); PreparedStatement prestmt = null; try { @@ -1857,9 +1831,9 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.setAutoCommit(true); prestmt = conn.prepareQueryStatement(sql); prestmt.setFetchSize(1); - final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); - T rs = set.next() ? selects == null ? info.getBuilder().getFullEntityValue(set) : info.getBuilder().getEntityValue(selects, set) : null; - set.close(); + ResultSet rr = prestmt.executeQuery(); + T rs = rr.next() ? info.getBuilder().getEntityValue(selects, createDataResultSet(info, rr)) : null; + rr.close(); conn.offerQueryStatement(prestmt); slowLog(s, sql); return rs; @@ -1903,9 +1877,9 @@ public class DataJdbcSource extends AbstractDataSqlSource { } prestmt = conn.prepareQueryStatement(sql); prestmt.setFetchSize(1); - final DataResultSet set = createDataResultSet(info, prestmt.executeQuery()); - T rs = set.next() ? selects == null ? info.getBuilder().getFullEntityValue(set) : info.getBuilder().getEntityValue(selects, set) : null; - set.close(); + ResultSet rr = prestmt.executeQuery(); + T rs = rr.next() ? info.getBuilder().getEntityValue(selects, createDataResultSet(info, rr)) : null; + rr.close(); conn.offerQueryStatement(prestmt); slowLog(s, sql); return rs; @@ -1916,9 +1890,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -1929,7 +1901,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected Serializable findColumnDB(EntityInfo info, String[] tables, String sql, boolean onlypk, String column, Serializable defValue, Serializable pk, FilterNode node) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); PreparedStatement prestmt = null; final Attribute attr = info.getAttribute(column); @@ -1996,16 +1968,16 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.offerQueryStatement(prestmt); slowLog(s, sql); return val == null ? defValue : val; - } catch (SQLException se) { + } catch (Exception se) { throw new SourceException(se); } } } throw new SourceException(e); + } catch (Exception ex) { + throw new SourceException(ex); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -2016,7 +1988,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override protected boolean existsDB(EntityInfo info, String[] tables, String sql, boolean onlypk, Serializable pk, FilterNode node) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); PreparedStatement prestmt = null; try { @@ -2087,9 +2059,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -2098,12 +2068,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { final EntityInfo info = loadEntityInfo(clazz); Serializable[] ids = pks.toArray(serialArrayFunc); if (info.getTableStrategy() == null) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); + final List list = new ArrayList(); try { conn = readPool.pollConnection(); conn.setAutoCommit(true); - final List list = new ArrayList(); try { String prepareSQL = info.getFindQuestionPrepareSQL(ids[0]); PreparedStatement prestmt = conn.prepareQueryStatement(prepareSQL); @@ -2133,9 +2103,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (Exception e) { throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } else { return queryList(info.getType(), null, null, FilterNodes.in(info.getPrimarySQLColumn(), ids)); @@ -2153,7 +2121,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } protected Sheet querySheetFullListDB(EntityInfo info) { - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); try { conn = readPool.pollConnection(); @@ -2182,9 +2150,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (Exception e) { throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -2193,7 +2159,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (!needTotal && !distinct && selects == null && flipper == null && node == null && info.getTableStrategy() == null) { return querySheetFullListDB(info); } - SourceConnection conn = null; + JdbcConnection conn = null; final long s = System.currentTimeMillis(); final SelectColumn sels = selects; final Map joinTabalis = node == null ? null : node.getJoinTabalis(); @@ -2258,14 +2224,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (Exception e) { throw new SourceException(e); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } private Sheet executeQuerySheet(EntityInfo info, boolean needTotal, Flipper flipper, SelectColumn sels, - long s, SourceConnection conn, boolean mysqlOrPgsql, String listSql, String countSql) throws SQLException { + long s, JdbcConnection conn, boolean mysqlOrPgsql, String listSql, String countSql) throws SQLException { final List list = new ArrayList(); if (mysqlOrPgsql) { //sql可以带limit、offset ResultSet set; @@ -2383,7 +2347,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return new String[]{listSql, countSql}; } - protected List checkNotExistTablesNoThrows(SourceConnection conn, String[] tables) { + protected List checkNotExistTablesNoThrows(JdbcConnection conn, String[] tables) { try { return checkNotExistTables(conn, tables); //, firstNotExistTable } catch (SQLException e) { @@ -2391,7 +2355,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - protected List checkNotExistTables(SourceConnection conn, String[] tables) throws SQLException { //, String firstNotExistTable + protected List checkNotExistTables(JdbcConnection conn, String[] tables) throws SQLException { //, String firstNotExistTable // 数据库不一定要按批量提交的SQL顺序执行, 所以第一个不存在的表不一定在tables的第一位, // 比如 DELETE FROM table1; DELETE FROM table2; 如果table1、table2都不存在,SQL可能会抛出table2不存在的异常 // List maybeNoTables = new ArrayList<>(); @@ -2444,7 +2408,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } final long s = System.currentTimeMillis(); Statement stmt = null; - SourceConnection conn = writePool.pollConnection(); + JdbcConnection conn = writePool.pollConnection(); try { conn.setAutoCommit(false); stmt = conn.createUpdateStatement(); @@ -2461,9 +2425,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.rollback(stmt); throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } @@ -2486,7 +2448,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { public int nativeUpdate(String sql, Map params) { DataNativeSqlStatement sinfo = super.nativeParse(sql, params); final long s = System.currentTimeMillis(); - SourceConnection conn = writePool.pollConnection(); + JdbcConnection conn = writePool.pollConnection(); Statement stmt = null; try { conn.setAutoCommit(false); @@ -2513,9 +2475,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { conn.rollback(stmt); throw new SourceException(e); } finally { - if (conn != null) { - writePool.offerConnection(conn); - } + writePool.offerConnection(conn); } } @@ -2533,7 +2493,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { @Override public V nativeQuery(String sql, BiConsumer consumer, Function handler) { final long s = System.currentTimeMillis(); - final SourceConnection conn = readPool.pollConnection(); + final JdbcConnection conn = readPool.pollConnection(); try { conn.setAutoCommit(true); if (logger.isLoggable(Level.FINEST)) { @@ -2552,9 +2512,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (Exception ex) { throw new SourceException(ex); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -2563,7 +2521,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { public V nativeQuery(String sql, BiConsumer consumer, Function handler, Map params) { DataNativeSqlStatement sinfo = super.nativeParse(sql, params); final long s = System.currentTimeMillis(); - final SourceConnection conn = readPool.pollConnection(); + final JdbcConnection conn = readPool.pollConnection(); try { conn.setAutoCommit(true); if (logger.isLoggable(Level.FINEST)) { @@ -2599,9 +2557,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (Exception ex) { throw new SourceException(ex); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -2609,7 +2565,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { final boolean mysqlOrPgsql = "mysql".equals(dbtype()) || "postgresql".equals(dbtype()); DataNativeSqlStatement sinfo = super.nativeParse(sql, params); final long s = System.currentTimeMillis(); - final SourceConnection conn = readPool.pollConnection(); + final JdbcConnection conn = readPool.pollConnection(); try { conn.setAutoCommit(true); if (logger.isLoggable(Level.FINEST)) { @@ -2671,9 +2627,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } catch (Exception ex) { throw new SourceException(ex); } finally { - if (conn != null) { - readPool.offerConnection(conn); - } + readPool.offerConnection(conn); } } @@ -2743,26 +2697,30 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - public Serializable getObject(Attribute attr, int index, String column) { + public Serializable getObject(Attribute attr, int index, String columnLabel) { Class t = attr.type(); - if (t == int.class || t == String.class) { - return DataResultSet.getRowColumnValue(this, attr, index, column); + if (t == int.class) { + return index > 0 ? getInt(index) : getInt(columnLabel); + } else if (t == String.class) { + return index > 0 ? getString(index) : getString(columnLabel); + } else if (t == long.class) { + return index > 0 ? getLong(index) : getLong(columnLabel); } else if (t == java.util.Date.class) { - Object val = index > 0 ? getObject(index) : getObject(column); + Object val = index > 0 ? getObject(index) : getObject(columnLabel); return val == null ? null : new java.util.Date(((java.sql.Date) val).getTime()); } else if (t == java.time.LocalDate.class) { - Object val = index > 0 ? getObject(index) : getObject(column); + Object val = index > 0 ? getObject(index) : getObject(columnLabel); return val == null ? null : ((java.sql.Date) val).toLocalDate(); } else if (t == java.time.LocalTime.class) { - Object val = index > 0 ? getObject(index) : getObject(column); + Object val = index > 0 ? getObject(index) : getObject(columnLabel); return val == null ? null : ((java.sql.Time) val).toLocalTime(); } else if (t == java.time.LocalDateTime.class) { - Object val = index > 0 ? getObject(index) : getObject(column); + Object val = index > 0 ? getObject(index) : getObject(columnLabel); return val == null ? null : ((java.sql.Timestamp) val).toLocalDateTime(); } else if (t.getName().startsWith("java.sql.")) { - return index > 0 ? (Serializable) getObject(index) : (Serializable) getObject(column); + return index > 0 ? (Serializable) getObject(index) : (Serializable) getObject(columnLabel); } - return DataResultSet.getRowColumnValue(this, attr, index, column); + return DataResultSet.getRowColumnValue(this, attr, index, columnLabel); } @Override @@ -2825,6 +2783,54 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } + public int getInt(int index) { + try { + return rr.getInt(index); + } catch (SQLException e) { + throw new SourceException(e); + } + } + + public int getInt(String column) { + try { + return rr.getInt(column); + } catch (SQLException e) { + throw new SourceException(e); + } + } + + public long getLong(int index) { + try { + return rr.getLong(index); + } catch (SQLException e) { + throw new SourceException(e); + } + } + + public long getLong(String column) { + try { + return rr.getLong(column); + } catch (SQLException e) { + throw new SourceException(e); + } + } + + public String getString(int index) { + try { + return rr.getString(index); + } catch (SQLException e) { + throw new SourceException(e); + } + } + + public String getString(String column) { + try { + return rr.getString(column); + } catch (SQLException e) { + throw new SourceException(e); + } + } + @Override public EntityInfo getEntityInfo() { return info; @@ -2846,7 +2852,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { protected final Properties connectAttrs; - protected ArrayBlockingQueue queue; + protected ArrayBlockingQueue queue; protected int connectTimeoutSeconds; @@ -2882,8 +2888,10 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (password != null) { this.connectAttrs.put("password", password); } - if (!url.contains("prepareThreshold=")) { - this.connectAttrs.put("prepareThreshold", "-1"); + if ("postgresql".equals(dbtype())) { + if (!url.contains("prepareThreshold=")) { + this.connectAttrs.put("prepareThreshold", "-1"); + } } try { this.driver = DriverManager.getDriver(this.url); @@ -2917,10 +2925,12 @@ public class DataJdbcSource extends AbstractDataSqlSource { || !Objects.equals(newPassword, this.connectAttrs.get("password")) || !Objects.equals(newUrl, url)) { this.urlVersion.incrementAndGet(); } - if (!newUrl.contains("prepareThreshold=")) { - this.connectAttrs.put("prepareThreshold", "-1"); - } else { - this.connectAttrs.remove("prepareThreshold"); + if ("postgresql".equals(dbtype())) { + if (!newUrl.contains("prepareThreshold=")) { + this.connectAttrs.put("prepareThreshold", "-1"); + } else { + this.connectAttrs.remove("prepareThreshold"); + } } this.url = newUrl; this.connectTimeoutSeconds = newConnectTimeoutSeconds; @@ -2960,21 +2970,21 @@ public class DataJdbcSource extends AbstractDataSqlSource { } private void changeMaxConns(int newMaxconns) { - ArrayBlockingQueue newQueue = new ArrayBlockingQueue<>(newMaxconns); - ArrayBlockingQueue oldQueue = this.queue; + ArrayBlockingQueue newQueue = new ArrayBlockingQueue<>(newMaxconns); + ArrayBlockingQueue oldQueue = this.queue; Semaphore oldSemaphore = this.newSemaphore; this.queue = newQueue; this.maxConns = newMaxconns; this.newSemaphore = new Semaphore(this.maxConns); - SourceConnection c; + JdbcConnection c; while ((c = oldQueue.poll()) != null) { c.version = -1; offerConnection(c, oldSemaphore, this.queue); } } - public SourceConnection pollConnection() { - SourceConnection conn = queue.poll(); + public JdbcConnection pollConnection() { + JdbcConnection conn = queue.poll(); if (conn == null) { return newConnection(this.queue); } @@ -2990,28 +3000,16 @@ public class DataJdbcSource extends AbstractDataSqlSource { } //用于事务的连接 - public SourceConnection pollTransConnection() { - SourceConnection conn = queue.poll(); - if (conn == null) { - return newConnection(this.queue); - } - usingCounter.increment(); - if (checkValid(conn)) { - cycleCounter.increment(); - return conn; - } else { - offerConnection(conn); - conn = null; - } - return newConnection(this.queue); + public JdbcConnection pollTransConnection() { + return pollTransConnection(); } - private SourceConnection newConnection(ArrayBlockingQueue queue) { + private JdbcConnection newConnection(ArrayBlockingQueue queue) { Semaphore semaphore = this.newSemaphore; - SourceConnection conn = null; + JdbcConnection conn = null; if (semaphore.tryAcquire()) { try { - conn = new SourceConnection(driver.connect(url, connectAttrs), readFlag, this.urlVersion.get()); + conn = new JdbcConnection(driver.connect(url, connectAttrs), readFlag, this.urlVersion.get()); } catch (SQLException ex) { semaphore.release(); throw new SourceException(ex); @@ -3040,8 +3038,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { offerConnection(connection, this.newSemaphore, this.queue); } - private void offerConnection(final C connection, Semaphore semaphore, Queue queue) { - SourceConnection conn = (SourceConnection) connection; + private void offerConnection(final C connection, Semaphore semaphore, Queue queue) { + JdbcConnection conn = (JdbcConnection) connection; if (conn != null) { conn.commiting = false; try { @@ -3059,7 +3057,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - protected boolean checkValid(SourceConnection conn) { + protected boolean checkValid(JdbcConnection conn) { try { return !conn.conn.isClosed() && conn.version == this.urlVersion.get(); } catch (SQLException ex) { @@ -3082,7 +3080,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - protected class SourceConnection extends DataJdbcConnection { + protected class JdbcConnection extends DataJdbcConnection { public int version; @@ -3090,7 +3088,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { boolean commiting; - public SourceConnection(Connection conn, boolean readFlag, int version) { + public JdbcConnection(Connection conn, boolean readFlag, int version) { super(readFlag); Objects.requireNonNull(conn); this.conn = conn; diff --git a/src/main/java/org/redkale/source/EntityBuilder.java b/src/main/java/org/redkale/source/EntityBuilder.java index 88b75d5ce..5727e0d8e 100644 --- a/src/main/java/org/redkale/source/EntityBuilder.java +++ b/src/main/java/org/redkale/source/EntityBuilder.java @@ -350,6 +350,9 @@ public class EntityBuilder { * @return Entity对象 */ public T getEntityValue(final SelectColumn sels, final DataResultSetRow row) { + if (sels == null) { + return getFullEntityValue(row); + } if (row.wasNull()) { return null; }