LCOV - code coverage report
Current view: top level - contrib/ltree_plpython - ltree_plpython.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 16 17 94.1 %
Date: 2024-03-28 17:11:35 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "postgres.h"
       2             : 
       3             : #include "fmgr.h"
       4             : #include "ltree/ltree.h"
       5             : #include "plpython.h"
       6             : 
       7           2 : PG_MODULE_MAGIC;
       8             : 
       9             : /* Linkage to functions in plpython module */
      10             : typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
      11             : static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
      12             : 
      13             : 
      14             : /*
      15             :  * Module initialize function: fetch function pointers for cross-module calls.
      16             :  */
      17             : void
      18           2 : _PG_init(void)
      19             : {
      20             :     /* Asserts verify that typedefs above match original declarations */
      21             :     AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
      22           2 :     PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
      23           2 :         load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
      24             :                                true, NULL);
      25           2 : }
      26             : 
      27             : 
      28             : /* These defines must be after the module init function */
      29             : #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
      30             : 
      31             : 
      32           4 : PG_FUNCTION_INFO_V1(ltree_to_plpython);
      33             : 
      34             : Datum
      35           4 : ltree_to_plpython(PG_FUNCTION_ARGS)
      36             : {
      37           4 :     ltree      *in = PG_GETARG_LTREE_P(0);
      38             :     int         i;
      39             :     PyObject   *list;
      40             :     ltree_level *curlevel;
      41             : 
      42           4 :     list = PyList_New(in->numlevel);
      43           4 :     if (!list)
      44           0 :         ereport(ERROR,
      45             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
      46             :                  errmsg("out of memory")));
      47             : 
      48           4 :     curlevel = LTREE_FIRST(in);
      49          16 :     for (i = 0; i < in->numlevel; i++)
      50             :     {
      51          12 :         PyList_SetItem(list, i, PLyUnicode_FromStringAndSize(curlevel->name, curlevel->len));
      52          12 :         curlevel = LEVEL_NEXT(curlevel);
      53             :     }
      54             : 
      55           4 :     PG_FREE_IF_COPY(in, 0);
      56             : 
      57           4 :     return PointerGetDatum(list);
      58             : }

Generated by: LCOV version 1.14