Re: [gnome-db] GdaSqlSelectJoin

Vivien Malerba <[email protected]>
Newsgroups gmane.comp.gnome.db
Message-ID <[email protected]>
On 2 March 2010 19:22, Piotr Pokora <[email protected]> wrote:
> Hi!
>
> How to add join to select statement?
>
> What I try is:
>
> GdaSqlStatementSelect *select = valid_select_statement;
> GdaSqlSelectFrom *from = select->from;
> GdaSqlSelectJoin *join = gda_sql_select_join_new (GDA_SQL_ANY_PART (from));
> join->type = GDA_SQL_SELECT_JOIN_LEFT;
>
> GdaSqlExpr *expr = gda_sql_expr_new (GDA_SQL_ANY_PART (join));
> expr->value = gda_value_new (G_TYPE_STRING);
> g_value_take_string (expr->value, g_strdup ("style"));
> join->expr = expr;
>
> GdaSqlField *field_a = gda_sql_field_new (GDA_SQL_ANY_PART (join));
> field_a->field_name = g_strdup ("t1.style");
> join->use = g_slist_append (join->use, field_a);
>
> GdaSqlField *field_b = gda_sql_field_new (GDA_SQL_ANY_PART (join));
> field_b->field_name = g_strdup ("style.id");
> join->use = g_slist_append (join->use, field_b);
>
> gda_sql_select_from_take_new_join (from , join);
>
>
> gda_sql_select_join_serialize (join) returns:
> JOIN:
> {"join_type":"LEFT","join_pos":"0","on_cond":{"value":"style"},"using":["t1.style","style.id"]}
> So I assume join is valid.

Yes, the GdaSqlSelectJoin's structure is valid (I mean no wild pointers, ...)

However this would translate as
... FROM ... LEFT JOIN ... ON (style) USING (t1.style, style.id)
which is not what you want: you need to either define the USING part
or the ON part, more likely in your case:
... ON (t1.style=style.id)

So the "style" has to be defined as a GdaSqlSelectTarget and the
join->on has to point to a GdaSqlExpr representing the
"t1.style=style.id" condition (the same construct as for the WHERE
clause applies here), and join->use = NULL.

Vivien
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.