/** * Adds a set of parameters to this <code>Query</code> * object's batch of commands and returns any generated keys. <br/> * * If maxBatchRecords is more than 0, executeBatch is called upon adding that many * commands to the batch. This method will return any generated keys if <code>fetchGeneratedKeys</code> is set. <br/> * * The current number of batched commands is accessible via the <code>getCurrentBatchRecords()</code> * method. */ public <A> List<A> addToBatchGetKeys(Class<A> klass){ this.addToBatch(); if (this.currentBatchRecords == 0) { return this.connection.getKeys(klass); } else { return Collections.emptyList(); } }
/** * Adds a set of parameters to this <code>Query</code> * object's batch of commands and returns any generated keys. <br/> * * If maxBatchRecords is more than 0, executeBatch is called upon adding that many * commands to the batch. This method will return any generated keys if <code>fetchGeneratedKeys</code> is set. <br/> * * The current number of batched commands is accessible via the <code>getCurrentBatchRecords()</code> * method. */ public <A> List<A> addToBatchGetKeys(Class<A> klass){ this.addToBatch(); if (this.currentBatchRecords == 0) { return this.connection.getKeys(klass); } else { return Collections.emptyList(); } }
/** * Adds a set of parameters to this <code>Query</code> * object's batch of commands and returns any generated keys. <br/> * <p> * If maxBatchRecords is more than 0, executeBatch is called upon adding that many * commands to the batch. This method will return any generated keys if <code>fetchGeneratedKeys</code> is set. <br/> * <p> * The current number of batched commands is accessible via the <code>getCurrentBatchRecords()</code> * method. */ public <A> List<A> addToBatchGetKeys(Class<A> klass) { this.addToBatch(); if (this.currentBatchRecords == 0) { return this.connection.getKeys(klass); } else { return Collections.emptyList(); } }
/** * @param connection * @param domain * @param results * @param timestamp * @return */ public static boolean addCurrentResults(Connection connection, String domain, List<Result> results, Instant timestamp) { String queryText = "insert into current_tests(domain,test,success,timestamp) " + "values(:domain,:test,:success,:timestamp)"; Query query = connection.createQuery(queryText); results.forEach( result -> query .addParameter("test", result.getTest().short_name()) .addParameter("success", result.isSuccess()) .addParameter("domain", domain) .addParameter("timestamp", timestamp) .addToBatch() ); query.executeBatch(); return true; }
/** * @param connection should be a transaction of type java.sql.Connection.TRANSACTION_SERIALIZABLE for sqlite database * @param rdpList * @param beginTime * @param endTime * @return */ public static boolean addPeriodicResults(Connection connection, List<ResultDomainPair> rdpList, Instant beginTime, Instant endTime) { //Add the iteration to iterations list String query = "insert into periodic_tests(domain,test,success,iteration_number)" + "values(:domain,:test,:success,:iteration)"; int iterationNumber = getNextIterationNumber(connection); addIteration(connection, iterationNumber, beginTime, endTime); Query resultInsertQuery = connection.createQuery(query); rdpList.forEach(rdp -> { String domain = rdp.getDomain(); rdp.getResults().forEach( result -> resultInsertQuery .addParameter("test", result.getTest().short_name()) .addParameter("success", result.isSuccess()) .addParameter("domain", domain) .addParameter("iteration", iterationNumber) .addToBatch()); }); resultInsertQuery.executeBatch(); for (ResultDomainPair rdp : rdpList) { addCurrentResults(connection, rdp.getDomain(), rdp.getResults(), beginTime); } return true; }
public Pizza save(Pizza pizza) { try (Connection con = sql2o.beginTransaction()) { long pizzaId = (long) con.createQuery("INSERT INTO pizza (base_id, name, price) VALUES (:baseId, :name, :price)", true) .addParameter("baseId", pizza.getBase().getId()) .addParameter("name", pizza.getName()) .addParameter("price", pizza.getPrice()) .executeUpdate() .getKey(); Query queryForPt = con.createQuery("INSERT INTO pizza_toppings (pizza_id, toppings_id) VALUES (:pizzaId, :toppingsId)"); pizza.getToppings().forEach(t -> queryForPt .addParameter("pizzaId", pizzaId) .addParameter("toppingsId", t.getId()) .addToBatch()); queryForPt.executeBatch(); con.commit(); } return pizza; }