-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataAccess.fs
297 lines (260 loc) · 10.8 KB
/
DataAccess.fs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
module Champs.Db
type DbKeys =
| LastTrackedBattle
| LastTrackedTraitSwap
| LastTrackedBattleDateTime2
[<RequireQualifiedAccess>]
module internal SQL =
let createTablesSQL = """
CREATE TABLE IF NOT EXISTS Champ (
ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
AssetID INTEGER NOT NULL UNIQUE,
Name TEXT NOT NULL,
IPFS TEXT
);
CREATE TABLE IF NOT EXISTS Battle (
ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
BattleNum INTEGER NOT NULL UNIQUE,
WinnerID INTEGER NOT NULL,
LoserID INTEGER NOT NULL,
Description TEXT NOT NULL,
Wager INTEGER NOT NULL,
Timestamp DATETIME,
FOREIGN KEY (WinnerID)
REFERENCES Champ (ID),
FOREIGN KEY (LoserID)
REFERENCES Champ (ID)
);
CREATE TABLE IF NOT EXISTS KeyValue (
Key TEXT NOT NULL PRIMARY KEY,
Value TEXT NOT NULL
);
"""
let AlterBattleTable = """
ALTER TABLE Battle
ADD COLUMN Timestamp DATETIME
"""
let getTimestampColumnInfo = """
select count(*) from
pragma_table_info('Battle')
where name='Timestamp'
"""
let GetValueByKey =
"SELECT Value FROM KeyValue WHERE Key = @key"
let SetKeyValue = "
INSERT INTO KeyValue(Key, Value) VALUES(@key, @value)
ON CONFLICT(Key) DO UPDATE SET Value = @value;"
let ChampExists =
"SELECT EXISTS(SELECT 1 FROM Champ WHERE AssetID = @assetId LIMIT 1);"
let BattleExists =
"SELECT EXISTS(SELECT 1 FROM Battle WHERE BattleNum = @battleNum LIMIT 1);"
let AddOrUpdateChamp = "
INSERT INTO Champ(AssetID, Name, IPFS) VALUES(@assetId, @name, @ipfs)
ON CONFLICT(AssetID) DO UPDATE SET Name = @name, IPFS = @ipfs;"
let AddOrUpdateBattle = "
INSERT INTO Battle(BattleNum, WinnerID, LoserID, Description, Wager, Timestamp)
VALUES(@battleNum, @winnerId, @loserId, @description, @wager, @timestamp)
ON CONFLICT(BattleNum) DO
UPDATE SET WinnerID = @winnerId, LoserID = @loserId, Description = @description, Wager = @wager, Timestamp = @timestamp;
"
let GetChampIdByAssetId = "
SELECT ID FROM Champ
WHERE AssetID = @assetId
"
let GetChampByAssetId = "
SELECT Name, AssetID, IPFS FROM Champ
WHERE AssetID = @assetId
"
let GetBattleByBattleNum = "
SELECT BattleNum, Description, Wager,
wc.AssetID as WAssetId, wc.Name as Winner, wc.IPFS as WIPFS,
lc.AssetID as LAssetId, lc.Name as Loser, lc.IPFS as LIPFS,
Timestamp
FROM Battle
JOIN Champ wc ON wc.ID = Battle.WinnerID
JOIN Champ lc ON lc.ID = Battle.LoserID
WHERE BattleNum = @battleNum
"
let GetAllChamps = "
SELECT Name, AssetID, IPFS FROM Champ
"
let GetBattlesWithoutTimestamp = "
SELECT BattleNum FROM Battle WHERE Timestamp IS NULL
"
let GetAllBattles = "
SELECT
BattleNum, Description, Wager,
wc.AssetID as WAssetId, wc.Name as Winner, wc.IPFS as WIPFS,
lc.AssetID as LAssetId, lc.Name as Loser, lc.IPFS as LIPFS,
Timestamp
FROM Battle
JOIN Champ wc ON wc.ID = Battle.WinnerID
JOIN Champ lc ON lc.ID = Battle.LoserID
"
open Champs.Core
open System.Collections.Generic
open Microsoft.Data.Sqlite
open Donald
open System
type SqliteStorage(cs: string)=
let conn = new SqliteConnection(cs)
do Db.newCommand SQL.createTablesSQL conn
|> Db.exec
do Db.newCommand SQL.getTimestampColumnInfo conn
|> Db.scalar(fun v -> tryUnbox<int64> v)
|> Option.iter(fun i ->
if i = 0L then
Db.newCommand SQL.AlterBattleTable conn
|> Db.exec)
do conn.Dispose()
let getChampIdByAssetId(assetId: uint64) =
try
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetChampIdByAssetId conn
|> Db.setParams [ "assetId", SqlType.Int64 <| int64 assetId ]
|> Db.scalar (fun v -> tryUnbox<int64> v)
with _ -> None
member t.GetLastTrackedBattle() =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetValueByKey conn
|> Db.setParams [ "key", SqlType.String (DbKeys.LastTrackedBattle.ToString()) ]
|> Db.query (fun rd -> rd.ReadString "Value")
|> List.tryHead
member _.GetLastTrackedBattleDateTime() =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetValueByKey conn
|> Db.setParams [ "key", SqlType.String (DbKeys.LastTrackedBattleDateTime2.ToString()) ]
|> Db.query (fun rd -> rd.ReadDateTime "Value")
|> List.tryHead
member t.SetLastTrackedBattleDateTime(dt:DateTime) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.SetKeyValue conn
|> Db.setParams [
"key", SqlType.String (DbKeys.LastTrackedBattleDateTime2.ToString())
"value", SqlType.DateTime dt
]
|> Db.exec
member t.SetLastTrackedBattle(battle:uint64) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.SetKeyValue conn
|> Db.setParams [
"key", SqlType.String (DbKeys.LastTrackedBattle.ToString())
"value", SqlType.Int64 <| int64 battle
]
|> Db.exec
member t.GetLastTrackedTraitSwap() =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetValueByKey conn
|> Db.setParams [ "key", SqlType.String (DbKeys.LastTrackedTraitSwap.ToString()) ]
|> Db.query (fun rd -> rd.ReadString "Value")
|> List.tryHead
member t.SetLastTrackedTraitSwap(round:uint64) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.SetKeyValue conn
|> Db.setParams [
"key", SqlType.String (DbKeys.LastTrackedTraitSwap.ToString())
"value", SqlType.Int64 <| int64 round
]
|> Db.exec
member t.ChampExists(assetId: uint64) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.ChampExists conn
|> Db.setParams [ "assetId", SqlType.Int64 <| int64 assetId ]
|> Db.scalar (fun v -> tryUnbox<int64> v |> Option.map(fun v -> v > 0) |> Option.defaultValue false)
member t.BattleExists(battleNum: uint64) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.BattleExists conn
|> Db.setParams [ "battleNum", SqlType.Int64 <| int64 battleNum ]
|> Db.scalar (fun v -> tryUnbox<int64> v |> Option.map(fun v -> v > 0) |> Option.defaultValue false)
member t.AddOrUpdateChamp(champ:Champ) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.AddOrUpdateChamp conn
|> Db.setParams [
"assetId", SqlType.Int64 <| int64 champ.AssetId
"name", SqlType.String champ.Name
"ipfs", if champ.Ipfs.IsSome then SqlType.String champ.Ipfs.Value else SqlType.Null
]
|> Db.exec
member t.AddOrUpdateBattle(battle:Battle) =
use conn = new SqliteConnection(cs)
match getChampIdByAssetId battle.Winner.AssetId, getChampIdByAssetId battle.Loser.AssetId with
| Some winnerId, Some loserId ->
Db.newCommand SQL.AddOrUpdateBattle conn
|> Db.setParams [
"battleNum", SqlType.Int64 <| int64 battle.BattleNum
"winnerId", SqlType.Int64 <| winnerId
"loserId", SqlType.Int64 <| loserId
"description", SqlType.String <| battle.Description
"wager", SqlType.Decimal <| battle.Wager
"timestamp", if battle.UTCDateTime.IsSome then SqlType.DateTime battle.UTCDateTime.Value else SqlType.Null
]
|> Db.exec
| _ -> ()
member t.TryGetChamp(assetId: uint64) =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetChampByAssetId conn
|> Db.setParams [
"assetId", SqlType.Int64 <| int64 assetId
]
|> Db.querySingle(fun reader ->
{
Name = reader.GetString(0);
AssetId = uint64 (reader.GetInt64(1));
Ipfs = if reader.IsDBNull(2) then None else Some(reader.GetString(2))
})
member t.TryGetBattle(battleNum: uint64) : Battle option =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetBattleByBattleNum conn
|> Db.setParams [
"battleNum", SqlType.Int64 <| int64 battleNum
]
|> Db.querySingle(fun reader ->
{
BattleNum = uint64 (reader.GetInt64(0))
Description = reader.GetString(1)
Wager = reader.GetDecimal(2)
Winner = {
AssetId = reader.GetInt64(3) |> uint64
Name = reader.GetString(4)
Ipfs = if reader.IsDBNull(5) then None else Some(reader.GetString(5))
}
Loser = {
AssetId = reader.GetInt64(6) |> uint64
Name = reader.GetString(7)
Ipfs = if reader.IsDBNull(8) then None else Some(reader.GetString(8))
}
UTCDateTime = if reader.IsDBNull(9) then None else Some(reader.GetDateTime(9))
})
member t.GetAllChamps() =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetAllChamps conn
|> Db.query(fun reader ->
{
Name = reader.GetString(0);
AssetId = uint64 (reader.GetInt64(1))
Ipfs = if reader.IsDBNull(2) then None else Some(reader.GetString(2))
})
member t.GetAllBattles() : Battle list =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetAllBattles conn
|> Db.query(fun reader ->
{
BattleNum = uint64 (reader.GetInt64(0))
Description = reader.GetString(1)
Wager = reader.GetDecimal(2)
Winner = {
AssetId = reader.GetInt64(3) |> uint64
Name = reader.GetString(4)
Ipfs = if reader.IsDBNull(5) then None else Some(reader.GetString(5))
}
Loser = {
AssetId = reader.GetInt64(6) |> uint64
Name = reader.GetString(7)
Ipfs = if reader.IsDBNull(8) then None else Some(reader.GetString(8))
}
UTCDateTime = if reader.IsDBNull(9) then None else Some(reader.GetDateTime(9))
})
member t.BattlesWithoutTimestamp() : uint64 list =
use conn = new SqliteConnection(cs)
Db.newCommand SQL.GetBattlesWithoutTimestamp conn
|> Db.query(fun reader -> uint64 <| reader.GetInt64(0))