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
0 commit comments