Commit 0ba1de16 authored by Javinator9889's avatar Javinator9889 🎼

Created class for database managing - files for database creation adapted to Cassandra model

parent abc06a8a
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDataSourceStorage">
<option name="dataSources">
<list>
<State>
<option name="id" value="9b469808-a645-4c18-b66c-ba653aa374e1" />
<option name="name" value="DDL data source" />
<option name="urls">
<array>
<option value="file://$PROJECT_DIR$/db_script_pycharm.ddl" />
</array>
</option>
</State>
</list>
</option>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
class DatabaseOperations:
def __init__(self):
\ No newline at end of file
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
# Singleton class
class DatabaseOperations(object):
__instance = None
def __new__(cls, username: str, password: str):
if DatabaseOperations.__instance is None:
DatabaseOperations.__instance = object.__new__(cls)
auth_provider = PlainTextAuthProvider(username=username, password=password)
cluster = Cluster(auth_provider=auth_provider)
DatabaseOperations.__instance.__session = cluster.connect()
DatabaseOperations.__instance.__createTables()
return DatabaseOperations.__instance
def __createTables(self):
with open("../../Design/db_script.cql", "r") as sql_script:
queries = sql_script.read().splitlines()
for query in queries:
self.__session.execute(query)
def registerNewUser(self, user_id: int, username: str, name: str):
query = """INSERT INTO YouTubeMDApp.User(user_id, username, name) VALUES (%s, %s, %s);"""
self.__session.execute(query, (user_id, username, name))
def registerPreferences(self, user_id: int, audio_quality: str, audio_format: str, os: str,
should_ask_metadata: bool):
query = """
INSERT INTO YouTubeMDApp.preferences(audio_quality, audio_format, os, should_ask_metadata, user_id)
VALUES (%s, %s, %s, %s, %s);
"""
self.__session.execute(query, (audio_quality, audio_format, os, should_ask_metadata, user_id))
def registerStatistics(self, user_id: int, lang: str, downloads: int, last_time_active):
query = """
INSERT INTO YouTubeMDApp.Statistics(lang, downloads, last_time_active, user_id) VALUES (%s, %s, %s, %s);
"""
self.__session.execute(query, (lang, downloads, last_time_active, user_id))
def registerNewSong(self, file_id: str, video_id: str, audio_quality: int, audio_format: int, times_requested: int,
is_metadata_by_user: bool):
query = """
INSERT INTO YouTubeMDApp.music (file_id, video_id, audio_quality, audio_format, times_requested,
is_metadata_by_user) VALUES (%s, %s, %s, %s, %s, %s)
"""
self.__session.execute(query, (file_id, video_id, audio_quality, audio_format, times_requested,
is_metadata_by_user))
CREATE KEYSPACE IF NOT EXISTS YouTubeMDApp WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};
USE YouTubeMDApp;
CREATE TABLE IF NOT EXISTS history (user_id int NOT NULL, file_id varchar NOT NULL);
CREATE TABLE IF NOT EXISTS metadata (title varchar, artist varchar, cover text, duration varchar, music_file_id varchar NOT NULL, PRIMARY KEY (music_file_id));
CREATE TABLE IF NOT EXISTS music (file_id varchar NOT NULL,video_id varchar, audio_quality int, audio_format int, times_requested int, is_metadata_by_user boolean, PRIMARY KEY (file_id));
CREATE TABLE IF NOT EXISTS playlist (playlist_id varchar NOT NULL, number_elements int, times_requested int, PRIMARY KEY (playlist_id));
CREATE TABLE IF NOT EXISTS playlist_has_music (playlist_playlist_id varchar NOT NULL, music_file_id varchar NOT NULL);
CREATE TABLE IF NOT EXISTS preferences (audio_quality varchar, audio_format varchar, os varchar, should_ask_metadata boolean, user_id int NOT NULL, PRIMARY KEY (user_id));
CREATE TABLE IF NOT EXISTS "User" (user_id int NOT NULL, username varchar, name text, PRIMARY KEY (user_id));
CREATE TABLE IF NOT EXISTS Statistics (lang varchar, downloads int, last_time_active datetime, user_id int NOT NULL, PRIMARY KEY(user_id));
\ No newline at end of file
cassandra-driver
telegram
pip
python-telegram-bot
CREATE KEYSPACE IF NOT EXISTS YouTubeMDApp
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}
CREATE TABLE IF NOT EXISTS history (
user_id int NOT NULL,
file_id varchar NOT NULL
);
CREATE TABLE IF NOT EXISTS metadata (
title varchar,
artist varchar,
cover text,
duration varchar,
music_file_id varchar NOT NULL,
PRIMARY KEY (music_file_id)
);
CREATE TABLE IF NOT EXISTS music (
file_id varchar NOT NULL,
video_id varchar,
audio_quality int,
audio_format int,
times_requested int,
is_metadata_by_user boolean,
PRIMARY KEY (file_id)
);
CREATE TABLE IF NOT EXISTS playlist (
playlist_id varchar NOT NULL,
number_elements int,
times_requested int,
PRIMARY KEY (playlist_id)
);
CREATE TABLE IF NOT EXISTS playlist_has_music (
playlist_playlist_id varchar NOT NULL,
music_file_id varchar NOT NULL
);
CREATE TABLE IF NOT EXISTS preferences (
audio_quality varchar,
audio_format varchar,
os varchar,
should_ask_metadata boolean,
user_id int NOT NULL,
PRIMARY KEY (user_id)
);
CREATE TABLE IF NOT EXISTS "User" (
user_id int NOT NULL,
username varchar,
name text,
PRIMARY KEY (user_id)
);
\ No newline at end of file
CREATE KEYSPACE IF NOT EXISTS YouTubeMDApp WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};
USE YouTubeMDApp;
CREATE TABLE IF NOT EXISTS history (user_id int NOT NULL, file_id varchar NOT NULL);
CREATE TABLE IF NOT EXISTS metadata (title varchar, artist varchar, cover text, duration varchar, music_file_id varchar NOT NULL, PRIMARY KEY (music_file_id));
CREATE TABLE IF NOT EXISTS music (file_id varchar NOT NULL,video_id varchar, audio_quality int, audio_format int, times_requested int, is_metadata_by_user boolean, PRIMARY KEY (file_id));
CREATE TABLE IF NOT EXISTS playlist (playlist_id varchar NOT NULL, number_elements int, times_requested int, PRIMARY KEY (playlist_id));
CREATE TABLE IF NOT EXISTS playlist_has_music (playlist_playlist_id varchar NOT NULL, music_file_id varchar NOT NULL);
CREATE TABLE IF NOT EXISTS preferences (audio_quality varchar, audio_format varchar, os varchar, should_ask_metadata boolean, user_id int NOT NULL, PRIMARY KEY (user_id));
CREATE TABLE IF NOT EXISTS "User" (user_id int NOT NULL, username varchar, name text, PRIMARY KEY (user_id));
CREATE TABLE IF NOT EXISTS Statistics (lang varchar, downloads int, last_time_active datetime, user_id int NOT NULL, PRIMARY KEY(user_id));
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment