GO Online Toolset
home
search
To Boss

sql to SQLAlchemy
62  |   |   |  3

feature introduction

  1. Table creation sql to gorm supporting multiple databases, including (pg, oracle, mysql) and other databases; table-building statements, table-building statements, table-building statements (important statements are said 3 times)
  2. When converting sql, you need to confirm whether you need to switch the processing type. Three processing methods are temporarily supported (normal, postgresql, oracle). Default: normal.
  3. Support custom filtering of unwanted fields. Multiple fields are separated by ,.
    Note: cannot include comments in sql (that is, --xxx or # xxx), because this tool divides statement processing through ;. Therefore, multiple table creation statements can be processed at the same time in normal, postgresql and oracle only support a single table creation statement because they need to parse comments.

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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Column, DateTime, Integer, SmallInteger, String, func
import datetime

Base = declarative_base()

class UserModel(Base): 
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, nullable=False)
    name: Mapped[str] = mapped_column(String(10), nullable=False, comment="Name")
    gender: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="Gender 0-unknown 1-male 2-female")
    age: Mapped[int] = mapped_column(Integer, nullable=False, comment="Age")
    created_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())
    updated_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())

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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Column, DateTime, Integer, SmallInteger, String, func
import datetime

Base = declarative_base()

class UserModel(Base): 
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, nullable=False)
    name: Mapped[str] = mapped_column(String(10), nullable=False, comment="Name")
    gender: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="Gender 0-unknown 1-male 2-female")
    age: Mapped[int] = mapped_column(Integer, nullable=False, comment="Age")
    created_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())
    updated_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())

oracle 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 date default sysdate
    updated_date date default sysdate
    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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Column, DateTime, Integer, SmallInteger, String, func
import datetime

Base = declarative_base()

class UserModel(Base): 
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, nullable=False)
    name: Mapped[str] = mapped_column(String(10), nullable=False, comment="Name")
    gender: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="Gender 0-unknown 1-male 2-female")
    age: Mapped[int] = mapped_column(Integer, nullable=False, comment="Age")
    created_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())
    updated_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())