#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:		dquermfile_repqpnl.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "repqpnl"
#
@querytitle			Newlines in Aktions-Parametern im Regelwerk reparieren
@querydescription	tauscht "/", gefolgt von "n" in Query-Parametern im Regelwerk gegen "\n" (linefeed-Zeichen) aus
@group				ADMIN
@querytype			function
@function			seqtrans.seqtrans
#
# legacy flowsequance configuration:
#
# @aexpression		functions.flowsequence(that.session,that.query,that,that.pcallback)
# @flowsequence		readqrysql,repqrysql,writeqrysql
#
# @readqrysql_steptype	dbselect
# @readqrysql_sql	select RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC from CCRW where RWTYP='DQUERYPARAM' and RWVALUE like ('%/' || 'n%')
# @readqrysql_bindvars	[]
# @readqrysql_postsqlexpression	console.log("…results:",util.inspect(that.results,{depth:20})); 
#
# @repqrysql_steptype	jsexpr
# @repqrysql_qexpression	for (var ri=0; ri<that.results[0].rows.length; ri++) { if (that.results[0].rows[ri][3])  { that.results[0].rows[ri][3] = that.results[0].rows[ri][3].replace(/\/[n]/g,'\n'); console.log("...row "+ri+" replaced:\""+that.results[0].rows[ri][3]+"\""); } else { console.log("RWVALUE of row "+ri+" - "+that.results[0].rows[ri][0]+"/"+that.results[0].rows[ri][1]+"/"+that.results[0].rows[ri][2]+" is null"); } }
#
# @writeqrysql_steptype	dbselect_loop
# @writeqrysql_initexpression	that.arrayindex = 0; that.curarray = that.results[0].rows; that.rowstowrite = that.curarray.length;
# @writeqrysql_checkexpression	that.arrayindex<that.curarray.length
# @writeqrysql_presqlexpression	that.curow = that.curarray[that.arrayindex]; if (that.curow[3] && that.curow[2]) { that.r = {skip: false} } else { console.log("…Skipping row "+that.arrayindex); that.r = {skip: true} } that.r = that.r
# @writeqrysql_sql	update CCRW set RWVALUE=:newvalue where RWTYP=:rwtyp and RWPARENT=:qryname and RWNAME=:paramname
# @writeqrysql_bindvars	[that.curow[3],that.curow[0],that.curow[1],that.curow[2]]
# @writeqrysql_postsqlexpression	console.log("writeqrysql: step "+that.curstep+" iteration "+that.arrayindex+" complete")
# @writeqrysql_nextexpression	that.arrayindex++;
#
# @finalexpression	that.restext = that.query.dataname+" complete, "+that.rowstowrite+" rows updated"; console.log(that.restext); that.result = {resulttype: 'string', body: that.restext};

~query.tsteps

#
# <<TSTEPNUM:0>>: readqrysql
#
pre_qexpression
	this.ppfx = this.fname+"("+this.dataname+")";
sql				select RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC from CCRW where RWTYP='DQUERYPARAM' and RWVALUE like ('%/' || 'n%')
result_varname	rqres
post_qexpression
	this.recstoprocess = this.rqres.getRowCount();
	ppush(this,[this.ppfx+".START","RECSTOPROCESS="+this.recstoprocess,"",""]);

#
# <<TSTEPNUM:1>>: repqrysql
#
qexpression
	for (var ri=0; ri<this.rqres.getRowCount(); ri++) {
		if (this.rqres.rows[ri][3])  { 
			this.rqres.rows[ri][3] = this.rqres.rows[ri][3].replace(/\/[n]/g,'\n'); 
			ppush(this,[this.ppfx+".PROCROW="+ri,"...row "+ri+" replaced:\""+this.rqres.rows[ri][3]+"\"","",""]); 
		} else { 
			ppush(this,[this.ppfx+".PROCROW="+ri,"RWVALUE of row "+ri+" - "+this.rqres.rows[ri][0]+"/"+this.rqres.rows[ri][1]+"/"+this.rqres.rows[ri][2]+" is null","",""); 
		}
	}

#
# <<TSTEPNUM:2>>: writeqrysql - setup loop
#
qexpression
	this.arrayindex = 0; 
	this.curarray = this.rqres.rows; 
	this.rowstowrite = this.curarray.length;

#
# <<TSTEPNUM:3>>: writeqrysql - check at loop entry
#
loop1:
if		(this.arrayindex>=this.curarray.length)
goto	loop1_end

#
# <<TSTEPNUM:4>>: writeqrysql - execute SQL in loop
#
qexpression
	this.curow = that.curarray[that.arrayindex]; 
	if (this.curow[3] && this.curow[2]) { 
		this.skip =false;
	} else { 
		ppush(this,[this.ppfx+".SKIP_ROW="+this.arrayindex); 
		this.skip = true; 
	} 

#
# <<TSTEPNUM:5>>: writeqrysql - check skip condition
#
if		(this.skip)
goto	loop1_sql_bypass

#
# <<TSTEPNUM:6>>: writeqrysql - execute SQL
#
pre_qexpression
	this.cdata.newvalue = this.curow[3];
	this.cdata.rwtyp	= this.curow[0];
	this.cdata.qryname	= this.curow[1];
	this.cdata.paramname= this.curow[2];
sql				update CCRW set RWVALUE=::newvalue:: where RWTYP=::rwtyp:: and RWPARENT=::qryname:: and RWNAME=::paramname::
named_params	newvalue,rwtyp,qryname,paramname
result_varname	updres
post_qexpression
	ppush(this,[this.ppfx+"_WRITE_RECDONE","RECDONE="+this.arrayindex,"",""]); 

#
# <<TSTEPNUM:7>>: writeqrysql - loop end increment
#
loop1_sql_bypass:
qexpression
	this.arrayindex++;

#
# <<TSTEPNUM:8>>: writeqrysql - loop repeat
#
goto	loop1

#
# <<TSTEPNUM:9>>: finalexpression
#
loop1_end:
qexpression
	ppush(this,[this.ppfx+".complete",that.rowstowrite+" rows updated","",""]);



	