Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
added support to allow nullables in response to issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Berardi committed Jul 7, 2010
1 parent f066328 commit 3cf9974
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 15 deletions.
1 change: 1 addition & 0 deletions FluentCassandra.Test/FluentCassandra.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Types\AsciiTypeTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Types\BytesTypeTest.cs" />
<Compile Include="Types\NullTypeTest.cs" />
<Compile Include="Types\LexicalUUIDTypeTest.cs" />
<Compile Include="Types\TimeUUIDTypeTest.cs" />
<Compile Include="Types\LongTypeTest.cs" />
Expand Down
70 changes: 70 additions & 0 deletions FluentCassandra.Test/Types/NullTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using FluentCassandra.Types;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.ComponentModel;
using System.Text;
using System.Linq;

namespace FluentCassandra.Test
{
[TestClass]
public class NullTypeTest
{
private CassandraContext _db;
private CassandraColumnFamily<AsciiType> _family;
private CassandraSuperColumnFamily<AsciiType, AsciiType> _superFamily;
private const string _testKey = "Test1";
private const string _testName = "Test1";
private const string _testSuperName = "SubTest1";

[TestInitialize]
public void TestInit()
{
_db = new CassandraContext("Testing", "localhost");
_family = _db.GetColumnFamily<AsciiType>("Standard");
_superFamily = _db.GetColumnFamily<AsciiType, AsciiType>("Super");

_family.InsertColumn(_testKey, "Test1", Math.PI);
_family.InsertColumn(_testKey, "Test2", Math.PI);
_family.InsertColumn(_testKey, "Test3", Math.PI);

_superFamily.InsertColumn(_testKey, _testSuperName, "Test1", Math.PI);
_superFamily.InsertColumn(_testKey, _testSuperName, "Test2", Math.PI);
_superFamily.InsertColumn(_testKey, _testSuperName, "Test3", Math.PI);
}

[TestCleanup]
public void TestCleanup()
{
_db.Dispose();
}

[TestMethod]
public void Implicity_Cast_To_Int64()
{
// arranage
long? expected = null;

// act
long? actual = _family.Get(_testKey).Fetch(_testName).FirstOrDefault().AsDynamic().ShouldNotBeFound;

// assert
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void Implicity_Cast_To_FluentSuperColumn()
{
// arranage
var expectedName = "ShouldNotBeFound";
var expectedColumnCount = 0;

// act
FluentSuperColumn<AsciiType, AsciiType> actual = _superFamily.Get(_testKey).FirstOrDefault().AsDynamic().ShouldNotBeFound;

// assert
Assert.AreEqual(expectedName, (string)actual.ColumnName);
Assert.AreEqual(expectedColumnCount, actual.Columns.Count);
}
}
}
16 changes: 16 additions & 0 deletions FluentCassandra/FluentRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public FluentRecord()
/// </summary>
public abstract IList<T> Columns { get; }

/// <summary>
/// Checks to see if a column exists.
/// </summary>
public virtual bool ColumnExists(string name)
{
return ColumnExists(name, StringComparison.InvariantCultureIgnoreCase);
}

/// <summary>
/// Checks to see if a column exists.
/// </summary>
public virtual bool ColumnExists(string name, StringComparison comparison)
{
return Columns.Any(c => String.Equals(c.ColumnName, name, comparison));
}

/// <summary>
///
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion FluentCassandra/FluentSuperColumnFamily.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public IFluentSuperColumn<CompareWith, CompareSubcolumnWith> CreateSuperColumn()
return new FluentSuperColumn<CompareWith, CompareSubcolumnWith>();
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public IFluentSuperColumn<CompareWith, CompareSubcolumnWith> CreateSuperColumn(CompareWith name)
{
return new FluentSuperColumn<CompareWith, CompareSubcolumnWith> {
ColumnName = name
};
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -103,7 +114,7 @@ public override bool TryGetColumn(object name, out object result)
{
var col = Columns.FirstOrDefault(c => c.ColumnName == name);

result = (col == null) ? CreateSuperColumn() : col;
result = (col == null) ? CreateSuperColumn(CassandraType.GetType<CompareWith>(name)) : col;
//return col != null;
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions FluentCassandra/Types/AsciiType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override object GetValue(Type type)
{
var converter = Converter;

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
var nc = new NullableConverter(type);
type = nc.UnderlyingType;
}

if (!converter.CanConvertTo(type))
throw new InvalidCastException(type + " cannot be cast to " + TypeCode);

Expand Down
24 changes: 24 additions & 0 deletions FluentCassandra/Types/BytesType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override object GetValue(Type type)
{
var converter = Converter;

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
var nc = new NullableConverter(type);
type = nc.UnderlyingType;
}

if (!converter.CanConvertTo(type))
throw new InvalidCastException(type + " cannot be cast to " + TypeCode);

Expand Down Expand Up @@ -118,6 +124,24 @@ public static implicit operator BytesType(byte[] s)
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 byte?(BytesType o) { return ConvertTo<byte>(o); }
public static implicit operator sbyte?(BytesType o) { return ConvertTo<sbyte>(o); }
public static implicit operator short?(BytesType o) { return ConvertTo<short>(o); }
public static implicit operator ushort?(BytesType o) { return ConvertTo<ushort>(o); }
public static implicit operator int?(BytesType o) { return ConvertTo<int>(o); }
public static implicit operator uint?(BytesType o) { return ConvertTo<uint>(o); }
public static implicit operator long?(BytesType o) { return ConvertTo<long>(o); }
public static implicit operator ulong?(BytesType o) { return ConvertTo<ulong>(o); }
public static implicit operator float?(BytesType o) { return ConvertTo<float>(o); }
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); }

private static T ConvertTo<T>(BytesType type)
{
if (type == null)
Expand Down
4 changes: 2 additions & 2 deletions FluentCassandra/Types/BytesTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, System.Global
case TypeCode.String:
return Encoding.UTF8.GetBytes((string)value);
default:
throw new NotSupportedException(value.GetType() + " is not supported for binary serialization.");
return null;
}
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public override object ConvertTo(ITypeDescriptorContext context, System.Globaliz
case TypeCode.String:
return Encoding.UTF8.GetString(bytes);
default:
throw new NotSupportedException(destinationType + " is not supported for binary serialization.");
return null;
}
}

