-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDBEvolution.cs
More file actions
200 lines (180 loc) · 6.14 KB
/
DBEvolution.cs
File metadata and controls
200 lines (180 loc) · 6.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityORM
{
/// <summary>
/// Schme management tool.
/// Execute only diff sqls.
/// </summary>
public class DBEvolution
{
private string dbFilePath;
private SqliteDatabase database;
public SqliteDatabase Database {
get {
if (database == null)
{
database = new SqliteDatabase ();
database.Open (dbFilePath);
CreateMetaTable ();
}
return database;
}
}
public bool RecreateTableIfHashDiffers{ get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="com.geishatokyo.DB.DBEvolution"/> class.
/// </summary>
/// <param name='dbFilePath'>
/// Db file path.
/// </param>
public DBEvolution (string dbFilePath)
{
this.dbFilePath = dbFilePath;
RecreateTableIfHashDiffers = false;
}
/// <summary>
/// Evolute the specified tableName with sqls.
/// </summary>
/// <param name='tableName'>
/// Table name.
/// </param>
/// <param name='sqls'>
/// Sqls.
/// </param>
public void Evolute (string tableName, List<string> sqls)
{
TableVersion version = GetTableVersion (tableName);
var db = this.Database;
if (RecreateTableIfHashDiffers && version.QueryHash != null)
{
string hash;
if (sqls.Count > version.QueryIndex)
{
hash = GetHash (sqls.GetRange (0, version.QueryIndex + 1));
}
else
{
hash = string.Empty;
}
if (hash != version.QueryHash)
{
Debug.Log (string.Format ("Table hash error.OnDB:{0} Passed:{1}", version.QueryHash, hash));
DropTable (tableName);
version.QueryIndex = -1;
version.QueryHash = null;
}
}
// TODO: use updated?
//bool updated = false;
int successIndex = -1;
bool error = false;
for (int i = version.QueryIndex + 1; i < sqls.Count; i++)
{
Debug.Log (string.Format ("Execute query of {0}:{1}", tableName, i));
//updated = true;
try
{
db.ExecuteNonQuery (sqls [i]);
successIndex = i;
}
catch (SqliteException e)
{
error = true;
Debug.LogError ("Error SQL:" + sqls [i]);
Debug.LogException (e);
break;
}
}
if (successIndex >= 0)
{
sqls = sqls.GetRange (0, successIndex + 1);
if (version.QueryHash == null)
{
string hashCode = GetHash (sqls);
CreateRecord (tableName, sqls.Count - 1, hashCode);
}
else
{
string hashCode = GetHash (sqls);
UpdateRecord (tableName, sqls.Count - 1, hashCode);
}
}
if (error)
{
Debug.LogError ("There are some errors.Please fix your sql");
}
}
public void Close ()
{
if (database != null)
{
database.Close ();
database = null;
}
}
private string GetHash (List<string> sqls)
{
string join = string.Join ("_", sqls.ToArray ());
return join.GetHashCode ().ToString ();
}
void CreateRecord (string tableName, int index, string queryHash)
{
string sql = string.Format (
@"INSERT INTO MetaTable VALUES('{0}',{1},'{2}');", tableName, index, queryHash);
Database.ExecuteNonQuery (sql);
}
void UpdateRecord (string tableName, int index, string queryHash)
{
string sql = string.Format (
@"UPDATE MetaTable SET queryIndex={0},queryHash='{1}' WHERE tableName='{2}';",
index, queryHash, tableName);
Database.ExecuteNonQuery (sql);
}
private void DropTable (string tableName)
{
Debug.Log ("DROP TABLE IF EXISTS " + tableName);
string sql = string.Format (@"DROP TABLE IF EXISTS {0};", tableName);
Database.ExecuteNonQuery (sql);
string metaSql = string.Format (@"DELETE FROM MetaTable WHERE tableName='{0}';", tableName);
Database.ExecuteNonQuery (metaSql);
}
private TableVersion GetTableVersion (string tableName)
{
string sql = @"SELECT * FROM MetaTable WHERE tableName = '" + tableName + "';";
DataTable dt = Database.ExecuteQuery (sql);
TableVersion tv = new TableVersion ();
if (dt.Rows.Count > 0)
{
tv.TableName = tableName;
var row = dt.Rows [0];
tv.QueryIndex = row.GetAsInt ("queryIndex");
tv.QueryHash = row ["queryHash"].ToString ();
}
else
{
tv.TableName = tableName;
tv.QueryIndex = -1;
tv.QueryHash = null;
}
return tv;
}
private bool CreateMetaTable ()
{
string createTable = @"CREATE TABLE IF NOT EXISTS MetaTable(
tableName VARCHAR(100) NOT NULL,
queryIndex Int NOT NULL,
queryHash VARCHAR(100) NOT NULL);";
Database.ExecuteNonQuery (createTable);
// TODO fix to check table is really created.
return true;
}
}
struct TableVersion
{
public string TableName;
public int QueryIndex;
public string QueryHash;
}
}