Switching to C++ #60
@@ -13,6 +13,7 @@ set(SOURCES
|
||||
src/database/coverArtRepository.cpp
|
||||
src/database/songRepository.cpp
|
||||
src/dto/loginResultDto.hpp
|
||||
src/dto/songDto.hpp
|
||||
src/main.cpp
|
||||
src/managers/coverArtManager.cpp
|
||||
src/managers/directory_manager.cpp
|
||||
|
||||
@@ -15,16 +15,14 @@ class songRepository : public base_repository
|
||||
public:
|
||||
songRepository(const std::string&);
|
||||
|
||||
std::vector<Song> retrieveRecords();
|
||||
|
||||
Song retrieveRecord(Song&, songFilter);
|
||||
|
||||
void saveRecord(const Song&);
|
||||
private:
|
||||
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*);
|
||||
};
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ Cover coverArtRepository::retrieveRecord(Cover& cov, coverFilter filter = coverF
|
||||
}
|
||||
|
||||
const std::string query = qry.str();
|
||||
std::cout << query << std::endl;
|
||||
auto results = perform_mysql_query(conn, query);
|
||||
std::cout << "the query has been performed" << std::endl;
|
||||
|
||||
|
||||
+116
-64
@@ -5,37 +5,71 @@
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
|
||||
#include "types/songFilter.h"
|
||||
|
||||
songRepository::songRepository(const std::string& path) : base_repository(path)
|
||||
{ }
|
||||
|
||||
Song songRepository::retrieveRecord(Song& song, songFilter)
|
||||
std::vector<Song> songRepository::retrieveRecords()
|
||||
{
|
||||
auto conn = setup_mysql_connection();
|
||||
const std::string query = "SELECT * FROM Song";
|
||||
|
||||
auto results = perform_mysql_query(conn, query);
|
||||
auto songs = parseRecords(results);
|
||||
|
||||
mysql_close(conn);
|
||||
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
Song songRepository::retrieveRecord(Song& song, songFilter filter)
|
||||
{
|
||||
std::stringstream qry;
|
||||
auto conn = setup_mysql_connection();
|
||||
qry << "SELECT * FROM Song WHERE ";
|
||||
|
||||
std::unique_ptr<char*> param;
|
||||
switch (filter) {
|
||||
case songFilter::id:
|
||||
qry << "SongId = " << song.id;
|
||||
break;
|
||||
case songFilter::title:
|
||||
param = std::make_unique<char*>(new char[song.title.size()]);
|
||||
mysql_real_escape_string(conn, *param, song.title.c_str(), song.title.size());
|
||||
std::cout << *param << std::endl;
|
||||
qry << "Title = '" << *param << "'";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const std::string query = qry.str();
|
||||
auto results = perform_mysql_query(conn, query);
|
||||
std::cout << "the query has been performed" << std::endl;
|
||||
|
||||
song = parseRecord(results);
|
||||
|
||||
mysql_close(conn);
|
||||
std::cout << "done" << std::endl;
|
||||
|
||||
return song;
|
||||
}
|
||||
|
||||
void songRepository::saveRecord(const Song& song)
|
||||
{
|
||||
// TODO: Yeeesh! Clean this shit up!
|
||||
auto conn = setup_mysql_connection();
|
||||
auto status = 0;
|
||||
|
||||
MYSQL_STMT *stmt;
|
||||
stmt = mysql_stmt_init(conn);
|
||||
MYSQL_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;
|
||||
@@ -88,67 +122,85 @@ void songRepository::saveRecord(const Song& song)
|
||||
|
||||
std::vector<Song> songRepository::parseRecords(MYSQL_RES* results)
|
||||
{
|
||||
std::vector<Song> songs;
|
||||
auto fieldNum = mysql_num_fields(results);
|
||||
auto numRows = mysql_num_rows(results);
|
||||
|
||||
std::vector<Song> songs;
|
||||
songs.reserve(numRows);
|
||||
|
||||
for (MYSQL_ROW row = nullptr; (row = mysql_fetch_row(results)) != nullptr; ) {
|
||||
Song song;
|
||||
|
||||
for (auto i = 0; i != fieldNum; ++i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
song.id = std::stoi(row[i]);
|
||||
break;
|
||||
case 1:
|
||||
song.title = row[i];
|
||||
break;
|
||||
case 2:
|
||||
song.artist= row[i];
|
||||
break;
|
||||
case 3:
|
||||
song.album = row[i];
|
||||
break;
|
||||
case 4:
|
||||
song.genre = row[i];
|
||||
break;
|
||||
case 5:
|
||||
song.year = std::stoi(row[i]);
|
||||
break;
|
||||
case 6:
|
||||
song.duration = std::stoi(row[i]);
|
||||
break;
|
||||
case 7:
|
||||
song.coverArtId = std::stoi(row[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
songs.push_back(song);
|
||||
}
|
||||
|
||||
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 song;
|
||||
auto fieldNum = mysql_num_fields(results);
|
||||
|
||||
MYSQL_ROW row = mysql_fetch_row(results);
|
||||
|
||||
for (auto i = 0; i != fieldNum; ++i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
song.id = std::stoi(row[i]);
|
||||
break;
|
||||
case 1:
|
||||
song.title = row[i];
|
||||
break;
|
||||
case 2:
|
||||
song.artist= row[i];
|
||||
break;
|
||||
case 3:
|
||||
song.album = row[i];
|
||||
break;
|
||||
case 4:
|
||||
song.genre = row[i];
|
||||
break;
|
||||
case 5:
|
||||
song.year = std::stoi(row[i]);
|
||||
break;
|
||||
case 6:
|
||||
song.duration = std::stoi(row[i]);
|
||||
break;
|
||||
case 7:
|
||||
song.coverArtId = std::stoi(row[i]);
|
||||
}
|
||||
}
|
||||
std::cout << "done parsing record" << std::endl;
|
||||
|
||||
return song;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef SONGDTO_H_
|
||||
#define SONGDTO_H_
|
||||
|
||||
#include "oatpp/core/data/mapping/type/Object.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
class songDto : public oatpp::data::mapping::type::Object
|
||||
{
|
||||
DTO_INIT(songDto, Object)
|
||||
|
||||
DTO_FIELD(Int32, id);
|
||||
DTO_FIELD(String, title);
|
||||
DTO_FIELD(String, artist);
|
||||
DTO_FIELD(String, album);
|
||||
DTO_FIELD(String, genre);
|
||||
DTO_FIELD(Int32, year);
|
||||
DTO_FIELD(Int32, duration);
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif
|
||||
@@ -25,11 +25,9 @@ void song_manager::saveSong(Song& song)
|
||||
auto data = std::move(song.data);
|
||||
song = meta.retrieve_metadata(song.songPath);
|
||||
song.data = std::move(data);
|
||||
printSong(song);
|
||||
|
||||
coverArtManager covMgr(exe_path);
|
||||
auto coverRootPath = directory_manager::pathConfigContent(exe_path)["cover_root_path"].get<std::string>();
|
||||
//const nlohmann::json databaseConfig = directory_manager::databaseConfigContent(exe_path);
|
||||
|
||||
auto stockCoverPath = directory_manager::configPath(exe_path);
|
||||
stockCoverPath.append("/CoverArt.png");
|
||||
|
||||
Reference in New Issue
Block a user