Expand Down
19 changes: 19 additions & 0 deletions FluentCassandra/Types/CassandraType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private static T Convert<T>(CassandraType type)
}

public static implicit operator byte[](CassandraType o) { return Convert<byte[]>(o); }

public static implicit operator byte(CassandraType o) { return Convert<byte>(o); }
public static implicit operator sbyte(CassandraType o) { return Convert<sbyte>(o); }
public static implicit operator short(CassandraType o) { return Convert<short>(o); }
Expand All @@ -88,6 +89,24 @@ private static T Convert<T>(CassandraType type)
public static implicit operator DateTime(CassandraType o) { return Convert<DateTime>(o); }
public static implicit operator DateTimeOffset(CassandraType o) { return Convert<DateTimeOffset>(o); }

public static implicit operator byte?(CassandraType o) { return Convert<byte?>(o); }
public static implicit operator sbyte?(CassandraType o) { return Convert<sbyte?>(o); }
public static implicit operator short?(CassandraType o) { return Convert<short?>(o); }
public static implicit operator ushort?(CassandraType o) { return Convert<ushort?>(o); }
public static implicit operator int?(CassandraType o) { return Convert<int?>(o); }
public static implicit operator uint?(CassandraType o) { return Convert<uint?>(o); }
public static implicit operator long?(CassandraType o) { return Convert<long?>(o); }
public static implicit operator ulong?(CassandraType o) { return Convert<ulong?>(o); }
public static implicit operator float?(CassandraType o) { return Convert<float?>(o); }
public static implicit operator double?(CassandraType o) { return Convert<double?>(o); }
public static implicit operator decimal?(CassandraType o) { return Convert<decimal?>(o); }
public static implicit operator bool?(CassandraType o) { return Convert<bool?>(o); }
//public static implicit operator string(CassandraType o) { return Convert<string>(o); }
public static implicit operator char?(CassandraType o) { return Convert<char?>(o); }
public static implicit operator Guid?(CassandraType o) { return Convert<Guid?>(o); }
public static implicit operator DateTime?(CassandraType o) { return Convert<DateTime?>(o); }
public static implicit operator DateTimeOffset?(CassandraType o) { return Convert<DateTimeOffset?>(o); }

#endregion

