18 #include "boost/format.hpp"
19 #include <boost/regex.hpp>
20 #include <boost/tokenizer.hpp>
27 get_column_name(
int j) {
29 for (
int i=0; i<n; ++i) {
37 return std::string(
"COLUMN_NAME_NOT_FOUND_") + ss.str();
40 struct option_element{
42 const char* cpp_macro;
46 option_element queryWideOptionsMap[] = {
54 option_element selectInpOptionsMap[] = {
59 option_element selectInpFunctionMap[] = {
68 format_selected_column(
int columnIndex,
int columnOption) {
69 std::string ret = get_column_name(columnIndex);
70 if (columnOption == 0 || columnOption == 1) {
74 for (
size_t i=0; i<
sizeof(selectInpOptionsMap)/
sizeof(selectInpOptionsMap[0]); ++i) {
75 if (columnOption == selectInpOptionsMap[i].key) {
76 ret = std::string(selectInpOptionsMap[i].
token) +
"(" + ret +
")";
80 for (
size_t i=0; i<
sizeof(selectInpFunctionMap)/
sizeof(selectInpFunctionMap[0]); ++i) {
81 if (columnOption == selectInpFunctionMap[i].key) {
82 ret = std::string(selectInpFunctionMap[i].
token) +
"(" + ret +
")";
89 ret = std::string(
"combo_func_") + ss.str() +
"(" + ret +
")";
93 template <
typename OutStream>
95 insert_genquery_inp_into_stream(
const genQueryInp_t *genQueryInp, OutStream& f) {
96 f <<
"maxRows: " << genQueryInp->
maxRows <<
" continueInx: " << genQueryInp->
continueInx <<
" rowOffset: " << genQueryInp->
rowOffset <<
'\n';
97 f <<
"options: " << genQueryInp->
options;
98 for (
size_t i=0; i<
sizeof(queryWideOptionsMap)/
sizeof(queryWideOptionsMap[0]); ++i) {
99 if (genQueryInp->
options & queryWideOptionsMap[i].key) {
100 f <<
" " << queryWideOptionsMap[i].cpp_macro;
105 f <<
"selectInp.len: " << genQueryInp->
selectInp.
len <<
'\n';
109 for (
size_t j=0; j<
sizeof(selectInpOptionsMap)/
sizeof(selectInpOptionsMap[0]); ++j) {
110 if (genQueryInp->
selectInp.
value[i] & selectInpOptionsMap[j].key) {
111 f <<
" " << selectInpOptionsMap[j].cpp_macro;
114 for (
size_t j=0; j<
sizeof(selectInpFunctionMap)/
sizeof(selectInpFunctionMap[0]); ++j) {
115 if ((genQueryInp->
selectInp.
value[i] & 0x7) == selectInpFunctionMap[j].key) {
116 f <<
" " << selectInpFunctionMap[j].cpp_macro;
122 f <<
"sqlCondInp.len: " << genQueryInp->
sqlCondInp.
len <<
'\n';
134 std::stringstream ss;
135 insert_genquery_inp_into_stream(q, ss);
142 std::stringstream ss;
147 ss <<
"ERROR: 0 columns selected, printing query contents instead of iquest string\n";
148 insert_genquery_inp_into_stream(q, ss);
153 for (
int i=1; i<n; ++i) {
164 for (
int i=1; i<n; ++i) {
180 return genquery_inp_;
183 genquery_out_ptr_ =
nullptr;
189 return genquery_out_ptr_;
210 for (
int i = 0; i < tmp.
len; ++i ) {
232 const int COL_D_RESC_GROUP_NAME = 408;
253 for (
int i = 0; i < tmp.
len; ++i ) {
254 if ( tmp.
inx[i] == COL_D_RESC_GROUP_NAME ) {
264 if ( tmp.
inx ) { free( tmp.
inx ); }
275 const int COL_D_RESC_GROUP_NAME = 408;
294 sqlResult->
attriInx = COL_D_RESC_GROUP_NAME;
303 std::string& _new_cond ) {
304 const std::string in_cond( _cond );
305 const std::string::size_type p0 = in_cond.find_first_of(
"'" );
306 const std::string::size_type p1 = in_cond.find_last_of(
"'" );
311 std::string hier = in_cond.substr( p0 + 1, p1 - p0 - 1);
317 if ( std::string::npos != in_cond.find(
"=" ) &&
318 std::string::npos != hier.find(
"%" ) ) {
328 std::string::size_type pos( hier.find_first_of(
"%" ) );
329 std::string hierRegex( hier );
330 while ( std::string::npos != pos ) {
331 hierRegex.replace( pos, 1,
"(.*)" );
332 pos = hierRegex.find_first_of(
"%" );
335 std::vector<rodsLong_t> leaf_ids;
338 for (
const auto& hier_ : hier_list ) {
339 if ( boost::regex_match( hier_, boost::regex( hierRegex ) ) ) {
345 leaf_ids.push_back( leaf_id );
353 if ( leaf_ids.empty() ) {
358 for (
const auto& leaf_id : leaf_ids ) {
359 std::stringstream current_cond;
360 current_cond <<
"'" << leaf_id <<
"'";
361 if( leaf_id != leaf_ids.back() ) {
364 _new_cond += current_cond.str();
392 for (
int i = 0; i < tmpI.
len; ++i ) {
403 if ( tmpI.
inx ) { free( tmpI.
inx ); }
421 for (
int i = 0; i < tmpV.
len; ++i ) {
423 std::string new_cond =
"='0'";
470 std::vector<std::string> resc_hiers(_out->
rowCnt);
471 for (
int i = 0; i < _out->
rowCnt; ++i ) {
472 char* leaf_id_str = &sqlResult->
value[i*sqlResult->
len];
490 if(resc_hiers[i].
size() > max_len ) {
491 max_len = resc_hiers[i].size();
496 free( sqlResult->
value );
500 sqlResult->
len = max_len+1;
502 for( std::size_t i = 0; i < resc_hiers.size(); ++i ) {
504 &sqlResult->
value[i*sqlResult->
len],
507 resc_hiers[i].c_str() );
517 const std::string& _zone_hint,
524 boost::char_separator<char> sep(
"/");
525 boost::tokenizer<boost::char_separator<char> > tokens(_zone_hint, sep);
526 if (tokens.begin() != tokens.end()) {
543 tmp_zone = tmp_zone->
next;
554 print_gen_query_input(
556 if(!genQueryInp)
return 0;
559 int *ip1 = 0, *ip2 = 0;
563 printf(
"maxRows=%d\n", genQueryInp->
maxRows );
566 printf(
"sel len=%d\n", len );
571 for ( i = 0; i < len; i++ ) {
572 printf(
"sel inx [%d]=%d\n", i, *ip1 );
573 printf(
"sel val [%d]=%d\n", i, *ip2 );
580 printf(
"sqlCond len=%d\n", len );
587 for ( i = 0; i < len; i++ ) {
588 printf(
"sel inx [%d]=%d\n", i, *ip1 );
589 printf(
"sel val [%d]=:%s:\n", i, cp );
612 std::string zone_hint_str;
617 if(
'/' == zone_hint_str[0]) {
618 zone_hint_str = zone_hint_str.substr(1);
620 std::string::size_type pos = zone_hint_str.find_first_of(
"/");
621 if(std::string::npos != pos ) {
622 zone_hint_str = zone_hint_str.substr(0,pos);
625 if(
'\'' == zone_hint_str[zone_hint_str.size()-1]) {
626 zone_hint_str = zone_hint_str.substr(0,zone_hint_str.size()-1);
634 zone_hint_str.c_str(),
643 if ( !zone_hint_str.empty() ) {
651 std::string svc_role;
662 "rsGenQuery error. RCAT is not configured on this host" );
667 "role not supported [%s]",
687 genQueryInp, genQueryOut );
692 "rsGenQuery: %sGenQuery failed, status = %d", prefix.c_str(),
status );
701 int resc_grp_attr_pos = -1;
702 int resc_hier_attr_pos = -1;
704 static int ruleExecuted = 0;
708 static int PrePostProcForGenQueryFlag = -2;
716 std::string local_rel_version = RODS_REL_VERSION;
717 if ( client_rel_version != local_rel_version ) {
729 if ( PrePostProcForGenQueryFlag < 0 ) {
730 if ( getenv(
"PREPOSTPROCFORGENQUERYFLAG" ) !=
NULL ) {
731 PrePostProcForGenQueryFlag = 1;
734 PrePostProcForGenQueryFlag = 0;
739 rei2.rsComm = rsComm;
744 memset( (
char * )*genQueryOut, 0,
sizeof(
genQueryOut_t ) );
746 if ( ruleExecuted == 0 ) {
747 memset( (
char* )&rei, 0,
sizeof( rei ) );
776 bool agent_conn_flag =
true;
780 agent_conn_flag =
false;
787 if ( agent_conn_flag && dis_kw ) {
800 if ( PrePostProcForGenQueryFlag == 1 ) {
801 std::string arg = str( boost::format(
"%ld" ) % ( (
long )genQueryInp ) );
802 args[0] = arg.c_str();
807 "rsGenQuery:acPreProcForGenQuery error,stat=%d", i );
819 if ( agent_conn_flag && dis_kw ) {
829 if ( PrePostProcForGenQueryFlag == 1 ) {
830 std::string in_string = str( boost::format(
"%ld" ) % ( (
long )genQueryInp ) );
831 std::string out_string = str( boost::format(
"%ld" ) % ( (
long )genQueryOut ) );
832 std::string status_string = str( boost::format(
"%d" ) % ( (
long )
status ) );
833 args[0] = in_string.c_str();
834 args[1] = out_string.c_str();
835 args[2] = status_string.c_str();
840 "rsGenQuery:acPostProcForGenQuery error,stat=%d", i );
850 if (
status >= 0 && resc_grp_attr_pos >= 0 ) {
857 if (
status >= 0 && resc_hier_attr_pos >= 0 ) {
871 "_rsGenQuery: genQuery status = %d",
status );