public CacheEntry findMostRecent(Connection con) { return con.createQuery("select id, revision, date from cache order by revision desc, date desc limit 1") .executeAndFetchFirst(CacheEntry.class); }
public List<IndexEntry> findIndexesForCache(Connection con, CacheEntry cache) { return con.createQuery("select id, indexId, crc, revision from `index` where cache = :cache") .addParameter("cache", cache.getId()) .executeAndFetch(IndexEntry.class); }
public ResultSetIterable<ArchiveEntry> findArchivesForIndex(Connection con, IndexEntry indexEntry) { return con.createQuery("select archive.id, archive.archiveId, archive.nameHash," + " archive.crc, archive.revision, archive.hash from index_archive " + "join archive on index_archive.archive = archive.id " + "where index_archive.index = :id") .addParameter("id", indexEntry.getId()) .executeAndFetchLazy(ArchiveEntry.class); }
public void associateArchiveToIndex(Connection con, ArchiveEntry archive, IndexEntry index) { if (associateArchive == null) { associateArchive = con.createQuery("insert into index_archive (`index`, archive) values (:index, :archive)"); } associateArchive .addParameter("index", index.getId()) .addParameter("archive", archive.getId()) .executeUpdate(); }
public void associateFileToArchive(Connection con, ArchiveEntry archive, int fileId, int nameHash) { if (associateFile == null) { associateFile = con.createQuery("insert into file (archive, fileId, nameHash) values (:archive, :fileId, :nameHash)"); } associateFile .addParameter("archive", archive.getId()) .addParameter("fileId", fileId) .addParameter("nameHash", nameHash) .executeUpdate(); } }
public CacheEntry createCache(Connection con, int revision, Instant date) { int cacheId = con.createQuery("insert into cache (revision, date) values (:revision, :date)") .addParameter("revision", revision) .addParameter("date", date) .executeUpdate() .getKey(int.class); CacheEntry entry = new CacheEntry(); entry.setId(cacheId); entry.setRevision(revision); entry.setDate(date); return entry; }
public ArchiveEntry findArchive(Connection con, IndexEntry index, int archiveId, int nameHash, int crc, int revision) { if (findArchive == null) { findArchive = con.createQuery("select distinct archive.id, archive.archiveId, archive.nameHash," + " archive.crc, archive.revision, archive.hash from archive " + " join index_archive on index_archive.archive = archive.id" + " join `index` on index.id = index_archive.index" + " where archive.archiveId = :archiveId" + " and archive.nameHash = :nameHash" + " and archive.crc = :crc" + " and archive.revision = :revision" + " and index.indexId = :indexId"); } ArchiveEntry entry = findArchive .addParameter("archiveId", archiveId) .addParameter("nameHash", nameHash) .addParameter("crc", crc) .addParameter("revision", revision) .addParameter("indexId", index.getIndexId()) .executeAndFetchFirst(ArchiveEntry.class); return entry; }
public IndexEntry createIndex(Connection con, CacheEntry cache, int indexId, int crc, int revision) { int id = con.createQuery("insert into `index` (cache, indexId, crc, revision) values (:cache, :indexId, :crc, :revision)") .addParameter("cache", cache.getId()) .addParameter("indexId", indexId) .addParameter("crc", crc) .addParameter("revision", revision) .executeUpdate() .getKey(int.class); IndexEntry entry = new IndexEntry(); entry.setId(id); entry.setIndexId(indexId); entry.setCrc(crc); entry.setRevision(revision); return entry; }
public ArchiveEntry createArchive(Connection con, IndexEntry index, int archiveId, int nameHash, int crc, int revision, byte[] hash) { if (insertArchive == null) { insertArchive = con.createQuery("insert into archive (archiveId, nameHash, crc, revision, hash) values " + "(:archiveId, :nameHash, :crc, :revision, :hash)"); } int id = insertArchive .addParameter("archiveId", archiveId) .addParameter("nameHash", nameHash) .addParameter("crc", crc) .addParameter("revision", revision) .addParameter("hash", hash) .executeUpdate() .getKey(int.class); ArchiveEntry entry = new ArchiveEntry(); entry.setId(id); entry.setArchiveId(archiveId); entry.setNameHash(nameHash); entry.setCrc(crc); entry.setRevision(revision); entry.setHash(hash); return entry; }
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); }
/** * Creates a {@link Query} * @param query the sql query string * @param returnGeneratedKeys boolean value indicating if the database should return any generated keys. * @return the {@link Query} instance * * @deprecated create queries with {@link org.sql2o.Connection} class instead, using try-with-resource blocks * <code> * try (Connection con = sql2o.open()) { * return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class); * } * </code> */ @Deprecated public Query createQuery(String query, boolean returnGeneratedKeys) { return new Connection(this, true).createQuery(query, returnGeneratedKeys); }
/** * Creates a {@link Query} * @param query the sql query string * @return the {@link Query} instance * * @deprecated create queries with {@link org.sql2o.Connection} class instead, using try-with-resource blocks * <code> * try (Connection con = sql2o.open()) { * return sql2o.createQuery(query, name).executeAndFetch(Pojo.class); * } * </code> */ @Deprecated public Query createQuery(String query){ Connection connection = new Connection(this, true); return connection.createQuery(query); }
public Query createQuery(String queryText){ boolean returnGeneratedKeys = this.sql2o.getQuirks().returnGeneratedKeysByDefault(); return createQuery(queryText, returnGeneratedKeys); }
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); }
private List<String> getCategoriesFor(Connection conn, UUID post_uuid) { return conn.createQuery("select category from posts_categories where post_uuid=:post_uuid") .addParameter("post_uuid", post_uuid) .executeAndFetch(String.class); }
public static List<String> getCompliantServers(Connection connection) { String query = "select servers.domain from servers inner join current_tests on current_tests.domain = servers.domain" + " where test in (:tests) group by current_tests.domain having sum(success) = count(success) and listed = 1"; List<String> tests = new ArrayList<>(TestUtils.getTestNames()); //Make sure server supports in-band registration tests.add("xep0077"); return connection.createQuery(query) .addParameter("tests",tests) .executeAndFetch(String.class); }
public static <T> List<T> find_all(Class<T> tClass){ Logger logger = LogManager.getLogger(ActiveRecordBase.class); Connection connection = DB.sql2o.beginTransaction(); String table = Util.getclassName(tClass).toLowerCase(); String sql = "select * from "+table; logger.info(Constant.LOG_AC_NAME+"execute sql " + sql); List<T> list = connection.createQuery(sql).executeAndFetch(tClass); return list; }
@Override public void deletePost(UUID uuid) { try (Connection conn = sql2o.open()) { conn.createQuery("delete from posts where post_uuid=:post_uuid") .addParameter("post_uuid", uuid) .executeUpdate(); } }
public static <T> List<T> getList(String sql, Class<T> clazz, Map<String, Object> params){ Connection con = sql2o.open(); Query query = con.createQuery(sql); executeQuery(query, params); List<T> list = query.executeAndFetch(clazz); con.close(); return list; }
@Override public void delete(final Todo todo) { sql2oHandler.withSql2o(sql2o -> { sql2o.withConnection((connection, argument) -> { connection.createQuery("DELETE FROM todos WHERE id = :id") .addParameter("id", todo.getId()) .executeUpdate(); }); return todo; }); }