【店小二】公告:
sql转ent
| | |
功能介绍
1.支持多种数据库的建表 sql转ent, 其中包含(pg、mysql)数据库; 建表语句, 建表语句, 建表语句(重要的说3遍)
2.在转换 sql 的时候, 需要确认下是否需要切换处理类型, 暂时支持3种处理方式(mysql, postgresql), 默认: mysql.
3.支持自定义过滤不需要的字段, 多个字段通过 , 隔开 说明: 不能在 sql 中包含注释内容(即:-- xxx
或# xxx
); 由于此工具是通过 ; 来分割语句处理.
示例
mysql 建表语句
CREATE TABLE if not exists user (
id int NOT NULL AUTO_INCREMENT,
name varchar(10) NOT NULL COMMENT '姓名',
gender tinyint NOT NULL DEFAULT 0 COMMENT '性别 0-未知 1-男 2-女',
age int NOT NULL COMMENT '年龄',
created_date datetime DEFAULT CURRENT_TIMESTAMP,
updated_date datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
处理后
// Code generated by https://gotool.top
package model
import (
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
)
type User struct {
ent.Schema
}
func (u *User) Fields() []ent.Field {
return []ent.Field{
field.String("id").SchemaType(map[string]string{dialect.MySQL: "character"}).Default(sys_guid).Nillable(),
field.String("name").SchemaType(map[string]string{dialect.MySQL: "character"}).Nillable().Comment("姓名"),
field.Int32("gender").SchemaType(map[string]string{dialect.MySQL: "tinyint"}).Nillable().Comment("性别 0-未知 1-男 2-女"),
field.Int32("age").SchemaType(map[string]string{dialect.MySQL: "tinyint"}).Nillable().Comment("年龄"),
field.Time("created_date").SchemaType(map[string]string{dialect.MySQL: "timestamp"}).Optional().Default(now()).Nillable(),
field.Time("updated_date").SchemaType(map[string]string{dialect.MySQL: "timestamp"}).Optional().Default(now()).Nillable()
}
}
func (u *User) Edges() []ent.Edge {
return nil
}
func (u *User) Annotations() []schema.Annotation {
return []schema.Annotation{entsql.Annotation{Table: "user"}}
}
pgsql 建表语句
CREATE TABLE if not exists user (
id character varying(32) NOT NULL DEFAULT sys_guid(),
name character varying(10) NOT NULL,
gender tinyint NOT NULL,
age tinyint NOT NULL,
created_date timestamp without time zone DEFAULT now(),
updated_date timestamp without time zone DEFAULT now(),
CONSTRAINT user_pkey PRIMARY KEY (id)
);
COMMENT ON TABLE user IS '用户表';
COMMENT ON COLUMN user.id IS '主键';
COMMENT ON COLUMN user.name IS '姓名';
COMMENT ON COLUMN user.gender IS '性别 0-未知 1-男 2-女';
COMMENT ON COLUMN user.age IS '年龄';
COMMENT ON COLUMN user.created_date IS '创建时间';
COMMENT ON COLUMN user.updated_date IS '更新时间';
处理后
// Code generated by https://gotool.top
package model
import (
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
)
type User struct {
ent.Schema
}
func (u *User) Fields() []ent.Field {
return []ent.Field{
field.String("id").SchemaType(map[string]string{dialect.Postgres: "character"}).Default(sys_guid).Nillable(),
field.String("name").SchemaType(map[string]string{dialect.Postgres: "character"}).Nillable(),
field.Int32("gender").SchemaType(map[string]string{dialect.Postgres: "tinyint"}).Nillable(),
field.Int32("age").SchemaType(map[string]string{dialect.Postgres: "tinyint"}).Nillable(),
field.Time("created_date").SchemaType(map[string]string{dialect.Postgres: "timestamp"}).Optional().Default(now()).Nillable(),
field.Time("updated_date").SchemaType(map[string]string{dialect.Postgres: "timestamp"}).Optional().Default(now()).Nillable()
}
}
func (u *User) Edges() []ent.Edge {
return nil
}
func (u *User) Annotations() []schema.Annotation {
return []schema.Annotation{entsql.Annotation{Table: "user"}}
}