Last active
January 18, 2017 18:33
-
-
Save binki/dbeee64273b404042e3ae81f042358be to your computer and use it in GitHub Desktop.
Play with C# string interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * FROM blah b WHERE b.Thing = @p0 | |
p0=plant | |
SELECT * FROM anotherThing at WHERE at.Value = 'asdf' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace FormattablePlay | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var stringVar = "plant"; | |
Append($"SELECT * FROM blah b WHERE b.Thing = {stringVar}"); | |
var rawSql = new RawSql("'asdf'"); | |
Append($"SELECT * FROM anotherThing at WHERE at.Value = {rawSql}"); | |
} | |
static void Append(IFormattable query) | |
{ | |
var formatter = new SqlFormatInfo(); | |
Console.WriteLine(query.ToString(null, formatter)); | |
foreach (var p in formatter.Parameters) | |
Console.WriteLine($"\t{p.Key}={p.Value}"); | |
Console.WriteLine(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace FormattablePlay | |
{ | |
class RawSql | |
: ISqlFormattable | |
{ | |
string Sql { get; } | |
public RawSql( | |
string sql) | |
{ | |
Sql = sql; | |
} | |
public string ToSql() => Sql; | |
public override string ToString() => Sql; | |
} | |
internal interface ISqlFormattable | |
{ | |
string ToSql(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace FormattablePlay | |
{ | |
class SqlFormatInfo | |
: ICustomFormatter | |
, IFormatProvider | |
{ | |
Dictionary<string, object> parameters { get; } = new Dictionary<string, object>(); | |
public IReadOnlyDictionary<string, object> Parameters => parameters; | |
int LastParameterIndex { get; set; } | |
public string Format(string format, object arg, IFormatProvider formatProvider) | |
{ | |
var sqlFormattable = arg as ISqlFormattable; | |
if (sqlFormattable != null) | |
return sqlFormattable.ToSql(); | |
var parameterName = $"p{LastParameterIndex++}"; | |
parameters[parameterName] = arg; | |
return $"@{parameterName}"; | |
} | |
public object GetFormat(Type formatType) | |
=> this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment