#include "database.hpp" namespace db { // Объект работы с БД static sqlite3* DB; void databaseInit(std::string filename) { db_path = filename; } int databaseOpen() { return sqlite3_open(db_path.c_str(), &DB); } void databaseClose() { sqlite3_close(DB); } // Для каждого запроса на создание таблиц из массива quieries мы вызываем // sqlite3_exec. Если запрос провалится, выводим сообщение об ошибке void createTables() { char* message_error; int status; std::array queries = { "CREATE TABLE IF NOT EXISTS \"author\" (\ \"id\" INTEGER,\ \"yt_id\" TEXT,\ \"name\" TEXT,\ PRIMARY KEY(\"id\" AUTOINCREMENT)\ )", "CREATE TABLE IF NOT EXISTS \"video\" (\ \"id\" INTEGER,\ \"yt_id\" TEXT,\ \"title\" TEXT NOT NULL,\ \"description\" TEXT,\ \"author_id\" INTEGER NOT NULL,\ \"published_at\" TEXT NOT NULL,\ PRIMARY KEY(\"id\" AUTOINCREMENT)\ )" }; databaseOpen(); for (int i = 0; i < 2; i++) { status = sqlite3_exec(DB, queries[i].c_str(), NULL, 0, &message_error); if (status != SQLITE_OK) { std::cerr << "Error creating tables" << std::endl; sqlite3_free(message_error); return; } } databaseClose(); } bool getVideoByYTID(std::string yt_id, video* output) { sqlite3_stmt* stmt; std::string query = "SELECT v.id,v.yt_id,v.title,v.description,a.id,a.yt_id,a.name,v.published_at" " FROM video v LEFT JOIN author a ON v.author_id=a.id" " WHERE v.yt_id=?"; databaseOpen(); author video_author; // Подготовка sqlite3_prepare_v2(DB, query.c_str(), query.length(), &stmt, nullptr); // Привязка данных sqlite3_bind_text(stmt, 1, yt_id.c_str(), yt_id.length(), SQLITE_STATIC); // Выполнение int result = sqlite3_step(stmt); if (result == SQLITE_DONE) { // Видео не найдено return false; } // Заполнение данными // unsigned const char* конвертируется в std::string // https://stackoverflow.com/a/804131 video_author.id = sqlite3_column_int(stmt, 4); video_author.yt_id = std::string(reinterpret_cast( sqlite3_column_text(stmt, 5) )); video_author.name = Glib::ustring(reinterpret_cast( sqlite3_column_text(stmt, 6) )); output->id = sqlite3_column_int(stmt, 0); output->yt_id = std::string(reinterpret_cast( sqlite3_column_text(stmt, 1) )); output->title = Glib::ustring(reinterpret_cast( sqlite3_column_text(stmt, 2) )); output->description = Glib::ustring(reinterpret_cast( sqlite3_column_text(stmt, 3) )); output->published_at = std::string(reinterpret_cast( sqlite3_column_text(stmt, 7) )); output->author_obj = video_author; // Завершение sqlite3_finalize(stmt); databaseClose(); return true; } bool getAuthorByYTID(std::string yt_id, author* output) { sqlite3_stmt* stmt; std::string query = "SELECT a.id, a.name" " FROM author a" " WHERE a.yt_id=?"; databaseOpen(); // Подготовка sqlite3_prepare_v2(DB, query.c_str(), query.length(), &stmt, nullptr); // Привязка данных sqlite3_bind_text(stmt, 1, yt_id.c_str(), yt_id.length(), SQLITE_STATIC); // Выполнение int result = sqlite3_step(stmt); if (result == SQLITE_DONE) { // Видео не найдено return false; } // Заполнение данными output->id = sqlite3_column_int(stmt, 0); output->name = std::string(reinterpret_cast( sqlite3_column_text(stmt, 1) )); // Завершение sqlite3_finalize(stmt); databaseClose(); return true; } void addAuthor(author* a) { sqlite3_stmt* stmt; std::string query = "INSERT INTO author (yt_id, name) VALUES(?, ?)"; databaseOpen(); sqlite3_prepare_v2(DB, query.c_str(), query.length(), &stmt, nullptr); sqlite3_bind_text(stmt, 1, a->yt_id.c_str(), a->yt_id.length(), SQLITE_STATIC); sqlite3_bind_text(stmt, 2, a->name.c_str(), a->name.length(), SQLITE_STATIC); sqlite3_step(stmt); a->id = sqlite3_last_insert_rowid(DB); sqlite3_finalize(stmt); databaseClose(); } void addVideo(video* v) { sqlite3_stmt* stmt; std::string query = "INSERT INTO video (yt_id, title, description, author_id, published_at)" " VALUES(?, ?, ?, ?, ?)"; databaseOpen(); sqlite3_prepare_v2(DB, query.c_str(), query.length(), &stmt, nullptr); sqlite3_bind_text(stmt, 1, v->yt_id.c_str(), v->yt_id.length(), SQLITE_STATIC); sqlite3_bind_text(stmt, 2, v->title.c_str(), v->title.length(), SQLITE_STATIC); sqlite3_bind_text(stmt, 3, v->description.c_str(), v->description.length(), SQLITE_STATIC); sqlite3_bind_int(stmt, 4, v->author_obj.id); sqlite3_bind_text(stmt, 5, v->published_at.c_str(), v->published_at.length(), SQLITE_STATIC); sqlite3_step(stmt); v->id = sqlite3_last_insert_rowid(DB); sqlite3_finalize(stmt); databaseClose(); } }