├── .gitignore ├── src ├── test │ ├── resources │ │ └── simplelogger.properties │ └── java │ │ └── com │ │ └── github │ │ └── piotrkot │ │ └── oojdbc │ │ ├── package-info.java │ │ ├── outcomes │ │ ├── package-info.java │ │ ├── ColumnOutcomeTest.java │ │ ├── ListOutcomeTest.java │ │ ├── OutcomeTest.java │ │ └── SingleOutcomeTest.java │ │ ├── H2Source.java │ │ ├── UtcTest.java │ │ ├── JdbcSessionMySqlTest.java │ │ ├── JdbcSessionH2Test.java │ │ └── JdbcSessionPsqlTest.java └── main │ └── java │ └── com │ └── github │ └── piotrkot │ └── oojdbc │ ├── outcomes │ ├── package-info.java │ ├── ListOutcome.java │ ├── ColumnOutcome.java │ ├── StoredProcedureOutcome.java │ └── SingleOutcome.java │ ├── statements │ ├── package-info.java │ ├── Insert.java │ ├── Select.java │ ├── Update.java │ ├── Exec.java │ ├── ProcCall.java │ └── Args.java │ ├── Stmnt.java │ ├── package-info.java │ ├── Preparation.java │ ├── Sql.java │ ├── JdbcSession.java │ ├── JdbcSessionTx.java │ ├── Request.java │ ├── Connect.java │ ├── DefaultMappings.java │ ├── Utc.java │ └── Outcome.java ├── .travis.yml ├── LICENSE ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.idea 2 | *target 3 | *.iml 4 | -------------------------------------------------------------------------------- /src/test/resources/simplelogger.properties: -------------------------------------------------------------------------------- 1 | org.slf4j.simpleLogger.showDateTime=true 2 | org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: trusty 3 | jdk: 4 | - oraclejdk8 5 | script: 6 | - mvn clean package 7 | after_success: 8 | - mvn clean test coveralls:report -DrepoToken=$REPO_TOKEN -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2018, jcabi.com 2 | Copyright (c) 2021, github.com/piotrkot 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 1) Redistributions of source code must retain the above 8 | copyright notice, this list of conditions and the following 9 | disclaimer. 2) Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following 11 | disclaimer in the documentation and/or other materials provided 12 | with the distribution. 3) Neither the name of the jcabi.com nor 13 | the names of its contributors may be used to endorse or promote 14 | products derived from this software without specific prior written 15 | permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 | THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 | OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/outcomes/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | /** 33 | * JDBC outcomes. 34 | */ 35 | package com.github.piotrkot.oojdbc.outcomes; 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | /** 33 | * JDBC statements. 34 | */ 35 | package com.github.piotrkot.oojdbc.statements; 36 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | /** 33 | * JDBC wrapper, tests. 34 | * 35 | * @since 1.0 36 | */ 37 | package com.github.piotrkot.oojdbc; 38 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/outcomes/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | /** 33 | * JDBC wrapper outcomes, tests. 34 | * 35 | * @since 1.0 36 | */ 37 | package com.github.piotrkot.oojdbc.outcomes; 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Stmnt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.sql.Connection; 34 | 35 | /** 36 | * Arbitrary JDBC statement. 37 | * @param Type of expected result 38 | * @since 1.0 39 | */ 40 | public interface Stmnt { 41 | /** 42 | * Makes SQL statement or query. 43 | * @param conn Connection 44 | * @return Outcome of ResultSet 45 | * @throws Exception When fails 46 | */ 47 | T using(Connection conn) throws Exception; 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | /** 33 | * JDBC wrapper. 34 | * 35 | *

The only dependency you need is (check our latest version available 36 | * at jdbc.jcabi.com): 37 | * 38 | *

<depedency>
39 |  *   <groupId>com.jcabi</groupId>
40 |  *   <artifactId>jcabi-jdbc</artifactId>
41 |  * </dependency>
42 | * 43 | * @since 1.0 44 | * @see project website 45 | */ 46 | package com.github.piotrkot.oojdbc; 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Preparation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.sql.PreparedStatement; 34 | import java.sql.SQLException; 35 | 36 | /** 37 | * Preparation of a {@link java.sql.PreparedStatement}. 38 | * 39 | * @since 1.0 40 | */ 41 | public interface Preparation { 42 | 43 | /** 44 | * Prepares this statement. 45 | * @param stmt Statement to modify/prepare 46 | * @throws SQLException If something goes wrong inside 47 | */ 48 | void prepare(PreparedStatement stmt) throws SQLException; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Sql.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import lombok.RequiredArgsConstructor; 34 | import org.cactoos.iterable.IterableOf; 35 | import org.cactoos.iterable.Joined; 36 | 37 | /** 38 | * SQL command. 39 | * @since 1.0 40 | */ 41 | @RequiredArgsConstructor 42 | public final class Sql { 43 | /** 44 | * Parts of this SQL. 45 | */ 46 | private final Iterable parts; 47 | 48 | /** 49 | * Ctor. 50 | * @param part First part of SQL 51 | * @param parts Other parts of SQL 52 | */ 53 | public Sql(final String part, final String... parts) { 54 | this( 55 | new Joined<>(part, new IterableOf<>(parts)) 56 | ); 57 | } 58 | 59 | /** 60 | * String representation of the SQL parts joined with space. 61 | * @return SQL String. 62 | */ 63 | public String asString() { 64 | return String.join(" ", this.parts); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/JdbcSession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.sql.Connection; 34 | import java.sql.SQLException; 35 | import javax.sql.DataSource; 36 | import lombok.RequiredArgsConstructor; 37 | 38 | /** 39 | * JDBC session without the transaction block. 40 | * @param Type of expected result 41 | * @since 1.0 42 | */ 43 | @RequiredArgsConstructor 44 | public final class JdbcSession { 45 | 46 | /** 47 | * JDBC statement. 48 | */ 49 | private final Stmnt stmnt; 50 | 51 | /** 52 | * Makes SQL statement or query. 53 | * @param source Data source 54 | * @return Outcome of ResultSet 55 | * @throws SQLException When fails 56 | */ 57 | @SuppressWarnings("PMD.AvoidCatchingGenericException") 58 | public T using(final DataSource source) throws SQLException { 59 | try (Connection conn = source.getConnection()) { 60 | return this.stmnt.using(conn); 61 | // @checkstyle IllegalCatch (1 line) 62 | } catch (final Exception ex) { 63 | throw new SQLException(ex); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/JdbcSessionTx.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.sql.Connection; 34 | import java.sql.SQLException; 35 | import javax.sql.DataSource; 36 | import lombok.RequiredArgsConstructor; 37 | 38 | /** 39 | * JDBC session with transaction block. 40 | * @param Type of expected result 41 | * @since 1.0 42 | */ 43 | @RequiredArgsConstructor 44 | public final class JdbcSessionTx { 45 | 46 | /** 47 | * JDBC statement. 48 | */ 49 | private final Stmnt stmnt; 50 | 51 | /** 52 | * Makes SQL statement or query. 53 | * @param source Data source 54 | * @return Outcome of ResultSet 55 | * @throws SQLException When fails 56 | */ 57 | @SuppressWarnings("PMD.AvoidCatchingGenericException") 58 | public T using(final DataSource source) throws SQLException { 59 | final Connection conn = source.getConnection(); 60 | try { 61 | conn.setAutoCommit(false); 62 | final T result = this.stmnt.using(conn); 63 | conn.commit(); 64 | return result; 65 | // @checkstyle IllegalCatch (1 line) 66 | } catch (final Exception ex) { 67 | conn.rollback(); 68 | throw new SQLException(ex); 69 | } finally { 70 | conn.close(); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Request.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.sql.PreparedStatement; 34 | import java.sql.ResultSet; 35 | import java.sql.SQLException; 36 | 37 | /** 38 | * Request. 39 | * 40 | * @since 1.0 41 | */ 42 | public interface Request { 43 | 44 | /** 45 | * Execute. 46 | */ 47 | Request EXECUTE = new Request() { 48 | @Override 49 | public ResultSet fetch(final PreparedStatement stmt) 50 | throws SQLException { 51 | stmt.execute(); 52 | return stmt.getGeneratedKeys(); 53 | } 54 | }; 55 | 56 | /** 57 | * Execute update. 58 | */ 59 | Request EXECUTE_UPDATE = new Request() { 60 | @Override 61 | public ResultSet fetch(final PreparedStatement stmt) 62 | throws SQLException { 63 | stmt.executeUpdate(); 64 | return stmt.getGeneratedKeys(); 65 | } 66 | }; 67 | 68 | /** 69 | * Execute query. 70 | */ 71 | Request EXECUTE_QUERY = new Request() { 72 | @Override 73 | public ResultSet fetch(final PreparedStatement stmt) 74 | throws SQLException { 75 | return stmt.executeQuery(); 76 | } 77 | }; 78 | 79 | /** 80 | * Fetch result set from statement. 81 | * @param stmt The statement 82 | * @return The result set 83 | * @throws SQLException If some problem 84 | */ 85 | ResultSet fetch(PreparedStatement stmt) throws SQLException; 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/Insert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.statements; 32 | 33 | import com.github.piotrkot.oojdbc.Connect; 34 | import com.github.piotrkot.oojdbc.Outcome; 35 | import com.github.piotrkot.oojdbc.Request; 36 | import com.github.piotrkot.oojdbc.Sql; 37 | import com.github.piotrkot.oojdbc.Stmnt; 38 | import java.sql.Connection; 39 | import java.sql.PreparedStatement; 40 | import lombok.RequiredArgsConstructor; 41 | 42 | /** 43 | * JDBC insert. 44 | * @param Type of expected result 45 | * @since 1.0 46 | */ 47 | @RequiredArgsConstructor 48 | public final class Insert implements Stmnt { 49 | /** 50 | * SQL command. 51 | */ 52 | private final Sql sql; 53 | 54 | /** 55 | * Parameters to SQL command. 56 | */ 57 | private final Args args; 58 | 59 | /** 60 | * Outcome of ResultSet. 61 | */ 62 | private final Outcome outcome; 63 | 64 | /** 65 | * Ctor. 66 | * @param sql SQL command 67 | * @param outcome Outcome of ResultSet 68 | */ 69 | public Insert(final Sql sql, final Outcome outcome) { 70 | this(sql, new Args(), outcome); 71 | } 72 | 73 | @Override 74 | public T using(final Connection conn) throws Exception { 75 | final PreparedStatement stmt = new Connect.WithKeys(this.sql.asString()) 76 | .open(conn); 77 | this.args.prepare(stmt); 78 | return this.outcome.handle( 79 | Request.EXECUTE.fetch(stmt), 80 | stmt 81 | ); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/Select.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.statements; 32 | 33 | import com.github.piotrkot.oojdbc.Connect; 34 | import com.github.piotrkot.oojdbc.Outcome; 35 | import com.github.piotrkot.oojdbc.Request; 36 | import com.github.piotrkot.oojdbc.Sql; 37 | import com.github.piotrkot.oojdbc.Stmnt; 38 | import java.sql.Connection; 39 | import java.sql.PreparedStatement; 40 | import lombok.RequiredArgsConstructor; 41 | 42 | /** 43 | * JDBC select. 44 | * @param Type of expected result 45 | * @since 1.0 46 | */ 47 | @RequiredArgsConstructor 48 | public final class Select implements Stmnt { 49 | /** 50 | * SQL command. 51 | */ 52 | private final Sql sql; 53 | 54 | /** 55 | * Parameters to SQL command. 56 | */ 57 | private final Args args; 58 | 59 | /** 60 | * Outcome of ResultSet. 61 | */ 62 | private final Outcome outcome; 63 | 64 | /** 65 | * Ctor. 66 | * @param sql SQL command 67 | * @param outcome Outcome of ResultSet 68 | */ 69 | public Select(final Sql sql, final Outcome outcome) { 70 | this(sql, new Args(), outcome); 71 | } 72 | 73 | @Override 74 | public T using(final Connection conn) throws Exception { 75 | final PreparedStatement stmt = new Connect.Plain(this.sql.asString()) 76 | .open(conn); 77 | this.args.prepare(stmt); 78 | return this.outcome.handle( 79 | Request.EXECUTE_QUERY.fetch(stmt), 80 | stmt 81 | ); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/Update.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.statements; 32 | 33 | import com.github.piotrkot.oojdbc.Connect; 34 | import com.github.piotrkot.oojdbc.Outcome; 35 | import com.github.piotrkot.oojdbc.Request; 36 | import com.github.piotrkot.oojdbc.Sql; 37 | import com.github.piotrkot.oojdbc.Stmnt; 38 | import java.sql.Connection; 39 | import java.sql.PreparedStatement; 40 | import lombok.RequiredArgsConstructor; 41 | 42 | /** 43 | * JDBC update. 44 | * @param Type of expected result 45 | * @since 1.0 46 | */ 47 | @RequiredArgsConstructor 48 | public final class Update implements Stmnt { 49 | /** 50 | * SQL command. 51 | */ 52 | private final Sql sql; 53 | 54 | /** 55 | * Parameters to SQL command. 56 | */ 57 | private final Args args; 58 | 59 | /** 60 | * Outcome of ResultSet. 61 | */ 62 | private final Outcome outcome; 63 | 64 | /** 65 | * Ctor. 66 | * @param sql SQL command 67 | * @param outcome Outcome of ResultSet 68 | */ 69 | public Update(final Sql sql, final Outcome outcome) { 70 | this(sql, new Args(), outcome); 71 | } 72 | 73 | @Override 74 | public T using(final Connection conn) throws Exception { 75 | final PreparedStatement stmt = new Connect.WithKeys(this.sql.asString()) 76 | .open(conn); 77 | this.args.prepare(stmt); 78 | return this.outcome.handle( 79 | Request.EXECUTE_UPDATE.fetch(stmt), 80 | stmt 81 | ); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/Exec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.statements; 32 | 33 | import com.github.piotrkot.oojdbc.Connect; 34 | import com.github.piotrkot.oojdbc.Outcome; 35 | import com.github.piotrkot.oojdbc.Request; 36 | import com.github.piotrkot.oojdbc.Sql; 37 | import com.github.piotrkot.oojdbc.Stmnt; 38 | import java.sql.Connection; 39 | import java.sql.PreparedStatement; 40 | import lombok.RequiredArgsConstructor; 41 | 42 | /** 43 | * JDBC execute. 44 | * @since 1.0 45 | */ 46 | @RequiredArgsConstructor 47 | public final class Exec implements Stmnt { 48 | /** 49 | * SQL command. 50 | */ 51 | private final Sql sql; 52 | 53 | /** 54 | * Parameters to SQL command. 55 | */ 56 | private final Args args; 57 | 58 | /** 59 | * Ctor. 60 | * @param sql SQL command 61 | */ 62 | public Exec(final Sql sql) { 63 | this(sql, new Args()); 64 | } 65 | 66 | @Override 67 | public Void using(final Connection conn) throws Exception { 68 | final String vendor = conn.getMetaData().getDatabaseProductName(); 69 | final Connect connect; 70 | if (vendor.equalsIgnoreCase("mysql")) { 71 | connect = new Connect.WithKeys(this.sql.asString()); 72 | } else { 73 | connect = new Connect.Plain(this.sql.asString()); 74 | } 75 | final PreparedStatement stmt = connect.open(conn); 76 | this.args.prepare(stmt); 77 | return Outcome.VOID.handle( 78 | Request.EXECUTE.fetch(stmt), 79 | stmt 80 | ); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/outcomes/ListOutcome.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.Outcome; 34 | import java.sql.ResultSet; 35 | import java.sql.Statement; 36 | import java.util.LinkedList; 37 | import java.util.List; 38 | import lombok.EqualsAndHashCode; 39 | import lombok.ToString; 40 | 41 | /** 42 | * Outcome that returns a list. 43 | * 44 | *

Use it when you need a full collection: 45 | * 46 | *

 Collection<User> users = new JdbcSession(source)
47 |  *   .sql("SELECT * FROM user")
48 |  *   .select(
49 |  *     new ListOutcome<User>(
50 |  *       new ListOutcome.Mapping<User>() {
51 |  *         @Override
52 |  *         public User map(final ResultSet rset) throws SQLException {
53 |  *           return new User.Simple(rset.getLong(1), rset.getString(2));
54 |  *         }
55 |  *       }
56 |  *     )
57 |  *   );
58 | * 59 | * @since 1.0 60 | * @param Type of items 61 | */ 62 | @ToString 63 | @EqualsAndHashCode(of = "mapping") 64 | public final class ListOutcome implements Outcome> { 65 | 66 | /** 67 | * Mapping. 68 | */ 69 | private final ListOutcome.Mapping mapping; 70 | 71 | /** 72 | * Public ctor. 73 | * @param mpg Mapping 74 | */ 75 | public ListOutcome(final ListOutcome.Mapping mpg) { 76 | this.mapping = mpg; 77 | } 78 | 79 | @Override 80 | public List handle(final ResultSet rset, final Statement stmt) 81 | throws Exception { 82 | final List result = new LinkedList<>(); 83 | while (rset.next()) { 84 | result.add(this.mapping.map(rset)); 85 | } 86 | return result; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/outcomes/ColumnOutcome.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.Outcome; 34 | import com.github.piotrkot.oojdbc.Utc; 35 | import java.sql.ResultSet; 36 | import java.sql.Statement; 37 | import java.util.Collection; 38 | import java.util.Date; 39 | import java.util.LinkedList; 40 | import lombok.EqualsAndHashCode; 41 | import lombok.RequiredArgsConstructor; 42 | import lombok.ToString; 43 | 44 | /** 45 | * Outcome that returns first column. 46 | * 47 | *

Use it when you need the first column: 48 | * 49 | *

 Collection<Long> salaries = new JdbcSession(source)
50 |  *   .sql("SELECT salary FROM user")
51 |  *   .select(new ColumnOutcome<Long>(Long.class));
52 | * 53 | *

Supported types are: {@link String}, {@link Long}, {@link Boolean}, 54 | * {@link Byte}, {@link Date}, and {@link Utc}. 55 | * 56 | * @param Type of items 57 | * @since 1.0 58 | */ 59 | @ToString 60 | @EqualsAndHashCode 61 | @RequiredArgsConstructor 62 | public final class ColumnOutcome implements Outcome> { 63 | 64 | /** 65 | * Mapping. 66 | */ 67 | private final Mapping mapping; 68 | 69 | /** 70 | * Public ctor. 71 | * 72 | * @param tpe The type to convert to 73 | */ 74 | public ColumnOutcome(final Class tpe) { 75 | this(tpe, Outcome.DEFAULT_MAPPINGS); 76 | } 77 | 78 | /** 79 | * Public ctor. 80 | * 81 | * @param tpe The type to convert to 82 | * @param mps The mappings. 83 | */ 84 | public ColumnOutcome(final Class tpe, final Mappings mps) { 85 | this(mps.forType(tpe)); 86 | } 87 | 88 | @Override 89 | public Collection handle(final ResultSet rset, final Statement stmt) 90 | throws Exception { 91 | final Collection result = new LinkedList<>(); 92 | while (rset.next()) { 93 | result.add(this.mapping.map(rset)); 94 | } 95 | return result; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/outcomes/StoredProcedureOutcome.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.Outcome; 34 | import java.sql.CallableStatement; 35 | import java.sql.ResultSet; 36 | import java.sql.SQLException; 37 | import java.sql.Statement; 38 | import lombok.EqualsAndHashCode; 39 | import lombok.ToString; 40 | 41 | /** 42 | * Outcome of a stored procedure with OUT parameters. 43 | * @since 1.0 44 | * @param Type of the returned result, which has to be Object[] 45 | */ 46 | @ToString 47 | @EqualsAndHashCode 48 | public final class StoredProcedureOutcome implements Outcome { 49 | 50 | /** 51 | * OUT parameters' indexes. 52 | */ 53 | private final int[] indexes; 54 | 55 | /** 56 | * Ctor. 57 | * @param indexes Indexes of the OUT params. 58 | * Index count starts from 1. 59 | */ 60 | @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") 61 | public StoredProcedureOutcome(final int... indexes) { 62 | if (indexes.length == 0) { 63 | throw new IllegalArgumentException( 64 | "At least 1 OUT param's index needs to be specified!" 65 | ); 66 | } 67 | final int size = indexes.length; 68 | this.indexes = new int[size]; 69 | for (int idx = 0; idx < size; ++idx) { 70 | this.indexes[idx] = indexes[idx]; 71 | } 72 | } 73 | 74 | @Override 75 | @SuppressWarnings("unchecked") 76 | public T handle(final ResultSet rset, final Statement stmt) 77 | throws SQLException { 78 | final int params = this.indexes.length; 79 | final Object[] outs = new Object[params]; 80 | if (stmt instanceof CallableStatement) { 81 | for (int idx = 0; idx < params; ++idx) { 82 | outs[idx] = ((CallableStatement) stmt).getObject( 83 | this.indexes[idx] 84 | ); 85 | } 86 | } 87 | return (T) outs; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/outcomes/ColumnOutcomeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.H2Source; 34 | import com.github.piotrkot.oojdbc.JdbcSessionTx; 35 | import com.github.piotrkot.oojdbc.Outcome; 36 | import com.github.piotrkot.oojdbc.Sql; 37 | import com.github.piotrkot.oojdbc.statements.Args; 38 | import com.github.piotrkot.oojdbc.statements.Exec; 39 | import com.github.piotrkot.oojdbc.statements.Insert; 40 | import com.github.piotrkot.oojdbc.statements.Select; 41 | import java.util.Collection; 42 | import javax.sql.DataSource; 43 | import org.hamcrest.MatcherAssert; 44 | import org.hamcrest.Matchers; 45 | import org.junit.jupiter.api.Test; 46 | 47 | /** 48 | * Test case for {@link ColumnOutcome}. 49 | * 50 | * @since 1.0 51 | * @checkstyle ClassDataAbstractionCoupling (2 lines) 52 | */ 53 | final class ColumnOutcomeTest { 54 | /** 55 | * ColumnOutcome can return the first column. 56 | * @throws Exception If there is some problem inside 57 | */ 58 | @Test 59 | void retrievesFirstColumn() throws Exception { 60 | final DataSource source = new H2Source("i8o98"); 61 | new JdbcSessionTx<>( 62 | conn -> { 63 | new Exec( 64 | new Sql("CREATE TABLE foo (name VARCHAR(50))") 65 | ).using(conn); 66 | new Insert<>( 67 | new Sql("INSERT INTO foo (name) VALUES (?) "), 68 | new Args("Jeff Lebowski"), 69 | Outcome.VOID 70 | ).using(conn); 71 | return new Insert<>( 72 | new Sql("INSERT INTO foo (name) VALUES (?)"), 73 | new Args("Walter Sobchak"), 74 | Outcome.VOID 75 | ).using(conn); 76 | } 77 | ).using(source); 78 | final Collection names = new JdbcSessionTx<>( 79 | new Select<>( 80 | new Sql("SELECT name FROM foo"), 81 | new ColumnOutcome<>(String.class) 82 | ) 83 | ).using(source); 84 | MatcherAssert.assertThat( 85 | names, Matchers.hasSize(2) 86 | ); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/ProcCall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.statements; 32 | 33 | import com.github.piotrkot.oojdbc.Connect; 34 | import com.github.piotrkot.oojdbc.Outcome; 35 | import com.github.piotrkot.oojdbc.Preparation; 36 | import com.github.piotrkot.oojdbc.Request; 37 | import com.github.piotrkot.oojdbc.Sql; 38 | import com.github.piotrkot.oojdbc.Stmnt; 39 | import java.sql.Connection; 40 | import java.sql.PreparedStatement; 41 | import lombok.RequiredArgsConstructor; 42 | 43 | /** 44 | * JDBC procedure call. 45 | * @param Type of expected result 46 | * @since 1.0 47 | */ 48 | @RequiredArgsConstructor 49 | public final class ProcCall implements Stmnt { 50 | /** 51 | * SQL command. 52 | */ 53 | private final Sql sql; 54 | 55 | /** 56 | * Parameters to SQL command. 57 | */ 58 | private final Args args; 59 | 60 | /** 61 | * Output parameters preparation. 62 | */ 63 | private final Preparation prep; 64 | 65 | /** 66 | * Outcome of ResultSet. 67 | */ 68 | private final Outcome outcome; 69 | 70 | /** 71 | * Ctor. 72 | * @param sql SQL command 73 | * @param prep Preparation for output 74 | * @param outcome Outcome of ResultSet 75 | */ 76 | public ProcCall(final Sql sql, final Preparation prep, 77 | final Outcome outcome) { 78 | this(sql, new Args(), prep, outcome); 79 | } 80 | 81 | /** 82 | * Ctor. 83 | * @param sql SQL command 84 | * @param args Preparation for input 85 | * @param outcome Outcome of ResultSet 86 | */ 87 | public ProcCall(final Sql sql, final Args args, 88 | final Outcome outcome) { 89 | this(sql, args, stmt -> { 90 | }, outcome); 91 | } 92 | 93 | @Override 94 | public T using(final Connection conn) throws Exception { 95 | final PreparedStatement stmt = new Connect.Call(this.sql.asString()) 96 | .open(conn); 97 | this.args.prepare(stmt); 98 | this.prep.prepare(stmt); 99 | return this.outcome.handle( 100 | Request.EXECUTE_UPDATE.fetch(stmt), 101 | stmt 102 | ); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/outcomes/ListOutcomeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.H2Source; 34 | import com.github.piotrkot.oojdbc.JdbcSession; 35 | import com.github.piotrkot.oojdbc.JdbcSessionTx; 36 | import com.github.piotrkot.oojdbc.Outcome; 37 | import com.github.piotrkot.oojdbc.Sql; 38 | import com.github.piotrkot.oojdbc.statements.Args; 39 | import com.github.piotrkot.oojdbc.statements.Exec; 40 | import com.github.piotrkot.oojdbc.statements.Insert; 41 | import com.github.piotrkot.oojdbc.statements.Select; 42 | import java.util.List; 43 | import javax.sql.DataSource; 44 | import org.hamcrest.MatcherAssert; 45 | import org.hamcrest.Matchers; 46 | import org.junit.jupiter.api.Test; 47 | 48 | /** 49 | * Test case for {@link ListOutcome}. 50 | * @since 1.0 51 | * @checkstyle ClassDataAbstractionCoupling (2 lines) 52 | */ 53 | final class ListOutcomeTest { 54 | 55 | /** 56 | * ListOutcome can return the full list. 57 | * @throws Exception If there is some problem inside 58 | */ 59 | @Test 60 | void retrievesList() throws Exception { 61 | final DataSource source = new H2Source("tto98"); 62 | new JdbcSessionTx<>( 63 | conn -> { 64 | new Exec( 65 | new Sql("CREATE TABLE foo (name VARCHAR(50))") 66 | ).using(conn); 67 | new Insert<>( 68 | new Sql("INSERT INTO foo (name) VALUES (?) "), 69 | new Args("Jeff Lebowski"), 70 | Outcome.VOID 71 | ).using(conn); 72 | return new Insert<>( 73 | new Sql("INSERT INTO foo (name) VALUES (?)"), 74 | new Args("Walter Sobchak"), 75 | Outcome.VOID 76 | ).using(conn); 77 | } 78 | ).using(source); 79 | final List names = new JdbcSession<>( 80 | new Select<>( 81 | new Sql("SELECT name FROM foo"), 82 | new ListOutcome<>( 83 | rset -> rset.getString(1) 84 | ) 85 | ) 86 | ).using(source); 87 | MatcherAssert.assertThat( 88 | names, Matchers.hasSize(2) 89 | ); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/statements/Args.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.statements; 32 | 33 | import com.github.piotrkot.oojdbc.Preparation; 34 | import com.github.piotrkot.oojdbc.Utc; 35 | import java.sql.Date; 36 | import java.sql.PreparedStatement; 37 | import java.sql.SQLException; 38 | import java.sql.Types; 39 | import lombok.RequiredArgsConstructor; 40 | import org.cactoos.iterable.IterableOf; 41 | import org.cactoos.iterable.Joined; 42 | 43 | /** 44 | * Arguments to SQL. 45 | * @since 1.0 46 | */ 47 | @RequiredArgsConstructor 48 | public final class Args implements Preparation { 49 | /** 50 | * Arguments. 51 | */ 52 | private final Iterable arguments; 53 | 54 | /** 55 | * Ctor. 56 | * @param part First argument 57 | * @param parts Other arguments 58 | */ 59 | public Args(final Object part, final Object... parts) { 60 | this( 61 | new Joined<>( 62 | part, new IterableOf<>(parts) 63 | ) 64 | ); 65 | } 66 | 67 | /** 68 | * Ctor. 69 | */ 70 | Args() { 71 | this(new IterableOf<>()); 72 | } 73 | 74 | @Override 75 | @SuppressWarnings("PMD.CyclomaticComplexity") 76 | public void prepare(final PreparedStatement stmt) throws SQLException { 77 | int pos = 1; 78 | for (final Object arg : this.arguments) { 79 | if (arg == null) { 80 | stmt.setNull(pos, Types.NULL); 81 | } else if (arg instanceof Long) { 82 | stmt.setLong(pos, (Long) arg); 83 | } else if (arg instanceof Boolean) { 84 | stmt.setBoolean(pos, (Boolean) arg); 85 | } else if (arg instanceof Date) { 86 | stmt.setDate(pos, (Date) arg); 87 | } else if (arg instanceof Integer) { 88 | stmt.setInt(pos, (Integer) arg); 89 | } else if (arg instanceof Utc) { 90 | ((Utc) arg).setTimestamp(stmt, pos); 91 | } else if (arg instanceof Float) { 92 | stmt.setFloat(pos, (Float) arg); 93 | } else if (arg instanceof byte[]) { 94 | stmt.setBytes(pos, (byte[]) arg); 95 | } else { 96 | stmt.setObject(pos, arg); 97 | } 98 | ++pos; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/H2Source.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.io.PrintWriter; 34 | import java.sql.Connection; 35 | import java.sql.Driver; 36 | import java.sql.SQLException; 37 | import java.util.Properties; 38 | import java.util.logging.Logger; 39 | import javax.sql.DataSource; 40 | import lombok.EqualsAndHashCode; 41 | import lombok.ToString; 42 | 43 | /** 44 | * H2 data source, for unit testing. 45 | * 46 | * @since 1.0 47 | */ 48 | @ToString 49 | @EqualsAndHashCode(of = "name") 50 | public final class H2Source implements DataSource { 51 | 52 | /** 53 | * H2 driver. 54 | */ 55 | private static final Driver DRIVER = new org.h2.Driver(); 56 | 57 | /** 58 | * Unique name of the DB. 59 | */ 60 | private final String name; 61 | 62 | /** 63 | * Public ctor. 64 | * @param dbname DB name 65 | */ 66 | public H2Source(final String dbname) { 67 | this.name = dbname; 68 | } 69 | 70 | @Override 71 | public Connection getConnection() throws SQLException { 72 | return H2Source.DRIVER.connect( 73 | String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", this.name), 74 | new Properties() 75 | ); 76 | } 77 | 78 | @Override 79 | public Connection getConnection(final String username, 80 | final String password) { 81 | throw new UnsupportedOperationException("#getConnection()"); 82 | } 83 | 84 | @Override 85 | public PrintWriter getLogWriter() { 86 | throw new UnsupportedOperationException("#getLogWriter()"); 87 | } 88 | 89 | @Override 90 | public void setLogWriter(final PrintWriter writer) { 91 | throw new UnsupportedOperationException("#setLogWriter()"); 92 | } 93 | 94 | @Override 95 | public void setLoginTimeout(final int seconds) { 96 | throw new UnsupportedOperationException("#setLoginTimeout()"); 97 | } 98 | 99 | @Override 100 | public int getLoginTimeout() { 101 | throw new UnsupportedOperationException("#getLoginTimeout()"); 102 | } 103 | 104 | @Override 105 | public Logger getParentLogger() { 106 | throw new UnsupportedOperationException("#getParentLogger()"); 107 | } 108 | 109 | @Override 110 | public T unwrap(final Class iface) { 111 | throw new UnsupportedOperationException("#unwrap()"); 112 | } 113 | 114 | @Override 115 | public boolean isWrapperFor(final Class iface) { 116 | throw new UnsupportedOperationException("#isWrapperFor()"); 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/outcomes/OutcomeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.H2Source; 34 | import com.github.piotrkot.oojdbc.JdbcSession; 35 | import com.github.piotrkot.oojdbc.Outcome; 36 | import com.github.piotrkot.oojdbc.Sql; 37 | import com.github.piotrkot.oojdbc.statements.Args; 38 | import com.github.piotrkot.oojdbc.statements.Exec; 39 | import com.github.piotrkot.oojdbc.statements.Insert; 40 | import com.github.piotrkot.oojdbc.statements.Update; 41 | import javax.sql.DataSource; 42 | import org.hamcrest.MatcherAssert; 43 | import org.hamcrest.Matchers; 44 | import org.junit.jupiter.api.Test; 45 | 46 | /** 47 | * Test case for {@link Outcome}. 48 | * @since 1.0 49 | */ 50 | final class OutcomeTest { 51 | 52 | /** 53 | * Outcome can fetch last insert id. 54 | * @throws Exception If there is some problem inside 55 | */ 56 | @Test 57 | void fetchesLastInsertId() throws Exception { 58 | final DataSource source = new H2Source("trrto98"); 59 | final Long num = new JdbcSession<>( 60 | conn -> { 61 | new Exec( 62 | new Sql( 63 | "CREATE TABLE foo (id INT auto_increment, name VARCHAR(50))" 64 | ) 65 | ).using(conn); 66 | return new Insert<>( 67 | new Sql("INSERT INTO foo (name) VALUES (?)"), 68 | new Args("Jeff Lebowski"), 69 | Outcome.LAST_INSERT_ID 70 | ).using(conn); 71 | } 72 | ).using(source); 73 | MatcherAssert.assertThat(num, Matchers.equalTo(1L)); 74 | } 75 | 76 | /** 77 | * Outcome can count updates. 78 | * @throws Exception If there is some problem inside 79 | */ 80 | @Test 81 | void countsUpdates() throws Exception { 82 | final DataSource source = new H2Source("xogaa98"); 83 | final int num = new JdbcSession<>( 84 | conn -> { 85 | new Exec( 86 | new Sql( 87 | "CREATE TABLE boo (id INT auto_increment, name VARCHAR(50))" 88 | ) 89 | ).using(conn); 90 | new Insert<>( 91 | new Sql("INSERT INTO boo (name) VALUES (?)"), 92 | new Args("Jeff Brown"), 93 | Outcome.LAST_INSERT_ID 94 | ).using(conn); 95 | return new Update<>( 96 | new Sql("UPDATE boo SET name = ? WHERE id = 1"), 97 | new Args("Mark Smith"), 98 | Outcome.UPDATE_COUNT 99 | ).using(conn); 100 | } 101 | ).using(source); 102 | MatcherAssert.assertThat(num, Matchers.equalTo(1)); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Connect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 34 | import java.sql.Connection; 35 | import java.sql.PreparedStatement; 36 | import java.sql.SQLException; 37 | import java.sql.Statement; 38 | 39 | /** 40 | * Connect. 41 | * 42 | * @since 1.0 43 | */ 44 | public interface Connect { 45 | 46 | /** 47 | * Create prepare statement. 48 | * 49 | * @param conn Open connection 50 | * @return The statement 51 | * @throws SQLException If some problem 52 | */ 53 | PreparedStatement open(Connection conn) throws SQLException; 54 | 55 | /** 56 | * Connect which opens a CallableStatement, which 57 | * is used for calling stored procedures. 58 | * 59 | * @since 1.0 60 | */ 61 | final class Call implements Connect { 62 | /** 63 | * SQL function call. 64 | */ 65 | private final String sql; 66 | 67 | /** 68 | * Ctor. 69 | * 70 | * @param query Query 71 | */ 72 | public Call(final String query) { 73 | this.sql = query; 74 | } 75 | 76 | @Override 77 | public PreparedStatement open(final Connection conn) throws SQLException { 78 | return conn.prepareCall(this.sql); 79 | } 80 | } 81 | 82 | /** 83 | * Plain, without keys. 84 | * 85 | * @since 1.0 86 | */ 87 | final class Plain implements Connect { 88 | /** 89 | * SQL query. 90 | */ 91 | private final String sql; 92 | 93 | /** 94 | * Ctor. 95 | * 96 | * @param query Query 97 | */ 98 | public Plain(final String query) { 99 | this.sql = query; 100 | } 101 | 102 | @Override 103 | @SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING") 104 | public PreparedStatement open(final Connection conn) throws SQLException { 105 | return conn.prepareStatement(this.sql); 106 | } 107 | } 108 | 109 | /** 110 | * With returned keys. 111 | * 112 | * @since 1.0 113 | */ 114 | final class WithKeys implements Connect { 115 | /** 116 | * SQL query. 117 | */ 118 | private final String sql; 119 | 120 | /** 121 | * Ctor. 122 | * 123 | * @param query Query 124 | */ 125 | public WithKeys(final String query) { 126 | this.sql = query; 127 | } 128 | 129 | @Override 130 | @SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING") 131 | public PreparedStatement open(final Connection conn) throws SQLException { 132 | return conn.prepareStatement( 133 | this.sql, 134 | Statement.RETURN_GENERATED_KEYS 135 | ); 136 | } 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![Coverage Status](https://coveralls.io/repos/github/piotrkot/oojdbc/badge.svg?branch=master)](https://coveralls.io/github/piotrkot/oojdbc?branch=main) 3 | 4 | # Object-oriented wrapper for JDBC 5 | 6 | Simple object-oriented wrapper around JDBC. This is a fork of [jcabi-jdbc](https://jdbc.jcabi.com/). 7 | 8 | ## SQL queries 9 | 10 | Single value queries 11 | 12 | ```java 13 | String name = new JdbcSession<>( 14 | new Select<>( 15 | new Sql("SELECT name FROM users"), 16 | new SingleOutcome<>(String.class) 17 | ) 18 | ).using(datasource); 19 | ``` 20 | 21 | Collection queries 22 | 23 | ```java 24 | List names = new JdbcSession<>( 25 | new Select<>( 26 | new Sql( 27 | "SELECT name FROM users", 28 | "WHERE age > ?" 29 | ), 30 | new Args(20), 31 | new ColumnOutcome<>(String.class) 32 | ) 33 | ).using(datasource); 34 | ``` 35 | 36 | Custom collection queries 37 | 38 | ```java 39 | List kids = new JdbcSession<>( 40 | new Select<>( 41 | new Sql( 42 | "SELECT name, age FROM users", 43 | "WHERE age < ?" 44 | ), 45 | new Args(10), 46 | new ListOutcome<>( 47 | rset -> new User( 48 | rset.getString("name"), 49 | rset.getInt("age") 50 | ); 51 | ) 52 | ) 53 | ).using(datasource); 54 | 55 | @RequiredArgsConstructor 56 | class User { 57 | private final String name; 58 | private final int age; 59 | } 60 | ``` 61 | 62 | ## Insert/Update statements 63 | 64 | Single table inserts 65 | 66 | ```java 67 | long id = new JdbcSession<>( 68 | new Insert<>( 69 | new Sql( 70 | "INSERT INTO users (name, age)", 71 | "VALUES (?, ?)" 72 | ), 73 | new Args("Mark", 32), 74 | Outcome.LAST_INSERT_ID 75 | ) 76 | ).using(datasource); 77 | ``` 78 | 79 | Multiple (transactional) table updates 80 | 81 | ```java 82 | new JdbcSessionTx<>( 83 | conn -> { 84 | long id = new Insert<>( 85 | new Sql( 86 | "INSERT INTO users (name, age)", 87 | "VALUES (?, ?)" 88 | ), 89 | new Args("Mark", 32), 90 | Outcome.LAST_INSERT_ID 91 | ).using(conn); 92 | return new Insert<>( 93 | new Sql( 94 | "INSERT INTO family (user)", 95 | "VALUES (?)" 96 | ), 97 | new Args(id), 98 | Outcome.VOID 99 | ).using(conn); 100 | } 101 | ).using(datasource); 102 | ``` 103 | 104 | ## Other statements 105 | 106 | Table creation 107 | 108 | ```java 109 | new JdbcSession<>( 110 | new Exec( 111 | new Sql( 112 | "CREATE TABLE IF NOT EXISTS foo (name VARCHAR(50))" 113 | ) 114 | ) 115 | ).using(datasource); 116 | ``` 117 | 118 | Procedure creation and call 119 | 120 | ```java 121 | final Object[] result = new JdbcSession<>( 122 | conn -> { 123 | new Exec( 124 | new Sql( 125 | "CREATE PROCEDURE proc(OUT username text, OUT day date) ", 126 | "BEGIN SELECT name, CURDATE() INTO username, day ", 127 | "FROM users; ", 128 | "SELECT username, day; ", 129 | "END" 130 | ) 131 | ).using(conn); 132 | return new ProcCall<>( 133 | new Sql("CALL proc(?, ?)"), 134 | stmt -> { 135 | ((CallableStatement) stmt).registerOutParameter(1, Types.VARCHAR); 136 | ((CallableStatement) stmt).registerOutParameter(2, Types.DATE); 137 | }, 138 | new StoredProcedureOutcome(1, 2) 139 | ).using(conn); 140 | } 141 | ).using(datasource); 142 | ``` 143 | 144 | Please, note the library is still in early development and it's API can 145 | frequently change. 146 | 147 | To get started, add dependency to your project: 148 | ```xml 149 | 150 | com.github.piotrkot 151 | oojdbc 152 | 1.4.0 153 | 154 | ``` 155 | 156 | Feel free to fork me on GitHub, report bugs or post comments. 157 | 158 | For Pull Requests, please run `mvn clean package`, first. 159 | 160 | Tests depend on [testcontainers](https://www.testcontainers.org/). To successfully 161 | run them, you must have Docker installed. Since the Docker daemon always runs as 162 | the root user it may be tempting to create and give access to the 'docker' group. 163 | This however, may give [permanent and non-password protected root access](https://fosterelli.co/privilege-escalation-via-docker.html). 164 | Instead, it's better to run `sudo mvn clean package -Dmaven.repo.local=/home/user/.m2/repository`. 165 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/DefaultMappings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.math.BigDecimal; 34 | import java.util.AbstractMap; 35 | import java.util.Date; 36 | import java.util.Map; 37 | import java.util.stream.Collectors; 38 | import java.util.stream.Stream; 39 | import lombok.AccessLevel; 40 | import lombok.RequiredArgsConstructor; 41 | 42 | /** 43 | * Default mappings for types. 44 | * 45 | * @since 1.0 46 | */ 47 | @RequiredArgsConstructor(access = AccessLevel.PRIVATE) 48 | final class DefaultMappings implements Outcome.Mappings { 49 | /** 50 | * Per-type extraction methods. 51 | */ 52 | private final Map, Outcome.Mapping> map; 53 | 54 | /** 55 | * Ctor. 56 | */ 57 | DefaultMappings() { 58 | this(1); 59 | } 60 | 61 | /** 62 | * Ctor. 63 | * 64 | * @param column Column position. 65 | */ 66 | DefaultMappings(final int column) { 67 | this( 68 | new AbstractMap.SimpleImmutableEntry<>( 69 | String.class, rs -> rs.getString(column) 70 | ), 71 | new AbstractMap.SimpleImmutableEntry<>( 72 | Long.class, rs -> rs.getLong(column) 73 | ), 74 | new AbstractMap.SimpleImmutableEntry<>( 75 | Boolean.class, rs -> rs.getBoolean(column) 76 | ), 77 | new AbstractMap.SimpleImmutableEntry<>( 78 | Byte.class, rs -> rs.getByte(column) 79 | ), 80 | new AbstractMap.SimpleImmutableEntry<>( 81 | Date.class, rs -> rs.getDate(column) 82 | ), 83 | new AbstractMap.SimpleImmutableEntry<>( 84 | Utc.class, rs -> new Utc(Utc.getTimestamp(rs, column)) 85 | ), 86 | new AbstractMap.SimpleImmutableEntry<>( 87 | byte[].class, rs -> rs.getBytes(column) 88 | ), 89 | new AbstractMap.SimpleImmutableEntry<>( 90 | BigDecimal.class, rs -> rs.getBigDecimal(column) 91 | ) 92 | ); 93 | } 94 | 95 | /** 96 | * Ctor. 97 | * 98 | * @param mappings Mappings. 99 | */ 100 | @SafeVarargs 101 | private DefaultMappings( 102 | final Map.Entry, Outcome.Mapping>... mappings 103 | ) { 104 | this( 105 | Stream 106 | .of(mappings) 107 | .collect( 108 | Collectors.toMap( 109 | Map.Entry::getKey, 110 | Map.Entry::getValue 111 | ) 112 | ) 113 | ); 114 | } 115 | 116 | @Override 117 | @SuppressWarnings("unchecked") 118 | public Outcome.Mapping forType(final Class tpe) { 119 | if (!this.map.containsKey(tpe)) { 120 | throw new IllegalArgumentException( 121 | String.format("type %s is not supported", tpe.getName()) 122 | ); 123 | } 124 | return (Outcome.Mapping) this.map.get(tpe); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Utc.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 34 | import java.sql.PreparedStatement; 35 | import java.sql.ResultSet; 36 | import java.sql.SQLException; 37 | import java.sql.Timestamp; 38 | import java.util.Calendar; 39 | import java.util.Date; 40 | import java.util.SimpleTimeZone; 41 | import lombok.EqualsAndHashCode; 42 | import lombok.ToString; 43 | 44 | /** 45 | * UTC time zone manipulator. 46 | * 47 | *

When it's necessary to save date/time to the DB in UTC timezone, use 48 | * this class: 49 | * 50 | *

 new JdbcSession(source)
 51 |  *   .sql("INSERT INTO payment (amount, date) VALUES (?, ?)")
 52 |  *   .set(500)
 53 |  *   .set(new Utc()) // current date to be set, in UTC timezone
 54 |  *   .insert(Outcome.VOID);
55 | * 56 | *

This class also helps during date/time retrieval: 57 | * 58 | *

 Date date = new JdbcSession(source)
 59 |  *   .sql("SELECT date FROM payment WHERE id = 555")
 60 |  *   .select(
 61 |  *     new Outcome<Date>() {
 62 |  *       @Override
 63 |  *       public Date handle(final ResultSet rset) throws SQLException {
 64 |  *         return Utc.getTimestamp(rset, 1);
 65 |  *       }
 66 |  *     }
 67 |  *   );
68 | * 69 | *

{@link Timestamp} is used because {@link java.sql.Date} 70 | * supports only dates (without time). 71 | * 72 | * @since 1.0 73 | */ 74 | @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") 75 | @ToString 76 | @EqualsAndHashCode(of = "date") 77 | public final class Utc { 78 | 79 | /** 80 | * The calendar to use. 81 | */ 82 | private static final Calendar CALENDAR = 83 | Calendar.getInstance(new SimpleTimeZone(0, "UTC")); 84 | 85 | /** 86 | * The date to work with. 87 | */ 88 | private final long date; 89 | 90 | /** 91 | * Public ctor, with current date. 92 | */ 93 | public Utc() { 94 | this(new Date()); 95 | } 96 | 97 | /** 98 | * Public ctor. 99 | * @param when The date to use. 100 | */ 101 | public Utc(final Date when) { 102 | this.date = when.getTime(); 103 | } 104 | 105 | /** 106 | * Get date that is encapsulated. 107 | * @return The date 108 | */ 109 | public Date getDate() { 110 | return new Date(this.date); 111 | } 112 | 113 | /** 114 | * Convert date to timestamp and save to the statement. 115 | * @param stmt The statement 116 | * @param pos Position in the statement 117 | * @throws SQLException If some SQL problem inside 118 | */ 119 | public void setTimestamp(final PreparedStatement stmt, final int pos) 120 | throws SQLException { 121 | stmt.setTimestamp( 122 | pos, 123 | new Timestamp(this.date), 124 | Utc.CALENDAR 125 | ); 126 | } 127 | 128 | /** 129 | * Retrieve timestamp from the result set. 130 | * @param rset The result set 131 | * @param pos Position in the result set 132 | * @return The date 133 | * @throws SQLException If some SQL problem inside 134 | */ 135 | @SuppressWarnings("PMD.ProhibitPublicStaticMethods") 136 | public static Date getTimestamp(final ResultSet rset, final int pos) 137 | throws SQLException { 138 | final Timestamp stamp = rset.getTimestamp(pos, Utc.CALENDAR); 139 | Date when = null; 140 | if (stamp != null) { 141 | when = new Date(stamp.getTime()); 142 | } 143 | return when; 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/outcomes/SingleOutcome.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.Outcome; 34 | import com.github.piotrkot.oojdbc.Utc; 35 | import java.sql.ResultSet; 36 | import java.sql.SQLException; 37 | import java.sql.Statement; 38 | import java.util.Date; 39 | import lombok.EqualsAndHashCode; 40 | import lombok.RequiredArgsConstructor; 41 | import lombok.ToString; 42 | 43 | /** 44 | * Outcome that returns first column in the first row. 45 | * 46 | *

Use it when you need the first column in the first row: 47 | * 48 | *

 Long id = new JdbcSession(source)
 49 |  *   .sql("SELECT id FROM user WHERE name = ?")
 50 |  *   .set("Jeff Lebowski")
 51 |  *   .select(new SingleOutcome<Long>(Long.class));
52 | * 53 | *

Supported types are: {@link String}, {@link Long}, {@link Boolean}, 54 | * {@link Byte}, {@link Date}, and {@link Utc}. 55 | * 56 | *

By default, the outcome throws {@link SQLException} if no records 57 | * are found in the {@link ResultSet}. You can change this behavior by using 58 | * a two-arguments constructor ({@code null} will be returned if 59 | * {@link ResultSet} is empty): 60 | * 61 | *

 String name = new JdbcSession(source)
 62 |  *   .sql("SELECT name FROM user WHERE id = ?")
 63 |  *   .set(555)
 64 |  *   .select(new SingleOutcome<Long>(Long.class), true);
 65 |  * if (name == null) {
 66 |  *   // such a record wasn't found in the database
 67 |  * }
68 | * 69 | * @param Type of items 70 | * @since 1.0 71 | */ 72 | @ToString 73 | @EqualsAndHashCode(of = {"mapping", "silently"}) 74 | @RequiredArgsConstructor 75 | public final class SingleOutcome implements Outcome { 76 | 77 | /** 78 | * The type. 79 | */ 80 | private final Mapping mapping; 81 | 82 | /** 83 | * Silently return NULL if no row found. 84 | */ 85 | private final boolean silently; 86 | 87 | /** 88 | * Public ctor. 89 | * 90 | * @param tpe The type to convert to 91 | */ 92 | public SingleOutcome(final Class tpe) { 93 | this(tpe, false); 94 | } 95 | 96 | /** 97 | * Public ctor. 98 | * 99 | * @param tpe The type to convert to 100 | * @param slnt Silently return NULL if there is no row 101 | */ 102 | @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") 103 | public SingleOutcome(final Class tpe, final boolean slnt) { 104 | this( 105 | tpe, 106 | Outcome.DEFAULT_MAPPINGS, 107 | slnt 108 | ); 109 | } 110 | 111 | /** 112 | * Public ctor. 113 | * 114 | * @param tpe The type to convert to 115 | * @param mps The mappings 116 | * @param slnt Silently return NULL if there is no row 117 | */ 118 | public SingleOutcome(final Class tpe, final Mappings mps, final boolean slnt) { 119 | this(mps.forType(tpe), slnt); 120 | } 121 | 122 | @Override 123 | public T handle(final ResultSet rset, final Statement stmt) 124 | throws Exception { 125 | T result = null; 126 | if (rset.next()) { 127 | result = this.fetch(rset); 128 | } else if (!this.silently) { 129 | throw new SQLException("no records found"); 130 | } 131 | return result; 132 | } 133 | 134 | /** 135 | * Fetch the value from result set. 136 | * 137 | * @param rset Result set 138 | * @return The result 139 | * @throws Exception If some error inside 140 | */ 141 | private T fetch(final ResultSet rset) throws Exception { 142 | return this.mapping.map(rset); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/com/github/piotrkot/oojdbc/Outcome.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import java.sql.ResultSet; 34 | import java.sql.SQLException; 35 | import java.sql.Statement; 36 | 37 | /** 38 | * Outcome of ResultSet. 39 | * 40 | *

The following convenience implementations are provided: 41 | * 42 | *

    43 | *
  • {@link Outcome#NOT_EMPTY} to check that at least one result row is 44 | * returned. 45 | *
  • {@link Outcome#VOID} for when you wish to disregard the result. 46 | *
  • {@link Outcome#UPDATE_COUNT} to check the number of updated rows. 47 | *
48 | * 49 | * @param Type of expected result 50 | * @since 1.0 51 | */ 52 | public interface Outcome { 53 | 54 | /** 55 | * Returns {@code TRUE} if at least one SQL record found in 56 | * {@link ResultSet}. 57 | * 58 | *

The outcome returns the value of {@link ResultSet#next()} and throws 59 | * {@link SQLException} in case of a problem. 60 | * 61 | * @since 1.0 62 | */ 63 | Outcome NOT_EMPTY = (rset, stmt) -> rset.next(); 64 | 65 | /** 66 | * Returns {@code TRUE} if there is no SQL records in {@link ResultSet}. 67 | * 68 | *

The outcome returns the negation value of {@link ResultSet#next()} 69 | * and throws {@link SQLException} in case of a problem. 70 | * 71 | * @since 1.0 72 | */ 73 | Outcome EMPTY = (rset, stmt) -> !rset.next(); 74 | 75 | /** 76 | * Outcome that does nothing (and always returns {@code null}). 77 | * 78 | *

Useful when you're not interested in the result: 79 | * 80 | *

 new JdbcSession(source)
 81 |      *   .sql("INSERT INTO foo (name) VALUES (?)")
 82 |      *   .set("Jeff Lebowski")
 83 |      *   .insert(Outcome.VOID);
84 | * 85 | * @since 1.0 86 | */ 87 | Outcome VOID = (rset, stmt) -> Void.TYPE.cast(null); 88 | 89 | /** 90 | * Outcome that returns the number of updated rows. 91 | * 92 | *

Use it when you need to determine the number of rows updated: 93 | * 94 | *

 Integer count = new JdbcSession(source)
 95 |      *   .sql("UPDATE employee SET salary = 35000 WHERE department = ?")
 96 |      *   .set("Finance")
 97 |      *   .update(Outcome.UPDATE_COUNT);
98 | * 99 | * @since 1.0 100 | */ 101 | Outcome UPDATE_COUNT = (rset, stmt) -> stmt.getUpdateCount(); 102 | 103 | /** 104 | * Outcome that returns last insert ID. 105 | * 106 | *

Use it when you need to get last insert ID from INSERT: 107 | * 108 | *

 long id = new JdbcSession(source)
109 |      *   .sql("INSERT INTO employee (name) VALUES (?)")
110 |      *   .set("Jeffrey")
111 |      *   .insert(Outcome.LAST_INSERT_ID);
112 | * 113 | * @since 1.0 114 | */ 115 | Outcome LAST_INSERT_ID = (rset, stmt) -> { 116 | if (!rset.next()) { 117 | throw new SQLException("no last_insert_id() available"); 118 | } 119 | return rset.getLong(1); 120 | }; 121 | 122 | /** 123 | * Default mappings. 124 | */ 125 | Mappings DEFAULT_MAPPINGS = new DefaultMappings(); 126 | 127 | /** 128 | * Process the result set and return some value. 129 | * 130 | * @param rset The result set to process 131 | * @param stmt The statement used in the run 132 | * @return The result 133 | * @throws Exception If something goes wrong inside 134 | */ 135 | T handle(ResultSet rset, Statement stmt) throws Exception; 136 | 137 | /** 138 | * Mapping. 139 | * 140 | * @param Type of output 141 | * @since 1.0 142 | */ 143 | interface Mapping { 144 | /** 145 | * Map. 146 | * 147 | * @param rset Result set 148 | * @return Object 149 | * @throws Exception If fails 150 | */ 151 | T map(ResultSet rset) throws Exception; 152 | } 153 | 154 | /** 155 | * Mappings for different types. 156 | * 157 | * @since 1.0 158 | */ 159 | interface Mappings { 160 | /** 161 | * Mapping for a type. 162 | * 163 | * @param tpe Class of result. 164 | * @param Type of result. 165 | * @return Mapping. 166 | */ 167 | Mapping forType(Class tpe); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/outcomes/SingleOutcomeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc.outcomes; 32 | 33 | import com.github.piotrkot.oojdbc.H2Source; 34 | import com.github.piotrkot.oojdbc.JdbcSession; 35 | import com.github.piotrkot.oojdbc.JdbcSessionTx; 36 | import com.github.piotrkot.oojdbc.Outcome; 37 | import com.github.piotrkot.oojdbc.Sql; 38 | import com.github.piotrkot.oojdbc.Utc; 39 | import com.github.piotrkot.oojdbc.statements.Args; 40 | import com.github.piotrkot.oojdbc.statements.Exec; 41 | import com.github.piotrkot.oojdbc.statements.Insert; 42 | import com.github.piotrkot.oojdbc.statements.Select; 43 | import java.math.BigDecimal; 44 | import java.util.Date; 45 | import javax.sql.DataSource; 46 | import org.hamcrest.MatcherAssert; 47 | import org.hamcrest.Matchers; 48 | import org.junit.jupiter.api.Assertions; 49 | import org.junit.jupiter.api.Test; 50 | 51 | /** 52 | * Test case for {@link SingleOutcome}. 53 | * 54 | * @since 1.0 55 | * @checkstyle ClassDataAbstractionCoupling (2 lines) 56 | */ 57 | final class SingleOutcomeTest { 58 | @Test 59 | void retrievesByte() throws Exception { 60 | MatcherAssert.assertThat( 61 | new JdbcSession<>( 62 | new Select<>( 63 | new Sql("CALL 65"), 64 | new SingleOutcome<>(Byte.class) 65 | ) 66 | ).using(this.datasource()), 67 | Matchers.is((byte) 'A') 68 | ); 69 | } 70 | 71 | @Test 72 | void retrievesBigDecimal() throws Exception { 73 | MatcherAssert.assertThat( 74 | new JdbcSession<>( 75 | new Select<>( 76 | new Sql("CALL POWER(10, 10)"), 77 | new SingleOutcome<>(BigDecimal.class) 78 | ) 79 | ).using(this.datasource()), 80 | Matchers.is(new BigDecimal("1.0E+10")) 81 | ); 82 | } 83 | 84 | @Test 85 | void retrievesBytes() throws Exception { 86 | final int size = 256; 87 | MatcherAssert.assertThat( 88 | new JdbcSession<>( 89 | new Select<>( 90 | new Sql(String.format("CALL SECURE_RAND(%d)", size)), 91 | new SingleOutcome<>(byte[].class) 92 | ) 93 | ).using(this.datasource()).length, 94 | Matchers.is(size) 95 | ); 96 | } 97 | 98 | @Test 99 | void retrievesUtc() throws Exception { 100 | MatcherAssert.assertThat( 101 | new JdbcSession<>( 102 | new Select<>( 103 | new Sql("CALL CURRENT_TIMESTAMP()"), 104 | new SingleOutcome<>(Utc.class) 105 | ) 106 | ).using(this.datasource()), 107 | Matchers.notNullValue() 108 | ); 109 | } 110 | 111 | @Test 112 | void retrievesDate() throws Exception { 113 | MatcherAssert.assertThat( 114 | new JdbcSession<>( 115 | new Select<>( 116 | new Sql("CALL CURRENT_DATE()"), 117 | new SingleOutcome<>(Date.class) 118 | ) 119 | ).using(this.datasource()), 120 | Matchers.notNullValue() 121 | ); 122 | } 123 | 124 | @Test 125 | void retrievesString() throws Exception { 126 | final DataSource source = this.datasource(); 127 | new JdbcSessionTx<>( 128 | conn -> { 129 | new Exec( 130 | new Sql("CREATE TABLE foo (name VARCHAR(50))") 131 | ).using(conn); 132 | new Insert<>( 133 | new Sql("INSERT INTO foo (name) VALUES (?) "), 134 | new Args("Jeff Lebowski"), 135 | Outcome.VOID 136 | ).using(conn); 137 | return new Insert<>( 138 | new Sql("INSERT INTO foo (name) VALUES (?)"), 139 | new Args("Walter Sobchak"), 140 | Outcome.VOID 141 | ).using(conn); 142 | } 143 | ).using(source); 144 | final String name = new JdbcSession<>( 145 | new Select<>( 146 | new Sql("SELECT name FROM foo"), 147 | new SingleOutcome<>(String.class) 148 | ) 149 | ).using(source); 150 | MatcherAssert.assertThat(name, Matchers.startsWith("Jeff")); 151 | } 152 | 153 | @Test 154 | void failsFast() { 155 | Assertions.assertThrows( 156 | IllegalArgumentException.class, 157 | () -> new SingleOutcome<>(Exception.class) 158 | ); 159 | } 160 | 161 | /** 162 | * Create datasource. 163 | * 164 | * @return Source. 165 | */ 166 | private DataSource datasource() { 167 | return new H2Source("ytt68"); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/UtcTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import com.github.piotrkot.oojdbc.statements.Args; 34 | import com.github.piotrkot.oojdbc.statements.Exec; 35 | import com.github.piotrkot.oojdbc.statements.Insert; 36 | import java.security.SecureRandom; 37 | import java.sql.Connection; 38 | import java.sql.PreparedStatement; 39 | import java.sql.ResultSet; 40 | import java.text.DateFormat; 41 | import java.text.SimpleDateFormat; 42 | import java.util.Date; 43 | import java.util.GregorianCalendar; 44 | import java.util.Locale; 45 | import java.util.Random; 46 | import java.util.TimeZone; 47 | import javax.sql.DataSource; 48 | import org.hamcrest.MatcherAssert; 49 | import org.hamcrest.Matchers; 50 | import org.junit.jupiter.api.BeforeEach; 51 | import org.junit.jupiter.api.Test; 52 | 53 | /** 54 | * Test case of {@link Utc}. 55 | * @since 1.0 56 | * @checkstyle ClassDataAbstractionCoupling (500 lines) 57 | */ 58 | final class UtcTest { 59 | 60 | /** 61 | * Randomizer. 62 | */ 63 | private static final Random RND = new SecureRandom(); 64 | 65 | /** 66 | * Format to use in tests. 67 | */ 68 | private DateFormat fmt; 69 | 70 | /** 71 | * Data source. 72 | */ 73 | private DataSource source; 74 | 75 | /** 76 | * Prepare this test case. 77 | * @throws Exception If there is some problem inside 78 | */ 79 | @BeforeEach 80 | void prepare() throws Exception { 81 | this.source = new H2Source( 82 | String.format("xpo%d", UtcTest.RND.nextInt()) 83 | ); 84 | new JdbcSession<>( 85 | new Exec( 86 | new Sql("CREATE TABLE foo (date DATETIME)") 87 | ) 88 | ).using(this.source); 89 | this.fmt = new SimpleDateFormat( 90 | "yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH 91 | ); 92 | } 93 | 94 | /** 95 | * Utc can save date to prepared statement. 96 | * @throws Exception If there is some problem inside 97 | */ 98 | @Test 99 | void savesDateWithUtcTimezone() throws Exception { 100 | this.fmt.setCalendar( 101 | new GregorianCalendar(TimeZone.getTimeZone("GMT-5")) 102 | ); 103 | final Date date = this.fmt.parse("2008-05-24 05:06:07.000"); 104 | final String saved; 105 | try (Connection conn = this.source.getConnection()) { 106 | final PreparedStatement ustmt = conn.prepareStatement( 107 | "INSERT INTO foo (date) VALUES (?)" 108 | ); 109 | new Utc(date).setTimestamp(ustmt, 1); 110 | ustmt.executeUpdate(); 111 | final PreparedStatement rstmt = conn.prepareStatement( 112 | "SELECT date FROM foo" 113 | ); 114 | try (ResultSet rset = rstmt.executeQuery()) { 115 | if (!rset.next()) { 116 | throw new IllegalArgumentException(); 117 | } 118 | saved = rset.getString(1); 119 | } 120 | } 121 | MatcherAssert.assertThat( 122 | saved, 123 | Matchers.startsWith("2008-05-24 10:06:07") 124 | ); 125 | } 126 | 127 | /** 128 | * Utc can load date from result set. 129 | * @throws Exception If there is some problem inside 130 | */ 131 | @Test 132 | void loadsDateWithUtcTimezone() throws Exception { 133 | final Connection conn = this.source.getConnection(); 134 | final Date loaded; 135 | try { 136 | final PreparedStatement ustmt = conn.prepareStatement( 137 | "INSERT INTO foo (date) VALUES (?) " 138 | ); 139 | ustmt.setString(1, "2005-02-02 10:07:08.000"); 140 | ustmt.executeUpdate(); 141 | final PreparedStatement rstmt = conn.prepareStatement( 142 | "SELECT date FROM foo " 143 | ); 144 | try (ResultSet rset = rstmt.executeQuery()) { 145 | if (!rset.next()) { 146 | throw new IllegalArgumentException(); 147 | } 148 | loaded = Utc.getTimestamp(rset, 1); 149 | } 150 | } finally { 151 | conn.close(); 152 | } 153 | this.fmt.setCalendar( 154 | new GregorianCalendar(TimeZone.getTimeZone("GMT-3")) 155 | ); 156 | MatcherAssert.assertThat( 157 | this.fmt.format(loaded), 158 | Matchers.startsWith("2005-02-02 07:07:08") 159 | ); 160 | } 161 | 162 | /** 163 | * Utc can set and read message date, with different timezone. 164 | * @throws Exception If there is some problem inside 165 | */ 166 | @Test 167 | void setsAndReadsDateWithDifferentTimezone() throws Exception { 168 | final Date date = new Date(); 169 | new JdbcSession<>( 170 | new Insert<>( 171 | new Sql("INSERT INTO foo VALUES (?)"), 172 | new Args(new Utc(date)), 173 | Outcome.VOID 174 | ) 175 | ).using(this.source); 176 | final Connection conn = this.source.getConnection(); 177 | final String saved; 178 | try { 179 | final PreparedStatement stmt = conn.prepareStatement( 180 | "SELECT date FROM foo " 181 | ); 182 | try (ResultSet rset = stmt.executeQuery()) { 183 | if (!rset.next()) { 184 | throw new IllegalStateException(); 185 | } 186 | saved = rset.getString(1); 187 | } 188 | } finally { 189 | conn.close(); 190 | } 191 | this.fmt.setTimeZone(TimeZone.getTimeZone("GMT")); 192 | final Date absolute = this.fmt.parse(saved); 193 | MatcherAssert.assertThat( 194 | absolute.toString(), 195 | Matchers.equalTo(date.toString()) 196 | ); 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/JdbcSessionMySqlTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import com.github.piotrkot.oojdbc.outcomes.StoredProcedureOutcome; 34 | import com.github.piotrkot.oojdbc.statements.Args; 35 | import com.github.piotrkot.oojdbc.statements.Exec; 36 | import com.github.piotrkot.oojdbc.statements.Insert; 37 | import com.github.piotrkot.oojdbc.statements.ProcCall; 38 | import com.github.piotrkot.oojdbc.statements.Select; 39 | import com.mysql.cj.jdbc.MysqlDataSource; 40 | import java.sql.CallableStatement; 41 | import java.sql.SQLException; 42 | import java.sql.Types; 43 | import javax.sql.DataSource; 44 | import org.hamcrest.MatcherAssert; 45 | import org.hamcrest.Matchers; 46 | import org.junit.jupiter.api.Test; 47 | import org.testcontainers.containers.JdbcDatabaseContainer; 48 | import org.testcontainers.containers.MySQLContainer; 49 | import org.testcontainers.junit.jupiter.Container; 50 | import org.testcontainers.junit.jupiter.Testcontainers; 51 | import org.testcontainers.utility.DockerImageName; 52 | 53 | /** 54 | * Integration case for {@link JdbcSession} on MySQL. 55 | * 56 | * @since 1.0 57 | * @checkstyle ClassDataAbstractionCoupling (2 lines) 58 | */ 59 | @SuppressWarnings("PMD.AvoidDuplicateLiterals") 60 | @Testcontainers 61 | final class JdbcSessionMySqlTest { 62 | 63 | /** 64 | * The database container. 65 | */ 66 | @Container 67 | private final JdbcDatabaseContainer mysql = 68 | new MySQLContainer<>( 69 | DockerImageName 70 | .parse("mysql/mysql-server:latest") 71 | .asCompatibleSubstituteFor("mysql") 72 | ); 73 | 74 | @Test 75 | void worksWithExecute() throws Exception { 76 | final DataSource source = this.source(); 77 | new JdbcSessionTx<>( 78 | conn -> { 79 | new Exec( 80 | new Sql("CREATE TABLE foo (name VARCHAR(50))") 81 | ).using(conn); 82 | return new Insert<>( 83 | new Sql("INSERT INTO foo (name) VALUES (?)"), 84 | new Args("Jeff Lebowski"), 85 | Outcome.VOID 86 | ).using(conn); 87 | } 88 | ).using(source); 89 | } 90 | 91 | @Test 92 | void worksLastInsertId() throws Exception { 93 | new JdbcSession<>( 94 | new Exec( 95 | new Sql( 96 | "CREATE TABLE IF NOT EXISTS foo (", 97 | "id INT NOT NULL AUTO_INCREMENT, ", 98 | "name VARCHAR(50), ", 99 | "PRIMARY KEY (id)", 100 | ")" 101 | ) 102 | ) 103 | ).using(this.source()); 104 | MatcherAssert.assertThat( 105 | new JdbcSession<>( 106 | new Insert<>( 107 | new Sql("INSERT INTO foo (name) VALUES (?)"), 108 | new Args("test"), 109 | Outcome.LAST_INSERT_ID 110 | ) 111 | ).using(this.source()), 112 | Matchers.is(1L) 113 | ); 114 | } 115 | 116 | @Test 117 | void worksLastInsertIdAndTransaction() throws Exception { 118 | new JdbcSession<>( 119 | new Exec( 120 | new Sql( 121 | "CREATE TABLE IF NOT EXISTS foo (", 122 | "id INT NOT NULL AUTO_INCREMENT, ", 123 | "name VARCHAR(50), ", 124 | "PRIMARY KEY (id)", 125 | ")" 126 | ) 127 | ) 128 | ).using(this.source()); 129 | try { 130 | new JdbcSessionTx<>( 131 | conn -> { 132 | new Exec( 133 | new Sql("START TRANSACTION") 134 | ).using(conn); 135 | MatcherAssert.assertThat( 136 | new Insert<>( 137 | new Sql("INSERT INTO foo (name) VALUES (?)"), 138 | new Args("test"), 139 | Outcome.LAST_INSERT_ID 140 | ).using(conn), 141 | Matchers.is(1L) 142 | ); 143 | throw new SQLException("forced"); 144 | } 145 | ).using(this.source()); 146 | } catch (final SQLException ignored) { 147 | } 148 | MatcherAssert.assertThat( 149 | new JdbcSession<>( 150 | new Select<>( 151 | new Sql("SELECT name FROM foo WHERE name = 'foo'"), 152 | Outcome.EMPTY 153 | ) 154 | ).using(this.source()), 155 | Matchers.is(true) 156 | ); 157 | } 158 | 159 | @Test 160 | void callsFunctionWithOutParam() throws Exception { 161 | new JdbcSessionTx<>( 162 | conn -> { 163 | new Exec( 164 | new Sql("CREATE TABLE IF NOT EXISTS users (name VARCHAR(50))") 165 | ).using(conn); 166 | new Insert<>( 167 | new Sql("INSERT INTO users (name) VALUES (?)"), 168 | new Args("Jeff Charles"), 169 | Outcome.VOID 170 | ).using(conn); 171 | return new Exec( 172 | new Sql( 173 | "CREATE PROCEDURE proc(OUT username text, OUT day date) ", 174 | "BEGIN SELECT name, CURDATE() INTO username, day ", 175 | "FROM users; ", 176 | "SELECT username, day; ", 177 | "END" 178 | ) 179 | ).using(conn); 180 | } 181 | ).using(this.source()); 182 | final Object[] result = new JdbcSession<>( 183 | new ProcCall<>( 184 | new Sql("CALL proc(?, ?)"), 185 | stmt -> { 186 | ((CallableStatement) stmt).registerOutParameter(1, Types.VARCHAR); 187 | ((CallableStatement) stmt).registerOutParameter(2, Types.DATE); 188 | }, 189 | new StoredProcedureOutcome(1, 2) 190 | ) 191 | ).using(this.source()); 192 | MatcherAssert.assertThat( 193 | result, 194 | Matchers.arrayContaining( 195 | Matchers.is("Jeff Charles"), 196 | Matchers.notNullValue() 197 | ) 198 | ); 199 | } 200 | 201 | /** 202 | * Get data source. 203 | * 204 | * @return Source 205 | */ 206 | private DataSource source() { 207 | final MysqlDataSource src = new MysqlDataSource(); 208 | src.setUrl(this.mysql.getJdbcUrl()); 209 | src.setUser(this.mysql.getUsername()); 210 | src.setPassword(this.mysql.getPassword()); 211 | return src; 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/JdbcSessionH2Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import com.github.piotrkot.oojdbc.outcomes.ColumnOutcome; 34 | import com.github.piotrkot.oojdbc.outcomes.ListOutcome; 35 | import com.github.piotrkot.oojdbc.statements.Args; 36 | import com.github.piotrkot.oojdbc.statements.Exec; 37 | import com.github.piotrkot.oojdbc.statements.Insert; 38 | import com.github.piotrkot.oojdbc.statements.Select; 39 | import java.io.IOException; 40 | import java.sql.SQLException; 41 | import java.util.Collection; 42 | import javax.sql.DataSource; 43 | import org.cactoos.iterable.IterableOf; 44 | import org.hamcrest.MatcherAssert; 45 | import org.hamcrest.Matchers; 46 | import org.junit.jupiter.api.Test; 47 | 48 | /** 49 | * Test case for {@link JdbcSession}. 50 | * @since 1.0 51 | * @checkstyle ClassDataAbstractionCoupling (2 lines) 52 | */ 53 | final class JdbcSessionH2Test { 54 | 55 | /** 56 | * JdbcSession can do SQL manipulations. 57 | * @throws Exception If there is some problem inside 58 | */ 59 | @Test 60 | void sendsSqlManipulationsToJdbcDriver() throws Exception { 61 | final DataSource source = new H2Source("tiu78"); 62 | new JdbcSessionTx<>( 63 | conn -> { 64 | new Exec( 65 | new Sql("CREATE TABLE foo (name VARCHAR(50))") 66 | ).using(conn); 67 | return new Insert<>( 68 | new Sql("INSERT INTO foo (name) VALUES (?)"), 69 | new Args("Jeff Lebowski"), 70 | Outcome.VOID 71 | ).using(conn); 72 | } 73 | ).using(source); 74 | final String name = new JdbcSession<>( 75 | new Select<>( 76 | new Sql("SELECT name FROM foo WHERE name = 'Jeff Lebowski'"), 77 | (rset, stmt) -> { 78 | rset.next(); 79 | return rset.getString(1); 80 | } 81 | ) 82 | ).using(source); 83 | MatcherAssert.assertThat(name, Matchers.startsWith("Jeff")); 84 | } 85 | 86 | /** 87 | * JdbcSession can execute SQL. 88 | * @throws Exception If there is some problem inside 89 | * @since 1.0 90 | */ 91 | @Test 92 | void executesSql() throws Exception { 93 | final DataSource source = new H2Source("tpl98"); 94 | new JdbcSessionTx<>( 95 | conn -> { 96 | new Exec( 97 | new Sql("CREATE TABLE foo5 (name VARCHAR(30))") 98 | ).using(conn); 99 | return new Exec( 100 | new Sql("DROP TABLE foo5") 101 | ).using(conn); 102 | } 103 | ).using(source); 104 | } 105 | 106 | /** 107 | * JdbcSession can automatically commit. 108 | * @throws Exception If there is some problem inside 109 | */ 110 | @Test 111 | void automaticallyCommitsByDefault() throws Exception { 112 | final DataSource source = new H2Source("tt8u"); 113 | final String name = new JdbcSession<>( 114 | conn -> { 115 | new Exec( 116 | new Sql("CREATE TABLE foo16 (name VARCHAR(50))") 117 | ).using(conn); 118 | new Insert<>( 119 | new Sql("INSERT INTO foo16 (name) VALUES (?)"), 120 | new Args("Walter"), 121 | Outcome.VOID 122 | ).using(conn); 123 | return new Select<>( 124 | new Sql("SELECT name FROM foo16 WHERE name = 'Walter'"), 125 | (rset, stmt) -> { 126 | rset.next(); 127 | return rset.getString(1); 128 | } 129 | ).using(conn); 130 | } 131 | ).using(source); 132 | MatcherAssert.assertThat(name, Matchers.startsWith("Wa")); 133 | } 134 | 135 | /** 136 | * JdbcSession can release connections from the pool. 137 | * @throws Exception If there is some problem inside 138 | * @since 1.0 139 | */ 140 | @Test 141 | @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") 142 | void releasesConnectionsFromThePool() throws Exception { 143 | final DataSource source = new H2Source("t445p"); 144 | new JdbcSession<>( 145 | new Exec( 146 | new Sql("CREATE TABLE foo776 (name VARCHAR(30))") 147 | ) 148 | ).using(source); 149 | // @checkstyle MagicNumber (1 line) 150 | for (int idx = 0; idx < 10; ++idx) { 151 | new JdbcSession<>( 152 | new Insert<>( 153 | new Sql("INSERT INTO foo776 VALUES ('hello, world!')"), 154 | Outcome.VOID 155 | ) 156 | ).using(source); 157 | } 158 | } 159 | 160 | /** 161 | * JdbcSession can execute SQL in parallel threads. 162 | * @throws Exception If there is some problem inside 163 | * @since 1.0 164 | */ 165 | @Test 166 | void executesSqlInParallelThreads() throws Exception { 167 | final DataSource source = new H2Source("til87"); 168 | new JdbcSession<>( 169 | new Exec( 170 | new Sql("CREATE TABLE foo99 (name VARCHAR(30))") 171 | ) 172 | ).using(source); 173 | this.insert(source, "foo99"); 174 | } 175 | 176 | /** 177 | * JdbcSession can rollback transaction. 178 | * @throws Exception If there is some problem inside 179 | */ 180 | @Test 181 | void rollbacksTransaction() throws Exception { 182 | final DataSource source = new H2Source("t228x"); 183 | new JdbcSession<>( 184 | conn -> { 185 | new Exec( 186 | new Sql("CREATE TABLE t228x (name VARCHAR(30))") 187 | ).using(conn); 188 | return new Insert<>( 189 | new Sql("INSERT INTO t228x VALUES ('foo')"), 190 | Outcome.VOID 191 | ).using(conn); 192 | } 193 | ).using(source); 194 | try { 195 | new JdbcSessionTx<>( 196 | conn -> { 197 | new Insert<>( 198 | new Sql("INSERT INTO t228x VALUES ('bar')"), 199 | Outcome.VOID 200 | ).using(conn); 201 | throw new IOException(); 202 | } 203 | ).using(source); 204 | } catch (final SQLException ignored) { 205 | } 206 | MatcherAssert.assertThat( 207 | new JdbcSession<>( 208 | new Select<>( 209 | new Sql("SELECT * FROM t228x"), 210 | new ListOutcome<>(rset -> rset.getString("name")) 211 | ) 212 | ).using(source), 213 | Matchers.contains("foo") 214 | ); 215 | } 216 | 217 | @Test 218 | void shouldPassArgsIterable() throws Exception { 219 | final DataSource source = new H2Source("i8o98"); 220 | new JdbcSession<>( 221 | conn -> { 222 | new Exec( 223 | new Sql("CREATE TABLE i8o98 (name VARCHAR(50))") 224 | ).using(conn); 225 | new Insert<>( 226 | new Sql("INSERT INTO i8o98 (name) VALUES ('Mark')"), 227 | Outcome.VOID 228 | ).using(conn); 229 | return new Insert<>( 230 | new Sql("INSERT INTO i8o98 (name) VALUES ('Peter')"), 231 | Outcome.VOID 232 | ).using(conn); 233 | } 234 | ).using(source); 235 | final Collection fetched = new JdbcSession<>( 236 | new Select<>( 237 | new Sql("SELECT name FROM i8o98 WHERE name IN (?, ?)"), 238 | new Args(new IterableOf<>("Mark", "Peter")), 239 | new ColumnOutcome<>(String.class) 240 | ) 241 | ).using(source); 242 | MatcherAssert.assertThat( 243 | fetched, Matchers.hasSize(2) 244 | ); 245 | } 246 | 247 | /** 248 | * Insert a row into a table. 249 | * @param src Data source 250 | * @param table Name of the table to INSERT into 251 | * @throws Exception If there is some problem inside 252 | * @since 1.0 253 | */ 254 | private void insert(final DataSource src, final String table) 255 | throws Exception { 256 | new JdbcSession<>( 257 | new Insert<>( 258 | new Sql( 259 | String.format("INSERT INTO %s VALUES ('hey')", table) 260 | ), 261 | Outcome.VOID 262 | ) 263 | ).using(src); 264 | } 265 | 266 | } 267 | -------------------------------------------------------------------------------- /src/test/java/com/github/piotrkot/oojdbc/JdbcSessionPsqlTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2018, jcabi.com 3 | * Copyright (c) 2021, github.com/piotrkot 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 1) Redistributions of source code must retain the above 9 | * copyright notice, this list of conditions and the following 10 | * disclaimer. 2) Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following 12 | * disclaimer in the documentation and/or other materials provided 13 | * with the distribution. 3) Neither the name of the jcabi.com nor 14 | * the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written 16 | * permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package com.github.piotrkot.oojdbc; 32 | 33 | import com.github.piotrkot.oojdbc.outcomes.ColumnOutcome; 34 | import com.github.piotrkot.oojdbc.outcomes.StoredProcedureOutcome; 35 | import com.github.piotrkot.oojdbc.statements.Args; 36 | import com.github.piotrkot.oojdbc.statements.Exec; 37 | import com.github.piotrkot.oojdbc.statements.Insert; 38 | import com.github.piotrkot.oojdbc.statements.ProcCall; 39 | import com.github.piotrkot.oojdbc.statements.Select; 40 | import com.zaxxer.hikari.HikariDataSource; 41 | import java.io.IOException; 42 | import java.sql.CallableStatement; 43 | import java.sql.SQLException; 44 | import java.sql.Types; 45 | import java.util.Date; 46 | import javax.sql.DataSource; 47 | import org.hamcrest.MatcherAssert; 48 | import org.hamcrest.Matchers; 49 | import org.junit.jupiter.api.Test; 50 | import org.testcontainers.containers.JdbcDatabaseContainer; 51 | import org.testcontainers.containers.PostgreSQLContainer; 52 | import org.testcontainers.junit.jupiter.Container; 53 | import org.testcontainers.junit.jupiter.Testcontainers; 54 | 55 | /** 56 | * Integration case for {@link JdbcSession}. 57 | * @since 1.0 58 | * @checkstyle ClassDataAbstractionCoupling (2 lines) 59 | */ 60 | @Testcontainers 61 | final class JdbcSessionPsqlTest { 62 | 63 | /** 64 | * The database container. 65 | */ 66 | @Container 67 | private final JdbcDatabaseContainer postgres = 68 | new PostgreSQLContainer<>("postgres:9.6.12"); 69 | 70 | /** 71 | * JdbcSession can do PostgreSQL manipulations. 72 | * 73 | * @throws Exception If there is some problem inside 74 | */ 75 | @Test 76 | void manipulatesPostgresql() throws Exception { 77 | final DataSource source = this.source(); 78 | new JdbcSessionTx<>( 79 | conn -> { 80 | new Exec( 81 | new Sql( 82 | "CREATE TABLE IF NOT EXISTS foo (name VARCHAR(50))" 83 | ) 84 | ).using(conn); 85 | return new Insert<>( 86 | new Sql( 87 | "INSERT INTO foo (name) VALUES (?)" 88 | ), 89 | new Args( 90 | "Jeff Lebowski" 91 | ), 92 | Outcome.VOID 93 | ).using(conn); 94 | } 95 | ).using(source); 96 | } 97 | 98 | /** 99 | * JdbcSession can change transaction isolation level. 100 | * 101 | * @throws Exception If there is some problem inside 102 | */ 103 | @Test 104 | void changesTransactionIsolationLevel() throws Exception { 105 | final DataSource source = this.source(); 106 | new JdbcSession<>( 107 | new Exec( 108 | new Sql("VACUUM") 109 | ) 110 | ).using(source); 111 | } 112 | 113 | /** 114 | * JdbcSession can run a function (stored procedure) with 115 | * output parameters. 116 | * 117 | * @throws Exception If something goes wrong 118 | */ 119 | @Test 120 | void callsFunctionWithOutParam() throws Exception { 121 | final DataSource source = this.source(); 122 | new JdbcSessionTx<>( 123 | conn -> { 124 | new Exec( 125 | new Sql("CREATE TABLE IF NOT EXISTS users (name VARCHAR(50))") 126 | ).using(conn); 127 | new Insert<>( 128 | new Sql("INSERT INTO users (name) VALUES (?)"), 129 | new Args("Jeff Charles"), 130 | Outcome.VOID 131 | ).using(conn); 132 | return new Exec( 133 | new Sql( 134 | "CREATE OR REPLACE FUNCTION fetchUser(username OUT text,", 135 | "day OUT date)", 136 | "AS $$ BEGIN SELECT name, CURRENT_DATE INTO username, day", 137 | "FROM users; END; $$ LANGUAGE plpgsql;" 138 | ) 139 | ).using(conn); 140 | } 141 | ).using(source); 142 | final Object[] result = new JdbcSessionTx<>( 143 | new ProcCall<>( 144 | new Sql( 145 | "{call fetchUser(?, ?)}" 146 | ), 147 | stmt -> { 148 | final CallableStatement cstmt = (CallableStatement) stmt; 149 | cstmt.registerOutParameter(1, Types.VARCHAR); 150 | cstmt.registerOutParameter(2, Types.DATE); 151 | }, 152 | new StoredProcedureOutcome(1, 2) 153 | ) 154 | ).using(source); 155 | MatcherAssert.assertThat(result.length, Matchers.is(2)); 156 | MatcherAssert.assertThat( 157 | result[0].toString(), 158 | Matchers.containsString("Charles") 159 | ); 160 | MatcherAssert.assertThat( 161 | (Date) result[1], 162 | Matchers.notNullValue() 163 | ); 164 | } 165 | 166 | /** 167 | * JdbcSession can run a function (stored procedure) with 168 | * input and output parameters. 169 | * 170 | * @throws Exception If something goes wrong 171 | */ 172 | @Test 173 | void callsFunctionWithInOutParam() throws Exception { 174 | final DataSource source = this.source(); 175 | new JdbcSessionTx<>( 176 | conn -> { 177 | new Exec( 178 | new Sql( 179 | "CREATE TABLE IF NOT EXISTS", 180 | "usersids (id INTEGER, name VARCHAR(50))" 181 | ) 182 | ).using(conn); 183 | new Insert<>( 184 | new Sql( 185 | "INSERT INTO usersids (id, name) VALUES (?, ?)" 186 | ), 187 | new Args(1, "Marco Polo"), 188 | Outcome.VOID 189 | ).using(conn); 190 | return new Exec( 191 | new Sql( 192 | "CREATE OR REPLACE FUNCTION fetchUserById(uid IN INTEGER,", 193 | "usrnm OUT text) AS $$ BEGIN", 194 | "SELECT name INTO usrnm FROM usersids WHERE id=uid;", 195 | "END; $$ LANGUAGE plpgsql;" 196 | ) 197 | ).using(conn); 198 | } 199 | ).using(source); 200 | final Object[] result = new JdbcSessionTx<>( 201 | new ProcCall<>( 202 | new Sql("{call fetchUserById(?, ?)}"), 203 | new Args(1), 204 | stmt -> ((CallableStatement) stmt) 205 | .registerOutParameter(2, Types.VARCHAR), 206 | new StoredProcedureOutcome(2) 207 | ) 208 | ).using(source); 209 | MatcherAssert.assertThat(result.length, Matchers.is(1)); 210 | MatcherAssert.assertThat( 211 | result[0].toString(), 212 | Matchers.containsString("Polo") 213 | ); 214 | } 215 | 216 | /** 217 | * JdbcSession can rollback transaction on exception. 218 | * @throws Exception If there is some problem inside 219 | */ 220 | @Test 221 | void rollbacksTransactionOnException() throws Exception { 222 | final DataSource source = this.source(); 223 | new JdbcSessionTx<>( 224 | new Exec( 225 | new Sql( 226 | "CREATE TABLE employee (id serial PRIMARY KEY, name VARCHAR(30));", 227 | "CREATE TABLE team (ref int references employee ON DELETE CASCADE);" 228 | ) 229 | ) 230 | ).using(source); 231 | try { 232 | new JdbcSessionTx<>( 233 | conn -> { 234 | new Insert<>( 235 | new Sql("INSERT INTO employee VALUES ('Jeff Igorowski')"), 236 | Outcome.VOID 237 | ).using(conn); 238 | return new Insert<>( 239 | new Sql("INSERT INTO employee VALUES (?)"), 240 | new Args(this.exceptionForValue()), 241 | Outcome.VOID 242 | ).using(conn); 243 | } 244 | ).using(source); 245 | MatcherAssert.assertThat("shall not pass", false); 246 | } catch (final SQLException ignored) { 247 | } 248 | MatcherAssert.assertThat( 249 | new JdbcSessionTx<>( 250 | new Select<>( 251 | new Sql("SELECT name FROM employee"), 252 | new ColumnOutcome<>(String.class) 253 | ) 254 | ).using(source), 255 | Matchers.empty() 256 | ); 257 | } 258 | 259 | /** 260 | * Throws exception instead of providing a value. 261 | * @return Value. 262 | * @throws IOException Exception thrown. 263 | */ 264 | private String exceptionForValue() throws IOException { 265 | throw new IOException(); 266 | } 267 | 268 | /** 269 | * Get data source. 270 | * 271 | * @return Source 272 | */ 273 | private DataSource source() { 274 | final HikariDataSource src = new HikariDataSource(); 275 | src.setDriverClassName(this.postgres.getDriverClassName()); 276 | src.setJdbcUrl(this.postgres.getJdbcUrl()); 277 | src.setUsername(this.postgres.getUsername()); 278 | src.setPassword(this.postgres.getPassword()); 279 | return src; 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.github.piotrkot 5 | oojdbc 6 | OO-JDBC wrapper 7 | Object-Oriented Interface to JDBC. 8 | 9 | jar 10 | https://github.com/piotrkot/oojdbc 11 | 1.4.0 12 | 2021 13 | 14 | 15 | Piotr Kotlicki 16 | piotr.kotlicki@gmail.com 17 | None 18 | https://github.com/piotrkot 19 | 20 | 21 | 22 | 23 | BSD 24 | https://github.com/piotrkot/oojdbc/blob/master/LICENSE 25 | repo 26 | BSD License 27 | 28 | 29 | 30 | GitHub 31 | https://github.com/piotrkot/oojdbc/issues 32 | 33 | 34 | scm:git:git@github.com:piotrkot/oojdbc.git 35 | scm:git:git@github.com:piotrkot/oojdbc.git 36 | 37 | https://github.com/piotrkot/oojdbc 38 | 39 | 40 | 41 | ossrh 42 | https://oss.sonatype.org/content/repositories/snapshots 43 | 44 | 45 | ossrh 46 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 47 | 48 | 49 | 50 | 51 | UTF-8 52 | 53 | 54 | 55 | org.cactoos 56 | cactoos 57 | 0.55.0 58 | 59 | 60 | org.projectlombok 61 | lombok 62 | 1.18.28 63 | provided 64 | 65 | 66 | com.h2database 67 | h2 68 | 2.2.220 69 | test 70 | 71 | 72 | org.postgresql 73 | postgresql 74 | 42.6.0 75 | test 76 | 77 | 78 | com.mysql 79 | mysql-connector-j 80 | 8.0.33 81 | provided 82 | 83 | 84 | com.zaxxer 85 | HikariCP 86 | 5.0.1 87 | test 88 | 89 | 90 | org.testcontainers 91 | testcontainers 92 | 1.18.3 93 | test 94 | 95 | 96 | junit 97 | junit 98 | 99 | 100 | 101 | 102 | org.testcontainers 103 | jdbc 104 | 1.18.3 105 | test 106 | 107 | 108 | org.testcontainers 109 | postgresql 110 | 1.18.3 111 | test 112 | 113 | 114 | org.testcontainers 115 | mysql 116 | 1.18.3 117 | test 118 | 119 | 120 | org.testcontainers 121 | junit-jupiter 122 | 1.18.3 123 | test 124 | 125 | 126 | org.junit.platform 127 | junit-platform-engine 128 | 1.9.3 129 | test 130 | 131 | 132 | org.junit.platform 133 | junit-platform-commons 134 | 1.9.3 135 | test 136 | 137 | 138 | org.junit.jupiter 139 | junit-jupiter-api 140 | 5.9.3 141 | test 142 | 143 | 144 | org.junit.jupiter 145 | junit-jupiter-engine 146 | 5.9.3 147 | test 148 | 149 | 150 | org.junit.vintage 151 | junit-vintage-engine 152 | 5.9.3 153 | test 154 | 155 | 156 | org.slf4j 157 | slf4j-api 158 | 2.0.7 159 | provided 160 | 161 | 162 | org.slf4j 163 | slf4j-simple 164 | 2.0.7 165 | runtime 166 | 167 | 168 | org.hamcrest 169 | hamcrest 170 | 2.2 171 | test 172 | 173 | 174 | org.hamcrest 175 | hamcrest-core 176 | 1.3 177 | test 178 | 179 | 180 | com.google.code.findbugs 181 | annotations 182 | 3.0.1 183 | provided 184 | 185 | 186 | com.google.code.findbugs 187 | jsr305 188 | 189 | 190 | net.jcip 191 | jcip-annotations 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | org.apache.maven.plugins 200 | maven-compiler-plugin 201 | 3.11.0 202 | 203 | 1.8 204 | 1.8 205 | UTF-8 206 | 207 | 208 | 209 | maven-surefire-plugin 210 | 3.1.2 211 | 212 | false 213 | random 214 | false 215 | true 216 | true 217 | all 218 | 0 219 | 0 220 | true 221 | 4 222 | 223 | 224 | 225 | com.qulice 226 | qulice-maven-plugin 227 | 0.22.0 228 | 229 | file:${basedir}/LICENSE 230 | 231 | 232 | 233 | test 234 | 235 | check 236 | 237 | 238 | 239 | 240 | 241 | org.jacoco 242 | jacoco-maven-plugin 243 | 0.8.10 244 | 245 | 246 | 247 | prepare-agent 248 | 249 | 250 | 251 | report 252 | test 253 | 254 | report 255 | 256 | 257 | 258 | 259 | 260 | org.eluder.coveralls 261 | coveralls-maven-plugin 262 | 4.3.0 263 | 264 | ${repoToken} 265 | 266 | 267 | 268 | 269 | 270 | 271 | release 272 | 273 | -Xdoclint:none 274 | 275 | 276 | 277 | 278 | org.apache.maven.plugins 279 | maven-source-plugin 280 | 3.3.0 281 | 282 | 283 | attach-sources 284 | 285 | jar-no-fork 286 | 287 | 288 | 289 | 290 | 291 | org.apache.maven.plugins 292 | maven-javadoc-plugin 293 | 3.5.0 294 | 295 | 296 | 300 | 301 | todo 302 | a 303 | To do: 304 | 305 | 306 | checkstyle 307 | a 308 | Suppressed Checkstyle violations: 309 | 310 | 311 | 312 | 313 | 314 | attach-javadocs 315 | 316 | jar 317 | 318 | 319 | ${javadoc.opts} 320 | 321 | 322 | 323 | 324 | 325 | 326 | org.apache.maven.plugins 327 | maven-gpg-plugin 328 | 3.1.0 329 | 330 | 331 | sign-artifacts 332 | verify 333 | 334 | sign 335 | 336 | 337 | 338 | 339 | 340 | org.sonatype.plugins 341 | nexus-staging-maven-plugin 342 | 1.6.13 343 | true 344 | 345 | ossrh 346 | https://oss.sonatype.org/ 347 | true 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | --------------------------------------------------------------------------------