How to escape single quote on MSSQL query into JDBC variable - jmeter

Using JDBC at Jmeter I need to escape single quote performed on variable.
My original query is:
select id from [Teams] where name = '${team}'.
But, when I got a team like: Ain M'lila, the query is not executed
What I tried, and not working is:
DECLARE #NevName nvarchar
SET #NevName = REPLACE({${team}, '''', ''''''')
select id from [test8].[Team] where name = #NevName
Any solution is appreciated

In order to escape a single quote you need to add another single quote to it
In particular your case you can escape ' with an extra ' using __groovy() function like:
${__groovy(vars.get('team').replaceAll("\'"\, "''"),)}
Demo:

thanks to Dmitri T for his efforts, but the solution for me was:
JSR223 PreProcessor: and inside:
String userString = vars.get("team");
String changedUserString = userString.replace("'","''");
vars.put("teamChanged", changedUserString);
and then used as into the query:
select id from [test8].[Team] where name = '${teamChanged}'

Related

How to append a delimiter between multiple values coming from a repeating field in xquery

I have a xml file which has a repeating element generating multiple values.
I would like to split all the values generated from that xpath delimited by any delimiter like , |_
I have tried the following which did not work -
tokenize(/*:ShippedUnit/*:Containment/*:ContainerManifest/*:Consignments/*:Consignment/*:ConsignmentHeader/*:ConsignmentRef, '\s')
replace(/*:ShippedUnit/*:Containment/*:ContainerManifest/*:Consignments/*:Consignment/*:ConsignmentHeader/*:ConsignmentRef," ","_")
example :
Now getting - CBR123 CBR678 CBR656
Expecting to get - CBR123|CBR678|CBR656
Note : In some transactions, there can be only one value present for that xpath. And therefore replace doesnot work here
To achieve the expected result assuming the sample source XML added to the comments in the original post, use the fn:string-join() function:
string-join(
//ConsignmentRef,
"|"
)
This will return:
CBR00464833N|CBR01264878K
For more on this function, see https://www.w3.org/TR/xpath-functions-31/#func-string-join.
Another option in XQuery 3.1 would be
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method 'text';
declare option output:item-separator '|';
//ConsignmentRef

Multiline string interpolation on Google Workflows

I'm trying to use Google Workflows to do some automated BigQuery scheduling tasks. The requirement is to run a query on multiple datasets as the following:
- execute_query_job:
call: execute_query_job
args:
query_text: >-
SELECT
* EXCEPT(row_number)
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY uuid) row_number
FROM
`project.${database_id}.table`)
WHERE
row_number = 1
however, this doesn't work since the string is interpreted as is and no interpolation happened.
The ${} syntax will not span over multiple lines and ansible syntax with {{ var }} also did not work.
Try to change the query to a single line in a similar fashion as:
- execute_query_job:
call: execute_query_job
args:
query_text: ${"SELECT * EXCEPT(row_number) FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY uuid) row_number FROM `project."+database_id+".table`) WHERE row_number = 1"}
Notice that as per the Workflow's docs:
Variables can be assigned to a particular value or to the result of an expression.
If that doesn't work notice that making a POST request to the BigQuery's API jobs.insert method will allow you to specify a JobConfiguration, where you could change the defaultDataset field and change this values for each different dataset at each iteration. The following sample shows how to make iterations based on the values of an array in Workflows.
You may want take a look at the official document. you could concat the variable by multiple lines.
- assign_vars:
assign:
- string: "say"
- string: ${string+" "+"hello"}
- string: ${string+" "+"to the world"}
This is a necessary feature...
But in the meantime I have a more elegant solution:
- assign_vars:
assign:
- multilineVar: |
#!/bin/bash
echo Hi ${workflowVar}!
- workflowVar: StackOverflow
- multilineExapanded: ${ text.replace_all(multilineVar, ${workflowVar}, workflowVar) }

pg: exec_params not replacing parameters?

First time using pg gem to access postgres database. I've connected successfully and can run queries using #exec, but now building a simple query with #exec_params does not seem to be replacing parameters. I.e:
get '/databases/:db/tables/:table' do |db_name, table_name|
conn = connect(db_name)
query_result = conn.exec_params("SELECT * FROM $1;", [table_name])
end
results in #<PG::SyntaxError: ERROR: syntax error at or near "$1" LINE 1: SELECT * FROM $1; ^ >
This seems like such a simple example to get working - am I fundamentally misunderstanding how to use this method?
You can use placeholders for values, not for identifiers (such as table and column names). This is the one place where you're stuck using string interpolation to build your SQL. Of course, if you're using string wrangling for your SQL, you must be sure to properly quote/escape things; for identifiers, that means using quote_ident:
+ (Object) quote_ident(str)
Returns a string that is safe for inclusion in a SQL query as an identifier. Note: this is not a quote function for values, but for identifiers.
So you'd say something like:
table_name = conn.quote_ident(table_name)
query_result = conn.exec("SELECT * FROM #{table_name}")

How to use "Result Variable Name" in JDBC Request object of Jmeter

In JMeter I added the configuration for oracle server. Then I added a JDBC request object and put the ResultSet variable name to status.
The test executes fine and result is displayed in treeview listener.
I want to use the variable status and compare it with string but jmeter is throwing error about casting arraylist to string.
How to retrieve this variable and compare with string in While Controller?
Just used some time to figure this out and think the accepted answer is slightly incorrect as the JDBC request sampler has two types of result variables.
The ones you specify in the Variable names box map to individual columns returned by your query and these you can access by saying columnVariable_{index}.
The one you specify in the Result variable name contains the entire result set and in practice this is a list of maps to values. The above syntax will obviously not work in this case.
The ResultSet variable returned with JDBC request in JMeter are in the for of array. So if you want to use variable status, you will have to use it with index. If you want to use the first(or only) record user status_1. So you need to use it like status_{index}.
String host = vars.getObject("status").get(0).get("option_value");
print(host);
log.info("----- " + host);
Form complete infromation read the "yellow box" in this link:
http://jmeter.apache.org/usermanual/component_reference.html#JDBC_Request
Other util example:
http://jmeter.apache.org/usermanual/build-db-test-plan.html
You can use Beanshell/Groovy (same code works) in JSR233 PostProcessor to work with “Result Variable Name” from JDBC Request like this:
ArrayList results = vars.getObject("status");
for (HashMap row: results){
Iterator it = row.entrySet().iterator();
while (it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
log.info(pair.getKey() + "=" + pair.getValue());
}
}
Instead of output to log replace with adding to string with delimiters of your choice.

Using YQL multi-query & XPath to parse HTML, how to escape nested quotes?

The title is more complicated than it has to be, here's the problem query.
SELECT *
FROM query.multi
WHERE queries="
SELECT *
FROM html
WHERE url='http://www.stumbleupon.com/url/http://www.guildwars2.com'
AND xpath='//li[#class=\"listLi\"]/div[#class=\"views\"]/a/span';
SELECT *
FROM xml
WHERE url='http://services.digg.com/1.0/endpoint?method=story.getAll&link=http://www.guildwars2.com';
SELECT *
FROM json
WHERE url='http://api.tweetmeme.com/url_info.json?url=http://www.guildwars2.com';
SELECT *
FROM xml
WHERE url='http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.guildwars2.com';
SELECT *
FROM json
WHERE url='http://www.reddit.com/button_info.json?url=http://www.guildwars2.com'"
Specifically this line,
xpath='//li[#class=\"listLi\"]/div[#class=\"views\"]/a/span'
It's problematic because of the quoting, I have to nest them three levels deep and I've run out of quote characters to use. I've tried the following variations without success:
//no attribute quoting
xpath='//li[#class=listLi]/div[#class=views]/a/span'
//try to quote attribute w/ backslash & single quote
xpath='//li[#class=\'listLi\']/div[#class=\'views\']/a/span'
//try to quote attribute w/ backslash & double quote
xpath='//li[#class=\"listLi\"]/div[#class=\"views\"]/a/span'
//try to quote attribute with double single quotes, like SQL
xpath='//li[#class=''listLi'']/div[#class=''views'']/a/span'
//try to quote attribute with double double quotes, like SQL
xpath='//li[#class=""listLi""]/div[#class=""views""]/a/span'
//try to quote attribute with quote entities
xpath='//li[#class="listLi"]/div[#class="views"]/a/span'
//try to surround XPath with backslash & double quote
xpath=\"//li[#class='listLi']/div[#class='views']/a/span\"
//try to surround XPath with double double quote
xpath=""//li[#class='listLi']/div[#class='views']/a/span""
All without success.
I don't see much out there about escaping XPath strings but everything I've found seems to be variations on using concat (which won't help because neither ' nor " are available) or html entities. Not using quotes for the attributes doesn't throw an error but fails because it's not the actual XPath string I need.
I don't see anything in the YQL docs about how to handle escaping. I'm aware of how edge-casey this is but was hoping they'd have some sort of escaping guide.
You need to escape whatever character is delimiting your XPath query with a double backslash... in other words:
SELECT * FROM query.multi
WHERE queries="
SELECT *
FROM html
WHERE url='http://www.stumbleupon.com/url/http://www.guildwars2.com'
AND xpath='//li[#class=\\'listLi\\']/div[#class=\\'views\\']/a/span';
SELECT *
FROM xml
WHERE url='http://services.digg.com/1.0/endpoint?method=story.getAll&link=http://www.guildwars2.com';
SELECT *
FROM json
WHERE url='http://api.tweetmeme.com/url_info.json?url=http://www.guildwars2.com';
SELECT *
FROM xml
WHERE url='http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.guildwars2.com';
SELECT *
FROM json
WHERE url='http://www.reddit.com/button_info.json?url=http://www.guildwars2.com'"
(try this in the YQL console)
I've come up with a solution that doesn't really answer my original question but does solve the problem.
The data.html.cssselect table will take a CSS selector & parse it into an XPath, avoiding the nasty escaping issues.
SELECT *
FROM query.multi
WHERE queries="
SELECT *
FROM data.html.cssselect
WHERE url='http://www.stumbleupon.com/url/http://www.guildwars2.com'
AND css='li.listLi div.views a span';
SELECT *
FROM xml
WHERE url='http://services.digg.com/1.0/endpoint?method=story.getAll&link=http://www.guildwars2.com';
SELECT *
FROM json
WHERE url='http://api.tweetmeme.com/url_info.json?url=http://www.guildwars2.com';
SELECT *
FROM xml
WHERE url='http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.guildwars2.com';
SELECT *
FROM json
WHERE url='http://www.reddit.com/button_info.json?url=http://www.guildwars2.com'"

Resources