All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件数据库ORMORM高级特性ORM高级特性-SQL捕获.md

:::warning 需要注意,框架SQL捕获的原理是,任何SQL操作生成的 SQL语句模板 加上 SQL执行参数,在被提交给底层数据库引擎前会被框架拦截,并通过框架组件自动格式化生成可供人工阅读的字符串, 仅供参考和调试,并不是完整提交给底层数据库引擎的SQL语句。捕获的SQL语句和ORM组件开启调试模式后输出的SQL语句是相同的,它们都由相同组件生成。 :::

CatchSQL

我们可以使用 gdb.CatchSQL 方法来捕获指定范围内执行的 SQL 列表。该方法的定义如下:

// CatchSQL catches and returns all sql statements that are EXECUTED in given closure function.
// Be caution that, all the following sql statements should use the context object passing by function `f`.
func CatchSQL(ctx context.Context, f func(ctx context.Context) error) (sqlArray []string, err error)

可以看到,该方法通过一个闭包函数来执行 SQL 语句,在闭包函数执行的所有 SQL 操作都将被记录并作为 []string 类型。这里需要注意的是,闭包中执行的 SQL 操作都应当传递 ctx 上下文对象,否则 SQL 操作对应的语句无法记录。使用示例:

user.sql

CREATE TABLE `user` (
    `id`          int(10) unsigned NOT NULL AUTO_INCREMENT,
    `passport`    varchar(45) NULL,
    `password`    char(32) NULL,
    `nickname`    varchar(45) NULL,
    `create_time` timestamp(6) NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

main.go

package main

import (
    "context"

    _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
    "github.com/gogf/gf/v2/database/gdb"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/os/gtime"
)

type User struct {
    Id         int
    Passport   string
    Password   string
    Nickname   string
    CreateTime *gtime.Time
}

func initUser(ctx context.Context) error {
    _, err := g.Model("user").Ctx(ctx).Data(User{
        Id:       1,
        Passport: "john",
        Password: "12345678",
        Nickname: "John",
    }).Insert()
    return err
}

func main() {
    var ctx = gctx.New()
    sqlArray, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
        return initUser(ctx)
    })
    if err != nil {
        panic(err)
    }
    g.Dump(sqlArray)
}

执行后,终端输出:

[
    "SHOW FULL COLUMNS FROM `user`",
    "INSERT INTO `user`(`id`,`passport`,`password`,`nickname`,`created_at`,`updated_at`) VALUES(1,'john','12345678','John','2023-12-19 21:43:57','2023-12-19 21:43:57') ",
]

ToSQL

我们可以通过 gdb.ToSQL 来将给定的 SQL 操作转换为 SQL 语句,并不真正提交执行。该方法的定义如下:

// ToSQL formats and returns the last one of sql statements in given closure function
// WITHOUT TRULY EXECUTING IT.
// Be caution that, all the following sql statements should use the context object passing by function `f`.
func ToSQL(ctx context.Context, f func(ctx context.Context) error) (sql string, err error)

可以看到,该方法通过一个闭包函数来预估 SQL 语句,在闭包函数中的所有 SQL 操作都将被预估,但只会返回最后一条 SQL 语句作为 string 类型返回。这里需要注意的是,闭包中的 SQL 操作都应当传递 ctx 上下文对象,否则 SQL 操作对应的语句无法记录。使用示例:

user.sql

CREATE TABLE `user` (
    `id`          int(10) unsigned NOT NULL AUTO_INCREMENT,
    `passport`    varchar(45) NULL,
    `password`    char(32) NULL,
    `nickname`    varchar(45) NULL,
    `create_time` timestamp(6) NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

main.go

package main

import (
    "context"

    _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
    "github.com/gogf/gf/v2/database/gdb"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/os/gtime"
)

type User struct {
    Id         int
    Passport   string
    Password   string
    Nickname   string
    CreateTime *gtime.Time
}

func initUser(ctx context.Context) error {
    _, err := g.Model("user").Ctx(ctx).Data(User{
        Id:       1,
        Passport: "john",
        Password: "12345678",
        Nickname: "John",
    }).Insert()
    return err
}

func main() {
    var ctx = gctx.New()
    sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
        return initUser(ctx)
    })
    if err != nil {
        panic(err)
    }
    g.Dump(sql)
}

执行后,终端输出:

"INSERT INTO `user`(`id`,`passport`,`password`,`nickname`,`created_at`,`updated_at`) VALUES(1,'john','12345678','John','2023-12-19 21:49:21','2023-12-19 21:49:21') "

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.