/** @see #query(String, String, Object...) */ public <T> List<T> query(String sql, Object... paras) { Connection conn = null; try { conn = config.getConnection(); return query(config, conn, sql, paras); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** * Execute callback. It is useful when all the API can not satisfy your requirement. * * @param config the Config object * @param callback the ICallback interface */ Object execute(Config config, ICallback callback) { Connection conn = null; try { conn = config.getConnection(); return callback.run(conn); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** * Update Record. * * @param tableName the table name of the Record save to * @param primaryKey the primary key of the table * @param record the Record object * @param true if update succeed otherwise false */ public boolean update(String tableName, String primaryKey, Record record) { Connection conn = null; try { conn = config.getConnection(); return update(config, conn, tableName, primaryKey, record); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** @see #paginate(String, int, int, String, String, Object...) */ public Page<Record> paginate( int pageNumber, int pageSize, String select, String sqlExceptSelect, Object... paras) { Connection conn = null; try { conn = config.getConnection(); return paginate(config, conn, pageNumber, pageSize, select, sqlExceptSelect, paras); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. Example: * * <pre> * int[] result = DbPro.use().batch("myConfig", sqlList, 500); * </pre> * * @param sqlList The SQL list to execute. * @param batchSize batch size. * @return The number of rows updated per statement */ public int[] batch(List<String> sqlList, int batchSize) { Connection conn = null; Boolean autoCommit = null; try { conn = config.getConnection(); autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); return batch(config, conn, sqlList, batchSize); } catch (Exception e) { throw new ActiveRecordException(e); } finally { if (autoCommit != null) try { conn.setAutoCommit(autoCommit); } catch (Exception e) { e.printStackTrace(); } config.close(conn); } }
/** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. * * <pre> * Example: * String sql = "insert into user(name, cash) values(?, ?)"; * int[] result = DbPro.use().batch(sql, new Object[][]{{"James", 888}, {"zhanjin", 888}}); * </pre> * * @param sql The SQL to execute. * @param paras An array of query replacement parameters. Each row in this array is one set of * batch replacement values. * @return The number of rows updated per statement */ public int[] batch(String sql, Object[][] paras, int batchSize) { Connection conn = null; Boolean autoCommit = null; try { conn = config.getConnection(); autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); return batch(config, conn, sql, paras, batchSize); } catch (Exception e) { throw new ActiveRecordException(e); } finally { if (autoCommit != null) try { conn.setAutoCommit(autoCommit); } catch (Exception e) { LogKit.error(e.getMessage(), e); } config.close(conn); } }