WEB开发网
开发学院数据库DB2 从 SQL 进行操作系统调用 阅读

从 SQL 进行操作系统调用

 2009-11-20 00:00:00 来源:WEB开发网   
核心提示: 清单 8. 将数据写入文件的 C 代码 #include<stdio.h>#include<string.h>#include<sqludf.h>voidSQL_API_FNwriteFileFromParams(SQLUDF_VARCHAR*fileName

清单 8. 将数据写入文件的 C 代码

#include <stdio.h> 
#include <string.h> 
#include <sqludf.h> 
void SQL_API_FN writeFileFromParams( 
    SQLUDF_VARCHAR *fileName,  /* input */ 
    SQLUDF_INTEGER *intCol,   /* input */ 
    SQLUDF_VARCHAR *charCol,   /* input */ 
    SQLUDF_INTEGER *result,   /* output */ 
    /* null indicators */ 
    SQLUDF_NULLIND *fileName_ind, SQLUDF_NULLIND *intCol_ind, 
    SQLUDF_NULLIND *charCol_ind,  SQLUDF_NULLIND *result_ind, 
    SQLUDF_TRAIL_ARGS_ALL) 
{ 
  int rc = 0; 
  int writtenLen = 0; 
  char row[60+1] = { '\\0' }; 
  struct scratchMap { 
    FILE *f; 
  }; 
  /* map the scratchpad */ 
  struct scratchMap *scratch = 
  struct scratchMap *scratch = 
    (struct scratchMap *)SQLUDF_SCRAT->data; 
  *result_ind = -1; 
  switch (SQLUDF_CALLT) { 
   case SQLUDF_FIRST_CALL: 
     /* open file and store the pointer on the scratchpad */ 
     scratch->f = fopen(fileName, "wb"); 
     if (!scratch->f) { 
       strcpy(SQLUDF_MSGTX, "Could not open file "); 
       strncat(SQLUDF_MSGTX, fileName, SQLUDF_MSGTEXT_LEN - 
           strlen(SQLUDF_MSGTX)-1); 
       strncpy(SQLUDF_STATE, "38300", SQLUDF_SQLSTATE_LEN); 
       rc = -1; 
       goto cleanup; 
     } 
     } 
   case SQLUDF_NORMAL_CALL: 
     if (*intCol < -999999999) { 
       /* a number of -1000000000 or smaller needs 
         11 bytes, which is too long for our format */ 
       sprintf(SQLUDF_MSGTX, "The number %d is too long.", 
           *intCol); 
       strncpy(SQLUDF_STATE, "38301", SQLUDF_SQLSTATE_LEN); 
       rc = -2; 
       goto cleanup; 
     } 
     if (strlen(charCol) > 50) { 
       /* strings must not be longer than 50 characters */ 
       sprintf(SQLUDF_MSGTX, "The string %s is too long.", 
           charCol); 
       strncpy(SQLUDF_STATE, "38302", SQLUDF_SQLSTATE_LEN); 
       rc = -3; 
       goto cleanup; 
     } 
     /* write the given integer and string */ 
     writtenLen = fprintf(scratch->f, "%10d%-50s", *intCol, 
         charCol); 
     if (writtenLen != 60) { 
       sprintf(SQLUDF_MSGTX, "Did not write 60 bytes " 
           "(wrote %d).", writtenLen); 
       strncpy(SQLUDF_STATE, "38303", SQLUDF_SQLSTATE_LEN); 
       rc = -4; 
       goto cleanup; 
     } 
      
     *result = 0; 
     *result_ind = 0; 
     break; 
  } 
 cleanup: 
  switch (SQLUDF_CALLT) { 
   case SQLUDF_FIRST_CALL: 
     if (rc == 0) { 
       break; 
     } 
     /* we must close the file descriptor upon an error in 
       the FIRST call; so we fall through to the final call 
       processing below */ 
   case SQLUDF_FINAL_CALL: 
   case SQLUDF_FINAL_CRA: 
     /* close the file */ 
     fclose(scratch->f); 
     fclose(scratch->f); 
     scratch->f = NULL; 
  } 
} 

上一页  4 5 6 7 8 9 10  下一页

Tags:SQL 进行 操作系统

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接