2022-09-11 10:31:35 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -- coding:utf-8 --
|
|
|
|
|
# @Author: markushammered@gmail.com
|
|
|
|
|
# @Development Tool: PyCharm
|
|
|
|
|
# @Create Time: 2022/9/11
|
|
|
|
|
# @File Name: db.py
|
|
|
|
|
|
|
|
|
|
|
2022-12-21 21:12:21 +08:00
|
|
|
from src.config import Config
|
|
|
|
|
|
|
|
|
|
if Config.database == 'sqlite3':
|
|
|
|
|
import sqlite3 as operator
|
|
|
|
|
else:
|
|
|
|
|
import pymysql as operator
|
2022-09-11 10:31:35 +08:00
|
|
|
|
|
|
|
|
|
2022-11-13 15:28:38 +08:00
|
|
|
class BaseSQL:
|
2022-09-11 10:31:35 +08:00
|
|
|
def __init__(self):
|
|
|
|
|
self.conn = None
|
|
|
|
|
self.cursor = None
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
self.conn.commit()
|
|
|
|
|
self.cursor.close()
|
|
|
|
|
self.conn.close()
|
|
|
|
|
|
2022-12-22 18:10:02 +08:00
|
|
|
async def query(self, _id: str) -> tuple:
|
2022-09-11 10:31:35 +08:00
|
|
|
self.cursor.execute('select * from data where id="%s";' % _id)
|
|
|
|
|
result = self.cursor.fetchone()
|
|
|
|
|
if result is None:
|
2022-12-22 18:10:02 +08:00
|
|
|
await self.insert(_id)
|
2022-09-11 10:31:35 +08:00
|
|
|
return tuple([_id, 0])
|
2022-12-22 18:10:02 +08:00
|
|
|
await self.update(_id, result[1])
|
2022-09-11 10:31:35 +08:00
|
|
|
return result
|
|
|
|
|
|
2022-12-22 18:10:02 +08:00
|
|
|
async def insert(self, _id: str) -> bool:
|
2022-12-21 21:12:21 +08:00
|
|
|
self.cursor.execute('insert into data (id, times) values ("%s", 1);' % _id)
|
2022-09-11 10:31:35 +08:00
|
|
|
return True
|
|
|
|
|
|
2022-12-22 18:10:02 +08:00
|
|
|
async def update(self, _id: str, times: int) -> bool:
|
2022-09-11 10:31:35 +08:00
|
|
|
times += 1
|
|
|
|
|
self.cursor.execute('update data set times=%s where id="%s";' % (times, _id))
|
|
|
|
|
return True
|
|
|
|
|
|
2022-12-22 18:10:02 +08:00
|
|
|
async def query_all(self) -> list:
|
2022-09-11 10:31:35 +08:00
|
|
|
self.cursor.execute('select * from data;')
|
|
|
|
|
result = self.cursor.fetchall()
|
|
|
|
|
return result
|
|
|
|
|
|
2022-12-22 18:10:02 +08:00
|
|
|
async def query_image(self, theme: str) -> list:
|
2022-10-02 17:30:12 +08:00
|
|
|
self.cursor.execute('select * from %s;' % theme)
|
2022-09-11 10:31:35 +08:00
|
|
|
result = self.cursor.fetchall()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2022-11-13 15:30:34 +08:00
|
|
|
class SQLite(BaseSQL):
|
2022-09-11 10:31:35 +08:00
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
2022-12-22 17:02:00 +08:00
|
|
|
if Config.DETA:
|
|
|
|
|
self.conn = operator.connect('/tmp/data.sqlite')
|
|
|
|
|
else:
|
|
|
|
|
self.conn = operator.connect('./src/db/data.sqlite')
|
2022-09-11 10:31:35 +08:00
|
|
|
self.cursor = self.conn.cursor()
|
2022-12-21 21:12:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MySQL(BaseSQL):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
_CONFIG = Config.database.split('@')
|
|
|
|
|
user = _CONFIG[0].split(':')[0]
|
|
|
|
|
pwd = _CONFIG[0].split(':')[1]
|
|
|
|
|
host = _CONFIG[1].split(':')[0]
|
|
|
|
|
port = int(_CONFIG[1].split(':')[1].split('/')[0])
|
|
|
|
|
db = _CONFIG[1].split('/')[1]
|
|
|
|
|
|
|
|
|
|
self.conn = operator.connect(user=user,
|
|
|
|
|
passwd=pwd,
|
|
|
|
|
host=host,
|
|
|
|
|
port=port,
|
|
|
|
|
database=db)
|
|
|
|
|
self.cursor = self.conn.cursor()
|