|
| 1 | +-- |
| 2 | +-- tests for utl_file package |
| 3 | +-- |
| 4 | +-- insert a few directory objects for testing |
| 5 | +insert into sys.utl_file_directory(dirname, dir) |
| 6 | +select 'data_directory', current_setting('data_directory'); |
| 7 | +-- test case for fopen with invalid directory object name |
| 8 | +declare |
| 9 | + f sys.ora_utl_file_file_type; |
| 10 | +begin |
| 11 | + -- an invalid directory object name |
| 12 | + f := utl_file.fopen('wrong_directory','regress.txt','a',1024); |
| 13 | +end; |
| 14 | +/ |
| 15 | +ERROR: INVALID_PATH |
| 16 | +DETAIL: File location is invalid. |
| 17 | +HINT: locality is not found in sys.utl_file_directory table |
| 18 | +CONTEXT: PL/iSQL function fopen line 56 at assignment |
| 19 | +PL/iSQL function inline_code_block line 5 at assignment |
| 20 | +-- test fopen, put, putf, put_line, new_line, get_line, is_open, fclose, fclose_all |
| 21 | +declare |
| 22 | + f sys.ora_utl_file_file_type; |
| 23 | + line text; |
| 24 | + i integer; |
| 25 | +begin |
| 26 | + f := utl_file.fopen('data_directory', 'regress.txt', 'w'); |
| 27 | + -- data writing |
| 28 | + utl_file.put_line(f, 'abc'); |
| 29 | + utl_file.put_line(f, '123'::numeric); |
| 30 | + utl_file.put_line(f, '-----'); |
| 31 | + utl_file.new_line(f); |
| 32 | + utl_file.put_line(f, '-----'); |
| 33 | + utl_file.new_line(f, 0); |
| 34 | + utl_file.put_line(f, '-----'); |
| 35 | + utl_file.new_line(f, 2); |
| 36 | + utl_file.put_line(f, '-----'); |
| 37 | + utl_file.put(f, 'a'); |
| 38 | + utl_file.put(f, 'b'); |
| 39 | + utl_file.new_line(f); |
| 40 | + utl_file.putf(f, '[1=%s, 2=%s, 3=%s, 4=%s, 5=%s]', '1', '2', '3', '4', '5'); |
| 41 | + utl_file.new_line(f); |
| 42 | + utl_file.put_line(f, '1234567890'); |
| 43 | + utl_file.fclose(f); |
| 44 | + f := utl_file.fopen('data_directory', 'regress.txt', 'r'); |
| 45 | + -- data reading |
| 46 | + i := 1; |
| 47 | + utl_file.get_line(f, line); |
| 48 | + while line is not null and i < 11 loop |
| 49 | + raise notice '[%] >>%<<', i, line; |
| 50 | + utl_file.get_line(f, line); |
| 51 | + i:=i+1; |
| 52 | + end loop; |
| 53 | + while line is not null loop |
| 54 | + raise notice '[%] >>%<<', i, line; |
| 55 | + utl_file.get_line(f, line, 3); |
| 56 | + i:=i+1; |
| 57 | + end loop; |
| 58 | + raise notice '>>%<<', line; |
| 59 | + utl_file.fclose(f); |
| 60 | + exception |
| 61 | + when others then |
| 62 | + raise notice 'exception: % ', sqlerrm; |
| 63 | + raise notice 'is_open(f) = %', utl_file.is_open(f); |
| 64 | + utl_file.fclose_all(); |
| 65 | + raise notice 'is_open(f) = %', utl_file.is_open(f); |
| 66 | +end; |
| 67 | +/ |
| 68 | +NOTICE: [1] >>abc<< |
| 69 | +NOTICE: [2] >>123<< |
| 70 | +NOTICE: [3] >>-----<< |
| 71 | +NOTICE: [4] >><< |
| 72 | +NOTICE: [5] >>-----<< |
| 73 | +NOTICE: [6] >>-----<< |
| 74 | +NOTICE: [7] >><< |
| 75 | +NOTICE: [8] >><< |
| 76 | +NOTICE: [9] >>-----<< |
| 77 | +NOTICE: [10] >>ab<< |
| 78 | +NOTICE: [11] >>[1=1, 2=2, 3=3, 4=4, 5=5]<< |
| 79 | +NOTICE: [12] >>123<< |
| 80 | +NOTICE: [13] >>456<< |
| 81 | +NOTICE: [14] >>789<< |
| 82 | +NOTICE: [15] >>0<< |
| 83 | +NOTICE: >><NULL><< |
| 84 | +-- test fgetattr, fcopy, fremove, frename |
| 85 | +declare |
| 86 | + fexists boolean; |
| 87 | + file_length number; |
| 88 | + block_size integer; |
| 89 | +begin |
| 90 | + utl_file.fgetattr('data_directory', 'regress.txt', fexists, file_length, block_size); |
| 91 | + raise notice 'fexists = %, file_length = %, block_size = %', fexists, file_length, block_size; |
| 92 | + utl_file.fcopy('data_directory', 'regress.txt', 'data_directory', 'regress2.txt'); |
| 93 | + utl_file.fgetattr('data_directory', 'regress2.txt', fexists, file_length, block_size); |
| 94 | + raise notice 'fexists = %, file_length = %, block_size = %', fexists, file_length, block_size; |
| 95 | + utl_file.fremove('data_directory', 'regress2.txt'); |
| 96 | + utl_file.fgetattr('data_directory', 'regress2.txt', fexists, file_length, block_size); |
| 97 | + raise notice 'fexists = %, file_length = %, block_size = %', fexists, file_length, block_size; |
| 98 | + utl_file.frename('data_directory', 'regress.txt', 'data_directory', 'regress2.txt', true); |
| 99 | + utl_file.fgetattr('data_directory', 'regress2.txt', fexists, file_length, block_size); |
| 100 | + raise notice 'fexists = %, file_length = %, block_size = %', fexists, file_length, block_size; |
| 101 | +end; |
| 102 | +/ |
| 103 | +NOTICE: fexists = t, file_length = 75, block_size = 4096 |
| 104 | +NOTICE: fexists = t, file_length = 75, block_size = 4096 |
| 105 | +NOTICE: fexists = f, file_length = <NULL>, block_size = <NULL> |
| 106 | +NOTICE: fexists = t, file_length = 75, block_size = 4096 |
| 107 | +-- test fflush, fseek |
| 108 | +declare |
| 109 | + f sys.ora_utl_file_file_type; |
| 110 | + f1 sys.ora_utl_file_file_type; |
| 111 | + line text; |
| 112 | + i integer; |
| 113 | + line2_pos integer; |
| 114 | +begin |
| 115 | + f := utl_file.fopen('data_directory', 'regressflush.txt', 'w'); |
| 116 | + f1 := utl_file.fopen('data_directory', 'regressflush.txt', 'r'); |
| 117 | + -- data writing |
| 118 | + for i in 1..5 loop |
| 119 | + utl_file.put_line(f, 'line ' || i::text); |
| 120 | + end loop; |
| 121 | + -- data reading before fflush |
| 122 | + raise notice 'data read before fflush'; |
| 123 | + utl_file.get_line(f1, line); |
| 124 | + while line is not null loop |
| 125 | + raise notice '>>%<<', line; |
| 126 | + utl_file.get_line(f1, line); |
| 127 | + end loop; |
| 128 | + raise notice '>>%<<', line; |
| 129 | + utl_file.fflush(f); |
| 130 | + -- data reading after fflush |
| 131 | + utl_file.fseek(f1); -- reset off_set to 0 |
| 132 | + raise notice 'data read after fflush'; |
| 133 | + utl_file.get_line(f1, line); |
| 134 | + line2_pos := utl_file.fgetpos(f1); -- after reading line 1, get position |
| 135 | + while line is not null loop |
| 136 | + raise notice '>>%<<', line; |
| 137 | + utl_file.get_line(f1, line); |
| 138 | + end loop; |
| 139 | + raise notice '>>%<<', line; |
| 140 | + raise notice 'lets print line#2 again'; |
| 141 | + utl_file.fseek(f1, line2_pos, NULL); -- seek to line 2 position |
| 142 | + utl_file.get_line(f1, line); |
| 143 | + raise notice '>>%<<', line; |
| 144 | + raise notice 'lets print line#2 again via relative seek'; |
| 145 | + utl_file.fseek(f1, NULL, -7); -- move back one fixed "line X\n" |
| 146 | + utl_file.get_line(f1, line); |
| 147 | + raise notice '>>%<<', line; |
| 148 | + utl_file.fclose_all(); |
| 149 | + exception |
| 150 | + when others then |
| 151 | + raise notice 'exception: % ', sqlerrm; |
| 152 | + raise notice 'is_open(f) = %', utl_file.is_open(f); |
| 153 | + raise notice 'is_open(f1) = %', utl_file.is_open(f1); |
| 154 | + utl_file.fclose_all(); |
| 155 | + raise notice 'is_open(f) = %', utl_file.is_open(f); |
| 156 | + raise notice 'is_open(f1) = %', utl_file.is_open(f1); |
| 157 | +end; |
| 158 | +/ |
| 159 | +NOTICE: data read before fflush |
| 160 | +NOTICE: >><NULL><< |
| 161 | +NOTICE: data read after fflush |
| 162 | +NOTICE: >>line 1<< |
| 163 | +NOTICE: >>line 2<< |
| 164 | +NOTICE: >>line 3<< |
| 165 | +NOTICE: >>line 4<< |
| 166 | +NOTICE: >>line 5<< |
| 167 | +NOTICE: >><NULL><< |
| 168 | +NOTICE: lets print line#2 again |
| 169 | +NOTICE: >>line 2<< |
| 170 | +NOTICE: lets print line#2 again via relative seek |
| 171 | +NOTICE: >>line 2<< |
| 172 | +-- test cases for automatic file closing on session terminated/rollback etc |
| 173 | +declare |
| 174 | + f sys.ora_utl_file_file_type; |
| 175 | +begin |
| 176 | + f := utl_file.fopen('data_directory','regress.txt','a',1024); |
| 177 | + raise notice 'is_open = %', utl_file.is_open(f); |
| 178 | + commit; |
| 179 | + raise notice 'is_open = %', utl_file.is_open(f); |
| 180 | + rollback; |
| 181 | + raise notice 'is_open = %', utl_file.is_open(f); |
| 182 | +end; |
| 183 | +/ |
| 184 | +NOTICE: is_open = t |
| 185 | +NOTICE: is_open = t |
| 186 | +NOTICE: is_open = f |
| 187 | +-- clean up |
| 188 | +delete from sys.utl_file_directory where dirname = 'data_directory'; |
| 189 | +/ |
0 commit comments