How to write and in one Compare in GOSU - gosu

I have a query below and need to combine the 2nd and 3rd .compare condition with 'AND' operator
var query = ii.join(InvoiceItem#PolicyPeriod)
.compare(PolicyPeriod#Plan, Relop.NotEquals, xyzPlan)
.compareIn(PolicyPeriod#ProdType, {Prod.TC_FREE, Prod.TC_UNIQ})
.compareIn(PolicyPeriod#Elig,{OffElig.TC_Yes})
Second compareIn statement should be like below in SQL but do not know how to combine two variables in one compare with AND operator.
if ( prodtype in ('FREE','UNIQ') and Elig = 'YES')
Any Advice!!

You can use .and() method from the QueryAPI:
var query = ii.join(InvoiceItem#PolicyPeriod)
.compare(PolicyPeriod#Plan, Relop.NotEquals, xyzPlan)
.and(\and1 -> {
and1.compareIn(PolicyPeriod#ProdType, {Prod.TC_FREE, Prod.TC_UNIQ})
and1.compareIn(PolicyPeriod#Elig, {OffElig.TC_Yes})
})
In the same manner, you can use .or() method or even combine and() and or() in one statement.

Related

DynamoDB Java SDK query to match items in a list

I'm trying to use SQL IN clause kind of feature in dynamoDB. I tried using withFilterExpression but I'm not sure how to do it. I looked at similar questions as they were too old. Is there a better method to do this? This is the segment of code I have got. I have used a static List as example but it is actually dynamic.
def getQuestionItems(conceptCode : String) = {
val qIds = List("1","2","3")
val querySpec = new QuerySpec()
.withKeyConditionExpression("concept_id = :c_id")
.withFilterExpression("question_id in :qIds") // obviously wrong
.withValueMap(new ValueMap()
.withString(":c_id", conceptCode));
questionsTable.query(querySpec);
}
I need to pass qID list to fetch results similar to IN clause in SQL Query.
Please refer to this answer. Basically you need to form key list/value list dynamically
.withFilterExpression("question_id in (:qId1, :qId2, ... , :qIdN)")
.withValueMap(new ValueMap()
.withString(":qId1", ..) // just do this for each element in the list in a loop programmatically
....
.withString(":qIdN", ..)
);
Mind there is a restriction on maxItems in 'IN'

Xquery variable concat based on condition

I am trying to concat the value of an element based on certain condition, but unable to do so. What's wrong here?
For below given sample structure, I need to concat the value of CID based upon OutcomeCode code. Say if we have OutcomeCode as OC and PC, then we should display concatenated value of CId in a string variable.
<v4:ValidateResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v4="http://service.com/v4">
<v4:Details>
<v4:Detail>
<v4:CId>001</v4:CId>
</v4:Detail>
<v4:OutcomeCode>FC</v4:OutcomeCode>
</v4:Details>
<v4:Details>
<v4:Detail>
<v4:CId>002</v4:CId>
</v4:Detail>
<v4:OutcomeCode>PC</v4:OutcomeCode>
</v4:Details>
<v4:Details>
<v4:Detail>
<v4:CId>003</v4:CId>
</v4:Detail>
<v4:OutcomeCode>OC</v4:OutcomeCode>
</v4:Details>
</v4:ValidateResponse>
Here is my transformation
as xs:string
{
for $Details in $ValidateResponse /*:Details
let $OutcomeCode := data($Details/*:OutcomeCode)
return
if (($OutcomeCode ='OC') or ($OutcomeCode='PC'))
then
contact('CID is-',data($Details/*:Detail/*:CId))
else
fn:data('Technical_Check')
};
I am unable to get concat values.
Expected result should be like: CID is- 002,003
as these 2 meet the OC and PC condition.
You could simplify this for loop and combine the criteria into a single XPath to select the CId from Details that have OutcomeCode of "OC" or "PC".
Then, use string-join() in order to produce a comma separated value.
Then, use concat() to produce a string with the prefix and the CSV value:
concat('CID is- ',
string-join(
$ValidateResponse/*:Details[*:OutcomeCode =('OC','PC')]/*:Detail/*:CId,
",")
)

Passing list of search string in contains in FilterExpression

Is there any way to pass a list of search strings in the contains() method of FilterExpression in DynamoDb?
Something like below:
search_str = ['value-1', 'value-2', 'value-3']
result = kb_table.scan(
FilterExpression="contains (title, :titleVal)",
ExpressionAttributeValues={ ":titleVal": search_str }
)
For now I can only think of looping through the list and scanning the table multiple times (as in below code), but I think it will be resource heavy.
for item in search_str:
result += kb_table.scan(
FilterExpression="contains (title, :titleVal)",
ExpressionAttributeValues={ ":titleVal": item }
)
Any suggestions.
For the above scenario, the CONTAINS should be used with OR condition. When you give array as input for CONTAINS, DynamoDB will check for the SET attribute ("SS", "NS", or "BS"). It doesn't looks for the sub-sequence on the string attribute.
If the target attribute of the comparison is of type String, then the
operator checks for a substring match. If the target attribute of the
comparison is of type Binary, then the operator looks for a
subsequence of the target that matches the input. If the target
attribute of the comparison is a set ("SS", "NS", or "BS"), then the
operator evaluates to true if it finds an exact match with any member
of the set.
Example:-
movies1 = "MyMovie"
movies2 = "Big New"
fe1 = Attr('title').contains(movies1)
fe2 = Attr('title').contains(movies2)
response = table.scan(
FilterExpression=fe1 or fe2
)
a little bit late but to allow people to find a solution i give here my method.
lets assume that in your DB you have a props called 'EMAIL you want to filter your scan on this EMAIL with a list of value. you can proceed as following.
list_of_elem=['mail1#mail.com','mail2#mail.com','mail3#mail.com']
#set an empty string to create your query
stringquery=""
# loop each element in your list
for index,value in enumerate(list_of_elem):
# add your query of contains with mail value
stringquery=stringquery+f"Attr('EMAIL').contains('{value }')"
# while your value is not the last element in list add the 'OR' operator
if index < len(list_of_elem)-1:
stringquery=stringquery+ ' | '
dynamodb = boto3.resource('dynamodb')
# Use eval of your query string to parse the string as filter expression
tableUser = dynamodb.Table('mytable')
tableUser.scan(
FilterExpression=eval(stringquery)
)

Using the replace function in firestore security rules

I'm struggling with Firestore security rules. I want to check on a value that needs the replace function, i.e. an e-mail address. I can find some documentation in the general security docs, but that does not seem to work with Firestore.
For example this works:
allow write: if resource.data.members.data[(request.auth.token.email)] in ["admin"];
but this doesn't (and I changed the key in the members object accordingly):
allow write: if resource.data.members.data[(request.auth.token.email.replace('.' , ',')] in ["admin"];
Another option would be to have a way to use dots in the address of a query, so they don't have to be replaced like this:
var emailSanitized = email.replace('.' , '.');
db.collection('someCollection').where('members.' + emailSanitized, '==', 'admin')
Any ideas here?
A little late, but you can simulate the replace function on a string with this one :
function replace(string, replace, by) {
return string.split(replace).join(by);
}
So you need to define this function in your firestore.rules file and then you can call replace(request.auth.token.email, '.' , ',') to get the same result as request.auth.token.email.replace('.' , ',') in javascript.
There are two reasons why you might have been having issues.
The replace function was added to Security Rules after you asked your question.
The replace function uses regular expressions for the first argument and so matching on '.' will match literally everything.
Consider instead using: request.auth.token.email.replace('\\.' , ',')
var emailSanitized = email.replace('.' , '.');
db.collection('someCollection').where('members.' + emailSanitized, '==', 'admin')

Linq-to-SQL question

im really new to linq-to-SQL so this may sound like a really dumb question, i have the following code
var query = from p in DC.General
where p.GeneralID == Int32.Parse(row.Cells[1].Text)
select new
{
p.Comment,
};
how do i got about getting the result from this query to show in a text box ??
That would be:
TextBox1.Text = query.Single().Comment;
You have to filter the first result from your query. To do that, you can use Single() if you know the query only returns one value. You could also use First(), if the results might contain more than one row.
Also, if it's only a single value, you could rewrite the code to:
var query = from p in DC.General
where p.GeneralID == Int32.Parse(row.Cells[1].Text)
select p.Comment;
TextBox1.Text = query.Single();

Resources