If robotframework has keyword to handle exception? - exception

I write a keyword like this:
My Keyword
[Argument] ${param}
Internal Keyword 1
...
Internal Keyword n
Run When Any of The Upper Keywords Failed
I want the keyword run as its' description, who can tell me how to achieve it?

There are a few ways to achieve this, one is to use Run Keyword And Return Status - it returns a boolean true/false did the wrapped keyword pass or fail. And based on that, you can run the "exception" keyword:
${passed 1}= Run Keyword And Return Status Internal Keyword 1
${passed 2}= Run Keyword And Return Status Internal Keyword 2
Run Keyword If not ${passed 1} or not ${passed 2} Run When Any of The Upper Keywords Failed

Related

Rails6 how to do `XXX.update_all` with mysql function

I would like to use update_all in order to update all records in a database.
Let me assume that the name of the attribute to be update is price.
I succeeded command A.
A
MyModel.update_all(price: "$500")
However, I would like to mysql function(e.g. IF, CONCAT, arithmetic...) in updating.
so I tried command B, but failed and the string value CONCAT('$', 500) was stored.
B
MyModel.update_all(price: "CONCAT('$', 500)")
I succeeded when I tried C, but I don't want use it because of the SQL injection risks.
C
MyModel.update_all("price = CONCAT('$', 500)")
How can I do that without any risk of SQL injection attacks?
Here, I have to use sql function here for some reasons.
Thank you in advance.
You need to create methods corresponding to allowable sql functions that take the params as input. So if you want to provide "CONCAT(..,...)" then user should be able to call a method like this. You can extent it to check type of arguments are allowed and add checks there as well.
def allowed_functions(func_name, all_args_here)
case func_name
when 'concat' then "CONCAT(#{all_args_here})"
end
end

Spring data Couchbase #n1ql.fields query

I'm trying to make a N1QL based query on Spring Data Couchbase. The documentation says
#n1ql.fields will be replaced by the list of fields (eg. for a SELECT clause) necessary to reconstruct the entity.
My repository implementation is this one:
#Query("#{#n1ql.fields} WHERE #{#n1ql.filter}")
List<User> findAllByFields(String fields);
And I'm calling this query as follows:
this.userRepository.findAllByFields("SELECT firstName FROM default");
I'm getting this error:
Caused by: org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Unable to execute query due to the following n1ql errors:
{"msg":"syntax error - at AS","code":3000}
After a little bit of researching, I also tryed:
#Query("SELECT #{#n1ql.fields} FROM #{#n1ql.bucket} WHERE #{#n1ql.filter}")
With this query, I don't get an error, I get all the documents stored but only the ID the other fields are set to null, when my query tries to get the firstName field.
this.userRepository.findAllByFields("firstName");
Anyone knows how to do such a query?
Thank you in advance.
You're misunderstanding the concept, I encourage you to give the documentation more time and see more examples. I'm not sure what exactly you're trying to achieve but I'll throw some examples.
Find all users (with all of their stored data)
#Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter}")
List<User> findAllUsers();
This will basically generate SELECT meta().id,_cas,* FROM bucket WHERE type='com.example.User'
Notice findAllUsers() does not take any parameters because there are no param placeholders defined in the #Query above.
Find all users where firstName like
#Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND firstName like $1")
List<User> findByFirstNameLike(String keyword);
This will generate something like the above query but with an extra where condition firstName like
Notice this method takes a keyword because there is a param placeholder defined $1.
Notice in the documentation it says
#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND test = $1
is equivalent to
SELECT #{#n1ql.fields} FROM #{#n1ql.bucket} WHERE
#{#n1ql.filter} AND test = $1
Now if you don't want to fetch all the data for user(s), you'll need to specify the fields being selected, read following links for more info
How to fetch a field from document using n1ql with spring-data-couchbase
https://docs.spring.io/spring-data/couchbase/docs/2.2.4.RELEASE/reference/html/#_dto_projections
I think you should try below query, that should resolve the issue to get fields based parameter you have sent as arguments.
Please refer blow query.
#Query("SELECT $1 FROM #{#n1q1.bucket} WHERE #{#n1ql.filter}")
List findByFirstName(String fieldName);
Here, bucket name resolve to the User entity and and n1ql.filter would be a default filter.

Sqlinjection in where Clause

I am testing an application and encountered a bit unique issue I have found that application is sending parameters like
?$filter=ModuleName+ne+'Bookings'+and+ModuleName+eq+'Transport'+and+(ContactID+eq+null+and+IsToBeShown+eq+true)+&$orderby=ReportName
Obviously I can add and +1+eq+1 and all results are shown but if I try to terminate the query like using (;, or ') it gives me error.
Kind of not sure how to terminate the query and add a union etc. clause to extract data .
Any thoughts are welcome
It seams that $filter is used in where clause and probably is added to some other hard coded conditions. To get all the result you need to add or 1 eq 1 (or instead of and).
It may make a difference if other condidions are added before or after $filter.
Try $filter=union all select ... where 1 eq 1 but remember that column list must be the same in all unioned queries. You don't need to terminate the query with ;
Replace spaces with +, I wrote spaces to make it easier to read.

erlang - exception error: no function clause matching

I've created a concatenate/1 function, which should concatenate a list of lists and atoms:
concatenate(L)->
concatenate(L,[]).
concatenate([],R)->
reverse(R);
concatenate([H|T],R) when is_atom(H) ->
concatenate(T,[H|R]);
concatenate([[]|L],R)->
concatenate(L,R);
concatenate([[H|T]|L],R)->
concatenate([T|L],[H|R]).
However I get the error:
15> ml:concatenate([[1,2],4,[3,4,5],[5,6]]).
** exception error: no function clause matching
ml:concatenate([4,[3,4,5],[5,6]],[2,1]) (ml.erl, line 27)
which I believe should match, because of the second concatenate/2 clause.
May I ask, what am I doing wrong here?
Following the reasoning in Viacheslav Kovalev's answer, given that I want to handle atoms and numbers here, I have come up with the following solution:
concatenate([],R)->
R;
concatenate([H|T],R) when not(is_list(H)) ->
reverse(concatenate(T,[H|R]));
concatenate([[]|L],R)->
concatenate(L,R);
concatenate([[H|T]|L],R)->
concatenate([T|L],[H|R]).
In the second clause you are using when is_atom(H) guard, which fails to match clause (because H in this case is number). Change it to when is_number(H).

With MySQL Can the Query LIMIT use an optional value in a PreparedStatement?

Specifically, in my prepared statement, can I do something akin to:
... LIMIT IFNULL(?,1000)
meaning, use the limit passed in as a parameter, but if passed as null use 1000?
So far, my experimenting seems to indicate that a function cannot be used for LIMIT.