001: package liquibase.database;
002:
003: import liquibase.database.sql.RawSqlStatement;
004: import liquibase.database.sql.SqlStatement;
005: import liquibase.exception.JDBCException;
006:
007: import java.sql.Connection;
008: import java.sql.Types;
009: import java.text.ParseException;
010:
011: public class DerbyDatabase extends AbstractDatabase {
012:
013: public boolean isCorrectDatabaseImplementation(Connection conn)
014: throws JDBCException {
015: return "Apache Derby"
016: .equalsIgnoreCase(getDatabaseProductName(conn));
017: }
018:
019: public String getDefaultDriver(String url) {
020: if (url.startsWith("jdbc:derby")) {
021: return "org.apache.derby.jdbc.EmbeddedDriver";
022: }
023: return null;
024: }
025:
026: public String getProductName() {
027: return "Apache Derby";
028: }
029:
030: public String getTypeName() {
031: return "derby";
032: }
033:
034: protected String getDefaultDatabaseSchemaName()
035: throws JDBCException {//NOPMD
036: return super .getDefaultDatabaseSchemaName().toUpperCase();
037: }
038:
039: public boolean supportsSequences() {
040: return false;
041: }
042:
043: public boolean supportsInitiallyDeferrableColumns() {
044: return false;
045: }
046:
047: public String getBooleanType() {
048: return "SMALLINT";
049: }
050:
051: public String getCurrencyType() {
052: return "DECIMAL";
053: }
054:
055: public String getUUIDType() {
056: return "CHAR(36)";
057: }
058:
059: public String getClobType() {
060: return "CLOB";
061: }
062:
063: public String getBlobType() {
064: return "BLOB";
065: }
066:
067: public String getDateTimeType() {
068: return "TIMESTAMP";
069: }
070:
071: public String getCurrentDateTimeFunction() {
072: return "CURRENT_TIMESTAMP";
073: }
074:
075: public String getFalseBooleanValue() {
076: return "0";
077: }
078:
079: public String getTrueBooleanValue() {
080: return "1";
081: }
082:
083: public String getAutoIncrementClause() {
084: return "GENERATED BY DEFAULT AS IDENTITY";
085: }
086:
087: public String getDateLiteral(String isoDate) {
088: if (isDateOnly(isoDate)) {
089: return "DATE(" + super .getDateLiteral(isoDate) + ")";
090: } else if (isTimeOnly(isoDate)) {
091: return "TIME(" + super .getDateLiteral(isoDate) + ")";
092: } else {
093: String dateString = super .getDateLiteral(isoDate);
094: int decimalDigits = dateString.length()
095: - dateString.indexOf('.') - 2;
096: String padding = "";
097: for (int i = 6; i > decimalDigits; i--) {
098: padding += "0";
099: }
100: return "TIMESTAMP("
101: + dateString.replaceFirst("'$", padding + "'")
102: + ")";
103: }
104: }
105:
106: public boolean supportsTablespaces() {
107: return false;
108: }
109:
110: public SqlStatement getViewDefinitionSql(String schemaName,
111: String name) throws JDBCException {
112: return new RawSqlStatement(
113: "select V.VIEWDEFINITION from SYS.SYSVIEWS V, SYS.SYSTABLES T, SYS.SYSSCHEMAS S WHERE V.TABLEID=T.TABLEID AND T.SCHEMAID=S.SCHEMAID AND T.TABLETYPE='V' AND T.TABLENAME='"
114: + name
115: + "' AND S.SCHEMANAME='"
116: + convertRequestedSchemaToSchema(schemaName)
117: + "'");
118: }
119:
120: public String getViewDefinition(String schemaName, String name)
121: throws JDBCException {
122: return super .getViewDefinition(schemaName, name).replaceFirst(
123: "CREATE VIEW \\w+ AS ", "");
124: }
125:
126: public void setConnection(Connection conn) {
127: super .setConnection(new DerbyConnectionDelegate(conn));
128: }
129:
130: public Object convertDatabaseValueToJavaObject(Object defaultValue,
131: int dataType, int columnSize, int decimalDigits)
132: throws ParseException {
133: if (defaultValue != null && defaultValue instanceof String) {
134: if (dataType == Types.TIMESTAMP) {
135: defaultValue = ((String) defaultValue).replaceFirst(
136: "^TIMESTAMP\\('", "").replaceFirst("'\\)", "");
137: } else if (dataType == Types.DATE) {
138: defaultValue = ((String) defaultValue).replaceFirst(
139: "^DATE\\('", "").replaceFirst("'\\)", "");
140: } else if (dataType == Types.TIME) {
141: defaultValue = ((String) defaultValue).replaceFirst(
142: "^TIME\\('", "").replaceFirst("'\\)", "");
143: }
144: }
145: return super.convertDatabaseValueToJavaObject(defaultValue,
146: dataType, columnSize, decimalDigits);
147: }
148: }
|