|
| 1 | +package com.softwareverde.database.connection; |
| 2 | + |
| 3 | +import com.softwareverde.database.DatabaseConnection; |
| 4 | +import com.softwareverde.database.DatabaseException; |
| 5 | +import com.softwareverde.database.query.Query; |
| 6 | +import com.softwareverde.database.query.parameter.TypedParameter; |
| 7 | +import com.softwareverde.database.row.JdbcRowFactory; |
| 8 | +import com.softwareverde.database.row.Row; |
| 9 | +import com.softwareverde.database.row.RowFactory; |
| 10 | +import com.softwareverde.util.Util; |
| 11 | + |
| 12 | +import java.sql.*; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class JdbcDatabaseConnection implements DatabaseConnection<Connection>, AutoCloseable { |
| 17 | + protected static final Long INVALID_ID = -1L; |
| 18 | + protected static final Integer INVALID_ROW_COUNT = -1; |
| 19 | + |
| 20 | + protected final RowFactory _rowFactory; |
| 21 | + protected final Connection _connection; |
| 22 | + protected Long _lastInsertId = INVALID_ID; |
| 23 | + protected Integer _lastRowAffectedCount = INVALID_ROW_COUNT; |
| 24 | + |
| 25 | + protected TypedParameter[] _stringArrayToTypedParameters(final String[] parameters) { |
| 26 | + if (parameters == null) { return new TypedParameter[0]; } |
| 27 | + |
| 28 | + final TypedParameter[] typedParameters = new TypedParameter[parameters.length]; |
| 29 | + for (int i=0; i<parameters.length; ++i) { |
| 30 | + typedParameters[i] = new TypedParameter(parameters[i]); |
| 31 | + } |
| 32 | + return typedParameters; |
| 33 | + } |
| 34 | + |
| 35 | + protected Long _extractInsertId(final PreparedStatement preparedStatement) throws SQLException { |
| 36 | + try (final ResultSet resultSet = preparedStatement.getGeneratedKeys()) { |
| 37 | + final Long insertId = (resultSet.next() ? resultSet.getLong(1) : null); |
| 38 | + return Util.coalesce(insertId, INVALID_ID); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + protected PreparedStatement _prepareStatement(final String query, final TypedParameter[] parameters) throws SQLException { |
| 43 | + final PreparedStatement preparedStatement = _connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); |
| 44 | + if (parameters != null) { |
| 45 | + for (int i = 0; i < parameters.length; ++i) { |
| 46 | + final int parameterIndex = (i + 1); |
| 47 | + final TypedParameter typedParameter = parameters[i]; |
| 48 | + switch (typedParameter.type) { |
| 49 | + case STRING: { |
| 50 | + preparedStatement.setString(parameterIndex, (String) parameters[i].value); |
| 51 | + } break; |
| 52 | + case BYTE_ARRAY: { |
| 53 | + preparedStatement.setBytes(parameterIndex, (byte[]) parameters[i].value); |
| 54 | + } break; |
| 55 | + case WHOLE_NUMBER: { |
| 56 | + preparedStatement.setLong(parameterIndex, (Long) parameters[i].value); |
| 57 | + } break; |
| 58 | + case FLOATING_POINT_NUMBER: { |
| 59 | + preparedStatement.setDouble(parameterIndex, (Double) parameters[i].value); |
| 60 | + } break; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return preparedStatement; |
| 65 | + } |
| 66 | + |
| 67 | + protected void _executeAsPreparedStatement(final String query, final TypedParameter[] typedParameters) throws SQLException { |
| 68 | + try (final PreparedStatement preparedStatement = _prepareStatement(query, typedParameters)) { |
| 69 | + preparedStatement.execute(); |
| 70 | + _lastInsertId = _extractInsertId(preparedStatement); |
| 71 | + _lastRowAffectedCount = preparedStatement.getUpdateCount(); |
| 72 | + } |
| 73 | + catch (final SQLException exception) { |
| 74 | + _lastInsertId = INVALID_ID; |
| 75 | + _lastRowAffectedCount = INVALID_ROW_COUNT; |
| 76 | + throw exception; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected Long _executeSql(final String query, final TypedParameter[] parameters) throws DatabaseException { |
| 81 | + try { |
| 82 | + if (_connection.isClosed()) { |
| 83 | + throw new DatabaseException("Attempted to execute SQL statement while disconnected."); |
| 84 | + } |
| 85 | + |
| 86 | + _executeAsPreparedStatement(query, parameters); |
| 87 | + return _lastInsertId; |
| 88 | + } |
| 89 | + catch (final SQLException exception) { |
| 90 | + throw new DatabaseException("Error executing SQL statement.", exception); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + protected List<Row> _query(final String query, final TypedParameter[] typedParameters) throws DatabaseException { |
| 95 | + try { |
| 96 | + if (_connection.isClosed()) { |
| 97 | + throw new DatabaseException("Attempted to execute query while disconnected."); |
| 98 | + } |
| 99 | + |
| 100 | + final List<Row> results = new ArrayList<Row>(); |
| 101 | + try (final PreparedStatement preparedStatement = _prepareStatement(query, typedParameters); |
| 102 | + final ResultSet resultSet = preparedStatement.executeQuery() ) { |
| 103 | + |
| 104 | + if (resultSet.first()) { |
| 105 | + do { |
| 106 | + results.add(_rowFactory.fromResultSet(resultSet)); |
| 107 | + } while (resultSet.next()); |
| 108 | + } |
| 109 | + } |
| 110 | + return results; |
| 111 | + } |
| 112 | + catch (final SQLException exception) { |
| 113 | + throw new DatabaseException("Error executing query.", exception); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + protected JdbcDatabaseConnection(final Connection connection, final RowFactory rowFactory) { |
| 118 | + _rowFactory = rowFactory; |
| 119 | + _connection = connection; |
| 120 | + } |
| 121 | + |
| 122 | + public JdbcDatabaseConnection(final Connection connection) { |
| 123 | + _rowFactory = new JdbcRowFactory(); |
| 124 | + _connection = connection; |
| 125 | + } |
| 126 | + |
| 127 | + public Integer getRowsAffectedCount() { |
| 128 | + return _lastRowAffectedCount; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public synchronized void executeDdl(final String query) throws DatabaseException { |
| 133 | + try { |
| 134 | + if (_connection.isClosed()) { |
| 135 | + throw new DatabaseException("Attempted to execute DDL statement while disconnected."); |
| 136 | + } |
| 137 | + |
| 138 | + try (final Statement statement = _connection.createStatement()) { |
| 139 | + statement.execute(query); |
| 140 | + } |
| 141 | + } |
| 142 | + catch (final SQLException exception) { |
| 143 | + throw new DatabaseException("Error executing DDL statement.", exception); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public synchronized void executeDdl(final Query query) throws DatabaseException { |
| 149 | + this.executeDdl(query.getQueryString()); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public synchronized Long executeSql(final String query, final String[] parameters) throws DatabaseException { |
| 154 | + final TypedParameter[] typedParameters = _stringArrayToTypedParameters(parameters); |
| 155 | + return _executeSql(query, typedParameters); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public synchronized Long executeSql(final Query query) throws DatabaseException { |
| 160 | + final String queryString = query.getQueryString(); |
| 161 | + final List<TypedParameter> parameters = query.getParameters(); |
| 162 | + return _executeSql(queryString, parameters.toArray(new TypedParameter[0])); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public synchronized List<Row> query(final String query, final String[] parameters) throws DatabaseException { |
| 167 | + final TypedParameter[] typedParameters = _stringArrayToTypedParameters(parameters); |
| 168 | + return _query(query, typedParameters); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public synchronized List<Row> query(final Query query) throws DatabaseException { |
| 173 | + final String queryString = query.getQueryString(); |
| 174 | + final List<TypedParameter> parameters = query.getParameters(); |
| 175 | + return _query(queryString, parameters.toArray(new TypedParameter[0])); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public Connection getRawConnection() { |
| 180 | + return _connection; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void close() throws DatabaseException { |
| 185 | + try { |
| 186 | + _connection.close(); |
| 187 | + } |
| 188 | + catch (final SQLException exception) { |
| 189 | + throw new DatabaseException("Unable to close database connection.", exception); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments