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

Moritz Lennert mlennert at club.worldonline.be
Sat Oct 7 10:02:32 EDT 2006

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 !

-------------- next part --------------
A non-text attachment was scrubbed...
Name: dbf.diff.tgz
Type: application/x-compressed-tar
Size: 1919 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-dev/attachments/20061007/5a26e8d2/dbf.diff.bin

More information about the grass-dev mailing list