Search in sources :

Example 1 with QuerySimple

use of com.github.fabriciofx.cactoos.jdbc.query.QuerySimple in project cactoos-jdbc by fabriciofx.

the class LoggedTest method loggedUpdate.

@Test
public void loggedUpdate() throws Exception {
    final Logger logger = new LoggerFake();
    final String sql = "CREATE TABLE t012 (id INT AUTO_INCREMENT, name VARCHAR(50))";
    final Session session = new Logged(new SessionNoAuth(new SourceH2("testdb")), "cactoos-jdbc", logger);
    new StatementUpdate(session, new QuerySimple(sql)).result();
    MatcherAssert.assertThat("Can't connection from cactoos-jdbc", logger.toString(), Matchers.allOf(Matchers.containsString("Connection[#0] has been opened with properties"), Matchers.containsString(String.format("PreparedStatement[#0] created using SQL '%s'", sql)), Matchers.containsString("PreparedStatement[#0] updated a source and returned '0' in"), Matchers.containsString("PreparedStatement[#0] closed"), Matchers.containsString("Connection[#0] closed")));
}
Also used : SourceH2(com.github.fabriciofx.cactoos.jdbc.source.SourceH2) StatementUpdate(com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) Logger(java.util.logging.Logger) SessionNoAuth(com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth) Logged(com.github.fabriciofx.cactoos.jdbc.session.Logged) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 2 with QuerySimple

use of com.github.fabriciofx.cactoos.jdbc.query.QuerySimple in project cactoos-jdbc by fabriciofx.

the class StatementInsertTest method insertWithKeysH2.

@Test
public // @checkstyle MethodNameCheck (1 line)
void insertWithKeysH2() throws Exception {
    try (Server server = new ServerH2()) {
        server.start();
        final Session session = server.session();
        new StatementUpdate(session, new QuerySimple(new Joined(" ", "CREATE TABLE t02 (id INT AUTO_INCREMENT,", "name VARCHAR(50), PRIMARY KEY (id))"))).result();
        MatcherAssert.assertThat("Can't insert with an integer keys", new ResultAsValue<>(new StatementInsertKeyed<>(session, new QueryKeyed("INSERT INTO t02 (name) VALUES (:name)", new ParamText("name", "Jeff Malony")))), new HasValue<>(1));
    }
}
Also used : ServerH2(com.github.fabriciofx.cactoos.jdbc.server.ServerH2) ParamText(com.github.fabriciofx.cactoos.jdbc.param.ParamText) Server(com.github.fabriciofx.cactoos.jdbc.Server) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) Joined(org.cactoos.text.Joined) QueryKeyed(com.github.fabriciofx.cactoos.jdbc.query.QueryKeyed) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 3 with QuerySimple

use of com.github.fabriciofx.cactoos.jdbc.query.QuerySimple in project cactoos-jdbc by fabriciofx.

the class StatementInsertTest method insertWithKeysPsql.

@Test
public void insertWithKeysPsql() throws Exception {
    try (Server server = new ServerPgsql()) {
        server.start();
        final Session session = server.session();
        new StatementUpdate(session, new QuerySimple(new Joined(" ", "CREATE TABLE t02 (id SERIAL,", "name VARCHAR(50), PRIMARY KEY (id))"))).result();
        MatcherAssert.assertThat("Can't insert with an integer keys", new ResultAsValue<>(new StatementInsertKeyed<>(session, new QueryKeyed("INSERT INTO t02 (name) VALUES (:name)", new ParamText("name", "Jeff Malony")))), new HasValue<>(1));
    }
}
Also used : ParamText(com.github.fabriciofx.cactoos.jdbc.param.ParamText) Server(com.github.fabriciofx.cactoos.jdbc.Server) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) ServerPgsql(com.github.fabriciofx.cactoos.jdbc.server.ServerPgsql) Joined(org.cactoos.text.Joined) QueryKeyed(com.github.fabriciofx.cactoos.jdbc.query.QueryKeyed) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 4 with QuerySimple

use of com.github.fabriciofx.cactoos.jdbc.query.QuerySimple in project cactoos-jdbc by fabriciofx.

the class StatementSelectTest method any.

