1 package org.riverock.dbrevision.manager.dao;
2
3 import org.riverock.dbrevision.manager.RevisionBean;
4 import org.riverock.dbrevision.db.Database;
5 import org.riverock.dbrevision.db.DatabaseStructureManager;
6 import org.riverock.dbrevision.annotation.schema.db.DbTable;
7 import org.riverock.dbrevision.annotation.schema.db.DbField;
8 import org.riverock.dbrevision.Constants;
9 import org.riverock.dbrevision.utils.DbUtils;
10 import org.riverock.dbrevision.exception.DbRevisionException;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.sql.DatabaseMetaData;
15 import java.sql.Types;
16 import java.sql.SQLException;
17 import java.sql.ResultSet;
18 import java.sql.PreparedStatement;
19 import java.sql.Connection;
20
21
22
23
24
25
26 public class ManagerDaoImpl implements ManagerDao {
27 private static final String SELECT_REVISION_SQL = "select MODULE_NAME, CURRENT_VERSION, LAST_PATCH from "+ Constants.DB_REVISION_TABLE_NAME;
28
29 public List<RevisionBean> getRevisions(Database database) {
30 checkDbRevisionTableExist(database);
31 List<RevisionBean> list = new ArrayList<RevisionBean>();
32 ResultSet rs = null;
33 PreparedStatement ps = null;
34 try {
35 ps = database.getConnection().prepareStatement(SELECT_REVISION_SQL);
36 rs = ps.executeQuery();
37 while (rs.next()) {
38 RevisionBean bean = new RevisionBean();
39 bean.setModuleName(rs.getString("MODULE_NAME"));
40 bean.setCurrentVerson(rs.getString("CURRENT_VERSION"));
41 bean.setLastPatch(rs.getString("LAST_PATCH"));
42 if (rs.wasNull()) {
43 bean.setLastPatch(null);
44 }
45 list.add(bean);
46 }
47 }
48 catch (SQLException e) {
49 throw new DbRevisionException(e);
50 }
51 finally {
52 DbUtils.close(rs, ps);
53
54 rs = null;
55
56 ps = null;
57 }
58 return list;
59 }
60
61 public RevisionBean getRevision(Database database, String moduleName, String versionName) {
62 checkDbRevisionTableExist(database);
63 ResultSet rs = null;
64 PreparedStatement ps = null;
65 try {
66 ps = database.getConnection().prepareStatement(
67 "select MODULE_NAME, CURRENT_VERSION, LAST_PATCH from "+ Constants.DB_REVISION_TABLE_NAME + ' ' +
68 "where MODULE_NAME=? and CURRENT_VERSION=?"
69 );
70 ps.setString(1, moduleName);
71 ps.setString(2, versionName);
72 rs = ps.executeQuery();
73 RevisionBean revision=null;
74 if (rs.next()) {
75 revision = new RevisionBean();
76 revision.setModuleName(rs.getString("MODULE_NAME"));
77 revision.setCurrentVerson(rs.getString("CURRENT_VERSION"));
78 revision.setLastPatch(rs.getString("LAST_PATCH"));
79 if (rs.wasNull()) {
80 revision.setLastPatch(null);
81 }
82 }
83 return revision;
84 }
85 catch (SQLException e) {
86 throw new DbRevisionException(e);
87 }
88 finally {
89 DbUtils.close(rs, ps);
90
91 rs = null;
92
93 ps = null;
94 }
95 }
96
97 public void checkDbRevisionTableExist(Database database) {
98 try {
99 DatabaseMetaData metaData = database.getConnection().getMetaData();
100 String dbSchema = database.getDefaultSchemaName(metaData);
101 List<DbTable> list = DatabaseStructureManager.getTableList(database.getConnection(), dbSchema, Constants.DB_REVISION_TABLE_NAME);
102 if (list.isEmpty()) {
103 DbTable table = new DbTable();
104 table.setName(Constants.DB_REVISION_TABLE_NAME);
105 table.setSchema(null);
106
107 table.getFields().add(getField("MODULE_NAME", Types.VARCHAR, 50, 0, 0));
108 table.getFields().add(getField("CURRENT_VERSION", Types.VARCHAR, 50, 0, 0));
109 table.getFields().add(getField("LAST_PATCH", Types.VARCHAR, 50, 0, 1));
110
111
112
113
114
115
116
117
118 database.createTable(table);
119 }
120 }
121 catch (SQLException e) {
122 throw new DbRevisionException(e);
123 }
124 }
125
126 public void makrCurrentVersion(Database database, String moduleName, String versionName, String patchName) {
127 checkDbRevisionTableExist(database);
128 ResultSet rs = null;
129 PreparedStatement ps = null;
130 try {
131 Connection conn = database.getConnection();
132 DbUtils.runSQL(
133 conn,
134 "delete from "+Constants.DB_REVISION_TABLE_NAME + " where MODULE_NAME=?",
135 new Object[]{moduleName},
136 new int[] {Types.VARCHAR}
137 );
138
139 ps = conn.prepareStatement(
140 "insert into " + Constants.DB_REVISION_TABLE_NAME+ " " +
141 "(MODULE_NAME, CURRENT_VERSION, LAST_PATCH)" +
142 "values" +
143 "(?, ?, ?)"
144 );
145 ps.setString(1, moduleName);
146 ps.setString(2, versionName);
147 if (patchName!=null) {
148 ps.setString(3, patchName);
149 }
150 else {
151 ps.setNull(3, Types.VARCHAR);
152 }
153 ps.executeUpdate();
154
155 conn.commit();
156 }
157 catch (SQLException e) {
158 throw new DbRevisionException(e);
159 }
160 finally {
161 DbUtils.close(rs, ps);
162
163 rs = null;
164
165 ps = null;
166 }
167 }
168
169 public RevisionBean getRevision(Database database, String moduleName) {
170 checkDbRevisionTableExist(database);
171 ResultSet rs = null;
172 PreparedStatement ps = null;
173 try {
174 ps = database.getConnection().prepareStatement(
175 "select MODULE_NAME, CURRENT_VERSION, LAST_PATCH from "+ Constants.DB_REVISION_TABLE_NAME + ' ' +
176 "where MODULE_NAME=?"
177 );
178 ps.setString(1, moduleName);
179 rs = ps.executeQuery();
180 RevisionBean revision=null;
181 if (rs.next()) {
182 revision = new RevisionBean();
183 revision.setModuleName(rs.getString("MODULE_NAME"));
184 revision.setCurrentVerson(rs.getString("CURRENT_VERSION"));
185 revision.setLastPatch(rs.getString("LAST_PATCH"));
186 if (rs.wasNull()) {
187 revision.setLastPatch(null);
188 }
189 }
190 return revision;
191 }
192 catch (SQLException e) {
193 throw new DbRevisionException(e);
194 }
195 finally {
196 DbUtils.close(rs, ps);
197
198 rs = null;
199
200 ps = null;
201 }
202 }
203
204 private static DbField getField(String name, int type, int size, int decimalDigit, int nullable) {
205 DbField field;
206 field = new DbField();
207 field.setName(name);
208 field.setJavaType(type);
209 field.setSize(size);
210 field.setDecimalDigit(decimalDigit);
211 field.setNullable(nullable);
212 return field;
213 }
214 }