This commit is contained in:
ddrwode
2025-11-19 15:15:50 +08:00
parent f7343d26a3
commit efec2d729e
10 changed files with 3626 additions and 0 deletions

View File

@@ -2,3 +2,56 @@ from peewee import *
# 连接到 SQLite 数据库,如果文件不存在会自动创建
db = SqliteDatabase(r'E:\新建文件夹\lm_job\models\database.db')
import pymysql
from peewee import *
from playhouse.pool import PooledMySQLDatabase
pymysql.install_as_MySQLdb()
# 数据库配置
db_config = {
'database': 'lm',
'user': 'lm',
'password': 'nXMZ5JWbeKZ7N7RM',
'host': '192.168.1.105',
'port': 3306
}
# 全局数据库实例
db1 = MySQLDatabase(
db_config['database'],
user=db_config['user'],
password=db_config['password'],
host=db_config['host'],
port=db_config['port']
)
class BaseModel(Model):
class Meta:
database = db1
def save(self, *args, **kwargs):
"""在调用 save 时自动连接和关闭(若无事务)"""
db.connect(reuse_if_open=True)
try:
result = super().save(*args, **kwargs)
finally:
# 若当前没有事务且连接仍然打开,则关闭连接
if not db.in_transaction() and not db.is_closed():
db.close()
return result
@classmethod
def get_or_create(cls, defaults=None, **kwargs):
"""在调用 get_or_create 时自动连接和关闭(若无事务)"""
db.connect(reuse_if_open=True)
try:
obj, created = super().get_or_create(defaults=defaults, **kwargs)
finally:
# 若当前没有事务且连接仍然打开,则关闭连接
if not db.in_transaction() and not db.is_closed():
db.close()
return obj, created

15
models/ips.py Normal file
View File

@@ -0,0 +1,15 @@
from peewee import *
from models import BaseModel, db1
class Ips(BaseModel):
id = IntegerField(primary_key=True)
host = CharField(null=True)
port = CharField(null=True)
username = CharField(null=True)
password = CharField(null=True)
class Meta:
database = db1
table_name = 'ips'

15
models/xstart.py Normal file
View File

@@ -0,0 +1,15 @@
from peewee import *
from models import db1, BaseModel
class Xstart(BaseModel):
id = AutoField(primary_key=True) # 自增主键
bit_id = IntegerField(null=True)
start = CharField(null=True)
x_id = IntegerField(null=True)
ip_id = IntegerField(null=True)
class Meta:
database = db1 # 所属数据库
table_name = 'xstart'

23
models/xtoken.py Normal file
View File

@@ -0,0 +1,23 @@
from peewee import *
# 假设 db 已经在其他地方定义并连接到数据库
from models import BaseModel, db1
class XToken(BaseModel):
id = AutoField(primary_key=True) # 自增主键
hub_id = IntegerField(null=True) # hub_id 字段,整型,可为空
start = IntegerField(null=True) # start 字段,整型,可为空
account_start = IntegerField(null=True) # account_start 字段,整型,可为空
user_name = CharField(max_length=255, null=True) # user_name 字段,最大长度 255可为空
password = CharField(max_length=255, null=True) # password 字段,最大长度 255可为空
email = CharField(max_length=255, null=True) # email 字段,最大长度 255可为空
two_fa = CharField(max_length=255, null=True) # 2fa 字段,由于 2fa 是 Python 中的无效标识符,这里使用 two_fa 替代,最大长度 255可为空
token = CharField(max_length=255, null=True) # token 字段,最大长度 255可为空
email_pwd = CharField(max_length=255, null=True) # token 字段,最大长度 255可为空
class Meta:
database = db1 # 所属数据库
table_name = 'x_token' # 表名
# if __name__ == '__main__':
# XToken.create_table()