dbschema

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package dbschema 连接数据库,内省表结构,并将其转换为 parser.ParseResult, 供 generator 直接生成完整的 RESTful 微服务代码。

支持驱动:mysql / postgres / sqlite / sqlserver

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SnakeToCamel

func SnakeToCamel(s string) string

SnakeToCamel 将 snake_case 转换为 CamelCase(首字母大写,导出供外部使用)

func ToModelParseResult added in v1.3.0

func ToModelParseResult(schemas []*TableSchema, pkgName string) *parser.ParseResult

ToModelParseResult converts table schemas into parser model metadata only. It exists as a compatibility bridge while generator/model templates still consume parser.Model during the IR migration.

func ToParseResult

func ToParseResult(schemas []*TableSchema, serviceName, pkgName string) *parser.ParseResult

ToParseResult converts table schemas into a legacy parser.ParseResult with CRUD service metadata included. Prefer IR + ToModelParseResult for the main DB generation path; this helper remains for compatibility and tests.

func WriteIDL

func WriteIDL(schemas []*TableSchema, pkgName, outDir string) (string, error)

WriteIDL 将数据库 schema 写成 idl.go 文件(包含 GORM model + DTO + Service 接口), 输出到 outDir/idl.go,供后续 parser.ParseFull 解析再生成完整项目。 返回生成的 idl.go 绝对路径。

Types

type ColumnInfo

type ColumnInfo struct {
	Name       string // 列名
	DBType     string // 数据库原始类型(varchar(64)、int、bigint unsigned …)
	IsNullable bool   // 是否允许 NULL
	IsPrimary  bool   // 是否主键
	IsAutoIncr bool   // 是否自增
	IsUnique   bool   // 是否唯一索引
	Comment    string // 列注释(MySQL/Postgres 支持)
	Default    string // 默认值(可为空)
}

ColumnInfo 单列元数据

type Introspector

type Introspector interface {
	// Tables 返回数据库中所有(或指定)表的 schema
	Tables(db *sql.DB, dbName string, tables []string) ([]*TableSchema, error)
}

Introspector 数据库内省器接口

func NewIntrospector

func NewIntrospector(driver string) (Introspector, error)

NewIntrospector 根据驱动名返回对应的内省器

type TableSchema

type TableSchema struct {
	TableName string
	Columns   []ColumnInfo
}

TableSchema 单张表的列信息

func ModelToSchema

func ModelToSchema(m *parser.Model) *TableSchema

ModelToSchema converts a parser.Model (read from an existing idl.go) back into a TableSchema so it can be passed through WriteIDL again during incremental generation. Column metadata is reconstructed from the model fields' gorm tags.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL