Skip to content

Commit 5f3d366

Browse files
author
yanchunhuo
committed
新增sqlacodegen、sqlachemy及demo
1 parent 5f03a99 commit 5f3d366

18 files changed

+258
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* kazoo:用于操作zookeeper,https://github.com/python-zk/kazoo
2525
* websockets:用于websocket请求,https://github.com/aaugustin/websockets
2626
* Js2Py:用于执行js代码,https://github.com/PiotrDabkowski/Js2Py
27+
* sqlacodegen:用于根据数据库表结构生成python对象
28+
* SQLAlchemy:SQL工具包及对象关系映射(ORM)工具
2729
* 当前仅支持python3.6.8
2830

2931
# [使用]()

base/sqliteOpt/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:50:18.677Z+08:00
6+
# @last-modified 2022-07-25T19:58:15.291Z+08:00
7+
# github https://github.com/yanchunhuo
8+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:50:18.680Z+08:00
6+
# @last-modified 2022-07-25T19:58:20.268Z+08:00
7+
# github https://github.com/yanchunhuo
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# demoProject_sessions.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:32:07.806Z+08:00
6+
# @last-modified 2022-07-25T19:58:23.123Z+08:00
7+
# github https://github.com/yanchunhuo
8+
9+
10+
from common.sqlalchemy_tools.sqlalchemy_sqlite_tool import SQLAlchemy_Sqlite_Tool
11+
12+
class DemoProject_Sessions(object):
13+
__instance = None
14+
__inited = None
15+
16+
def __new__(cls, *args, **kwargs):
17+
if cls.__instance is None:
18+
cls.__instance = object.__new__(cls)
19+
return cls.__instance
20+
21+
def __init__(self,):
22+
if self.__inited is None:
23+
self.db_demoProject_session=SQLAlchemy_Sqlite_Tool('models/demoProject/demoProject.db').get_session()
24+
25+
self.__inited = True

common/sqlacodegen_tools/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:48:34.753Z+08:00
6+
# @last-modified 2022-07-25T19:57:30.557Z+08:00
7+
# github https://github.com/yanchunhuo
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# sqlacodegen_too.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-21T17:00:26.678Z+08:00
6+
# @last-modified 2022-07-25T19:57:33.428Z+08:00
7+
# github https://github.com/yanchunhuo
8+
import platform
9+
import subprocess
10+
11+
class Sqlacodegen_Mysql_Tool:
12+
def __init__(self,host:str=None,port:str=None,username:str=None,password:str=None,db:str=None,driver_type='pymysql') -> None:
13+
"""_summary_
14+
15+
Args:
16+
host (str, optional): _description_. Defaults to None.
17+
port (str, optional): _description_. Defaults to None.
18+
username (str, optional): _description_. Defaults to None.
19+
password (str, optional): _description_. Defaults to None.
20+
db (str, optional): _description_. Defaults to None.
21+
driver_type (str, optional): pymysql:pymysql,mysqldb:mysqlclient. Defaults to 'pymysql'.
22+
"""
23+
self.url='mysql+%s://%s:%s@%s:%s/%s'%(driver_type,username,password,host,str(port),db)
24+
25+
def generate_table_model(self,table_name:str,output_file_path:str):
26+
command='sqlacodegen %s --tables %s --outfile %s'%(self.url,table_name,output_file_path)
27+
output = subprocess.check_output(command, shell=True, timeout=3600)
28+
if 'Windows' == platform.system():
29+
output=output.decode('cp936')
30+
else:
31+
output=output.decode('utf-8')
32+
return output
33+
34+
def generate_db_models(self,output_file_path:str):
35+
command='sqlacodegen %s --outfile %s'%(self.url,output_file_path)
36+
output = subprocess.check_output(command, shell=True, timeout=3600)
37+
if 'Windows' == platform.system():
38+
output=output.decode('cp936')
39+
else:
40+
output=output.decode('utf-8')
41+
return output
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# sqlacodegen_sqlite_tool.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:23:33.845Z+08:00
6+
# @last-modified 2022-07-25T19:57:36.776Z+08:00
7+
# github https://github.com/yanchunhuo
8+
9+
import platform
10+
import subprocess
11+
12+
class Sqlacodegen_Sqlite_Tool:
13+
def __init__(self,file_path) -> None:
14+
"""_summary_
15+
16+
Args:
17+
file_path (_type_): _description_
18+
"""
19+
self.url='sqlite:///%s'%(file_path)
20+
21+
def generate_table_model(self,table_name:str,output_file_path:str):
22+
command='sqlacodegen %s --tables %s --outfile %s'%(self.url,table_name,output_file_path)
23+
output = subprocess.check_output(command, shell=True, timeout=3600)
24+
if 'Windows' == platform.system():
25+
output=output.decode('cp936')
26+
else:
27+
output=output.decode('utf-8')
28+
return output
29+
30+
def generate_db_models(self,output_file_path:str):
31+
command='sqlacodegen %s --outfile %s'%(self.url,output_file_path)
32+
output = subprocess.check_output(command, shell=True, timeout=3600)
33+
if 'Windows' == platform.system():
34+
output=output.decode('cp936')
35+
else:
36+
output=output.decode('utf-8')
37+
return output

