Using a variable for a column in QB where clause

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Using a variable for a column in QB where clause

Postby huber » Tue Nov 13, 2018 6:38 pm

Hi

Is it possible to use in the QueryBuilder where clause variables? Something like:

Code: Select all
var tableColumn = getColumn();
var value = getValue();

var query = datasources.db.<db>.<table>.createSelect();
...
...
query.where
   .add(tableColumn.eq(value));


Regards,
Robert Huber
7r AG, Switzerland
SAN Developer
http://www.seven-r.ch
User avatar
huber
 
Posts: 516
Joined: Mon May 14, 2012 11:31 pm

Re: Using a variable for a column in QB where clause

Postby paronne » Wed Nov 14, 2018 9:58 am

Hi,

sorry, i am not sure i understand correctly your question; is not clear to me if you want to build a query dinamically for any x column, or if you are trying to Get the values set on an existing Query/QueryBuilder.

If you are simply looking for a way to construct dinamically your query, using any column X, well yes that's possible; see my sample below.

Code: Select all
// columnName can be any column of dataSource customers.
var columnName = "city";
var query = datasources.db.example_data.customers.createSelect();
var qbColumn = query.getColumn(columnName);
query.result.add(qbColumn);
query.where.add(qbColumn.like(value));
query.sort.add(qbColumn.asc);
var dataset = databaseManager.getDataSetByQuery(query,-1);


Instead if you are looking for something else, can you tell us more on what kind of API are you looking for and most importantly what are you trying to achieve exactly ? Perhaps there is a different pattern to get to your desired result.

Regards,
Paolo
paronne
 
Posts: 202
Joined: Fri Nov 02, 2012 3:21 pm

Re: Using a variable for a column in QB where clause

Postby huber » Wed Nov 14, 2018 4:02 pm

Hi Paolo

You perfectly answered my question! I am just having two different columns depending on a selected tab and wanted to have the query a bit dynamic.

The missing piece of code for me was the following line:
Code: Select all
var qbColumn = query.getColumn(columnName);


Now the QB Select works as expected.

Many thanks and regards,
Robert Huber
7r AG, Switzerland
SAN Developer
http://www.seven-r.ch
User avatar
huber
 
Posts: 516
Joined: Mon May 14, 2012 11:31 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 7 guests

cron