@Test public void testExecuteAndFetchWithNulls() { String sql = "create table testExecWithNullsTbl (" + "id int identity primary key, " + "text varchar(255), " + "aNumber int, " + "aLongNumber bigint)"; sql2o.createQuery(sql, "testExecuteAndFetchWithNulls").executeUpdate(); Connection connection = sql2o.beginTransaction(); Query insQuery = connection.createQuery( "insert into testExecWithNullsTbl (text, aNumber, aLongNumber) values(:text, :number, :lnum)"); insQuery .addParameter("text", "some text") .addParameter("number", 2) .addParameter("lnum", 10L) .executeUpdate(); insQuery .addParameter("text", "some text") .addParameter("number", (Integer) null) .addParameter("lnum", 10L) .executeUpdate(); insQuery .addParameter("text", (String) null) .addParameter("number", 21) .addParameter("lnum", (Long) null) .executeUpdate(); insQuery .addParameter("text", "some text") .addParameter("number", 1221) .addParameter("lnum", 10) .executeUpdate(); insQuery .addParameter("text", "some text") .addParameter("number", 2311) .addParameter("lnum", 12) .executeUpdate(); connection.commit(); List<Entity> fetched = sql2o.createQuery("select * from testExecWithNullsTbl").executeAndFetch(Entity.class); assertTrue(fetched.size() == 5); assertNull(fetched.get(2).text); assertNotNull(fetched.get(3).text); assertNull(fetched.get(1).aNumber); assertNotNull(fetched.get(2).aNumber); assertNull(fetched.get(2).aLongNumber); assertNotNull(fetched.get(3).aLongNumber); }
@Test public void testCaseInsensitive() { sql2o .createQuery( "create table testCI(id2 int primary key, value2 varchar(20), sometext varchar(20), valwithgetter varchar(20))") .executeUpdate(); Query query = sql2o.createQuery( "insert into testCI(id2, value2, sometext, valwithgetter) values(:id, :value, :someText, :valwithgetter)"); for (int i = 0; i < 20; i++) { query .addParameter("id", i) .addParameter("value", "some text " + i) .addParameter("someText", "whatever " + i) .addParameter("valwithgetter", "spaz" + i) .addToBatch(); } query.executeBatch(); List<CIEntity> ciEntities = sql2o .createQuery("select * from testCI") .setCaseSensitive(false) .executeAndFetch(CIEntity.class); assertTrue(ciEntities.size() == 20); // test defaultCaseSensitive; sql2o.setDefaultCaseSensitive(false); List<CIEntity> ciEntities2 = sql2o.createQuery("select * from testCI").executeAndFetch(CIEntity.class); assertTrue(ciEntities2.size() == 20); }
/** ************ Helper stuff ***************** */ private void createAndFillUserTable() { int rowCount = 10000; sql2o .createQuery( "create table User(\n" + "id int identity primary key,\n" + "name varchar(20),\n" + "email varchar(255),\n" + "text varchar(100))") .executeUpdate(); Query insQuery = sql2o.createQuery("insert into User(name, email, text) values (:name, :email, :text)"); Date before = new Date(); for (int idx = 0; idx < rowCount; idx++) { insQuery .addParameter("name", "a name " + idx) .addParameter("email", String.format("*****@*****.**", idx)) .addParameter("text", "some text") .addToBatch(); } insQuery.executeBatch(); Date after = new Date(); Long span = after.getTime() - before.getTime(); System.out.println( String.format("inserted %d rows into User table. Time used: %s ms", rowCount, span)); insertIntoUsers += rowCount; }
public Query withParams(Object... paramValues) { int i = 0; for (Object paramValue : paramValues) { addParameter("p" + (++i), paramValue); } return this; }
private static void executeQuery(Query query, Map<String, Object> params) { if (null != params && params.size() > 0) { Set<String> keys = params.keySet(); for (String key : keys) { query.addParameter(key, params.get(key)); } } }
@RequestMapping(path = "/system/sync") public ResponseEntity<RestResponse> sync( Authentication authentication, HttpServletRequest request) throws Throwable { String json = org.apache.commons.io.IOUtils.toString(request.getInputStream(), "UTF-8"); Sync sync = this.gson.fromJson(json, Sync.class); if (sync == null) { sync = new Sync(); } List<String> pageIds = new ArrayList<>(); List<String> restIds = new ArrayList<>(); try (Connection connection = sql2o.open()) { if (sync.getPages() != null && !sync.getPages().isEmpty()) { for (Page clientPage : sync.getPages()) { Query query = connection.createQuery( "select groovy.java_class as javaClass, page.path as mountPath, groovy.groovy_id as groovyId, page.page_id as pageId, html as serverHtml, html_crc32 as serverHtmlCrc32, groovy.script as serverGroovy, groovy.script_crc32 as serverGroovyCrc32 from page inner join groovy on page.groovy_id = groovy.groovy_id where page.page_id = :pageId"); query.addParameter("pageId", clientPage.getPageId()); Page serverPage = query.executeAndFetchFirst(Page.class); boolean groovyConflicted = !clientPage.getServerGroovyCrc32().equals(serverPage.getServerGroovyCrc32()); boolean htmlConflicted = !clientPage.getServerHtmlCrc32().equals(serverPage.getServerHtmlCrc32()); pageIds.add(clientPage.getPageId()); clientPage.setGroovyConflicted(groovyConflicted); clientPage.setHtmlConflicted(htmlConflicted); String path = StringUtils.replaceChars(serverPage.getJavaClass(), '.', '/'); clientPage.setHtmlPath(path + ".html"); clientPage.setGroovyPath(path + ".groovy"); if (!groovyConflicted && !htmlConflicted && Strings.isNullOrEmpty(clientPage.getClientGroovyCrc32()) && Strings.isNullOrEmpty(clientPage.getClientHtmlCrc32())) { // delete command clientPage.setServerGroovyCrc32(null); clientPage.setServerGroovy(null); clientPage.setServerHtmlCrc32(null); clientPage.setServerHtml(null); classLoader.removeSourceCache(serverPage.getGroovyId()); classLoader.removeClassCache(serverPage.getJavaClass()); connection .createQuery("delete from page where page_id = :page_id") .addParameter("page_id", serverPage.getPageId()) .executeUpdate(); connection .createQuery("delete from groovy where groovy_id = :groovy_id") .addParameter("groovy_id", serverPage.getGroovyId()) .executeUpdate(); Application.get().unmount(serverPage.getMountPath()); Application.get().getMarkupSettings().getMarkupFactory().getMarkupCache().clear(); } else { if (!groovyConflicted) { // update command classLoader.removeSourceCache(serverPage.getGroovyId()); classLoader.removeClassCache(serverPage.getJavaClass()); GroovyCodeSource source = new GroovyCodeSource( Strings.isNullOrEmpty(clientPage.getClientGroovy()) ? serverPage.getServerGroovy() : clientPage.getClientGroovy(), serverPage.getGroovyId(), "/groovy/script"); source.setCachable(true); Class<?> pageClass = classLoader.parseClass(source, true); Application.get() .mountPage( serverPage.getMountPath(), (Class<? extends org.apache.wicket.Page>) pageClass); connection .createQuery( "update groovy set script = :script, script_crc32 = :script_crc32, java_class = :java_class where groovy_id = :groovy_id") .addParameter( "script", Strings.isNullOrEmpty(clientPage.getClientGroovy()) ? serverPage.getServerGroovy() : clientPage.getClientGroovy()) .addParameter("script_crc32", clientPage.getClientGroovyCrc32()) .addParameter("java_class", pageClass.getName()) .addParameter("groovy_id", serverPage.getGroovyId()) .executeUpdate(); clientPage.setServerGroovyCrc32(clientPage.getClientGroovyCrc32()); clientPage.setServerGroovy( Strings.isNullOrEmpty(clientPage.getClientGroovy()) ? serverPage.getServerGroovy() : clientPage.getClientGroovy()); } else { clientPage.setServerGroovyCrc32(serverPage.getServerGroovyCrc32()); clientPage.setServerGroovy(serverPage.getServerGroovy()); if (Strings.isNullOrEmpty(clientPage.getClientGroovy()) && Strings.isNullOrEmpty(clientPage.getClientGroovyCrc32())) { clientPage.setGroovyConflicted(false); } } if (!htmlConflicted) { // update command connection .createQuery( "update page set html = :html, html_crc32 = :html_crc32 where page_id = :page_id") .addParameter( "html", Strings.isNullOrEmpty(clientPage.getClientHtml()) ? serverPage.getServerHtml() : clientPage.getClientHtml()) .addParameter("html_crc32", clientPage.getClientHtmlCrc32()) .addParameter("page_id", serverPage.getPageId()) .executeUpdate(); Application.get().getMarkupSettings().getMarkupFactory().getMarkupCache().clear(); clientPage.setServerHtmlCrc32(clientPage.getClientHtmlCrc32()); clientPage.setServerHtml( Strings.isNullOrEmpty(clientPage.getClientHtml()) ? serverPage.getServerHtml() : clientPage.getClientHtml()); } else { clientPage.setServerHtmlCrc32(serverPage.getServerHtmlCrc32()); clientPage.setServerHtml(serverPage.getServerHtml()); } } } } if (sync.getRests() != null && !sync.getRests().isEmpty()) { for (Rest clientRest : sync.getRests()) { restIds.add(clientRest.getRestId()); Query query = connection.createQuery( "select groovy.java_class as javaClass, groovy.groovy_id as groovyId, rest.rest_id as restId, groovy.script as serverGroovy, groovy.script_crc32 as serverGroovyCrc32 from rest inner join groovy on rest.groovy_id = groovy.groovy_id where rest.rest_id = :restId"); query.addParameter("restId", clientRest.getRestId()); Rest serverRest = query.executeAndFetchFirst(Rest.class); boolean groovyConflicted = !clientRest.getServerGroovyCrc32().equals(serverRest.getServerGroovyCrc32()); clientRest.setGroovyConflicted(groovyConflicted); String path = StringUtils.replaceChars(serverRest.getJavaClass(), '.', '/'); clientRest.setGroovyPath(path + ".groovy"); if (!groovyConflicted && Strings.isNullOrEmpty(clientRest.getClientGroovyCrc32())) { // delete command clientRest.setServerGroovy(null); clientRest.setServerGroovyCrc32(null); classLoader.removeSourceCache(serverRest.getGroovyId()); classLoader.removeClassCache(serverRest.getJavaClass()); connection .createQuery("delete from rest where rest_id = :rest_id") .addParameter("rest_id", serverRest.getRestId()) .executeUpdate(); connection .createQuery("delete from groovy where groovy_id = :groovy_id") .addParameter("groovy_id", serverRest.getGroovyId()) .executeUpdate(); } else { if (!groovyConflicted) { // update command classLoader.removeSourceCache(serverRest.getGroovyId()); classLoader.removeClassCache(serverRest.getJavaClass()); GroovyCodeSource source = new GroovyCodeSource( Strings.isNullOrEmpty(clientRest.getClientGroovy()) ? serverRest.getServerGroovy() : clientRest.getClientGroovy(), serverRest.getGroovyId(), "/groovy/script"); source.setCachable(true); Class<?> serviceClass = classLoader.parseClass(source, true); connection .createQuery( "update groovy set script = :script, script_crc32 = :script_crc32, java_class = :java_class where groovy_id = :groovy_id") .addParameter( "script", Strings.isNullOrEmpty(clientRest.getClientGroovy()) ? serverRest.getServerGroovy() : clientRest.getClientGroovy()) .addParameter("script_crc32", clientRest.getClientGroovyCrc32()) .addParameter("java_class", serviceClass.getName()) .addParameter("groovy_id", serverRest.getGroovyId()) .executeUpdate(); clientRest.setServerGroovyCrc32(clientRest.getClientGroovyCrc32()); clientRest.setServerGroovy( Strings.isNullOrEmpty(clientRest.getClientGroovy()) ? serverRest.getServerGroovy() : clientRest.getClientGroovy()); } else { clientRest.setServerGroovyCrc32(serverRest.getServerGroovyCrc32()); clientRest.setServerGroovy(serverRest.getServerGroovy()); } } } } List<Page> serverPages; if (!pageIds.isEmpty()) { Query query = connection.createQuery( "select groovy.java_class as javaClass, page.page_id as pageId, html as serverHtml, html_crc32 as serverHtmlCrc32, groovy.script as serverGroovy, groovy.script_crc32 as serverGroovyCrc32 from page inner join groovy on page.groovy_id = groovy.groovy_id where page.system = false and page.page_id not in (:pageId)"); query.addParameter("pageId", pageIds); serverPages = query.executeAndFetch(Page.class); } else { Query query = connection.createQuery( "select groovy.java_class as javaClass, page.page_id as pageId, html as serverHtml, html_crc32 as serverHtmlCrc32, groovy.script as serverGroovy, groovy.script_crc32 as serverGroovyCrc32 from page inner join groovy on page.groovy_id = groovy.groovy_id where page.system = false"); serverPages = query.executeAndFetch(Page.class); } if (serverPages != null && !serverPages.isEmpty()) { for (Page serverPage : serverPages) { String path = StringUtils.replaceChars(serverPage.getJavaClass(), '.', '/'); serverPage.setHtmlPath(path + ".html"); serverPage.setGroovyPath(path + ".groovy"); serverPage.setGroovyConflicted(false); serverPage.setHtmlConflicted(false); sync.addPage(serverPage); } } List<Rest> serverRests; if (!restIds.isEmpty()) { Query query = connection.createQuery( "select groovy.java_class as javaClass, rest.rest_id as restId, groovy.script as serverGroovy, groovy.script_crc32 as serverGroovyCrc32 from rest inner join groovy on rest.groovy_id = groovy.groovy_id where rest.system = false and rest.rest_id not in (:restId)"); query.addParameter("restId", restIds); serverRests = query.executeAndFetch(Rest.class); } else { Query query = connection.createQuery( "select groovy.java_class as javaClass, rest.rest_id as restId, groovy.script as serverGroovy, groovy.script_crc32 as serverGroovyCrc32 from rest inner join groovy on rest.groovy_id = groovy.groovy_id where rest.system = false"); serverRests = query.executeAndFetch(Rest.class); } if (serverRests != null && !serverRests.isEmpty()) { for (Rest restPage : serverRests) { String path = StringUtils.replaceChars(restPage.getJavaClass(), '.', '/'); restPage.setGroovyPath(path + ".groovy"); restPage.setGroovyConflicted(false); sync.addRest(restPage); } } } RestResponse response = new RestResponse(); response.setData(sync); response.setResultCode(HttpStatus.OK.value()); response.setResultMessage(HttpStatus.OK.getReasonPhrase()); return ResponseEntity.ok(response); }