MCPcopy Create free account
hub / github.com/CL-lau/SQL-GPT / SQL_operator

Class SQL_operator

sql/SQLOperator.py:92–258  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90
91
92class SQL_operator(nn.Module):
93 def __init__(self, jdbcList=None, userNames=None, passwords=None):
94 super().__init__()
95
96 self.jdbcList = jdbcList # jdbcList的每一项的格式[host/db_name]
97 if self.jdbcList is None or len(self.jdbcList) <= 0:
98 self.jdbcList = []
99 self.userNames = userNames
100 self.userNameMap = {}
101 if self.userNames is None or len(self.userNames) <= 0:
102 self.userNames = []
103 self.userNameMap = {}
104 self.passwords = passwords
105 self.userNameMap = {}
106 if self.passwords is None or len(self.passwords) <= 0:
107 self.passwords = []
108 self.passwordMap = {}
109 self.tableList = {}
110 self.columnsList = {}
111 self.connMap = {}
112
113 self.config_file = "config.json"
114 self.initJDBC()
115 self.get_table_columns()
116 self.sql_class = SQL_class()
117
118 def initJDBC(self):
119 if os.path.exists(self.config_file):
120 with open(self.config_file, 'r') as config_file:
121 config = json.load(config_file)
122 jdbc_s = config['jdbc']
123 for jdbc in jdbc_s:
124 host = jdbc['host']
125 db = jdbc['db']
126 name = jdbc['name']
127 password = jdbc['password']
128 self.jdbcList.append(host + '/' + db)
129 self.userNames.append(name)
130 self.passwords.append(password)
131 self.userNameMap[host + '/' + db] = name
132 self.passwordMap[host + '/' + db] = password
133 self.connMap[host + '/' + db] = create_engine('mysql+pymysql://' + name + ':' + password + '@' + host + '/' + db)
134
135 def get_table_columns(self):
136 for jdbc in self.jdbcList:
137 # self.connMap[jdbc] = create_engine('mysql+pymysql://root:password@localhost/test_db')
138 # self.connMap[jdbc] = create_engine('mysql+pymysql://' + self.userNames + ':' +
139 # self.passwords + '@' + jdbc)
140 inspector = inspect(self.connMap[jdbc])
141 tables = inspector.get_table_names()
142 self.tableList[jdbc] = [table for table in tables]
143 for table in tables:
144 self.columnsList[jdbc + "_" + table] = [column for column in inspector.get_columns(table)]
145
146 def process_result(self, sql, result):
147 operator_type = get_db_operation_class(sql)
148 if operator_type == self.sql_class.INSERT:
149 pass

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected