postgresql 多行變一行,C語言自訂函數

來源:互聯網
上載者:User

 

函數效果如下:

select * from cxf limit 10;id----------abcdefghij(10 row)mysql=# select STRCAT2(id,'|') from (select * from cxf limit 10) t;                                         strcat2                                         ----------------------------------------------------------------------------------------- a|b|c|d|e|f|g|h|i|j(1 row)

 

建立聚集合函式的子函數:

 

drop FUNCTION strcat2_sfunc(varchar, varchar,varchar);CREATE FUNCTION strcat2_sfunc(varchar, varchar,varchar)RETURNS varchar AS '/home/mysql/cxf/postgresql-8.2.16/contrib/strcat/libstrcat2_sfunc'LANGUAGE C IMMUTABLE;

建立聚集合函式

drop AGGREGATE PUBLIC.STRCAT2(VARCHAR,varchar);CREATE  AGGREGATE PUBLIC.STRCAT2(VARCHAR,varchar)(  SFUNC=strcat2_sfunc,   STYPE=VARCHAR  );

 

子函數源碼如下:

strcat2_sfunc.c#include "postgres.h"#include "funcapi.h"#include "fmgr.h"#ifdef PG_MODULE_MAGICPG_MODULE_MAGIC;#endifPG_FUNCTION_INFO_V1(strcat2_sfunc);Datumstrcat2_sfunc(PG_FUNCTION_ARGS){                VarChar  *arg1;                VarChar  *arg2;                VarChar  *arg3;                if(PG_ARGISNULL(0) && PG_ARGISNULL(1))                {                         PG_RETURN_NULL();                }                else if(PG_ARGISNULL(0)){                                PG_RETURN_VARCHAR_P(PG_GETARG_VARCHAR_P(1));                }                else if(PG_ARGISNULL(1))                {                                PG_RETURN_VARCHAR_P(PG_GETARG_VARCHAR_P(0));                }                else{            arg1 = PG_GETARG_VARCHAR_P(0);            arg2 = PG_GETARG_VARCHAR_P(1);            arg3 = PG_GETARG_VARCHAR_P(2);             int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) +VARSIZE(arg3)- VARHDRSZ*2;            VarChar *new_text = (VarChar *) palloc(new_text_size);             VARATT_SIZEP(new_text) = new_text_size;            memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1) - VARHDRSZ);            memcpy(VARDATA(new_text) + (VARSIZE(arg1) - VARHDRSZ), VARDATA(arg3), VARSIZE(arg3) - VARHDRSZ);            memcpy(VARDATA(new_text) + (VARSIZE(arg1)+VARSIZE(arg3) - VARHDRSZ*2),                  VARDATA(arg2), VARSIZE(arg2)- VARHDRSZ);            PG_RETURN_VARCHAR_P(new_text);  }}

 Makefile:

 

## Makefile for building PostgreSQL extension modules#MODULE_big = strcat2_sfuncOBJS = strcat2_sfunc.oDATA = DOCS = REGRESS = ifdef USE_PGXSPG_CONFIG = pg_configPGXS := $(shell $(PG_CONFIG) --pgxs)include $(PGXS)elsesubdir = contrib/statfunctop_builddir = ../..include $(top_builddir)/src/Makefile.globalinclude $(top_srcdir)/contrib/contrib-global.mkendif 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.