I got params.user return the list.
Ex:
params.user = 10 20
then I used
params.user.each { id ->
}
Then id has value:
id = 10, id = 20
When params.user only returns 10, iterating over it I get 1 and 0.
So my idea is count the length() of params.user and put the condition
if (params.user == 1){
//to do something
}
But when I used params.user.length(), it returns 2 and I need it return 1. Please help
When you want to use the reserved params map, you actually use a Map. Then of course, when you do params.myParams.length(), you will get the length of your String value.
But Grails provides tools to get data from the param map. For a list use:
List myList = params.list('user')
Grails provides other ways to grab other kind of data:
def intValue = params.int('paramInt')
def shortValue = params.short('paramShort')
def byteValue = params.byte('paramByte')
def longValue = params.long('paramLong')
def doubleValue = params.double('paramDouble')
def floatValue = params.float('paramFloat')
def booleanValue = params.boolean('paramBoolean')
Last but not least, if you have complex data to bind, don't forget to use Command Objects ;)
Related
I am new in Amazon Web Services. I want to apply expressionAttributeValues filter : value #"3" && :value #"1".
How can I do it?
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];
queryExpression.exclusiveStartKey = self.queryStartKey;
queryExpression.limit = #500;
queryExpression.indexName = #"st-lud-index";
if( addedDate > 0 )
{
queryExpression.keyConditionExpression = #"st = :value AND lud > :rangeVal";
queryExpression.expressionAttributeValues = #{#":value":#3,#":rangeVal":[NSNumber numberWithDouble:addedDate]};
}
else
{
queryExpression.keyConditionExpression = #"st = :value";
queryExpression.expressionAttributeValues = #{#":value":#3};
}
You can find instructions on how to do that in the Perform a Query section of the following document:
https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html#add-aws-mobile-nosql-database-crud-read
Thanks,
Rohan
Using keyConditionExpression you can query with only one value for the partition key at a time. Assuming st is your partition key, if you need to get two items one with st=3 and another with st=1, you will need to make two separate requests.
However, lud (assuming its your sort key) can use a comparison operator such as <= X or BETWEEN X and Y etc.
Another way to do this is using a scan which will return all items in the table or index. You can use filterExpression to only return those items with st=3 and st=1. Scan is expensive operation so better to call query twice.
I want a table like the following:
local Users = {}
local function GetUsers (user)
--cycle through all Users
local Id = GetUserID (user)
local Age = GetAge (user)
local Type = GetType (user)
--Id returns ID of User (value about 8 char length: 27296654)
table.insert (Users, {[Id] = {Age = Age, Type = Type}}
end
This is working as it should but
#Users == 0
if I call Users[Id].Age it returns correct value.
How to make the # work?
As I want to cycle trough all Users to check if a User is multiple times in or missing.
They need to be sorted via IDS.
I also thought on transforming the IDs to word with string.char()
As words will be counted as I want it to be.
I want to make it with
for i = 1, #Users do
An example table looks like this:
Users = {
[12345678] = {Age = 18, Type = 1}
[62952766] = {Age = 22, Type = 1}
[23456788] = {Age = 33, Type = 1}
}
So #Users have to be 3 for me but it shows 0.
But I can call the Age and type out of the table, that means they are in.
Does the table have Problems when the index is such a high number?
Look at # operator documentation:
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t1 is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).
You are using dictionary, so the # operator doesn't work as you expect it. The only way is to iterate the whole table with pairs(..).
function getTableLength(T)
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
I'm trying to use the createCriteria in a grails application to return some rows from a DB. I'm not getting any results.
def query = {
ilike('fullName','%${params.term}%')
projections {
property('id')
property('fullName')
}
}
def plist = Patient.createCriteria().list(query)
def patientSelectList = []
plist.each {
def pMap = [:]
pMap.put("id", it[0])
pMap.put("label", it[1])
pMap.put("value", it[1])
patientSelectList.add(pMap)
}
the fields i'm looking for exist as the following snippet returns results but is very slow.
def patientList = Patient.getAll()
def patientSelectList = []
patientList.each { patient ->
if (patient.fullName.toLowerCase().contains(params.term.toLowerCase()) ) {
def pMap = [:]
pMap.put("id", patient.id)
pMap.put("label", patient.fullName)
patientSelectList.add(pMap)
}
}
return patientSelectList
thanks for anyhelp
I had my Db fields encryted with jasypt. Removing the encryption on the field I needed to query fixed the issue!
You're using a String rather than a GString in the ilike() parameter, so params.term is not being evaluated. To use a GString, use double-quotes instead.
ilike('fullName',"%${params.term}%")
Also make sure % is an appropriate wildcard character for your database (it probably is).
I am trying to have one variable that has a number value as well as a string value.
I am coding in Lua and I don't know how to do this. Is it possible?
Tables. They are like a filing cabinet where you can store as many values as you want and retrieve them given some kind of "key". In Lua, the key can be any type, but the most common key is going to be a numerical index or a string.
Given:
local age = 30 -- your number values
local name = 'Fred' -- your string value
There's a tons of different ways we can structure that in Lua:
local person = { age = 30, name = 'Fred' )
print(person.age, person.name)
local person = { 'Fred', 30 }
print(person[1], person[2])
print(unpack(person))
local person = { Fred = 30 }
print(person.Fred)
local person = { [30] = 'Fred' }
print(person[30])
So on and so forth.
So if i use..
coal = { name = "Coal", value = 80 }
I can then do this?
userInput = read()
if userInput == coal.name then
fuelUse = coal.value
end
When using projection on the properties, the result is returned as the list with the elements in the same sequence as that defined in the projections block. At the same time the property names are missing from the list and that is really disadvantageous to the developer as the result would be passed along and the caller needs to know what value belongs to which property. Is there a way to return a map from the Criteria query with property name as the key to the value?
so, the following code:
def c = Trade.createCriteria()
def remicTrades = c.list {
projections {
property('title', 'title')
property('author.name', 'author')
}
def now = new Date()
between('publishedDate', now-365, now)
}
This returns:
[['book1', 'author1']['book2', 'author2']]
Instead I would like it to return:
[[book:'book1', author:'author1'][book:'book2', author:'author2']]
I know I can arrange this way after getting the result but I earnestly feel that the property alias should have been used by the criteria to return a list of map that mimics the result of the SQL query and not a bland list.
Duplicate: Grails queries with criteria: how to get back a map with column?
And the corresponding answer (and solution): https://stackoverflow.com/a/16409512/1263227
Use resultTransformer.
import org.hibernate.criterion.CriteriaSpecification
Trade.withCriteria {
resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
projections {
property('title', 'title')
property('author.name', 'author')
}
def now = new Date()
between('publishedDate', now-365, now)
}
Agree with your question reasoning, this really should be part of the core GORM solution. That said, here's my workaround;
def props = ['name','phone']
def query = Person.where {}.projections {
props.each{
property(it)
}
}
def people = query.list().collect{ row->
def cols = [:]
row.eachWithIndex{colVal, ind->
cols[props[ind]] = colVal
}
cols
}
println people // shows [['name':'John','phone':'5551212'],['name':'Magdalena','phone':'5552423']]