Skip to content

Commit fdf4eb8

Browse files
make quote replacement optional in UnsafeLiteral
1 parent fe43f22 commit fdf4eb8

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

Program/Program.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,17 @@ private class Installment
3535
static void Main(string[] args)
3636
{
3737

38-
var db = SqlLiteQueryFactory();
39-
40-
var id = db.Query("accounts").InsertGetId<int>(new
38+
var query = new Query("accounts").AsInsert(new
4139
{
4240
name = "new Account",
4341
currency_id = "USD",
44-
created_at = DateTime.UtcNow
42+
created_at = DateTime.UtcNow,
43+
Value = SqlKata.Expressions.UnsafeLiteral("nextval('hello')", replaceQuotes: false)
4544
});
4645

47-
var id2 = db.Select<int>("insert into accounts(name, currency_id, created_at) values ('account 2','usd','2019-01-01 20:00:00');select last_insert_rowid();");
48-
49-
Console.WriteLine($"last id is: {id}");
50-
Console.WriteLine($"last id2 is: {id2.First()}");
51-
46+
var compiler = new SqlServerCompiler();
47+
var sql = compiler.Compile(query).Sql;
48+
Console.WriteLine(sql);
5249
}
5350

5451
private static void log(Compiler compiler, Query query)

QueryBuilder/Expressions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ public static Variable Variable(string name)
1818
/// WARNING: don't pass user data directly to this method.
1919
/// </summary>
2020
/// <param name="value"></param>
21+
/// <param name="replaceQuotes">if true it will esacpe single quotes</param>
2122
/// <returns></returns>
22-
public static UnsafeLiteral UnsafeLiteral(string value)
23+
public static UnsafeLiteral UnsafeLiteral(string value, bool replaceQuotes = true)
2324
{
24-
return new UnsafeLiteral(value);
25+
return new UnsafeLiteral(value, replaceQuotes);
2526
}
2627
}
2728
}

QueryBuilder/UnsafeLiteral.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ namespace SqlKata
22
{
33
public class UnsafeLiteral
44
{
5-
private string _value;
6-
public string Value
5+
public string Value {get; set;}
6+
7+
public UnsafeLiteral(string value, bool replaceQuotes = true)
78
{
8-
get
9+
if(value == null)
910
{
10-
return _value;
11+
value = "";
1112
}
12-
set
13+
14+
if(replaceQuotes)
1315
{
14-
_value = value.Replace("'", "''");
16+
value = value.Replace("'", "''");
1517
}
16-
}
1718

18-
public UnsafeLiteral(string value)
19-
{
2019
this.Value = value;
2120
}
2221

0 commit comments

Comments
 (0)