no nulls because null != null

dev
Anton Franzluebbers 2023-04-27 17:30:11 -04:00
parent 4a02fa2757
commit 6e2fcf7096
2 changed files with 28 additions and 21 deletions

View File

@ -72,7 +72,7 @@ CREATE TABLE `DataBlock` (
`id` TEXT NOT NULL, `id` TEXT NOT NULL,
-- id of the owner of this file. Ownership is not transferable because ids may collide, -- id of the owner of this file. Ownership is not transferable because ids may collide,
-- but the owner could be null for global scope -- but the owner could be null for global scope
`owner_id` TEXT, `owner_id` TEXT NOT NULL DEFAULT 'none',
`visibility` TEXT CHECK( `visibility` IN ('public','private','unlisted') ) NOT NULL DEFAULT 'public', `visibility` TEXT CHECK( `visibility` IN ('public','private','unlisted') ) NOT NULL DEFAULT 'public',
-- This is an indexable field to filter out different types of datablocks -- This is an indexable field to filter out different types of datablocks
`category` TEXT, `category` TEXT,

View File

@ -92,7 +92,8 @@ def get_device_by_pairing_code(pairing_code: str):
def get_device_by_pairing_code_dict(pairing_code: str) -> dict | None: def get_device_by_pairing_code_dict(pairing_code: str) -> dict | None:
values = db.query("SELECT * FROM `Device` WHERE `pairing_code`=:pairing_code;", {'pairing_code': pairing_code}) values = db.query(
"SELECT * FROM `Device` WHERE `pairing_code`=:pairing_code;", {'pairing_code': pairing_code})
if len(values) == 1: if len(values) == 1:
device = dict(values[0]) device = dict(values[0])
parse_data(device) parse_data(device)
@ -101,7 +102,8 @@ def get_device_by_pairing_code_dict(pairing_code: str) -> dict | None:
def get_user_for_device(hw_id: str) -> dict: def get_user_for_device(hw_id: str) -> dict:
values = db.query("""SELECT * FROM `UserDevice` WHERE `hw_id`=:hw_id;""", {'hw_id': hw_id}) values = db.query(
"""SELECT * FROM `UserDevice` WHERE `hw_id`=:hw_id;""", {'hw_id': hw_id})
if len(values) == 1: if len(values) == 1:
user_id = dict(values[0])['user_id'] user_id = dict(values[0])['user_id']
user = get_user_dict(user_id=user_id) user = get_user_dict(user_id=user_id)
@ -149,7 +151,8 @@ def get_device_data(request: Request, response: Response, hw_id: str):
room_data = get_data(response, key=room_key, user_id=user['id']) room_data = get_data(response, key=room_key, user_id=user['id'])
if "error" in room_data: if "error" in room_data:
set_data(request, data={}, key=room_key, modified_by=None, category="room") set_data(request, data={}, key=room_key,
modified_by=None, category="room")
room_data = get_data(response, key=room_key, user_id=user['id']) room_data = get_data(response, key=room_key, user_id=user['id'])
return {'device': block, 'room': room_data, 'user': user} return {'device': block, 'room': room_data, 'user': user}
@ -264,19 +267,21 @@ def set_data(request: fastapi.Request, data: dict, key: str = None, owner: str =
# add the data to the db # add the data to the db
db.insert(""" db.insert("""
REPLACE INTO `DataBlock` (id, owner_id, category, modified_by, data, last_modified) UPDATE `DataBlock` SET
category = :category,
modified_by = :modified_by,
data = :data,
last_modified = CURRENT_TIMESTAMP
WHERE id=:id AND owner_id = :owner_id;
""", {"id": key, "category": category, "modified_by": modified_by, "owner_id": owner, "data": json.dumps(data)})
else:
# add the data to the db
db.insert("""
INSERT INTO `DataBlock` (id, owner_id, category, modified_by, data, last_modified)
VALUES(:id, :owner_id, :category, :modified_by, :data, CURRENT_TIMESTAMP); VALUES(:id, :owner_id, :category, :modified_by, :data, CURRENT_TIMESTAMP);
""", {"id": key, "owner_id": owner, "category": category, "modified_by": modified_by, "data": json.dumps(data)}) """, {"id": key, "owner_id": owner, "category": category, "modified_by": modified_by, "data": json.dumps(data)})
# # add the data to the db
# db.insert("""
# REPLACE INTO `DataBlock` SET
# category = :category,
# modified_by = :modified_by,
# data = :data,
# last_modified = CURRENT_TIMESTAMP
# WHERE id=:id AND owner_id = :owner_id;
# """, {"id": key, "category": category, "modified_by": modified_by, "owner_id": owner, "data": json.dumps(data)})
return {'key': key} return {'key': key}
@ -347,7 +352,8 @@ def get_user(response: Response, user_id: str):
def get_user_dict(user_id: str) -> dict | None: def get_user_dict(user_id: str) -> dict | None:
values = db.query("SELECT * FROM `User` WHERE `id`=:user_id;", {'user_id': user_id}) values = db.query(
"SELECT * FROM `User` WHERE `id`=:user_id;", {'user_id': user_id})
if len(values) == 1: if len(values) == 1:
user = dict(values[0]) user = dict(values[0])
parse_data(user) parse_data(user)
@ -377,7 +383,8 @@ async def upload_file(request: fastapi.Request, file: UploadFile, key: str | Non
content = await file.read() # async read content = await file.read() # async read
await out_file.write(content) # async write await out_file.write(content) # async write
# add a datablock to link to the file # add a datablock to link to the file
set_data(request, data={'filename': file.filename}, key=key, category='file', modified_by=modified_by) set_data(request, data={'filename': file.filename},
key=key, category='file', modified_by=modified_by)
return {"filename": file.filename, 'key': key} return {"filename": file.filename, 'key': key}