Coder Social home page Coder Social logo

sql-convertor's Introduction

sql-convertor

Convert SQL table definition to peewee/django/... model definition

Convert peewee model definition to SQL

Convert django model definition to peewee

convertor

install

pip3 install sql-convertor 

usage

$ convertor --help
$ ...

example

$ echo "CREATE TABLE \`t_record\` ( \
\`c_id\` INT(64) NOT NULL AUTO_INCREMENT COMMENT '自增主键', \
PRIMARY KEY (\`c_id\`), \
KEY \`ix_company\` (\`c_company_id\`) USING BTREE \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='记录表';" > 1.sql
# only support create table definition
# 只支持建表语句
# convertor 是程序入口名称
# sql 是数据来源格式
# peewee 是输出代码格式
# 1.sql 是来源文件
# 用法就是 convertor 源格式 目标格式 来源文件/代码
$ convertor sql peewee 1.sql

The output

class Record(BaseModel):
    """本段代码由程序从SQL建表语句自动生成, 需要帮助请联系 [email protected]"""
    id = peewee.IntegerField(
        verbose_name="自增主键",
        null=False,
        primary_key=True,
        column_name="c_id")

    class Meta:
        table_name = "t_record"
        database = db

    def to_dict(self):
        return {
            "id": self.id
        }
$ convertor sql peewee 1.sql out.py
$ cat out.py
# output is the same as above
$ convertor sql peewee "CREATE TABLE \`t_record\` ( \
    \`c_id\` INT(64) NOT NULL AUTO_INCREMENT COMMENT '自增主键', \
    PRIMARY KEY (\`c_id\`), \
    KEY \`ix_company\` (\`c_company_id\`) USING BTREE \
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='记录表';"
# output is the same as above

unittest

sh unittest.sh

sql-convertor's People

Contributors

zpoint avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.