以下是来自官方的一个user group pet 的查询demo

参考关系图

环境准备

  • docker-compose mysql 环境
version: "3"
services:
  mysql:
    image: mysql:5.7.16
    ports:
      - 3306:3306
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: dalongrong
      MYSQL_DATABASE: gogs
      MYSQL_USER: gogs
      MYSQL_PASSWORD: dalongrong
      TZ: Asia/Shanghai
 
 
  • golang mod 项目
go mod init github.com/rongfengliang/ent-pet-user-group

生成实体模型

  • 生成schema
entc init Pet User Group
  • 配置schema 字段以及边
    pets
package schema
import (
  "github.com/facebookincubator/ent"
  "github.com/facebookincubator/ent/schema/edge"
  "github.com/facebookincubator/ent/schema/field"
)
// Pet holds the schema definition for the Pet entity.
type Pet struct {
  ent.Schema
}
// Fields of the Pet.
func (Pet) Fields() []ent.Field {
  return []ent.Field{
    field.String("name"),
  }
}
// Edges of the Pet.
func (Pet) Edges() []ent.Edge {
  return []ent.Edge{
    edge.To("friends", Pet.Type),
    edge.From("owner", User.Type).
      Ref("pets").
      Unique(),
  }
}
 
 

user

package schema
import (
  "github.com/facebookincubator/ent"
  "github.com/facebookincubator/ent/schema/edge"
  "github.com/facebookincubator/ent/schema/field"
)
// User holds the schema definition for the User entity.
type User struct {
  ent.Schema
}
// Fields of the User.
func (User) Fields() []ent.Field {
  return []ent.Field{
    field.Int("age"),
    field.String("name"),
  }
}
// Edges of the User.
func (User) Edges() []ent.Edge {
  return []ent.Edge{
    edge.To("pets", Pet.Type),
    edge.To("friends", User.Type),
    edge.From("groups", Group.Type).
      Ref("users"),
    edge.From("manage", Group.Type).
      Ref("admin"),
  }
}
 
 

group

package schema
import (
  "github.com/facebookincubator/ent"
  "github.com/facebookincubator/ent/schema/edge"
  "github.com/facebookincubator/ent/schema/field"
)
// Group holds the schema definition for the Group entity.
type Group struct {
  ent.Schema
}
// Fields of the Group.
func (Group) Fields() []ent.Field {
  return []ent.Field{
    field.String("name"),
  }
}
// Edges of the Group.
func (Group) Edges() []ent.Edge {
  return []ent.Edge{
    edge.To("users", User.Type),
    edge.To("admin", User.Type).
      Unique(),
  }
}
 

生成边以及定点数据

package main
import (
  "context"
  "fmt"
  "log"
  _ "github.com/go-sql-driver/mysql"
  "github.com/rongfengliang/ent-pet-user-group/ent"
)
func main() {
  client, err := ent.Open("mysql", "root:dalongrong@tcp(127.0.0.1)/gogs")
  if err != nil {
    log.Fatalf("failed opening connection to sqlite: %v", err)
  }
  defer client.Close()
  ctx := context.Background()
  Gen(ctx, client.Debug())
}
// Gen for generate demo datas
func Gen(ctx context.Context, client *ent.Client) error {
  hub, err := client.Group.
    Create().
    SetName("Github").
    Save(ctx)
  if err != nil {
    return fmt.Errorf("failed creating the group: %v", err)
  }
  // Create the admin of the group.
  // Unlike `Save`, `SaveX` panics if an error occurs.
  dan := client.User.
    Create().
    SetAge(29).
    SetName("Dan").
    AddManage(hub).
    SaveX(ctx)
  // Create "Ariel" and its pets.
  a8m := client.User.
    Create().
    SetAge(30).
    SetName("Ariel").
    AddGroups(hub).
    AddFriends(dan).
    SaveX(ctx)
  pedro := client.Pet.
    Create().
    SetName("Pedro").
    SetOwner(a8m).
    SaveX(ctx)
  xabi := client.Pet.
    Create().
    SetName("Xabi").
    SetOwner(a8m).
    SaveX(ctx)
  // Create "Alex" and its pets.
  alex := client.User.
    Create().
    SetAge(37).
    SetName("Alex").
    SaveX(ctx)
  coco := client.Pet.
    Create().
    SetName("Coco").
    SetOwner(alex).
    AddFriends(pedro).
    SaveX(ctx)
  fmt.Println("Pets created:", pedro, xabi, coco)
  // Output:
  // Pets created: Pet(id=1, name=Pedro) Pet(id=2, name=Xabi) Pet(id=3, name=Coco)
  return nil
}
 

一些查询

为了方便查看生成的sql 我使用了debug,同时也可以开启慢查询

set global slow_query_log=1;
set global long_query_time=0;
  • 一个demo
    上面的遍历从一个Group实体开始,继续到其admin(边),继续到其friends(边),获取其pets(边),获取每个宠物的
    friends(边),并请求其所有者
    参考图

    代码:

 
