#
#		ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:		dquermfile_updateTextFragment.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "updateTextFragment"
#
@querytitle			Text-Fragment ändern oder löschen
@querydescription	Ändert Text-Fragment oder löscht den Eintrag
@group				ADMINTEXTFRAG
@attributenames		action:string:{{actionoptions}},type,ident,version,parent:string:{{parentoptions}},child_index,frag_index,data:string:{{dataoptions}}
@actionoptions	{
	labeltext:		"Aktion",
	typedesc:		"Aktion, die durchgeführt werden soll: \"Ändern\": ändert den Eintrag, \"Löschen\": löscht den Eintrag",
	type:	{	structure:		"scalar.enum",
				vlist:			"update:Ändern:Eintrag ändern,delete:Löschen:Eintrag löschen"
			}
	}
@parentoptions {
	labeltext:		"Parent-Text",
	typedesc:		"\"ident\"-Attribut des übergeordneten Texts",
	is_optional:	true
	}
@dataoptions	{
	labeltext:		"Textdaten",
	typedesc:		"neuer Inhalt der Textdaten für dieses Textfragment",
	is_optional:	true
	}
@querytype			function
@function			seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: determine action
#
pre_qexpression
	proc: {
		this.copycvars("action,type,ident,version,parent,child_index,frag_index,data");
		this.actlab = "action_"+this.action;
		if (!this.labels[this.actlab]) {
			this.errcoll.collect(null,"Cannot perform action "+this.action+", TSTEP label "+this.actlab+" does not exist",this.query);
			break proc;
		}
	}
goto @@actlab@@

#
# <<TSTEPNUM:1>>: delete text fragment
#
action_delete:
sql				delete from CCTEXTFRAG where type=::type:: and ident=::ident:: and version=::version:: and child_index=::child_index:: and frag_index=::frag_index:: 
named_params	type,ident,version,child_index,frag_index
result_varname	dbres
post_qexpression
	proc: {
		let rc = (this.dbres && this.dbres.resulttype && this.dbres.resulttype=='object' && this.dbres.resultobject && this.dbres.resultobject.hasOwnProperty("affectedRows"))?this.dbres.resultobject.affectedRows:"FEHLER";
		if (rc=="FEHLER") {
			logger.error(this.phead("delNotValidRes: ")+aux.objTxt(this.dbres));
			this.errcoll.collect(null,"Error not-wellformed result deleting TEXTFRAG record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({	resulttype: 'plain', 
										body: "Text Fragment "+
												this.query.type+"/"+this.query.ident+"/"+this.query.child_index+"/"+this.query.frag_index+
												" gelöscht, "+rc+" Datensätze verändert",
										structure_has_changed: true});
	}

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

#
# <<TSTEPNUM:3>>: update database record, first check if record already exists
#
action_update:
sql				select type,ident,version,child_index,frag_index,data from CCTEXTFRAG 
				 where type=::type:: and ident=::ident:: and version=::version:: and child_index=::child_index:: and frag_index=::frag_index:: 
named_params	type,ident,version,parent,child_index,frag_index
result_varname	dbres
post_qexpression
	proc: {
		let rc = this.dbres.getRowCount();
		if (rc<1) {
			this.updmode = "create";
		} else if (rc==1) {
			this.updmode = "update";
			this.oldrow = this.dbres.getRowObject(0);
			this.oldtype		= this.oldrow.type;
			this.oldident		= this.oldrow.ident;
			this.oldparent		= this.oldrow.parent;
			this.oldversion		= this.oldrow.version;
			this.oldchild_index	= this.oldrow.child_index;
			this.oldfrag_index	= this.oldrow.frag_index;
			this.olddata		= this.oldrow.data;
		} else {
			this.errcoll.collect(null,"Illegal number of records in checking for update",this.dbres);
			break proc;
		}
	}
then_goto	action_update_@@updmode@@

#
# 5: action update mode create
#
action_update_create:
sql				insert into CCTEXTFRAG(type,ident,parent,version,child_index,frag_index,data) values(::type::,::ident::,::parent::,::version::,::child_index::,::frag_index::,::data::)
named_params	type,ident,parent,version,child_index,frag_index,data
result_varname	dbres
post_qexpression
	proc: {
		let rc = (this.dbres && this.dbres.resulttype && this.dbres.resulttype=='object' && this.dbres.resultobject && this.dbres.resultobject.hasOwnProperty("affectedRows"))?this.dbres.resultobject.affectedRows:"FEHLER";
		if (rc=="FEHLER") {
			this.errcoll.collect(null,"Error not-wellformed result creating TEXTFRAG record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({	resulttype: 'plain', 
										body: "neues Text Fragment "+
												this.query.type+"/"+this.query.indent+"/"+this.query.child_index+"/"+this.query.frag_index+
												" angelegt, "+rc+" Datensätze verändert",
										structure_has_changed: true});
	}

#
# <<TSTEPNUM:4>>: action mode update create end
#
end
		

#
# <<TSTEPNUM:5>>: action update mode update
#
action_update_update:
pre_qexpression
	if ((this.olddata!=this.data) || (this.oldparent!=this.parent)){
		this.recchange = "change";
	} else {
		this.recchange = "nochange";
	}
goto update_@@recchange@@

#
# <<TSTEPNUM:6>>: action update update no change
#
update_nochange:
qexpression
	this.result = new aux.Result({	resulttype: "plain",
									body:		"keine Daten verändert"
								});

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

#
# <<TSTEPNUM:8>>: action update update change
#
update_change:								
sql				update CCTEXTFRAG set version=::oldversion::+1,data=::data::,parent=::parent::
					 where type=::oldtype:: and ident=::oldident:: and version=::oldversion::
					   and child_index=::oldchild_index:: and frag_index=::oldfrag_index::
named_params	oldtype,oldident,oldversion,oldparent,oldchild_index,oldfrag_index,parent,data
result_varname	dbres
post_qexpression 
	proc: {
		let rc = (this.dbres && this.dbres.resulttype && this.dbres.resulttype=='object' && this.dbres.resultobject && this.dbres.resultobject.hasOwnProperty("affectedRows"))?this.dbres.resultobject.affectedRows:"FEHLER";
		if (rc=="FEHLER") {
			this.errcoll.collect(null,"Error not-wellformed result updating TEXTFRAG record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({	resulttype: 'plain', 
										body: "Text Fragment "+
												this.query.type+"/"+this.query.indent+"/"+this.query.child_index+"/"+this.query.frag_index+
												" geändert, "+rc+" Datensätze verändert",
										structure_has_changed: false});
	}

#
# <<TSTEPNUM:9>>: action update update change end
#
pre_qexpression
	logger.debug("RESULT: "+aux.objTxt(this.result));
end
