fix for duplicate records being saved. I'm going to continually monitor it, some minor changes might need to be made. The changes are changing to prepared statements for queries
This commit is contained in:
@@ -24,29 +24,45 @@ model::Album database::AlbumRepository::retrieveRecord(model::Album& album, type
|
||||
std::cout << "retrieving album record" << std::endl;
|
||||
std::stringstream qry;
|
||||
auto conn = setupMysqlConnection();
|
||||
auto stmt = mysql_stmt_init(conn);
|
||||
|
||||
MYSQL_BIND params[1];
|
||||
memset(params, 0, sizeof(params));
|
||||
|
||||
qry << "SELECT alb.* FROM Album alb WHERE ";
|
||||
|
||||
std::unique_ptr<char*> param;
|
||||
auto titleLength = album.title.size();
|
||||
switch (filter) {
|
||||
case type::AlbumFilter::id:
|
||||
qry << "alb.AlbumId = " << album.id;
|
||||
|
||||
params[0].buffer_type = MYSQL_TYPE_LONG;
|
||||
params[0].buffer = (char*)&album.id;
|
||||
params[0].length = 0;
|
||||
params[0].is_null = 0;
|
||||
break;
|
||||
case type::AlbumFilter::title:
|
||||
param = std::make_unique<char*>(new char[album.title.size()]);
|
||||
mysql_real_escape_string(conn, *param, album.title.c_str(), album.title.size());
|
||||
qry << "alb.Title = '" << *param << "'";
|
||||
qry << "alb.Title = ?";
|
||||
|
||||
params[0].buffer_type = MYSQL_TYPE_STRING;
|
||||
params[0].buffer = (char*)album.title.c_str();
|
||||
params[0].length = &titleLength;
|
||||
params[0].is_null = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
qry << " ORDER BY AlbumId DESC LIMIT 1";
|
||||
qry << " LIMIT 1";
|
||||
|
||||
const std::string query = qry.str();
|
||||
auto results = performMysqlQuery(conn, query);
|
||||
const auto query = qry.str();
|
||||
auto status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
|
||||
status = mysql_stmt_bind_param(stmt, params);
|
||||
status = mysql_stmt_execute(stmt);
|
||||
|
||||
album = parseRecord(results);
|
||||
album = parseRecord(stmt);
|
||||
|
||||
mysql_stmt_close(stmt);
|
||||
mysql_close(conn);
|
||||
std::cout << "done" << std::endl;
|
||||
|
||||
@@ -55,14 +71,6 @@ model::Album database::AlbumRepository::retrieveRecord(model::Album& album, type
|
||||
|
||||
bool database::AlbumRepository::doesAlbumExists(const model::Album& album, type::AlbumFilter filter)
|
||||
{
|
||||
// TODO: continue working on this part.
|
||||
// Reason: there should be a check to see if an album record already exists
|
||||
// at the moment there is no check and every time a song is uploaded
|
||||
// a new album record is created, even for songs that are on the
|
||||
// same album. This should not happen. There should be one album
|
||||
// record for songs on the same album. After fixing this, do the
|
||||
// same for Artist, Genre, and Year records
|
||||
|
||||
auto conn = setupMysqlConnection();
|
||||
auto stmt = mysql_stmt_init(conn);
|
||||
|
||||
@@ -182,8 +190,56 @@ model::Album database::AlbumRepository::parseRecord(MYSQL_RES* results)
|
||||
|
||||
model::Album database::AlbumRepository::parseRecord(MYSQL_STMT *stmt)
|
||||
{
|
||||
// TODO: implement this
|
||||
std::cout << "parsing album record" << std::endl;
|
||||
mysql_stmt_store_result(stmt);
|
||||
auto rows = mysql_stmt_num_rows(stmt);
|
||||
|
||||
model::Album album;
|
||||
auto status = 0;
|
||||
auto time = 0;
|
||||
auto valAmt = 3;
|
||||
unsigned long len[valAmt];
|
||||
my_bool nullRes[valAmt];
|
||||
|
||||
while (status == 0) {
|
||||
if (mysql_stmt_field_count(stmt) > 0) {
|
||||
auto res = mysql_stmt_result_metadata(stmt);
|
||||
auto fields = mysql_fetch_fields(res);
|
||||
auto strLen = 1024;
|
||||
|
||||
MYSQL_BIND val[valAmt];
|
||||
memset(val, 0, sizeof(val));
|
||||
|
||||
char title[strLen];
|
||||
|
||||
val[0].buffer_type = MYSQL_TYPE_LONG;
|
||||
val[0].buffer = (char*)&album.id;
|
||||
val[0].length = &len[0];
|
||||
val[0].is_null = &nullRes[0];
|
||||
|
||||
val[1].buffer_type = MYSQL_TYPE_STRING;
|
||||
val[1].buffer = (char*)title;
|
||||
val[1].buffer_length = strLen;
|
||||
val[1].length = &len[1];
|
||||
val[1].is_null = &nullRes[1];
|
||||
|
||||
val[2].buffer_type = MYSQL_TYPE_LONG;
|
||||
val[2].buffer = (char*)&album.year;
|
||||
val[2].length = &len[2];
|
||||
val[2].is_null = &nullRes[2];
|
||||
|
||||
status = mysql_stmt_bind_result(stmt, val);
|
||||
mysql_stmt_store_result(stmt);
|
||||
|
||||
status = mysql_stmt_fetch(stmt);
|
||||
|
||||
album.title = title;
|
||||
}
|
||||
|
||||
status = mysql_stmt_next_result(stmt);
|
||||
}
|
||||
|
||||
std::cout << "done parsing album record" << std::endl;
|
||||
|
||||
return album;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user