func Traverse(ctx context.Context, client *ent.Client) error {
    owner, err := client.Group. // GroupClient.
        Query(). // Query builder.
        Where(group.Name("Github")). // Filter only Github group (only 1).
        QueryAdmin(). // Getting Dan.
        QueryFriends(). // Getting Dan's friends: [Ariel].
        QueryPets(). // Their pets: [Pedro, Xabi].
        QueryFriends(). // Pedro's friends: [Coco], Xabi's friends: [].
        QueryOwner(). // Coco's owner: Alex.
        Only(ctx) // Expect only one entity to return in the query.
    if err != nil {
        return fmt.Errorf("failed querying the owner: %v", err)
    }
    fmt.Println(owner)
    // Output:
    // User(id=3, age=37, name=Alex)
    return nil
}
 

效果:

2019/10/15 13:10:12 driver.Query: query=SELECT DISTINCT `users`.`id`, `users`.`age`, `users`.`name` FROM `users` JOIN (SELECT `pets`.`owner_id` FROM
 `pets` JOIN (SELECT `pet_friends`.`friend_id` FROM `pet_friends` JOIN (SELECT `pets`.`id` FROM `pets` JOIN (SELECT `users`.`id` FROM `users` JOIN (
SELECT `user_friends`.`friend_id` FROM `user_friends` JOIN (SELECT `users`.`id` FROM `users` JOIN (SELECT `groups`.`admin_id` FROM `groups` WHERE `g
roups`.`name` = ?) AS `t1` ON `users`.`id` = `t1`.`admin_id`) AS `t1` ON `user_friends`.`user_id` = `t1`.`id`) AS `t1` ON `users`.`id` = `t1`.`frien
d_id`) AS `t1` ON `pets`.`owner_id` = `t1`.`id`) AS `t1` ON `pet_friends`.`pet_id` = `t1`.`id`) AS `t1` ON `pets`.`id` = `t1`.`friend_id`) AS `t1` O
N `users`.`id` = `t1`.`owner_id` LIMIT ? args=[Github 2]
User(id=3, age=37, name=Alex)
 
 

sql 查询计划

EXPLAIN SELECT
    DISTINCT `users`.`id`,
    `users`.`age`,
    `users`.`name`
FROM
    `users`
    JOIN (
        SELECT
            `pets`.`owner_id`
        FROM
            `pets`
            JOIN (
                SELECT
                    `pet_friends`.`friend_id`
                FROM
                    `pet_friends`
                    JOIN (
                        SELECT
                            `pets`.`id`
                        FROM
                            `pets`
                            JOIN (
                                SELECT
                                    `users`.`id`
                                FROM
                                    `users`
                                    JOIN (
                                        SELECT
                                            `user_friends`.`friend_id`
                                        FROM
                                            `user_friends`
                                            JOIN (
                                                SELECT
                                                    `users`.`id`
                                                FROM
                                                    `users`
                                                    JOIN (
                                                        SELECT
                                                            `groups`.`admin_id`
                                                        FROM
                                                            `groups`
                                                        WHERE
                                                            `groups`.`name` = 'Github'
                                                    ) AS `t1` ON `users`.`id` = `t1`.`admin_id`
                                            ) AS `t1` ON `user_friends`.`user_id` = `t1`.`id`
                                    ) AS `t1` ON `users`.`id` = `t1`.`friend_id`
                            ) AS `t1` ON `pets`.`owner_id` = `t1`.`id`
                    ) AS `t1` ON `pet_friends`.`pet_id` = `t1`.`id`
            ) AS `t1` ON `pets`.`id` = `t1`.`friend_id`
    ) AS `t1` ON `users`.`id` = `t1`.`owner_id`
 
 

效果:

  • 另外一个demo
    我们要获取所有owner(edge)是friend 某个组admin(边)的宠物(实体)。 
func Traverse(ctx context.Context, client *ent.Client) error {
    pets, err := client.Pet.
        Query().
        Where(
            pet.HasOwnerWith(
                user.HasFriendsWith(
                    user.HasManage(),
                ),
            ),
        ).
        All(ctx)
    if err != nil {
        return fmt.Errorf("failed querying the pets: %v", err)
    }
    fmt.Println(pets)
    // Output:
    // [Pet(id=1, name=Pedro) Pet(id=2, name=Xabi)]
    return nil
}
 
 

效果

go run cmd/query2/main.go 
2019/10/15 13:18:47 driver.Query: query=SELECT DISTINCT `pets`.`id`, `pets`.`name` FROM `pets` WHERE `pets`.`owner_id` IN (SELECT `id` FROM `users` 
WHERE `users`.`id` IN (SELECT `user_friends`.`user_id` FROM `user_friends` JOIN `users` AS `t0` ON `user_friends`.`friend_id` = `t0`.`id` WHERE `t0`
.`id` IN (SELECT `admin_id` FROM `groups` WHERE `admin_id` IS NOT NULL))) args=[]
[Pet(id=1, name=Pedro) Pet(id=2, name=Xabi)]
 

查询计划

EXPLAIN SELECT
    DISTINCT `pets`.`id`,
    `pets`.`name`
FROM
    `pets`