common/sqlalchemy_tools/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:57:53.244Z+08:00
6+
# @last-modified 2022-07-25T19:57:58.145Z+08:00
7+
# github https://github.com/yanchunhuo
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# sqlalchemy_mysql_tool.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-21T18:07:28.086Z+08:00
6+
# @last-modified 2022-07-25T19:58:02.887Z+08:00
7+
# github https://github.com/yanchunhuo
8+
9+
from sqlalchemy import create_engine
10+
from sqlalchemy.orm import scoped_session
11+
from sqlalchemy.orm import sessionmaker
12+
13+
class SQLAlchemy_Mysql_Tool:
14+
def __init__(self,host:str=None,port:str=None,username:str=None,password:str=None,db:str=None,
15+
driver_type='pymysql',encoding='utf8',echo=False) -> None:
16+
self.url='mysql+%s://%s:%s@%s:%s/%s'%(driver_type,username,password,host,str(port),db)
17+
self.encoding=encoding
18+
self.echo=echo
19+
20+
def get_session(self):
21+
engine=create_engine(url=self.url,encoding=self.encoding,echo=self.echo)
22+
# 线程安全
23+
return scoped_session(sessionmaker(bind=engine))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# sqlalchemy_sqlite_tool.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:11:48.078Z+08:00
6+
# @last-modified 2022-07-25T19:58:05.872Z+08:00
7+
# github https://github.com/yanchunhuo
8+
9+
from sqlalchemy import create_engine
10+
from sqlalchemy.orm import scoped_session
11+
from sqlalchemy.orm import sessionmaker
12+
13+
class SQLAlchemy_Sqlite_Tool:
14+
def __init__(self,file_path,encoding='utf8',echo=False) -> None:
15+
self.url='sqlite:///%s'%(file_path)
16+
self.encoding=encoding
17+
self.echo=echo
18+
19+
def get_session(self):
20+
engine=create_engine(url=self.url,encoding=self.encoding,echo=self.echo)
21+
# 线程安全
22+
return scoped_session(sessionmaker(bind=engine))

common_projects/sqliteOpt/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:50:47.075Z+08:00
6+
# @last-modified 2022-07-25T19:57:13.013Z+08:00
7+
# github https://github.com/yanchunhuo
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:50:47.078Z+08:00
6+
# @last-modified 2022-07-25T19:57:21.838Z+08:00
7+
# github https://github.com/yanchunhuo
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# user.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:40:40.521Z+08:00
6+
# @last-modified 2022-07-25T19:57:16.904Z+08:00
7+
# github https://github.com/yanchunhuo
8+
9+
from base.sqliteOpt.demoProject.demoProject_sessions import DemoProject_Sessions
10+
from models.demoProject.user import User as User_Model
11+
12+
class User:
13+
def __init__(self) -> None:
14+
self.db_demoProject_session=DemoProject_Sessions().db_demoProject_session
15+
16+
def get_user_by_name(self,user_name):
17+
users=self.db_demoProject_session.query(User_Model).filter(User_Model.name==user_name).all()
18+
return users

models/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:51:16.164Z+08:00
6+
# @last-modified 2022-07-25T19:56:58.880Z+08:00
7+
# github https://github.com/yanchunhuo
8+

models/demoProject/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# __init__.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:51:16.169Z+08:00
6+
# @last-modified 2022-07-25T19:56:43.205Z+08:00
7+
# github https://github.com/yanchunhuo
8+

models/demoProject/demoProject.db

12 KB
Binary file not shown.

models/demoProject/user.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# user.py
3+
# @author yanchunhuo
4+
# @description
5+
# @created 2022-07-25T19:51:16.167Z+08:00
6+
# @last-modified 2022-07-25T19:56:52.705Z+08:00
7+
# github https://github.com/yanchunhuo
8+
9+
10+
from sqlalchemy import Column, Integer, Text
11+
from sqlalchemy.orm import declarative_base
12+
13+
Base = declarative_base()
14+
15+
16+
class User(Base):
17+
__tablename__ = 'user'
18+
19+
id = Column(Integer, primary_key=True, index=True)
20+
name = Column(Text, nullable=False)
21+
age = Column(Integer)
22+
sex = Column(Integer)
23+
phone = Column(Text)
24+
address = Column(Text)

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ kazoo==2.8.0
2323
websockets==9.1
2424
Js2Py==0.71
2525
pytest-collect-formatter2==0.1.3
26+
sqlacodegen==3.0.0b2
27+
SQLAlchemy==1.4.39

0 commit comments

Comments
 (0)