|
@@ -4,21 +4,21 @@ namespace db {
|
|
|
// Объект работы с БД
|
|
|
static sqlite3* DB;
|
|
|
|
|
|
- void database_init(std::string filename) {
|
|
|
+ void databaseInit(std::string filename) {
|
|
|
db_path = filename;
|
|
|
}
|
|
|
|
|
|
- int database_open() {
|
|
|
+ int databaseOpen() {
|
|
|
return sqlite3_open(db_path.c_str(), &DB);
|
|
|
}
|
|
|
|
|
|
- void database_close() {
|
|
|
+ void databaseClose() {
|
|
|
sqlite3_close(DB);
|
|
|
}
|
|
|
|
|
|
// Для каждого запроса на создание таблиц из массива quieries мы вызываем
|
|
|
// sqlite3_exec. Если запрос провалится, выводим сообщение об ошибке
|
|
|
- void create_tables() {
|
|
|
+ void createTables() {
|
|
|
char* message_error;
|
|
|
int status;
|
|
|
std::array<std::string, 2> queries = {
|
|
@@ -40,7 +40,7 @@ namespace db {
|
|
|
)"
|
|
|
};
|
|
|
|
|
|
- database_open();
|
|
|
+ databaseOpen();
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
status = sqlite3_exec(DB, queries[i].c_str(), NULL, 0, &message_error);
|
|
|
|
|
@@ -50,23 +50,23 @@ namespace db {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- database_close();
|
|
|
+ databaseClose();
|
|
|
}
|
|
|
|
|
|
- bool get_video_by_ytid(std::string ytid, video* output) {
|
|
|
+ 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"
|
|
|
+ "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=?";
|
|
|
- database_open();
|
|
|
+ databaseOpen();
|
|
|
author video_author;
|
|
|
|
|
|
// Подготовка
|
|
|
sqlite3_prepare_v2(DB, query.c_str(), query.length(), &stmt, nullptr);
|
|
|
|
|
|
// Привязка данных
|
|
|
- sqlite3_bind_text(stmt, 1, ytid.c_str(), ytid.length(), SQLITE_STATIC);
|
|
|
+ sqlite3_bind_text(stmt, 1, yt_id.c_str(), yt_id.length(), SQLITE_STATIC);
|
|
|
|
|
|
// Выполнение
|
|
|
int result = sqlite3_step(stmt);
|
|
@@ -96,11 +96,85 @@ namespace db {
|
|
|
output->description = Glib::ustring(reinterpret_cast<const char*>(
|
|
|
sqlite3_column_text(stmt, 3)
|
|
|
));
|
|
|
+ output->published_at = std::string(reinterpret_cast<const char*>(
|
|
|
+ sqlite3_column_text(stmt, 7)
|
|
|
+ ));
|
|
|
output->author_obj = video_author;
|
|
|
|
|
|
// Завершение
|
|
|
sqlite3_finalize(stmt);
|
|
|
- database_close();
|
|
|
+ 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<const char*>(
|
|
|
+ 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();
|
|
|
+ }
|
|
|
}
|