Extracting order type from sql parser

360 Views Asked by At

using global sql parser (gsp) for extracting column and sorting type from order sql query and extract and or from where condition

SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales' and ID=1
ORDER BY salary, age DESC,ID;

I can extracting column name but can extract order type

1- how can extract order type?

2- how can extract and , or from where sql?

1

There are 1 best solutions below

2
On

If pSqlstmt is gsp_selectStatement * then you can do something like this:

if(pSqlstmt->orderbyClause != nullptr)
{       
    string sortType;
    int colNumOrderBy = pSqlstmt->orderbyClause->items->length;

    for(int i = 0; i < colNumOrderBy; i++)
    {
        gsp_orderByItem *field = reinterpret_cast<gsp_orderByItem *>(gsp_list_celldata(pSqlstmt->orderbyClause->items->head));

        //get order by column name
        char *sortCol = gsp_node_text(reinterpret_cast<gsp_node*>(field->sortKey)); 

        if(field->sortToken== nullptr)
        {
            //ERROR
        }
        else
        {
            //find out sorting type (ASC/DESC)
            sortType = sortType.substr(0,field->sortToken->nStrLen);
        }

        free(sortCol);
        pSqlstmt->orderbyClause->items->head = pSqlstmt->orderbyClause->items->head->nextCell;
    }

}