-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPostGreSQLReadThruProvider.cs
More file actions
75 lines (67 loc) · 2.57 KB
/
PostGreSQLReadThruProvider.cs
File metadata and controls
75 lines (67 loc) · 2.57 KB
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
using Alachisoft.NCache.Runtime.Caching;
using Alachisoft.NCache.Runtime.DatasourceProviders;
using Alachisoft.NCache.Samples.PostGreSQLNotificationDependency;
using Npgsql;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Entities;
namespace PostGreSQLBackingSourceProvider
{
public class PostGreSQLReadThruProvider : IReadThruProvider
{
private string _connectionString;
private string _dependencyKey;
private string _schema;
private string _table;
private string _channel;
private IDbConnection _connection;
public void Init(IDictionary parameters, string cacheId)
{
_connectionString = parameters["connectionString"] as string;
_connection = new NpgsqlConnection(_connectionString);
_connection.Open();
}
public ProviderDataTypeItem<IEnumerable> LoadDataTypeFromSource(string key, DistributedDataType dataType)
{
throw new NotImplementedException();
}
public ProviderCacheItem LoadFromSource(string key)
{
var query = $"SELECT customerid, address, country, city FROM customers WHERE customerid = '{key}'";
// Define a query returning a single row result set
NpgsqlCommand command = new NpgsqlCommand(query, _connection as NpgsqlConnection);
var reader = command.ExecuteReader();
ProviderCacheItem providerCacheItem = null;
while (reader.Read())
{
if (providerCacheItem == null)
{
var customer = new Customer()
{
customerid = reader[0] as string,
address = reader[1] as string,
country = reader[2] as string,
city = reader[3] as string,
};
providerCacheItem = new ProviderCacheItem(customer)
{
Dependency = new PostGreSQLDependency(_connectionString, customer.customerid, "public", "customers", "customer_channel"),
ResyncOptions = new ResyncOptions(true)
};
}
}
reader.Close();
return providerCacheItem;
}
public IDictionary<string, ProviderCacheItem> LoadFromSource(ICollection<string> keys)
{
throw new NotImplementedException();
}
public void Dispose()
{
_connection.Close();
}
}
}