@Test
public void any() throws Exception {
    try (Servers servers = new Servers(new ServerH2(), new ServerMysql(), new ServerPgsql())) {
        for (final Session session : servers.sessions()) {
            new StatementUpdate(session, new QuerySimple(new Joined(" ", "CREATE TABLE person (id INT, name VARCHAR(30),", "created_at DATE, city VARCHAR(20),", "working BOOLEAN, height DECIMAL(20,2),", "PRIMARY KEY (id))"))).result();
            new StatementBatch(session, new QueryBatch(new Joined(" ", "INSERT INTO person", "(id, name, created_at, city, working, height)", "VALUES (:id, :name, :created_at, :city,", ":working, :height)"), new ParamsNamed(new ParamInt("id", 1), new ParamText("name", "Rob Pike"), new ParamDate("created_at", LocalDate.now()), new ParamText("city", "San Francisco"), new ParamBool("working", true), new ParamDecimal("height", "1.86")), new ParamsNamed(new ParamInt("id", 2), new ParamText("name", "Ana Pivot"), new ParamDate("created_at", LocalDate.now()), new ParamText("city", "Washington"), new ParamBool("working", false), new ParamDecimal("height", "1.62")))).result();
            MatcherAssert.assertThat("Can't select a person name", new ResultSetAsValue<>(new StatementSelect(session, new QuerySimple("SELECT name FROM person"))), new HasValue<>("Rob Pike"));
        }
    }
}
Also used : ServerH2(com.github.fabriciofx.cactoos.jdbc.server.ServerH2) ParamsNamed(com.github.fabriciofx.cactoos.jdbc.params.ParamsNamed) ServerMysql(com.github.fabriciofx.cactoos.jdbc.server.ServerMysql) Servers(com.github.fabriciofx.cactoos.jdbc.Servers) ServerPgsql(com.github.fabriciofx.cactoos.jdbc.server.ServerPgsql) ParamInt(com.github.fabriciofx.cactoos.jdbc.param.ParamInt) ParamText(com.github.fabriciofx.cactoos.jdbc.param.ParamText) ParamBool(com.github.fabriciofx.cactoos.jdbc.param.ParamBool) ParamDecimal(com.github.fabriciofx.cactoos.jdbc.param.ParamDecimal) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) QueryBatch(com.github.fabriciofx.cactoos.jdbc.query.QueryBatch) ParamDate(com.github.fabriciofx.cactoos.jdbc.param.ParamDate) Joined(org.cactoos.text.Joined) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 5 with QuerySimple

use of com.github.fabriciofx.cactoos.jdbc.query.QuerySimple in project cactoos-jdbc by fabriciofx.

the class PhonebookSql method contact.

@Override
public Contact contact(final Map<String, String> properties) throws Exception {
    final UUID id = UUID.randomUUID();
    new StatementInsert(this.session, new QuerySimple("INSERT INTO contact (id, name) VALUES (:id, :name)", new ParamUuid("id", id), new ParamText("name", properties.get("name")))).result();
    return new ContactSql(this.session, id);
}
Also used : ParamUuid(com.github.fabriciofx.cactoos.jdbc.param.ParamUuid) ParamText(com.github.fabriciofx.cactoos.jdbc.param.ParamText) StatementInsert(com.github.fabriciofx.cactoos.jdbc.statement.StatementInsert) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) UUID(java.util.UUID)

Aggregations

QuerySimple (com.github.fabriciofx.cactoos.jdbc.query.QuerySimple)11 Session (com.github.fabriciofx.cactoos.jdbc.Session)9 ParamText (com.github.fabriciofx.cactoos.jdbc.param.ParamText)9 Joined (org.cactoos.text.Joined)9 Test (org.junit.Test)9 ServerMysql (com.github.fabriciofx.cactoos.jdbc.server.ServerMysql)6 ServerH2 (com.github.fabriciofx.cactoos.jdbc.server.ServerH2)5 ServerPgsql (com.github.fabriciofx.cactoos.jdbc.server.ServerPgsql)5 Server (com.github.fabriciofx.cactoos.jdbc.Server)4 Servers (com.github.fabriciofx.cactoos.jdbc.Servers)4 ParamInt (com.github.fabriciofx.cactoos.jdbc.param.ParamInt)4 QueryKeyed (com.github.fabriciofx.cactoos.jdbc.query.QueryKeyed)4 ParamsNamed (com.github.fabriciofx.cactoos.jdbc.params.ParamsNamed)3 QueryBatch (com.github.fabriciofx.cactoos.jdbc.query.QueryBatch)3 ParamBool (com.github.fabriciofx.cactoos.jdbc.param.ParamBool)2 ParamDate (com.github.fabriciofx.cactoos.jdbc.param.ParamDate)2 ParamDecimal (com.github.fabriciofx.cactoos.jdbc.param.ParamDecimal)2 ParamUuid (com.github.fabriciofx.cactoos.jdbc.param.ParamUuid)2 StatementUpdate (com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate)2 ResultSetAsXml (com.github.fabriciofx.cactoos.jdbc.result.ResultSetAsXml)1