建立C源文件,编写代码:highgo-yueyf@Highgo-DB:~/project/workspace$ vim myapi.c
C文件的源码:#include "postgres.h" #include "executor/spi.h" #include "utils/builtins.h" #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif int mydelete(int key); int mydelete(int key) { char command[128]; //视命令长短建立相应大小的数组 int ret; int proc; //对表数据操作的行数 /* 将命令赋值到command */ sprintf(command, "update test set label = 0 where id = %d and label = 1; ", key); SPI_connect(); //内部链接 ret = SPI_exec( command, 0); //执行操作 proc = SPI_processed; //为行数赋值 SPI_finish(); //中断连接 return (proc); //将操作行数作为返回结果 }
3. 编译源码
gcc -fpic -I ~/project/include/postgresql/server/ -shared -o myapi.so myapi.c
4. 拷贝SO到工程
cp myapi.so ~/project/lib/postgresql/
5. 启动数据库,并LOAD SO
test=# load 'myapi'; LOAD
6. 新建函数
test-# CREATE FUNCTION mydelete(integer) returns integer as '$libdir/myapi.so','mydelete' test-# LANGUAGE c strict; CREATE FUNCTION
7. 新建table并插入数据
test-# create table test(id int, name text, label int); CREATE TABLE
test=# INSERT INTO test VALUES (1,'abc',1); INSERT 0 1 test=# INSERT INTO test VALUES (2,'def',1); INSERT 0 1 test=# select * from test; id | name | label ----+----------+------- 1 | abc | 1 2 | def | 1 (2 rows)
8. 运行函数
test=# SELECT mydele(1);
9. 查询表进行验证
test=# SELECT * FROM test; id | name | label ----+----------+------- 2 | def | 1 1 | abc | 0 (2 rows)
过程是根据转载源一步一步的执行的,特此记录一下。
下面是在存储中添加了一个循环,可以一次插入多条数据。
int myinsert(int key) { char command[128]; int ret = 0; int proc; int i; char* sysname[50] = { "sys01","sys02","sys03","sys04","sys05", "sys06","sys07","sys08","sys09","sys10", "sys11","sys12","sys13","sys14","sys15", "sys16","sys17","sys18","sys19","sys20", "sys21","sys22","sys23","sys24","sys25", "sys26","sys27","sys28","sys29","sys30", "sys31","sys32","sys33","sys34","sys35", "sys36","sys37","sys38","sys39","sys40", "sys41","sys42","sys43","sys44","sys45", "sys46","sys47","sys48","sys49","sys50" }; SPI_connect(); for (i=0;i<50;i++) { sprintf(command,"INSERT INTO tbl_sys VALUES ('%s',5000,3,%d)",sysname[i],i); ret = SPI_exec(command,0); } proc = SPI_processed; SPI_finish(); return (proc); }
插入前:
mmm-db=# SELECT * FROM tbl_sys; name | cash | status | or_num ------+------+--------+-------- (0 rows)
调用函数插入数据:
mmm-db=# SELECT myinsert(1); myinsert ---------- 1 (1 row)
插入数据后:
mmm-db=# SELECT * FROM tbl_sys; name | cash | status | or_num -------+------+--------+-------- sys01 | 5000 | 3 | 0 sys02 | 5000 | 3 | 1 sys03 | 5000 | 3 | 2 sys04 | 5000 | 3 | 3 sys05 | 5000 | 3 | 4 sys06 | 5000 | 3 | 5 sys07 | 5000 | 3 | 6 sys08 | 5000 | 3 | 7 sys09 | 5000 | 3 | 8 sys10 | 5000 | 3 | 9 sys11 | 5000 | 3 | 10 sys12 | 5000 | 3 | 11 sys13 | 5000 | 3 | 12 sys14 | 5000 | 3 | 13 sys15 | 5000 | 3 | 14 sys16 | 5000 | 3 | 15 sys17 | 5000 | 3 | 16 sys18 | 5000 | 3 | 17 sys19 | 5000 | 3 | 18 sys20 | 5000 | 3 | 19 sys21 | 5000 | 3 | 20 sys22 | 5000 | 3 | 21 sys23 | 5000 | 3 | 22 sys24 | 5000 | 3 | 23 sys25 | 5000 | 3 | 24 sys26 | 5000 | 3 | 25 sys27 | 5000 | 3 | 26 sys28 | 5000 | 3 | 27 sys29 | 5000 | 3 | 28 sys30 | 5000 | 3 | 29 sys31 | 5000 | 3 | 30 sys32 | 5000 | 3 | 31 sys33 | 5000 | 3 | 32 sys34 | 5000 | 3 | 33 sys35 | 5000 | 3 | 34 sys36 | 5000 | 3 | 35 sys37 | 5000 | 3 | 36 sys38 | 5000 | 3 | 37 sys39 | 5000 | 3 | 38 sys40 | 5000 | 3 | 39 sys41 | 5000 | 3 | 40 sys42 | 5000 | 3 | 41 sys43 | 5000 | 3 | 42 sys44 | 5000 | 3 | 43 sys45 | 5000 | 3 | 44 sys46 | 5000 | 3 | 45 sys47 | 5000 | 3 | 46 sys48 | 5000 | 3 | 47 sys49 | 5000 | 3 | 48 sys50 | 5000 | 3 | 49 (50 rows) mmm-db=#