View Javadoc
1   /*
2    *    Copyright 2010-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.migration.operations;
17  
18  import java.math.BigDecimal;
19  import java.sql.Connection;
20  import java.sql.PreparedStatement;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.sql.Statement;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.ibatis.migration.Change;
28  import org.apache.ibatis.migration.options.DatabaseOperationOption;
29  
30  public class ChangelogOperation {
31    private final Connection con;
32    private final DatabaseOperationOption option;
33  
34    public ChangelogOperation(Connection con, DatabaseOperationOption option) {
35      this.con = con;
36      this.option = option;
37    }
38  
39    public boolean tableExists() {
40      try (Statement stmt = con.createStatement();
41          ResultSet rs = stmt.executeQuery("select count(1) from " + option.getChangelogTable())) {
42        return rs.next();
43      } catch (SQLException e) {
44        return false;
45      }
46    }
47  
48    public List<Change> selectAll() throws SQLException {
49      List<Change> changes = new ArrayList<>();
50      try (
51          PreparedStatement stmt = con
52              .prepareStatement("select ID, APPLIED_AT, DESCRIPTION from " + option.getChangelogTable() + " order by ID");
53          ResultSet rs = stmt.executeQuery()) {
54        while (rs.next()) {
55          changes.add(new Change(rs.getBigDecimal(1), rs.getString(2), rs.getString(3)));
56        }
57        return changes;
58      }
59    }
60  
61    public void insert(Change change) throws SQLException {
62      try (PreparedStatement stmt = con.prepareStatement(
63          "insert into " + option.getChangelogTable() + " (ID, APPLIED_AT, DESCRIPTION) values (?,?,?)")) {
64        stmt.setBigDecimal(1, change.getId());
65        stmt.setString(2, change.getAppliedTimestamp());
66        stmt.setString(3, change.getDescription());
67        stmt.execute();
68        con.commit();
69      }
70    }
71  
72    public void deleteById(BigDecimal id) throws SQLException {
73      try (PreparedStatement stmt = con.prepareStatement("delete from " + option.getChangelogTable() + " where ID = ?")) {
74        stmt.setBigDecimal(1, id);
75        stmt.execute();
76        con.commit();
77      }
78    }
79  }