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
/
CassandraType.cs
303 lines (244 loc) · 8.74 KB
/
CassandraType.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
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
298
299
300
301
302
303
using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
using System.Text;
namespace FluentCassandra.Types
{
public sealed class CassandraType
{
public static readonly CassandraType AsciiType = new CassandraType("org.apache.cassandra.db.marshal.AsciiType");
public static readonly CassandraType BooleanType = new CassandraType("org.apache.cassandra.db.marshal.BooleanType");
public static readonly CassandraType BytesType = new CassandraType("org.apache.cassandra.db.marshal.BytesType");
public static readonly CassandraType DateType = new CassandraType("org.apache.cassandra.db.marshal.DateType");
public static readonly CassandraType DecimalType = new CassandraType("org.apache.cassandra.db.marshal.DecimalType");
public static readonly CassandraType DoubleType = new CassandraType("org.apache.cassandra.db.marshal.DoubleType");
public static readonly CassandraType FloatType = new CassandraType("org.apache.cassandra.db.marshal.FloatType");
public static readonly CassandraType Int32Type = new CassandraType("org.apache.cassandra.db.marshal.Int32Type");
public static readonly CassandraType IntegerType = new CassandraType("org.apache.cassandra.db.marshal.IntegerType");
public static readonly CassandraType LexicalUUIDType = new CassandraType("org.apache.cassandra.db.marshal.LexicalUUIDType");
public static readonly CassandraType LongType = new CassandraType("org.apache.cassandra.db.marshal.LongType");
public static readonly CassandraType TimeUUIDType = new CassandraType("org.apache.cassandra.db.marshal.TimeUUIDType");
public static readonly CassandraType UTF8Type = new CassandraType("org.apache.cassandra.db.marshal.UTF8Type");
public static readonly CassandraType UUIDType = new CassandraType("org.apache.cassandra.db.marshal.UUIDType");
public static readonly CassandraType CounterColumnType = new CassandraType("org.apache.cassandra.db.marshal.CounterColumnType");
private static readonly CassandraType _CompositeType = new CassandraType("org.apache.cassandra.db.marshal.CompositeType");
private static readonly CassandraType _DynamicCompositeType = new CassandraType("org.apache.cassandra.db.marshal.DynamicCompositeType");
private const string _ReversedType = "org.apache.cassandra.db.marshal.ReversedType";
private readonly string _dbType;
private Type _type;
private bool? _dbTypeReversed;
private IList<CassandraType> _compositeTypes;
private IDictionary<char, CassandraType> _dynamicCompositeType;
public CassandraType(string type)
{
if (string.IsNullOrEmpty(type))
throw new ArgumentNullException("type");
_dbType = type;
}
public CassandraObject CreateInstance()
{
if (_type == null)
Parse();
CassandraObject obj;
if (_type == typeof(CompositeType))
obj = new CompositeType(_compositeTypes);
else if (_type == typeof(DynamicCompositeType))
obj = new DynamicCompositeType(_dynamicCompositeType);
else
obj = Activator.CreateInstance(_type) as CassandraObject;
if (obj == null)
return null;
return obj;
}
public string DatabaseType { get { return _dbType; } }
public bool Reversed
{
get
{
if (_dbTypeReversed == null)
Parse();
return _dbTypeReversed.Value;
}
}
public Type FluentType
{
get
{
if (_type == null)
Parse();
return _type;
}
}
private void Parse()
{
_dbTypeReversed = false;
int compositeStart = _dbType.IndexOf('(');
// check for composite type
if (compositeStart == -1) {
_type = Parse(_dbType);
return;
}
var part1 = _dbType.Substring(0, compositeStart);
var part2 = _dbType.Substring(compositeStart);
_type = Parse(part1);
if (_type == typeof(CompositeType))
ParseCompositeType(part2);
else if (_type == typeof(DynamicCompositeType))
ParseDynamicCompositeType(part2);
else if (_type == typeof(ReversedType))
ParseReversedType(part2);
else
throw new CassandraException("Type '" + _dbType + "' not found.");
}
private void ParseReversedType(string part)
{
part = part.Trim('(', ')');
_dbTypeReversed = true;
_type = Parse(part);
}
private void ParseCompositeType(string part)
{
part = part.Trim('(', ')');
var parts = part.Split(',');
_compositeTypes = new List<CassandraType>();
foreach (var p in parts)
_compositeTypes.Add(Parse(p));
}
private void ParseDynamicCompositeType(string part)
{
part = part.Trim('(', ')');
var parts = part.Split(',');
_dynamicCompositeType = new Dictionary<char, CassandraType>();
foreach (var p in parts)
{
char alias = p[0];
if (alias < 33 || alias > 127)
throw new CassandraException("An alias should be a single character in [0..9a..bA..B-+._&]");
if (p[1] != '=' || p[2] != '>')
throw new CassandraException("Expecting operator '=>' after the alias");
string type = p.Substring(3);
_dynamicCompositeType.Add(alias, Parse(type));
}
}
private Type Parse(string dbType)
{
Type type;
switch (dbType.Substring(dbType.LastIndexOf('.') + 1).ToLower())
{
case "asciitype": type = typeof(AsciiType); break;
case "booleantype": type = typeof(BooleanType); break;
case "bytestype": type = typeof(BytesType); break;
case "datetype": type = typeof(DateType); break;
case "decimaltype": type = typeof(DecimalType); break;
case "doubletype": type = typeof(DoubleType); break;
case "floattype": type = typeof(FloatType); break;
case "int32type": type = typeof(Int32Type); break;
case "integertype": type = typeof(IntegerType); break;
case "lexicaluuidtype": type = typeof(LexicalUUIDType); break;
case "longtype": type = typeof(LongType); break;
case "timeuuidtype": type = typeof(TimeUUIDType); break;
case "utf8type": type = typeof(UTF8Type); break;
case "uuidtype": type = typeof(UUIDType); break;
case "compositetype": type = typeof(CompositeType); break;
case "dynamiccompositetype": type = typeof(DynamicCompositeType); break;
case "countercolumntype": type = typeof(CounterColumnType); break;
case "reversedtype": type = typeof(ReversedType); break;
default: throw new CassandraException("Type '" + _dbType + "' not found.");
}
return type;
}
public override string ToString()
{
return _dbType;
}
public static CassandraType CompositeType(IEnumerable<CassandraType> hints)
{
var sb = new StringBuilder();
sb.Append(_CompositeType);
sb.Append("(");
sb.Append(String.Join(",", hints));
sb.Append(")");
return new CassandraType(sb.ToString());
}
public static CassandraType DynamicCompositeType(IDictionary<char, CassandraType> aliases)
{
var sb = new StringBuilder();
sb.Append(_DynamicCompositeType);
sb.Append("(");
sb.Append(String.Join(",", aliases.Select(x => x.Key + "=>" + x.Value)));
sb.Append(")");
return new CassandraType(sb.ToString());
}
public static CassandraType ReversedType(CassandraType baseType)
{
var sb = new StringBuilder();
sb.Append(_ReversedType);
sb.Append("(");
sb.Append(baseType.DatabaseType);
sb.Append(")");
return new CassandraType(sb.ToString());
}
public static CassandraType GetCassandraType(Type sourceType)
{
if (sourceType.BaseType == typeof(CassandraObject))
return new CassandraType(sourceType.Name) { _type = sourceType };
var destinationType = (CassandraType)null;
switch (Type.GetTypeCode(sourceType))
{
case TypeCode.DateTime:
destinationType = DateType;
break;
case TypeCode.Boolean:
destinationType = BooleanType;
break;
case TypeCode.Double:
destinationType = DoubleType;
break;
case TypeCode.Single:
destinationType = FloatType;
break;
case TypeCode.Int64:
case TypeCode.UInt64:
destinationType = LongType;
break;
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.UInt16:
case TypeCode.UInt32:
destinationType = Int32Type;
break;
case TypeCode.Decimal:
destinationType = DecimalType;
break;
case TypeCode.Char:
case TypeCode.String:
destinationType = UTF8Type;
break;
case TypeCode.Byte:
case TypeCode.SByte:
destinationType = BytesType;
break;
default:
if (sourceType == typeof(DateTimeOffset))
destinationType = DateType;
if (sourceType == typeof(BigInteger))
destinationType = IntegerType;
if (sourceType == typeof(BigDecimal))
destinationType = DecimalType;
if (sourceType == typeof(Guid))
destinationType = UUIDType;
if (sourceType == typeof(char[]))
destinationType = UTF8Type;
if (destinationType == null)
destinationType = BytesType;
break;
}
return destinationType;
}
public static implicit operator CassandraType(Type type)
{
return GetCassandraType(type);
}
}
}