-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPlainSqlite.cs
67 lines (60 loc) · 2.31 KB
/
MyPlainSqlite.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
using Microsoft.Data.Sqlite;
using MongoDB.Bson;
namespace DbBench;
public class MyPlainSqlite : BenchDb
{
public override async Task BenchWrite()
{
using var conn = new SqliteConnection(new SqliteConnectionStringBuilder()
{
DataSource = GetDatabaseStoragePath()
}.ToString());
await conn.OpenAsync().ConfigureAwait(false);
var cmd = conn.CreateCommand();
cmd.CommandText = @"
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
);
";
await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
cmd.CommandText = "DELETE FROM CommonRecord;";
await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
foreach (var numb in Enumerable.Range(0, NumberOfDocs))
{
using var cmd2 = conn.CreateCommand();
cmd2.CommandText =
"insert into CommonRecord (MongoDbObjectId, Name, Age, Timestamp) " +
"Values (@MongoDbObjectId, @Name, @Age, @Timestamp);";
cmd2.Parameters.AddWithValue("@MongoDbObjectId", ObjectId.GenerateNewId().ToString());
cmd2.Parameters.AddWithValue("@Name", "John");
cmd2.Parameters.AddWithValue("@Age", numb);
cmd2.Parameters.AddWithValue("@Timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
// cmd.Prepare();
// cmd.ExecuteNonQuery();
await cmd2.PrepareAsync().ConfigureAwait(false);
await cmd2.ExecuteNonQueryAsync().ConfigureAwait(false);
}
}
public override async Task<int> BenchRead()
{
using var conn = new SqliteConnection(new SqliteConnectionStringBuilder()
{
DataSource = GetDatabaseStoragePath()
}.ToString());
await conn.OpenAsync().ConfigureAwait(false);
using var cmd = new SqliteCommand("SELECT * FROM CommonRecord", conn);
using var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false);
var total = 0;
while (await reader.ReadAsync().ConfigureAwait(false))
{
total += reader.GetInt32(2);
}
return total;
}
}