Skip to content

Commit c9f66f7

Browse files
committed
update(core): 2.2.0
1 parent ab7f854 commit c9f66f7

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
[#] Bug Fixes<br>
88
[.] Others
99

10+
### V 2.0.0 - 07/05/2025
11+
[.] SharpEngine.Core : 2.2.0
12+
1013
### V 1.2.1 - 24/12/2024
1114
[.] SharpEngine.Core : 1.9.3<br/>
1215
[.] System.Data.SQLite : 1.0.119

SharpEngine.SQLite/SQLiteDataTable.cs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ namespace SharpEngine.SQLite;
66
/// <summary>
77
/// Sqlite Data Table
88
/// </summary>
9-
public class SQLiteDataTable<T> : IDataTable
10-
where T : new()
9+
public class SQLiteDataTable<T> : IDataTable<T>
10+
where T : class, new()
1111
{
12-
private List<dynamic> Objects { get; }
12+
private List<T> Objects { get; }
13+
14+
private string DbFile { get; set; }
15+
private string Version { get; set; }
1316

1417
/// <summary>
1518
/// Create Data Table from SQLite
@@ -19,6 +22,8 @@ public class SQLiteDataTable<T> : IDataTable
1922
/// <exception cref="NotImplementedException">If use not implement type</exception>
2023
public SQLiteDataTable(string dbFile, string version = "3")
2124
{
25+
DbFile = dbFile;
26+
Version = version;
2227
Objects = [];
2328

2429
var connection = new SQLiteConnection(
@@ -59,8 +64,73 @@ public SQLiteDataTable(string dbFile, string version = "3")
5964
}
6065

6166
/// <inheritdoc />
62-
public dynamic? Get(Predicate<dynamic> predicate)
67+
public void Add(T obj)
68+
{
69+
var connection = new SQLiteConnection(
70+
$"Data Source={DbFile};Version={Version};New=True;Compress=True;"
71+
);
72+
connection.Open();
73+
var cmd = connection.CreateCommand();
74+
cmd.CommandText = $"INSERT INTO {typeof(T).Name} VALUES ({string.Join(", ", typeof(T).GetProperties().Select(x => $"@{x.Name}"))});";
75+
foreach (var property in typeof(T).GetProperties())
76+
{
77+
var value = property.GetValue(obj);
78+
if (value == null)
79+
cmd.Parameters.AddWithValue($"@{property.Name}", DBNull.Value);
80+
else if (property.PropertyType == typeof(string))
81+
cmd.Parameters.AddWithValue($"@{property.Name}", value.ToString());
82+
else if (property.PropertyType == typeof(int))
83+
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToInt32(value));
84+
else if (property.PropertyType == typeof(bool))
85+
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToBoolean(value));
86+
else if (property.PropertyType == typeof(float))
87+
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToSingle(value));
88+
else
89+
throw new NotImplementedException(
90+
$"Not implemented type : {property.PropertyType.Name}"
91+
);
92+
}
93+
cmd.ExecuteNonQuery();
94+
connection.Close();
95+
96+
Objects.Add(obj);
97+
}
98+
99+
/// <inheritdoc />
100+
public void Remove(T obj)
101+
{
102+
var connection = new SQLiteConnection(
103+
$"Data Source={DbFile};Version={Version};New=True;Compress=True;"
104+
);
105+
connection.Open();
106+
var cmd = connection.CreateCommand();
107+
cmd.CommandText = $"DELETE FROM {typeof(T).Name} WHERE {string.Join(" AND ", typeof(T).GetProperties().Select(x => $"{x.Name} = @{x.Name}"))};";
108+
foreach (var property in typeof(T).GetProperties())
109+
{
110+
var value = property.GetValue(obj);
111+
if (value == null)
112+
cmd.Parameters.AddWithValue($"@{property.Name}", DBNull.Value);
113+
else if (property.PropertyType == typeof(string))
114+
cmd.Parameters.AddWithValue($"@{property.Name}", value.ToString());
115+
else if (property.PropertyType == typeof(int))
116+
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToInt32(value));
117+
else if (property.PropertyType == typeof(bool))
118+
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToBoolean(value));
119+
else if (property.PropertyType == typeof(float))
120+
cmd.Parameters.AddWithValue($"@{property.Name}", Convert.ToSingle(value));
121+
else
122+
throw new NotImplementedException(
123+
$"Not implemented type : {property.PropertyType.Name}"
124+
);
125+
}
126+
cmd.ExecuteNonQuery();
127+
connection.Close();
128+
Objects.Remove(obj);
129+
}
130+
131+
/// <inheritdoc />
132+
public IEnumerable<T> Get(Func<T, bool> predicate)
63133
{
64-
return Objects.Find(predicate);
134+
return Objects.Where(predicate);
65135
}
66136
}

SharpEngine.SQLite/SharpEngine.SQLite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<None Include="../README.md" Pack="true" PackagePath="\" />
2626
</ItemGroup>
2727
<ItemGroup>
28-
<PackageReference Include="SharpEngine.Core" Version="1.9.3" />
28+
<PackageReference Include="SharpEngine.Core" Version="2.2.0" />
2929
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
3030
</ItemGroup>
3131
</Project>

0 commit comments

Comments
 (0)