flash / Database /sql /userinfo_sql.py
Karma
Add files via upload
f45efbd
raw
history blame
2.83 kB
"""
MIT License
Copyright (c) 2022 Aʙɪsʜɴᴏᴏ
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import threading
from sqlalchemy import BigInteger, Column, UnicodeText
from Database.sql import BASE, SESSION
class UserInfo(BASE):
__tablename__ = "userinfo"
user_id = Column(BigInteger, primary_key=True)
info = Column(UnicodeText)
def __init__(self, user_id, info):
self.user_id = user_id
self.info = info
def __repr__(self):
return "<ᴜsᴇʀ ɪɴғᴏ %d>" % self.user_id
class UserBio(BASE):
__tablename__ = "userbio"
user_id = Column(BigInteger, primary_key=True)
bio = Column(UnicodeText)
def __init__(self, user_id, bio):
self.user_id = user_id
self.bio = bio
def __repr__(self):
return "<ᴜsᴇʀ ɪɴғᴏ %d>" % self.user_id
UserInfo.__table__.create(checkfirst=True)
UserBio.__table__.create(checkfirst=True)
INSERTION_LOCK = threading.RLock()
def get_user_me_info(user_id):
userinfo = SESSION.query(UserInfo).get(user_id)
SESSION.close()
if userinfo:
return userinfo.info
return None
def set_user_me_info(user_id, info):
with INSERTION_LOCK:
userinfo = SESSION.query(UserInfo).get(user_id)
if userinfo:
userinfo.info = info
else:
userinfo = UserInfo(user_id, info)
SESSION.add(userinfo)
SESSION.commit()
def get_user_bio(user_id):
userbio = SESSION.query(UserBio).get(user_id)
SESSION.close()
if userbio:
return userbio.bio
return None
def set_user_bio(user_id, bio):
with INSERTION_LOCK:
userbio = SESSION.query(UserBio).get(user_id)
if userbio:
userbio.bio = bio
else:
userbio = UserBio(user_id, bio)
SESSION.add(userbio)
SESSION.commit()