001/*
002MIT License
003
004Copyright (c) 2020 FBSQL Team
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022SOFTWARE.
023
024Home:   https://fbsql.github.io
025E-Mail: fbsql.team@gmail.com
026*/
027
028package org.fbsql.connection_pool;
029
030import java.sql.CallableStatement;
031import java.sql.Connection;
032import java.sql.PreparedStatement;
033import java.sql.SQLException;
034import java.sql.Statement;
035import java.util.HashMap;
036import java.util.Map;
037
038/**
039 * DbConnection contains JDBC Connection and associated with this connection prepared statements
040 */
041public class DbConnection {
042        private Connection                     connection;
043        private Map<String, PreparedStatement> mapPreparedStatements = new HashMap<>();
044        private Map<String, CallableStatement> mapCallableStatements = new HashMap<>();
045        private Statement                      st;
046
047        /**
048         * Constructs DbConnection
049         *
050         * @param connection
051         */
052        public DbConnection(Connection connection) {
053                this.connection = connection;
054        }
055
056        /**
057         * Get JDBC Connection
058         *
059         * @return Connection
060         */
061        public Connection getConnection() {
062                return connection;
063        }
064
065        /**
066         * Get PreparedStatement
067         * If the PreparedStatement is not prepared before it will be prepared and stored in map
068         *
069         * @param sql - SQL statement
070         * @return PreparedStatement
071         * @throws SQLException
072         */
073        public PreparedStatement getPreparedStatement(String sql) throws SQLException {
074                PreparedStatement ps = mapPreparedStatements.get(sql);
075                if (ps == null) {
076                        ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
077                        mapPreparedStatements.put(sql, ps);
078                }
079                ps.clearParameters();
080                return ps;
081        }
082
083        /**
084         * Get CallableStatement
085         * If the CallableStatement is not prepared before it will be prepared and stored in map
086         *
087         * @param sql - SQL statement
088         * @return CallableStatement
089         * @throws SQLException
090         */
091        public CallableStatement getCallableStatement(String sql) throws SQLException {
092                CallableStatement ps = mapCallableStatements.get(sql);
093                if (ps == null) {
094                        ps = connection.prepareCall(sql);
095                        mapCallableStatements.put(sql, ps);
096                }
097                ps.clearParameters();
098                return ps;
099        }
100
101        /**
102         * Get Statement
103         * If the Statement is not created before it will be created and stored as class member
104         *
105         * @return Statement
106         * @throws SQLException
107         */
108        public Statement getStatement() throws SQLException {
109                if (st == null)
110                        st = connection.createStatement();
111                return st;
112        }
113
114}
115
116/*
117Please contact FBSQL Team by E-Mail fbsql.team@gmail.com
118or visit https://fbsql.github.io if you need additional
119information or have any questions.
120*/
121
122/* EOF */