Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

all tests now pass

  • Loading branch information...
commit 10617e58aaa65725e281c36f6b39dd1f6babca3a 1 parent 2f86912
@nberardi nberardi authored
Showing with 598 additions and 494 deletions.
  1. +8 −8 src/BaseCassandraColumnFamily.cs
  2. +3 −0  src/CassandraColumnFamily.cs
  3. +5 −0 src/CassandraColumnFamilySchema.cs
  4. +15 −0 src/CassandraColumnPathSchema.cs
  5. +8 −7 src/CassandraContext.cs
  6. +11 −10 src/CassandraSuperColumnFamily.cs
  7. +0 −15 src/CassandraSuperColumnFamilySchema.cs
  8. +2 −2 src/FluentCassandra.csproj
  9. +9 −3 src/FluentColumn.cs
  10. +7 −7 src/FluentColumnFamily.cs
  11. +3 −3 src/FluentSuperColumn.cs
  12. +16 −52 src/FluentSuperColumnFamily.cs
  13. 0  src/Linq/{CassandraColumnFamily`1+QueryProvider.cs → CassandraColumnFamily+QueryProvider.cs}
  14. +1 −1  src/Linq/CqlQueryExtensions.cs
  15. +8 −3 src/Operations/ExecuteCqlQuery.cs
  16. +5 −3 src/Operations/GetColumn.cs
  17. +1 −1  src/Operations/GetSuperColumnFamilyIndexedSlices.cs
  18. +1 −1  src/Operations/GetSuperColumnFamilyRangeSlices.cs
  19. +1 −1  src/Operations/GetSuperColumnFamilySlice.cs
  20. +30 −10 src/Operations/Helper.cs
  21. +4 −2 src/Operations/InsertColumn.cs
  22. +5 −3 src/Operations/MultiGetColumnFamilySlice.cs
  23. +1 −1  src/Operations/MultiGetSuperColumnFamilySlice.cs
  24. +5 −5 src/Types/AsciiType.cs
  25. +3 −3 src/Types/AsciiTypeConverter.cs
  26. +5 −5 src/Types/BooleanType.cs
  27. +2 −2 src/Types/BooleanTypeConverter.cs
  28. +12 −6 src/Types/BytesType.cs
  29. +40 −11 src/Types/BytesTypeConverter.cs
  30. +2 −2 src/Types/CassandraConversionHelper.cs
  31. +91 −115 src/Types/CassandraType.cs
  32. +29 −3 src/Types/CassandraTypeConverter.cs
  33. +5 −5 src/Types/CompositeType.cs
  34. +2 −2 src/Types/CompositeTypeConverter.cs
  35. +5 −5 src/Types/DateType.cs
  36. +16 −10 src/Types/DateTypeConverter.cs
  37. +5 −5 src/Types/DecimalType.cs
  38. +2 −2 src/Types/DecimalTypeConverter.cs
  39. +5 −5 src/Types/DoubleType.cs
  40. +2 −2 src/Types/DoubleTypeConverter.cs
  41. +5 −5 src/Types/DynamicCompositeType.cs
  42. +3 −3 src/Types/DynamicCompositeTypeConverter.cs
  43. +5 −5 src/Types/FloatType.cs
  44. +2 −2 src/Types/FloatTypeConverter.cs
  45. +5 −5 src/Types/Int32Type.cs
  46. +2 −2 src/Types/Int32TypeConverter.cs
  47. +5 −5 src/Types/IntegerType.cs
  48. +2 −2 src/Types/IntegerTypeConverter.cs
  49. +5 −5 src/Types/LexicalUUIDType.cs
  50. +2 −2 src/Types/LexicalUUIDTypeConverter.cs
  51. +5 −5 src/Types/LongType.cs
  52. +11 −3 src/Types/LongTypeConverter.cs
  53. +42 −4 src/Types/NullType.cs
  54. +5 −5 src/Types/TimeUUIDType.cs
  55. +2 −2 src/Types/TimeUUIDTypeConverter.cs
  56. +5 −5 src/Types/UTF8Type.cs
  57. +3 −3 src/Types/UTF8TypeConverter.cs
  58. +5 −5 src/Types/UUIDType.cs
  59. +2 −2 src/Types/UUIDTypeConverter.cs
  60. +2 −12 src/Types/VoidType.cs
  61. +58 −50 test/FluentCassandra.Tests/CassandraDatabaseSetup.cs
  62. +1 −0  test/FluentCassandra.Tests/FluentCassandra.Tests.csproj
  63. +2 −2 test/FluentCassandra.Tests/FluentColumnFamilyTest.cs
  64. +2 −2 test/FluentCassandra.Tests/FluentSuperColumnTest.cs
  65. +22 −0 test/FluentCassandra.Tests/Helper.cs
  66. +5 −5 test/FluentCassandra.Tests/Linq/LinqToCassandraTests.cs
  67. +6 −6 test/FluentCassandra.Tests/Operations/GetSliceTest.cs
  68. +2 −2 test/FluentCassandra.Tests/Types/BytesTypeTest.cs
  69. +1 −1  test/FluentCassandra.Tests/Types/NullTypeTest.cs
  70. +3 −3 test/FluentCassandra.Tests/TypesToDatabase/BytesTypeTest.cs
  71. +0 −9 test/FluentCassandra.Tests/TypesToDatabase/IntegerTypeTest.cs
  72. +0 −8 test/FluentCassandra.Tests/TypesToDatabase/LongTypeTest.cs
  73. +3 −3 test/FluentCassandra.Tests/TypesToDatabase/TimeUUIDTypeTest.cs
View
16 src/BaseCassandraColumnFamily.cs
@@ -3,7 +3,6 @@
using System.Linq;
using System.Linq.Expressions;
using FluentCassandra.Operations;
-using FluentCassandra.Types;
namespace FluentCassandra
{
@@ -94,24 +93,25 @@ public TResult ExecuteOperation<TResult>(ColumnFamilyOperation<TResult> action,
if (!throwOnError.HasValue)
throwOnError = ThrowErrors;
- var localSession = CassandraSession.Current;
- if (localSession == null)
- localSession = _context.OpenSession();
+ var localSession = CassandraSession.Current == null;
+ var session = CassandraSession.Current;
+ if (session == null)
+ session = _context.OpenSession();
action.Context = _context;
action.ColumnFamily = this;
try
{
- var result = localSession.ExecuteOperation(action, throwOnError);
- LastError = localSession.LastError;
+ var result = session.ExecuteOperation(action, throwOnError);
+ LastError = session.LastError;
return result;
}
finally
{
- if (localSession != null)
- localSession.Dispose();
+ if (localSession && session != null)
+ session.Dispose();
}
}
View
3  src/CassandraColumnFamily.cs
@@ -37,6 +37,9 @@ public override CassandraColumnFamilySchema GetSchema()
{
var def = Context.Keyspace.GetColumnFamilyDescription(FamilyName);
+ if (def == null && _cachedSchema == null)
+ _cachedSchema = new CassandraColumnFamilySchema();
+
if (_cachedSchema == null)
{
_cachedSchema = new CassandraColumnFamilySchema();
View
5 src/CassandraColumnFamilySchema.cs
@@ -12,7 +12,11 @@ public class CassandraColumnFamilySchema
public CassandraColumnFamilySchema()
{
KeyName = DefaultKeyName;
+
KeyType = typeof(BytesType);
+ SuperColumnNameType = typeof(BytesType);
+ ColumnNameType = typeof(BytesType);
+
Columns = new List<CassandraColumnSchema>();
}
@@ -20,6 +24,7 @@ public CassandraColumnFamilySchema()
public CassandraType KeyName { get; set; }
public Type KeyType { get; set; }
+ public Type SuperColumnNameType { get; set; }
public Type ColumnNameType { get; set; }
public IList<CassandraColumnSchema> Columns { get; set; }
View
15 src/CassandraColumnPathSchema.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using FluentCassandra.Types;
+
+namespace FluentCassandra
+{
+ public class CassandraColumnPathSchema
+ {
+ public string ColumnFamily { get; set; }
+ public CassandraType SuperColumnNameType { get; set; }
+ public CassandraType ColumnNameType { get; set; }
+ }
+}
View
15 src/CassandraContext.cs
@@ -315,23 +315,24 @@ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwO
if (!throwOnError.HasValue)
throwOnError = ThrowErrors;
- var localSession = CassandraSession.Current;
- if (localSession == null)
- localSession = OpenSession();
+ var localSession = CassandraSession.Current == null;
+ var session = CassandraSession.Current;
+ if (session == null)
+ session = OpenSession();
action.Context = this;
try
{
- var result = localSession.ExecuteOperation(action, throwOnError);
- LastError = localSession.LastError;
+ var result = session.ExecuteOperation(action, throwOnError);
+ LastError = session.LastError;
return result;
}
finally
{
- if (localSession != null)
- localSession.Dispose();
+ if (localSession && session != null)
+ session.Dispose();
}
}
View
21 src/CassandraSuperColumnFamily.cs
@@ -12,17 +12,17 @@ public class CassandraSuperColumnFamily<CompareWith, CompareSubcolumnWith> : Cas
public CassandraSuperColumnFamily(CassandraContext context, string columnFamily)
: base(context, columnFamily)
{
- SetSchema(new CassandraSuperColumnFamilySchema {
+ SetSchema(new CassandraColumnFamilySchema {
FamilyName = columnFamily,
- ColumnNameType = typeof(CompareWith),
- SubColumnNameType = typeof(CompareSubcolumnWith)
+ SuperColumnNameType = typeof(CompareWith),
+ ColumnNameType = typeof(CompareSubcolumnWith)
});
}
}
public class CassandraSuperColumnFamily : BaseCassandraColumnFamily
{
- private CassandraSuperColumnFamilySchema _cachedSchema;
+ private CassandraColumnFamilySchema _cachedSchema;
public CassandraSuperColumnFamily(CassandraContext context, string columnFamily)
: base(context, columnFamily) { }
@@ -32,7 +32,7 @@ public FluentSuperColumnFamily CreateRecord(CassandraType key)
if (key.GetValue<byte[]>().Length == 0)
throw new ArgumentException("'key' is not allowed to be zero length.", "key");
- return new FluentSuperColumnFamily(key, FamilyName, (CassandraSuperColumnFamilySchema)GetSchema());
+ return new FluentSuperColumnFamily(key, FamilyName, GetSchema());
}
public override CassandraColumnFamilySchema GetSchema()
@@ -41,7 +41,7 @@ public override CassandraColumnFamilySchema GetSchema()
if (_cachedSchema == null)
{
- _cachedSchema = new CassandraSuperColumnFamilySchema();
+ _cachedSchema = new CassandraColumnFamilySchema();
var keyType = CassandraType.GetCassandraType(def.Key_validation_class);
var colNameType = CassandraType.GetCassandraType(def.Default_validation_class);
@@ -49,7 +49,8 @@ public override CassandraColumnFamilySchema GetSchema()
_cachedSchema.FamilyName = FamilyName;
_cachedSchema.KeyType = keyType;
- _cachedSchema.SubColumnNameType = subColNameType;
+ _cachedSchema.SuperColumnNameType = colNameType;
+ _cachedSchema.ColumnNameType = subColNameType;
_cachedSchema.Columns = def.Column_metadata.Select(col => new CassandraColumnSchema {
Name = CassandraType.GetTypeFromDatabaseValue(col.Name, colNameType),
ValueType = CassandraType.GetCassandraType(col.Validation_class)
@@ -61,10 +62,10 @@ public override CassandraColumnFamilySchema GetSchema()
public override void SetSchema(CassandraColumnFamilySchema schema)
{
- if (schema is CassandraSuperColumnFamilySchema)
- _cachedSchema = (CassandraSuperColumnFamilySchema)schema;
+ if (schema == null)
+ schema = new CassandraColumnFamilySchema { FamilyName = FamilyName };
- throw new ArgumentException("'schema' must be of CassandraSuperColumnFamilySchema type.", "schema");
+ _cachedSchema = schema;
}
public override void ClearCachedColumnFamilySchema()
View
15 src/CassandraSuperColumnFamilySchema.cs
@@ -1,15 +0,0 @@
-using System;
-using FluentCassandra.Types;
-
-namespace FluentCassandra
-{
- public class CassandraSuperColumnFamilySchema : CassandraColumnFamilySchema
- {
- public CassandraSuperColumnFamilySchema()
- {
- SubColumnNameType = typeof(BytesType);
- }
-
- public Type SubColumnNameType { get; set; }
- }
-}
View
4 src/FluentCassandra.csproj
@@ -95,6 +95,7 @@
<Compile Include="BaseCassandraColumnFamily.cs" />
<Compile Include="CassandraColumnFamilySchema.cs" />
<Compile Include="CassandraColumnFamily.cs" />
+ <Compile Include="CassandraColumnPathSchema.cs" />
<Compile Include="CassandraColumnSchema.cs" />
<Compile Include="CassandraContext.cs" />
<Compile Include="CassandraException.cs" />
@@ -102,7 +103,6 @@
<Compile Include="CassandraSession.cs" />
<Compile Include="CassandraSuperColumnFamilyOperations.cs" />
<Compile Include="CassandraSuperColumnFamily.cs" />
- <Compile Include="CassandraSuperColumnFamilySchema.cs" />
<Compile Include="CassandraTokenRange.cs" />
<Compile Include="Connections\Connection.cs" />
<Compile Include="Connections\ConnectionBuilder.cs" />
@@ -151,7 +151,7 @@
<Compile Include="IFluentRecordExpression.cs" />
<Compile Include="Linq\CqlQuery.cs" />
<Compile Include="Linq\CqlQueryEvaluator.cs" />
- <Compile Include="Linq\CassandraColumnFamily`1+QueryProvider.cs" />
+ <Compile Include="Linq\CassandraColumnFamily+QueryProvider.cs" />
<Compile Include="Linq\CqlQueryExtensions.cs" />
<Compile Include="Linq\ICqlRow.cs" />
<Compile Include="Operations\BatchMutate.cs" />
View
12 src/FluentColumn.cs
@@ -45,7 +45,7 @@ public CassandraType ColumnName
get { return _name; }
set
{
- _name = (CassandraType)value.ToType(GetSchema().NameType);
+ _name = (CassandraType)value.GetValue(GetSchema().NameType);
ColumnTimestamp = DateTimeOffset.UtcNow;
}
}
@@ -58,7 +58,7 @@ public CassandraType ColumnValue
get { return _value; }
set
{
- _value = (CassandraType)value.ToType(GetSchema().ValueType);
+ _value = (CassandraType)value.GetValue(GetSchema().ValueType);
ColumnTimestamp = DateTimeOffset.UtcNow;
}
}
@@ -148,7 +148,13 @@ public CassandraColumnSchema GetSchema()
public void SetSchema(CassandraColumnSchema schema)
{
if (schema == null)
- schema = new CassandraColumnSchema { Name = ColumnName, ValueType = ColumnValue.GetType() };
+ schema = new CassandraColumnSchema();
+
+ if (_name != null)
+ _name = (CassandraType)_name.GetValue(schema.NameType);
+
+ if (_value != null)
+ _value = (CassandraType)_value.GetValue(schema.ValueType);
_schema = schema;
}
View
14 src/FluentColumnFamily.cs
@@ -63,7 +63,7 @@ public CassandraType Key
get { return _key; }
set
{
- _key = (CassandraType)value.ToType(GetSchema().KeyType);
+ _key = (CassandraType)value.GetValue(GetSchema().KeyType);
}
}
@@ -179,7 +179,7 @@ private CassandraType GetColumnValue(object name)
return NullType.Value;
var schema = GetColumnSchema(name);
- return (CassandraType)col.ColumnValue.ToType(schema.ValueType);
+ return (CassandraType)col.ColumnValue.GetValue(schema.ValueType);
}
/// <summary>
@@ -190,12 +190,12 @@ private CassandraType GetColumnValue(object name)
private CassandraColumnSchema GetColumnSchema(object name)
{
var col = Columns.FirstOrDefault(c => c.ColumnName == name);
- var schema = GetSchema();
- if (col == null)
- return new CassandraColumnSchema { NameType = schema.ColumnNameType, ValueType = typeof(BytesType) };
+ if (col != null)
+ return col.GetSchema();
- var colSchema = schema.Columns.FirstOrDefault(c => c.Name == col.ColumnName);
+ var schema = GetSchema();
+ var colSchema = schema.Columns.FirstOrDefault(c => c.Name == name);
if (colSchema != null)
return colSchema;
@@ -242,7 +242,7 @@ public override bool TrySetColumn(object name, object value)
}
// set the column value
- col.ColumnValue = CassandraType.GetTypeFromObject<BytesType>(value);
+ col.ColumnValue = CassandraType.GetTypeFromObject(value);
// notify the tracker that the column has changed
OnColumnMutated(mutationType, col);
View
6 src/FluentSuperColumn.cs
@@ -56,7 +56,7 @@ public CassandraType ColumnName
get { return _name; }
set
{
- _name = (CassandraType)value.ToType(GetSchema().NameType);
+ _name = (CassandraType)value.GetValue(GetSchema().NameType);
}
}
@@ -170,7 +170,7 @@ private CassandraType GetColumnValue(object name)
return NullType.Value;
var schema = GetColumnSchema(name);
- return (CassandraType)col.ColumnValue.ToType(schema.ValueType);
+ return (CassandraType)col.ColumnValue.GetValue(schema.ValueType);
}
/// <summary>
@@ -225,7 +225,7 @@ public override bool TrySetColumn(object name, object value)
}
// set the column value
- col.ColumnValue = CassandraType.GetTypeFromObject<BytesType>(value);
+ col.ColumnValue = CassandraType.GetTypeFromObject(value);
// notify the tracker that the column has changed
OnColumnMutated(mutationType, col);
View
68 src/FluentSuperColumnFamily.cs
@@ -11,11 +11,11 @@ public class FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith> : Fluent
where CompareSubcolumnWith : CassandraType
{
public FluentSuperColumnFamily(CassandraType key, string columnFamily)
- : base(key, columnFamily, new CassandraSuperColumnFamilySchema {
+ : base(key, columnFamily, new CassandraColumnFamilySchema {
FamilyName = columnFamily,
KeyType = typeof(BytesType),
- ColumnNameType = typeof(CompareWith),
- SubColumnNameType = typeof(CompareSubcolumnWith)
+ SuperColumnNameType = typeof(CompareWith),
+ ColumnNameType = typeof(CompareSubcolumnWith)
}) { }
}
@@ -23,14 +23,14 @@ public class FluentSuperColumnFamily : FluentRecord<FluentSuperColumn>, IFluentB
{
private CassandraType _key;
private FluentColumnList<FluentSuperColumn> _columns;
- private CassandraSuperColumnFamilySchema _schema;
+ private CassandraColumnFamilySchema _schema;
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="columnFamily"></param>
- public FluentSuperColumnFamily(CassandraType key, string columnFamily, CassandraSuperColumnFamilySchema schema = null)
+ public FluentSuperColumnFamily(CassandraType key, string columnFamily, CassandraColumnFamilySchema schema = null)
{
SetSchema(schema);
@@ -47,7 +47,7 @@ public FluentSuperColumnFamily(CassandraType key, string columnFamily, Cassandra
/// <param name="key"></param>
/// <param name="columnFamily"></param>
/// <param name="columns"></param>
- internal FluentSuperColumnFamily(CassandraType key, string columnFamily, CassandraSuperColumnFamilySchema schema, IEnumerable<FluentSuperColumn> columns)
+ internal FluentSuperColumnFamily(CassandraType key, string columnFamily, CassandraColumnFamilySchema schema, IEnumerable<FluentSuperColumn> columns)
{
SetSchema(schema);
@@ -65,7 +65,7 @@ public CassandraType Key
get { return _key; }
set
{
- _key = (CassandraType)value.ToType(GetSchema().KeyType);
+ _key = (CassandraType)value.GetValue(GetSchema().KeyType);
}
}
@@ -102,25 +102,6 @@ public FluentSuperColumn CreateSuperColumn(CassandraType name)
/// <summary>
///
/// </summary>
- /// <param name="columnName"></param>
- /// <returns></returns>
- public FluentSuperColumn this[CassandraType columnName]
- {
- get
- {
- var value = GetColumnValue(columnName);
-
- if (value is NullType)
- throw new CassandraException(String.Format("Column, {0}, could not be found.", columnName));
-
- return value;
- }
- }
-
-
- /// <summary>
- ///
- /// </summary>
public override IList<FluentSuperColumn> Columns
{
get { return _columns; }
@@ -129,10 +110,10 @@ public override IList<FluentSuperColumn> Columns
/// <summary>
///
/// </summary>
- public CassandraSuperColumnFamilySchema GetSchema()
+ public CassandraColumnFamilySchema GetSchema()
{
if (_schema == null)
- _schema = new CassandraSuperColumnFamilySchema { FamilyName = FamilyName };
+ _schema = new CassandraColumnFamilySchema { FamilyName = FamilyName };
return _schema;
}
@@ -141,10 +122,10 @@ public CassandraSuperColumnFamilySchema GetSchema()
///
/// </summary>
/// <param name="schema"></param>
- public void SetSchema(CassandraSuperColumnFamilySchema schema)
+ public void SetSchema(CassandraColumnFamilySchema schema)
{
if (schema == null)
- schema = new CassandraSuperColumnFamilySchema { FamilyName = FamilyName };
+ schema = new CassandraColumnFamilySchema { FamilyName = FamilyName };
_schema = schema;
}
@@ -191,8 +172,8 @@ private CassandraColumnSchema GetColumnSchema(object name)
// mock up a fake schema to send to the fluent column
return new CassandraColumnSchema {
Name = CassandraType.GetTypeFromObject(name, schema.ColumnNameType),
- NameType = schema.ColumnNameType,
- ValueType = schema.SubColumnNameType };
+ NameType = schema.SuperColumnNameType,
+ ValueType = schema.ColumnNameType };
}
/// <summary>
@@ -203,8 +184,8 @@ private CassandraColumnSchema GetColumnSchema(object name)
/// <returns></returns>
public override bool TryGetColumn(object name, out object result)
{
- var col = Columns.FirstOrDefault(c => c.ColumnName == name);
- result = (col == null) ? CreateSuperColumn(CassandraType.GetTypeFromObject(name, GetSchema().ColumnNameType)) : col;
+ var col = GetColumnValue(name);
+ result = (col == null) ? CreateSuperColumn(CassandraType.GetTypeFromObject(name)) : col;
return true;
}
@@ -220,7 +201,7 @@ public override bool TrySetColumn(object name, object value)
if (!(value is FluentSuperColumn))
throw new ArgumentException("Value must be of type " + typeof(FluentSuperColumn) + ", because this column family is of type Super.", "value");
- var col = Columns.FirstOrDefault(c => c.ColumnName == name);
+ var col = GetColumnValue(name);
var mutationType = col == null ? MutationType.Added : MutationType.Changed;
col = (FluentSuperColumn)value;
@@ -240,22 +221,5 @@ public override bool TrySetColumn(object name, object value)
return true;
}
-
- #region IFluentBaseColumnFamily Members
-
- CassandraColumnFamilySchema IFluentBaseColumnFamily.GetSchema()
- {
- return GetSchema();
- }
-
- void IFluentBaseColumnFamily.SetSchema(CassandraColumnFamilySchema schema)
- {
- if (schema is CassandraSuperColumnFamilySchema)
- SetSchema((CassandraSuperColumnFamilySchema)schema);
-
- throw new ArgumentException("'schema' must be of CassandraSuperColumnFamilySchema type.", "schema");
- }
-
- #endregion
}
}
View
0  .../CassandraColumnFamily`1+QueryProvider.cs → ...nq/CassandraColumnFamily+QueryProvider.cs
File renamed without changes
View
2  src/Linq/CqlQueryExtensions.cs
@@ -16,7 +16,7 @@ public static CqlQuery Select(this IQueryable<ICqlRow> source, params CassandraT
if (columns == null)
throw new ArgumentNullException("columns");
- return (CqlQuery)source.Provider.CreateQuery<ICqlRow>(Expression.Call(null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[] { typeof(CassandraType) }), new Expression[] { source.Expression, Expression.Constant(columns) }));
+ return (CqlQuery)source.Provider.CreateQuery<ICqlRow>(Expression.Call(null, (MethodInfo)MethodBase.GetCurrentMethod(), new Expression[] { source.Expression, Expression.Constant(columns) }));
}
}
}
View
11 src/Operations/ExecuteCqlQuery.cs
@@ -88,10 +88,10 @@ private IEnumerable<ICqlRow> GetRows(Apache.Cassandra.CqlResult result)
CassandraType.GetTypeFromDatabaseValue<BytesType>(row.Key),
familyName,
schema,
- GetColumns(row));
+ GetColumns(row, schema));
}
- private IEnumerable<FluentColumn> GetColumns(Apache.Cassandra.CqlRow row)
+ private IEnumerable<FluentColumn> GetColumns(Apache.Cassandra.CqlRow row, CassandraColumnFamilySchema schema)
{
foreach (var col in row.Columns)
{
@@ -99,7 +99,12 @@ private IEnumerable<FluentColumn> GetColumns(Apache.Cassandra.CqlRow row)
if (col.Timestamp == -1)
continue;
- yield return Helper.ConvertColumnToFluentColumn(col);
+
+ var colSchema = schema.Columns.Where(x => x.Name == col.Name).FirstOrDefault();
+ var fcol = Helper.ConvertColumnToFluentColumn(col);
+ fcol.SetSchema(colSchema);
+
+ yield return fcol;
}
}
View
8 src/Operations/GetColumn.cs
@@ -17,15 +17,17 @@ public class GetColumn : ColumnFamilyOperation<FluentColumn>
public override FluentColumn Execute()
{
+ var schema = ColumnFamily.GetSchema();
+
var path = new CassandraColumnPath {
ColumnFamily = ColumnFamily.FamilyName
};
if (SuperColumnName != null)
- path.SuperColumn = SuperColumnName;
+ path.SuperColumn = SuperColumnName.GetValue(schema.SuperColumnNameType) as CassandraType;
if (ColumnName != null)
- path.Column = ColumnName;
+ path.Column = ColumnName.GetValue(schema.ColumnNameType) as CassandraType;
var output = Session.GetClient().get(
Key,
@@ -33,7 +35,7 @@ public override FluentColumn Execute()
Session.ReadConsistency
);
- return (FluentColumn)Helper.ConvertToFluentBaseColumn(output);
+ return (FluentColumn)Helper.ConvertToFluentBaseColumn(output, schema);
}
public GetColumn(CassandraType key, CassandraType superColumnName, CassandraType columnName)
View
2  src/Operations/GetSuperColumnFamilyIndexedSlices.cs
@@ -29,7 +29,7 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
foreach (var result in output)
{
- var r = new FluentSuperColumnFamily(result.Key, ColumnFamily.FamilyName, (CassandraSuperColumnFamilySchema)ColumnFamily.GetSchema(), result.Columns.Select(col => {
+ var r = new FluentSuperColumnFamily(result.Key, ColumnFamily.FamilyName, ColumnFamily.GetSchema(), result.Columns.Select(col => {
var superCol = Helper.ConvertSuperColumnToFluentSuperColumn(col.Super_column);
ColumnFamily.Context.Attach(superCol);
superCol.MutationTracker.Clear();
View
2  src/Operations/GetSuperColumnFamilyRangeSlices.cs
@@ -29,7 +29,7 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
foreach (var result in output)
{
- var r = new FluentSuperColumnFamily(result.Key, ColumnFamily.FamilyName, (CassandraSuperColumnFamilySchema)ColumnFamily.GetSchema(), result.Columns.Select(col => {
+ var r = new FluentSuperColumnFamily(result.Key, ColumnFamily.FamilyName, ColumnFamily.GetSchema(), result.Columns.Select(col => {
var superCol = Helper.ConvertSuperColumnToFluentSuperColumn(col.Super_column);
ColumnFamily.Context.Attach(superCol);
superCol.MutationTracker.Clear();
View
2  src/Operations/GetSuperColumnFamilySlice.cs
@@ -16,7 +16,7 @@ public class GetSuperColumnFamilySlice : ColumnFamilyOperation<FluentSuperColumn
public override FluentSuperColumnFamily Execute()
{
- var result = new FluentSuperColumnFamily(Key, ColumnFamily.FamilyName, (CassandraSuperColumnFamilySchema)ColumnFamily.GetSchema(), GetColumns(ColumnFamily));
+ var result = new FluentSuperColumnFamily(Key, ColumnFamily.FamilyName, ColumnFamily.GetSchema(), GetColumns(ColumnFamily));
ColumnFamily.Context.Attach(result);
result.MutationTracker.Clear();
View
40 src/Operations/Helper.cs
@@ -113,12 +113,12 @@ public static long ToTimestamp(this DateTimeOffset dt)
return Convert.ToInt64((dt - UnixStart).TotalMilliseconds);
}
- public static IFluentBaseColumn ConvertToFluentBaseColumn(ColumnOrSuperColumn col)
+ public static IFluentBaseColumn ConvertToFluentBaseColumn(ColumnOrSuperColumn col, CassandraColumnFamilySchema schema = null)
{
if (col.Super_column != null)
- return ConvertSuperColumnToFluentSuperColumn(col.Super_column);
+ return ConvertSuperColumnToFluentSuperColumn(col.Super_column, schema);
else if (col.Column != null)
- return ConvertColumnToFluentColumn(col.Column);
+ return ConvertColumnToFluentColumn(col.Column, schema);
else if (col.Counter_super_column != null)
throw new NotSupportedException("Reading CounterSuperColumns isn't supported yet.");
else if (col.Counter_column != null)
@@ -127,11 +127,24 @@ public static IFluentBaseColumn ConvertToFluentBaseColumn(ColumnOrSuperColumn co
return null;
}
- public static FluentColumn ConvertColumnToFluentColumn(Column col)
+ public static FluentColumn ConvertColumnToFluentColumn(Column col, CassandraColumnFamilySchema schema = null)
{
- var fcol = new FluentColumn {
- ColumnName = CassandraType.GetTypeFromDatabaseValue<BytesType>(col.Name),
- ColumnValue = CassandraType.GetTypeFromDatabaseValue<BytesType>(col.Value),
+ var colSchema = new CassandraColumnSchema();
+
+ if (schema != null)
+ {
+ colSchema = schema.Columns.Where(x => x.Name == col.Name).FirstOrDefault();
+
+ if (colSchema == null)
+ {
+ colSchema = new CassandraColumnSchema();
+ colSchema.NameType = schema.ColumnNameType;
+ }
+ }
+
+ var fcol = new FluentColumn(colSchema) {
+ ColumnName = CassandraType.GetTypeFromDatabaseValue(col.Name, colSchema.NameType),
+ ColumnValue = CassandraType.GetTypeFromDatabaseValue(col.Value, colSchema.ValueType),
ColumnTimestamp = UnixStart.AddMilliseconds(col.Timestamp),
};
@@ -141,14 +154,21 @@ public static FluentColumn ConvertColumnToFluentColumn(Column col)
return fcol;
}
- public static FluentSuperColumn ConvertSuperColumnToFluentSuperColumn(SuperColumn col)
+ public static FluentSuperColumn ConvertSuperColumnToFluentSuperColumn(SuperColumn col, CassandraColumnFamilySchema schema = null)
{
+ var nameType = typeof(BytesType);
+
+ if (schema != null)
+ {
+ nameType = schema.SuperColumnNameType;
+ }
+
var superCol = new FluentSuperColumn {
- ColumnName = CassandraType.GetTypeFromDatabaseValue<BytesType>(col.Name)
+ ColumnName = CassandraType.GetTypeFromDatabaseValue(col.Name, nameType)
};
foreach (var xcol in col.Columns)
- superCol.Columns.Add(ConvertColumnToFluentColumn(xcol));
+ superCol.Columns.Add(ConvertColumnToFluentColumn(xcol, schema));
return superCol;
}
View
6 src/Operations/InsertColumn.cs
@@ -25,15 +25,17 @@ public class InsertColumn : ColumnFamilyOperation<Void>
public override Void Execute()
{
+ var schema = ColumnFamily.GetSchema();
+
var parent = new CassandraColumnParent {
ColumnFamily = ColumnFamily.FamilyName,
};
if (SuperColumnName != null)
- parent.SuperColumn = SuperColumnName;
+ parent.SuperColumn = SuperColumnName.GetValue(schema.SuperColumnNameType) as CassandraType;
var column = new CassandraColumn {
- Name = ColumnName,
+ Name = ColumnName.GetValue(schema.ColumnNameType) as CassandraType,
Value = ColumnValue,
Timestamp = Timestamp,
Ttl = TimeToLive
View
8 src/Operations/MultiGetColumnFamilySlice.cs
@@ -15,6 +15,8 @@ public class MultiGetColumnFamilySlice : QueryableColumnFamilyOperation<FluentCo
public override IEnumerable<FluentColumnFamily> Execute()
{
+ var schema = ColumnFamily.GetSchema();
+
var parent = new CassandraColumnParent {
ColumnFamily = ColumnFamily.FamilyName
};
@@ -28,10 +30,10 @@ public override IEnumerable<FluentColumnFamily> Execute()
foreach (var result in output)
{
- var key = CassandraType.GetTypeFromDatabaseValue<BytesType>(result.Key);
+ var key = CassandraType.GetTypeFromDatabaseValue(result.Key, schema.KeyType);
- var r = new FluentColumnFamily(key, ColumnFamily.FamilyName, ColumnFamily.GetSchema(), result.Value.Select(col => {
- return Helper.ConvertColumnToFluentColumn(col.Column);
+ var r = new FluentColumnFamily(key, ColumnFamily.FamilyName, schema, result.Value.Select(col => {
+ return Helper.ConvertColumnToFluentColumn(col.Column, schema);
}));
ColumnFamily.Context.Attach(r);
r.MutationTracker.Clear();
View
2  src/Operations/MultiGetSuperColumnFamilySlice.cs
@@ -39,7 +39,7 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
});
var familyName = ColumnFamily.FamilyName;
- var schema = (CassandraSuperColumnFamilySchema)ColumnFamily.GetSchema();
+ var schema = ColumnFamily.GetSchema();
var r = new FluentSuperColumnFamily(key, familyName, schema, superColumns);
ColumnFamily.Context.Attach(r);
r.MutationTracker.Clear();
View
10 src/Types/AsciiType.cs
@@ -8,14 +8,14 @@ public class AsciiType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private string _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is AsciiType)
return _value == ((AsciiType)obj)._value;
- return _value == CassandraType.GetValue<string>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
6 src/Types/AsciiTypeConverter.cs
@@ -20,7 +20,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]);
}
- public override string ConvertFrom(object value)
+ public override string ConvertFromInternal(object value)
{
if (value is byte[])
return ((byte[])value).FromBytes<string>();
@@ -28,7 +28,7 @@ public override string ConvertFrom(object value)
return (string)Convert.ChangeType(value, typeof(string));
}
- public override object ConvertTo(string value, Type destinationType)
+ public override object ConvertToInternal(string value, Type destinationType)
{
if (!(value is string))
return null;
@@ -47,7 +47,7 @@ public override byte[] ToBigEndian(string value)
public override string FromBigEndian(byte[] value)
{
- var obj = ConvertFrom(value);
+ var obj = ConvertFromInternal(value);
return obj;
}
}
View
10 src/Types/BooleanType.cs
@@ -8,14 +8,14 @@ public class BooleanType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private bool _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is BooleanType)
return _value == ((BooleanType)obj)._value;
- return _value == CassandraType.GetValue<bool>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
4 src/Types/BooleanTypeConverter.cs
@@ -20,7 +20,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]);
}
- public override bool ConvertFrom(object value)
+ public override bool ConvertFromInternal(object value)
{
if (value is byte[])
return ((byte[])value).FromBytes<bool>();
@@ -28,7 +28,7 @@ public override bool ConvertFrom(object value)
return (bool)Convert.ChangeType(value, typeof(bool));
}
- public override object ConvertTo(bool value, Type destinationType)
+ public override object ConvertToInternal(bool value, Type destinationType)
{
if (destinationType == typeof(byte[]))
return value.ToBytes();
View
18 src/Types/BytesType.cs
@@ -1,5 +1,6 @@
using System;
using System.Linq;
+using System.Numerics;
namespace FluentCassandra.Types
{
@@ -9,7 +10,7 @@ public class BytesType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
// change value if source type is different and source type wasn't raw bytes
if (_sourceType != type && _sourceType != typeof(byte[]) && _bigEndianValue != null)
@@ -18,13 +19,13 @@ public override object GetValue(Type type)
_sourceType = type;
}
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
_sourceType = obj.GetType();
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
_bigEndianValue = Converter.ToBigEndian(_value, _sourceType);
}
@@ -50,7 +51,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _bigEndianValue; }
+ protected override object GetRawValue() { return _bigEndianValue; }
private Type _sourceType;
private byte[] _bigEndianValue;
@@ -60,6 +61,9 @@ public override string ToString()
public override bool Equals(object obj)
{
+ if (obj == null)
+ return false;
+
if (obj is BytesType)
{
BytesType b1 = this;
@@ -83,7 +87,7 @@ public override bool Equals(object obj)
return b1._value.SequenceEqual(b2._value);
}
- return _value.SequenceEqual(CassandraType.GetValue<byte[]>(obj, Converter));
+ return obj.Equals(GetValue(obj.GetType()));
}
/// <remarks>
@@ -136,6 +140,7 @@ public override int GetHashCode()
public static implicit operator BytesType(Guid o) { return ConvertFrom(o); }
public static implicit operator BytesType(DateTime o) { return ConvertFrom(o); }
public static implicit operator BytesType(DateTimeOffset o) { return ConvertFrom(o); }
+ public static implicit operator BytesType(BigInteger o) { return ConvertFrom(o); }
public static implicit operator byte(BytesType o) { return ConvertTo<byte>(o); }
public static implicit operator sbyte(BytesType o) { return ConvertTo<sbyte>(o); }
@@ -154,6 +159,7 @@ public override int GetHashCode()
public static implicit operator Guid(BytesType o) { return ConvertTo<Guid>(o); }
public static implicit operator DateTime(BytesType o) { return ConvertTo<DateTime>(o); }
public static implicit operator DateTimeOffset(BytesType o) { return ConvertTo<DateTimeOffset>(o); }
+ public static implicit operator BigInteger(BytesType o) { return ConvertTo<BigInteger>(o); }
public static implicit operator byte?(BytesType o) { return ConvertTo<byte>(o); }
public static implicit operator sbyte?(BytesType o) { return ConvertTo<sbyte>(o); }
@@ -167,11 +173,11 @@ public override int GetHashCode()
public static implicit operator double?(BytesType o) { return ConvertTo<double>(o); }
public static implicit operator decimal?(BytesType o) { return ConvertTo<decimal>(o); }
public static implicit operator bool?(BytesType o) { return ConvertTo<bool>(o); }
- //public static implicit operator string(BytesType o) { return ConvertTo<string>(o); }
public static implicit operator char?(BytesType o) { return ConvertTo<char>(o); }
public static implicit operator Guid?(BytesType o) { return ConvertTo<Guid>(o); }
public static implicit operator DateTime?(BytesType o) { return ConvertTo<DateTime>(o); }
public static implicit operator DateTimeOffset?(BytesType o) { return ConvertTo<DateTimeOffset>(o); }
+ public static implicit operator BigInteger?(BytesType o) { return ConvertTo<BigInteger>(o); }
private static T ConvertTo<T>(BytesType type)
{
View
51 src/Types/BytesTypeConverter.cs
@@ -7,6 +7,8 @@ namespace FluentCassandra.Types
{
internal class BytesTypeConverter : CassandraTypeConverter<byte[]>
{
+ private static readonly DateTimeOffset UnixStartOffset = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
+
public override bool CanConvertFrom(Type sourceType)
{
if (sourceType == typeof(Guid))
@@ -21,6 +23,9 @@ public override bool CanConvertFrom(Type sourceType)
if (sourceType == typeof(char[]))
return true;
+ if (sourceType == typeof(BigInteger))
+ return true;
+
switch (Type.GetTypeCode(sourceType))
{
case TypeCode.Byte:
@@ -59,6 +64,9 @@ public override bool CanConvertTo(Type destinationType)
if (destinationType == typeof(char[]))
return true;
+ if (destinationType == typeof(BigInteger))
+ return true;
+
switch (Type.GetTypeCode(destinationType))
{
case TypeCode.Byte:
@@ -83,7 +91,7 @@ public override bool CanConvertTo(Type destinationType)
}
}
- public override byte[] ConvertFrom(object value)
+ public override byte[] ConvertFromInternal(object value)
{
if (value is Guid)
return ((Guid)value).ToByteArray();
@@ -96,8 +104,23 @@ public override byte[] ConvertFrom(object value)
if (value is BigInteger)
bytes = ((BigInteger)value).ToByteArray();
- if (value is DateTimeOffset)
- bytes = BitConverter.GetBytes(((DateTimeOffset)value).UtcTicks);
+ if (value is DateTimeOffset || value is DateTime)
+ {
+ var dto = DateTimeOffset.MinValue;
+
+ if (value is DateTimeOffset)
+ dto = (DateTimeOffset)value;
+
+ if (value is DateTime)
+ {
+ var dt = (DateTime)value;
+ var utc = dt.Kind == DateTimeKind.Utc;
+
+ dto = new DateTimeOffset(dt, utc ? TimeSpan.Zero : (DateTimeOffset.Now.Offset));
+ }
+
+ bytes = BitConverter.GetBytes(Convert.ToInt64(Math.Floor((dto - UnixStartOffset).TotalMilliseconds)));
+ }
if (value is char[])
bytes = ((char[])value).Select(c => (byte)c).ToArray();
@@ -110,8 +133,6 @@ public override byte[] ConvertFrom(object value)
bytes = new byte[] { (byte)value }; break;
case TypeCode.SByte:
bytes = new byte[] { Convert.ToByte((sbyte)value) }; break;
- case TypeCode.DateTime:
- bytes = BitConverter.GetBytes(((DateTime)value).Ticks); break;
case TypeCode.Boolean:
bytes = BitConverter.GetBytes((bool)value); break;
case TypeCode.Char:
@@ -147,7 +168,7 @@ public override byte[] ConvertFrom(object value)
return bytes;
}
- public override object ConvertTo(byte[] value, Type destinationType)
+ public override object ConvertToInternal(byte[] value, Type destinationType)
{
if (destinationType == typeof(Guid))
return new Guid(value);
@@ -157,8 +178,15 @@ public override object ConvertTo(byte[] value, Type destinationType)
var bytes = value;
- if (destinationType == typeof(DateTimeOffset))
- return new DateTimeOffset(BitConverter.ToInt64(bytes, 0), new TimeSpan(0L));
+ if (destinationType == typeof(DateTimeOffset) || destinationType == typeof(DateTime))
+ {
+ var dto = UnixStartOffset.AddMilliseconds(BitConverter.ToInt64(bytes, 0));
+
+ if (destinationType == typeof(DateTime))
+ return dto.LocalDateTime;
+
+ return dto;
+ }
if (destinationType == typeof(BigInteger))
return new BigInteger(bytes);
@@ -172,12 +200,13 @@ public override object ConvertTo(byte[] value, Type destinationType)
return bytes[0];
case TypeCode.SByte:
return Convert.ToSByte(bytes[0]);
- case TypeCode.DateTime:
- return new DateTime(BitConverter.ToInt64(bytes, 0));
case TypeCode.Boolean:
return BitConverter.ToBoolean(bytes, 0);
case TypeCode.Char:
- return BitConverter.ToChar(bytes, 0);
+ if (bytes.Length == 1)
+ return (char)bytes[0];
+ else
+ return BitConverter.ToChar(bytes, 0);
case TypeCode.Double:
return BitConverter.ToDouble(bytes, 0);
case TypeCode.Int16:
View
4 src/Types/CassandraConversionHelper.cs
@@ -8,7 +8,7 @@ internal static class CassandraConversionHelper
public static byte[] ToBytes(this object value)
{
- return BitConverter.ConvertFrom(value);
+ return BitConverter.ConvertFromInternal(value);
}
public static T FromBytes<T>(this byte[] value)
@@ -18,7 +18,7 @@ public static T FromBytes<T>(this byte[] value)
public static object FromBytes(this byte[] value, Type destinationType)
{
- return BitConverter.ConvertTo(value, destinationType);
+ return BitConverter.ConvertToInternal(value, destinationType);
}
private static void ReverseLowFieldTimestamp(byte[] guid)
View
206 src/Types/CassandraType.cs
@@ -1,7 +1,6 @@
using System;
-using System.ComponentModel;
-using System.Numerics;
using System.Collections.Generic;
+using System.Numerics;
namespace FluentCassandra.Types
{
@@ -12,31 +11,24 @@ public T GetValue<T>()
return (T)GetValue(typeof(T));
}
- internal object GetValue<T>(T value, Type type, CassandraTypeConverter<T> converter)
- {
- if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- var nc = new NullableConverter(type);
- type = nc.UnderlyingType;
- }
+ public abstract void SetValue(object obj);
- if (!converter.CanConvertTo(type))
- throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", type, TypeCode));
+ public object GetValue(Type type)
+ {
+ if (type == GetType())
+ return this;
- return converter.ConvertTo(value, type);
- }
+ if (GetType() == typeof(BytesType) && type.BaseType == typeof(CassandraType))
+ return GetTypeFromDatabaseValue((byte[])GetRawValue(), type);
- internal T SetValue<T>(object obj, CassandraTypeConverter<T> converter)
- {
- if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", obj.GetType(), TypeCode));
+ if (type.BaseType == typeof(CassandraType))
+ return GetTypeFromObject(GetRawValue(), type);
- return converter.ConvertFrom(obj);
+ return GetValueInternal(type);
}
- public abstract void SetValue(object obj);
- public abstract object GetValue(Type type);
-
+ protected abstract object GetValueInternal(Type type);
+ protected abstract object GetRawValue();
protected abstract TypeCode TypeCode { get; }
public abstract byte[] ToBigEndian();
@@ -90,73 +82,7 @@ private static T ConvertTo<T>(CassandraType type)
private static CassandraType ConvertFrom(object o)
{
- var sourceType = o.GetType();
- var destinationType = (Type)null;
-
- switch (Type.GetTypeCode(sourceType))
- {
-
- case TypeCode.DateTime:
- destinationType = typeof(DateType);
- break;
-
- case TypeCode.Boolean:
- destinationType = typeof(BooleanType);
- break;
-
- case TypeCode.Char:
- case TypeCode.Double:
- destinationType = typeof(DoubleType);
- break;
-
- case TypeCode.Single:
- destinationType = typeof(FloatType);
- break;
-
- case TypeCode.Int64:
- case TypeCode.UInt64:
- destinationType = typeof(LongType);
- break;
-
- case TypeCode.Int16:
- case TypeCode.Int32:
- case TypeCode.UInt16:
- case TypeCode.UInt32:
- destinationType = typeof(Int32Type);
- break;
-
- case TypeCode.Decimal:
- destinationType = typeof(DecimalType);
- break;
-
- case TypeCode.String:
- destinationType = typeof(UTF8Type);
- break;
-
- case TypeCode.Object:
- if (sourceType == typeof(DateTimeOffset))
- destinationType = typeof(DateType);
-
- if (sourceType == typeof(BigInteger))
- destinationType = typeof(IntegerType);
-
- if (sourceType == typeof(char[]))
- destinationType = typeof(UTF8Type);
-
- goto default;
-
- case TypeCode.Byte:
- case TypeCode.SByte:
- goto default;
-
- default:
- destinationType = typeof(BytesType);
- break;
- }
-
- var type = (CassandraType)Activator.CreateInstance(destinationType);
- type.SetValue(o);
- return type;
+ return GetTypeFromObject(o);
}
public static implicit operator CassandraType(byte[] o) { return ConvertFrom(o); }
@@ -229,19 +155,6 @@ private static CassandraType ConvertFrom(object o)
#endregion
- internal abstract object GetRawValue();
-
- public object ToType(Type conversionType)
- {
- if (GetType() == typeof(BytesType))
- return GetTypeFromDatabaseValue((byte[])GetRawValue(), conversionType);
-
- if (conversionType.BaseType == typeof(CassandraType))
- return GetTypeFromObject(GetRawValue(), conversionType);
-
- return GetValue(conversionType);
- }
-
#region IConvertible Members
TypeCode IConvertible.GetTypeCode()
@@ -251,7 +164,7 @@ TypeCode IConvertible.GetTypeCode()
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
- return ToType(conversionType);
+ return GetValue(conversionType);
}
bool IConvertible.ToBoolean(IFormatProvider provider) { return GetValue<bool>(); }
@@ -304,6 +217,82 @@ public static T GetTypeFromObject<T>(object obj)
return type;
}
+ public static CassandraType GetTypeFromObject(object obj)
+ {
+ if (obj == null)
+ return NullType.Value;
+
+ var sourceType = obj.GetType();
+ var destinationType = (Type)null;
+
+ switch (Type.GetTypeCode(sourceType))
+ {
+
+ case TypeCode.DateTime:
+ destinationType = typeof(DateType);
+ break;
+
+ case TypeCode.Boolean:
+ destinationType = typeof(BooleanType);
+ break;
+
+ case TypeCode.Double:
+ destinationType = typeof(DoubleType);
+ break;
+
+ case TypeCode.Single:
+ destinationType = typeof(FloatType);
+ break;
+
+ case TypeCode.Int64:
+ case TypeCode.UInt64:
+ destinationType = typeof(LongType);
+ break;
+
+ case TypeCode.Int16:
+ case TypeCode.Int32:
+ case TypeCode.UInt16:
+ case TypeCode.UInt32:
+ destinationType = typeof(Int32Type);
+ break;
+
+ case TypeCode.Decimal:
+ destinationType = typeof(DecimalType);
+ break;
+
+ case TypeCode.Char:
+ case TypeCode.String:
+ destinationType = typeof(UTF8Type);
+ break;
+
+ case TypeCode.Byte:
+ case TypeCode.SByte:
+ destinationType = typeof(BytesType);
+ break;
+
+ default:
+ if (sourceType == typeof(DateTimeOffset))
+ destinationType = typeof(DateType);
+
+ if (sourceType == typeof(BigInteger))
+ destinationType = typeof(IntegerType);
+
+ if (sourceType == typeof(Guid))
+ destinationType = typeof(UUIDType);
+
+ if (sourceType == typeof(char[]))
+ destinationType = typeof(UTF8Type);
+
+ if (destinationType == null)
+ destinationType = typeof(BytesType);
+ break;
+ }
+
+ var type = (CassandraType)Activator.CreateInstance(destinationType);
+ type.SetValue(obj);
+ return type;
+ }
+
public static CassandraType GetTypeFromObject(object obj, Type cassandraType)
{
CassandraType type = Activator.CreateInstance(cassandraType) as CassandraType;
@@ -347,18 +336,5 @@ public static Type GetCassandraType(string type)
return cassandraType;
}
-
- internal static T GetValue<T>(object obj, CassandraTypeConverter<T> converter)
- {
- if (obj is CassandraType)
- return ((CassandraType)obj).GetValue<T>();
-
- var objType = obj.GetType();
-
- if (!converter.CanConvertFrom(objType))
- throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", objType, typeof(T)));
-
- return converter.ConvertFrom(obj);
- }
}
}
View
32 src/Types/CassandraTypeConverter.cs
@@ -1,5 +1,6 @@
using System;
using System.Linq;
+using System.ComponentModel;
namespace FluentCassandra.Types
{
@@ -9,15 +10,40 @@ internal abstract class CassandraTypeConverter<T>
public abstract bool CanConvertTo(Type destinationType);
- public abstract T ConvertFrom(object value);
+ public abstract T ConvertFromInternal(object value);
- public abstract object ConvertTo(T value, Type destinationType);
+ public abstract object ConvertToInternal(T value, Type destinationType);
public TDestination ConvertTo<TDestination>(T value)
{
return (TDestination)ConvertTo(value, typeof(TDestination));
}
+ public object ConvertTo(T value, Type destinationType)
+ {
+ if (destinationType.IsGenericType && destinationType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
+ {
+ var nc = new NullableConverter(destinationType);
+ destinationType = nc.UnderlyingType;
+ }
+
+ if (!CanConvertTo(destinationType))
+ throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", typeof(T), destinationType));
+
+ return ConvertToInternal(value, destinationType);
+ }
+
+ public T ConvertFrom(object obj)
+ {
+ if (obj is CassandraType)
+ return ((CassandraType)obj).GetValue<T>();
+
+ if (!CanConvertFrom(obj.GetType()))
+ throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", obj.GetType(), typeof(T)));
+
+ return ConvertFromInternal(obj);
+ }
+
public virtual byte[] ToBigEndian(T value)
{
var bytes = ConvertTo<byte[]>(value);
@@ -27,7 +53,7 @@ public virtual byte[] ToBigEndian(T value)
public virtual T FromBigEndian(byte[] value)
{
var bytes = ConvertEndian(value);
- var obj = ConvertFrom(bytes);
+ var obj = ConvertFromInternal(bytes);
return obj;
}
View
10 src/Types/CompositeType.cs
@@ -64,9 +64,9 @@ public CompositeType()
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
@@ -74,7 +74,7 @@ public override void SetValue(object obj)
if (obj != null && obj.GetType().GetInterfaces().Contains(typeof(IEnumerable<CassandraType>)))
ComponentTypeHints = ((IEnumerable<CassandraType>)obj).Select(t => t.GetType()).ToList();
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -99,7 +99,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
public List<Type> ComponentTypeHints { get; set; }
@@ -114,7 +114,7 @@ public override bool Equals(object obj)
if (obj is CompositeType)
objArray = ((CompositeType)obj)._value;
else
- objArray = CassandraType.GetValue<List<CassandraType>>(obj, Converter);
+ objArray = Converter.ConvertFrom(obj);
if (objArray == null)
return false;
View
4 src/Types/CompositeTypeConverter.cs
@@ -24,7 +24,7 @@ public override bool CanConvertTo(Type destinationType)
destinationType == typeof(string);
}
- public override List<CassandraType> ConvertFrom(object value)
+ public override List<CassandraType> ConvertFromInternal(object value)
{
if (value is byte[])
{
@@ -64,7 +64,7 @@ public override List<CassandraType> ConvertFrom(object value)
return null;
}
- public override object ConvertTo(List<CassandraType> value, Type destinationType)
+ public override object ConvertToInternal(List<CassandraType> value, Type destinationType)
{
if (destinationType == typeof(string))
return String.Join(":", (IEnumerable<CassandraType>)value);
View
10 src/Types/DateType.cs
@@ -8,14 +8,14 @@ public class DateType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private DateTimeOffset _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is DateType)
return _value == ((DateType)obj)._value;
- return _value == CassandraType.GetValue<DateTimeOffset>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
26 src/Types/DateTypeConverter.cs
@@ -6,10 +6,16 @@ internal class DateTypeConverter : CassandraTypeConverter<DateTimeOffset>
{
private static readonly DateTimeOffset UnixStart = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
- public static long ToDate(DateTimeOffset dt)
+ private static long ToUnixTime(DateTimeOffset dt)
{
// this was changed from .NET Ticks to the Unix Epoch to be compatible with other cassandra libraries
- return Convert.ToInt64((dt - UnixStart).TotalMilliseconds);
+ return Convert.ToInt64(Math.Floor((dt - UnixStart).TotalMilliseconds));
+ }
+
+ private static DateTimeOffset FromUnixTime(long ms)
+ {
+ // this was changed from .NET Ticks to the Unix Epoch to be compatible with other cassandra libraries
+ return UnixStart.AddMilliseconds(ms);
}
public override bool CanConvertFrom(Type sourceType)
@@ -53,19 +59,19 @@ public override bool CanConvertTo(Type destinationType)
}
}
- public override DateTimeOffset ConvertFrom(object value)
+ public override DateTimeOffset ConvertFromInternal(object value)
{
if (value is DateTimeOffset)
return (DateTimeOffset)value;
if (value is byte[])
- return UnixStart.AddMilliseconds(((byte[])value).FromBytes<long>());
+ return FromUnixTime(((byte[])value).FromBytes<long>());
if (value is long)
- return UnixStart.AddMilliseconds((long)value);
+ return FromUnixTime((long)value);
if (value is ulong)
- return UnixStart.AddMilliseconds((ulong)value);
+ return FromUnixTime(Convert.ToInt64((ulong)value));
if (value is DateTime)
{
@@ -78,19 +84,19 @@ public override DateTimeOffset ConvertFrom(object value)
return default(DateTimeOffset);
}
- public override object ConvertTo(DateTimeOffset value, Type destinationType)
+ public override object ConvertToInternal(DateTimeOffset value, Type destinationType)
{
if (destinationType == typeof(DateTimeOffset))
return value;
if (destinationType == typeof(byte[]))
- return ToDate(value).ToBytes();
+ return ToUnixTime(value).ToBytes();
if (destinationType == typeof(long))
- return ToDate(value);
+ return ToUnixTime(value);
if (destinationType == typeof(ulong))
- return (ulong)ToDate(value);
+ return (ulong)ToUnixTime(value);
if (destinationType == typeof(DateTime))
return value.LocalDateTime;
View
10 src/Types/DecimalType.cs
@@ -8,14 +8,14 @@ public class DecimalType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private decimal _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is DecimalType)
return _value == ((DecimalType)obj)._value;
- return _value == CassandraType.GetValue<decimal>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
4 src/Types/DecimalTypeConverter.cs
@@ -20,7 +20,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]);
}
- public override decimal ConvertFrom(object value)
+ public override decimal ConvertFromInternal(object value)
{
if (value is byte[])
return ((byte[])value).FromBytes<decimal>();
@@ -28,7 +28,7 @@ public override decimal ConvertFrom(object value)
return (decimal)Convert.ChangeType(value, typeof(decimal));
}
- public override object ConvertTo(decimal value, Type destinationType)
+ public override object ConvertToInternal(decimal value, Type destinationType)
{
if (destinationType == typeof(byte[]))
return value.ToBytes();
View
10 src/Types/DoubleType.cs
@@ -8,14 +8,14 @@ public class DoubleType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private Double _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is DoubleType)
return _value == ((DoubleType)obj)._value;
- return _value == CassandraType.GetValue<Double>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
4 src/Types/DoubleTypeConverter.cs
@@ -20,7 +20,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]);
}
- public override double ConvertFrom(object value)
+ public override double ConvertFromInternal(object value)
{
if (value is byte[])
return ((byte[])value).FromBytes<double>();
@@ -28,7 +28,7 @@ public override double ConvertFrom(object value)
return (double)Convert.ChangeType(value, typeof(double));
}
- public override object ConvertTo(double value, Type destinationType)
+ public override object ConvertToInternal(double value, Type destinationType)
{
if (destinationType == typeof(byte[]))
return value.ToBytes();
View
10 src/Types/DynamicCompositeType.cs
@@ -27,14 +27,14 @@ public DynamicCompositeType(IDictionary<char, Type> aliases)
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -59,7 +59,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private List<CassandraType> _value;
@@ -72,7 +72,7 @@ public override bool Equals(object obj)
if (obj is DynamicCompositeType)
objArray = ((DynamicCompositeType)obj)._value;
else
- objArray = CassandraType.GetValue<List<CassandraType>>(obj, Converter);
+ objArray = Converter.ConvertFrom(obj);
if (objArray == null)
return false;
View
6 src/Types/DynamicCompositeTypeConverter.cs
@@ -24,7 +24,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]) || destinationType == typeof(List<CassandraType>) || destinationType == typeof(CassandraType[]) || destinationType == typeof(string);
}
- public override List<CassandraType> ConvertFrom(object value)
+ public override List<CassandraType> ConvertFromInternal(object value)
{
if (value is byte[])
{
@@ -67,7 +67,7 @@ public override List<CassandraType> ConvertFrom(object value)
return null;
}
- public override object ConvertTo(List<CassandraType> value, Type destinationType)
+ public override object ConvertToInternal(List<CassandraType> value, Type destinationType)
{
if (!(value is List<CassandraType>))
return null;
@@ -121,7 +121,7 @@ public override byte[] ToBigEndian(List<CassandraType> value)
public override List<CassandraType> FromBigEndian(byte[] value)
{
- var obj = ConvertFrom(value);
+ var obj = ConvertFromInternal(value);
return obj;
}
}
View
10 src/Types/FloatType.cs
@@ -8,14 +8,14 @@ public class FloatType : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private float _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is FloatType)
return _value == ((FloatType)obj)._value;
- return _value == CassandraType.GetValue<float>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
4 src/Types/FloatTypeConverter.cs
@@ -20,7 +20,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]);
}
- public override float ConvertFrom(object value)
+ public override float ConvertFromInternal(object value)
{
if (value is byte[])
return ((byte[])value).FromBytes<float>();
@@ -28,7 +28,7 @@ public override float ConvertFrom(object value)
return (float)Convert.ChangeType(value, typeof(float));
}
- public override object ConvertTo(float value, Type destinationType)
+ public override object ConvertToInternal(float value, Type destinationType)
{
if (destinationType == typeof(byte[]))
return value.ToBytes();
View
10 src/Types/Int32Type.cs
@@ -8,14 +8,14 @@ public class Int32Type : CassandraType
#region Implimentation
- public override object GetValue(Type type)
+ protected override object GetValueInternal(Type type)
{
- return GetValue(_value, type, Converter);
+ return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
- _value = SetValue(obj, Converter);
+ _value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
@@ -40,7 +40,7 @@ public override string ToString()
#endregion
- internal override object GetRawValue() { return _value; }
+ protected override object GetRawValue() { return _value; }
private int _value;
@@ -51,7 +51,7 @@ public override bool Equals(object obj)
if (obj is Int32Type)
return _value == ((Int32Type)obj)._value;
- return _value == CassandraType.GetValue<Int32>(obj, Converter);
+ return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
View
4 src/Types/Int32TypeConverter.cs
@@ -20,7 +20,7 @@ public override bool CanConvertTo(Type destinationType)
return destinationType == typeof(byte[]);
}
- public override int ConvertFrom(object value)
+ public override int ConvertFromInternal(object value)
{
if (value is byte[])
return ((byte[])value).FromBytes<int>();
@@ -28,7 +28,7 @@ public override int ConvertFrom(object value)
return (int)Convert.ChangeType(value, typeof(int));
}
- public override object ConvertTo(int value, Type destinationType)
+ public override object ConvertToInternal(int value, Type destinationType)
{
if (destinationType == typeof(byte[]))
return value.ToBytes();
View
10 src/Types/IntegerType.cs
@@ -9,14 +9,14 @@ public class IntegerType : CassandraType
#region Implimentation
- public override object GetValue(Type type)