Re: [gnome-db] GdaSqlBuilder and WHERE clauses



On 4 May 2010 17:24, Vivien Malerba <vmalerba gmail com> wrote:
> On 4 May 2010 16:45, Murray Cumming <murrayc murrayc com> wrote:
>> On Tue, 2010-04-27 at 21:41 +0200, Vivien Malerba wrote:
>>> On 27 April 2010 18:03, Murray Cumming <murrayc murrayc com> wrote:
>>> > In Glom, I have several generic functions that combine a fixed SQL query
>>> > with various WHERE clauses. Imagine something like this
>>> >
>>> >  DataModel get_data_from_table(string where_clause)
>>> >
>>> > It does this by doing a simple string concatenation.
>>> >
>>> > But how can I do this with SqlBuilder, whose gda_sql_builder_add_cond()
>>> > expects the details of the condition in terms of arguments and
>>> > operator?:
>>> > http://library.gnome.org/devel/libgda/unstable/GdaSqlBuilder.html#gda-sql-builder-add-cond
>>> >
>>> > Ideally, I think I'd have a GdaSqlBuilderCond object that I could pass
>>> > around and even AND with another GdaSqlBuilderCond.
>>>
>>> So if I understand you'd like to have a function as:
>>> DataModel get_data_from_table(GdaSqlBuilderCond where_clause)
>>>
>>> I would prefer to avoid creating a new object just for this case, and
>>> prefer to add more flexibility to the GdaSqlBuilder by allowing the
>>> import/export of parts of a statement (as an opaque GdaSqlAnyPart
>>> pointer):
>>>
>>> GdaSqlAnyPart *gda_sql_builder_export_part (GdaSqlBuilder *b, guint id);
>>
>> How would I know what id to parse?
>
> It's the ID returned by gda_sql_builder_add_cond() in the
> GdaSqlBuilder refered to in step 1/6 below.
>
>>
>>> and
>>> guint gda_sql_builder_import_part (GdaSqlBuilder *b, guint id,
>>> GdaSqlAnyPart *part);
>>>
>>> With these 2 new calls you can create a
>>> DataModel get_data_from_table(GdaSqlAnyPart where_clause):
>>>
>>> 1- create a GdaSqlBuilder with the where clause you want
>>
>> A complete SQL query, part of which I would then ignore after exporting
>> the WHERE clause?
>
> As only the WHERE part will be used, you don't even need the
> GdaSqlBuilder in step 1 to contain a valid SQL statement (it may only
> contain a WHERE clause if you prefer, it does not matter).
>
>>
>>> 2- export that WHERE clause as a GdaSqlAnyPart, and use it as argument
>>> to your get_data_from_table() function
>>> 3- create a new GdaSqlBuilder with the specific SQL you need
>>> 4- import the GdaSqlAnyPart into that builder object
>>> 5- get the GdaStatement and execute it.
>>> 6- discard the unneeded objects
>>>
>>> How does it sound?
>>
>> I'd be happy to try that out, please.
>
> Ok, I'll make the proposed API changes along with an example usage.

git/master now contains 2 new API:
gda_sql_builder_export_expression() and
gda_sql_builder_import_expression(). An example about
how to use these is at the end of the
http://git.gnome.org/browse/libgda/tree/samples/SqlBuilder/example.c
example file.

Tell me if this implementation is Ok for you.

Vivien


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]