首页 > 其他分享 >自研ORM 子查询&嵌套查询

自研ORM 子查询&嵌套查询

时间:2023-07-07 11:57:07浏览次数:37  
标签:p2 p1 自研 db 查询 ORM Query Include CategoryId

作者 Mr-zhong

代码改变世界....

一、前言

Fast Framework 基于NET6.0 封装的轻量级 ORM 框架 支持多种数据库 SqlServer Oracle MySql PostgreSql Sqlite

优点: 体积小、可动态切换不同实现类库、原生支持微软特性、流畅API、使用简单、性能高、模型数据绑定采用 委托、强大的表达式解析、支持多种子查询可实现较为复杂查询、源代码可读性强。

缺点:目前仅支持Db Frist Code Frist 后续迭代。

开源地址:https://github.com/China-Mr-zhong/Fast-Framework (唯一)

更多示例:https://www.cnblogs.com/China-Mr-zhong/p/17514567.html

  • 导航查询

    • 示例代码

                      /// <summary>
                      /// 类别
                      /// </summary>
                      public class Category
                      {
                          /// <summary>
                          /// 类别ID
                          /// </summary>
                          [Key]
                          public int CategoryId { get; set; }
                      
                          /// <summary>
                          /// 类别名称
                          /// </summary>
                          public string CategoryName { get; set; }
                      
                          /// <summary>
                          /// 产品 Navigate MainName和ChildName 可不显示指定,会自动查找主键匹配或ID为结尾的属性
                          /// </summary>
                          [Navigate(MainName = nameof(CategoryId), ChildName = nameof(Product.CategoryId))]
                          public IEnumerable<Product> Products { get; set; }
                      
                      }
      
                      var data = db.Query<Category>()
                          .Include(i => i.Products)
                          .ToList();
      
    • 执行后Sql

      SELECT Include_A.`CategoryId`,Include_A.`CategoryName`,Include_B.`ProductId`,Include_B.`ProductCode`,Include_B.`ProductName`,Include_B.`DeleteMark`,Include_B.`CreateTime`,Include_B.`ModifyTime`,Include_B.`Custom1`,Include_B.`Custom2`,Include_B.`Custom3`,Include_B.`Custom4`,Include_B.`Custom5`,Include_B.`Custom6`,Include_B.`Custom7`,Include_B.`Custom8`,Include_B.`Custom9`,Include_B.`Custom10`,Include_B.`Custom11`,Include_B.`Custom12` FROM `Category` `Include_A`
      INNER JOIN `Product` `Include_B` ON `Include_A`.`CategoryId` = `Include_B`.`CategoryId`
      
  • Join子查询

    • 示例代码

                      var subQuery1 = db.Query<Product>().Select(s => new
                      {
                          s.ProductId,
                          s.CategoryId,
                          s.ProductCode,
                          s.ProductName,
                          s.DeleteMark
                      });
                      var data = db.Query<Category>().InnerJoin(subQuery1, (a, b) => a.CategoryId == b.CategoryId).ToList();
      
    • 执行后Sql

      SELECT p1.`CategoryId`,p1.`CategoryName`,p2.`ProductId`,p2.`ProductCode`,p2.`ProductName`,p2.`DeleteMark` FROM `Category` `p1`
      INNER JOIN ( SELECT `ProductId` AS `ProductId`,`CategoryId` AS `CategoryId`,`ProductCode` AS `ProductCode`,`ProductName` AS `ProductName`,`DeleteMark` AS `DeleteMark` FROM `Product` ) `p2` ON ( `p1`.`CategoryId` = `p2`.`CategoryId` )
      
  • From子查询

    • 示例代码

                      var subQuery2 = db.Query<Product>().Select(s=>new
                      {
                          s.ProductId,
                          s.CategoryId,
                          s.ProductCode,
                          s.ProductName,
                          s.DeleteMark
                      });
                      var data = db.Query(subQuery2).ToList();
      
    • 执行后Sql

      SELECT * FROM ( SELECT `ProductId` AS `ProductId`,`CategoryId` AS `CategoryId`,`ProductCode` AS `ProductCode`,`ProductName` AS `ProductName`,`DeleteMark` AS `DeleteMark` FROM `Product` ) x
      
  • Select子查询

    • 示例代码

                      var data = db.Query<Product>().Select(s => new
                      {
                          CategoryName = db.Query<Category>().Where(w => w.CategoryId == 1).Select(s => s.CategoryName).First()
                      }).First();
      
    • 执行后Sql

      SELECT ( SELECT `p2`.`CategoryName` FROM `Category` `p2`
      WHERE ( `p2`.`CategoryId` = 1 ) Limit 1 ) AS `CategoryName` FROM `Product` `p1` Limit 1
      
  • Select嵌套查询

    • 示例代码

      var data1 = db.Query<Product>().Select(s => new
                      {
                          NestedQuery = db.Query<Category>().Where(w => w.CategoryId == s.CategoryId).ToList()
                      }).First();
      
                      var data2 = db.Query<Product>().Where(w => w.ProductId == 1).Select(s => new
                      {
                          NestedQuery = db.Query<Category>().Where(w => w.CategoryId == s.CategoryId).ToList()
                      }).First();
      
    • 执行后Sql

      //内部机制主查询有结果才执行嵌套查询,懒加载实现
      
      SELECT 0 AS `fast_args_index_0` FROM `Product` `p1` Limit 1
      
      --------------------------------------------------------------------------
      
      SELECT p2.`CategoryId`,p2.`CategoryName`,p1.`ProductId`,p1.`ProductCode`,p1.`ProductName`,p1.`DeleteMark`,p1.`CreateTime`,p1.`ModifyTime`,p1.`Custom1`,p1.`Custom2`,p1.`Custom3`,p1.`Custom4`,p1.`Custom5`,p1.`Custom6`,p1.`Custom7`,p1.`Custom8`,p1.`Custom9`,p1.`Custom10`,p1.`Custom11`,p1.`Custom12` FROM `Category` `p2`
      RIGHT JOIN `Product` `p1` ON ( `p2`.`CategoryId` = `p1`.`CategoryId` )
      
      SELECT 0 AS `fast_args_index_0` FROM `Product` `p1`
      WHERE ( `p1`.`ProductId` = 1 ) Limit 1
      
  • Where子查询

    • 示例代码

                      var data = db.Query<Category>().Where(w => w.CategoryId == 1 && db.Query<Product>().Where(w => w.CategoryId == 1).Select(s => 1).Any()).First();//Any支持取反
      
    • 执行后Sql

      SELECT p1.`CategoryId`,p1.`CategoryName` FROM `Category` `p1`
      WHERE ( ( `p1`.`CategoryId` = 1 ) AND EXISTS ( SELECT 1 FROM `Product` `p2`
      WHERE ( `p2`.`CategoryId` = 1 ) ) ) Limit 1
      

