Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(gdb): Allow to set table field metadata and allow to generate table fields registration code when generating dao#4460

Merged
hailaz merged 7 commits intogogf:masterfrom
LanceAdd:feature/gdb_table_fields
Oct 15, 2025
Merged

feat(gdb): Allow to set table field metadata and allow to generate table fields registration code when generating dao#4460
hailaz merged 7 commits intogogf:masterfrom
LanceAdd:feature/gdb_table_fields

Conversation

@LanceAdd
Copy link
Member

@LanceAdd LanceAdd commented Oct 10, 2025

gdb在第一次查询时会拉取一次tablefields meta信息,为后续orm的字段过滤和时间特性服务,gen dao时已经获得了table的所有fields meta,提供一个生成table的功能,允许客户自行决定是否生成,是否注入已知表结构缓存到指定db实例。

  1. 能解决部分兼容mysql的二开数据库在获取fields meta时无法和mysql保持一致的问题。
  2. 可以模拟表字段信息而不需要真实的数据库连接,对于已知的表结构,可以直接设置缓存,在无法连接数据库的情况下,仍然可以使用表字段信息,使用gdb构建sql时不需要受限于实际数据库即可使用gdb.ToSQL()方法。
  3. 提升访问速度

生成的示例目录
SCR-20251010-ntne

生成的示例代码

// =================================================================================
// This file is auto-generated by the GoFrame CLI tool. You may modify it as needed.
// =================================================================================

package table

import (
	"context"

	"github.com/gogf/gf/v2/database/gdb"
)

// RolePermissions defines the fields of table "role_permissions" with their properties.
// This map is used internally by GoFrame ORM to understand table structure.
var RolePermissions = map[string]*gdb.TableField{
	"role_id": {
		Index:   0,
		Name:    "role_id",
		Type:    "bigint unsigned",
		Null:    false,
		Key:     "PRI",
		Default: nil,
		Extra:   "",
		Comment: "",
	},
	"permission_id": {
		Index:   1,
		Name:    "permission_id",
		Type:    "bigint unsigned",
		Null:    false,
		Key:     "PRI",
		Default: nil,
		Extra:   "",
		Comment: "",
	},
	"created_at": {
		Index:   2,
		Name:    "created_at",
		Type:    "timestamp",
		Null:    false,
		Key:     "",
		Default: "CURRENT_TIMESTAMP",
		Extra:   "DEFAULT_GENERATED",
		Comment: "",
	},
	"updated_at": {
		Index:   3,
		Name:    "updated_at",
		Type:    "timestamp",
		Null:    false,
		Key:     "",
		Default: "CURRENT_TIMESTAMP",
		Extra:   "DEFAULT_GENERATED on update CURRENT_TIMESTAMP",
		Comment: "",
	},
	"deleted_at": {
		Index:   4,
		Name:    "deleted_at",
		Type:    "timestamp",
		Null:    true,
		Key:     "",
		Default: nil,
		Extra:   "",
		Comment: "",
	},
}

// SetRolePermissionsTableFields registers the table fields definition to the database instance.
// db: database instance that implements gdb.DB interface.
// schema: optional schema/namespace name, especially for databases that support schemas.
func SetRolePermissionsTableFields(ctx context.Context, db gdb.DB, schema ...string) error {
	return db.GetCore().SetTableFields(ctx, "role_permissions", RolePermissions, schema...)
}

- 添加 TablePath 和 TplDaoTablePath 配置选项- 新增 GenTable 命令行参数控制是否生成表文件
- 实现 generateTable 函数用于生成表结构代码
- 扩展模板变量支持 TplTableFields
- 更新单元测试以包含新的配置选项
- 在 Core 中增加 SetTableFields 方法用于设置表字段缓存
- 在生成 DAO 文件时,使用 strconv.Quote 替代手动拼接字符串- 确保字段名、类型、键、额外信息和注释正确转义- 提高代码可读性和安全性,避免潜在的语法错误
@hailaz hailaz requested a review from Copilot October 15, 2025 07:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functionality to the GoFrame CLI tool to generate table field metadata definitions and allow setting table field metadata in the database core. This enables pre-caching of table structure information to improve compatibility with MySQL-compatible databases and provide performance benefits.

Key changes:

  • Added SetTableFields method to database core for setting table field metadata
  • Added new table generation functionality to the DAO generator with configurable options
  • Created template for generating table field definition files with registration functions

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
database/gdb/gdb_core.go Adds SetTableFields method for caching table field metadata
cmd/gf/internal/consts/consts_gen_dao_template_table.go Defines template for generating table field definition files
cmd/gf/internal/cmd/gendao/gendao_tag.go Adds configuration constants for table generation feature
cmd/gf/internal/cmd/gendao/gendao_table.go Implements table generation logic and field definition formatting
cmd/gf/internal/cmd/gendao/gendao.go Integrates table generation into main DAO generation flow
cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go Updates test cases to include new GenTable configuration
cmd/gf/internal/cmd/cmd_z_unit_gen_dao_issue_test.go Updates additional test cases for GenTable configuration

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

- 将字段排序注释从中文"升序"改为英文"asc"- 移除模板文件中未使用的"g"包导入- 统一代码风格以提高可读性
@hailaz hailaz merged commit 1682cc9 into gogf:master Oct 15, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants