stablediffusionapi / database.py
yusufgundogdu's picture
Update database.py
3bff85a verified
raw
history blame
1.41 kB
import sqlite3
import os
from flask import g
def get_db_path():
"""Veritabanı dosya yolunu belirler"""
if os.path.exists('/data'):
return '/data/users.db'
elif os.path.exists('/home/user_data'):
return '/home/user_data/users.db'
return 'users.db'
def get_db():
"""Veritabanı bağlantısını getirir veya oluşturur"""
if 'db' not in g:
db_path = get_db_path()
os.makedirs(os.path.dirname(db_path), exist_ok=True)
g.db = sqlite3.connect(db_path)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
"""Veritabanı bağlantısını kapatır"""
db = g.pop('db', None)
if db is not None:
db.close()
def create_user(udid, platform):
"""Yeni kullanıcı oluşturur"""
try:
if platform not in ['AND', 'IOS']:
raise ValueError("Platform must be either 'AND' or 'IOS'")
db = get_db()
cursor = db.cursor()
cursor.execute('INSERT INTO users (udid, platform) VALUES (?, ?)', (udid, platform))
db.commit()
cursor.execute('SELECT * FROM users WHERE udid = ?', (udid,))
new_user = cursor.fetchone()
return dict(new_user) if new_user else None
except sqlite3.IntegrityError:
raise ValueError("User with this UDID already exists")
except Exception as e:
db.rollback()
raise e