public Query createQueryWithParams(String queryText, Object... paramValues){ // due to #146, creating a query will not create a statement anymore; // the PreparedStatement will only be created once the query needs to be executed // => there is no need to handle the query closing here anymore since there is nothing to close return createQuery(queryText) .withParams(paramValues); }
public Query createQueryWithParams(String queryText, Object... paramValues) { // due to #146, creating a query will not create a statement anymore; // the PreparedStatement will only be created once the query needs to be executed // => there is no need to handle the query closing here anymore since there is nothing to close return createQuery(queryText) .withParams(paramValues); }
public Query createQueryWithParams(String queryText, Object... paramValues){ // due to #146, creating a query will not create a statement anymore; // the PreparedStatement will only be created once the query needs to be executed // => there is no need to handle the query closing here anymore since there is nothing to close return createQuery(queryText) .withParams(paramValues); }
public long count(String sql, Object... args) { int pos = 1; while (sql.contains("?")) { sql = sql.replaceFirst("\\?", ":p" + (pos++)); } args = args == null ? new Object[]{} : args; try (Connection con = getSql2o().open()) { this.cleanParam(); log.debug(EXECUTE_SQL_PREFIX + " => {}", sql); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(args)); return con.createQuery(sql).withParams(args) .executeAndFetchFirst(Long.class); } }
public <T> T query(Class<T> type, String sql, Object... args) { int pos = 1; while (sql.contains(SQL_QM)) { sql = sql.replaceFirst("\\?", ":p" + (pos++)); } try (Connection con = getSql2o().open()) { log.debug(EXECUTE_SQL_PREFIX + " => {}", sql); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(args)); this.cleanParam(); Query query = con.createQuery(sql).withParams(args).throwOnMappingFailure(false); QueryMeta queryMeta = SqlBuilder.buildFindAllSql(this, null); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } return query.executeAndFetchFirst(type); } }
public <T> List<T> queryAll(Class<T> type, String sql, Object... args) { int pos = 1; while (sql.contains(SQL_QM)) { sql = sql.replaceFirst("\\?", ":p" + (pos++)); } PageRow pageRow = Base.pageLocal.get(); sql = SqlBuilder.appendPageParams(sql, pageRow); args = args == null ? new Object[]{} : args; try (Connection con = getSql2o().open()) { log.debug(EXECUTE_SQL_PREFIX + " => {}", sql); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(args)); this.cleanParam(); Query query = con.createQuery(sql).withParams(args).throwOnMappingFailure(false); QueryMeta queryMeta = SqlBuilder.buildFindAllSql(this, null); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } return query.executeAndFetch(type); } }
private int invoke(QueryMeta queryMeta) { log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams())); Connection con = getConn(); Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams()); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } int result = query.executeUpdate().getResult(); try { if (null == Base.connectionThreadLocal.get() && !con.getJdbcConnection().getAutoCommit()) { con.commit(); } } catch (Exception e) { throw new RuntimeException(e); } return result; }
private long count(boolean cleanParam) { QueryMeta queryMeta = SqlBuilder.buildCountSql(this); try (Connection con = getSql2o().open()) { if (cleanParam) this.cleanParam(); log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams())); return con.createQuery(queryMeta.getSql()) .withParams(queryMeta.getParams()) .executeAndFetchFirst(Long.class); } }
public <T extends ActiveRecord> T find() { QueryMeta queryMeta = SqlBuilder.buildFindSql(this); Class<T> type = (Class<T>) getClass(); try (Connection con = getSql2o().open()) { this.cleanParam(); log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams())); Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams()).throwOnMappingFailure(false); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } return query.executeAndFetchFirst(type); } }
/** * Save a model * * @param model model instance * @param <S> * @return ResultKey */ public <S extends Model> ResultKey save(S model) { List<Object> columnValues = AnimaUtils.toColumnValues(model, true); String sql = this.buildInsertSQL(model, columnValues); Connection conn = getConn(); try { List<Object> params = columnValues.stream() .filter(Objects::nonNull) .collect(toList()); return new ResultKey(conn.createQuery(sql) .withParams(params) .executeUpdate() .getKey()); } finally { this.closeConn(conn); this.clean(conn); } }
public <T> List<T> findAll(Class<T> type, Supplier<ConditionEnum>... conditions) { QueryMeta queryMeta = SqlBuilder.buildFindAllSql(this, conditions); try (Connection con = getSql2o().open()) { log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams())); this.cleanParam(); Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams()).throwOnMappingFailure(false); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } return query.executeAndFetch(type); } }
public <T> T find(Class<T> type, Serializable id) { String sql = "SELECT * FROM " + getTableName() + " WHERE " + getPk() + " = :p1"; QueryMeta queryMeta = new QueryMeta(); SqlBuilder.mapping(queryMeta, this.getClass()); try (Connection con = getSql2o().open()) { this.cleanParam(); log.debug(EXECUTE_SQL_PREFIX + " => {}", sql); log.debug(PARAMETER_PREFIX + " => [{}]", id); Query query = con.createQuery(sql).withParams(id).throwOnMappingFailure(false); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } return query.executeAndFetchFirst(type); } }
/** * Execute sql statement * * @param sql sql statement * @param params params * @return affect the number of rows */ public int execute(String sql, Object... params) { Connection conn = getConn(); try { return conn.createQuery(sql) .withParams(params) .executeUpdate() .getResult(); } finally { this.closeConn(conn); this.clean(conn); } }
/** * Querying a List<Map> * * @param sql sql statement * @param params params * @return List<Map> */ public List<Map<String, Object>> queryListMap(String sql, Object[] params) { Connection conn = getConn(); try { return conn.createQuery(sql) .withParams(params) .setAutoDeriveColumnNames(true) .throwOnMappingFailure(false) .executeAndFetchTable() .asList(); } finally { this.closeConn(conn); this.clean(null); } }
/** * Querying a list * * @param type model type * @param sql sql statement * @param params params * @param <S> * @return List<S> */ public <S> List<S> queryList(Class<S> type, String sql, Object[] params) { Connection conn = getConn(); try { return conn.createQuery(sql) .withParams(params) .setColumnMappings(computeModelColumnMappings(type)) .throwOnMappingFailure(false) .executeAndFetch(type); } finally { this.closeConn(conn); this.clean(null); } }
/** * Querying a model * * @param type model type * @param sql sql statement * @param params params * @param <S> * @return S */ public <S> S queryOne(Class<S> type, String sql, Object[] params) { Connection conn = getConn(); try { Query query = conn.createQuery(sql) .withParams(params) .setAutoDeriveColumnNames(true) .throwOnMappingFailure(false); return ifReturn(AnimaUtils.isBasicType(type), () -> query.executeScalar(type), () -> query.executeAndFetchFirst(type)); } finally { this.closeConn(conn); this.clean(null); } }
.withParams(params) .executeAndFetchFirst(Long.class); String pageSQL = this.buildPageSQL(sql, pageRow); List<T> list = conn.createQuery(pageSQL) .withParams(params) .setAutoDeriveColumnNames(true) .throwOnMappingFailure(false)