Permalink
Browse files

Merge pull request #146 from Aaronontheweb/blob_support

added support for blob constant when working with binary data in CQL3.
  • Loading branch information...
2 parents 343fe42 + 0e695d6 commit e372c41a6a697fda5b77a2907ce84a6d31991688 @Aaronontheweb Aaronontheweb committed Jan 8, 2014
Showing with 36 additions and 3 deletions.
  1. +2 −2 SharedAssemblyInfo.cs
  2. +1 −1 VERSION
  3. +17 −0 src/CqlHelper.cs
  4. +16 −0 test/FluentCassandra.Tests/CqlHelperTest.cs
View
@@ -25,5 +25,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.2.4.0")]
-[assembly: AssemblyFileVersion("1.2.4.0")]
+[assembly: AssemblyVersion("1.2.5.0")]
+[assembly: AssemblyFileVersion("1.2.5.0")]
View
@@ -1 +1 @@
-1.2.4.0
+1.2.5.0
View
@@ -1,9 +1,26 @@
+using System.Text;
+
namespace FluentCassandra
{
public static class CqlHelper
{
/// <summary>
+ /// Formats a provided byte array into a hex string for use with CQL.
+ /// </summary>
+ /// <param name="value">The byte array to format</param>
+ /// <returns>A hexidecimal string with a leading "0x", which is expected by the CQL3 format.</returns>
+ /// <remarks>Intended to be used with blob data types in CQL only.</remarks>
+ public static string EscapeForCql(byte[] value)
+ {
+ var hex = new StringBuilder((value.Length * 2) + 2);
+ hex.Append("0x");
+ foreach (var b in value)
+ hex.AppendFormat("{0:x2}", b);
+ return hex.ToString();
+ }
+
+ /// <summary>
/// Escapse the provided string for use with CQL.
/// </summary>
/// <param name="value">The string value to escape.</param>
@@ -1,5 +1,6 @@
using System;
using System.Linq;
+using System.Text;
using Xunit;
namespace FluentCassandra
@@ -33,5 +34,20 @@ public void FormatCqlTest()
// assert
Assert.Equal(expected, actual);
}
+
+ [Fact]
+ public void EscapeBytesForCqlTest()
+ {
+ //arrange
+ var bytes = Encoding.UTF8.GetBytes("hello world");
+ var expectedHex = "0x68656c6c6f20776f726c64"; //generated via the textAsBlob function in CQL3 shell
+
+ //act
+ var hex = CqlHelper.EscapeForCql(bytes);
+
+ //assert
+ Assert.Equal(expectedHex, hex);
+ }
+
}
}

0 comments on commit e372c41

Please sign in to comment.