#
#			ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquerymfile_markdqmgroup.txt
#	DESCRIPTION:	DQUERY definition for DQUERY markdqmgroup
#					this query marks all DQUERY definition MFILES with the group of their queries
#					this is done by setting the MFILE attribute "group" to the group of the DQUERY
#					the group is derived from the global atttribute "group" from the definition MFILE
#
@querytitle			Markiere alle Abfrage-Definitions-Dateien mit ihrer Abfragegruppe 
@querydescription	setzt für alle managed files der type "dquerymfile" das MFILE-Attribut "group" auf den Namen der in der Definition enthaltenen Abfragegruppe
@group				QUERIES
@querytype			function
@function			seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: list all dquerymfile s
#
sql				select MFILEID, MFILEATTRIBS from CCMFILES where MFILETYPE='dquerymfile'
result_varname	mfres
post_qexpression
	this.mfilestoprocess = this.mfres.getRowCount();
	this.mfilesprocessed = 0;
	this.mfnum = 0;
	this.ppush([this.phead("STARTED"),"MFILESTOPROCESS="+this.mfilestoprocess,"",""]);

#
# <<TSTEPNUM:1>>: check if all files processed
#
floop:
if 		(this.mfnum>=this.mfilestoprocess)
goto	allfilesdone

#
# <<TSTEPNUM:2>>: get group from DQUERY definition in MFILE
#
pre_qexpression
	this.mfid = this.mfres.getCellValue(this.mfnum,0);
	this.mfattribs = JSON.parse(this.mfres.getCellValue(this.mfnum,1));
	this.oldgroup = this.mfattribs.group?undefined:this.mfattribs.group;
	this.newgroup = null;
	this.cqd = {};
aexpression
	objcomp.compileMfile(this.mfid,this.cqd,{},
		function(err,res) {
			if (err) {
				this.errcoll.collect(err,"Error object-compiling managed file ID="+this.mfid,res);
				this();
			}
			if (this.cqd && this.cqd.group) {
				this.newgroup = this.cqd.group;
			} else {
				this.errcoll.collect(null,"dquerymfile ID="+this.mfid+" has no group definition",{ cqd: this.cqd });
				this();
			}
			this();
		}.bind(this));

#
# <<TSTEPNUM:3>>: update mfile attributes (if changed)
#
if 		(!(this.newgroup && (this.newgroup!=this.oldgroup)))
goto	nochange

#
# <<TSTEPNUM:4>>: write back attributes
#
pre_qexpression
	this.mfattribs.group = this.newgroup;
	this.mfattribstring = JSON.stringify(this.mfattribs);
sql				UPDATE CCMFILES set MFILEATTRIBS=::mfattribstring:: WHERE MFILEID=::mfid::
named_params	mfid,mfattribstring
result_varname	aures
post_qexpression
	this.ppush([this.phead("DQMUPDATED"),"UPDATED_MFILEID="+this.mfid,"NEWGROUP="+this.newgroup,this.aures]);
	this.mfilesprocessed++;
	this.mfnum++;
then_goto	floop

#
# <<TSTEPNUM:5>>: all files done
#
allfilesdone:
qexpression
	this.ppush([this.phead("FINISHED"),"MFILESTOPROCESS="+this.mfilestoprocess,"MFILESPROCESSED="+this.mfilesprocessed,""]);

#
# <<TSTEPNUM:6>>: end of processing
#
end