Search in sources :

Example 1 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class H2Tests method testIssue155.

@Test
public void testIssue155() {
    Sql2o sql2o = new Sql2o(ds, new NoQuirks(new HashMap<Class, Converter>() {

        {
            put(DateTime.class, new DateTimeConverter(DateTimeZone.getDefault()));
        }
    }));
    assertThat(sql2o.getQuirks(), is(instanceOf(NoQuirks.class)));
    try (Connection connection = sql2o.open()) {
        int val = connection.createQuery("select 42").executeScalar(Integer.class);
        assertThat(val, is(equalTo(42)));
    }
}
Also used : HashMap(java.util.HashMap) Connection(org.sql2o.Connection) DateTimeConverter(org.sql2o.converters.joda.DateTimeConverter) NoQuirks(org.sql2o.quirks.NoQuirks) Sql2o(org.sql2o.Sql2o) Test(org.junit.Test)

Example 2 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class IssuesTest method testIssue166OneCharacterParameterFail.

@Test
public void testIssue166OneCharacterParameterFail() {
    try (Connection connection = sql2o.open()) {
        connection.createQuery("create table testIssue166OneCharacterParameterFail(id integer, val varchar(10))").executeUpdate();
        // This because of the :v parameter.
        connection.createQuery("insert into testIssue166OneCharacterParameterFail(id, val) values(:id, :v)").addParameter("id", 1).addParameter("v", "foobar").executeUpdate();
        int cnt = connection.createQuery("select count(*) from testIssue166OneCharacterParameterFail where id = :p").addParameter("p", 1).executeScalar(Integer.class);
        assertEquals(1, cnt);
    }
}
Also used : Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 3 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class IssuesTest method testIndexOutOfRangeExceptionWithMultipleColumnsWithSameName.

/**
 * Test for issue #148 (https://github.com/aaberg/sql2o/issues/148)
 * ## IndexOutOfRange exception
 * When a resultset has multiple columns with the same name, sql2o 1.5.1 will throw an IndexOutOfRange exception when calling executeAndFetchTable() method.
 */
@Test
public void testIndexOutOfRangeExceptionWithMultipleColumnsWithSameName() {
    class ThePojo {

        public int id;

        public String name;
    }
    String sql = "select 11 id, 'something' name, 'something else' name from (values(0))";
    ThePojo p;
    Table t;
    try (Connection connection = sql2o.open()) {
        p = connection.createQuery(sql).executeAndFetchFirst(ThePojo.class);
        t = connection.createQuery(sql).executeAndFetchTable();
    }
    assertEquals(11, p.id);
    assertEquals("something else", p.name);
    assertEquals(11, (int) t.rows().get(0).getInteger("id"));
    assertEquals("something else", t.rows().get(0).getString("name"));
}
Also used : Table(org.sql2o.data.Table) Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 4 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class IssuesTest method testIssue149NullPointerWhenUsingWrongParameterName.

@Test
public void testIssue149NullPointerWhenUsingWrongParameterName() {
    try (Connection connection = sql2o.open()) {
        connection.createQuery("create table issue149 (id integer primary key, val varchar(20))").executeUpdate();
        connection.createQuery("insert into issue149(id, val) values (:id, :val)").addParameter("id", 1).addParameter("asdsa", // spell-error in parameter name
        "something").executeUpdate();
        fail("Expected exception!!");
    } catch (Sql2oException ex) {
    // awesome!
    } catch (Throwable t) {
        fail("A " + t.getClass().getName() + " was thrown, but An " + Sql2oException.class.getName() + " was expected");
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 5 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class OracleTest method testUUiID.

@Test
@Ignore
public void testUUiID() {
    UUID uuid1 = UUID.randomUUID();
    UUID uuid2 = UUID.randomUUID();
    final String ddl = "create table testUUID(id integer primary key, uuidval raw(16))";
    final String insertSql = "insert into testUUID(id, uuidval) values (:id, :val)";
    final String selectSql = "select uuidval from testUUID where id = :id";
    try {
        try (Connection connection = sql2o.open()) {
            connection.createQuery(ddl).executeUpdate();
            Query insertQuery = connection.createQuery(insertSql);
            insertQuery.addParameter("id", 1).addParameter("val", uuid1).executeUpdate();
            insertQuery.addParameter("id", 2).addParameter("val", uuid2).executeUpdate();
            Query selectQuery = connection.createQuery(selectSql);
            UUID uuid1FromDb = selectQuery.addParameter("id", 1).executeScalar(UUID.class);
            UUID uuid2FromDb = selectQuery.addParameter("id", 2).executeScalar(UUID.class);
            assertEquals(uuid1, uuid1FromDb);
            assertEquals(uuid2, uuid2FromDb);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("test failed. Exception");
    } finally {
        try (Connection con = sql2o.open()) {
            con.createQuery("drop table testUUID").executeUpdate();
        }
    }
}
Also used : Query(org.sql2o.Query) Connection(org.sql2o.Connection) UUID(java.util.UUID) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Connection (org.sql2o.Connection)31 Test (org.junit.Test)16 Query (org.sql2o.Query)7 UUID (java.util.UUID)5 Table (org.sql2o.data.Table)5 Instant (java.time.Instant)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Sql2oException (org.sql2o.Sql2oException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SessionEntry (net.runelite.http.service.account.beans.SessionEntry)2 PlayerEntity (net.runelite.http.service.xp.beans.PlayerEntity)2 Sql2o (org.sql2o.Sql2o)2 Row (org.sql2o.data.Row)2 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 SQLException (java.sql.SQLException)1