-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsql.py
55 lines (44 loc) · 1.58 KB
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# Name : broadcast-bot [ Telegram ]
# Repo : https://github.com/m4mallu/broadcast-bot
# Author : Renjith Mangal [ https://t.me/space4renjith ]
# Licence : GPL-3
import os
import threading
from sqlalchemy import create_engine
from sqlalchemy import Column, TEXT, Numeric
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
def start() -> scoped_session:
engine = create_engine("postgres://ykfmxfem:Pm3VDsQXsWt8c0hbaPUYdM9bGNgtMPjl@fanny.db.elephantsql.com/ykfmxfem", client_encoding="utf8")
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))
BASE = declarative_base()
SESSION = start()
INSERTION_LOCK = threading.RLock()
class Broadcast(BASE):
__tablename__ = "broadcast"
id = Column(Numeric, primary_key=True)
user_name = Column(TEXT)
def __init__(self, id, user_name):
self.id = id
self.user_name = user_name
Broadcast.__table__.create(checkfirst=True)
# ------------------------------------ Add user details ----------------------------- #
async def add_user(id, user_name):
with INSERTION_LOCK:
msg = SESSION.query(Broadcast).get(id)
if not msg:
usr = Broadcast(id, user_name)
SESSION.add(usr)
SESSION.commit()
else:
pass
async def query_msg():
try:
query = SESSION.query(Broadcast.id).order_by(Broadcast.id)
return query
finally:
SESSION.close()