summaryrefslogtreecommitdiff
path: root/db/user.py
diff options
context:
space:
mode:
Diffstat (limited to 'db/user.py')
-rw-r--r--db/user.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/db/user.py b/db/user.py
new file mode 100644
index 0000000..a129a13
--- /dev/null
+++ b/db/user.py
@@ -0,0 +1,32 @@
+from time import time
+
+from sqlalchemy.sql import text
+
+class DBUser:
+ def __init__(self, db):
+ self.db = db
+
+ def get_nick(self, id):
+ sql = "SELECT nick \
+ FROM users \
+ WHERE id=(:id);"
+ result = self.db.session.execute(text(sql), { "id":id }).fetchone()
+ return result[0] if result else result
+
+ def new(self, nick):
+ sql = "INSERT \
+ INTO users (nick, created) \
+ VALUES (:nick, :created) \
+ RETURNING id ;"
+ result = self.db.session.execute(
+ text(sql), { "nick":nick, "created":int(time()) }
+ )
+ self.db.session.commit()
+ return result.fetchone()[0]
+
+ def exists(self, nick):
+ sql = "SELECT COUNT(id) \
+ FROM users \
+ WHERE nick=(:nick);"
+ return self.db.session.execute(text(sql), { "nick":nick }).scalar()
+