Skip to content

Commit

Permalink
Initial new raw sql with sqlite_modern_cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Oct 27, 2021
1 parent a917d15 commit 16fb231
Show file tree
Hide file tree
Showing 56 changed files with 1,727 additions and 267,548 deletions.
31 changes: 10 additions & 21 deletions lib/include/lib/cache/dbcache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

#include "lib/cache.hpp"
#include "lib/cache/jsoncache.hpp"
#include "lib/db/tables.hpp"
#include "lib/db/types.hpp"

#include "lib/paths/paths.hpp"
#include "lib/stopwatch.hpp"

namespace orm = sqlite_orm;
#include "thirdparty/sqlite_modern_cpp.h"

namespace lib
{
Expand Down Expand Up @@ -41,25 +39,16 @@ namespace lib
auto get_all_crashes() const -> std::vector<lib::crash_info> override;

private:
lib::db::storage storage;
sqlite::database db;

static auto make_storage(const lib::paths &paths) -> lib::db::storage;
void make_storage();

// template<typename T>
// auto get_by_id(const std::string &id) -> T
// {
// try
// {
// auto items = storage.get_all<lib::spt::entity>(orm::where(orm
// ::c(&lib::spt::entity::id) == "0"));
// }
// catch (const std::system_error &e)
// {
// lib::log::dev("{} not found in cache: {}",
// id, e.what());
// }
//
// return T();
// }
template<typename T>
auto sql(const std::string &query) -> T
{
T result;
db << query >> result;
return result;
}
};
}
20 changes: 0 additions & 20 deletions lib/include/lib/db/image.hpp

This file was deleted.

48 changes: 0 additions & 48 deletions lib/include/lib/db/tables.hpp

This file was deleted.

48 changes: 0 additions & 48 deletions lib/include/lib/db/types.hpp

This file was deleted.

97 changes: 60 additions & 37 deletions lib/src/cache/dbcache.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,82 @@
#include "lib/cache/dbcache.hpp"

lib::db_cache::db_cache(const lib::paths &paths)
: storage(make_storage(paths))
: db(paths.cache() / "cache.db")
{
lib::stopwatch stopwatch;
stopwatch.start();

storage.sync_schema();
make_storage();

stopwatch.stop();
lib::log::dev("Synced schema in {} ms",
lib::log::debug("Synced schema in {} ms",
stopwatch.elapsed<lib::stopwatch::ms, long>());
}

auto lib::db_cache::make_storage(const lib::paths &paths) -> lib::db::storage
void lib::db_cache::make_storage()
{
// Spotify entities
//region Metadata

auto albums = orm::make_table<lib::spt::album>("albums",
orm::make_column("id", &lib::spt::album::id, orm::primary_key()),
orm::make_column("name", &lib::spt::album::name),
orm::make_column("album_group", &lib::spt::album::album_group),
orm::make_column("image", &lib::spt::album::image),
orm::make_column("artist", &lib::spt::album::artist),
orm::make_column("release_date", &lib::spt::album::release_date));
db << "create table if not exists metadata (version int)";
if (sql<int>("select count(*) from metadata") == 0)
{
db << "insert into metadata (version) values (?)"
<< 1;
}

auto artists = orm::make_table<lib::spt::artist>("artists",
orm::make_column("id", &lib::spt::artist::id, orm::primary_key()),
orm::make_column("name", &lib::spt::artist::name),
orm::make_column("followers", &lib::spt::artist::followers),
orm::make_column("popularity", &lib::spt::artist::popularity),
orm::make_column("image", &lib::spt::artist::image));
//endregion

auto tracks = orm::make_table<lib::spt::track>("tracks",
orm::make_column("id", &lib::spt::track::id, orm::primary_key()),
orm::make_column("name", &lib::spt::track::name),
orm::make_column("is_local", &lib::spt::track::is_local),
orm::make_column("is_playable", &lib::spt::track::is_playable),
orm::make_column("duration", &lib::spt::track::duration),
orm::make_column("added_at", &lib::spt::track::added_at),
orm::make_column("image", &lib::spt::track::image));
//region Generic

// Other
db << "create table if not exists image"
"(url text primary key not null,"
"width integer not null,"
"height integer not null,"
"data blob null)";

auto images = orm::make_table<lib::db::image>("images",
orm::make_column("url", &lib::db::image::url, orm::primary_key()),
orm::make_column("data", &lib::db::image::data));
//endregion

return orm::make_storage(paths.cache() / "cache.db",
albums, artists, tracks, images);
//region Artist

db << "create table if not exists artist"
"(id text primary key not null,"
"name text not null,"
"href text not null,"
"uri text not null,"
"followers integer not null,"
"popularity integer not null)";

db << "create table if not exists artist_external_url"
"(artist_id text not null,"
"url text not null,"
"description text not null,"
"foreign key (artist_id) references artist (id))";

db << "create table if not exists artist_genre"
"(artist_id text not null,"
"genre text not null,"
"foreign key (artist_id) references artist (id))";

db << "create table if not exists artist_image"
"(artist_id text not null,"
"image_url text not null,"
"foreign key (artist_id) references artist (id),"
"foreign key (image_url) references image (url))";

//endregion

//region Album

db << "create table if not exists album"
"(id text primary key not null,"
"name text not null,"
"album_group integer not null,"
"image text not null,"
"artist_id text not null,"
"release_date text not null,"
"foreign key (artist_id) references artist (id))";

//endregion
}

void lib::db_cache::from_json(const ::lib::json_cache &json_cache)
Expand All @@ -68,11 +96,6 @@ auto lib::db_cache::get_album_image(const std::string &url) -> std::vector<unsig

void lib::db_cache::set_album_image(const std::string &url, const std::vector<unsigned char> &data)
{
lib::db::image image;
image.url = url;
image.data = std::vector<char>(data.cbegin(), data.cend());

storage.replace(image);
}

auto lib::db_cache::get_playlists() const -> std::vector<lib::spt::playlist>
Expand Down
31 changes: 24 additions & 7 deletions lib/thirdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
project(spotify-qt-lib-third-party)

# SQLiteCpp
add_subdirectory(SQLiteCpp)
# Set as library
add_library(${PROJECT_NAME} STATIC)

# Link
add_library(spotify-qt-lib-third-party STATIC)
target_link_libraries(spotify-qt-lib-third-party
SQLiteCpp
sqlite3)
# SQLite cache support
if (NOT DEFINED USE_DB_CACHE)
set(USE_DB_CACHE ON)
endif ()

# TODO: On error, use bundled SQLite instead
if (USE_DB_CACHE)
find_package(PkgConfig QUIET)
if (PkgConfig_FOUND)
pkg_check_modules(SQLITE sqlite3 QUIET)
if (SQLITE_FOUND)
target_link_directories(${PROJECT_NAME} PRIVATE "${SQLITE_LIBRARY_DIRS}")
target_include_directories(${PROJECT_NAME} PRIVATE "${SQLITE_INCLUDE_DIRS}")
target_link_libraries(${PROJECT_NAME} PRIVATE "${SQLITE_LIBRARIES}")
message(STATUS "Using SQLite ${SQLITE_VERSION} (system)")
else ()
message(WARNING "SQLite error: sqlite3 not found")
endif ()
else ()
message(WARNING "SQLite error: pkg-config not found")
endif ()
endif ()
14 changes: 0 additions & 14 deletions lib/thirdparty/SQLiteCpp/.editorconfig

This file was deleted.

7 changes: 0 additions & 7 deletions lib/thirdparty/SQLiteCpp/.gitbugtraq

This file was deleted.

Loading

0 comments on commit 16fb231

Please sign in to comment.