#
#
#		ITSV GmbH
#		CCDB - Command and Control Database
#
#	FILE:			dquerymfile_loadRWfromMFILE
#	DESCRIPTION:	DQUERY definition file for DQUERY loadRWfromMFILE
#					this query loads one specific Regelwerk from an MFILE into CCRW
#
@querytitle			Ein Regelwerk aus einer Datei laden
@querydescription	lädt ein definiertes Regelwerk der CCDB aus einem managed file
@group				RWDATA
@attributenames		mfileid:mfileid,rwid:rwid
@querytype			function
@function			seqtrans.seqtrans
@FLIST				RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC
@init.qexpression
	this.valid_rws = {
		QUERIES:	{	rwtypes:	[	"DQUERY","DQUERYPARAM"	]	},
		TYPES:		{	rwtypes:	[	"TYPE","TYPEATTRIB"		]	},
		OBJTYPES:	{	rwtypes:	[	"OBJTYPE","OBJACTION","OACTION","OACTATTRIB" ] }
	}

~query.tsteps

#
# <<TSTEPNUM:0>>: if already initialized, skip run-once initialization steps
#
pre_qexpression	
	copycvars(this,this.query.FLIST);
if	(this.init_done)  
goto record_proper

#
# <<TSTEPNUM:1>>: initialization
#
qexpression
	proc: {
		this.ppfx = this.fname+"("+this.dataname+")";
		this.FLIST = this.query.FLIST;
		this.IFNAMES = this.FLIST.split(",");
		this.TPLIST = "::" + this.IFNAMES.join("::,::") + "::";
		this.cns = {};
		let cn;
		for (let ci=0; ci<this.IFNAMES.length; ci++) {
			cn = this.inputresult.findColumn(this.IFNAMES[ci]);
			if (cn<0) {
				this.errcoll.collect(null,"Column "+this.IFNAMES[ci]+" not in input dataset",this.inputresult);
			} else {
				this.cns[this.IFNAMES[ci]] = cn;
			}
		}
		if (this.errcoll.hasErrors()) {
			break proc;
		}
		if (this.valid_rws[this.query.rwid]) {
			this.rwdesc = this.valid_rws[this.query.rwid];
		} else {
			this.errcoll.collect(null,"RW "+this.query.rwid+" is not valid for "+this.query.dataname,{ valid_rws: this.valid_rws});
			break proc;
		}
		this.rwdesc.rwts = {};
		for (let ti=0; ti<this.rwdesc.rwtypes.length; ti++) {
			this.rwdesc.rwts[this.rwdesc.rwtypes[ti]] = { load_record: true };
		}
		this.recsprocessed = 0;
		this.recsloaded	= 0;
		this.recstoprocess = this.inputresult.getRowCount();		
		ppush(this,[this.ppfx+"_STARTED","MFILEID="+this.query.mfileid,"RW="+this.query.rwid,"RECORDS_TO_PROCESS="+this.recstoprocess,""]);
	}

#
# <<TSTEPNUM:2>>: remove old RW contents
#
pre_qexpression
	this.remove_sql	=	"DELETE from CCRW where RWTYP in ('"+this.rwdesc.rwtypes.join("','")+"')";
sql				@@remove_sql@@
result_varname	remres
post_qexpression
	ppush(this,[this.ppfx+"_OLD_REMOVED","ROWS_AFFECTED="+this.remres.resultobject.affectedRows,"DB_MESSAGE="+this.remres.resultobject.message,"REMOVE_SQL=\""+this.remove_sql+"\""]);
	this.init_done = true;

#
# <<TSTEPNUM:3>>: check if current record shall be inserted
#
record_proper:
pre_qexpression
	logger.debug("RECNUM="+this.recnum+".RWTYP="+this.RWTYP+".RWTS="+aux.objTxt(this.rwdesc.rwts));
if		(!(this.rwdesc.rwts[this.RWTYP] && this.rwdesc.rwts[this.RWTYP].load_record))
goto	skip_record

#
# <<TSTEPNUM:4>>: load current record in CCRW
#
sql				INSERT INTO CCRW(@@FLIST@@) values(@@TPLIST@@)
named_params	@@FLIST@@
result_varname	insres
post_qexpression
	if (this.insres.resultobject.affectedRows!=1) {
		this.errcoll.collect(null,"insert into CCRW affected wrong number of rows",this.insres);
	} else {
		this.recsloaded += this.insres.resultobject.affectedRows;
	}

#
# <<TSTEPNUM:5>>: check if last record
#
skip_record:
pre_qexpression
	this.recsprocessed++;
if		(!this.is_last_record)
goto	done

#
# <<TSTEPNUM:6>>: last record, close off protocol
#
qexpression
	ppush(this,[this.ppfx+"_FINISHED","RECSTOPROCESS="+this.recstoprocess,"RECSPROCESSED="+this.recsprocessed,"RECSLOADED="+this.recsloaded]);

#
# <<TSTEPNUM:7>>: end of record
#
done:
end
	
	