Scanning into struct of gorm query - go

I'm trying to scan the result of a query into a res structure.
The code builds and the query passes but the result array consists of default values like this:
[{0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0}]
Also, result array has the exact length as the query result should have.
When i try generated query in postgres shell it returns the result correctly.
Code:
type res struct{
id int
number int
user_id int
}
func getDataJoin(){
new := []res{}
db.Db.Table("users").Select("users.id as id, credit_cards.number as number, credit_cards.user_id as user_id").Joins("left join credit_cards on credit_cards.user_id = users.id").Scan(&new)
fmt.Println("user\n",new)
}
Generated Query:
SELECT users.id as id, credit_cards.number as number, credit_cards.user_id as user_id FROM "users" left join credit_cards on credit_cards.user_id = users.id
Database result
id | number | user_id
----+--------+---------
1 | 1 | 1
1 | 2 | 1
2 | 1 | 2
2 | 2 | 2
3 | 1 | 3
3 | 2 | 3
(6 rows)

Since go-gorm has a certain convention when it comes to naming, you might want to try two things.
Make your res struct publicly available, with public fields:
type Res struct{
ID int
Number int
UserID int
}
Or, specify mappings between columns and fields:
type res struct{
id int `gorm:"column:id"`
number int `gorm:"column:number"`
user_id int `gorm:"column:user_id"`
}

gorm can only read/write on exported fields much like Marshal/Unmarshal methods of json package. If the first letter of your field is in capital, it will be used. By default, gorm matches struct fields with their camel-cased forms. You can also define your own column names.
Since camel-cased form of both ID and Id, is id, as long as the first letter of your field is in capital, it should work. On a different note, it's good practice to write ID, i.e., both letter capital.

Related

Check if input is in range. HASURA - GRAPHQL

I would like to check if a number is in a specific range dynamically. For example i have a table like:
id
value
age_range
1
a
3-7
2
b
7-10
3
c
3-7
4
d
0-3
if the age of the user is 5, i would like to retrieve all rows where age_range is 3-7 without writing the range in the where clause query. For example something like:
table (where: { 5 in age_range })...
Make an age_from and an age_to column if possible.
query MyQuery($int: Int!) {
table(where: {_and: [{age_to: {_gte: $int}}, {age_from: {_lte: $int}}]}) {
#Fields....
}
}

spring data exists by all values of some column

I want to know, is there set of entities by following rule:
I have a table with two primary keys:
| id | key |
| 1 | a |
| 2 | b |
| 1 | c |
So, I want to do something like that:
boolean existsByIdAndAllOfKey(
long id,
Set<Key> keys
)
This query should return true if in the database there are entities with all keys presented in input Set.
I wondering is there any keyword from spring data? Or what is the best way to do that?
found following solution:
int countByIdAndKeyIn(
long id,
Set<Key> keys
)
boolean isThereEntityWithAllKeys(long id, Set<Key> keys) {
return countByIdAndKeyIn(id, keys) == keys.size;
}

Count instances on a table

If I have a table with duplicate instances how can I count if I don't have a count function?
All I have is select, project, union, difference, product, intersect, njoin. I am using WinRDBI.
Table looks like this:
Children
ID| NAME|
A | 'alice'
A | 'jon'
A | 'alex'
B | 'joe'
B | 'mary'
C | 'amy'
Parent
ID| NAME|
A | 'Smith'
B | 'Johnson'
C | 'Meyer'
I want to know how what parent has two children.
Use the difference operator and the fact that (n*n)-n = n is true only for n = 2 when n > 0.
For each parent, create a cross product of their Children [call this "C"] with itself by renaming second copy of Children [call this "C1"]. Let's call this "CxC1"
If (((select attributes of C from CxC1) - C) = C) then the parent has exactly 2 children [1]
[1] Assuming referential integrity such that a parent cannot have zero children.

Linq - multiple rows as string

Lets assume that I have Table which contains Username and the other one which contains FirstName. A single user can have multiple FirstNames:
How can I obtain a record which will contain Username and all first names separated by comma?
i. e.
Users
id | Username
1 | Test
Names
UserId | FirstName
1 | Mike
1 | John
I would like to receive a record which will contain
Test, "Mike, John"
How can I do that?
Edit: What if Users table will have more columns which I want to get?
i. e.
id | Username | Status
1 | Test | Active
How to get Test, Active, "Mike, John"?
You can use GroupBy and String.Join
var userGroups = from u in users
join n in names
on u.ID equals n.UserID
group new{n, u} by n.UserID into UserGrp
select new
{
Username = UserGrp.Key,
Status = UserGrp.First().u.Status,
Names = string.Join(",", UserGrp.Select(x => x.n.Name))
};
foreach (var ug in userGroups)
Console.WriteLine("{0}, {1}, \"{2}\"", ug.Username, ug.Status, ug.Names);

Select list of x via list of y

Here's the DB setup of 3 tables:
Template
-----------
TemplateId (Pk Identity)
Name
Example Data:
TemplateId Name
1 Homepage
2 Generic Landing Page
TemplateArea (Bridge table to keep track of each template type's list of areas)
----------------
TemplateAreaId (Pk Identity)
TemplateId (Fk)
AreaId (Fk)
Example Data:
TemplateAreaId TemplateId AreaId
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
So every template has 3 areas (you're seeing a set of templateIds (e.g. 2) and related set of AreaIds (1 & 2))
Area
-----
AreaId (Pk Name)
Name
Example Data:
AreaId Name
1 Top
2 Middle
3 Bottom
I'm trying to get a list of Areas based on a list of TemplateAreas keyed off of AreaId in my list of TemplateAreas:
so for example I should get this list of content areas for a TemplateId 2:
AreaId Name
1 Top
2 Middle
int templateId = 2;
List<TemplateArea> templateAreas = TemplateAreas.Where(ta => ta.TemplateId == templateId).ToList();
List<Area> areas = Areas.Where()); // this is where I'm stuck, how to get the list of areas (1 & 2) relatd to templateId 2
so in other words, get a list of Template Areas then get a list of related Areas keyed off of the specific TemplateId.
I'm basically trying to join to TemplateArea from Area on TemplateArea.TemplateId = templateId or something like that if this were T-SQL, e.g. Something like:
select AreaId, Name from Area join TemplateArea on Area.AreaId = TemplateArea.AreaId where TemplateArea.TemplateId = templateId
Using your code approach as a guide, you can join to the templateAreas:
List<Area> areas = Area.Join(templateAreas, a => a.AreaId, t => t.AreaId, (a, t) => a);
Try This :
List<Area> areas = Areas.Where(ta => ta.TemplateId == templateId).Select(ta => ta.Areas).ToList();
you can access any of the area field using this..
How about:
List<Area> areas = TemplateAreas.Where(ta => ta.TemplateId == templateId).Select(ta => ta.Areas).Distinct().ToList();
I am pretty sure this would work in Entity Framework, just not sure in Linq to Sql.

Resources