-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #90 实现kv数据库接口 #93
+948
−68
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4dbba6f
Create db.py
HisAtri f88f123
Update db.py
HisAtri 4a7e715
更新db类方法,提供创建数据表的API接口
HisAtri c650360
实现KV数据库API(sqlite后端)
HisAtri acaddb5
合并相同端点API
HisAtri 3ae1f39
手动定义端点名
HisAtri 52b8717
更新自定义SQL接口
HisAtri 01087d8
忽略文件增加用户数据目录
HisAtri e573277
更新数据库API:
HisAtri db7feac
提供批量处理方法
HisAtri 750cea4
修改变量名
HisAtri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading status checks…
实现KV数据库API(sqlite后端)
commit c6503607e862711f37acd4e7b9e2adbb7114cb5d
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
from . import * | ||
|
||
import re | ||
from flask import request | ||
from mod.auth import require_auth_decorator | ||
|
||
from mod.db import SqliteDict | ||
|
||
SQLITE_RESERVED_WORDS = { | ||
"ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", "ASC", "ATTACH", "AUTOINCREMENT", | ||
"BEFORE", "BEGIN", "BETWEEN", "BY", "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", | ||
"CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", | ||
"DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", "ELSE", "END", | ||
"ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", | ||
"GROUP", "HAVING", "IF", "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT", | ||
"INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", | ||
"NO", "NOT", "NOTNULL", "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", "PRIMARY", | ||
"QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", | ||
"RIGHT", "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", "TEMPORARY", "THEN", "TO", "TRANSACTION", | ||
"TRIGGER", "UNION", "UNIQUE", "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", "WITH", | ||
"WITHOUT" | ||
} | ||
|
||
|
||
def valide_tablename(table_name: str) -> tuple[bool, str, int]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if not table_name: | ||
return False, "Missing table_name.", 422 | ||
invalid_chars = re.compile(r"[^a-zA-Z0-9_]") # 表名仅允许包含字母、数字和下划线 | ||
if invalid_chars.search(table_name): | ||
return False, "Invalid table_name: contains invalid characters.", 422 | ||
if table_name.upper() in SQLITE_RESERVED_WORDS: | ||
return False, "Invalid table_name: is a reserved keyword.", 422 | ||
# 限制表名长度为64字符 | ||
if len(table_name) > 64: | ||
return False, "Invalid table_name: too long.", 422 | ||
|
||
|
||
def kv_set(table_name: str, para: dict) -> tuple[bool, str, int]: | ||
""" | ||
写入或更新k-v数据 | ||
""" | ||
check_status: tuple[bool, str, int] = valide_tablename(table_name) | ||
if not check_status[0]: | ||
return check_status | ||
key = para.get("key") | ||
if not key: | ||
return False, "Missing key.", 422 | ||
elif type(key) is not str: | ||
return False, "Invalid key: must be a string.", 422 | ||
value = para.get("value") | ||
if not value: | ||
return False, "Missing value.", 422 | ||
try: | ||
with SqliteDict(tablename=table_name) as db: | ||
db[key] = value | ||
db.commit() | ||
except Exception as e: | ||
return False, str(e), 500 | ||
return True, table_name, 200 | ||
|
||
def kv_get(table_name: str, para: dict) -> tuple[bool, any, int]: | ||
""" | ||
读取k-v数据 | ||
""" | ||
check_status: tuple[bool, str, int] = valide_tablename(table_name) | ||
if not check_status[0]: | ||
return check_status | ||
key = para.get("key") | ||
if not key: | ||
return False, "Missing key.", 422 | ||
elif type(key) is not str: | ||
return False, "Invalid key: must be a string.", 422 | ||
try: | ||
with SqliteDict(tablename=table_name) as db: | ||
return True, db[key], 200 | ||
except KeyError: | ||
return False, "Key not found.", 404 | ||
except Exception as e: | ||
return False, str(e), 500 | ||
|
||
def kv_del(table_name: str, para: dict) -> tuple[bool, any, int]: | ||
""" | ||
删除k-v数据 | ||
""" | ||
check_status: tuple[bool, str, int] = valide_tablename(table_name) | ||
if not check_status[0]: | ||
return check_status | ||
key = para.get("key") | ||
if not key: | ||
return False, "Missing key.", 422 | ||
elif type(key) is not str: | ||
return False, "Invalid key: must be a string.", 422 | ||
|
||
try: | ||
with SqliteDict(tablename=table_name) as db: | ||
del db[key] | ||
db.commit() | ||
return True, key, 200 | ||
except KeyError: | ||
return False, "Key not found.", 404 | ||
except Exception as e: | ||
return False, str(e), 500 | ||
|
||
@v1_bp.route("/db/<path:table_name>", methods=["POST", "PUT"]) | ||
@require_auth_decorator(permission='rw') | ||
def db_set(table_name): | ||
""" | ||
写入或更新k-v数据 | ||
""" | ||
para: dict = request.json | ||
if not para: | ||
return {"code": 422, "message": "Missing JSON."}, 422 | ||
|
||
type = para.get("type") | ||
if not type: | ||
return {"code": 422, "message": "Missing type."}, 422 | ||
match type: | ||
case "kv": | ||
status, message, code = kv_set(table_name, para) | ||
return {"code": code, "message": message}, code | ||
case _: | ||
return {"code": 422, "message": "Invalid type."}, 422 | ||
|
||
@v1_bp.route("/db/<path:table_name>", methods=["GET"]) | ||
@require_auth_decorator(permission='rw') | ||
def db_get(table_name): | ||
""" | ||
读取k-v数据 | ||
""" | ||
para: dict = request.json | ||
if not para: | ||
return {"code": 422, "message": "Missing JSON."}, 422 | ||
|
||
type = para.get("type") | ||
if not type: | ||
return {"code": 422, "message": "Missing type."}, 422 | ||
match type: | ||
case "kv": | ||
status, message, code = kv_get(table_name, para) | ||
if status: | ||
return message, 200 | ||
else: | ||
return {"code": code, "message": message}, code | ||
case _: | ||
return {"code": 422, "message": "Invalid type."}, 422 | ||
|
||
@v1_bp.route("/db/<path:table_name>", methods=["DELETE"]) | ||
@require_auth_decorator(permission='rw') | ||
def db_del(table_name): | ||
""" | ||
删除k-v数据 | ||
""" | ||
para: dict = request.json | ||
if not para: | ||
return {"code": 422, "message": "Missing JSON."}, 422 | ||
|
||
type = para.get("type") | ||
if not type: | ||
return {"code": 422, "message": "Missing type."}, 422 | ||
match type: | ||
case "kv": | ||
status, message, code = kv_del(table_name, para) | ||
if status: | ||
return message, 200 | ||
else: | ||
return {"code": code, "message": message}, code | ||
case _: | ||
return {"code": 422, "message": "Invalid type."}, 422 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像并没有用于限制
CREATE TABLE
,检查这个好像不是特别有用(There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像确实表名没那么多限制,引号加一个就行