[GRASS-dev] adding 'desc' to dbf sql driver

Daniel Calvelo dca.gis at gmail.com
Sun Oct 8 13:13:46 EDT 2006


Hi Moritz,

Looks good to me. In the yac.y patch, you don't need to duplicate the
code for ORDER BY NAME and ORDER BY NAME ASC. For cmp_row_desc, you
could just return( - cmp_row_asc) with the same arguments. Glynn, a
cursory look?

FYI, the text file in lib/db/sqlp/test/test allows for some testing of
the parser. That may help you find any hidden bugs in the sqlp part of
your patch. You might also wish to update print.c to accomodate for
asc/desc (it's a one-line change near the end).

And of course, after testing, this has to be documented :)

Daniel.

On 10/7/06, Moritz Lennert <mlennert at club.worldonline.be> wrote:
> Hi Daniel,
>
> On Thu, September 28, 2006 03:31, Daniel Calvelo wrote:
> > Hi Moritz. See below
> >
> > On 9/22/06, Moritz Lennert <mlennert at club.worldonline.be> wrote:
> >> Hi,
> >>
> >> Reworking on d.vect.chart I need to be able to sort the reults of a
> >> select in descending order. As the dbf driver doesn't allow this, I have
> >> been trying to see how to extend the driver.
> >>
> >> IIUC, it's "just" a question of conditionalising the qsort call on line
> >> 566 of db/drivers/dbf/dbfexe.c, and (would this be enough ?) use
> >>
> >> qsort(set, nset, sizeof(int), -cmp_row);
> >
> > That might work. Although it would be much nicer to use an expression
> > as sorting order.
> >
> >> if the desc keyword is present.
> >>
> >> However, I have some trouble understanding the parsing of the sql
> >> statement.
> >>
> >> If I understand correctly, we would need to extent the SQLPSTMT
> >> structure in include/sqlp.h to include a flag for 'desc' (and possible
> >> 'asc') and the parser to identify and set that flag.
> >>
> >> But this is as far as I get. Could someone help me with this ?
> >
> > Quick shot: you need to
> >
> > - change SQLPSTMT to have an "order"/"direction"/"sort_order"  element
> >
> > - add ASC and DESC in lex.l as tokens
> >
> > - change yac.y to stg like:
> >
> > y_order: y_order_asc | y_order_desc ;
> > y_order_asc:
> >        NAME ASC { sqpOrderColumn( $1 ); sqpOrderDirection( 1 ); }
> > ;
> > y_order_desc:
> >       NAME DESC { sqpOrderColumn( $1 );sqpOrderDirection( 2 );}
> > ;
> >
> > - add in sql.c a sqpOrderDirection() function that sets the element
> > you added to SQLPSTMT
> >
> > (Alternatively, you can change sqpOrderColumn() to receive two
> > arguments, one being the sorting direction. I'd also rather #define
> > SORT_ASC and SORT_DESC instead of using magic numbers as above.)
> >
> > - use that value in dbfexe.c to test the sorting direction.
>
> Here's an attempt at translating your very clear instructions. Could you
> or someone else just have a quick look to see if I didn't do anything bad.
> One question for me was how to handle the different comparison functions
> for ASC and DESC. At this stage I just duplicated the function
> (cmp_row_asc and cpm_row_desc) and inverted the signs. Maybe there is a
> more elegant way ?
>
> Thanks !
>
> Moritz
>
>


-- 
-- Daniel Calvelo Aros




More information about the grass-dev mailing list