001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.jpa.entity;
021
022 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
023 import org.apache.commons.lang.builder.ToStringStyle;
024
025 import javax.persistence.*;
026
027 import java.sql.Connection;
028 import java.sql.ResultSet;
029 import java.sql.SQLException;
030 import java.sql.Statement;
031
032 @Entity
033 @Table(name = SchemaMigration.TABLE_NAME, uniqueConstraints = {@UniqueConstraint(columnNames = {"version"})})
034 public class SchemaMigration {
035
036 public static final int VERSION_UNKNOWN = -1;
037
038 public static final int LAST_VERSION = 263;
039 public static final int VERSION_2_13 = 241;
040
041 public static final String TABLE_NAME = "schema_migrations";
042
043 @Id
044 @Column(name = "version", updatable = true)
045 private String version;
046
047 public String getVersion() {
048 return version;
049 }
050
051 public void setVersion(String s) {
052 this.version = s;
053 }
054
055 public void setVersion(int i) {
056 this.version = String.valueOf(i);
057 }
058
059 public static int getCurrentVersion(Connection connection) {
060 Statement stmt = null;
061 ResultSet rs = null;
062 int version = VERSION_UNKNOWN;
063 try {
064 stmt = connection.createStatement();
065 rs = stmt.executeQuery("SELECT version FROM " + SchemaMigration.TABLE_NAME);
066 while (rs.next()) {
067 int i = Integer.parseInt(rs.getString(1));
068 if (i > version) {
069 version = i;
070 }
071 }
072 } catch (SQLException e) {
073 // ignore
074 } finally {
075 close(rs);
076 close(stmt);
077 }
078
079 return version;
080 }
081
082 private static void close(ResultSet rs) {
083 if (rs != null) {
084 try {
085 rs.close();
086 } catch (SQLException e) {
087 // why does close() throw a checked-exception ???
088 }
089 }
090 }
091
092 private static void close(Statement st) {
093 if (st != null) {
094 try {
095 st.close();
096 } catch (SQLException e) {
097 // why does close() throw a checked-exception ???
098 }
099 }
100 }
101
102 @Override
103 public String toString() {
104 return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
105 }
106 }