Add a sublist of OR conditions with Query Builder

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

Add a sublist of OR conditions with Query Builder

Postby juan.cristobo » Wed Aug 10, 2016 11:28 am

Hi all, I have this (pseudo) code where I build a SQL query string:

Code: Select all
var values = ['value1', 'value2', 'value3'];
var query = "SELECT columnA FROM myTable WHERE columnB = 'someValue' AND (";

values.forEach(
   function(val) {
      query += " columnC = '"+val+"' OR ";
   }
);

query += " 1=1)"; //avoiding syntax error with a trailing 'OR'


I'd like to use Query Builder instead of using a query string, but I don't know how to add the list of OR conditions. Any ideas?
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: Add a sublist of OR conditions with Query Builder

Postby jdbruijn » Wed Aug 10, 2016 12:50 pm

Not tested, but I think it is something like this:
Code: Select all
   /**@type {QBSelect<db:/example_data/person>}*/
   var q = databaseManager.createSelect('db:/example_data/person');
   q.where.add(q.or
                   .add(q.columns.firstname.like('%jos%'))
                   .add(q.columns.firstname.like('%juan%')));
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Add a sublist of OR conditions with Query Builder

Postby juan.cristobo » Wed Aug 10, 2016 12:58 pm

Thanks Jos... It's ok if you know how many OR conditions you have, but if (in my example code) "values" array is a method parameter with unknown elements, you code doesn't work, I think.
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: Add a sublist of OR conditions with Query Builder

Postby sovanm » Wed Aug 10, 2016 1:59 pm

Hi Juan,

I hope this will help in passing an unknown number of conditions parameters into OR logical condition.

Code: Select all
// This can be obteined from any function
   /**@type {Array<String>}*/
   var projectNames = ['Done ','Locking System', 'Hello'];
   
   /**@type {QBSelect<db:/db/projects>}*/
   var query = databaseManager.createSelect('db:/rfs/projects')
   
   var condition = query.or;
   
   projectNames.forEach(function(val){
      condition.add(query.columns.project_name.like('%' +val+ '%'));
   });
   
   query.result.add(query.columns.project_id)
           .add(query.columns.project_name)
   query.where.add(condition);
   
   var ds = databaseManager.getDataSetByQuery(query,-1)
   application.output(ds.getAsHTML());


I hope this will help.

Thanks
Last edited by sovanm on Wed Aug 10, 2016 2:03 pm, edited 1 time in total.
sovanm
 
Posts: 99
Joined: Fri Oct 28, 2011 1:55 pm
Location: Bhubaneswar, India

Re: Add a sublist of OR conditions with Query Builder

Postby juan.cristobo » Wed Aug 10, 2016 2:03 pm

Thanks, Sova, I'll try it!
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: Add a sublist of OR conditions with Query Builder

Postby juan.cristobo » Thu Aug 11, 2016 10:28 am

It works, thank!
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: Add a sublist of OR conditions with Query Builder

Postby rgansevles » Fri Aug 12, 2016 4:58 pm

Juan,

In your original question, you did not ask for like-expressions, just fixed values.
In that case it is more simple to use isin:

Code: Select all
   /**@type {Array<String>}*/
   var values = ['A', 'B', 'C'];
   
   var query = datasources.db.example_data.book_nodes.createSelect();
   
   query.result.addPk();
   query.where.add(query.columns.node_id.eq(42))
              .add(query.columns.label_text.isin(values))
   
   var dataset = databaseManager.getDataSetByQuery(query,100)


Rob
Rob Gansevles
Servoy
User avatar
rgansevles
 
Posts: 1927
Joined: Wed Nov 15, 2006 6:17 pm
Location: Amersfoort, NL


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 17 guests

cron