Skip to content

Commit 3101a0f

Browse files
committed
add functions and sample
1 parent f2b74d8 commit 3101a0f

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

create_function_toddog.sql

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
prompt create library using libtoddog.so
2+
3+
CREATE OR REPLACE LIBRARY libtoddog AS '${ORACLE_HOME}/lib/libtoddog.so';
4+
/
5+
prompt create function f_metrictoddog
6+
7+
CREATE OR REPLACE FUNCTION f_metrictoddog (
8+
name IN VARCHAR2,
9+
metric IN BINARY_INTEGER,
10+
kind IN VARCHAR2,
11+
tag IN VARCHAR2)
12+
RETURN VARCHAR2 AS LANGUAGE C
13+
NAME "metrictoddog"
14+
LIBRARY libtoddog
15+
WITH CONTEXT
16+
PARAMETERS (
17+
CONTEXT,
18+
name STRING,
19+
name INDICATOR short,
20+
metric INT,
21+
metric INDICATOR short,
22+
kind STRING,
23+
kind INDICATOR short,
24+
tag STRING,
25+
tag INDICATOR short,
26+
RETURN INDICATOR short,
27+
RETURN LENGTH short,
28+
RETURN STRING);
29+
/
30+
show err
31+
32+
CREATE OR REPLACE FUNCTION f_gaugetoddog(
33+
name IN VARCHAR2,
34+
metric IN number,
35+
tag IN VARCHAR2)
36+
RETURN VARCHAR2 IS
37+
BEGIN
38+
return f_metrictoddog(name,metric,'g',tag);
39+
END;
40+
/
41+
show err
42+
43+
CREATE OR REPLACE FUNCTION f_counttoddog(
44+
name IN VARCHAR2,
45+
metric IN number,
46+
tag IN VARCHAR2)
47+
RETURN VARCHAR2 IS
48+
BEGIN
49+
return f_metrictoddog(name,metric,'c',tag);
50+
END;
51+
/
52+
show err
53+
54+
prompt calling function from pl/sql block
55+
set serveroutput on
56+
57+
declare
58+
name varchar2(50);
59+
metric int(10);
60+
tag varchar2(50);
61+
result varchar2(10);
62+
err_num NUMBER;
63+
err_msg VARCHAR2(100);
64+
begin
65+
name := 'sample.gauge';
66+
metric := 15;
67+
tag := 'plsql';
68+
result := f_gaugetoddog(name,metric,tag);
69+
dbms_output.put_line(result);
70+
EXCEPTION
71+
WHEN OTHERS THEN
72+
err_num := SQLCODE;
73+
err_msg := SUBSTR(SQLERRM, 1, 100);
74+
raise_application_error(err_num, err_msg);
75+
END;
76+
/
77+
show err
78+
79+
declare
80+
name varchar2(50);
81+
metric int(10);
82+
tag varchar2(50);
83+
result varchar2(10);
84+
err_num NUMBER;
85+
err_msg VARCHAR2(100);
86+
begin
87+
name := 'sample.count';
88+
metric := 15;
89+
tag := 'plsql';
90+
result := f_counttoddog(name,metric,tag);
91+
dbms_output.put_line(result);
92+
EXCEPTION
93+
WHEN OTHERS THEN
94+
err_num := SQLCODE;
95+
err_msg := SUBSTR(SQLERRM, 1, 100);
96+
raise_application_error(err_num, err_msg);
97+
END;
98+
/
99+
show err
100+
101+
102+
exit

samples/rowcount.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
set serveroutput on
2+
3+
declare
4+
name varchar2(50);
5+
result varchar2(10);
6+
cursor c_table is
7+
select TABLE_NAME, NUM_ROWS from all_tables where OWNER = 'HR';
8+
errorinloop EXCEPTION;
9+
err_num NUMBER;
10+
err_msg VARCHAR2(100);
11+
begin
12+
name := 'sample.rowcount';
13+
for t in c_table loop
14+
result := f_gaugetoddog(name,t.num_rows,t.table_name);
15+
IF result != 'done' THEN
16+
RAISE errorinloop;
17+
END IF;
18+
end loop;
19+
EXCEPTION
20+
WHEN errorinloop THEN
21+
raise_application_error(-20101, 'error inside loop on samples/rowcount.sql');
22+
WHEN OTHERS THEN
23+
err_num := SQLCODE;
24+
err_msg := SUBSTR(SQLERRM, 1, 100);
25+
raise_application_error(err_num, err_msg);
26+
END;
27+
/
28+
29+
show err
30+
31+
exit

0 commit comments

Comments
 (0)