#
#					ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:				dquerymfile_updccrwrec.txt
#	DESCRIPTION:		DQUERY definition for DQUERY updccrwrec
#
@querytitle			Eintrag in CCRW hinzufügen oder ändern
@description		Fügt einen neuen Eintrag in CCRW hinzu oder ändert ihn wenn schon vorhanden
@group				RWDATA
@attributenames		action:string:{{actionoptions}},RWTYP:string:{{rwtypoptions}},RWPARENT:string:{{rwparentoptions}},RWNAME:string:{{rwnameoptions}},RWVALUE:string:{{rwvalueoptions}},RWDESC:string:{{rwdescoptions}}
@actionoptions	{
	labeltext:		"Aktion",
	typedesc:		"Durchzuführende Aktion: update: Ändere CCRW-Eintrag, delete: lösche CCRW-Eintrag" }
@rwtypoptions		{"labeltext":"Typ","typedesc":"RWTYP, Teil des Eindeutigkeits-Schlüssels"}
@rwparentoptions	{"labeltext":"Parent","typedesc":"RWPARENT, Teil des Eindeutigkeits-Schlüssels"}
@rwnameoptions		{"labeltext":"Name","typedesc":"RWNAME, Teil des Eindeutigkeits-Schlüssels"}
@rwvalueoptions {
	labeltext:		"Value",
	typedesc:		"RWVALUE",
	is_optional:	true }
@rwdescoptions		{"labeltext":"Description","typedesc":"RWDESC","is_optional":"true"}
@querytype			function
@function			seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: determine action to be done
#
pre_qexpression
	proc: {
		this.action = this.query.action;
		this.RWTYP = this.query.RWTYP;
		this.RWPARENT = this.query.RWPARENT;
		this.RWNAME = this.query.RWNAME;
		if (this.query.hasOwnProperty("RWVALUE")) this.RWVALUE = this.query.RWVALUE;
		if (this.query.hasOwnProperty("RWDESC")) this.RWDESC = this.query.RWDESC;
		aux.default_param(this,"RWVALUE","");
		aux.default_param(this,"RWDESC","");
		this.actlab = "action_"+this.action;
		if (!this.labels[this.actlab]) {
			this.errcoll.collect(null,"Cannot process action \""+this.action+"\" there is no label \""+this.actlab+"\" in the seqtrans sequence of \""+this.dataname+"\"", this.query);
			break proc;
		}
	}
goto	@@actlab@@

#
# <<TSTEPNUM:1>>: action delete
#
action_delete:
sql				delete from CCRW where RWTYP=::RWTYP:: and RWPARENT=::RWPARENT:: and RWNAME=::RWNAME::
named_params	RWTYP,RWPARENT,RWNAME
result_varname	dbres
post_qexpression
	proc: {
		let rc = (this.dbres && this.dbres.resulttype && this.dbres.resulttype=='object' && this.dbres.resultobject && this.dbres.resultobject.affectedRows)?this.dbres.resultobject.affectedRows:"FEHLER";
		if (rc=="FEHLER") {
			this.errcoll.collect(null,"Error deleting CCRW entry "+this.RWTYP+"/"+this.RWPARENT+"/"+this.RWNAME,this.dbres);
			break proc;
		}
		this.result = new aux.Result({
			resulttype: 'plain', 
			body: "CCRW-Eintrag "+this.RWTYP+"/"+this.RWPARENT+"/"+this.RWNAME+" gelöscht, "+rc+" Datensätze verändert",
			structure_has_changed:	true});
	}

#
# <<TSTEPNUM:2>>: action delete end
#
end

#
# <<TSTEPNUM:3>>: action update, check if the record is already there
#
action_update:
sql				select RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC from CCRW where RWTYP=::RWTYP:: and RWPARENT=::RWPARENT:: and RWNAME=::RWNAME::
named_params	RWTYP,RWPARENT,RWNAME
result_varname	dbres
post_qexpression
	proc: {
		let rc = this.dbres.getRowCount();
		if ((rc<0) || (rc>1)) {
			this.errcoll.collect(null,"wrong row count "+rc+" for CCRW-Entry \""+this.RWTYP+"/"+this.RWPARENT+"/"+this.RWNAME+"\"",this.dbres);
			break proc;
		} else if (rc==0) {
			this.mode = "create";
		} else if (rc==1) {
			this.mode = "update";
		}
	}
then_goto	update_mode_@@mode@@

#
# <<TSTEPNUM:4>>: update mode create
#
update_mode_create:
sql				insert into CCRW(RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC) values(::RWTYP::,::RWPARENT::,::RWNAME::,::RWVALUE::,::RWDESC::)
named_params	RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC
result_varname	dbres
post_qexpression
	proc: {
		let rc = (this.dbres && this.dbres.resulttype && this.dbres.resulttype=='object' && this.dbres.resultobject && this.dbres.resultobject.affectedRows)?this.dbres.resultobject.affectedRows:"FEHLER";
		if (rc=="FEHLER") {
			this.errcoll.collect(null,"Error creating CCRW record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({	resulttype: 'plain', 
										body: "neuer CCRW-Eintrag \""+this.RWTYP+"/"+this.RWPARENT+"/"+this.RWNAME+"\" angelegt, "+rc+" Datensätze verändert",
										structure_has_changed: true});
	}

#
# <<TSTEPNUM:5>>: update mode create end
#
end

#
# <<TSTEPNUM:6>>: update mode update
#
update_mode_update:
sql				update CCRW set RWTYP=::RWTYP::,RWPARENT=::RWPARENT::,RWNAME=::RWNAME::,RWVALUE=::RWVALUE::,RWDESC=::RWDESC::
				 where RWTYP=::RWTYP:: and RWPARENT=::RWPARENT:: and RWNAME=::RWNAME::
named_params	RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC
result_varname	dbres
post_qexpression
	proc: {
		let rc = (this.dbres && this.dbres.resulttype && this.dbres.resulttype=='object' && this.dbres.resultobject && this.dbres.resultobject.affectedRows)?this.dbres.resultobject.affectedRows:"FEHLER";
		if (rc=="FEHLER") {
			this.errcoll.collect(null,"Error updating CCRW record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({	resulttype: 'plain', 
										body: "CCRW-Eintrag \""+this.RWTYP+"/"+this.RWPARENT+"/"+this.RWNAME+"\" geändert, "+rc+" Datensätze verändert",
										structure_has_changed: false});
	}

#
# <<TSTEPNUM:7>>: update mode update end
#
end
