Skip to content

Commit 9e21491

Browse files
committed
Add more method to SQL converters
1 parent 70b7c49 commit 9e21491

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

SQLiteSharp.Tests/OperatorTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ public void Test1() {
2222
// Find one item in the table matching a predicate with method calls
2323
ShopItem? Item2 = ShopItems.FindOne(ShopItem => !string.IsNullOrEmpty(ShopItem.ItemName) && ShopItem.ItemName.Replace("dragon", "drag") == "dragfruit");
2424
Assert.NotNull(Item2);
25+
26+
ShopItem? Item3 = ShopItems.FindOne(ShopItem => int.Parse(ShopItem.Count.ToString()).Equals(1));
2527
}
2628
}

SQLiteSharp/SqlBuilder.cs

+76
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,82 @@ private string ConvertMethodCallToSql(MethodCallExpression methodCallExpression,
663663
string valueSql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
664664
return $"atan({valueSql})";
665665
},
666+
667+
// int.ToString()
668+
[typeof(int).GetMethod(nameof(int.ToString), [])!] = (methodCall, rowExpression) => {
669+
string intSql = ExpressionToSql(methodCall.Object!, rowExpression);
670+
return $"cast({intSql} as text)";
671+
},
672+
673+
// int.Parse(string)
674+
[typeof(int).GetMethod(nameof(int.Parse), [typeof(string)])!] = (methodCall, rowExpression) => {
675+
string strSql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
676+
return $"cast({strSql} as integer)";
677+
},
678+
679+
// int.Equals(int)
680+
[typeof(int).GetMethod(nameof(int.Equals), [typeof(int)])!] = (methodCall, rowExpression) => {
681+
string int1Sql = ExpressionToSql(methodCall.Object!, rowExpression);
682+
string int2Sql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
683+
return $"{int1Sql} = {int2Sql}";
684+
},
685+
686+
// long.ToString()
687+
[typeof(long).GetMethod(nameof(long.ToString), [])!] = (methodCall, rowExpression) => {
688+
string longSql = ExpressionToSql(methodCall.Object!, rowExpression);
689+
return $"cast({longSql} as text)";
690+
},
691+
692+
// long.Parse(string)
693+
[typeof(long).GetMethod(nameof(long.Parse), [typeof(string)])!] = (methodCall, rowExpression) => {
694+
string strSql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
695+
return $"cast({strSql} as integer)";
696+
},
697+
698+
// long.Equals(long)
699+
[typeof(long).GetMethod(nameof(long.Equals), [typeof(long)])!] = (methodCall, rowExpression) => {
700+
string long1Sql = ExpressionToSql(methodCall.Object!, rowExpression);
701+
string long2Sql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
702+
return $"{long1Sql} = {long2Sql}";
703+
},
704+
705+
// float.ToString()
706+
[typeof(float).GetMethod(nameof(float.ToString), [])!] = (methodCall, rowExpression) => {
707+
string floatSql = ExpressionToSql(methodCall.Object!, rowExpression);
708+
return $"cast({floatSql} as text)";
709+
},
710+
711+
// float.Parse(string)
712+
[typeof(float).GetMethod(nameof(float.Parse), [typeof(string)])!] = (methodCall, rowExpression) => {
713+
string strSql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
714+
return $"cast({strSql} as integer)";
715+
},
716+
717+
// float.Equals(float)
718+
[typeof(float).GetMethod(nameof(float.Equals), [typeof(float)])!] = (methodCall, rowExpression) => {
719+
string float1Sql = ExpressionToSql(methodCall.Object!, rowExpression);
720+
string float2Sql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
721+
return $"{float1Sql} = {float2Sql}";
722+
},
723+
724+
// double.ToString()
725+
[typeof(double).GetMethod(nameof(double.ToString), [])!] = (methodCall, rowExpression) => {
726+
string doubleSql = ExpressionToSql(methodCall.Object!, rowExpression);
727+
return $"cast({doubleSql} as text)";
728+
},
729+
730+
// double.Parse(string)
731+
[typeof(double).GetMethod(nameof(double.Parse), [typeof(string)])!] = (methodCall, rowExpression) => {
732+
string doubleSql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
733+
return $"cast({doubleSql} as integer)";
734+
},
735+
736+
// double.Equals(double)
737+
[typeof(double).GetMethod(nameof(double.Equals), [typeof(double)])!] = (methodCall, rowExpression) => {
738+
string double1Sql = ExpressionToSql(methodCall.Object!, rowExpression);
739+
string double2Sql = ExpressionToSql(methodCall.Arguments[0], rowExpression);
740+
return $"{double1Sql} = {double2Sql}";
741+
},
666742
};
667743
private Dictionary<MemberInfo, Func<MemberExpression, ParameterExpression, string>> GetDefaultMemberToSqlConverters() => new() {
668744
// string.Length

0 commit comments

Comments
 (0)