-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSQLiteDesign.cs
190 lines (159 loc) · 4.74 KB
/
SQLiteDesign.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
namespace SQLiteDesign
{
class OpenTable
{
public SQLiteDataAdapter Adapter;
public DataTable Table;
}
class ReplaceCommandBuilder
{
}
class SQLiteDatabase
{
protected SQLiteConnection m_DbConnection;
protected DataSet m_Dataset;
protected List<OpenTable> m_OpenTables;
public SQLiteDatabase(SQLiteConnection a_DbConnection, DataSet a_DataSet)
{
m_DbConnection = a_DbConnection;
m_Dataset = a_DataSet;
}
public DataSet DataSet
{
get
{
return m_Dataset;
}
}
protected virtual string NetTypeToSQLiteType(Type a_NetType)
{
switch (a_NetType.Name)
{
case "String":
return "text";
case "UInt64":
case "UInt32":
case "UInt16":
case "UInt8":
case "Int64":
case "Int32":
case "Int16":
case "Int8":
return "integer";
case "Double":
case "double":
return "real";
}
string errorText = "Bad type in NetTypeToSQLiteType:" + a_NetType.Name;
Debug.Assert(false, errorText);
throw new global::System.ArgumentException(errorText);
}
// WARNING this method is not finished and ignores many features
protected string GetCreateTableSql(DataTable a_Table)
{
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.Append("CREATE TABLE IF NOT EXISTS ");
resultBuilder.Append(a_Table.TableName);
resultBuilder.Append(" (");
for (int i = 0; i < a_Table.Columns.Count; i++)
{
DataColumn currColumn = a_Table.Columns[i];
if (i != 0)
resultBuilder.Append(", ");
resultBuilder.Append(currColumn.ColumnName);
resultBuilder.Append(" ");
resultBuilder.Append(NetTypeToSQLiteType(currColumn.DataType));
}
if (0 != a_Table.PrimaryKey.Count())
{
resultBuilder.Append(", PRIMARY KEY (");
for (int i = 0; i < a_Table.PrimaryKey.Count(); i++)
{
DataColumn currColumn = a_Table.PrimaryKey[i];
if (i != 0)
resultBuilder.Append(", ");
resultBuilder.Append(currColumn.ColumnName);
}
resultBuilder.Append(")");
}
resultBuilder.Append(");");
return resultBuilder.ToString();
}
// Summary:
// Creates tables in Database according to the design
public void CreateTables()
{
foreach (DataTable currTable in m_Dataset.Tables)
{
SQLiteCommand createTableCmd = m_DbConnection.CreateCommand();
createTableCmd.CommandText = GetCreateTableSql(currTable);
createTableCmd.ExecuteNonQuery();
}
}
// Summary:
// Load tables from database into a DataSet
// WARNING consumes a lot of memory since it loads the entire database into memory!
public void LoadDatabase()
{
if (m_OpenTables != null)
return;
m_OpenTables = new List<OpenTable>();
foreach (DataTable currTable in m_Dataset.Tables)
{
OpenTable newTable = new OpenTable();
newTable.Table = currTable;
newTable.Adapter = new SQLiteDataAdapter("Select * from " + currTable.TableName, m_DbConnection);
newTable.Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
newTable.Adapter.Fill(m_Dataset, currTable.TableName);
SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder(newTable.Adapter);
m_OpenTables.Add(newTable);
}
}
// Summary:
// Saves changes made to assigned DataSet after loading with LoadDatabase()
public void SaveDataSetChanges()
{
if (m_OpenTables == null)
return;
foreach (OpenTable currTable in m_OpenTables)
{
currTable.Adapter.Update(currTable.Table);
}
}
// Summary:
// Generates a command usable for replacing rows in a specified table
private SQLiteCommand GetReplaceCommand(DataTable a_Template)
{
SQLiteDataAdapter adapter = new SQLiteDataAdapter("Select * from " + a_Template.TableName, m_DbConnection);
SQLiteCommandBuilder cmdBuilder = new SQLiteCommandBuilder(adapter);
SQLiteCommand replaceCommand = (SQLiteCommand)cmdBuilder.GetInsertCommand().Clone();
replaceCommand.CommandText = replaceCommand.CommandText.Replace("INSERT INTO", "REPLACE INTO");
// remove SQLiteCommandBuilder's side effects
cmdBuilder.DataAdapter = null;
return replaceCommand;
}
// Summary:
// Replaces rows into a database table
public void ReplaceRows(DataTable a_Template, DataRowCollection a_Rows)
{
SQLiteCommand replaceCommand = GetReplaceCommand(a_Template);
SQLiteTransaction transaction = m_DbConnection.BeginTransaction();
foreach (DataRow currRow in a_Rows)
{
foreach (SQLiteParameter currParam in replaceCommand.Parameters)
{
currParam.Value = currRow[currParam.SourceColumn];
}
replaceCommand.ExecuteNonQuery();
}
transaction.Commit();
}
}
}