1 | |
package liquibase.database.core; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Arrays; |
5 | |
import java.util.HashSet; |
6 | |
import java.util.List; |
7 | |
import java.util.Set; |
8 | |
import java.util.Vector; |
9 | |
|
10 | |
import liquibase.change.ColumnConfig; |
11 | |
import liquibase.change.core.CreateTableChange; |
12 | |
import liquibase.database.AbstractDatabase; |
13 | |
import liquibase.database.Database; |
14 | |
import liquibase.database.DatabaseConnection; |
15 | |
import liquibase.database.structure.Column; |
16 | |
import liquibase.database.structure.Index; |
17 | |
import liquibase.database.structure.Table; |
18 | |
import liquibase.exception.DatabaseException; |
19 | |
import liquibase.exception.UnsupportedChangeException; |
20 | |
import liquibase.snapshot.DatabaseSnapshot; |
21 | |
import liquibase.snapshot.DatabaseSnapshotGeneratorFactory; |
22 | |
import liquibase.statement.SqlStatement; |
23 | |
import liquibase.statement.core.CopyRowsStatement; |
24 | |
import liquibase.statement.core.CreateIndexStatement; |
25 | |
import liquibase.statement.core.DropTableStatement; |
26 | |
import liquibase.statement.core.ReindexStatement; |
27 | |
import liquibase.statement.core.RenameTableStatement; |
28 | |
import liquibase.util.ISODateFormat; |
29 | |
|
30 | 3 | public class SQLiteDatabase extends AbstractDatabase { |
31 | |
|
32 | 3 | private Set<String> systemTables = new HashSet<String>(); |
33 | |
|
34 | |
{ |
35 | 3 | systemTables.add("sqlite_sequence"); |
36 | |
} |
37 | |
|
38 | |
public static final String PRODUCT_NAME = "SQLite"; |
39 | |
|
40 | |
@Override |
41 | |
public String getCurrentDateTimeFunction() { |
42 | 0 | if (currentDateTimeFunction != null) { |
43 | 0 | return currentDateTimeFunction; |
44 | |
} |
45 | |
|
46 | 0 | return "CURRENT_TIMESTAMP"; |
47 | |
} |
48 | |
|
49 | |
@Override |
50 | |
public String getDefaultDriver(String url) { |
51 | 0 | if (url.startsWith("jdbc:sqlite:")) { |
52 | 0 | return "SQLite.JDBCDriver"; |
53 | |
} |
54 | 0 | return null; |
55 | |
} |
56 | |
|
57 | |
@Override |
58 | |
public int getPriority() { |
59 | 0 | return PRIORITY_DEFAULT; |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
public String getTypeName() { |
64 | 0 | return "sqlite"; |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException { |
69 | 0 | return "SQLite".equalsIgnoreCase(conn.getDatabaseProductName()); |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public boolean supportsInitiallyDeferrableColumns() { |
74 | 0 | return false; |
75 | |
} |
76 | |
|
77 | |
@Override |
78 | |
public boolean supportsTablespaces() { |
79 | 0 | return false; |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public String getViewDefinition(String schemaName, String viewName) throws DatabaseException { |
84 | 0 | return null; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public boolean supportsSequences() { |
89 | 0 | return false; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public boolean supportsSchemas() { |
94 | 0 | return false; |
95 | |
} |
96 | |
|
97 | |
public String getTrigger(String table, String column) { |
98 | 0 | return "CREATE TRIGGER insert_" + table + "_timeEnter AFTER INSERT ON " + table + " BEGIN" + " UPDATE " |
99 | |
+ table + " SET " + column + " = DATETIME('NOW')" + " WHERE rowid = new.rowid END "; |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public String getAutoIncrementClause() { |
104 | 0 | return "AUTOINCREMENT"; |
105 | |
} |
106 | |
|
107 | |
public static List<SqlStatement> getAlterTableStatements(AlterTableVisitor alterTableVisitor, Database database, |
108 | |
String schemaName, String tableName) throws UnsupportedChangeException, DatabaseException { |
109 | |
|
110 | 0 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
111 | |
|
112 | 0 | DatabaseSnapshot snapshot = DatabaseSnapshotGeneratorFactory.getInstance().createSnapshot(database, null, null); |
113 | 0 | Table table = snapshot.getTable(tableName); |
114 | |
|
115 | 0 | List<ColumnConfig> createColumns = new Vector<ColumnConfig>(); |
116 | 0 | List<ColumnConfig> copyColumns = new Vector<ColumnConfig>(); |
117 | 0 | if (table != null) { |
118 | 0 | for (Column column : table.getColumns()) { |
119 | 0 | ColumnConfig new_column = new ColumnConfig(column); |
120 | 0 | if (alterTableVisitor.createThisColumn(new_column)) { |
121 | 0 | createColumns.add(new_column); |
122 | |
} |
123 | 0 | ColumnConfig copy_column = new ColumnConfig(column); |
124 | 0 | if (alterTableVisitor.copyThisColumn(copy_column)) { |
125 | 0 | copyColumns.add(copy_column); |
126 | |
} |
127 | 0 | } |
128 | |
} |
129 | 0 | for (ColumnConfig column : alterTableVisitor.getColumnsToAdd()) { |
130 | 0 | ColumnConfig new_column = new ColumnConfig(column); |
131 | 0 | if (alterTableVisitor.createThisColumn(new_column)) { |
132 | 0 | createColumns.add(new_column); |
133 | |
} |
134 | 0 | ColumnConfig copy_column = new ColumnConfig(column); |
135 | 0 | if (alterTableVisitor.copyThisColumn(copy_column)) { |
136 | 0 | copyColumns.add(copy_column); |
137 | |
} |
138 | |
} |
139 | |
|
140 | 0 | List<Index> newIndices = new Vector<Index>(); |
141 | 0 | for (Index index : snapshot.getIndexes()) { |
142 | 0 | if (index.getTable().getName().equalsIgnoreCase(tableName)) { |
143 | 0 | if (alterTableVisitor.createThisIndex(index)) { |
144 | 0 | newIndices.add(index); |
145 | |
} |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
|
150 | 0 | String temp_table_name = tableName + "_temporary"; |
151 | 0 | statements.add(new RenameTableStatement(schemaName, tableName, temp_table_name)); |
152 | |
|
153 | 0 | CreateTableChange ct_change_tmp = new CreateTableChange(); |
154 | 0 | ct_change_tmp.setSchemaName(schemaName); |
155 | 0 | ct_change_tmp.setTableName(tableName); |
156 | 0 | for (ColumnConfig column : createColumns) { |
157 | 0 | ct_change_tmp.addColumn(column); |
158 | |
} |
159 | 0 | statements.addAll(Arrays.asList(ct_change_tmp.generateStatements(database))); |
160 | |
|
161 | 0 | statements.add(new CopyRowsStatement(temp_table_name, tableName, copyColumns)); |
162 | |
|
163 | 0 | statements.add(new DropTableStatement(schemaName, temp_table_name, false)); |
164 | |
|
165 | 0 | statements.add(new ReindexStatement(schemaName, tableName)); |
166 | |
|
167 | 0 | for (Index index_config : newIndices) { |
168 | 0 | statements.add(new CreateIndexStatement(index_config.getName(), schemaName, tableName, index_config |
169 | |
.isUnique(), index_config.getAssociatedWithAsString(), index_config.getColumns().toArray( |
170 | |
new String[index_config.getColumns().size()]))); |
171 | |
} |
172 | |
|
173 | 0 | return statements; |
174 | |
} |
175 | |
|
176 | |
@Override |
177 | |
protected Set<String> getSystemTablesAndViews() { |
178 | 0 | return systemTables; |
179 | |
} |
180 | |
|
181 | |
@Override |
182 | |
public String getDateTimeLiteral(java.sql.Timestamp date) { |
183 | 0 | return getDateLiteral(new ISODateFormat().format(date).replaceFirst("^'", "").replaceFirst("'$", "")); |
184 | |
} |
185 | |
|
186 | 3 | public interface AlterTableVisitor { |
187 | |
public ColumnConfig[] getColumnsToAdd(); |
188 | |
|
189 | |
public boolean copyThisColumn(ColumnConfig column); |
190 | |
|
191 | |
public boolean createThisColumn(ColumnConfig column); |
192 | |
|
193 | |
public boolean createThisIndex(Index index); |
194 | |
} |
195 | |
|
196 | |
} |