-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteDataTable.cs
More file actions
136 lines (124 loc) · 5.18 KB
/
SQLiteDataTable.cs
File metadata and controls
136 lines (124 loc) · 5.18 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
using System.Data.SQLite;
using SharpEngine.Core.Data.DataTable;
namespace SharpEngine.SQLite;
/// <summary>
/// Sqlite Data Table
/// </summary>
public class SQLiteDataTable<T> : IDataTable<T>
where T : class, new()
{
private List<T> Objects { get; }
private string DbFile { get; set; }
private string Version { get; set; }
/// <summary>
/// Create Data Table from SQLite
/// </summary>
/// <param name="dbFile">SQLite File</param>
/// <param name="version">Version</param>
/// <exception cref="NotImplementedException">If use not implement type</exception>
public SQLiteDataTable(string dbFile, string version = "3")
{
DbFile = dbFile;
Version = version;
Objects = [];
var connection = new SQLiteConnection(
$"Data Source={dbFile};Version={version};New=True;Compress=True;"
);
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = $"SELECT * FROM {typeof(T).Name};";
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var obj = new T();
var index = 0;
foreach (var property in typeof(T).GetProperties())
{
if (reader.IsDBNull(index))
property.SetValue(obj, null);
else if (property.PropertyType == typeof(string))
property.SetValue(obj, reader.GetString(index));
else if (property.PropertyType == typeof(int))
property.SetValue(obj, reader.GetInt32(index));
else if (property.PropertyType == typeof(bool))
property.SetValue(obj, reader.GetBoolean(index));
else if (property.PropertyType == typeof(float))
property.SetValue(obj, reader.GetFloat(index));
else
throw new NotImplementedException(
$"Not implemented type : {property.PropertyType.Name}"
);
index++;
}
Objects.Add(obj);
}
connection.Close();
}
/// <inheritdoc />
public void Add(T obj)
{
var connection = new SQLiteConnection(
$"Data Source={DbFile};Version={Version};New=True;Compress=True;"
);
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = $"INSERT INTO {typeof(T).Name} VALUES ({string.Join(", ", typeof(T).GetProperties().Select(x => $"@{x.Name}"))});";
foreach (var property in typeof(T).GetProperties())
{
var value = property.GetValue(obj);
if (value == null)
cmd.Parameters.AddWithValue($"@{property.Name}", DBNull.Value);
else if (property.PropertyType == typeof(string))
cmd.Parameters.AddWithValue($"@{property.Name}", value.ToString());
else if (property.PropertyType == typeof(int))
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToInt32(value));
else if (property.PropertyType == typeof(bool))
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToBoolean(value));
else if (property.PropertyType == typeof(float))
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToSingle(value));
else
throw new NotImplementedException(
$"Not implemented type : {property.PropertyType.Name}"
);
}
cmd.ExecuteNonQuery();
connection.Close();
Objects.Add(obj);
}
/// <inheritdoc />
public void Remove(T obj)
{
var connection = new SQLiteConnection(
$"Data Source={DbFile};Version={Version};New=True;Compress=True;"
);
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = $"DELETE FROM {typeof(T).Name} WHERE {string.Join(" AND ", typeof(T).GetProperties().Select(x => $"{x.Name} = @{x.Name}"))};";
foreach (var property in typeof(T).GetProperties())
{
var value = property.GetValue(obj);
if (value == null)
cmd.Parameters.AddWithValue($"@{property.Name}", DBNull.Value);
else if (property.PropertyType == typeof(string))
cmd.Parameters.AddWithValue($"@{property.Name}", value.ToString());
else if (property.PropertyType == typeof(int))
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToInt32(value));
else if (property.PropertyType == typeof(bool))
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToBoolean(value));
else if (property.PropertyType == typeof(float))
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToSingle(value));
else
throw new NotImplementedException(
$"Not implemented type : {property.PropertyType.Name}"
);
}
cmd.ExecuteNonQuery();
connection.Close();
Objects.Remove(obj);
}
/// <inheritdoc />
public IEnumerable<T> Get(Func<T, bool> predicate)
{
return Objects.Where(predicate);
}
}