Documentation
¶
Overview ¶
Package dbschema 连接数据库,内省表结构,并将其转换为 parser.ParseResult, 供 generator 直接生成完整的 RESTful 微服务代码。
支持驱动:mysql / postgres / sqlite / sqlserver
Index ¶
- func SnakeToCamel(s string) string
- func ToModelParseResult(schemas []*TableSchema, pkgName string) *parser.ParseResult
- func ToParseResult(schemas []*TableSchema, serviceName, pkgName string) *parser.ParseResult
- func WriteIDL(schemas []*TableSchema, pkgName, outDir string) (string, error)
- type ColumnInfo
- type Introspector
- type TableSchema
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SnakeToCamel ¶
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.
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.