Arrumar "LIMIT" e "OFFSET" do db

This commit is contained in:
Augusto Gunsch 2021-10-14 16:31:56 -03:00
parent db4640de72
commit ad5aa0d69e
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
2 changed files with 6 additions and 8 deletions

View File

@ -34,10 +34,10 @@ class Database:
return trainers return trainers
def get_trainer_by_nickname(self, nickname, limit, offset): def get_trainer_by_nickname(self, nickname, limit, offset):
return self.__get_trainers(("SELECT * FROM Trainers WHERE nickname = ? ? OFFSET ?", (nickname, limit, offset))) return self.__get_trainers(("SELECT * FROM Trainers WHERE nickname = ? LIMIT ? OFFSET ?", (nickname, limit, offset)))
def get_trainers_by_nickname_contains(self, contains, limit, offset): def get_trainers_by_nickname_contains(self, contains, limit, offset):
return self.__get_trainers(("SELECT * FROM Trainers WHERE nickname LIKE ? ? OFFSET", ("%" + contains + "%", limit, offset))) return self.__get_trainers(("SELECT * FROM Trainers WHERE nickname LIKE ? LIMIT ? OFFSET ?", ("%" + contains + "%", limit, offset)))
def insert_trainer(self, trainer): def insert_trainer(self, trainer):
con = sqlite3.connect(self.db_file) con = sqlite3.connect(self.db_file)

View File

@ -7,7 +7,7 @@ def route_get_trainers():
args = request.args args = request.args
try: try:
limit = int(args.get("limit", 0)) limit = int(args.get("limit", -1))
offset = int(args.get("offset", 0)) offset = int(args.get("offset", 0))
except ValueError: except ValueError:
return { return {
@ -16,15 +16,13 @@ def route_get_trainers():
"message": "Couldn't parse parameter as integer" "message": "Couldn't parse parameter as integer"
}, 500 }, 500
if limit < 0 or offset < 0: if limit < -1 or offset < 0:
return { return {
"code": 2, "code": 2,
"type": "Integer parsing error", "type": "Integer parsing error",
"message": "Expected positive integer as parameter" "message": "Expected positive integer as parameter"
}, 500 }, 500
limit_str = "LIMIT %d" % limit if "limit" in args else "" # gambiarra pra tornar o limite infinito por padrão
nickname = args.get("nickname", "") nickname = args.get("nickname", "")
nickname_contains = args.get("nickname_contains", "") nickname_contains = args.get("nickname_contains", "")
@ -36,6 +34,6 @@ def route_get_trainers():
}, 500 }, 500
if nickname: if nickname:
return jsonify(db.get_trainer_by_nickname(nickname, limit_str, offset)) return jsonify(db.get_trainer_by_nickname(nickname, limit, offset))
else: else:
return jsonify(db.get_trainers_by_nickname_contains(nickname_contains, limit_str, offset)) return jsonify(db.get_trainers_by_nickname_contains(nickname_contains, limit, offset))