标签:p2,p1,自研,db,查询,ORM,Query,Include,CategoryId
From: https://www.cnblogs.com/China-Mr-zhong/p/17534566.html

相关文章

  • MySQL查询语句优化方法
    1、应尽量避免在where子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。2、对查询进行优化,应尽量避免全表扫描,首先应考虑在where及orderby涉及的列上建立索引。3、应尽量避免在where子句中对字段进行null值判断,否则将导致引擎放弃使用索引而进行全表扫......
  • 批量查询快递单号的方法和步骤
    要进行快递售后管理,就必须掌握快递的物流信息,及时跟踪快递的动态。今天小编给你安利一款快递查询软件:“固乔快递查询助手”,可以帮助我们快速批量查询订单号,一键保存物流数据,有兴趣的伙伴可以一起往下看。先给大家展示一下用这款软件查询快递单号的效果如何。我们可以清楚地看到所有......
  • Gorm一对一,一对多,查询报错 unsupported relations for schema
    Gorm一对一,一对多,搞我半天bug标签(空格分隔):go,gorm问题:一对一,一对多,查询数据报错:unsupportedrelationsforschema代码//GoodsSpecificationAttrModel商品规格-属性表typeGoodsSpecificationAttrModelstruct{ IDint32`gorm:"co......
  • C#winform软件移植上linux的秘密,用GTK开发System.Windows.Forms
    国产系统大势所趋,如果你公司的winform界面软件需要在linux上运行,如果软件是用C#开发的,现在我有一个好的快速解决方案。世界第一的微软的MicrosoftVisualStudio,确实好用,C#开发起来确实效率高,不过微软的开发语言开发的软件的界面都是跟windows系统绑定的,现在.netcore已......
  • No bean named 'transactionManager' available: No matching PlatformTransactionMan
    报错内容:找不到transactionManager原因:xml配置平台事务管理器的时候给了id。配置@Transaction注解时没有配置transactionManager 解决方案:将xml中配置的id="tranManager"改为id="transactionManager"。原因是因为@Transaction中transactionManager的默认名称是”transactionM......
  • 【滨州学院】通过学生学号查询辅导员JS-miniui
    表单内容:1<divid="complex"class="mini-complex"style="background:rgb(255,255,255);padding:2px20px10px;box-shadow:rgb(144,144,144)0px0px5px;margin:10pxauto12px;font-size:14px;font-family:MicrosoftYaHei;......
  • 解决Java 线程安全的DateFormat的具体操作步骤
    Java线程安全的DateFormat在多线程的环境下使用Java的SimpleDateFormat类进行日期格式化操作时,可能会遇到线程安全的问题。这篇文章将会介绍为什么SimpleDateFormat不是线程安全的,以及如何解决这个问题。为什么SimpleDateFormat不是线程安全的?SimpleDateFormat是Java中用于格......
  • 【论文阅读】CrossFormer: A Versatile Vision Transformer Based on Cross-scale Att
    来自CVPR2021论文地址:https://link.zhihu.com/?target=https%3A//arxiv.org/pdf/2108.00154.pdf代码地址:https://link.zhihu.com/?target=https%3A//github.com/cheerss/CrossFormer一、Motivation 主要还是ViT的历史遗留问题ViT在处理输入时,将图片划分为了相等大小的图像......
  • XAML UI 框架横向对比(Avalonia/Uno Platform/.NET MAUI)
    本文翻译自 https://github.com/robloo/PublicDocs/blob/master/XAMLFrameworkComparison.md为了最佳阅读体验,请前往 https://github.com/1357310795/XAML-UI-Docs/blob/master/XAMLFrameworkComparison.md https://zhuanlan.zhihu.com/p/638115608XAML框架横向对比多年......
  • 【Oracle】当条件中存在空值时,同时将空值和非空值的结果查询出来
    【Oracle】当条件中存在空值时,同时将空值和非空值的结果查询出来如果不是一定要用这个存在空值的条件的话,最好还是不用为好,省的麻烦正常的查询结果如下select*fromttt20230705twheret.code='AA'如果一个表的查询条件中数据为空的时候,是不会查询出来这条空值相关的数......