Pass values to IN operator in a Worklight SQL adapter - javascript

I have started to work with SQL adapters in Worklight, but I do not understand how can I pass values to an IN condition when invoking my adapter procedure.

You will need to edit your question with your adapter's XML as well as implementation JavaScript...
Also, make sure to read the SQL adapters training module.
What you need to do is have your function get the values:
function myFunction (value1, value2) { ... }
And your SQL query will use them, like so (just as an example how to pass variables to any SQL query, doesn't matter if it contains an IN condition or not):
SELECT * FROM person where name='$[value1]' or id=$[value2];
Note the quotation marks for value1 (for text) and lack of for value2 (for numbers).

Related

How to pass a seed value to random order function in Sequelize

In SQL dialects you can sort by random and you can pass a seed to the random function in order to get a repeatable random order of rows.
In MySQL you'd do it like this:
SELECT * FROM `users` ORDER BY RAND("192.168.1.1")
I'm aware of how to use the RAND function when querying with Sequelize:
users.findAll({
order: [sequelize.random()],
});
I can't seem to figure out how to pass a seed to the random function.
I've looked at the docs: https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-method-random
And it looks like the sequelize.random() function doesn't take any parameters.
Is this possible?
If you wish your query will work only in MySQL then just use sequelize.fn:
users.findAll({
order: [sequelize.fn('RAND', '192.168.1.1')],
});
Absent any official documentation mention of the issue, the definition of the sequelize.random() method in sequelize.js (line 892) shows that it doesn't accept any parameters currently, and simply functions as a method by which to properly build a query dependent on the specific RDBMS configured for use.
As such, it is safe to say that, as of this writing, this is currently not supported in sequelize's master branch by default; #Anatoly's answer shows how you might be able to achieve this by leveraging sequelize.fn in your own project.

How do you receive a MySQL Stored Procedure OUT parameter as a variable in Javascript?

Disclaimer; my background is in C#, so I'm not familiar with JavaScript more than at a cursory level; however, I was reviewing some code with one of our developers today and he was receiving some strange behavior of return results "merging" with pre-existing return results. I was very surprised to see every single JavaScript example of calling a Stored Procedure uses a Global Variable on the MySQL side to store the out parameter! This blows my mind and IMO is a big "no no" relative to writing code. Take the below example:
exports.updateRasSql = 'CALL update_ras_data(?, ?, #out_result, #out_result_value);
What it comes down to is we do NOT want to assign the OUT parameter variables to a MySQL global variable. We want the OUT values to be assigned to internal JavaScript variables, but any time we try to do this (using var or let as a definer), MySQL returns:
Error: ER_SP_NOT_VAR_ARG: OUT or INOUT argument 3 for routine empowercrm_main.update_ras_data is not a variable or NEW pseudo-variable in BEFORE trigger
So here's the question: How do we call a MySQL Stored Procedure from Node/JavaScript and have it return the OUT variables to internal JavaScript variables and not MySQL global variables?
You should return a result set (make query in a procedure) instead of using OUT-variables. The only way of getting the value of a OUT-variable is to select it after you have executed the procedure. It's easier just to handle the result set.

How to implement Search using azure-mobile-apps-js-client

I have a hybrid cordova app which uses javascript azure-mobile-apps-js-client for communicating with server. Data is also synchronized in sqlite DB on device.
I need to implement search against Person entities by their Full Names.
All persons which matches search term(contains term inside Full Name) should be returned.
Something like "LIKE" in SQL.
I did read this article but didn't found a way to do that.
Seems like this client is supporting only operations like =, >, <.
Does it mean I need to retrieve all records from table and filter them on client (which sounds weird to me) or I just missing something ?
Thanks.
Finally found an option to do that using javascript string.indexOf function.
//Declare a query
function queryFunction(term){
return this.FullName.indexOf(term) != -1
}
//Pass it to where function
table
.where(queryFunction, term)
.read()
.then(success, failure);

Parsing and constructing filtering queries similiar to SQL WHERE clause in Python/JavaScript

I am building a query engine for a database which is pulling data from SQL and other sources. For normal use cases the users can use a web form where the use can specify filtering parameters with select and ranged inputs. But for advanced use cases, I'd like to to specify a filtering equation box where the users could type
AND, OR
Nested parenthesis
variable names
, <, =, != operators
So the filtering equation could look something like:
((age > 50) or (weight > 100)) and diabetes='yes'
Then this input would be parsed, input errors detected (non-existing variable name, etc) and SQL Alchemy queries built based on it.
I saw an earlier post about the similar problem https://stackoverflow.com/a/1395854/315168
There seem to exist several language and mini-language parsers for Python http://navarra.ca/?p=538
However, does there exist any package which would be out of the box solution or near solution for my problem? If not what would be the simplest way to construct such query parser and constructor in Python?
Have a look at https://github.com/dfilatov/jspath
It's similar to xpath, so the syntax isn't as familiar as SQL, but it's powerful over hierarchical data.
I don't know if this is still relevant to you, but here is my answer:
Firstly I have created a class that does exactly what you need. You may find it here:
https://github.com/snow884/filter_expression_parser/
It takes a list of dictionaries as an input + filter query and returns the filtered results. You just have to define the list of fields that are allowed plus functions for checking the format of the constants passed as a part of filter expression.
The filter expression it ingests has to have the following format:
(time > 45.34) OR (((user_id eq 1) OR (date gt '2019-01-04')) AND (username ne 'john.doe'))
or just
username ne 'john123'
Secondly it was foolish of me to even create this code because dataframe.query(...) from pandas already does almost exactly what you need: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html

Creating a BIRT Data Set with Dynamic Data - ORA-01722

Having some trouble getting BIRT to allow me to create a Data Set with Parameters that are set at run time.
The SQL that is giving me the error is:
...
FROM SPRIDEN, SPBPERS P, POSNCTL.NBRJOBS X, NHRDIST d1
where D1.NHRDIST_PAYNO between '#PAYNO_BEGIN' and '#PAYNO_BEGIN'
AND D1.NHRDIST_YEAR = '#YEAR'
...
I have my Report Parameters defined as PaynoBegin, PaynoEnd, Year
I also have a Data Set script set for beforeOpen as follows:
queryText = String (queryText).replace ("#PAYNO_END", Number(params["PaynoEnd"]));
queryText = String (queryText).replace ("#PAYNO_BEGIN", Number(params["PaynoBegin"]));
queryText = String (queryText).replace ("#YEAR", Number(params["Year"]));
The problem seems to be that the JDBC can't get the ResultSet from this, however I have 10 other reports that work the same way. I have commented out the where clause and it will generate the data set. I also tried breaking the where clause out into two and clauses with <= and >=, but it still throws a ORA-01722 invalid number error on the line.
Any thoughts on this?
Two quite separate thoughts:
1) You have single quotes around each of your parameters in the query, yet it appears as though each one is a numeric - try removing the single quotes, so that the where clause looks like this:
where D1.NHRDIST_PAYNO between #PAYNO_BEGIN and #PAYNO_BEGIN
AND D1.NHRDIST_YEAR = #YEAR
Don't forget that all three parameters should be required. If the query still returns an error, try replacing #PAYNO_BEGIN, #PAYNO_BEGIN and #YEAR with hardcoded numeric values in the query string, and see whether you still get an error.
2) You are currently using dynamic SQL - amending query strings to replace specified markers with the text of the entered parameters. This makes you vulnerable to SQL Injection attacks - if you are unfamiliar with the term, you can find a simple example here.
If you are familiar with the concept, you may be under the impression that SQL Injection attacks cannot be implemented with numeric parameters - Tom Kite has recently posted a few articles on his blog about SQL Injection, including one that deals with a SQL Injection flaw using NLS settings with numbers.
Instead, you should use bind parameters. To do so with your report, amend your query to include:
...
FROM SPRIDEN, SPBPERS P, POSNCTL.NBRJOBS X, NHRDIST d1
where D1.NHRDIST_PAYNO between ? and ?
AND D1.NHRDIST_YEAR = ?
...
instead of the existing code, remove the queryText replacement code from the beforeOpen script and map the three dataset parameters to the PaynoBegin, PaynoEnd and Year report parameters respectively in the Dataset Editor. (You should also change any other replaced text in your query text to bind parameter markers (?) and map dataset parameters to them as required.)

Categories

Resources