Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 65 lines (57 sloc) 2.21 kB
e5a7795 @nberardi fixed merge from issue #50
nberardi authored
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace FluentCassandra
7 {
8 public static class CqlHelper
9 {
10 /// <summary>
11 /// Escapse the provided string for use with CQL.
12 /// </summary>
13 /// <param name="value">The string value to escape.</param>
14 /// <returns>The escaped value.</returns>
15 public static string EscapeForCql(string value)
16 {
17 string returnValue = value;
18
19 if (value != null)
20 {
21 returnValue = value.Replace("'", "''");
22 }
23
24 return returnValue;
25 }
26
27 /// <summary>
28 /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
29 /// Arguments are updated to make sure reserved characters are escaped to support Cassandra's CQL.
30 /// </summary>
31 /// <param name="format">A composite format string.</param>
32 /// <param name="args">An object array that contains zero or more objects to format.</param>
33 /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.
34 /// Each arg has also been updated to escape reserved CQL characters.</returns>
35 public static string FormatCql(string format, params object[] args)
36 {
37 object[] cleanArgs;
38
39 if (args != null && args.Length > 0)
40 {
41 cleanArgs = new object[args.Length];
42 for (int lp1 = 0; lp1 < args.Length; lp1++)
43 {
44 if (args[lp1] != null)
45 {
46 //Espace single quote by replacing it with two single quotes.
47 cleanArgs[lp1] = EscapeForCql(args[lp1].ToString());
48 }
49 else
50 {
51 cleanArgs[lp1] = args[lp1];
52 }
53 }
54 }
55 else
56 {
57 cleanArgs = args;
58 }
59
60 return string.Format(format, cleanArgs);
61 }
62
63 }
64 }
Something went wrong with that request. Please try again.