#
#			ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquerymfile_updatetypeattribute.txt
#	DESCRIPTION:	DQUERY definition for DQUERY updatetypeattribute
#					this query updates one attribute of a specific type
#					a type is kept in CCRW and has at least one record: RWTYP='TYPE', RWNAME=<typename>
#					additional records define additional attributes of the type with RWTYP='TYPEATTRIB'
#
@querytitle			Ändere Attribut eines Datentyps
@group				TYPES
@querydescription	Ändert oder löscht ein Attribut eines Datentyps
@attributenames		action:string:{{actionoptions}},typname:string:{{typnameoptions}},attribname:string:{{attribnameoptions}},attribvalue:string:{{attribvalueoptions}},attribdesc:string:{{attribdescoptions}}
@actionoptions		{ type: { structure: "scalar.enum", vlist: "update:Ändern:Attribut ändern,delete:Löschen:Attribut löschen" }, labeltext: "Aktion" }
@typnameoptions		{ labeltext: "Typname" }
@attribnameoptions	{ labeltext: "Attributname" }
@attribvalueoptions	{ labeltext: "Attributwert", is_optional: true }
@attribdescoptions	{ labeltext: "Attributbeschreibung", is_optional: true }
@querytype			function
@function			seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: determine action
#
pre_qexpression
	proc: {
		this.action = this.query.action;
		this.typname	= this.query.typname;
		this.attribname = this.query.attribname;
		if (this.query.hasOwnProperty("attribvalue"))	this.attribvalue = this.query.attribvalue;
		if (this.query.hasOwnProperty("attribdesc"))	this.attribdesc = this.query.attribdesc;
		this.actlabel = "action_"+this.action;
		if (!this.labels[this.actlabel]) {
			this.errcoll.collect(null,"no handler for action \""+this.action+"\", no label \""+this.actlabel+"\" exists");
			break proc;
		}
	}
goto	@@actlabel@@

#
# <<TSTEPNUM:1>>: action "delete"
#
action_delete:
sql				delete from CCRW where RWTYP='TYPEATTRIB' and RWPARENT=::typname:: and RWNAME=::attribname::
named_params	typname, attribname
result_varname	dbres
post_qexpression
	this.result = new aux.Result({	resulttype: 'plain', 
									body:	"Attribut <code>"+this.attribname+"</code> von Typ <code>"+this.typname+"</code> gelöscht, "+
											this.dbres.resultobject.affectedRows+" Datensätze gelöscht",
									structure_has_changed: true });

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

#
# <<TSTEPNUM:3>>: action "update", determine attribute mode and if type is existing
#
action_update:
pre_qexpression
	switch (this.attribname) {
		case 'TYPNAME':
		case 'TYPETITLE':
		case 'TYPEDESC':
			this.attmode = this.attribname;
			break;
		default:
			this.attmode = 'plain';
	}
sql				select RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC from CCRW where RWTYP='TYPE' and RWPARENT='TYPES' and RWNAME=::typname::
named_params	typname
result_varname	dbres
post_qexpression
	proc: {
		let rc = this.dbres.getRowCount();
		if (rc==0) {
			this.errcoll.collect(null,"cannot set attribute \""+this.attribname+"\" of type \""+this.typname+"\", type does not (yet) exist",this.dbres);
			break proc;
		} else if (rc!=1) {
			this.errcoll.collect(null,"Type information about type \""+this.typname+"\" is incorrect, record count is "+rc,this.dbres);
			break proc;
		}
	}

#
# <<TSTEPNUM:4>>: action update, proceed according to attribute mode
#
goto	update_mode_update_@@attmode@@

#
# <<TSTEPNUM:5>>: action update mode update TYPNAME
#
update_mode_update_TYPNAME:
sql				update CCRW set RWNAME=::attribvalue:: where RWTYP='TYPE' and RWPARENT='TYPES' and RWNAME=::typname::
named_params	typname,attribvalue
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 TYPE "+this.typname+" record attribute "+this.attribname,this.dbres);
			break proc;
		}
		this.rc1 = rc;
	}

