GO Online Toolset
home
search
feedback
【the waiter】announcement:

Sql to ent
198  |   |   |  4

feature introduction

  1. Support a variety of databases to build tables sql ent, including (pg, mysql) database; ** construct predicative sentence **, ** construct predicative sentence **, ** construct predicative sentence **(important say 3 times)
  2. When converting sql, you need to confirm whether you need to switch the processing type. Three processing methods (mysql, postgresql) are temporarily supported, and the default is mysql.
  3. Support custom filtering of unwanted fields, multiple fields separated by ,
    note: comment content contained in the SQL(i.e., - or # XXX XXX); Since this tool is available through ; to split statement processing.

example

mysql table statement

CREATE TABLE if not exists user (
    id int NOT NULL AUTO_INCREMENT,
    name varchar(10) NOT NULL COMMENT 'name ',
    gender tinyint NOT NULL DEFAULT 0 COMMENT 'Gender 0 -unknown 1 -male 2 -female ',
    age int NOT NULL COMMENT 'age ',
    created_date datetime DEFAULT CURRENT_TIMESTAMP,
    updated_date datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

After treatment

// 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("name"),
        field.Int32("gender").SchemaType(map[string]string{dialectic.mysql: "tinyint"}).Nillable().Comment("Gender 0-unknown 1-male 2-female"),
        field.Int32("age").SchemaType(map[string]string{dialectal.mysql: "tinyint"}).Nillable().Comment("age"),
        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 table statement

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 'user Table'
COMMENT ON COLUMN user.id IS 'primary key'
COMMENT ON COLUMN user.name IS 'name'
COMMENT ON COLUMN user.gender IS 'Sex 0-unknown 1-male 2-female'
COMMENT ON COLUMN user.age IS 'age'
COMMENT ON COLUMN user.created_date IS 'creation time'
COMMENT ON COLUMN user.updated_date IS 'update time'

After treatment

// 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(),
        field.Int32("gender").SchemaType(map[string]string{dialect.MySQL: "tinyint"}).Nillable(),
        field.Int32("age").SchemaType(map[string]string{dialect.MySQL: "tinyint"}).Nillable(),
        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"}}
}