Way to get the parameters associated with a named query?

When you bind to a named you get a list of the parameters to feed to the named query -
image

Is there a way to get this list programatically? I want to be able to check the dictionary I feed to my system.db.runNamedQuery against it before running it so I can log warnings for missing parameters - I’ve had typos in my dictionary keys before that caused an error that were very hard to run down.

I get why the system.db.runNamedQuery lets dictionary keys that don’t correlate with the parameters fall through - it’s great for my design, so I don’t have to del dictionary['someKey'] to every other non-used key (that might be used for other logic) before running the query, but I do want to be able to at least catch the typos for myself in a log.

I want to modify my Form classses create function to be like

def __create(self):
    createNamedQuery = self.namedQueryFolder + '/create'
    self.logger.info("running create named query with payload")
    self.logger.info(str(self.newData))
    namedQueryParameters = someFunctionOn(createNamedQuery)
    # convert namedQueryParameters to dictionary
    for key in namedQueryParameters.keys():
        if key not in self.newData:
            self.logger.warn("%s in named query was not used"%(key))
    self.idx = system.db.runNamedQuery(createNamedQuery, self.newData, tx=self.tx, getKey=1)

I just don’t know what someFunctionOn() should be.

A snippet from my BlobServe module:

            NamedQueryManager nqm = GatewayHook.getMyContext().getNamedQueryManager();
            NamedQuery nq = nqm.getQueryFromPath(project, namedQueryPath);
...
            List<Parameter> paramDefs = nq.getParameters();

Start here:

https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.1/com/inductiveautomation/ignition/common/db/namedquery/package-summary.html

3 Likes

This probably isn’t the slickest way, but according to the manual any version beyond 8.1.6, you can get this information from the resource.json file from

%installationDirectory%\data\projects\PROJECTNAME\ignition\named-query\QUERYNAME

I am having trouble getting the GatewayHook part. I am lost in the weeds with the documentation -
image

Looking at your first line I searched for getNamedQueryManager and see this, and given you use a getMyContext, I highlighted what I think is the right one -

Doing import com.inductiveautomation.ignition.gateway.model.GatewayContext throws me an import error that there is no module named model.

I know this is a lapse in my knowledge with reading java documentaiton/java knowledge in general like interfaces etc. How can I get the GatewayHook?

Sorry, I assumed too much. The key is to get the GatewayContext. Outside of a module, the typical (but unsupported) method is to import IgnitionGateway then use .get() to obtain the platform singleton.

{ This stuff only works in gateway scope. Delegate to gateway scope via a message handler when in designer or vision client scopes. }

Ah just realized that when trying. You think it would be problematic to be calling and using IgntionGateway to get these named query parameters somewhat frequently? Or would it be safe? My second thought is to write a script to run on the gateway context that goes through all my named queries, saves the path and their parameters into a database table in case it’s problematic.

I wouldn’t be troubled. These are the interfaces that run the Queries themselves, so I would expect them to be well-optimized. If you are concerned, at most I would cache in a script module top level dictionary. That would be wiped when the project is saved.

1 Like

This is where I ended up with, which I call with a system.util.sendRequest

def getNamedQueryParameters(payload):
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	project = payload['project']
	query = payload['query']
	namedQuery = IgnitionGateway.get().getNamedQueryManager().getQueryFromPath(project, query)
	parameters = namedQuery.getParameters()
	listOfParams = [param.getIdentifier() for param in parameters]
	logger.info(str(listOfParams))
	return listOfParams

And this logs exaclty what I need so I can utlize it.

I am testing it in script console now though and I actually am not getting the result - is that a known thing with testing sendRequest in script console?

project = 'PSM4_Project'
messageHandler = 'getNamedQueryParameters'
d = {'project':project, 'query':'forms/Customer/get'}
result = system.util.sendRequest(project, messageHandler, d)
print str(result)
print str(type(result))

Gives me None/NoneType, but I would expect the list of params that are logged. I assume it will work in the client, I just like to try to build these things in script console as I find it easier to debug and fix issues.

EDIT: Issue with sendRequest was of course my own mistake. I had

def handleMessage(payload):
	classes.helpers.getNamedQueryParameters(payload)

but needed

def handleMessage(payload):
	return classes.helpers.getNamedQueryParameters(payload)
4 Likes