@@ -23,11 +23,11 @@ namespace SQLiteSharp;
2323 /// <summary>
2424 /// Functions to convert CLR methods to SQL expressions.
2525 /// </summary>
26- public Dictionary < MethodInfo , Func < MethodCallExpression , ParameterExpression , string > > MethodToSqlConverters { get ; } = [ ] ;
26+ public Dictionary < MethodInfo , Func < MethodCallExpression , ParameterExpression , string > > MethodToSqlConverters { get ; }
2727 /// <summary>
2828 /// Functions to convert CLR properties/fields to SQL expressions.
2929 /// </summary>
30- public Dictionary < MemberInfo , Func < MemberExpression , ParameterExpression , string > > MemberToSqlConverters { get ; } = [ ] ;
30+ public Dictionary < MemberInfo , Func < MemberExpression , ParameterExpression , string > > MemberToSqlConverters { get ; }
3131
3232 private readonly List < string > SelectList = [ ] ;
3333 private readonly List < string > OrderByList = [ ] ;
@@ -45,7 +45,8 @@ namespace SQLiteSharp;
4545 internal SqlBuilder ( SqliteTable < T > table ) {
4646 Table = table ;
4747
48- AddDefaultSqlConverters ( ) ;
48+ MethodToSqlConverters = GetDefaultMethodToSqlConverters ( ) ;
49+ MemberToSqlConverters = GetDefaultMemberToSqlConverters ( ) ;
4950 }
5051
5152 /// <summary>
@@ -467,208 +468,209 @@ private string ConvertMethodCallToSql(MethodCallExpression methodCallExpression,
467468 // Method call not recognised
468469 return AddParameter ( methodCallExpression . Execute ( ) ) ;
469470 }
470- private void AddDefaultSqlConverters ( ) {
471+ private Dictionary < MethodInfo , Func < MethodCallExpression , ParameterExpression , string > > GetDefaultMethodToSqlConverters ( ) => new ( ) {
471472 // 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 ) => {
473474 string str1Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
474475 string str2Sql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
475476 return $ "{ str1Sql } = { str2Sql } ";
476- } ) ;
477+ } ,
477478
478479 // 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 ) => {
480481 string str1Sql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
481482 string str2Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
482483 return $ "{ str1Sql } = { str2Sql } ";
483- } ) ;
484+ } ,
484485
485486 // 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 ) => {
487488 string str1Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
488489 string str2Sql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
489490 StringComparison strComparison = ( StringComparison ) methodCall . Arguments [ 2 ] . Execute ( ) ! ;
490491 return $ "{ str1Sql } = { str2Sql } collate { StringComparisonToCollation ( strComparison ) . SqlQuote ( ) } ";
491- } ) ;
492+ } ,
492493
493494 // 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 ) => {
495496 string str1Sql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
496497 string str2Sql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
497498 StringComparison strComparison = ( StringComparison ) methodCall . Arguments [ 1 ] . Execute ( ) ! ;
498499 return $ "{ str1Sql } = { str2Sql } collate { StringComparisonToCollation ( strComparison ) . SqlQuote ( ) } ";
499- } ) ;
500+ } ,
500501
501502 // 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 ) => {
503504 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
504505 string ? subStr = ( string ? ) methodCall . Arguments [ 0 ] . Execute ( ) ;
505506 return $ "{ strSql } like { AddParameter ( "%" + subStr + "%" ) } escape '\\ '";
506- } ) ;
507+ } ,
507508
508509 // 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 ) => {
510511 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
511512 string ? subStr = ( string ? ) methodCall . Arguments [ 0 ] . Execute ( ) ;
512513 return $ "{ strSql } like { AddParameter ( subStr + "%" ) } escape '\\ '";
513- } ) ;
514+ } ,
514515
515516 // 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 ) => {
517518 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
518519 string ? subStr = ( string ? ) methodCall . Arguments [ 0 ] . Execute ( ) ;
519520 return $ "{ strSql } like { AddParameter ( "%" + subStr ) } escape '\\ '";
520- } ) ;
521+ } ,
521522
522523 // 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 ) => {
524525 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
525526 string oldSubStrSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
526527 string newSubStrSql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
527528 return $ "replace({ strSql } , { oldSubStrSql } , { newSubStrSql } )";
528- } ) ;
529+ } ,
529530
530531 // 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 ) => {
532533 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
533534 string startIndexSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
534535 string lengthSql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
535536 return $ "substr({ strSql } , { startIndexSql } , { lengthSql } )";
536- } ) ;
537+ } ,
537538
538539 // 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 ) => {
540541 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
541542 string startIndexSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
542543 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+ } ,
550545
551546 // string.ToLower()
552- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . ToLower ) , [ ] ) ! , ( methodCall , rowExpression ) => {
547+ [ typeof ( string ) . GetMethod ( nameof ( string . ToLower ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
553548 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
554549 return $ "lower({ strSql } )";
555- } ) ;
550+ } ,
556551
557552 // string.ToUpper()
558- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . ToUpper ) , [ ] ) ! , ( methodCall , rowExpression ) => {
553+ [ typeof ( string ) . GetMethod ( nameof ( string . ToUpper ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
559554 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
560555 return $ "upper({ strSql } )";
561- } ) ;
556+ } ,
562557
563558 // 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 ) => {
565560 string strSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
566561 return $ "({ strSql } is null or { strSql } = '')";
567- } ) ;
562+ } ,
568563
569564 // string.Trim()
570- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . Trim ) , [ ] ) ! , ( methodCall , rowExpression ) => {
565+ [ typeof ( string ) . GetMethod ( nameof ( string . Trim ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
571566 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
572567 return $ "trim({ strSql } )";
573- } ) ;
568+ } ,
574569
575570 // string.TrimStart()
576- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . TrimStart ) , [ ] ) ! , ( methodCall , rowExpression ) => {
571+ [ typeof ( string ) . GetMethod ( nameof ( string . TrimStart ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
577572 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
578573 return $ "ltrim({ strSql } )";
579- } ) ;
574+ } ,
580575
581576 // string.TrimEnd()
582- MethodToSqlConverters . Add ( typeof ( string ) . GetMethod ( nameof ( string . TrimEnd ) , [ ] ) ! , ( methodCall , rowExpression ) => {
577+ [ typeof ( string ) . GetMethod ( nameof ( string . TrimEnd ) , [ ] ) ! ] = ( methodCall , rowExpression ) => {
583578 string strSql = ExpressionToSql ( methodCall . Object ! , rowExpression ) ;
584579 return $ "rtrim({ strSql } )";
585- } ) ;
580+ } ,
586581
587582 // 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 ) => {
589584 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
590585 return $ "abs({ valueSql } )";
591- } ) ;
586+ } ,
592587
593588 // 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 ) => {
595590 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
596591 return $ "round({ valueSql } )";
597- } ) ;
592+ } ,
598593
599594 // 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 ) => {
601596 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
602597 return $ "ceil({ valueSql } )";
603- } ) ;
598+ } ,
604599
605600 // 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 ) => {
607602 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
608603 return $ "floor({ valueSql } )";
609- } ) ;
604+ } ,
610605
611606 // 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 ) => {
613608 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
614609 return $ "exp({ valueSql } )";
615- } ) ;
610+ } ,
616611
617612 // 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 ) => {
619614 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
620615 return $ "log({ valueSql } )";
621- } ) ;
616+ } ,
622617
623618 // 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 ) => {
625620 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
626621 string exponentSql = ExpressionToSql ( methodCall . Arguments [ 1 ] , rowExpression ) ;
627622 return $ "power({ valueSql } , { exponentSql } )";
628- } ) ;
623+ } ,
629624
630625 // 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 ) => {
632627 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
633628 return $ "sqrt({ valueSql } )";
634- } ) ;
629+ } ,
635630
636631 // 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 ) => {
638633 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
639634 return $ "sin({ valueSql } )";
640- } ) ;
635+ } ,
641636
642637 // 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 ) => {
644639 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
645640 return $ "cos({ valueSql } )";
646- } ) ;
641+ } ,
647642
648643 // 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 ) => {
650645 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
651646 return $ "tan({ valueSql } )";
652- } ) ;
647+ } ,
653648
654649 // 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 ) => {
656651 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
657652 return $ "asin({ valueSql } )";
658- } ) ;
653+ } ,
659654
660655 // 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 ) => {
662657 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
663658 return $ "acos({ valueSql } )";
664- } ) ;
659+ } ,
665660
666661 // 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 ) => {
668663 string valueSql = ExpressionToSql ( methodCall . Arguments [ 0 ] , rowExpression ) ;
669664 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+ } ;
672674}
673675
674676/// <summary>
0 commit comments