#
# <<TSTEPNUM:6>>: action update mode update TYPNAME, update subordinate type attribute records
#
sql				update CCRW set RWPARENT=::attribvalue:: where RWTYP='TYPEATTRIB' and RWPARENT=::typname::
named_params	typname,attribvalue
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 TYPE "+this.typname+" record attribute "+this.attribname,this.dbres);
			break proc;
		}
		this.rc2 = rc;
		this.result = new aux.Result({
			resulttype: 'plain', 
			body: "Datentyp \""+this.typname+"\" umbenannt auf \""+this.attribvalue+\"", "+this.rc1+" Datensätze verändert, "+this.rc2+" untergeordnete Attribute angepasst"});
	}

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

#
# <<TSTEPNUM:8>>: action update mode update TYPETITLE
#
update_mode_update_TYPETITLE:
sql				update CCRW set RWVALUE=::attribvalue:: where RWTYP='TYPE' and RWPARENT='TYPES' and RWNAME=::typname::
named_params	typname,attribvalue
result_varname	dbres
	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 TYPE "+this.typname+" record attribute "+this.attribname,this.dbres);
			break proc;
		}
		this.result = new aux.Result({
			resulttype: 'plain', 
			body: "Datentyp \""+this.typname+"\" Attribut \""+this.attribname+"\" geändert auf \""+this.attribvalue+\"", "+this.rc1+" Datensätze verändert"});
	}

#
# <<TSTEPNUM:9>>: action update mode update TYPETITLE end
#
end

#
# <<TSTEPNUM:10>>: action update mode update TYPEDESC
#
update_mode_update_TYPEDESC:
sql				update CCRW set RWDESC=::attribvalue:: where RWTYP='TYPE' and RWPARENT='TYPES' and RWNAME=::typname::
named_params	typname,attribvalue
result_varname	dbres
	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 TYPE "+this.typname+" record attribute "+this.attribname,this.dbres);
			break proc;
		}
		this.result = new aux.Result({
			resulttype: 'plain', 
			body: "Datentyp \""+this.typname+"\" Attribut \""+this.attribname+"\" geändert auf \""+this.attribvalue+\"", "+this.rc1+" Datensätze verändert"});
	}

#
# <<TSTEPNUM:11>>: action update mode update TYPEDESC end
#
end

#
# <<TSTEPNUM:12>>: action update, attribute node plain, determine if attribute already present
#
update_mode_update_plain:
sql				select RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC from CCRW where RWTYP='TYPEATTRIB' and RWPARENT=::typname:: and RWNAME=::attribname::
named_params	typname,attribname
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 attribute \""+this.attribname+"\" of type \""+this.typname+"\"",this.dbres);
			break proc;
		} else if (rc==0) {
			this.plainmode = "create";
		} else if (rc==1) {
			this.plainmode = "update";
		}
	}
then_goto	update_mode_plain_@@plainmode@@

#
# <<TSTEPNUM:13>>: update mode plain "create"
#
update_mode_plain_create:
sql				insert into CCRW(RWTYP,RWPARENT,RWNAME,RWVALUE,RWDESC) values('TYPEATTRIB',::typname::,::attribname::,::attribvalue::,::attribdesc::)
named_params	typname,attribname,attribvalue,attribdesc
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 TYPEATTRIB record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({	resulttype: 'plain', 
										body: "neues Attribut \""+this.attribname+"\" für Typ \""+this.typname+"\" angelegt, "+rc+" Datensätze verändert",
										structure_has_changed: true });
	}

#
# <<TSTEPNUM:14>>: update mode plain create end
#
end

#
# <<TSTEPNUM:15>>: update mode update plain attribute
#
update_mode_plain_update:
sql				update CCRW set RWVALUE=::attribvalue::,RWDESC=::attribdesc:: where RWTYP='TYPEATTRIB' and RWPARENT=::typname:: and RWNAME=::attribname::
named_params	typname,attribname,attribvalue,attribdesc
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 not-wellformed result updating TYPEATTRIB record",this.dbres);
			break proc;
		}
		this.result = new aux.Result({resulttype: 'plain', body: "Attribut \""+this.attribname+"\" für Typ \""+this.typname+"\" geändert, "+rc+" Datensätze verändert"});
	}

#
# <<TSTEPNUM:16>>: update mode plain update end
#
end