新增sqlacodegen、sqlachemy及demo
This commit is contained in:
parent
5f03a99bb3
commit
5f3d366e12
|
@ -24,6 +24,8 @@
|
|||
* kazoo:用于操作zookeeper,https://github.com/python-zk/kazoo
|
||||
* websockets:用于websocket请求,https://github.com/aaugustin/websockets
|
||||
* Js2Py:用于执行js代码,https://github.com/PiotrDabkowski/Js2Py
|
||||
* sqlacodegen:用于根据数据库表结构生成python对象
|
||||
* SQLAlchemy:SQL工具包及对象关系映射(ORM)工具
|
||||
* 当前仅支持python3.6.8
|
||||
|
||||
# [使用]()
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:50:18.677Z+08:00
|
||||
# @last-modified 2022-07-25T19:58:15.291Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:50:18.680Z+08:00
|
||||
# @last-modified 2022-07-25T19:58:20.268Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#
|
||||
# demoProject_sessions.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:32:07.806Z+08:00
|
||||
# @last-modified 2022-07-25T19:58:23.123Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
||||
|
||||
from common.sqlalchemy_tools.sqlalchemy_sqlite_tool import SQLAlchemy_Sqlite_Tool
|
||||
|
||||
class DemoProject_Sessions(object):
|
||||
__instance = None
|
||||
__inited = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls.__instance is None:
|
||||
cls.__instance = object.__new__(cls)
|
||||
return cls.__instance
|
||||
|
||||
def __init__(self,):
|
||||
if self.__inited is None:
|
||||
self.db_demoProject_session=SQLAlchemy_Sqlite_Tool('models/demoProject/demoProject.db').get_session()
|
||||
|
||||
self.__inited = True
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:48:34.753Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:30.557Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
#
|
||||
# sqlacodegen_too.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-21T17:00:26.678Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:33.428Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
class Sqlacodegen_Mysql_Tool:
|
||||
def __init__(self,host:str=None,port:str=None,username:str=None,password:str=None,db:str=None,driver_type='pymysql') -> None:
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
host (str, optional): _description_. Defaults to None.
|
||||
port (str, optional): _description_. Defaults to None.
|
||||
username (str, optional): _description_. Defaults to None.
|
||||
password (str, optional): _description_. Defaults to None.
|
||||
db (str, optional): _description_. Defaults to None.
|
||||
driver_type (str, optional): pymysql:pymysql,mysqldb:mysqlclient. Defaults to 'pymysql'.
|
||||
"""
|
||||
self.url='mysql+%s://%s:%s@%s:%s/%s'%(driver_type,username,password,host,str(port),db)
|
||||
|
||||
def generate_table_model(self,table_name:str,output_file_path:str):
|
||||
command='sqlacodegen %s --tables %s --outfile %s'%(self.url,table_name,output_file_path)
|
||||
output = subprocess.check_output(command, shell=True, timeout=3600)
|
||||
if 'Windows' == platform.system():
|
||||
output=output.decode('cp936')
|
||||
else:
|
||||
output=output.decode('utf-8')
|
||||
return output
|
||||
|
||||
def generate_db_models(self,output_file_path:str):
|
||||
command='sqlacodegen %s --outfile %s'%(self.url,output_file_path)
|
||||
output = subprocess.check_output(command, shell=True, timeout=3600)
|
||||
if 'Windows' == platform.system():
|
||||
output=output.decode('cp936')
|
||||
else:
|
||||
output=output.decode('utf-8')
|
||||
return output
|
|
@ -0,0 +1,37 @@
|
|||
#
|
||||
# sqlacodegen_sqlite_tool.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:23:33.845Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:36.776Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
class Sqlacodegen_Sqlite_Tool:
|
||||
def __init__(self,file_path) -> None:
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
file_path (_type_): _description_
|
||||
"""
|
||||
self.url='sqlite:///%s'%(file_path)
|
||||
|
||||
def generate_table_model(self,table_name:str,output_file_path:str):
|
||||
command='sqlacodegen %s --tables %s --outfile %s'%(self.url,table_name,output_file_path)
|
||||
output = subprocess.check_output(command, shell=True, timeout=3600)
|
||||
if 'Windows' == platform.system():
|
||||
output=output.decode('cp936')
|
||||
else:
|
||||
output=output.decode('utf-8')
|
||||
return output
|
||||
|
||||
def generate_db_models(self,output_file_path:str):
|
||||
command='sqlacodegen %s --outfile %s'%(self.url,output_file_path)
|
||||
output = subprocess.check_output(command, shell=True, timeout=3600)
|
||||
if 'Windows' == platform.system():
|
||||
output=output.decode('cp936')
|
||||
else:
|
||||
output=output.decode('utf-8')
|
||||
return output
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:57:53.244Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:58.145Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#
|
||||
# sqlalchemy_mysql_tool.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-21T18:07:28.086Z+08:00
|
||||
# @last-modified 2022-07-25T19:58:02.887Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import scoped_session
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
class SQLAlchemy_Mysql_Tool:
|
||||
def __init__(self,host:str=None,port:str=None,username:str=None,password:str=None,db:str=None,
|
||||
driver_type='pymysql',encoding='utf8',echo=False) -> None:
|
||||
self.url='mysql+%s://%s:%s@%s:%s/%s'%(driver_type,username,password,host,str(port),db)
|
||||
self.encoding=encoding
|
||||
self.echo=echo
|
||||
|
||||
def get_session(self):
|
||||
engine=create_engine(url=self.url,encoding=self.encoding,echo=self.echo)
|
||||
# 线程安全
|
||||
return scoped_session(sessionmaker(bind=engine))
|
|
@ -0,0 +1,22 @@
|
|||
#
|
||||
# sqlalchemy_sqlite_tool.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:11:48.078Z+08:00
|
||||
# @last-modified 2022-07-25T19:58:05.872Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import scoped_session
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
class SQLAlchemy_Sqlite_Tool:
|
||||
def __init__(self,file_path,encoding='utf8',echo=False) -> None:
|
||||
self.url='sqlite:///%s'%(file_path)
|
||||
self.encoding=encoding
|
||||
self.echo=echo
|
||||
|
||||
def get_session(self):
|
||||
engine=create_engine(url=self.url,encoding=self.encoding,echo=self.echo)
|
||||
# 线程安全
|
||||
return scoped_session(sessionmaker(bind=engine))
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:50:47.075Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:13.013Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:50:47.078Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:21.838Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
#
|
||||
# user.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:40:40.521Z+08:00
|
||||
# @last-modified 2022-07-25T19:57:16.904Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
||||
from base.sqliteOpt.demoProject.demoProject_sessions import DemoProject_Sessions
|
||||
from models.demoProject.user import User as User_Model
|
||||
|
||||
class User:
|
||||
def __init__(self) -> None:
|
||||
self.db_demoProject_session=DemoProject_Sessions().db_demoProject_session
|
||||
|
||||
def get_user_by_name(self,user_name):
|
||||
users=self.db_demoProject_session.query(User_Model).filter(User_Model.name==user_name).all()
|
||||
return users
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:51:16.164Z+08:00
|
||||
# @last-modified 2022-07-25T19:56:58.880Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#
|
||||
# __init__.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:51:16.169Z+08:00
|
||||
# @last-modified 2022-07-25T19:56:43.205Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
Binary file not shown.
|
@ -0,0 +1,24 @@
|
|||
#
|
||||
# user.py
|
||||
# @author yanchunhuo
|
||||
# @description
|
||||
# @created 2022-07-25T19:51:16.167Z+08:00
|
||||
# @last-modified 2022-07-25T19:56:52.705Z+08:00
|
||||
# github https://github.com/yanchunhuo
|
||||
|
||||
|
||||
from sqlalchemy import Column, Integer, Text
|
||||
from sqlalchemy.orm import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'user'
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(Text, nullable=False)
|
||||
age = Column(Integer)
|
||||
sex = Column(Integer)
|
||||
phone = Column(Text)
|
||||
address = Column(Text)
|
|
@ -23,3 +23,5 @@ kazoo==2.8.0
|
|||
websockets==9.1
|
||||
Js2Py==0.71
|
||||
pytest-collect-formatter2==0.1.3
|
||||
sqlacodegen==3.0.0b2
|
||||
SQLAlchemy==1.4.39
|
Loading…
Reference in New Issue