Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418636
  • 博文数量: 66
  • 博客积分: 1416
  • 博客等级: 上尉
  • 技术积分: 922
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-16 10:37
个人简介

高級Oracle DBA,善長Linux系統維運以及Oracle數據庫管理,開發,調優. 具有多年PL/SQL開發經驗.

文章分类

全部博文(66)

文章存档

2015年(9)

2014年(4)

2013年(5)

2010年(1)

2009年(3)

2008年(6)

2007年(30)

2006年(8)

我的朋友

分类: Oracle

2014-03-29 15:38:57

pb 開發使用 Oracle數據開發系統時,
一般情況下無法讀寫oracle bfile數據,
可重新封裝package讓PB調用.
測試代碼如下
pb package封裝

点击(此处)折叠或打开

  1. create or replace package PB_API is
  2. /****************************************
  3.    Ver Date Author Description
  4.    --------- ---------- --------------- ------------------------------------
  5.    1.0 2014-3-24 gangjh 1. pb 無法返回record 類型.
  6.                                             無法調用某些package
  7.                                             重新封裝一下
  8. *****************************************************************/

  9.  function fopen(location IN VARCHAR2,
  10.                  filename IN VARCHAR2,
  11.                  open_mode IN VARCHAR2) return INTEGER ;
  12.  
  13.  PROCEDURE fput_raw(p_file IN integer, buffer IN RAW) ;
  14.  PROCEDURE fclose(p_file IN integer) ;
  15.  
  16.  
  17.  function blob_new return INTEGER ;
  18.  PROCEDURE blob_put_raw(p_loc IN integer, buffer IN RAW) ;
  19.  function blob_get_value(p_loc in integer ) return blob ;
  20.  PROCEDURE blob_tofile(p_loc IN integer, p_file varchar2) ;

  21.  PROCEDURE blob_close(p_loc IN integer) ;
  22.  
  23.  function get_blob(src in bfile ) return blob ;
  24.  
  25.  procedure test ;

  26. end PB_API;
  27. /
  28. create or replace package body PB_API is


  29.  type file_type_tab is table of utl_file.file_type ;
  30.  type blob_tab is table of blob ;
  31.  
  32.  v_files file_type_tab := new file_type_tab() ;
  33.  v_lobs blob_tab := new blob_tab();

  34.  function fopen(location IN VARCHAR2,
  35.                  filename IN VARCHAR2,
  36.                  open_mode IN VARCHAR2) return INTEGER is
  37.    aa utl_file.file_type ;
  38.    x number ;
  39.  BEGIN
  40.    v_files.extend() ;
  41.    x := v_files.last() ;
  42.    aa:= utl_file.fopen(location , filename , open_mode) ;
  43.    v_files(x) := aa;
  44.    return x ;
  45.  END fopen;
  46.  
  47.  

  48.    PROCEDURE fput_raw(p_file IN integer, buffer IN RAW) is
  49.    begin
  50.      utl_file.put_raw(v_files(p_file), buffer) ;
  51.    end ;
  52.           
  53.    PROCEDURE fclose(p_file IN integer) is
  54.    begin
  55.      utl_file.fclose(v_files(p_file)) ;
  56.      v_files.delete(p_file) ;
  57.      v_files.trim() ;
  58.    end ;
  59.    

  60.    
  61.  function blob_new return INTEGER is
  62.    x integer ;
  63.  begin
  64.    v_lobs.extend() ;
  65.    x := v_lobs.last() ;
  66.    dbms_lob.createtemporary(v_lobs(x), true) ;
  67.    return x;
  68.  end ;
  69.  
  70.    
  71.    PROCEDURE blob_put_raw(p_loc IN integer, buffer IN RAW) is
  72.    begin
  73.       dbms_lob.append(v_lobs(p_loc) ,buffer);
  74.    end ;
  75.    

  76. -- for pb use set trans_object
  77.    function blob_get_value(p_loc in integer ) return blob is
  78.    begin
  79.         return v_lobs(p_loc) ;
  80.    end ;
  81.  
  82.    PROCEDURE blob_close(p_loc IN integer) is
  83.  
  84.    begin
  85.      dbms_lob.freetemporary(v_lobs(p_loc)) ;
  86.      v_lobs.delete(p_loc) ;
  87.      v_lobs.trim() ;
  88.    end ;
  89.        
  90.    PROCEDURE blob_tofile(p_loc IN integer, p_file varchar2) is
  91.    begin
  92.     utl_lob.to_file( blob_get_value(p_loc), 'MAILTEMP', p_file) ;
  93.    end ;
  94.       

  95. function get_blob(src in bfile ) return blob is
  96.   v_file varchar2(200);
  97.   v_path varchar2(200);
  98.   dest blob ;
  99.   bfile_loc bfile;
  100. begin
  101.   if src is null then
  102.     return null;
  103.   end if ;
  104.   dbms_lob.createtemporary(dest, true, dbms_lob.call) ;
  105.   dbms_lob.filegetname(src, v_path, v_file) ;
  106.   
  107.   bfile_loc := bfilename(v_path, v_file) ;
  108.   dbms_lob.open(bfile_loc, dbms_lob.lob_readonly);
  109.   dbms_lob.loadfromfile(dest, bfile_loc, dbms_lob.getlength(bfile_loc) ) ;
  110.   dbms_lob.close(bfile_loc) ;
  111.   return dest ;
  112.     
  113. end ;
  114.        
  115.    
  116. end PB_API;
  117. /

PB端封裝,定義user transaction object

