2013-01-19 24 views
1

我正在使用SPI_getbinval從我的光標中拖出一個類型爲double precision[]的列。我怎麼能重建這在C中的double *,所以我可以使用值?Postgresql SPI:操縱類型「Datum」以創建雙數組

我在文檔中看到的大多數示例都引用了隱含幫助函數(即construct_md_array()),但我找不到這些函數的列表或者如何根據我的情況適當地使用它們。

基本上,我需要知道從Datum構建double數組的最有效方法,其中包含double precision[]

(參見之前的問題Achieving high-performance transactions when extending PostgreSQL with C-functions)。

+0

這可能很有趣:http://www.postgresql.org/message-id/[email protected]。同時檢查'intarray' contrib模塊。 –

回答

2
...  
bool is_null = true; 
Datum raw_array = SPI_getbinval(heap_tuple, tuple_desc, column_of_array, &is_null); 

if (!is_null) { 
    ArrayType *pg_array = DatumGetArrayTypeP(raw_array); 
    int array_dimensionality = ARR_NDIM(pg_array); 

    // Not prepared for multi-dimension array 
    if (array_dimensionality == 1) { 
     double *c_array = VARDATA(pg_array); // Pointer to the array data 
     int array_size = ARR_DIMS(pg_array)[0]; // Element count of array at [dimension] 

     // Cycle through a single dimension array 
     for (int i = 0 ; i < array_size ; ++i) { 
      elog(INFO, "Double value at element %d: %lf\n", i, c_array[i]); 
     } 
    } 
} 
... 

P.S. elog()是PostgreSQL版本的printf()函數(具有附加功能) - 使用它。