From d54715ac254d13cc615222fdf91f8936ad8a958a Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 Aug 2019 01:30:13 -0400 Subject: [PATCH] Able to insert song records into the database. Left TODO for clean up --- include/database/songRepository.h | 6 ++ src/database/songRepository.cpp | 121 +++++++++++++++++++++++++++++- src/managers/song_manager.cpp | 5 ++ 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/include/database/songRepository.h b/include/database/songRepository.h index f442c5b..580aa86 100644 --- a/include/database/songRepository.h +++ b/include/database/songRepository.h @@ -1,6 +1,7 @@ #ifndef SONGREPOSITORY_H_ #define SONGREPOSITORY_H_ +#include #include #include @@ -19,6 +20,11 @@ public: void saveRecord(const Song&); private: std::vector parseRecords(MYSQL_RES*); + + std::unique_ptr bindParams(const Song&); + //std::unique_ptr bindParams(const Song&); + //std::shared_ptr bindParams(const Song&); + Song parseRecord(MYSQL_RES*); }; diff --git a/src/database/songRepository.cpp b/src/database/songRepository.cpp index 55a088c..89fa828 100644 --- a/src/database/songRepository.cpp +++ b/src/database/songRepository.cpp @@ -16,7 +16,74 @@ Song songRepository::retrieveRecord(Song& song, songFilter) void songRepository::saveRecord(const Song& song) { - // TODO: pick up here. Save the song record to the database + // TODO: Yeeesh! Clean this shit up! + auto conn = setup_mysql_connection(); + auto status = 0; + + MYSQL_STMT *stmt; + stmt = mysql_stmt_init(conn); + + std::string query = "INSERT INTO Song(Title, Artist, Album, Genre, "; + query.append("Year, Duration, CoverArtId) VALUES(?, ?, ?, ?, ?, ?, ?)"); + + status = mysql_stmt_prepare(stmt, query.c_str(), query.size()); + + + //auto params = bindParams(song); + //MYSQL_BIND *b; + //b = *params.get(); + //auto status = mysql_stmt_bind_param(stmt, b); + // + MYSQL_BIND params[7]; + + memset(params, 0, sizeof(params)); + + params[0].buffer_type = MYSQL_TYPE_STRING; + params[0].buffer = (char*)song.title.c_str(); + auto titleLength = song.title.size(); + params[0].length = &titleLength; + params[0].is_null = 0; + + params[1].buffer_type = MYSQL_TYPE_STRING; + params[1].buffer = (char*)song.artist.c_str(); + auto artistLength = song.artist.size(); + params[1].length = &artistLength; + params[1].is_null = 0; + + params[2].buffer_type = MYSQL_TYPE_STRING; + params[2].buffer = (char*)song.album.c_str(); + auto albumLength = song.album.size(); + params[2].length = &albumLength; + params[2].is_null = 0; + + params[3].buffer_type = MYSQL_TYPE_STRING; + params[3].buffer = (char*)song.genre.c_str(); + auto genreLength = song.genre.size(); + params[3].length = &genreLength; + params[3].is_null = 0; + + params[4].buffer_type = MYSQL_TYPE_LONG; + params[4].buffer = (char*)&song.year; + params[4].length = 0; + params[4].is_null = 0; + + params[5].buffer_type = MYSQL_TYPE_LONG; + params[5].buffer = (char*)&song.duration; + params[5].length = 0; + params[5].is_null = 0; + + params[6].buffer_type = MYSQL_TYPE_LONG; + params[6].buffer = (char*)&song.coverArtId; + params[6].length = 0; + params[6].is_null = 0; + + status = mysql_stmt_bind_param(stmt, params); + status = mysql_stmt_execute(stmt); + + mysql_stmt_close(stmt); + mysql_close(conn); + + std::cout << "done inserting song record" << std::endl; } std::vector songRepository::parseRecords(MYSQL_RES* results) @@ -27,6 +94,58 @@ std::vector songRepository::parseRecords(MYSQL_RES* results) return songs; } +std::unique_ptr songRepository::bindParams(const Song& song) +//std::unique_ptr songRepository::bindParams(const Song& song) +//std::shared_ptr songRepository::bindParams(const Song& song) +{ + MYSQL_BIND params[7]; + + params[0].buffer_type = MYSQL_TYPE_STRING; + params[0].buffer = (char*)song.title.c_str(); + auto titleLength = song.title.size(); + params[0].length = &titleLength; + params[0].is_null = 0; + + params[1].buffer_type = MYSQL_TYPE_STRING; + params[1].buffer = (char*)song.artist.c_str(); + auto artistLength = song.artist.size(); + params[1].length = &artistLength; + params[1].is_null = 0; + + params[2].buffer_type = MYSQL_TYPE_STRING; + params[2].buffer = (char*)song.album.c_str(); + auto albumLength = song.album.size(); + params[2].length = &albumLength; + params[2].is_null = 0; + + params[3].buffer_type = MYSQL_TYPE_STRING; + params[3].buffer = (char*)song.genre.c_str(); + auto genreLength = song.genre.size(); + params[3].length = &genreLength; + params[3].is_null = 0; + + params[4].buffer_type = MYSQL_TYPE_LONG; + params[4].buffer = (char*)&song.year; + params[4].length = 0; + params[4].is_null = 0; + + params[5].buffer_type = MYSQL_TYPE_LONG; + params[5].buffer = (char*)&song.duration; + params[5].length = 0; + params[5].is_null = 0; + + params[6].buffer_type = MYSQL_TYPE_LONG; + params[6].buffer = (char*)&song.coverArtId; + params[6].length = 0; + params[6].is_null = 0; + + std::cout << "binding params" << std::endl; + + //return std::make_unique(params); + return std::make_unique(params); + //return std::make_shared(params); +} + Song songRepository::parseRecord(MYSQL_RES* results) { Song song; diff --git a/src/managers/song_manager.cpp b/src/managers/song_manager.cpp index cce4592..018c25d 100644 --- a/src/managers/song_manager.cpp +++ b/src/managers/song_manager.cpp @@ -37,6 +37,8 @@ void song_manager::saveSong(Song& song) auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath); song.coverArtId = cov.id; + printSong(song); + songRepository songRepo(exe_path); songRepo.saveRecord(song); } @@ -51,6 +53,9 @@ void song_manager::printSong(const Song& song) std::cout << "duration: " << song.duration << std::endl; std::cout << "year: " << song.year << std::endl; std::cout << "song path: " << song.songPath << std::endl; + if (song.coverArtId != 0) { + std::cout << "cover art id: " << song.coverArtId << std::endl; + } } void song_manager::saveSongTemp(Song& song)