Nonstop Database, HiRDB Version 9 Command Reference
This section describes examples of data loading using UOCs.
These UOC examples are provided as sample databases. The storage directory is $PDDIR/sample/sampleUOC.
This example changes an input data file with a 2-digit year designation to a 4-digit designation to store the data in a Y2K-supported database that can handle dates in the year 2000 and beyond. The input data file is in DAT format, and pdload is used to input the input data file.
CREATE TABLE DIRECTORY (EMPLOYEE_NUMBER INTEGER, BIRTH_DATE DATE, HOMETOWN CHAR(10), NAME CHAR(16));
10001, 68/04/30, BOSTON,DAVID IVERSON 10002, 64/09/13, TEXAS,DONALD YOUNG 20001, 70/11/02, CHICAGO,MATT CARR |
pdload DIRECTORY control-information-file-name |
source input-data-filename srcuoc library-name entry = date_change_func |
(File name: sample1.c) /***************************************************************************** ** ** ** HiRDB sample Data input User Own Coding (for CSV file) ** ** ~ ~ ~ ** ** name : date_change_func ** ** ** ** func : YY/MM/DD ===> YYYY-MM-DD ** ** ** ** i/o : none ** ** ** ** return : none ** ** ** *****************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <pdutluoc.h> 1 #define DATELEN 10 /* DATE length */ static void sub_date_change(char *,char *); void date_change_func( UTL_UOC_INF *uocinf /* A(UOC interface area) */ 2 ){ long wk_leng; /* length */ char date_data[10]; /* DATE (HiRDB) */ char *date_ptr; /* A(birth date) */ char *address_p; /* A(native) */ static char buff[512]; /* buffer */ switch(uocinf->req_cd){ 3 case UTL_UOC_START: /*--------------------------------------------------------------------------*/ /* START */ /*--------------------------------------------------------------------------*/ uocinf->edit_adr = buff; uocinf->rtn_code = UTL_UOC_NML; break; case UTL_UOC_EDIT: /*--------------------------------------------------------------------------*/ /* EDIT */ /*--------------------------------------------------------------------------*/ date_ptr = strchr(uocinf->data_adr,','); if (date_ptr == NULL){ strcpy(uocinf->err_msg,"Invalid data"); goto OWARI; } sub_date_change(++date_ptr,date_data); wk_leng = (long)date_ptr - (long)uocinf->data_adr; strncpy(buff,uocinf->data_adr,wk_leng); strncpy((char*)((long)buff + wk_leng),date_data,DATELEN); wk_leng += DATELEN; address_p = strchr(date_ptr,','); strcpy((char*)((long)buff + wk_leng),address_p); uocinf->rtn_code = UTL_UOC_NML; break; case UTL_UOC_END: case UTL_UOC_TERM: /*--------------------------------------------------------------------------*/ /* END */ /*--------------------------------------------------------------------------*/ uocinf->rtn_code = UTL_UOC_NML; break; default: strcpy(uocinf->err_msg,"Invalid request code"); goto OWARI; } return; OWARI: uocinf->rtn_code = UTL_UOC_ERR; return; } static void sub_date_change( char *year_two, /* YY/MM/DD */ char *year_four /* YYYY-MM-DD */ ){ strcpy(year_four,"19"); strncat(year_four,year_two,8); year_four[4] = '-'; year_four[7] = '-'; return; } |
This example converts the character encoding of the file transferred from machine M by FTP in the binary mode from EBCDIK to ASCII and then stores it in the database. The input data file is in binary format, and the UOC is used to input the input data file.
CREATE TABLE EMPLOYEE_LIST (EMPLOYEE_NUMBER INTEGER, EMPLOYEE_NAME CHAR(16));
00002711 E8E4E4D1 C940C9D2 C5C4C140 40404040 ...Hexadecimal 10001 DAVID IVERSON ...Characters 00002712 E8D6E2C8 C9D640D6 C4C14040 40404040 10002 DONALD YOUNG 00004E21 D4C1E2C1 D4C940D2 C1E6C1C7 E4C3C8C9 20001 MATT CARR |
pdload EMPLOYEE_LIST control-information-file-name |
source (uoc) srcuoc library-name entry=table_load_func param='input-data-filename' |
(File name: sample2.c) /***************************************************************************** ** ** ** HiRDB sample Data input User Own Coding (for Binary file 1/2) ** ** ~ ~ ~ ** ** name : table_load_func ** ** ** ** func : Character code convert ** ** ** ** i/o : none ** ** ** ** return : none ** ** ** *****************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <pdutluoc.h> #define F_START 'S' /* convert start */ #define F_CONV 'C' /* convert execute */ #define F_END 'E' /* convert end */ extern long data_convert_func(long,long,char*,char*); void table_load_func( UTL_UOC_INF *uocinf /* A(UOC interface area) */ ){ long rc; /* return code */ long redsiz; /* input data length */ char *ebc_adr; /* A(EBCDIK) */ char *asc_adr; /* A(ASCII) */ char buff[20]; /* input buffer */ static int inflp = EOF; /* file ID */ switch(uocinf->req_cd){ case UTL_UOC_START: /*--------------------------------------------------------------------------*/ /* START */ /*--------------------------------------------------------------------------*/ uocinf->e_data_len = sizeof(buff); uocinf->edit_adr = malloc(sizeof(buff)); if (uocinf->edit_adr == NULL){ strcpy(uocinf->err_msg,strerror(errno)); goto OWARI; } if (uocinf->user_param == NULL){ strcpy(uocinf->err_msg,"File name not specified"); goto OWARI; } strcpy(uocinf->err_msg,"FILE NAME:"); strcat(uocinf->err_msg,uocinf->user_param); 1 inflp = EOF; inflp = open(uocinf->user_param,O_RDONLY | O_NONBLOCK); if (inflp == EOF){ strcpy(uocinf->err_msg,strerror(errno)); goto OWARI; } rc = data_convert_func(F_START,0,0,0); if (rc != 0){ strcpy(uocinf->err_msg,strerror(rc)); goto OWARI; } uocinf->rtn_code = UTL_UOC_DBG; 2 break; case UTL_UOC_EDIT: /*--------------------------------------------------------------------------*/ /* EDIT */ /*--------------------------------------------------------------------------*/ redsiz = read(inflp,buff,sizeof(buff)); switch(redsiz){ case 0: uocinf->rtn_code = UTL_UOC_EOF; break; case -1: strcpy(uocinf->err_msg,strerror(errno)); goto OWARI; default: if (redsiz == sizeof(buff)){ memcpy(uocinf->edit_adr,buff,sizeof(long)); ebc_adr = (char *)((long)buff + sizeof(long)); asc_adr = (char *)((long)uocinf->edit_adr + sizeof(long)); rc = data_convert_func(F_CONV,sizeof(buff) - sizeof(long), ebc_adr,asc_adr); if (rc != 0){ strcpy(uocinf->err_msg,strerror(rc)); goto OWARI; } uocinf->rtn_code = UTL_UOC_NML; }else{ strcpy(uocinf->err_msg,"Invalid data"); goto OWARI; } } break; case UTL_UOC_END: case UTL_UOC_TERM: /*--------------------------------------------------------------------------*/ /* END */ /*--------------------------------------------------------------------------*/ rc = data_convert_func(F_END,0,0,0); if (rc != 0){ strcpy(uocinf->err_msg,strerror(rc)); goto OWARI; } if (inflp != EOF){ rc = close(inflp); inflp = EOF; if (rc == EOF){ strcpy(uocinf->err_msg,strerror(errno)); goto OWARI; } } if (uocinf->edit_adr != NULL){ free(uocinf->edit_adr); uocinf->edit_adr = NULL; } uocinf->rtn_code = UTL_UOC_NML; break; default: strcpy(uocinf->err_msg,"Invalid request code"); goto OWARI; } return; OWARI: uocinf->rtn_code = UTL_UOC_ERR; return; } |
All Rights Reserved. Copyright (C) 2011, 2015, Hitachi, Ltd.