点击(此处)折叠或打开

  1. $PBExportHeader$uo_trans_yy3mat.sru
  2. forward
  3. global type uo_trans_yy3mat from transaction
  4. end type
  5. end forward

  6. global type uo_trans_yy3mat from transaction
  7. end type
  8. global uo_trans_yy3mat uo_trans_yy3mat

  9. type prototypes
  10. subroutine PBAPI_FCLOSE(long P_FILE) RPCFUNC ALIAS FOR "~"PCNDBA~".PB_API.~"FCLOSE~""
  11. function long PBAPI_FOPEN(string LOCATION,string FILENAME,string OPEN_MODE) RPCFUNC ALIAS FOR "~"PCNDBA~".PB_API.~"FOPEN~""
  12. subroutine PBAPI_FPUT_RAW(long P_FILE,blob BUFFER) RPCFUNC ALIAS FOR "~"PCNDBA~".PB_API.~"FPUT_RAW~""


  13. function long PBAPI_BLOB_NEW() RPCFUNC ALIAS FOR "~"PCNDBA~".PB_API.~"BLOB_NEW~""
  14. subroutine PBAPI_BLOB_PUT_RAW(long P_LOC,blob BUFFER) RPCFUNC ALIAS FOR "~"PCNDBA~".PB_API.~"BLOB_PUT_RAW~""
  15. subroutine PBAPI_BLOB_CLOSE(long P_LOC) RPCFUNC ALIAS FOR "~"PCNDBA~".PB_API.~"BLOB_CLOSE~""

  16. end prototypes
  17. type variables
  18. //
  19. end variables

  20. forward prototypes
  21. public function integer fwritefile (string filename, string dbpath, string dbfile)
  22. public function integer fwriteblob (string filename)
  23. end prototypes

  24. public function integer fwritefile (string filename, string dbpath, string dbfile);integer li_FileNum, loops, i
  25. long flen, bytes_read
  26. blob b
  27. long filehandle;


  28. flen = FileLength(filename)
  29. li_FileNum = FileOpen(filename, StreamMode!, Read!, LockRead!)

  30. if ( li_FileNum = -1 or isnull(li_FileNum) ) then
  31. messagebox('error', 'file ['+filename+'] read error') ;
  32. return -1;
  33. end if ;

  34. IF flen > 32765 THEN
  35. IF Mod(flen, 32765) = 0 THEN
  36. loops = flen/32765
  37. ELSE
  38. loops = (flen/32765) + 1
  39. END IF
  40. ELSE
  41. loops = 1
  42. END IF

  43. filehandle = PBAPI_FOPEN(dbpath, dbfile, "wb") ;


  44. FOR i = 1 to loops
  45. bytes_read = FileRead(li_FileNum, b)

  46. PBAPI_FPUT_RAW(filehandle, b) ;
  47. NEXT
  48. PBAPI_FCLOSE(filehandle);
  49. FileClose(li_FileNum)

  50. return 0



  51. end function

  52. public function integer fwriteblob (string filename);integer li_FileNum, loops, i
  53. long flen, bytes_read
  54. blob b
  55. long blobhandle;


  56. flen = FileLength(filename)
  57. li_FileNum = FileOpen(filename, StreamMode!, Read!, LockRead!)

  58. if ( li_FileNum = -1 or isnull(li_FileNum) ) then
  59. messagebox('error', 'file ['+filename+'] read error') ;
  60. return -1;
  61. end if ;

  62. IF flen > 32765 THEN
  63. IF Mod(flen, 32765) = 0 THEN
  64. loops = flen/32765
  65. ELSE
  66. loops = (flen/32765) + 1
  67. END IF
  68. ELSE
  69. loops = 1
  70. END IF

  71. blobhandle = PBAPI_BLOB_NEW() ;
  72. if sqlcode <> 0 then
  73. messagebox('error', sqlerrtext) ;
  74. return -1;
  75. end if;

  76. FOR i = 1 to loops
  77. bytes_read = FileRead(li_FileNum, b)
  78. PBAPI_BLOB_PUT_RAW(blobhandle, b) ;
  79. if sqlcode <> 0 then
  80. messagebox('error', sqlerrtext) ;
  81. end if;
  82. NEXT

  83. //PBAPI_BLOB_CLOSE(filehandle);

  84. FileClose(li_FileNum)

  85. return blobhandle



  86. end function

  87. on uo_trans_yy3mat.create
  88. call super::create
  89. TriggerEvent( this, "constructor" )
  90. end on

  91. on uo_trans_yy3mat.destroy
  92. TriggerEvent( this, "destructor" )
  93. call super::destroy
  94. end on



PB測試代碼:
在buttun click事件中入調用代碼

点击(此处)折叠或打开

  1. string ls_path, ls_file_name, ls_filetype
  2. integer li_value ;
  3. blob xlsdata ;

  4. li_value = GetFileOpenName('選擇檔案:', ls_path, ls_file_name, "xls","Excel 2000 Files (*.xls),*.xls,Excel 2007 Files (*.xlsx),*.xlsx,OpenOffice Files (*.ods),*.ods,ALL Files (*.*),*.*")

  5. IF LI_VALUE <> 1 AND LI_VALUE <> 0 THEN
  6. MESSAGEBOX('提示:','無效路徑或文件名!!!',STOPSIGN!)
  7. RETURN
  8. ELSEIF LI_VALUE = 0 THEN
  9. //沒有選舉文件 cancel退出
  10. RETURN
  11. END IF




  12. sqlca.fwritefile(ls_path, 'MAILTEMP', ls_file_name) ;

以上,及可將本地文件寫入主機目錄.
查詢bfile時,需將bfile轉為blob

点击(此处)折叠或打开

  1. select pb_api.get_blob(bfilename('MAILTEMP','cc.sql'))
  2. from dual;

阿飛
2014/03/29











阅读(1643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~