summaryrefslogtreecommitdiff
path: root/db_actions.py
diff options
context:
space:
mode:
authorMarkku Okkonen <mokkonen@local>2023-11-10 15:06:45 +0200
committerMarkku Okkonen <mokkonen@local>2023-11-10 15:06:45 +0200
commit8128427a1d667f62ce4888fe2921ca12b6823deb (patch)
treed24c9f58e3980bc49ca45fece0cceffe9baa3b5d /db_actions.py
parentfcb6ffc64fa8cf3b57d5960fb75d10107b346e46 (diff)
Database basics and initial workings of nick registeration
Diffstat (limited to 'db_actions.py')
-rw-r--r--db_actions.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/db_actions.py b/db_actions.py
new file mode 100644
index 0000000..5ee2f16
--- /dev/null
+++ b/db_actions.py
@@ -0,0 +1,34 @@
+from time import time
+
+from flask import Flask
+from flask_sqlalchemy import SQLAlchemy
+from sqlalchemy.orm import DeclarativeBase
+from sqlalchemy.sql import text
+
+class Base(DeclarativeBase):
+ pass
+
+db = SQLAlchemy(model_class=Base)
+
+def user_new(nick):
+ sql = "INSERT \
+ INTO users (nick, created) \
+ VALUES (:nick, :created) \
+ RETURNING id ;"
+ result = db.session.execute(
+ text(sql), { "nick":nick, "created":int(time()) }
+ )
+ db.session.commit()
+ return result.fetchone()[0]
+
+def user_get_nick(id):
+ sql = "SELECT nick \
+ FROM users \
+ WHERE id=(:id);"
+ return db.session.execute(text(sql), { "id":id }).fetchone()[0]
+
+def user_exists(nick):
+ sql = "SELECT COUNT(id) \
+ FROM users \
+ WHERE nick=(:nick);"
+ return db.session.execute(text(sql), { "nick":nick }).scalar()