Browse files

fixed merge from issue #50

  • Loading branch information...
1 parent fdc415c commit e5a779512011c1ec2f19ded287f8c14b79c6d17d @nberardi nberardi committed Aug 29, 2012
View
64 src/CqlHelper.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace FluentCassandra
+{
+ public static class CqlHelper
+ {
+ /// <summary>
+ /// Escapse the provided string for use with CQL.
+ /// </summary>
+ /// <param name="value">The string value to escape.</param>
+ /// <returns>The escaped value.</returns>
+ public static string EscapeForCql(string value)
+ {
+ string returnValue = value;
+
+ if (value != null)
+ {
+ returnValue = value.Replace("'", "''");
+ }
+
+ return returnValue;
+ }
+
+ /// <summary>
+ /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
+ /// Arguments are updated to make sure reserved characters are escaped to support Cassandra's CQL.
+ /// </summary>
+ /// <param name="format">A composite format string.</param>
+ /// <param name="args">An object array that contains zero or more objects to format.</param>
+ /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.
+ /// Each arg has also been updated to escape reserved CQL characters.</returns>
+ public static string FormatCql(string format, params object[] args)
+ {
+ object[] cleanArgs;
+
+ if (args != null && args.Length > 0)
+ {
+ cleanArgs = new object[args.Length];
+ for (int lp1 = 0; lp1 < args.Length; lp1++)
+ {
+ if (args[lp1] != null)
+ {
+ //Espace single quote by replacing it with two single quotes.
+ cleanArgs[lp1] = EscapeForCql(args[lp1].ToString());
+ }
+ else
+ {
+ cleanArgs[lp1] = args[lp1];
+ }
+ }
+ }
+ else
+ {
+ cleanArgs = args;
+ }
+
+ return string.Format(format, cleanArgs);
+ }
+
+ }
+}
View
1 src/FluentCassandra.csproj
@@ -125,6 +125,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="CassandraCqlRowSchema.cs" />
+ <Compile Include="CqlHelper.cs" />
<Compile Include="FluentCqlRow.cs" />
<Compile Include="ILoadable.cs" />
<Compile Include="System\DateTimePrecise.cs" />
View
37 test/FluentCassandra.Tests/CqlHelperTest.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Linq;
+using Xunit;
+
+namespace FluentCassandra
+{
+ public class CQLHelperTest
+ {
+ [Fact]
+ public void EscapeForCqlTest()
+ {
+ // arrange
+ var expected = "My''Test''Data";
+
+ // act
+ var actual = CqlHelper.EscapeForCql("My'Test'Data");
+
+ // assert
+ Assert.Equal(expected, actual);
+ }
+
+ [Fact]
+ public void FormatCqlTest()
+ {
+ string[] arr1 = new string[] { "one'", "tw'o", "'three" };
+ string format = "{0} : {1} - {2}";
+ // arrange
+ var expected = "one'' : tw''o - ''three";
+
+ // act
+ var actual = CqlHelper.FormatCql(format, arr1);
+
+ // assert
+ Assert.Equal(expected, actual);
+ }
+ }
+}
View
1 test/FluentCassandra.Tests/FluentCassandra.Tests.csproj
@@ -59,6 +59,7 @@
<Compile Include="CassandraQueryTest.cs" />
<Compile Include="Connections\ConnectionProviderTests.cs" />
<Compile Include="Connections\NormalConnectionProviderTests.cs" />
+ <Compile Include="CqlHelperTest.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Linq\LinqToCqlObjectsCassandraTests.cs" />
<Compile Include="Linq\LinqToCqlObjectsTests.cs" />

0 comments on commit e5a7795

Please sign in to comment.