WHERE
    `pets`.`owner_id` IN (
        SELECT
            `id`
        FROM
            `users`
        WHERE
            `users`.`id` IN (
                SELECT
                    `user_friends`.`user_id`
                FROM
                    `user_friends`
                    JOIN `users` AS `t0` ON `user_friends`.`friend_id` = `t0`.`id`
                WHERE
                    `t0`.`id` IN (
                        SELECT
                            `admin_id`
                        FROM
                            `groups`
                        WHERE
                            `admin_id` IS NOT NULL
                    )
            )
    )
 
 

效果

参考资料

https://entgo.io/docs/traversals/
https://github.com/rongfengliang/ent-pet-user-group

ent 基本使用十五 一个图遍历的例子的更多相关文章

  1. PMP十五至尊图(第六版)

    PMP(Project Management Professinoal)项目经理专业资格认证,由美国项目管理学会PMI(Project Management Institute)发起并组织的一种资格认 ...

  2. 第七十五课 图的遍历(DFS)

    添加DFS函数: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" #include "SharedPointer. ...

  3. 【PMP】十五至尊图

    以上是PMP的10大知识领域与5个过程组,在PMP考试中属于必须记忆的知识,该知识来源于PMBOK 第6版 附件为每日练习记忆模板,可以更好的记忆上图 点击下载附件

  4. ExpandoObject与DynamicObject的使用 RabbitMQ与.net core(一)安装 RabbitMQ与.net core(二)Producer与Exchange ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler) .NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了

    ExpandoObject与DynamicObject的使用   using ImpromptuInterface; using System; using System.Dynamic; names ...

  5. 通过Dapr实现一个简单的基于.net的微服务电商系统(十五)——集中式接口文档实现

    之前有小伙伴在评论区留言说如何集成swagger,最开始没有想透给了对方一个似是而非的回答.实际上后来下来想了一下,用.NET5 提供的Source Generator其实可以很方便的实现接口集成.今 ...

  6. 第三十五个知识点:给针对ECDLP问题的Pollard rho,Pollard "Kangaroo",parallel Pollard rho攻击的一个粗略的描述

    第三十五个知识点:给针对ECDLP问题的Pollard rho,Pollard "Kangaroo",parallel Pollard rho攻击的一个粗略的描述 我们的目标是对任 ...

  7. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...

  8. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  9. 如鹏网学习笔记(十五)ASP.NET MVC核心基础笔记

    一.ASP.Net MVC简介 1,什么是ASP.NET MVC? HttpHandler是ASP.net的底层机制,如果直接使用HttpHandler进行开发难度比较大.工作量大.因此提供了ASP. ...

随机推荐

  1. 【题解】Luogu P4284 [SHOI2014]概率充电器

    原题传送门 我们知道,每个电器充电对充电电器数的贡献都是相等的1,所以若第\(i\)个电器有\(p_i\)的概率充电时 \[E=\sum_{i=1}^np_i\] 我们考虑如何求\(p_i\),根据树 ...

  2. Java基础扫盲系列(二)—— Java中BigDecimal和浮点类型

    一直以来我几乎未使用过BigDecimal类型,只有在DB中涉及到金额字段时听说要用Decimal类型,但是今天再项目代码中看到使用BigDecimal表示贷款金额. 本篇文章不是介绍BigDecim ...

  3. javascript 对象之hasOwnProperty()方法

    hasOwnProperty()方法是判断某一个对象是否有你给出的属性名称,需要注意的是该方法无法检测该对象原型连中是否具有该属性 具体格式下: var person = {"name&qu ...

  4. 18 java I/O 系统

    流的类继承结构 我们首先看看流的类继承结构,有助于理解下个标签的内容 InputStream OutputStream Reader Writer File类 File类技能表示一个特定文件的名称,又 ...

  5. JavaWeb第二天--CSS

    CSS CSS简述 CSS是什么?有什么作用? CSS(Cascading Style Sheets):层叠样式表. CSS通常称为CSS样式或层叠样式表.主要用于设置HTML页面中的文本内容(字体. ...

  6. vue条形码生成插件vue-barcode

    更详细的请查阅官方文档 https://github.com/lindell/vue-barcode vue-barcode是JsBarcode的一个简单包装.所以在使用时的配置属性需要在JsBarc ...

  7. 详解js中的this指向

    this指向问题是个老生常谈的问题了,现在我给大家一个例子 var obj={ bar:'Cynthia' , foo:function(){ console.log(this.bar,"w ...

  8. Java 之 List 接口

    一.List 接口介绍 java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支,习惯性地会将实现了 List 接口的对象称为 List 集合. 在 List 集 ...

  9. JCEF-tab形式展示浏览器

    当我们点击target值为_blank的链接时,JCEF默认以弹出窗口的形式打开新页面,要实现tab栏形式,可参考以下步骤 1.创建一个实现CefLifeSpanHandlerAdapter的类,重写 ...

  10. Python 常用语句

    条件语句 a=input("请输入数字a的值:\n") a=int(a) #从控制台接收到的都是字符串类型,需要转换 if a==0: #也可以写成if(a==0): print( ...