@@ -23,11 +23,11 @@ namespace SQLiteSharp;
23
23
/// <summary>
24
24
/// Functions to convert CLR methods to SQL expressions.
25
25
/// </summary>
26
- public Dictionary < MethodInfo , Func < MethodCallExpression , ParameterExpression , string > > MethodToSqlConverters { get ; } = [ ] ;
26
+ public Dictionary < MethodInfo , Func < MethodCallExpression , ParameterExpression , string > > MethodToSqlConverters { get ; }
27
27
/// <summary>
28
28
/// Functions to convert CLR properties/fields to SQL expressions.
29
29
/// </summary>
30
- public Dictionary < MemberInfo , Func < MemberExpression , ParameterExpression , string > > MemberToSqlConverters { get ; } = [ ] ;
30
+ public Dictionary < MemberInfo , Func < MemberExpression , ParameterExpression , string > > MemberToSqlConverters { get ; }
31
31
32
32
private readonly List < string > SelectList = [ ] ;
33
33
private readonly List < string > OrderByList = [ ] ;
@@ -45,7 +45,8 @@ namespace SQLiteSharp;
45
45
internal SqlBuilder ( SqliteTable < T > table ) {
46
46
Table = table ;
47
47
48
- AddDefaultSqlConverters ( ) ;
48
+ MethodToSqlConverters = GetDefaultMethodToSqlConverters ( ) ;
49
+ MemberToSqlConverters = GetDefaultMemberToSqlConverters ( ) ;
49
50
}
50
51
51
52
/// <summary>
@@ -467,208 +468,209 @@ private string ConvertMethodCallToSql(MethodCallExpression methodCallExpression,
467
468
// Method call not recognised
468
469
return AddParameter ( methodCallExpression . Execute ( ) ) ;
469
470
}
470
- private void AddDefaultSqlConverters ( ) {
471
+ private Dictionary < MethodInfo , Func < MethodCallExpression , ParameterExpression , string > > GetDefaultMethodToSqlConverters ( ) => new ( ) {
471
472
// string.Equals(string, string)
472
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) , typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
473
+ [ typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) , typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
473
474
string str1Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
474
475
string str2Sql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
475
476
return $ "{ str1Sql } = { str2Sql } ";
476
- } ) ;
477
+ } ,
477
478
478
479
// string.Equals(string)
479
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
480
+ [ typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
480
481
string str1Sql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
481
482
string str2Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
482
483
return $ "{ str1Sql } = { str2Sql } ";
483
- } ) ;
484
+ } ,
484
485
485
486
// string.Equals(string, string, StringComparison)
486
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) , typeof ( string ) , typeof ( StringComparison ) ] ) ! , ( methodCall , rowExpression ) => {
487
+ [ typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) , typeof ( string ) , typeof ( StringComparison ) ] ) ! ] = ( methodCall , rowExpression ) => {
487
488
string str1Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
488
489
string str2Sql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
489
490
StringComparison strComparison = ( StringComparison ) methodCall . Arguments [ 2 ] . Execute ( ) ! ;
490
491
return $ "{ str1Sql } = { str2Sql } collate { StringComparisonToCollation ( strComparison ) . SqlQuote ( ) } ";
491
- } ) ;
492
+ } ,
492
493
493
494
// string.Equals(string, StringComparison)
494
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) , typeof ( StringComparison ) ] ) ! , ( methodCall , rowExpression ) => {
495
+ [ typeof ( string ) . GetMethod ( nameof ( string . Equals ) , [ typeof ( string ) , typeof ( StringComparison ) ] ) ! ] = ( methodCall , rowExpression ) => {
495
496
string str1Sql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
496
497
string str2Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
497
498
StringComparison strComparison = ( StringComparison ) methodCall . Arguments [ 1 ] . Execute ( ) ! ;
498
499
return $ "{ str1Sql } = { str2Sql } collate { StringComparisonToCollation ( strComparison ) . SqlQuote ( ) } ";
499
- } ) ;
500
+ } ,
500
501
501
502
// string.Contains(string)
502
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Contains ) , [ typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
503
+ [ typeof ( string ) . GetMethod ( nameof ( string . Contains ) , [ typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
503
504
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
504
505
string ? subStr = ( string ? ) methodCall . Arguments [ 0 ] . Execute ( ) ;
505
506
return $ "{ strSql } like { AddParameter ( "%" + subStr + "%" ) } escape '\\ '";
506
- } ) ;
507
+ } ,
507
508
508
509
// string.StartsWith(string)
509
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . StartsWith ) , [ typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
510
+ [ typeof ( string ) . GetMethod ( nameof ( string . StartsWith ) , [ typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
510
511
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
511
512
string ? subStr = ( string ? ) methodCall . Arguments [ 0 ] . Execute ( ) ;
512
513
return $ "{ strSql } like { AddParameter ( subStr + "%" ) } escape '\\ '";
513
- } ) ;
514
+ } ,
514
515
515
516
// string.EndsWith(string)
516
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . EndsWith ) , [ typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
517
+ [ typeof ( string ) . GetMethod ( nameof ( string . EndsWith ) , [ typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
517
518
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
518
519
string ? subStr = ( string ? ) methodCall . Arguments [ 0 ] . Execute ( ) ;
519
520
return $ "{ strSql } like { AddParameter ( "%" + subStr ) } escape '\\ '";
520
- } ) ;
521
+ } ,
521
522
522
523
// string.Replace(string, string)
523
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Replace ) , [ typeof ( string ) , typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
524
+ [ typeof ( string ) . GetMethod ( nameof ( string . Replace ) , [ typeof ( string ) , typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
524
525
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
525
526
string oldSubStrSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
526
527
string newSubStrSql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
527
528
return $ "replace({ strSql } , { oldSubStrSql } , { newSubStrSql } )";
528
- } ) ;
529
+ } ,
529
530
530
531
// string.Substring(int, int)
531
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Substring ) , [ typeof ( int ) , typeof ( int ) ] ) ! , ( methodCall , rowExpression ) => {
532
+ [ typeof ( string ) . GetMethod ( nameof ( string . Substring ) , [ typeof ( int ) , typeof ( int ) ] ) ! ] = ( methodCall , rowExpression ) => {
532
533
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
533
534
string startIndexSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
534
535
string lengthSql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
535
536
return $ "substr({ strSql } , { startIndexSql } , { lengthSql } )";
536
- } ) ;
537
+ } ,
537
538
538
539
// string.Substring(int)
539
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Substring ) , [ typeof ( int ) ] ) ! , ( methodCall , rowExpression ) => {
540
+ [ typeof ( string ) . GetMethod ( nameof ( string . Substring ) , [ typeof ( int ) ] ) ! ] = ( methodCall , rowExpression ) => {
540
541
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
541
542
string startIndexSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
542
543
return $ "substr({ strSql } , { startIndexSql } )";
543
- } ) ;
544
-
545
- // string.Length
546
- MemberToSqlConverters . Add ( typeof ( string ) . GetProperty ( nameof ( string . Length ) ) ! , ( member , rowExpression ) => {
547
- string strSql = ExpressionToSql ( member . Expression ! , rowExpression ) ;
548
- return $ "length({ strSql } )";
549
- } ) ;
544
+ } ,
550
545
551
546
// string.ToLower()
552
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . ToLower ) , [ ] ) ! , ( methodCall , rowExpression ) => {
547
+ [ typeof ( string ) . GetMethod ( nameof ( string . ToLower ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
553
548
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
554
549
return $ "lower({ strSql } )";
555
- } ) ;
550
+ } ,
556
551
557
552
// string.ToUpper()
558
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . ToUpper ) , [ ] ) ! , ( methodCall , rowExpression ) => {
553
+ [ typeof ( string ) . GetMethod ( nameof ( string . ToUpper ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
559
554
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
560
555
return $ "upper({ strSql } )";
561
- } ) ;
556
+ } ,
562
557
563
558
// string.IsNullOrEmpty(string)
564
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . IsNullOrEmpty ) , [ typeof ( string ) ] ) ! , ( methodCall , rowExpression ) => {
559
+ [ typeof ( string ) . GetMethod ( nameof ( string . IsNullOrEmpty ) , [ typeof ( string ) ] ) ! ] = ( methodCall , rowExpression ) => {
565
560
string strSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
566
561
return $ "({ strSql } is null or { strSql } = '')";
567
- } ) ;
562
+ } ,
568
563
569
564
// string.Trim()
570
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Trim ) , [ ] ) ! , ( methodCall , rowExpression ) => {
565
+ [ typeof ( string ) . GetMethod ( nameof ( string . Trim ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
571
566
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
572
567
return $ "trim({ strSql } )";
573
- } ) ;
568
+ } ,
574
569
575
570
// string.TrimStart()
576
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . TrimStart ) , [ ] ) ! , ( methodCall , rowExpression ) => {
571
+ [ typeof ( string ) . GetMethod ( nameof ( string . TrimStart ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
577
572
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
578
573
return $ "ltrim({ strSql } )";
579
- } ) ;
574
+ } ,
580
575
581
576
// string.TrimEnd()
582
- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . TrimEnd ) , [ ] ) ! , ( methodCall , rowExpression ) => {
577
+ [ typeof ( string ) . GetMethod ( nameof ( string . TrimEnd ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
583
578
string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
584
579
return $ "rtrim({ strSql } )";
585
- } ) ;
580
+ } ,
586
581
587
582
// Math.Abs(double)
588
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Abs ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
583
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Abs ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
589
584
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
590
585
return $ "abs({ valueSql } )";
591
- } ) ;
586
+ } ,
592
587
593
588
// Math.Round(double)
594
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Round ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
589
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Round ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
595
590
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
596
591
return $ "round({ valueSql } )";
597
- } ) ;
592
+ } ,
598
593
599
594
// Math.Ceiling(double)
600
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Ceiling ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
595
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Ceiling ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
601
596
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
602
597
return $ "ceil({ valueSql } )";
603
- } ) ;
598
+ } ,
604
599
605
600
// Math.Floor(double)
606
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Floor ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
601
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Floor ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
607
602
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
608
603
return $ "floor({ valueSql } )";
609
- } ) ;
604
+ } ,
610
605
611
606
// Math.Exp(double)
612
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Exp ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
607
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Exp ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
613
608
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
614
609
return $ "exp({ valueSql } )";
615
- } ) ;
610
+ } ,
616
611
617
612
// Math.Log(double)
618
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Log ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
613
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Log ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
619
614
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
620
615
return $ "log({ valueSql } )";
621
- } ) ;
616
+ } ,
622
617
623
618
// Math.Pow(double, double)
624
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Pow ) , [ typeof ( double ) , typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
619
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Pow ) , [ typeof ( double ) , typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
625
620
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
626
621
string exponentSql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
627
622
return $ "power({ valueSql } , { exponentSql } )";
628
- } ) ;
623
+ } ,
629
624
630
625
// Math.Sqrt(double)
631
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Sqrt ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
626
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Sqrt ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
632
627
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
633
628
return $ "sqrt({ valueSql } )";
634
- } ) ;
629
+ } ,
635
630
636
631
// Math.Sin(double)
637
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Sin ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
632
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Sin ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
638
633
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
639
634
return $ "sin({ valueSql } )";
640
- } ) ;
635
+ } ,
641
636
642
637
// Math.Cos(double)
643
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Cos ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
638
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Cos ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
644
639
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
645
640
return $ "cos({ valueSql } )";
646
- } ) ;
641
+ } ,
647
642
648
643
// Math.Tan(double)
649
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Tan ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
644
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Tan ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
650
645
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
651
646
return $ "tan({ valueSql } )";
652
- } ) ;
647
+ } ,
653
648
654
649
// Math.Asin(double)
655
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Asin ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
650
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Asin ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
656
651
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
657
652
return $ "asin({ valueSql } )";
658
- } ) ;
653
+ } ,
659
654
660
655
// Math.Acos(double)
661
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Acos ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
656
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Acos ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
662
657
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
663
658
return $ "acos({ valueSql } )";
664
- } ) ;
659
+ } ,
665
660
666
661
// Math.Atan(double)
667
- MethodToSqlConverters . Add ( typeof ( Math ) . GetMethod ( nameof ( Math . Atan ) , [ typeof ( double ) ] ) ! , ( methodCall , rowExpression ) => {
662
+ [ typeof ( Math ) . GetMethod ( nameof ( Math . Atan ) , [ typeof ( double ) ] ) ! ] = ( methodCall , rowExpression ) => {
668
663
string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
669
664
return $ "atan({ valueSql } )";
670
- } ) ;
671
- }
665
+ } ,
666
+ } ;
667
+ private Dictionary < MemberInfo , Func < MemberExpression , ParameterExpression , string > > GetDefaultMemberToSqlConverters ( ) => new ( ) {
668
+ // string.Length
669
+ [ typeof ( string ) . GetProperty ( nameof ( string . Length ) ) ! ] = ( member , rowExpression ) => {
670
+ string strSql = ExpressionToSql ( member . Expression ! , rowExpression ) ;
671
+ return $ "length({ strSql } )";
672
+ } ,
673
+ } ;
672
674
}
673
675
674
676
/// <summary>
0 commit comments