$ cat IDL_userinfo.dsc STRUCT userinfo { INT 4 user_id STRING 16 user_name STRING 128 email CREATE_SQL "CREATE UNIQUE INDEX userinfo_idx1 ON userinfo ( user_id ) ;" DROP_SQL "DROP INDEX testable_idx1 ;" } $ dsc -f IDL_userinfo.dsc -c-xml -sql -ec-pqsql -c-LOG STRUCT userinfo INT 4 user_id STRING 16 user_name STRING 128 email ok! $ ls -1 IDL_userinfo.* IDL_userinfo.dsc IDL_userinfo.dsc.ESQL.ec IDL_userinfo.dsc.ESQL.eh IDL_userinfo.dsc.LOG.c IDL_userinfo.dsc.c IDL_userinfo.dsc.create.sql IDL_userinfo.dsc.drop.sql IDL_userinfo.dsc.h
$ cat test_db2json.c ... int test_client() { userinfo u ; char json_buffer[ 1024 + 1 ] ; int json_len ; FILE *fp = NULL ; int nret ; EXEC SQL CONNECT TO '[email protected]:18432' USER 'calvin' IDENTIFIED BY 'calvin' ; if( SQLCODE ) { printf( "CONNECT failed[%ld][%s]\n" , SQLCODE , SQLSTATE ); return 1; } else { printf( "CONNECT ok\n" ); } DSCINITV_userinfo(); EXEC SQL SELECT * INTO DBVLLIST_userinfo FROM userinfo WHERE user_id = 101 ; if( SQLCODE ) { printf( "SELECT failed[%ld][%s]\n" , SQLCODE , SQLSTATE ); EXEC SQL DISCONNECT ; return 1; } else { printf( "SELECT ok\n" ); } EXEC SQL DISCONNECT ; if( SQLCODE ) { printf( "DISCONNECT failed[%ld][%s]\n" , SQLCODE , SQLSTATE ); return 1; } else { printf( "DISCONNECT ok\n" ); } DSCVTOS_userinfo( & u ); memset( json_buffer , 0x00 , sizeof(json_buffer) ); nret = DSCSERIALIZE_JSON_userinfo( & u , "GBK" , json_buffer , & json_len ) ; if( nret ) { printf( "DSCSERIALIZE_JSON_userinfo failed[%d] , errno[%d]\n" , nret , errno ); return -1; } fp = fopen( "test_db2json.json" , "w" ) ; if( fp == NULL ) { printf( "fopen failed , errno[%d]\n" , errno ); return -1; } fwrite( json_buffer , 1 , json_len , fp ); fclose( fp ); return 0; } ...
$ # 执行综合示例一,在表userinfo中产生一条记录 $ ... $ make gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -I/root/local/postgresql/include -I/home/calvin/exinc/fasterxml -I/home/calvin/exinc/fasterjson -c IDL_userinfo.dsc.c gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -I/root/local/postgresql/include -I/home/calvin/exinc/fasterxml -I/home/calvin/exinc/fasterjson -E -x c test_db2json.ec > test_db2json.ecE ecpg -h test_db2json.ecE -o test_db2json.c sed -i 's/NULL/0/g' test_db2json.c gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -I/root/local/postgresql/include -I/home/calvin/exinc/fasterxml -I/home/calvin/exinc/fasterjson -c test_db2json.c gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_db2json IDL_userinfo.dsc.o IDL_userinfo.dsc.ESQL.o test_db2json.o -L. -L/root/local/postgresql/lib-lecpg -L/home/calvin/exlib -lfasterxml -lfasterjson
$ ./test_db2json CONNECT ok SELECT ok DISCONNECT ok $ cat test_db2json.json { "userinfo" : { "user_id" : 101 , "user_name" : "calvin" , "email" : "[email protected]" } }