来自Postgres的DataTable插件

时间:2015-04-22 13:16:35

标签: json postgresql datatable

我正在编写一个使用datatable插件的应用程序。 Datatable插件配置为使用服务器端处理。应用程序后端是用c ++编写的。要查询postgres db,我使用的是libPQ。

我将编写查询以获取JSON中的信息。

(select row_to_json(temp)  from (SELECT * from eventTable
) as temp)

但是所有我都在用

获取元组
{"eventName":"Init","id:"78","comment":"someTex"}
{"eventName":"commit","id:"78","comment":"someText"}

但DataTable插件接受以下格式的JSON:

{
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
    ["Init",
     "78",
     "some"],
    ...
    ]
 }

我是sob中的菜鸟,有点卡住了。我在postgres网站上阅读了refernece页面,但无法自拔。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这个问题很老了,但答案可能对某人有所帮助。 您可以根据需要构建json响应。

以下是您可能会做的一个示例。

with 
  base_request as (
    select id, iso2, iso3 from countries 
  )
select row_to_json(t) from ( 
  select 1 as draw, 
  (select count(*) from base_request) as recordsTotal,
  (select count(*) from base_request limit 5) as recordsFiltered,
  ( 
    select array_to_json(array_agg(row_to_json(u)))
    from (
       select * from base_request offset 0 limit 5
    ) u
  ) as data
) t;

给出

{"draw":1,"recordstotal":239,"recordsfiltered":239,"data":[{"id":3,"iso2":"ar","iso3":"ARG"},{"id":4,"iso2":"au","iso3":"AUS"},{"id":5,"iso2":"at","iso3":"AUT"},{"id":6,"iso2":"bb","iso3":"BRB"},{"id":7,"iso2":"be","iso3":"BEL"}]}