« first day (1725 days earlier)      last day (933 days later) » 

11:48 AM
Hi @TehShrike :)
Need some guidance about "views" or "functions" in postgresql
in fact, I think that "function" would fit my need... since views (look like they) do not handle parameters, but only show defined data
So, What I need is to retrieve data from several tables, related to a table!
concrete : I have a person, registered with name and id. Then I have a relationnal table that bind this person with several contacts information (like phone, email, ...)
and I'd like to get the whole informations about this person in one function based on this person id
for example :
Table "Person" : id (serial), name (txt)
Table "relation" : id (serial), personId (integer), contactId (integer)
Table "contact" : id (serial), value (txt), type (txt like "phone" ...)
 
 
2 hours later…
2:13 PM
@Julo0sS If I'm understanding correctly, you don't need a function or a view - a single SELECT query selecting FROM person and JOINing the relation table, with another JOIN for the contact table, should be all you need.
 
well
this could
where i'm confused is that I'll have ONE person and several contact informations
goal to this is to finally get a json object like this : (javascript side)
{"personId":"1" , "name":"MYNAME","contacts":[ {"relation":"123" , "contactId":"333","contactType":"PHONE","contactValue":"someValue"} , {/* another contact */} ]}
sounds useful to make these queries as functions since I will use them several times, and somehow from other applications...
 
3:14 PM
@Julo0sS yeah, the "SQL way" means that you'll get back one row for each person/contact combination in this situation.
There are intensely difficult and uncomfortable incantations that you could perform to try to prevent this or work around it, but they're not worth it.
Accept it, and just combine N contacts into 1 Person object in your code.
if you make sure to ORDER BY person_id in your query, it makes it easier, you can create a new Person object to add contacts to every time you see the personId change.
@Julo0sS I can understand that motivation too. I'll offer two words of caution there: first, it's a dangerous step to start putting business logic in code on your server. It's less testable, more difficult to version, more difficult to fix. Second, this isn't a difficult enough case to be worth encoding somewhere server-side like that - once you get more used to SQL, putting this relation into a function will seem like overkill.
 
 
4 hours later…
7:37 PM
@TehShrike this is what I was afraid of, so technically this solution is not possible "as is"
at least not to fit what I'd like
and you're right with the business logic in server
so i'll manage this through several controllers in my app
mean : one controller to get person data, another one to retrieve contact informations, and finally merge these results in json objects manually to deal with it later
 

« first day (1725 days earlier)      last day (933 days later) »