Copying named query results to tags based on column name

Continuing the discussion from Passing tag values to named query:

Now I want to go back the other way. I have a named query to which I pass a search term, and it returns a single row containing the following columns and their respective values. When I test the query using the Testing tab of the query builder, it returns something like this:

ProductCode    Description    WeightSP    ActualWeight
   12345       Product ABC      10.0         10.2

Now, I want to call this query based on a tag change, and have it load the values into a set of tags which have the same names, within the [default]PrevBatch tag folder, i.e. my tags are:

[default]PrevBatch/ProductCode
[default]PrevBatch/Description
[default]PrevBatch/WeightSP
[default]PrevBatch/ActualWeight

So, I put a tag change event on my triggering tag and have it call the named query. It returns a dataset.

Is there an equally elegant way to write these values into the tags that match the column names returned by the query?

Answering my own question - eventually got my head around the fact that trying to use the zip function is exactly the opposite of what I need to do here, since I need to separate lists to feed into the system.tag.writeBlocking function.

if currentValue.value and not initialChange:
	paths = []
	values = []
	data = system.db.runNamedQuery('My_ProjectName', 'My_QueryName', {'SearchTerm':'My_SearchTerm'})
	for col in range(data.getColumnCount()):
		paths.append(data.getColumnName(col))
		values.append(data.getValueAt(0,col))
	paths = ['[default]Process/PrevBatch/' + path for path in paths]
	system.tag.writeBlocking(paths,values)

Always interested in optimisations though, if anyone’s feeling creative!

1 Like

Since you seem to be familiar with list comprehensions, I’d use a few more, and take advantage of jython’s NetBeans automatic property syntax:

if currentValue.value and not initialChange:
	ds = system.db.runNamedQuery('My_ProjectName', 'My_QueryName', {'SearchTerm':'My_SearchTerm'})
	paths = ['[default]Process/PrevBatch/' + path for path in ds.columnNames]
	values = [ds.getValueAt(0, c) for c in range(ds.columnCount)]
	system.tag.writeBlocking(paths,values)
2 Likes

Thanks Phil, that simplifies things somewhat and tests out correctly.