Search in sources :

Example 6 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 7 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)

Example 8 with Connection

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

the class PostgresTest method testIssue10_StatementsOnPostgres_withTransaction.

@Test
public void testIssue10_StatementsOnPostgres_withTransaction() {
    try (final Connection connection = sql2o.beginTransaction()) {
        String createTableSql = "create table test_table(id SERIAL, val varchar(20))";
        connection.createQuery(createTableSql).executeUpdate();
        String insertSql = "insert into test_table (val) values(:val)";
        Long key = connection.createQuery(insertSql, true).addParameter("val", "something").executeUpdate().getKey(Long.class);
        assertNotNull(key);
        assertTrue(key > 0);
        String selectSql = "select id, val from test_table";
        Table resultTable = connection.createQuery(selectSql).executeAndFetchTable();
        assertThat(resultTable.rows().size(), is(1));
        Row resultRow = resultTable.rows().get(0);
        assertThat(resultRow.getLong("id"), equalTo(key));
        assertThat(resultRow.getString("val"), is("something"));
        // always rollback, as this is only for tesing purposes.
        connection.rollback();
    }
}
Also used : Table(org.sql2o.data.Table) Connection(org.sql2o.Connection) Row(org.sql2o.data.Row) Test(org.junit.Test)

Example 9 with Connection

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

the class PostgresTest method testKeyKeyOnSerial.

@Test
public void testKeyKeyOnSerial() {
    Connection connection = null;
    try {
        connection = sql2o.beginTransaction();
        String createTableSql = "create table test_serial_table (id serial primary key, val varchar(20))";
        connection.createQuery(createTableSql).executeUpdate();
        String insertSql = "insert into test_serial_table(val) values ('something')";
        Long key = connection.createQuery(insertSql, true).executeUpdate().getKey(Long.class);
        assertThat(key, equalTo(1L));
        key = connection.createQuery(insertSql, true).executeUpdate().getKey(Long.class);
        assertThat(key, equalTo(2L));
    } finally {
        if (connection != null) {
            connection.rollback();
        }
    }
}
Also used : Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 10 with Connection

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

the class PostgresTest method testIssue10StatementsOnPostgres_noTransaction.

@Test
public void testIssue10StatementsOnPostgres_noTransaction() {
    try {
        try (Connection connection = sql2o.open()) {
            connection.createQuery("create table test_table(id SERIAL, val varchar(20))").executeUpdate();
        }
        try (Connection connection = sql2o.open()) {
            Long key = connection.createQuery("insert into test_table (val) values(:val)", true).addParameter("val", "something").executeUpdate().getKey(Long.class);
            assertNotNull(key);
            assertTrue(key > 0);
            String selectSql = "select id, val from test_table";
            Table resultTable = connection.createQuery(selectSql).executeAndFetchTable();
            assertThat(resultTable.rows().size(), is(1));
            Row resultRow = resultTable.rows().get(0);
            assertThat(resultRow.getLong("id"), equalTo(key));
            assertThat(resultRow.getString("val"), is("something"));
        }
    } finally {
        try (final Connection connection = sql2o.open();
            final Query query = connection.createQuery("drop table if exists test_table")) {
            query.executeUpdate();
        }
    }
}
Also used : Table(org.sql2o.data.Table) Query(org.sql2o.Query) Connection(org.sql2o.Connection) Row(org.sql2o.data.Row) 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