Skip to content

Commit feec701

Browse files
File reorganize
1 parent 2fb6eb9 commit feec701

15 files changed

Lines changed: 173 additions & 73 deletions

Simple.Sqlite.Cipher/ConnectionFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Data.Sqlite;
2+
using Simple.Sqlite.Helpers;
23
using System;
34
using System.IO;
45

Simple.Sqlite.Cipher/Simple.Sqlite.Cipher.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<Compile Include="..\Simple.Sqlite\Extension\MigrationExtension.cs" Link="Extensions\MigrationExtension.cs" />
3131
<Compile Include="..\Simple.Sqlite\Extension\QueryExtension.cs" Link="Extensions\QueryExtension.cs" />
3232
<Compile Include="..\Simple.Sqlite\Extension\TableSchemaExtension.cs" Link="Extensions\TableSchemaExtension.cs" />
33-
<Compile Include="..\Simple.Sqlite\HelperFunctions.cs" Link="HelperFunctions.cs" />
33+
<Compile Include="..\Simple.Sqlite\Helpers\HelperFunctions.cs" Link="Helpers\HelperFunctions.cs" />
3434
<Compile Include="..\Simple.Sqlite\Schema\ColumnSchema.cs" Link="ColumnSchema.cs" />
3535
<Compile Include="..\Simple.Sqlite\Schema\TableMapper.cs" Link="TableMapper.cs" />
3636
<Compile Include="..\Simple.Sqlite\Schema\TableSchema.cs" Link="TableSchema.cs" />
@@ -49,5 +49,6 @@
4949
<ItemGroup>
5050
<Folder Include="Extensions\" />
5151
<Folder Include="Attributes\" />
52+
<Folder Include="Helpers\" />
5253
</ItemGroup>
5354
</Project>

Simple.Sqlite.xml

Lines changed: 87 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Simple.Sqlite/ConnectionFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Simple.Sqlite;
22

33
using Microsoft.Data.Sqlite;
4+
using Simple.Sqlite.Helpers;
45
using System;
56
using System.IO;
67

Simple.Sqlite/Extension/ExecuteExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Simple.Sqlite;
22

3+
using Simple.Sqlite.Helpers;
34
using System;
45

56
/// <summary>
@@ -71,7 +72,7 @@ static T executeScalar<T>(this ISqliteConnection connection, ISqliteTransaction?
7172
// In SQLite DateTime is returned as STRING after aggregate operations
7273
if (typeof(T) == typeof(DateTime))
7374
{
74-
if (DateTime.TryParse(obj.ToString(), out DateTime dt))
75+
if (DateTime.TryParse(obj?.ToString(), out DateTime dt))
7576
{
7677
return (T)(object)dt;
7778
}

Simple.Sqlite/Extension/InsertExtension.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Simple.Sqlite;
22

33
using Microsoft.Data.Sqlite;
4+
using Simple.Sqlite.Helpers;
45
using System.Collections.Generic;
56

67
/// <summary>

Simple.Sqlite/Extension/QueryExtension.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Simple.Sqlite;
22

33
using Simple.DatabaseWrapper.Helpers;
4+
using Simple.Sqlite.Helpers;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -84,13 +85,13 @@ static string queryBuilder<T>(ISqliteConnection connection, object parameters)
8485
return $"SELECT * FROM {tInfo.TypeName} WHERE {string.Join(" AND ", pairs)}";
8586
}
8687

87-
static IEnumerable<T> query<T>(ISqliteConnection connection, ISqliteTransaction? transaction, string query, object parameters, bool buffered)
88+
static IEnumerable<T> query<T>(ISqliteConnection connection, ISqliteTransaction? transaction, string query, object? parameters, bool buffered)
8889
{
8990
var q = _query<T>(connection, transaction, query, parameters);
9091
if (buffered) q = q.ToList();
9192
return q;
9293
}
93-
static IEnumerable<T> _query<T>(ISqliteConnection connection, ISqliteTransaction? transaction, string query, object parameters)
94+
static IEnumerable<T> _query<T>(ISqliteConnection connection, ISqliteTransaction? transaction, string query, object? parameters)
9495
{
9596
using var cmd = new Microsoft.Data.Sqlite.SqliteCommand(query, connection.connection, transaction?.transaction);
9697
HelperFunctions.fillParameters(cmd, parameters, connection.typeCollection);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Simple.Sqlite;
1+
namespace Simple.Sqlite.Helpers;
22

33
using Microsoft.Data.Sqlite;
44
using Simple.DatabaseWrapper.Attributes;

Simple.Sqlite/KeyValueStorage.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace Simple.Sqlite;
2+
3+
using Simple.DatabaseWrapper.Attributes;
4+
using System;
5+
using System.Linq;
6+
7+
/// <summary>
8+
/// A Simple KeyValue storage
9+
/// </summary>
10+
public class KeyValueStorage
11+
{
12+
private readonly ConnectionFactory db;
13+
14+
/// <summary>
15+
/// Creates a new KeyValueStorage using a ConnectionFactory
16+
/// </summary>
17+
public KeyValueStorage(ConnectionFactory db)
18+
{
19+
this.db = db;
20+
21+
using var cnn = db.GetConnection();
22+
cnn.CreateTables()
23+
.Add<KVStorageTable>()
24+
.Commit();
25+
}
26+
/// <summary>
27+
/// Sets a new KeyValue pair
28+
/// </summary>
29+
public void SetKey<T>(string key, T? value)
30+
{
31+
if (string.IsNullOrEmpty(key))
32+
{
33+
throw new ArgumentException($"'{nameof(key)}' cannot be null or empty.", nameof(key));
34+
}
35+
36+
using var cnn = db.GetConnection();
37+
38+
if (value == null)
39+
{
40+
cnn.Execute($"DELETE FROM KVStorageTable WHERE {nameof(KVStorageTable.Key)} = @key", new { key });
41+
}
42+
else
43+
{
44+
cnn.Insert(new KVStorageTable { Key = normalizeKey(key), Value = value }, OnConflict.Replace);
45+
}
46+
}
47+
48+
/// <summary>
49+
/// Gets the Value from a Key
50+
/// Inexistent keys returns as null
51+
/// </summary>
52+
/// <returns>Key's value or NULL</returns>
53+
public T? GetKey<T>(string key)
54+
{
55+
using var cnn = db.GetConnection();
56+
var values = cnn.Query<T>("SELECT Value FROM KVStorageTable WHERE Key = @Key", new { Key = normalizeKey(key) })
57+
.ToArray();
58+
59+
if (values.Length == 0) return default;
60+
return values[0];
61+
}
62+
63+
private static string normalizeKey(string key) => key.Trim().ToUpper();
64+
65+
internal record KVStorageTable
66+
{
67+
[PrimaryKey]
68+
public string Key { get; set; } = default!;
69+
public object Value { get; set; } = default!;
70+
}
71+
}

Simple.Sqlite/KeyValueStringStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void SetKey(string key, string? value)
7070

7171
internal record KVStorageTable
7272
{
73-
[Unique]
73+
[PrimaryKey]
7474
public string Key { get; set; } = default!;
7575
public string Value { get; set; } = default!;
7676
}

0 commit comments

Comments
 (0)