go在線工具集
首頁
搜索
致boss

sql轉sqlalchemy
89  |   |   |  3

功能介紹

1.支持多種資料庫的建表 sql轉gorm, 其中包含(pg、oracle、mysql)等資料庫; 建表語句, 建表語句, 建表語句(重要的說3遍)
2.在轉換 sql 的時候, 需要確認下是否需要切換處理類型, 暫時支持3種處理方式(normal, postgresql, oracle), 默認: normal.
3.支持自定義過濾不需要的欄位, 多個欄位通過 , 隔開
說明: 不能在 sql 中包含注釋內容(即: -- xxx# xxx); 由於此工具是通過 ; 來分割語句處理.所以 normal 狀態下可以同時處理多個建表語句; postgresqloracle 由於需要解析注釋, 只支持單個建表語句.


示例

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_TIMESP,
    updated_date datetime DEFAULT CURRENT_TIMESP ON UPDATE CURRENT_TIMESP,
    PRIMARY KEY (id)
);

處理後

# 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="姓名")
    gender: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="性別 0-未知 1-男 2-女")
    age: Mapped[int] = mapped_column(Integer, nullable=False, comment="年齡")
    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 建表語句

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 timesp without time zone DEFAULT now(),
    updated_date timesp 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
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="姓名")
    gender: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="性別 0-未知 1-男 2-女")
    age: Mapped[int] = mapped_column(Integer, nullable=False, comment="年齡")
    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 建表語句

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 '用戶表';
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
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="姓名")
    gender: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="性別 0-未知 1-男 2-女")
    age: Mapped[int] = mapped_column(Integer, nullable=False, comment="年齡")
    created_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())
    updated_date: Mapped[datetime.datetime] = mapped_column(DateTime, server_default=func.now())