#
# dquery definition for LOADSVCLRWRFC
#
# 
@group				SVCLCFSTATMVBRFAUSW
@querytitle 		STP-MVB Rückfragecodes in Datenbank laden
@querydescription	lädt MVB-Rückfragecode-Definitionen aus dem managed File in die Tabelle SVCLRWRFC
@attributenames		mfileid:mfileid:{{mfileidoptions}}
@mfileidoptions		{"labeltext":"Lade-Managed-File ID","typedesc":"Managed File ID der Datei, die geladen werden soll, diese muss ein XLSX-File mit 9 Spalten sein"}
@querytype			function
@function			seqtrans.seqtrans
@TABNAM				SVCLRWRFC
@FLIST				CODE,RUECKFRAGETEXT,DRINGENDJN,VARIABLEFELDER,OBJEKT,MVB020,MVB030,MVB040,BEISPIELTEXT

~query.tsteps
#
# 0: 
#
if	(this.init_done)
goto	record_proper

#
# 1: get MFILE info, determine load format
#
qexpression
	proc: {
		if (!this.init_done) {
			this.fname = "LOADSVCLRWRFC";
			this.TABNAM = this.query.TABNAM;
			this.FLIST = this.query.FLIST;
			this.ffnames = new Array();
			for (let ci=0; ci<9; ci++) {
				this.ffnames.push("Column"+ci);
			}
			this.tfnames = this.FLIST.split(",");
			this.FVLIST = "::"+this.tfnames.join("::,::")+"::";
			if (!this.mfileinfo) {
				this.errcoll.collect(null,"no MFILEINFO");
				break proc;
			}
			if (!this.mfileinfo.mfileid) {
				this.errcoll.collect(null,"MFILEINFO has no MFILEID",this.mfileinfo);
			}
			this.load_mfileid = this.mfileinfo.mfileid;
			if (!this.mfileinfo.mfilename) {
				this.errcoll.collect(null,"MFILEINFO has no MFILENAME attribute",this.mfileinfo);
				break proc;
			}
			this.load_mfileattribs = this.mfileinfo.mfileattribs;
			this.load_mfileattributes = aux.txtObjDecode(this.load_mfileattribs) || {};
			if (this.load_mfileattributes.loadstatus && this.load_mfileattributes.loadstatus=="loaded") {
				this.errcoll.collect(null,"LOAD_MFILE "+this.load_mfileid+" is already loaded",{ mfilinfo: this.mfileinfo, load_mfileattributes: this.load_mfileattributes});
				break proc;
			}
			this.mfnparts = this.mfileinfo.mfilename.split(".");
			if (this.mfnparts.length!=2) {
				this.errcoll.collect(null,"MFILENAME does not have 2 parts, separated by '.'",this.mfileinfo);
				break proc;
			}
			if (this.mfnparts[1]!="xlsx") {
				this.errcoll.collect(null,"MFILENAME does not signify XLSX file",this.mfileinfo);
				break proc;
			}
			this.protocol = new Array();
			this.protocol.push([this.fname+"_started","MFILEID_TO_LOAD="+this.mfileid,"",""]);
			this.recsprocessed = 0;
		}
	}

#
# 2: check if target table is there (if not, the SQL step will fail and everything will be rolled back)
#
sql		describe SVCLRWRFC
post_qexpression
	protocol.push(["TABLEDESC",this.TABNAM,"",aux.objTxt(this.stepresult)]);

#
# 3: init done
#
qexpression
	this.init_done = true;
	
#
# 4: insert one record
#
record_proper:
pre_qexpression
	/*
	for (let ci=0; ci<this.ffnames.length; ci++) {
		this.cdata[this.tfnames[ci]] = this.cdata[this.ffnames[ci]];
	}
	*/
	if (this.recnum>0 && (this.recnum%100)==0) logger.debug(this.fname+".UDBG.record_number="+this.recnum+".CDATA: "+aux.objTxt(this.cdata));
sql	insert into @@TABNAM@@(@@FLIST@@) VALUES(@@FVLIST@@)
named_params	@@FLIST@@
post_qexpression
	this.recsprocessed++;

#
# 5: check if this was the last record
#
if (!this.is_last_record)
goto record_done

#
# 6: all records processed, mark MFILE as loaded
#
pre_qexpression
	this.load_mfileattributes.loadtime = aux.nowstring();
	this.load_mfileattribs = JSON.stringify(this.load_mfileattributes);
sql				update CCMFILES SET MFILEATTRIBS=::load_mfileattribs:: WHERE MFILEID=::mfileid::
named_params	load_mfileattribs,mfileid
post_qexpression
	this.protocol.push([this.fname+"_finished","MFILEID_TO_LOAD="+this.mfileid,"RECORDS_PROCESSED="+this.recsprocessed,""]);
	this.result = new aux.Result({resulttype: 'dbresult', metaData: [{name: 'Info'},{name: 'parameter 1'},{name: 'parameter 2'},{name: 'parameter 3'}], rows: this.protocol});
	
#
# 7: end of record processing
#
record_done:
end