Search in sources :

Example 1 with StatementUpdate

use of com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate 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 StatementUpdate

use of com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate in project cactoos-jdbc by fabriciofx.

the class ResultAsValueTest method insertWithKeys.

@Test
public void insertWithKeys() throws Exception {
    try (Server server = new ServerMysql()) {
        server.start();
        final Session session = server.session();
        new StatementUpdate(session, new QuerySimple(new Joined(" ", "CREATE TABLE contact (", "id INT AUTO_INCREMENT,", "name VARCHAR(50) NOT NULL,", "CONSTRAINT pk_contact PRIMARY KEY(id))"))).result();
        MatcherAssert.assertThat("Can't get a generated key value", new ResultAsValue<>(new StatementInsertKeyed<>(session, new QueryKeyed(() -> "INSERT INTO contact (name) VALUES (:name)", "id", new ParamText("name", "Leonardo da Vinci")))), new HasValue<>(BigInteger.ONE));
    }
}
Also used : StatementInsertKeyed(com.github.fabriciofx.cactoos.jdbc.statement.StatementInsertKeyed) ParamText(com.github.fabriciofx.cactoos.jdbc.param.ParamText) Server(com.github.fabriciofx.cactoos.jdbc.Server) StatementUpdate(com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate) ServerMysql(com.github.fabriciofx.cactoos.jdbc.server.ServerMysql) 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)

Aggregations

Session (com.github.fabriciofx.cactoos.jdbc.Session)2 QuerySimple (com.github.fabriciofx.cactoos.jdbc.query.QuerySimple)2 StatementUpdate (com.github.fabriciofx.cactoos.jdbc.statement.StatementUpdate)2 Test (org.junit.Test)2 Server (com.github.fabriciofx.cactoos.jdbc.Server)1 ParamText (com.github.fabriciofx.cactoos.jdbc.param.ParamText)1 QueryKeyed (com.github.fabriciofx.cactoos.jdbc.query.QueryKeyed)1 ServerMysql (com.github.fabriciofx.cactoos.jdbc.server.ServerMysql)1 Logged (com.github.fabriciofx.cactoos.jdbc.session.Logged)1 SessionNoAuth (com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth)1 SourceH2 (com.github.fabriciofx.cactoos.jdbc.source.SourceH2)1 StatementInsertKeyed (com.github.fabriciofx.cactoos.jdbc.statement.StatementInsertKeyed)1 Logger (java.util.logging.Logger)1 Joined (org.cactoos.text.Joined)1