Search in sources :

Example 6 with QuerySimple

use of com.github.fabriciofx.cactoos.jdbc.query.QuerySimple 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)

Example 7 with QuerySimple

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

the class ContactSql method about.

@Override
public String about() throws Exception {
    final String contact = new ResultSetAsValue<String>(new StatementSelect(this.session, new QuerySimple("SELECT name FROM contact WHERE id = :id", new ParamUuid("id", this.id)))).value();
    final String phones = new ResultSetAsXmlEach(new StatementSelect(this.session, new QuerySimple(new Joined(" ", "SELECT number, carrier FROM phone WHERE", "contact_id = :contact_id"), new ParamUuid("contact_id", this.id))), "phone").value();
    final String xml;
    if (phones.isEmpty()) {
        xml = "<contact><name>%s</name></contact>";
    } else {
        xml = "<contact><name>%s</name><phones>%s</phones></contact>";
    }
    return new FormattedText(xml, contact, phones).asString();
}
Also used : ParamUuid(com.github.fabriciofx.cactoos.jdbc.param.ParamUuid) StatementSelect(com.github.fabriciofx.cactoos.jdbc.statement.StatementSelect) ResultSetAsXmlEach(com.github.fabriciofx.cactoos.jdbc.result.ResultSetAsXmlEach) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) FormattedText(org.cactoos.text.FormattedText) Joined(org.cactoos.text.Joined)

Example 8 with QuerySimple

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

the class StatementBatchTest method batch.

@Test
public void batch() 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 client (id INT,", "name VARCHAR(50), age INT, PRIMARY KEY (id))"))).result();
            new StatementBatch(session, new QueryBatch(new Joined(" ", "INSERT INTO client (id, name, age)", "VALUES (:id, :name, :age)"), new ParamsNamed(new ParamInt("id", 1), new ParamText("name", "Jeff Bridges"), // @checkstyle MagicNumber (1 line)
            new ParamInt("age", 34)), new ParamsNamed(new ParamInt("id", 2), new ParamText("name", "Anna Miller"), // @checkstyle MagicNumber (1 line)
            new ParamInt("age", 26)), new ParamsNamed(// @checkstyle MagicNumber (3 lines)
            new ParamInt("id", 3), new ParamText("name", "Michal Douglas"), new ParamInt("age", 32)))).result();
        }
    }
}
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) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) QueryBatch(com.github.fabriciofx.cactoos.jdbc.query.QueryBatch) Joined(org.cactoos.text.Joined) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 9 with QuerySimple

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

the class StatementInsertTest method insert.

@Test
public void insert() 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 t01 (id INT, name VARCHAR(50),", "PRIMARY KEY (id))"))).result();
            MatcherAssert.assertThat("Can't insert into table", new ResultAsValue<>(new StatementInsert(session, new QuerySimple(new Joined(" ", "INSERT INTO t01 (id, name)", "VALUES (:id, :name)"), new ParamInt("id", 1), new ParamText("name", "Yegor Bugayenko")))), new HasValue<>(false));
        }
    }
}
Also used : ServerH2(com.github.fabriciofx.cactoos.jdbc.server.ServerH2) 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) QuerySimple(com.github.fabriciofx.cactoos.jdbc.query.QuerySimple) Joined(org.cactoos.text.Joined) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 10 with QuerySimple

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

the class StatementInsertTest method insertWithKeysMysql.

@Test
public void insertWithKeysMysql() throws Exception {
    try (Server server = new ServerMysql()) {
        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)", "id", new ParamText("name", "Jeff Malony")))), new HasValue<>(BigInteger.ONE));
    }
}
Also used : ParamText(com.github.fabriciofx.cactoos.jdbc.param.ParamText) Server(com.github.fabriciofx.cactoos.jdbc.Server) 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

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