Skip to content
Snippets Groups Projects
Commit 59cbe4ba authored by Robert Butora's avatar Robert Butora
Browse files

vlkb-obscore: simplifies syntax of the groups-argument (converts to sql internally)

parent 8bf8d51e
No related branches found
No related tags found
No related merge requests found
......@@ -360,9 +360,12 @@ void database::dbModifyGroups(int sid, const string groups,
LOG_trace(__func__);
DbConn db(db_uri, db_schema);
Survey surv = db.querySurveyAttributes(sid);
vector<string> cmdModGroups{
"UPDATE obscore SET groups = '{" + groups + "}' WHERE (policy = 'PRIV') AND (obs_publisher_did IN (SELECT CONCAT('" + (obscore_publisher + "?") + "',pubdid) FROM headers WHERE survey_id =" + to_string(sid) + "))"};
vector<string> cmdModGroups
{
"UPDATE obscore SET groups = ARRAY["+ groups +"]::TEXT[] WHERE (policy = 'PRIV') AND (obs_collection = '"+ surv.getObsCollection() +"') AND (obs_title = '"+ surv.getObsTitle() +"')"
};
db.dbExecCmds(cmdModGroups);
}
......
......@@ -303,12 +303,63 @@ int cmd_dbAdd(int argc, char * argv[])
return rc;
}
// convert argv[] bash arg to sql-TEXT[] string
// segment fault if str is empty or contains _only_ delimiter(s): "," ",," ",,,,,,,,"
std::vector<std::string> split_by_delimiter(std::string_view str, std::string_view delimiter)
{
if (delimiter.empty()) return {std::string(str)};
// handle empty delimiters explicitly so we can't fall into an infinite loop
std::vector<std::string> tokens;
std::size_t cursor = 0;
std::size_t segment_start = cursor;
while ((cursor = str.find(delimiter, cursor)) != std::string_view::npos)
{
if (segment_start != cursor) tokens.emplace_back(str.substr(segment_start, cursor - segment_start));
// don't emplace empty tokens in case of leading/trailing/repeated delimiters
cursor += delimiter.size();
segment_start = cursor;
}
if (segment_start != str.size()) tokens.emplace_back(str.substr(segment_start));
// 'cursor' is now at 'npos', so we compare to the size instead
return tokens;
}
inline std::string join_strings(std::vector<std::string> arr, std::string sep, std::string wrap)
{
std::string out = wrap + arr[0] + wrap;
for(unsigned int i = 1; i < arr.size(); i++) {
out += sep + wrap + arr[i] + wrap;
}
return out;
}
const string EMPTY_STRING;
string to_sql_text_vec(string arg)
{
if(!arg.empty())
{
vector<string> arg_split = split_by_delimiter(arg, string{','} );
if(!arg_split.empty())
{
//for (const auto& elem: arg_split) cout << elem << ' ';
//cout << '<' << endl;
string sql_text_arr{join_strings(arg_split,",","'")};
OUT_STREAM << "SQL ARRAY[" << sql_text_arr << "]"<< endl;
return sql_text_arr;
}
}
OUT_STREAM << "Cleared all groups for the given datasets." << endl;
return EMPTY_STRING;
}
int cmd_dbModGroups(int argc, char * argv[])
{
// FIXME groups should be cmd arg and mandatory of policy PRIVATE and empy null if policu PUBLIC
// e.g. exit with error if dbAddSurvey gets sid of PRIVATE but no groups arg supplied
string groups;
int sid_from, sid_to;
int rc;
......@@ -327,15 +378,26 @@ int cmd_dbModGroups(int argc, char * argv[])
break;
default:
cout << "Usage: dbmodgroups <groups> <SID-from> [SID-to]" << endl
<< "modifies list of groups which can access PRIVATE surveys" << endl;
cout << endl
<< "Modifies list of groups which are allowed to access proprietary datasets given by SID." << endl
<< "SID (survey-id) is in the metadata file in the root of FITS-file store (typically /srv/datasets/<metadata>.csv)."
<< "Empty groups (\"\") arg clears the groups list in the database." << endl
<< "Usage:" << endl
<< " dbmodgroups <groups> <SID-from> [SID-to]" << endl
<< endl
<< "Examples (groups are comma separated):" << endl
<< " vlkb-obscore datasets.conf dbmodgroups \"Group A,Group B\" 3 6" << endl
<< " vlkb-obscore datasets.conf dbmodgroups \"\" 3 6" << endl;
rc = vlkb::EXIT_WITH_USAGE;
return rc;
}
try
{
string sql_groups_vec{to_sql_text_vec(groups)};
for(int i=sid_from; i<=sid_to; i++)
database::dbModifyGroups(i, groups,
database::dbModifyGroups(i, sql_groups_vec,
vlkb::conf.getObsCorePublisher(), vlkb::conf.getDbUri(WITH_PASSWORD), vlkb::conf.getDbSchema());
rc = 0;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment