This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
CassandraColumnFamily`1.cs
111 lines (98 loc) · 2.45 KB
/
CassandraColumnFamily`1.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Thrift.Transport;
using Thrift.Protocol;
using Apache.Cassandra;
using FluentCassandra.Configuration;
namespace FluentCassandra
{
public class CassandraColumnFamily<T> : CassandraColumnFamily
{
private ITypeGetConfiguration<T> _config;
/// <summary>
///
/// </summary>
/// <param name="keyspace"></param>
/// <param name="connection"></param>
public CassandraColumnFamily(CassandraKeyspace keyspace, IConnection connection)
: base(keyspace, connection, CassandraConfiguration.GetMapFor<T>().ColumnFamily)
{
_config = CassandraConfiguration.GetMapFor<T>();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public FluentColumnFamily PrepareColumnFamily(T obj)
{
FluentColumnFamily record = new FluentColumnFamily(
_config.KeyMap.KeyAccessor(obj),
_config.ColumnFamily
);
foreach (var col in _config.PropertyMap)
{
var fcol = new FluentColumn {
Name = col.Alias,
};
fcol.SetValue(col.PropertyAccessor(obj));
record.Columns.Add(fcol);
}
return record;
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
public void Add(T obj)
{
// TODO: wireup insert
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
public void Remove(T obj)
{
// TODO: wireup remove
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="key"></param>
/// <param name="exp"></param>
/// <returns></returns>
public TResult Get<TResult>(string key, Expression<Func<T, TResult>> exp)
{
var name = GetName(exp);
var selection = _config.PropertyMap.FirstOrDefault(x => x.PropertyName == name);
var col = base.Get(key, /* superColumn */ null, selection.Alias);
return col.GetValue<TResult>();
}
/// <summary>
///
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
private string GetName(Expression exp)
{
switch (exp.NodeType)
{
case ExpressionType.MemberAccess:
return ((MemberExpression)exp).Member.Name;
case ExpressionType.Convert:
case ExpressionType.Quote:
return GetName(((UnaryExpression)exp).Operand);
case ExpressionType.Lambda:
return GetName(((LambdaExpression)exp).Body);
default:
return null;
}
}
}
}