以下是来自官方的一个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. Win10 UEFI 系统安装教程

    1:首先我们需要先拿一个U盘,制作一个带UEFI PE.(网上的大白菜以及老毛桃都可以,我用的是从网上找的WIN10 PE.WIN10 PE的好处是集成了NVME驱动,可以认得到SM951 NVME版 ...

  2. MIME类型对应表:

    MIME类型对应表: 常用MIME类型: 扩展名 MIME类型 .iso ISO File .rar application/x-rar-compressed .zip application/zip ...

  3. CSP2019退役记

    写在前面 all last,我又失败了,我退役了 回忆我这个菜鸡OI生涯,有看机房神仙切题的乐趣,也有自己考场爆炸的辛酸 NOIP2017,我第一次参赛,我pj205二等打铁 NOIP2018,我第二 ...

  4. 前端开发常用 JS 方法

    1,获取文件本地url,在上传之前预览 /** * 获取图片嗯滴url,在上传之前预览 * @param file 选择的图片文件 * @returns {*} url */ getFileLocat ...

  5. Python基础之time、os模块

    1.时间模块 1)模块 python安装好之后,会有一些默认模块,我们称之为标准库,标准库中的模块python自带,无需安装. 除了标准库,还有一个第三方库,可以通过pip来安装,不同的库有不同的功能 ...

  6. hadoop中HDFS的NameNode原理

    1. hadoop中HDFS的NameNode原理 1.1. 组成 包括HDFS(分布式文件系统),YARN(分布式资源调度系统),MapReduce(分布式计算系统),等等. 1.2. HDFS架构 ...

  7. JavaScript设计模式与开发实践随笔(三)

    封装 1. 封装数据 a)      只能依赖变量的作用域来实现封装特性,es6中可以通过symbol创建私有属性 var myObject = (function(){ var __name = ' ...

  8. Android中自定义水球

    如图所示: 自定义属性: 在values下创建attrs.xml 文件 <?xml version="1.0" encoding="utf-8"?> ...

  9. k8s的node节点无法调度的问题

    1.现象,创建deployment时 2.查看污点 [fedora@k8s-cluster--ycmwlao4q5wz-master- ~]$ kubectl describe node k8s-cl ...

  10. 【Linux】在linux上java工具jps jstat jinfo等命令找不到怎么办

    一.yum安装方式 1)搜索openjdk-devel相关的安装包 yum search java|grep jdk 2)安装对应的版本 yum install -y java-1.8.0-openj ...