pyscriptbase 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pybase/__init__.py +0 -0
- pybase/cipher.py +169 -0
- pybase/database.py +470 -0
- pybase/env.py +87 -0
- pybase/net.py +440 -0
- pybase/panel.py +194 -0
- pybase/pusher.py +194 -0
- pybase/script.py +90 -0
- pybase/util.py +86 -0
- pybase/webview.py +54 -0
- pyscriptbase-1.0.1.dist-info/METADATA +24 -0
- pyscriptbase-1.0.1.dist-info/RECORD +14 -0
- pyscriptbase-1.0.1.dist-info/WHEEL +5 -0
- pyscriptbase-1.0.1.dist-info/top_level.txt +1 -0
pybase/__init__.py
ADDED
|
File without changes
|
pybase/cipher.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
from binascii import unhexlify
|
|
2
|
+
from Crypto.Cipher import AES, DES, DES3
|
|
3
|
+
from hashlib import md5, sha256
|
|
4
|
+
import hmac
|
|
5
|
+
from Crypto.PublicKey import RSA
|
|
6
|
+
from Crypto.Cipher import PKCS1_v1_5
|
|
7
|
+
from base64 import b64encode, b64decode
|
|
8
|
+
from Crypto.Util.Padding import pad, unpad
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AESCipher:
|
|
12
|
+
"""
|
|
13
|
+
AES/CBC/PKCS5Padding
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, key: str, iv: str):
|
|
17
|
+
self.key = key
|
|
18
|
+
self.iv = iv
|
|
19
|
+
self.size = AES.block_size
|
|
20
|
+
|
|
21
|
+
def encrypt(self, text: str, is_hex: bool = False) -> str:
|
|
22
|
+
"""
|
|
23
|
+
加密
|
|
24
|
+
"""
|
|
25
|
+
text = text.encode("utf-8")
|
|
26
|
+
if len(text) % self.size:
|
|
27
|
+
text = pad(text, self.size)
|
|
28
|
+
cipher = AES.new(key=self.key.encode(), mode=AES.MODE_CBC, IV=self.iv.encode())
|
|
29
|
+
encrypted_text = cipher.encrypt(text)
|
|
30
|
+
if is_hex:
|
|
31
|
+
return encrypted_text.hex()
|
|
32
|
+
else:
|
|
33
|
+
return b64encode(encrypted_text).decode("utf-8")
|
|
34
|
+
|
|
35
|
+
def decrypt(self, encrypted_text: str, is_hex: bool = False) -> str:
|
|
36
|
+
"""
|
|
37
|
+
解密
|
|
38
|
+
"""
|
|
39
|
+
if is_hex:
|
|
40
|
+
encrypted_text = unhexlify(encrypted_text)
|
|
41
|
+
else:
|
|
42
|
+
encrypted_text = encrypted_text.encode("utf-8")
|
|
43
|
+
encrypted_text = b64decode(encrypted_text)
|
|
44
|
+
if len(encrypted_text) % self.size:
|
|
45
|
+
encrypted_text = pad(encrypted_text, self.size)
|
|
46
|
+
cipher = AES.new(key=self.key.encode(), mode=AES.MODE_CBC, IV=self.iv.encode())
|
|
47
|
+
decrypted_text = cipher.decrypt(encrypted_text)
|
|
48
|
+
return unpad(decrypted_text, self.size).decode("utf-8")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class DESCipher:
|
|
52
|
+
"""
|
|
53
|
+
DES
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(self, key: str, iv: str):
|
|
57
|
+
self.key = key
|
|
58
|
+
self.iv = iv
|
|
59
|
+
self.size = DES.block_size
|
|
60
|
+
|
|
61
|
+
def __pad__(self, text: str):
|
|
62
|
+
return text + (self.size - len(text.encode()) % self.size) * chr(self.size - len(text.encode()) % self.size)
|
|
63
|
+
|
|
64
|
+
def __unpad__(self, text: str):
|
|
65
|
+
return text[: -ord(text[len(text) - 1 :])]
|
|
66
|
+
|
|
67
|
+
def encrypt(self, text):
|
|
68
|
+
"""
|
|
69
|
+
加密
|
|
70
|
+
"""
|
|
71
|
+
text = self.__pad__(text).encode()
|
|
72
|
+
cipher = DES.new(key=self.key.encode(), mode=DES.MODE_CBC, IV=self.iv.encode())
|
|
73
|
+
encrypted_text = cipher.encrypt(text)
|
|
74
|
+
return b64encode(encrypted_text).decode("utf-8")
|
|
75
|
+
|
|
76
|
+
def decrypt(self, encrypted_text: str):
|
|
77
|
+
"""
|
|
78
|
+
解密
|
|
79
|
+
"""
|
|
80
|
+
encrypted_text = encrypted_text.encode("utf-8")
|
|
81
|
+
encrypted_text = b64encode(encrypted_text)
|
|
82
|
+
cipher = DES.new(key=self.key.encode(), mode=DES.MODE_CBC, IV=self.iv.encode())
|
|
83
|
+
decrypted_text = cipher.decrypt(encrypted_text)
|
|
84
|
+
return self.__unpad__(decrypted_text).decode("utf-8")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class DES3Cipher:
|
|
88
|
+
"""
|
|
89
|
+
DES3
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
def __init__(self, key: str, iv: str):
|
|
93
|
+
self.key = key
|
|
94
|
+
self.iv = iv
|
|
95
|
+
self.size = DES3.block_size
|
|
96
|
+
|
|
97
|
+
def pad(self, text: str):
|
|
98
|
+
return text + (self.size - len(text.encode()) % self.size) * chr(self.size - len(text.encode()) % self.size)
|
|
99
|
+
|
|
100
|
+
def un_pad(self, text: str):
|
|
101
|
+
return text[: -ord(text[len(text) - 1 :])]
|
|
102
|
+
|
|
103
|
+
def encrypt(self, text):
|
|
104
|
+
"""
|
|
105
|
+
加密
|
|
106
|
+
"""
|
|
107
|
+
text = self.pad(text).encode()
|
|
108
|
+
cipher = DES3.new(key=self.key.encode(), mode=DES3.MODE_CBC, IV=self.iv.encode())
|
|
109
|
+
encrypted_text = cipher.encrypt(text)
|
|
110
|
+
return b64encode(encrypted_text).decode("utf-8")
|
|
111
|
+
|
|
112
|
+
def decrypt(self, encrypted_text):
|
|
113
|
+
"""
|
|
114
|
+
解密
|
|
115
|
+
"""
|
|
116
|
+
encrypted_text = b64decode(encrypted_text)
|
|
117
|
+
cipher = DES3.new(key=self.key.encode(), mode=DES3.MODE_CBC, IV=self.iv.encode())
|
|
118
|
+
decrypted_text = cipher.decrypt(encrypted_text)
|
|
119
|
+
return self.un_pad(decrypted_text).decode("utf-8")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class RSACipher:
|
|
123
|
+
"""
|
|
124
|
+
RSA-PKCS1
|
|
125
|
+
"""
|
|
126
|
+
|
|
127
|
+
def __init__(self, publicKey: str = "", privateKey: str = "") -> None:
|
|
128
|
+
if publicKey and not publicKey.startswith("-----BEGIN PUBLIC KEY-----"):
|
|
129
|
+
publicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-----END PUBLIC KEY-----"
|
|
130
|
+
if privateKey and not privateKey.startswith("-----BEGIN PRIVATE KEY-----"):
|
|
131
|
+
privateKey = "-----BEGIN PRIVATE KEY-----\n" + privateKey + "\n-----END PRIVATE KEY-----"
|
|
132
|
+
if publicKey:
|
|
133
|
+
self.publicKey = RSA.importKey(publicKey)
|
|
134
|
+
if privateKey:
|
|
135
|
+
self.privateKey = RSA.importKey(privateKey)
|
|
136
|
+
|
|
137
|
+
def encrypt(self, data) -> str:
|
|
138
|
+
"""
|
|
139
|
+
加密
|
|
140
|
+
"""
|
|
141
|
+
clipher = PKCS1_v1_5.new(self.publicKey)
|
|
142
|
+
encryptText = b64encode(clipher.encrypt(data.encode()))
|
|
143
|
+
return encryptText.decode()
|
|
144
|
+
|
|
145
|
+
def decrypt(self, data) -> str:
|
|
146
|
+
"""
|
|
147
|
+
解密
|
|
148
|
+
"""
|
|
149
|
+
clipher = PKCS1_v1_5.new(self.privateKey)
|
|
150
|
+
decryptText = clipher.decrypt(b64decode(data), None)
|
|
151
|
+
return decryptText.decode()
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def MD5(data: str) -> str:
|
|
155
|
+
"""
|
|
156
|
+
md5加密字符串
|
|
157
|
+
"""
|
|
158
|
+
if not data:
|
|
159
|
+
return ""
|
|
160
|
+
obj = md5()
|
|
161
|
+
obj.update(data.encode())
|
|
162
|
+
return obj.hexdigest()
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def hmacSha256(message: str, secret: str):
|
|
166
|
+
message = message.encode()
|
|
167
|
+
secret = secret.encode()
|
|
168
|
+
hash = hmac.new(secret, message, sha256)
|
|
169
|
+
return hash.hexdigest()
|
pybase/database.py
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
import pymysql
|
|
3
|
+
from . import env as Env
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DataConfig:
|
|
7
|
+
host = "127.0.0.1"
|
|
8
|
+
user = "root"
|
|
9
|
+
password = ""
|
|
10
|
+
database = "account"
|
|
11
|
+
table = ""
|
|
12
|
+
port = 3306
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def getDataConfig(database: str = "", table: str = "", port: int = 3306) -> DataConfig:
|
|
16
|
+
config = DataConfig()
|
|
17
|
+
config.host = Env.get("sql_host")
|
|
18
|
+
config.user = Env.get("sql_user")
|
|
19
|
+
config.password = Env.get("sql_password")
|
|
20
|
+
config.database = database
|
|
21
|
+
config.table = table
|
|
22
|
+
config.port = port
|
|
23
|
+
return config
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Key:
|
|
27
|
+
NULL = "NULL"
|
|
28
|
+
NOT_NULL = "NOT NULL"
|
|
29
|
+
IS = "IS"
|
|
30
|
+
EQUAL = "="
|
|
31
|
+
OR = "OR"
|
|
32
|
+
AND = "AND"
|
|
33
|
+
NOT_EQUAL = "!="
|
|
34
|
+
LIKE = "LIKE"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SQLHelper:
|
|
38
|
+
"""
|
|
39
|
+
数据库辅助类,不支持多线程
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, user="root", password="root", port=3306, database="", host="localhost") -> None:
|
|
43
|
+
self.host = host
|
|
44
|
+
self.user = user
|
|
45
|
+
self.password = password
|
|
46
|
+
self.database = database
|
|
47
|
+
self.keywords = ["NULL", "NOT NULL"]
|
|
48
|
+
self.db = pymysql.connect(
|
|
49
|
+
host=self.host,
|
|
50
|
+
user=self.user,
|
|
51
|
+
passwd=self.password,
|
|
52
|
+
database=self.database,
|
|
53
|
+
port=port,
|
|
54
|
+
)
|
|
55
|
+
self.__clear__()
|
|
56
|
+
|
|
57
|
+
def __clear__(self):
|
|
58
|
+
"""
|
|
59
|
+
重置条件变量
|
|
60
|
+
"""
|
|
61
|
+
self.table: str = None
|
|
62
|
+
self.wheres: list = None
|
|
63
|
+
self.fields: list = None
|
|
64
|
+
self.orders: list = None
|
|
65
|
+
self.limit: int = None
|
|
66
|
+
self.offset: int = None
|
|
67
|
+
self.values: list = None
|
|
68
|
+
|
|
69
|
+
def __join__(self, ds: list, delimiter: str = ","):
|
|
70
|
+
if not ds:
|
|
71
|
+
return ""
|
|
72
|
+
res = ""
|
|
73
|
+
for d in ds:
|
|
74
|
+
if res:
|
|
75
|
+
res += f"{delimiter}{d}"
|
|
76
|
+
else:
|
|
77
|
+
res += f"{d}"
|
|
78
|
+
return res
|
|
79
|
+
|
|
80
|
+
def __build_set__(self) -> str:
|
|
81
|
+
"""
|
|
82
|
+
构建SET子句
|
|
83
|
+
"""
|
|
84
|
+
if self.fields == None or self.values == None or len(self.fields) != len(self.values):
|
|
85
|
+
return None
|
|
86
|
+
kvs = []
|
|
87
|
+
for i in range(len(self.fields)):
|
|
88
|
+
field = self.fields[i]
|
|
89
|
+
value = self.values[i]
|
|
90
|
+
kvs.append(f"{field} = {value}")
|
|
91
|
+
return f"SET {self.__join__(kvs)} "
|
|
92
|
+
|
|
93
|
+
def __build_wheres__(self) -> str:
|
|
94
|
+
"""
|
|
95
|
+
构建WHERE子句
|
|
96
|
+
"""
|
|
97
|
+
if self.wheres == None or len(self.wheres) == 0:
|
|
98
|
+
return None
|
|
99
|
+
isConnect = False
|
|
100
|
+
ws = []
|
|
101
|
+
for where in self.wheres:
|
|
102
|
+
# 自定义连接符
|
|
103
|
+
if isinstance(where, str):
|
|
104
|
+
ws.append(where)
|
|
105
|
+
isConnect = False
|
|
106
|
+
else:
|
|
107
|
+
# 默认AND连接条件
|
|
108
|
+
if isConnect:
|
|
109
|
+
ws.append("AND")
|
|
110
|
+
ws.append(f"{where[0]} {where[1]} {where[2]}")
|
|
111
|
+
isConnect = not isConnect
|
|
112
|
+
return f"WHERE {self.__join__(ws,' ')} "
|
|
113
|
+
|
|
114
|
+
def __build_orders__(self) -> str:
|
|
115
|
+
"""
|
|
116
|
+
构建ORDER BY子句
|
|
117
|
+
"""
|
|
118
|
+
if self.orders == None or len(self.orders) == 0:
|
|
119
|
+
return None
|
|
120
|
+
ods = []
|
|
121
|
+
for order in self.orders:
|
|
122
|
+
# 默认升序
|
|
123
|
+
if len(order) == 1:
|
|
124
|
+
ods.append(f"{order[0]} ASC")
|
|
125
|
+
# 自定义排序
|
|
126
|
+
else:
|
|
127
|
+
ods.append(f"{order[0]} {order[1]}")
|
|
128
|
+
return f"ORDER BY {self.__join__(ods)} "
|
|
129
|
+
|
|
130
|
+
def __build_update__(self) -> str:
|
|
131
|
+
"""
|
|
132
|
+
构建UPDATE语句
|
|
133
|
+
"""
|
|
134
|
+
if self.table == None:
|
|
135
|
+
return None
|
|
136
|
+
sql = f"UPDATE {self.table} "
|
|
137
|
+
setW = self.__build_set__()
|
|
138
|
+
if setW == None:
|
|
139
|
+
return None
|
|
140
|
+
sql += setW
|
|
141
|
+
where = self.__build_wheres__()
|
|
142
|
+
if where == None:
|
|
143
|
+
return None
|
|
144
|
+
sql += where
|
|
145
|
+
return sql
|
|
146
|
+
|
|
147
|
+
def __build_insert__(self) -> str:
|
|
148
|
+
"""
|
|
149
|
+
构建SELECT语句
|
|
150
|
+
"""
|
|
151
|
+
if self.table == None:
|
|
152
|
+
return None
|
|
153
|
+
sql = f"INSERT INTO {self.table}({self.__join__(self.fields)}) VALUES({self.__join__(self.values)})"
|
|
154
|
+
return sql
|
|
155
|
+
|
|
156
|
+
def __build_delete__(self) -> str:
|
|
157
|
+
"""
|
|
158
|
+
构建DELETE语句,不允许没有where条件
|
|
159
|
+
"""
|
|
160
|
+
if self.table == None:
|
|
161
|
+
return None
|
|
162
|
+
where = self.__build_wheres__()
|
|
163
|
+
if not where:
|
|
164
|
+
return None
|
|
165
|
+
sql = f"DELETE FROM {self.table} {where}"
|
|
166
|
+
return sql
|
|
167
|
+
|
|
168
|
+
def __build_query__(self) -> str:
|
|
169
|
+
"""
|
|
170
|
+
构建SELECT语句
|
|
171
|
+
"""
|
|
172
|
+
# 初步判断
|
|
173
|
+
if self.table == None:
|
|
174
|
+
return None
|
|
175
|
+
# 查询语句
|
|
176
|
+
sql = "SELECT "
|
|
177
|
+
# 选择字段
|
|
178
|
+
if self.fields != None:
|
|
179
|
+
sql += f"{self.__join__(self.fields)} "
|
|
180
|
+
else:
|
|
181
|
+
sql += "* "
|
|
182
|
+
# 选择表
|
|
183
|
+
sql += f"FROM {self.table} "
|
|
184
|
+
# 选择条件
|
|
185
|
+
where = self.__build_wheres__()
|
|
186
|
+
if where:
|
|
187
|
+
sql += where
|
|
188
|
+
# 选择排序
|
|
189
|
+
orders = self.__build_orders__()
|
|
190
|
+
if orders:
|
|
191
|
+
sql += orders
|
|
192
|
+
# 选择数量限制
|
|
193
|
+
if self.limit != None:
|
|
194
|
+
sql += f"LIMIT {self.limit} "
|
|
195
|
+
# 选择偏移
|
|
196
|
+
if self.offset != None:
|
|
197
|
+
sql += f"OFFSET {self.offset} "
|
|
198
|
+
return sql
|
|
199
|
+
|
|
200
|
+
def __build_query_count__(self) -> str:
|
|
201
|
+
"""
|
|
202
|
+
构建SELECT语句
|
|
203
|
+
"""
|
|
204
|
+
# 初步判断
|
|
205
|
+
if self.table == None:
|
|
206
|
+
return None
|
|
207
|
+
# 查询语句
|
|
208
|
+
sql = "SELECT COUNT(*) "
|
|
209
|
+
# 选择表
|
|
210
|
+
sql += f"FROM {self.table} "
|
|
211
|
+
# 选择条件
|
|
212
|
+
where = self.__build_wheres__()
|
|
213
|
+
if where:
|
|
214
|
+
sql += where
|
|
215
|
+
return sql
|
|
216
|
+
|
|
217
|
+
def __build_replace__(self) -> str:
|
|
218
|
+
"""
|
|
219
|
+
构建REPLACE语句
|
|
220
|
+
"""
|
|
221
|
+
if self.table == None:
|
|
222
|
+
return None
|
|
223
|
+
sql = f"INSERT INTO {self.table}({self.__join__(self.fields)}) VALUES({self.__join__(self.values)}) ON DUPLICATE KEY UPDATE "
|
|
224
|
+
update_fields = []
|
|
225
|
+
for i in range(len(self.fields)):
|
|
226
|
+
update_fields.append(f"{self.fields[i]}=VALUES({self.fields[i]})")
|
|
227
|
+
sql += self.__join__(update_fields)
|
|
228
|
+
return sql
|
|
229
|
+
|
|
230
|
+
def __execute__(self, sql: str):
|
|
231
|
+
"""
|
|
232
|
+
执行SQL语句
|
|
233
|
+
"""
|
|
234
|
+
if not sql:
|
|
235
|
+
return None
|
|
236
|
+
try:
|
|
237
|
+
cursor = self.db.cursor()
|
|
238
|
+
cursor.execute(sql)
|
|
239
|
+
self.db.commit()
|
|
240
|
+
return cursor
|
|
241
|
+
except Exception as e:
|
|
242
|
+
print("======SQL ERROR======")
|
|
243
|
+
print(sql)
|
|
244
|
+
print(e)
|
|
245
|
+
return None
|
|
246
|
+
|
|
247
|
+
def set_database(self, db: str):
|
|
248
|
+
"""
|
|
249
|
+
(可选)设置数据库,不设置则使用上次操作的数据库
|
|
250
|
+
"""
|
|
251
|
+
self.db.select_db(db)
|
|
252
|
+
return self
|
|
253
|
+
|
|
254
|
+
def set_table(self, table: str):
|
|
255
|
+
"""
|
|
256
|
+
(必选)设置要操作的表
|
|
257
|
+
"""
|
|
258
|
+
self.__clear__() # 清除上次的值
|
|
259
|
+
self.table = table
|
|
260
|
+
return self
|
|
261
|
+
|
|
262
|
+
def set_fields(self, fields: list):
|
|
263
|
+
"""
|
|
264
|
+
(可选)设置insert、query或update字段,对于insert不设置则默认选择全部字段
|
|
265
|
+
"""
|
|
266
|
+
self.fields = list(fields)
|
|
267
|
+
return self
|
|
268
|
+
|
|
269
|
+
def set_values(self, values: list):
|
|
270
|
+
"""
|
|
271
|
+
(必选)设置insert或update字段的值
|
|
272
|
+
"""
|
|
273
|
+
self.values = list(values)
|
|
274
|
+
# 字符串类型值用''包括
|
|
275
|
+
for i in range(len(self.values)):
|
|
276
|
+
if isinstance(self.values[i], str):
|
|
277
|
+
newValue = self.values[i].replace("'", "\\'").replace('"', '\\"')
|
|
278
|
+
self.values[i] = f"'{newValue}'"
|
|
279
|
+
return self
|
|
280
|
+
|
|
281
|
+
def set_dict(self, obj: dict):
|
|
282
|
+
"""
|
|
283
|
+
(可选)设置insert或update的字典
|
|
284
|
+
"""
|
|
285
|
+
self.set_fields(obj.keys())
|
|
286
|
+
self.set_values(obj.values())
|
|
287
|
+
return self
|
|
288
|
+
|
|
289
|
+
def set_wheres(self, wheres: list):
|
|
290
|
+
"""
|
|
291
|
+
(可选)设置update或query的条件
|
|
292
|
+
"""
|
|
293
|
+
# 多层对象必须需要深拷贝
|
|
294
|
+
self.wheres = deepcopy(wheres)
|
|
295
|
+
for i in range(len(self.wheres)):
|
|
296
|
+
# 连接符
|
|
297
|
+
if isinstance(self.wheres[i], str):
|
|
298
|
+
continue
|
|
299
|
+
# kv默认用=号连接
|
|
300
|
+
if len(self.wheres[i]) == 2:
|
|
301
|
+
self.wheres[i].insert(1, "=")
|
|
302
|
+
# 非关键字字符串类型值用''包括
|
|
303
|
+
if len(self.wheres[i]) == 3 and isinstance(self.wheres[i][2], str):
|
|
304
|
+
if self.wheres[i][2].upper() not in self.keywords:
|
|
305
|
+
self.wheres[i][2] = f"'{self.wheres[i][2]}'"
|
|
306
|
+
return self
|
|
307
|
+
|
|
308
|
+
def set_limit(self, limit: int):
|
|
309
|
+
"""
|
|
310
|
+
(可选)设置query的数量
|
|
311
|
+
"""
|
|
312
|
+
self.limit = limit
|
|
313
|
+
return self
|
|
314
|
+
|
|
315
|
+
def set_offset(self, offset: int):
|
|
316
|
+
"""
|
|
317
|
+
(可选)设置query的偏移量
|
|
318
|
+
"""
|
|
319
|
+
self.offset = offset
|
|
320
|
+
return self
|
|
321
|
+
|
|
322
|
+
def set_orders(self, orders: list):
|
|
323
|
+
"""
|
|
324
|
+
(可选)设置query排序字段和排序方式(默认升序)
|
|
325
|
+
- [["id","DESC"]]
|
|
326
|
+
- ["id"]
|
|
327
|
+
"""
|
|
328
|
+
self.orders = deepcopy(orders)
|
|
329
|
+
return self
|
|
330
|
+
|
|
331
|
+
def update(self) -> bool:
|
|
332
|
+
"""
|
|
333
|
+
执行update操作,不允许没有where条件
|
|
334
|
+
"""
|
|
335
|
+
sql = self.__build_update__()
|
|
336
|
+
self.__clear__()
|
|
337
|
+
cursor = self.__execute__(sql)
|
|
338
|
+
if cursor != None:
|
|
339
|
+
return True
|
|
340
|
+
else:
|
|
341
|
+
return False
|
|
342
|
+
|
|
343
|
+
def insert(self) -> bool:
|
|
344
|
+
"""
|
|
345
|
+
执行insert操作
|
|
346
|
+
"""
|
|
347
|
+
sql = self.__build_insert__()
|
|
348
|
+
self.__clear__()
|
|
349
|
+
cursor = self.__execute__(sql)
|
|
350
|
+
if cursor != None:
|
|
351
|
+
return True
|
|
352
|
+
else:
|
|
353
|
+
return False
|
|
354
|
+
|
|
355
|
+
def delete(self) -> bool:
|
|
356
|
+
"""
|
|
357
|
+
执行delete操作
|
|
358
|
+
"""
|
|
359
|
+
sql = self.__build_delete__()
|
|
360
|
+
self.__clear__()
|
|
361
|
+
cursor = self.__execute__(sql)
|
|
362
|
+
if cursor != None:
|
|
363
|
+
return True
|
|
364
|
+
else:
|
|
365
|
+
return False
|
|
366
|
+
|
|
367
|
+
def query_dict(self) -> list:
|
|
368
|
+
"""
|
|
369
|
+
当指定了字段集时,将以字典的形式返回查询结果
|
|
370
|
+
"""
|
|
371
|
+
if not self.fields:
|
|
372
|
+
return self.query()
|
|
373
|
+
|
|
374
|
+
sql = self.__build_query__()
|
|
375
|
+
fields = self.fields
|
|
376
|
+
self.__clear__()
|
|
377
|
+
cursor = self.__execute__(sql)
|
|
378
|
+
if cursor != None:
|
|
379
|
+
datas = cursor.fetchall()
|
|
380
|
+
res = []
|
|
381
|
+
for data in datas:
|
|
382
|
+
index = 0
|
|
383
|
+
dic = {}
|
|
384
|
+
for field in fields:
|
|
385
|
+
dic[field] = data[index]
|
|
386
|
+
index += 1
|
|
387
|
+
res.append(dic)
|
|
388
|
+
return res
|
|
389
|
+
else:
|
|
390
|
+
return None
|
|
391
|
+
|
|
392
|
+
def query(self) -> tuple:
|
|
393
|
+
"""
|
|
394
|
+
执行query操作
|
|
395
|
+
"""
|
|
396
|
+
sql = self.__build_query__()
|
|
397
|
+
self.__clear__()
|
|
398
|
+
cursor = self.__execute__(sql)
|
|
399
|
+
if cursor != None:
|
|
400
|
+
return cursor.fetchall()
|
|
401
|
+
else:
|
|
402
|
+
return None
|
|
403
|
+
|
|
404
|
+
def query_count(self) -> int:
|
|
405
|
+
"""
|
|
406
|
+
执行query操作
|
|
407
|
+
"""
|
|
408
|
+
sql = self.__build_query_count__()
|
|
409
|
+
self.__clear__()
|
|
410
|
+
cursor = self.__execute__(sql)
|
|
411
|
+
if cursor != None:
|
|
412
|
+
return cursor.fetchone()[0]
|
|
413
|
+
else:
|
|
414
|
+
return None
|
|
415
|
+
|
|
416
|
+
def replace(self) -> bool:
|
|
417
|
+
"""
|
|
418
|
+
执行replace操作
|
|
419
|
+
"""
|
|
420
|
+
sql = self.__build_replace__()
|
|
421
|
+
self.__clear__()
|
|
422
|
+
cursor = self.__execute__(sql)
|
|
423
|
+
if cursor != None:
|
|
424
|
+
return True
|
|
425
|
+
else:
|
|
426
|
+
return False
|
|
427
|
+
|
|
428
|
+
def close(self):
|
|
429
|
+
"""
|
|
430
|
+
关闭数据库
|
|
431
|
+
"""
|
|
432
|
+
try:
|
|
433
|
+
self.db.close()
|
|
434
|
+
except:
|
|
435
|
+
pass
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
if __name__ == "__main__":
|
|
439
|
+
pass
|
|
440
|
+
# # 插入-字典形式
|
|
441
|
+
# proxy = {"ip": "127.0.0.1", "port": 10002}
|
|
442
|
+
# db.setTable("proxies").setObj(proxy).insert()
|
|
443
|
+
|
|
444
|
+
# # 插入-普通形式
|
|
445
|
+
# fileds = ["ip", "port"]
|
|
446
|
+
# values = ["127.0.0.1", 10002]
|
|
447
|
+
# db.setTable("proxies").setFields(fileds).setValues(values).insert()
|
|
448
|
+
|
|
449
|
+
# # 更新-字典形式
|
|
450
|
+
# proxy = {"ip": "127.0.0.1", "port": 8080}
|
|
451
|
+
# wheres = [['ip', "=", "127.0.0.1"]]
|
|
452
|
+
# db.setTable("proxies").setObj(proxy).setWheres(wheres).update()
|
|
453
|
+
|
|
454
|
+
# # 更新-普通形式
|
|
455
|
+
# fileds = ["ip", "port"]
|
|
456
|
+
# values = ["127.0.0.1", 8080]
|
|
457
|
+
# wheres = [['ip', "=", "127.0.0.1"]]
|
|
458
|
+
# db.setTable("proxies").setFields(fileds).setValues(
|
|
459
|
+
# values).setWheres(wheres).update()
|
|
460
|
+
|
|
461
|
+
# # 查询
|
|
462
|
+
# orders = ["ip", ("port", "DESC")]
|
|
463
|
+
# wheres = [["port", ">", 1000]]
|
|
464
|
+
# proxies = db.setTable("proxies").query()
|
|
465
|
+
# proxies = db.setTable("proxies").setLimit(10).query()
|
|
466
|
+
# proxies = db.setTable("proxies").setOrders(
|
|
467
|
+
# orders).setWheres(wheres).query()
|
|
468
|
+
|
|
469
|
+
# # 复杂SQL语句,可以直接调用execute()
|
|
470
|
+
# cursor = db.setTable("proxies").execute("your sql")
|