001: package liquibase.database;
002:
003: import liquibase.database.sql.RawSqlStatement;
004: import liquibase.database.sql.SqlStatement;
005: import liquibase.exception.DateParseException;
006: import liquibase.exception.JDBCException;
007: import liquibase.util.StringUtils;
008:
009: import java.sql.Connection;
010: import java.text.ParseException;
011: import java.text.SimpleDateFormat;
012: import java.util.Date;
013:
014: public class H2Database extends HsqlDatabase {
015:
016: public String getProductName() {
017: return "H2 Database";
018: }
019:
020: public String getTypeName() {
021: return "h2";
022: }
023:
024: public String getDefaultDriver(String url) {
025: if (url.startsWith("jdbc:h2")) {
026: return "org.h2.Driver";
027: }
028: return null;
029: }
030:
031: public boolean isCorrectDatabaseImplementation(Connection conn)
032: throws JDBCException {
033: return "H2".equals(getDatabaseProductName(conn));
034: }
035:
036: public SqlStatement createFindSequencesSQL(String schema)
037: throws JDBCException {
038: return new RawSqlStatement(
039: "SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA = '"
040: + convertRequestedSchemaToSchema(schema)
041: + "' AND IS_GENERATED=FALSE");
042: }
043:
044: // public void dropDatabaseObjects(String schema) throws JDBCException {
045: // DatabaseConnection conn = getConnection();
046: // Statement dropStatement = null;
047: // try {
048: // dropStatement = conn.createStatement();
049: // dropStatement.executeUpdate("DROP ALL OBJECTS");
050: // changeLogTableExists = false;
051: // changeLogLockTableExists = false;
052: // changeLogCreateAttempted = false;
053: // changeLogLockCreateAttempted = false;
054: // } catch (SQLException e) {
055: // throw new JDBCException(e);
056: // } finally {
057: // try {
058: // if (dropStatement != null) {
059: // dropStatement.close();
060: // }
061: // conn.commit();
062: // } catch (SQLException e) {
063: // ;
064: // }
065: // }
066: //
067: // }
068:
069: public boolean supportsTablespaces() {
070: return false;
071: }
072:
073: public String getViewDefinition(String schemaName, String name)
074: throws JDBCException {
075: return super .getViewDefinition(schemaName, name).replaceFirst(
076: ".*?\n", ""); //h2 returns "create view....as\nselect
077: }
078:
079: public SqlStatement getViewDefinitionSql(String schemaName,
080: String name) throws JDBCException {
081: return new RawSqlStatement(
082: "SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '"
083: + name + "' AND TABLE_SCHEMA='"
084: + convertRequestedSchemaToSchema(schemaName)
085: + "'");
086: }
087:
088: public Object convertDatabaseValueToJavaObject(Object defaultValue,
089: int dataType, int columnSize, int decimalDigits)
090: throws ParseException {
091: if (defaultValue != null && defaultValue instanceof String) {
092: if (StringUtils.trimToEmpty(((String) defaultValue))
093: .startsWith(
094: "(NEXT VALUE FOR PUBLIC.SYSTEM_SEQUENCE_")) {
095: return null;
096: }
097: if (StringUtils.trimToNull(((String) defaultValue)) == null) {
098: return null;
099: }
100: }
101: return super .convertDatabaseValueToJavaObject(defaultValue,
102: dataType, columnSize, decimalDigits);
103: }
104:
105: protected Date parseDate(String dateAsString)
106: throws DateParseException {
107: try {
108: if (dateAsString.indexOf(' ') > 0) {
109: return new SimpleDateFormat(
110: "yyyy-MM-dd HH:mm:ss.SSSSSSSSS")
111: .parse(dateAsString);
112: } else {
113: if (dateAsString.indexOf(':') > 0) {
114: return new SimpleDateFormat("HH:mm:ss")
115: .parse(dateAsString);
116: } else {
117: return new SimpleDateFormat("yyyy-MM-dd")
118: .parse(dateAsString);
119: }
120: }
121: } catch (ParseException e) {
122: throw new DateParseException(dateAsString);
123: }
124: }
125:
126: public String getDateTimeType() {
127: return "TIMESTAMP";
128: }
129: }
|