001: /*
002: * Created by IntelliJ IDEA.
003: * User: sg426575
004: * Date: May 8, 2006
005: * Time: 1:27:04 AM
006: */
007: package com.technoetic.xplanner.upgrade.schema;
008:
009: import java.util.Map;
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.ArrayList;
013: import java.sql.SQLException;
014: import java.sql.Connection;
015:
016: import net.sf.hibernate.Session;
017: import net.sf.hibernate.HibernateException;
018: import org.apache.commons.lang.StringUtils;
019: import org.apache.log4j.Logger;
020: import org.springframework.orm.hibernate.HibernateTemplate;
021: import org.springframework.orm.hibernate.HibernateCallback;
022: import org.springframework.jdbc.core.JdbcTemplate;
023: import org.springframework.jdbc.core.ConnectionCallback;
024: import org.springframework.dao.DataAccessException;
025:
026: import com.technoetic.xplanner.util.LogUtil;
027:
028: public class DBDialect {
029:
030: protected static final Logger LOG = LogUtil.getLogger();
031:
032: private static final Map DIALECT_BY_DATABASE = new HashMap(10);
033:
034: static {
035: initDialectByDatabase();
036: }
037:
038: private static void initDialectByDatabase() {
039: addDialect(new MySqlTo4_0DBDialect());
040: addDialect(new MySqlFrom4_1DBDialect());
041: addDialect(new HsqldbDBDialect());
042: }
043:
044: private static void addDialect(DBDialect dialect) {
045: String vendor = dialect.getVendor();
046: List versions = (List) DIALECT_BY_DATABASE.get(vendor);
047: if (versions == null) {
048: versions = new ArrayList(1);
049: DIALECT_BY_DATABASE.put(vendor, versions);
050: }
051: versions.add(dialect);
052: }
053:
054: public static DBDialect getDialect(JdbcTemplate template) {
055: return (DBDialect) template.execute(new ConnectionCallback() {
056: public Object doInConnection(Connection connection)
057: throws SQLException, DataAccessException {
058: DBDialect dialect = (DBDialect) getDialect(connection);
059: LOG.debug("Using " + dialect
060: + " to handle schema migration of "
061: + getDatabaseVendor(connection));
062: return dialect;
063: }
064: });
065: }
066:
067: private static Object getDialect(Connection connection)
068: throws SQLException {
069: List versions = (List) DIALECT_BY_DATABASE
070: .get(getDatabaseVendor(connection));
071: if (versions != null) {
072: double version = getDatabaseVersion(connection);
073: for (int i = 0; i < versions.size(); i++) {
074: DBDialect dialect = (DBDialect) versions.get(i);
075: if (dialect.getFromVersion() <= version
076: && version <= dialect.getToVersion()) {
077: return dialect;
078: }
079: }
080: }
081: return new DBDialect();
082: }
083:
084: private static String getDatabaseVendor(Connection connection)
085: throws SQLException {
086: return connection.getMetaData().getDatabaseProductName();
087: }
088:
089: private static double getDatabaseVersion(Connection connection)
090: throws SQLException {
091: int major = connection.getMetaData().getDatabaseMajorVersion();
092: int minor = connection.getMetaData().getDatabaseMinorVersion();
093: double version = Double.parseDouble("" + major + "." + minor);
094: return version;
095: }
096:
097: public String getVendor() {
098: return "default";
099: }
100:
101: public double getFromVersion() {
102: return Double.MIN_VALUE;
103: }
104:
105: public double getToVersion() {
106: return Double.MAX_VALUE;
107: }
108:
109: public String toString() {
110: String str = getVendor();
111: String min = getFromVersion() == Double.MIN_VALUE ? "first"
112: : "" + getFromVersion();
113: String max = getToVersion() == Double.MAX_VALUE ? "latest" : ""
114: + getToVersion();
115: return "{" + str + " [" + min + "-" + max + "]}";
116: }
117:
118: public String getAddForeignKeyConstraintQuery(String table,
119: String constraintName, String foreignKey,
120: String referencedTable, String referencedKey) {
121: return "alter table " + table + " add constraint "
122: + constraintName + " foreign key (" + foreignKey
123: + ") references " + referencedTable + " ("
124: + referencedKey + ")";
125: }
126:
127: public String getDropForeignKeyConstraintQuery(String table,
128: String constraintName) {
129: return getDropConstraintQuery(table, constraintName);
130: }
131:
132: public String getAddUniqueConstraintQuery(String table,
133: String constraintName, String column) {
134: return "alter table " + table + " add constraint "
135: + constraintName + " unique " + constraintName + " ( "
136: + column + " )";
137: }
138:
139: public String getDropUniqueConstraintQuery(String table,
140: String constraintName) {
141: return getDropConstraintQuery(table, constraintName);
142: }
143:
144: private String getDropConstraintQuery(String table,
145: String constraintName) {
146: return "alter table " + table + " drop constraint "
147: + constraintName;
148: }
149:
150: public String getCreateTableQuery(String table, String fieldSQL) {
151: return "create table " + table + " ( " + fieldSQL + " )";
152: }
153:
154: public String getDropTableQuery(String table) {
155: return "drop table " + table;
156: }
157:
158: public String getCreateIndexQuery(String table, String name,
159: String field, boolean unique) {
160: return "create " + (unique ? "unique" : "") + " index " + name
161: + " " + " on " + table + " ( " + field + " ) ";
162: }
163:
164: public String getDropIndexQuery(String table, String name) {
165: return "drop index " + name + " on " + table;
166: }
167:
168: public String getAddPrimaryKeyQuery(String table,
169: String[] primaryColumns) {
170: return "alter table " + table + " add primary key ( "
171: + StringUtils.join(primaryColumns, ',') + " )";
172: }
173:
174: public String getChangePrimaryKeyQuery(String table,
175: String[] primaryColumns) {
176: return "alter table " + table + " alter primary key ( "
177: + StringUtils.join(primaryColumns, ',') + " )";
178: }
179:
180: public String getDropPrimaryKeyQuery(String table) {
181: return "alter table " + table + " drop primary key";
182: }
183:
184: public String getRenameColumnQuery(String table, String oldName,
185: String newName, String type) {
186: return "alter table " + table + " change " + oldName + " "
187: + newName + " " + type;
188: }
189:
190: }
|