forked from pardom-zz/ActiveAndroid
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTableDifference.java
More file actions
86 lines (69 loc) · 2.67 KB
/
TableDifference.java
File metadata and controls
86 lines (69 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.activeandroid.automigration;
import android.util.Log;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.activeandroid.TableInfo;
import com.activeandroid.automigration.AutoMigration.IncompatibleColumnTypesException;
import com.activeandroid.util.SQLiteUtils;
class TableDifference {
private TableInfo mTableInfo;
private SQLTableInfo mSqlTableInfo;
private Map<SQLColumnInfo, SQLColumnInfo> mDifferences;
private List<SQLColumnInfo> mCurrentVersionTableDefinitions;
public TableDifference(TableInfo tableInfo, SQLTableInfo sqlTableInfo) {
this.mTableInfo = tableInfo;
this.mSqlTableInfo = sqlTableInfo;
this.mDifferences = new HashMap<SQLColumnInfo, SQLColumnInfo>();
this.mCurrentVersionTableDefinitions = new ArrayList<SQLColumnInfo>();
for (Field field : tableInfo.getFields()) {
SQLColumnInfo sqlColumnInfo = new SQLColumnInfo(SQLiteUtils.createColumnDefinition(tableInfo, field));
mCurrentVersionTableDefinitions.add(sqlColumnInfo);
boolean found = false;
for (SQLColumnInfo existingColumnInfo : sqlTableInfo.getColumns()) {
if (existingColumnInfo.getName().equalsIgnoreCase(sqlColumnInfo.getName()) == false)
continue;
found = true;
if (existingColumnInfo.getColumnDefinition().equalsIgnoreCase(sqlColumnInfo.getColumnDefinition()) == false) {
if (existingColumnInfo.getType() == sqlColumnInfo.getType()) {
mDifferences.put(sqlColumnInfo, existingColumnInfo);
} else {
// allow column type changes just to let SQLite attempt to cast these values
Log.w(TableDifference.class.getName(), "potentially incompatible column types (table='"
+ tableInfo.getTableName() + "' column='" + existingColumnInfo.getName()
+ "' current type='" + existingColumnInfo.getType() + "' new type='"
+ sqlColumnInfo.getType() + "')");
mDifferences.put(sqlColumnInfo, existingColumnInfo);
}
}
break;
}
if (!found)
mDifferences.put(sqlColumnInfo, null);
}
}
public boolean isOnlyAdd() {
for (SQLColumnInfo sqlColumnInfo : mDifferences.keySet()) {
if (mDifferences.get(sqlColumnInfo) != null || sqlColumnInfo.isPrimaryKey() || sqlColumnInfo.isUnique())
return false;
}
return true;
}
public boolean isEmpty() {
return mDifferences.size() == 0;
}
public Map<SQLColumnInfo, SQLColumnInfo> getDifferences() {
return mDifferences;
}
public List<SQLColumnInfo> getNewSchemaColumnInfos() {
return mCurrentVersionTableDefinitions;
}
public TableInfo getTableInfo() {
return mTableInfo;
}
public SQLTableInfo getSqlTableInfo() {
return mSqlTableInfo;
}
}