Able to insert song records into the database. Left TODO for clean up
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#ifndef SONGREPOSITORY_H_
|
#ifndef SONGREPOSITORY_H_
|
||||||
#define SONGREPOSITORY_H_
|
#define SONGREPOSITORY_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <mysql/mysql.h>
|
#include <mysql/mysql.h>
|
||||||
@@ -19,6 +20,11 @@ public:
|
|||||||
void saveRecord(const Song&);
|
void saveRecord(const Song&);
|
||||||
private:
|
private:
|
||||||
std::vector<Song> parseRecords(MYSQL_RES*);
|
std::vector<Song> parseRecords(MYSQL_RES*);
|
||||||
|
|
||||||
|
std::unique_ptr<MYSQL_BIND*> bindParams(const Song&);
|
||||||
|
//std::unique_ptr<MYSQL_BIND[]> bindParams(const Song&);
|
||||||
|
//std::shared_ptr<MYSQL_BIND> bindParams(const Song&);
|
||||||
|
|
||||||
Song parseRecord(MYSQL_RES*);
|
Song parseRecord(MYSQL_RES*);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,74 @@ Song songRepository::retrieveRecord(Song& song, songFilter)
|
|||||||
|
|
||||||
void songRepository::saveRecord(const Song& song)
|
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<Song> songRepository::parseRecords(MYSQL_RES* results)
|
std::vector<Song> songRepository::parseRecords(MYSQL_RES* results)
|
||||||
@@ -27,6 +94,58 @@ std::vector<Song> songRepository::parseRecords(MYSQL_RES* results)
|
|||||||
return songs;
|
return songs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<MYSQL_BIND*> songRepository::bindParams(const Song& song)
|
||||||
|
//std::unique_ptr<MYSQL_BIND[]> songRepository::bindParams(const Song& song)
|
||||||
|
//std::shared_ptr<MYSQL_BIND> 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<MYSQL_BIND*>(params);
|
||||||
|
return std::make_unique<MYSQL_BIND*>(params);
|
||||||
|
//return std::make_shared<MYSQL_BIND>(params);
|
||||||
|
}
|
||||||
|
|
||||||
Song songRepository::parseRecord(MYSQL_RES* results)
|
Song songRepository::parseRecord(MYSQL_RES* results)
|
||||||
{
|
{
|
||||||
Song song;
|
Song song;
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ void song_manager::saveSong(Song& song)
|
|||||||
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
|
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
|
||||||
song.coverArtId = cov.id;
|
song.coverArtId = cov.id;
|
||||||
|
|
||||||
|
printSong(song);
|
||||||
|
|
||||||
songRepository songRepo(exe_path);
|
songRepository songRepo(exe_path);
|
||||||
songRepo.saveRecord(song);
|
songRepo.saveRecord(song);
|
||||||
}
|
}
|
||||||
@@ -51,6 +53,9 @@ void song_manager::printSong(const Song& song)
|
|||||||
std::cout << "duration: " << song.duration << std::endl;
|
std::cout << "duration: " << song.duration << std::endl;
|
||||||
std::cout << "year: " << song.year << std::endl;
|
std::cout << "year: " << song.year << std::endl;
|
||||||
std::cout << "song path: " << song.songPath << 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)
|
void song_manager::saveSongTemp(Song& song)
|
||||||
|
|||||||
Reference in New Issue
Block a user