Merged in bugfix/HTTPASTE-30/backend/sqlite (pull request #9)
refactor(backend/sqlite): normalize functions
This commit is contained in:
commit
21d8b8c541
2 changed files with 82 additions and 49 deletions
|
|
@ -9,73 +9,92 @@ def load(proto: object, connection: Connection, model_class: type):
|
||||||
"""load a paste
|
"""load a paste
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cur = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cur.execute(
|
statement = '''SELECT pid, data, data_hash, sub, expiration, encoding
|
||||||
'SELECT pid, data, data_hash, sub, expiration, encoding FROM pastes WHERE pid=?',
|
FROM pastes
|
||||||
(proto.pid,
|
WHERE pid=?'''
|
||||||
))
|
|
||||||
|
|
||||||
result = cur.fetchone()
|
cursor.execute(statement, (proto.pid,))
|
||||||
|
|
||||||
if result:
|
row = cursor.fetchone()
|
||||||
|
|
||||||
|
if row is not None:
|
||||||
|
|
||||||
return model_class(
|
return model_class(
|
||||||
result['pid'],
|
row['pid'],
|
||||||
result['sub'],
|
row['sub'],
|
||||||
result['data'],
|
row['data'],
|
||||||
result['data_hash'],
|
row['data_hash'],
|
||||||
result['expiration'],
|
row['expiration'],
|
||||||
result['encoding'])
|
row['encoding'])
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def dump(model: object, connection: Connection):
|
def dump(model: object, connection: Connection) -> None:
|
||||||
"""dump a paste
|
"""dump a paste
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cur = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cur.execute(
|
statement = '''INSERT INTO pastes
|
||||||
'''INSERT INTO pastes (pid, data, data_hash, sub, expiration, encoding)
|
(pid, data, data_hash, sub, expiration, encoding)
|
||||||
VALUES (?,?,?,?,?,?)''',
|
VALUES (?,?,?,?,?,?)'''
|
||||||
(model.pid,
|
|
||||||
|
values = (model.pid,
|
||||||
model.data,
|
model.data,
|
||||||
model.data_hash,
|
model.data_hash,
|
||||||
model.sub,
|
model.sub,
|
||||||
model.expiration,
|
model.expiration,
|
||||||
model.encoding))
|
model.encoding)
|
||||||
|
|
||||||
|
cursor.execute(statement, values)
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def delete(proto: object, connection: Connection) -> bool:
|
|
||||||
|
|
||||||
cur = connection.cursor()
|
def delete(proto: object, connection: Connection) -> None:
|
||||||
|
|
||||||
cur.execute('''DELETE FROM pastes WHERE pid=?''', (proto.pid,))
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
cursor.execute('''DELETE FROM pastes WHERE pid=?''', (proto.pid,))
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def init(connection: Connection):
|
def init(connection: Connection):
|
||||||
|
|
||||||
cur = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
with open(path.join(path.dirname(__file__), 'paste.sql'), 'r') as fh:
|
with open(path.join(path.dirname(__file__), 'paste.sql'), 'r') as fh:
|
||||||
|
|
||||||
cur.execute(fh.read())
|
statement = fh.read()
|
||||||
|
|
||||||
|
cursor.execute(statement)
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
|
||||||
def sanitize(connection: Connection, model_class: type) -> bool:
|
def sanitize(connection: Connection, model_class: type) -> int:
|
||||||
|
|
||||||
cur = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cur.execute('''SELECT pid FROM pastes WHERE expiration < ? AND expiration > 0''', (int(time()),))
|
statement = '''SELECT pid FROM pastes
|
||||||
|
WHERE expiration < ? AND expiration > 0'''
|
||||||
|
|
||||||
|
cursor.execute(statement, (int(time()),))
|
||||||
|
|
||||||
|
srow_count = 0
|
||||||
|
|
||||||
for row in cur.fetchall():
|
for row in cur.fetchall():
|
||||||
|
|
||||||
delete(model_class(row['pid']))
|
delete(model_class(row['pid']))
|
||||||
|
|
||||||
|
srow_count += 1
|
||||||
|
|
||||||
|
return srow_count
|
||||||
|
|
@ -2,59 +2,73 @@
|
||||||
"""
|
"""
|
||||||
from os import path
|
from os import path
|
||||||
from sqlite3 import Connection
|
from sqlite3 import Connection
|
||||||
from httpaste.model import User
|
|
||||||
|
|
||||||
|
|
||||||
def load(proto: User, connection: Connection):
|
def load(proto: object, connection: Connection, model_class: type):
|
||||||
"""load a user
|
"""load a user
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cur = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cur.execute(
|
statement = '''SELECT sub, key_hash, paste_index
|
||||||
'SELECT sub, key_hash, paste_index FROM users WHERE sub=?', (proto.sub,))
|
FROM users
|
||||||
|
WHERE sub=?'''
|
||||||
|
|
||||||
result = cur.fetchone()
|
cursor.execute(statement, (proto.sub,))
|
||||||
|
|
||||||
if result:
|
row = cursor.fetchone()
|
||||||
|
|
||||||
return User(result['sub'], result['key_hash'], result['paste_index'])
|
if row is not None:
|
||||||
|
|
||||||
|
return model_class(result['sub'], result['key_hash'],
|
||||||
|
result['paste_index'])
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def dump(model: User, connection: Connection):
|
def dump(model: object, connection: Connection) -> None:
|
||||||
"""dump a user
|
"""dump a user
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cur = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cur.execute('''INSERT OR REPLACE INTO users (sub, key_hash, paste_index)
|
statement = '''INSERT OR REPLACE INTO users
|
||||||
VALUES (?,?,?)''', (model.sub, model.key_hash, model.index))
|
(sub, key_hash, paste_index)
|
||||||
|
VALUES (?,?,?)'''
|
||||||
|
|
||||||
|
cursor.execute(statement, (model.sub, model.key_hash, model.index))
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def delete(proto: object, connection: Connection) -> bool:
|
|
||||||
|
|
||||||
cur = connection.cursor()
|
def delete(proto: object, connection: Connection) -> None:
|
||||||
|
|
||||||
cur.execute('''DELETE FROM users WHERE sub=?''', (proto.sub,))
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
cursor.execute('''DELETE FROM users WHERE sub=?''', (proto.sub,))
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def init(connection: Connection):
|
|
||||||
|
|
||||||
cur = connection.cursor()
|
def init(connection: Connection) -> None:
|
||||||
|
|
||||||
|
cursor = connection.cursor()
|
||||||
|
|
||||||
with open(path.join(path.dirname(__file__), 'user.sql'), 'r') as fh:
|
with open(path.join(path.dirname(__file__), 'user.sql'), 'r') as fh:
|
||||||
|
|
||||||
cur.execute(fh.read())
|
statement = fh.read()
|
||||||
|
|
||||||
|
cursor.execute(statement)
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def sanitize(connection: Connection, model_class) -> bool:
|
|
||||||
|
|
||||||
return None
|
def sanitize(connection: Connection, model_class) -> int:
|
||||||
|
|
||||||
|
return 0
|
||||||
Loading…
Add table
Add a link
Reference in a new issue