I know I can chain several Where clauses using gorm, but can I do the same with several Select clauses ?
I've tried something like this with no luck:
query.Select("field1, field2").Select("field3").Find(&models)
The fact is that I need to chain different select clauses depending on certain conditions. How to achieve this?
Chaining multiple Select() calls won't work, as it will only apply the last one. So in your example, the query will look like:
SELECT "field3" FROM "model";
Select() also accepts a []string, so instead, make a slice of strings representing the fields that you need to SELECT, and append to it other columns under certain conditions:
selects := []string{"field1", "field2"}
if condition {
selects = append(selects, "field3")
}
query.Select(selects).Find(&models)
Related
Is there a way for me to simplify the following code: WITH DISTINCT n.prop as distinct_prop RETURN COUNT(distinct_prop)
As you can see, I use an aggregation function with distinct values. Can I somehow "skip" the WITH clause? It seems kind of redundant, but without it the code doesn't work.
With aggregation functions, you can use DISTINCT, so RETURN COUNT(DISTINCT n.prop)
Check the docs here -> https://memgraph.com/docs/cypher-manual/clauses/return#10-returning-unique-results
I have a requirement which is somewhat similar to this. My API supports a filter where there is more than 8 filter parameter. So, I want to create a query dynamically based on the filter parameter passed. I am using CriteriaBuilder to create a dynamic query.
Now, I am able to create dynamic queries successfully but the issue comes when the user wants to sort on an aggregate functions. In my query, I have 4 aggregate (count) function. So to support sorting on these columns, I just use the expression of this aggregate function but what I want is to use the alias of this expression
Repeating the expression in select and order by doesn't seem right to me. So, is there a solution/workaround to the problem. I want to declare the expression alias once and use it in both select and order by clause and if required in my group by clause in future
You probably need to build a custom projection class, you can find a decent and easy one, SQLProjectionWithAliasSupport in https://hibernate.atlassian.net/browse/HHH-2952 (from Sergey Pulyaev)
If then one wants to support also sub criteria, things get more complicate
I am using gorm to interact with a postgres database. I'm trying to ORDER BY a query that uses DISTINCT ON and this question documents how it's not that easy to do that. So I need to end up with a query in the form of
SELECT * FROM (<subquery>) ORDER BY column;
At first glance it looks like I need to use db.QueryExpr() to turn the query I have into an expression and build another query around it. However it doesn't seem gorm has an easy way to directly specify the FROM clause. I tried using db.Model(expr) or db.Table(fmt.Sprint(expr)) but Model seems to be completely ignored and fmt.Sprint(expr) doesn't return exactly what I thought. Expressions contain a few private variables. If I could turn the original query into a completely parsed string then I could use db.Table(query) but I'm not sure if I can generate the query as a string without running it.
If I have a fully built gorm query, how can I wrap it in another query to do the ORDER BY I'm trying to do?
If you want to write raw SQL (including one that has a SQL subquery) that will be executed and the results added to an object using gorm, you can use the .Raw() and .Scan() methods:
query := `
SELECT sub.*
FROM (<subquery>) sub
ORDER BY sub.column;`
db.Raw(query).Scan(&result)
You pass a pointer reference to an object to .Scan() that is structured like the resulting rows, very similarly to how you would use .First(). .Raw() can also have data added to the query using ? in the query and adding the values as comma separated inputs to the function:
query := `
SELECT sub.*
FROM (<subquery>) sub
WHERE
sub.column1 = ?
AND sub.column2 = ?
ORDER BY sub.column;`
db.Raw(query, val1, val2).Scan(&result)
For more information on how to use the SQL builder, .Raw(), and .Scan() take a look at the examples in the documentation: http://gorm.io/advanced.html#sql-builder
I have the following ReQL Query which Identifies distinct addresses, but can't seem to add the not() method to get the opposite.
r.db('mydb').table('users').orderBy(r.desc('email')).without('id').distinct()
not operates on booleans. .distinct().not() doesn't modify the distinct operation. It just gets applied applies to its result, which doesn't make much sense.
You could instead do something like this
r.db('mydb').table('users')
.group('email').count().ungroup()
.filter(row => row('reduction').gt(1))('group')
I need to write an hql query like :
select * from User aliasUser where ? in aliasUser.roles
All examples of IN clause I'm finding use collection as parameters, how can I write this query? Obviously doesn't work as it stands right now, I've tried already.