#region IConvertible Members
Expand Down
12 changes: 8 additions & 4 deletions FluentCassandra/Types/LexicalUUIDType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override object GetValue(Type type)
{
var converter = Converter;

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
var nc = new NullableConverter(type);
type = nc.UnderlyingType;
}

if (!converter.CanConvertTo(type))
throw new InvalidCastException(type + " cannot be cast to " + TypeCode);

Expand Down Expand Up @@ -72,10 +78,8 @@ public override int GetHashCode()

#region Conversion

public static implicit operator Guid(LexicalUUIDType type)
{
return type._value;
}
public static implicit operator Guid(LexicalUUIDType type) { return type._value; }
public static implicit operator Guid?(LexicalUUIDType type) { return type._value; }

public static implicit operator LexicalUUIDType(Guid o)
{
Expand Down
12 changes: 8 additions & 4 deletions FluentCassandra/Types/LongType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override object GetValue(Type type)
{
var converter = Converter;

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
var nc = new NullableConverter(type);
type = nc.UnderlyingType;
}

if (!converter.CanConvertTo(type))
throw new InvalidCastException(type + " cannot be cast to " + TypeCode);

Expand Down Expand Up @@ -72,10 +78,8 @@ public override int GetHashCode()

#region Conversion

public static implicit operator long(LongType type)
{
return type._value;
}
public static implicit operator long(LongType type) { return type._value; }
public static implicit operator long?(LongType type) { return type._value; }

public static implicit operator byte[](LongType type)
{
Expand Down
18 changes: 18 additions & 0 deletions FluentCassandra/Types/NullType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,23 @@ protected override TypeCode TypeCode
{
get { return TypeCode.Empty; }
}

public static implicit operator byte?(NullType o) { return null; }
public static implicit operator sbyte?(NullType o) { return null; }
public static implicit operator short?(NullType o) { return null; }
public static implicit operator ushort?(NullType o) { return null; }
public static implicit operator int?(NullType o) { return null; }
public static implicit operator uint?(NullType o) { return null; }
public static implicit operator long?(NullType o) { return null; }
public static implicit operator ulong?(NullType o) { return null; }
public static implicit operator float?(NullType o) { return null; }
public static implicit operator double?(NullType o) { return null; }
public static implicit operator decimal?(NullType o) { return null; }
public static implicit operator bool?(NullType o) { return null; }
public static implicit operator string(NullType o) { return null; }
public static implicit operator char?(NullType o) { return null; }
public static implicit operator Guid?(NullType o) { return null; }
public static implicit operator DateTime?(NullType o) { return null; }
public static implicit operator DateTimeOffset?(NullType o) { return null; }
}
}
18 changes: 14 additions & 4 deletions FluentCassandra/Types/TimeUUIDType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override object GetValue(Type type)
{
var converter = Converter;

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
var nc = new NullableConverter(type);
type = nc.UnderlyingType;
}

if (!converter.CanConvertTo(type))
throw new InvalidCastException(type + " cannot be cast to " + TypeCode);

Expand Down Expand Up @@ -72,10 +78,14 @@ public override int GetHashCode()

#region Conversion

public static implicit operator Guid(TimeUUIDType type)
{
return type._value;
}
public static implicit operator Guid(TimeUUIDType type) { return type._value; }
public static implicit operator Guid?(TimeUUIDType type) { return type._value; }

public static implicit operator DateTime(TimeUUIDType type) { return GuidGenerator.GetDateTime(type._value); }
public static implicit operator DateTime?(TimeUUIDType type) { return GuidGenerator.GetDateTime(type._value); }

public static implicit operator DateTimeOffset(TimeUUIDType type) { return GuidGenerator.GetDateTimeOffset(type._value); }
public static implicit operator DateTimeOffset?(TimeUUIDType type) { return GuidGenerator.GetDateTimeOffset(type._value); }

public static implicit operator TimeUUIDType(DateTime o)
{
Expand Down
6 changes: 6 additions & 0 deletions FluentCassandra/Types/UTF8Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override object GetValue(Type type)
{
var converter = Converter;

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
var nc = new NullableConverter(type);
type = nc.UnderlyingType;
}

if (!converter.CanConvertTo(type))
throw new InvalidCastException(type + " cannot be cast to " + TypeCode);

Expand Down

0 comments on commit 3cf9974

Please sign in to comment.