-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDapperSqlite.cs
55 lines (48 loc) · 1.71 KB
/
MyDapperSqlite.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
using Dapper;
using Microsoft.Data.Sqlite;
namespace DbBench;
public class MyDapperSqlite : BenchDb
{
public override async Task BenchWrite()
{
// if (File.Exists(Fp)) File.Delete(Fp);
using var conn = new SqliteConnection(new SqliteConnectionStringBuilder()
{
DataSource = GetDatabaseStoragePath()
}.ToString());
await conn.ExecuteAsync(@"
create table IF NOT EXISTS CommonRecord
(
MongoDbObjectId text
constraint Bench_pk
primary key,
Name text default '' not null,
Age integer default 0 not null,
Timestamp integer default 0 not null
);
").ConfigureAwait(false);
await conn.ExecuteAsync("DELETE FROM CommonRecord;").ConfigureAwait(false);
const string insertQuery =
"insert into CommonRecord (MongoDbObjectId, Name, Age, Timestamp) " +
"Values (@MongoDbObjectId, @Name, @Age, @Timestamp)";
foreach (var numb in Enumerable.Range(0, NumberOfDocs))
{
await conn.ExecuteAsync(insertQuery, new CommonRecord() {Name = "John", Age = numb, Timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()})
.ConfigureAwait(false);
}
}
public override async Task<int> BenchRead()
{
using var conn = new SqliteConnection(new SqliteConnectionStringBuilder()
{
DataSource = GetDatabaseStoragePath()
}.ToString());
var query = await conn.QueryAsync<CommonRecord>("select * from CommonRecord").ConfigureAwait(false);
var total = 0;
foreach (var myDapperSqliteBench in query)
{
total += myDapperSqliteBench.Age;
}
return total;
}
}