Move more database stuff out of the main application file

This commit is contained in:
Daniel Schulte 2020-11-22 21:29:45 +01:00
parent e47089325d
commit 87d85c60f3
5 changed files with 59 additions and 22 deletions

14
db.cpp
View File

@ -17,6 +17,20 @@ std::string get_string(sqlite3_stmt *row, int col)
return std::string((char*)sqlite3_column_text(row, col));
}
void db_check_schema();
void db_init(const std::string &filename)
{
SC(sqlite3_open(filename.c_str(), &db));
db_check_schema();
}
void db_shutdown()
{
sqlite3_close(db);
db = nullptr;
}
void db_check_schema() {
bool settings_table_found = false;

3
db.h
View File

@ -16,7 +16,8 @@ public:
};
std::string get_string(sqlite3_stmt *row, int col);
void db_check_schema();
void db_init(const std::string &filename);
void db_shutdown();
std::string db_get_setting(const std::string &key);
void db_set_setting(const std::string &key, const std::string &value);

View File

@ -134,21 +134,15 @@ void load_videos_for_channel(const std::string &channelId, bool force=false)
{
if(videos.find(channelId) != videos.end() && !force)
return;
std::vector<Video> &channelVideos = videos[channelId];
channelVideos.clear();
channelVideos = Video::get_all_for_channel(channelId);
for(Video &video: channelVideos) {
video.tui_title_width = string_width(video.title);
}
if(channels[*selected_channel].id == channelId)
selected_video = 0;
sqlite3_stmt *query;
SC(sqlite3_prepare_v2(db, "SELECT * FROM videos WHERE channelId=?1 ORDER BY published DESC;", -1, &query, nullptr));
SC(sqlite3_bind_text(query, 1, channelId.c_str(), -1, SQLITE_TRANSIENT));
while(sqlite3_step(query) == SQLITE_ROW) {
Video video(query);
video.tui_title_width = string_width(video.title);
channelVideos.push_back(video);
}
SC(sqlite3_finalize(query));
}
void fetch_videos_for_channel(Channel &ch, bool name_in_title=false)
@ -447,16 +441,10 @@ int main()
database_filename = config["database"];
}
SC(sqlite3_open(database_filename.c_str(), &db));
db_check_schema();
sqlite3_stmt *query;
sqlite3_prepare_v2(db, "SELECT * FROM channels;", -1, &query, nullptr);
while(sqlite3_step(query) == SQLITE_ROW) {
Channel channel(query);
db_init(database_filename);
for(Channel &channel: Channel::get_all(db)) {
add_channel_to_list(channel);
}
sqlite3_finalize(query);
if(channels.size())
select_channel_by_index(0);
@ -503,7 +491,7 @@ int main()
} while (!exit);
tp_shutdown();
sqlite3_close(db);
db_shutdown();
curl_global_cleanup();
return 0;

31
yt.cpp
View File

@ -104,6 +104,20 @@ Channel Channel::add(sqlite3 *db, const std::string &selector, const std::string
return Channel(channel_id, channel_name);
}
std::vector<Channel> Channel::get_all(sqlite3 *db)
{
std::vector<Channel> channels;
sqlite3_stmt *query;
SC(sqlite3_prepare_v2(db, "SELECT * FROM channels;", -1, &query, nullptr));
while(sqlite3_step(query) == SQLITE_ROW) {
channels.emplace_back(query);
}
SC(sqlite3_finalize(query));
return channels;
}
std::string Channel::upload_playlist() const
{
return "UU" + id.substr(2);
@ -256,3 +270,20 @@ void Video::set_flag(sqlite3 *db, VideoFlag flag, bool value)
SC(sqlite3_step(query));
SC(sqlite3_finalize(query));
}
std::vector<Video> Video::get_all_for_channel(const std::string &channel_id)
{
std::vector<Video> videos;
sqlite3_stmt *query;
SC(sqlite3_prepare_v2(db, "SELECT * FROM videos WHERE channelId=?1 ORDER BY published DESC;", -1, &query, nullptr));
SC(sqlite3_bind_text(query, 1, channel_id.c_str(), -1, SQLITE_TRANSIENT));
while(sqlite3_step(query) == SQLITE_ROW) {
videos.emplace_back(query);
Video video(query);
}
SC(sqlite3_finalize(query));
return videos;
}

3
yt.h
View File

@ -3,6 +3,7 @@
#include <map>
#include <optional>
#include <string>
#include <vector>
class sqlite3;
class sqlite3_stmt;
@ -21,6 +22,7 @@ public:
Channel(sqlite3_stmt *row);
static Channel add(sqlite3 *db, const std::string &selector, const std::string &value);
static std::vector<Channel> get_all(sqlite3 *db);
std::string upload_playlist() const;
void fetch_new_videos(sqlite3 *db, progress_info *info=nullptr, std::optional<std::string> after={}, std::optional<int> max_count={});
@ -50,6 +52,7 @@ struct Video
Video(sqlite3_stmt *row);
void set_flag(sqlite3 *db, VideoFlag flag, bool value=true);
static std::vector<Video> get_all_for_channel(const std::string &channel_id);
size_t tui_title_width;
};