blob: 7695334450b970380d3486c1b02957055d2842a2 (
plain)
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
|
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
namespace Tango.DataStore.EF
{
public class EFDataStoreCollection : IDataStoreCollection
{
public string Name { get; }
public EFDataStoreCollection(String name)
{
Name = name;
}
public void Put<T>(string key, T value)
{
Put(key, (object)value);
}
public void Put(string key, object value)
{
Put(key, DataStoreHelper.GetDataType(value), value);
}
public void Put(string key, DataType type, object value)
{
using (var db = ObservablesContext.CreateDefault())
{
DataStoreItem item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);
if (item == null)
{
item = new DataStoreItem();
db.DataStoreItems.Add(item);
}
item.CollectionName = Name;
item.Key = key;
item.DataType = (int)type;
item.Value = EFDataStoreHelper.CreateBytes(type, value);
db.SaveChanges();
}
}
public T Get<T>(string key)
{
return (T)Get(key);
}
public object Get(string key)
{
return GetItem(key).Value;
}
public IDataStoreItem GetItem(string key)
{
using (var db = ObservablesContext.CreateDefault())
{
var item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);
if (item == null)
{
throw new KeyNotFoundException("The specified data store key was not found.");
}
return item.ToDataStoreItem();
}
}
public List<IDataStoreItem> GetAll()
{
using (var db = ObservablesContext.CreateDefault())
{
return db.DataStoreItems.Where(x => x.CollectionName == Name).ToList().Select(x => x.ToDataStoreItem()).ToList();
}
}
public List<IDataStoreItem> GetUnsynchronized()
{
using (var db = ObservablesContext.CreateDefault())
{
return db.DataStoreItems.Where(x => x.CollectionName == Name && !x.IsSynchronized).ToList().Select(x => x.ToDataStoreItem()).ToList();
}
}
public void Delete(string key)
{
using (var db = ObservablesContext.CreateDefault())
{
db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}' AND KEY = '{key}'");
}
}
public void DeleteAll()
{
using (var db = ObservablesContext.CreateDefault())
{
db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}'");
}
}
public int Count()
{
using (var db = ObservablesContext.CreateDefault())
{
return db.DataStoreItems.Count(x => x.CollectionName == Name);
}
}
}
}
|