从sql解析器中提取订单类型

时间:2015-04-01 09:55:51

标签: sql parsing general-sql-parser

使用global sql parser (gsp)从命令sql查询中提取列和排序类型并从中提取和/或从哪里提取

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

我可以提取列名但可以提取订单类型

  

1-如何提取订单类型?

     

2-如何提取和/或从哪里获取SQL?

1 个答案:

答案 0 :(得分:0)

如果 pSqlstmt gsp_selectStatement *,那么你可以这样做:

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;
    }

}