阿里雲OSS前端直傳檔案方案注意點
阿新 • • 發佈:2020-11-14
傳參型別說明:
in: 是引數的預設模式,這種模式就是在程式執行的時候已經具有值,在程式體中值不會改變。
out: 模式定義的引數只能在過程體內部賦值,表示該引數可以將某個值傳遞迴呼叫他的過程。簡單說:out引數在呼叫的時候,不需要傳入引數(嘗試傳入會報錯),不需要rutrun該引數,function結束會自動返回該out引數。
in out: 表示引數可以向該過程中傳遞值,也可以將某個值傳出去。簡單說:傳參要指定對應引數,結束的時候,還需要專門當out引數去獲取。
這裡是以類PG資料庫做的實驗,本質基本相同:
1.建立型別、陣列型別:
DROP TABLE test_type CASCADE ; DROP TYPE test_type CASCADE ; CREATE type test_type as ( id INT, info varchar(32) ); DROP TYPE test_type_Array; CREATE TYPE test_type_Array AS TABLE OF test_type;
2.建立測試表,填充測試資料:
DROP TABLE test; CREATE TABLE test(id VARCHAR(32), info varchar(32), crt_time TIMESTAMP); INSERT INTO test values('1', 'test1', now()); INSERT INTO test values('1', 'test11', now()); INSERT INTO test values('2', 'test2', now());
3.建立四個function,分別測試簡單型別和物件型別(這裡建立陣列來測試):
/* * 測試簡單型別的in out引數使用,引數需要指定,結果需要當做out引數獲取* * exp:v_info := test_fnc(v_record_id, v_info); */ CREATE OR REPLACE FUNCTION test_fnc(id VARCHAR(32), info IN OUT varchar(32)) RETURNS varchar(32) AS $$ BEGIN info := info|| '_connect_' || id; raise notice 'info is: %', info; END $$ LANGUAGE plsql; /* * 測試簡單型別的out引數使用,引數不需要指定,結果需要獲取* * exp:v_info := test_fnc2(v_record_id); */ CREATE OR REPLACE FUNCTION test_fnc2(id VARCHAR(32), info OUT varchar(32)) RETURNS varchar(32) AS $$ BEGIN info := info|| '_connect_' || id; raise notice 'info is: %', info; END $$ LANGUAGE plsql; /* * 測試陣列型別的in out引數使用,引數需要指定,結果需要當做out引數獲取* * exp:test_A := test_array_fnc(v_record_id, test_A); */ CREATE OR REPLACE FUNCTION test_array_fnc(v_id VARCHAR(32), test_A IN OUT test_type_Array) RETURNS test_type_Array AS $$ DECLARE v_num int; one_type_value test_type; CURSOR test_values IS SELECT * FROM test WHERE id = v_id; BEGIN v_num := 1; FOR one_value IN test_values LOOP one_type_value.id := one_value.id; one_type_value.info := one_value.info; test_A.extend; test_A(v_num) := one_type_value; raise notice 'v_num is: %', v_num; raise notice 'test_A.count is: %', test_A.count; v_num := v_num + 1; END LOOP; END $$ LANGUAGE plsql; /* * 測試陣列型別的out引數使用,引數不需要指定,結果需要獲取* * exp:test_A := test_array_fnc2(v_record_id); */ CREATE OR REPLACE FUNCTION test_array_fnc2(v_id VARCHAR(32), test_A OUT test_type_Array) RETURNS test_type_Array AS $$ DECLARE v_num int; one_type_value test_type; CURSOR test_values IS SELECT * FROM test WHERE id = v_id; BEGIN v_num := 1; test_A := test_type_Array(); FOR one_value IN test_values LOOP one_type_value.id := one_value.id; one_type_value.info := one_value.info; test_A.extend; test_A(v_num) := one_type_value; raise notice 'v_num is: %', v_num; raise notice 'test_A.count is: %', test_A.count; v_num := v_num + 1; END LOOP; END $$ LANGUAGE plsql;
4.在匿名塊中測試:
DO $BODY$ DECLARE test_A test_type_Array; v_record_id varchar(32); v_info varchar(32); BEGIN v_record_id := '1'; raise notice 'test object, like array. must use "in out":'; test_A := test_type_Array(); test_A := test_array_fnc(v_record_id, test_A); raise notice 'out: test_A.count is: %', test_A.count; raise notice ''; raise notice 'execute again, the object value will in the function:'; test_A := test_array_fnc(v_record_id, test_A); raise notice 'out: test_A.count is: %', test_A.count; --NOTICE:這樣的用法,不會因為是in out引數而直接給引數賦值,相當於只是將引數傳遞進去了 raise notice ''; raise notice 'do not get the result:'; test_array_fnc(v_record_id, test_A); raise notice 'out: test_A.count is: %', test_A.count; raise notice ''; raise notice 'test object, like array. use "out":'; test_A := test_array_fnc2(v_record_id); raise notice 'out: test_A.count is: %', test_A.count; v_info := 'init'; raise notice ''; raise notice 'test vachar, use "in out":'; v_info := test_fnc(v_record_id, v_info); raise notice 'out: test_fnc: v_info is: %', v_info; raise notice ''; raise notice 'test varchar, just use "out":'; v_info := test_fnc2(v_record_id); raise notice 'out: test_fnc2: v_info is: %', v_info; END $BODY$
5.測試結果:
test object, like array. must use "in out": v_num is: 1 test_A.count is: 1 v_num is: 2 test_A.count is: 2 out: test_A.count is: 2 execute again, the object value will in the function: v_num is: 1 test_A.count is: 3 v_num is: 2 test_A.count is: 4 out: test_A.count is: 4
--上面可以看出,傳入和傳出都獲取了相應的值。 do not get the result: v_num is: 1 test_A.count is: 5 v_num is: 2 test_A.count is: 6 out: test_A.count is: 4 test object, like array. use "out": v_num is: 1 test_A.count is: 1 v_num is: 2 test_A.count is: 2 out: test_A.count is: 2
--在裡面做了初始化,傳出值直接獲取 test vachar, use "in out": info is: init_connect_1 out: test_fnc: v_info is: init_connect_1 test varchar, just use "out": info is: _connect_1 out: test_fnc2: v_info is: _connect_1