Skip to content

Commit 16ec63f

Browse files
author
Nirmala Sundarappa
committed
Adding a file for HRWebapp
Merge branch 'master' of https://github.com/oracle/oracle-db-examples
2 parents d482f3b + f4ea56a commit 16ec63f

13 files changed

+1663
-11
lines changed

java/ojvm/SODA.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ The furnished testSODA.java on this page already has the change
3131

3232
loadjava -r -v -user hr/hr testSODA.java
3333

34-
(iv) Create the table for persisting the JSON collection and documents using the furnished JSON-tables.sql
35-
36-
(v) Create a SQL wrapper for invoking the main method
34+
(iv) Create a SQL wrapper for invoking the main method
3735

3836
create or replace procedure testSODA as
3937
language java name 'testSODA.main(java.lang.String[])';
4038

41-
(vi) Invoke the wrapper of the main method and display the output
39+
(v) Invoke the wrapper of the main method and display the output
4240

4341
set serveroutput on
4442
call dbms_java.set_output(2000);

javascript/nashorn/SODAjs.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ JavaScript in Oracle database is enabled using Nashorn in the embedded JVM (a.k.
1616
**testSODA.js**
1717

1818
(i) We've rewritten testSODA.java in JavaScript (i.e., testSODA.js), using SODA for Java API.
19-
Grap testSODA.js from [Oracle DB Examples - JavaScript - Nashorn](https://github.com/oracle/oracle-db-examples/upload/master/javascript/nashorn)
20-
21-
(ii) Create the table for persisting the JSON collection and documents using [JSON-table.sql](https://github.com/oracle/oracle-db-examples/blob/master/java/ojvm/JSON-Tables.sql)
19+
Grap testSODA.js from [Oracle DB Examples - JavaScript - Nashorn](https://github.com/oracle/oracle-db-examples/blob/master/javascript/nashorn/testSODA.js)
2220

23-
Note: if you've already tried SODA for Java in the database using the HR schema, then the table already exists and you may skip this step
24-
25-
(iii) load testSODA.js in your schema using
21+
(ii) load testSODA.js in your schema using
2622

2723
loadjava -r -v -user hr/hr testSODA.js
2824

2925

30-
(iv) enable printing SQL output then invoke testSODA.js
26+
(iii) enable printing SQL output then invoke testSODA.js
3127
set serveroutput on
3228
call dbms_java.set_output(2000);
3329
call dbms_javascript.run('testSODA.js');

plsql/code-analysis/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
As a database programming language, PL/SQL code is stored in the Oracle Database.
2+
That means that we can analyze our code using data dictionary views, most importantly
3+
ALL_IDENTIFIERS and ALL_STATEMENTS, which are populated by PL/Scope.
4+
5+
This folder offers a set of stand-alone queries and procedures that demonstrate PL/Scope and give you a quick and easy way to get started with this fantastic utility.
6+
7+
Philipp Salvisberg has also put together a very nice utility full of packages and views that can take you a long way towards fully leverage PL/Scope. Check it out here: https://github.com/PhilippSalvisberg/plscope-utils
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Data is gathered when you compile with this setting on:
2+
3+
ALTER SESSION SET plscope_settings='identifiers:all, statements:all'
4+
5+
*/
6+
7+
SELECT st.owner,
8+
st.object_name,
9+
st.object_type,
10+
st.line,
11+
src.text
12+
FROM all_statements st, all_source src
13+
WHERE st.TYPE = 'COMMIT'
14+
AND st.object_name = src.name
15+
AND st.owner = src.owner
16+
AND st.line = src.line
17+
ORDER BY st.owner,
18+
st.object_name,
19+
st.object_type
20+
/
21+
22+
SELECT st.owner,
23+
st.object_name,
24+
st.object_type,
25+
st.line,
26+
src.text
27+
FROM all_statements st, all_source src
28+
WHERE st.TYPE = 'ROLLBACK'
29+
AND st.object_name = src.name
30+
AND st.owner = src.owner
31+
AND st.line = src.line
32+
ORDER BY st.owner,
33+
st.object_name,
34+
st.object_type
35+
/
36+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
ALTER SESSION SET plscope_settings='identifiers:all, statements:all'
2+
/
3+
4+
CREATE OR REPLACE PROCEDURE p1 (p_id NUMBER, p_name OUT VARCHAR2)
5+
IS
6+
BEGIN
7+
SELECT
8+
last_name
9+
INTO
10+
p_name
11+
FROM
12+
employees
13+
WHERE
14+
employee_id = p_id;
15+
END;
16+
/
17+
18+
CREATE OR REPLACE PROCEDURE p2 (id_in NUMBER, name_out OUT VARCHAR2)
19+
IS
20+
BEGIN
21+
SELECT last_name
22+
INTO name_out
23+
FROM EMPLOYEES
24+
WHERE employee_id = id_in;
25+
END;
26+
/
27+
28+
SELECT signature, sql_id, text
29+
FROM all_statements
30+
WHERE object_name IN ('P1', 'P2')
31+
ORDER BY line, col
32+
/
33+
34+
/* Same SQL appearing more than once? */
35+
36+
SELECT sql_id, text, COUNT (*)
37+
FROM all_statements
38+
WHERE sql_id IS NOT NULL
39+
GROUP BY sql_id, text
40+
HAVING COUNT (*) > 1
41+
/
42+
43+
SELECT owner,
44+
object_name,
45+
line,
46+
text
47+
FROM all_statements
48+
WHERE sql_id IN ( SELECT sql_id
49+
FROM all_statements
50+
WHERE sql_id IS NOT NULL
51+
GROUP BY sql_id
52+
HAVING COUNT (*) > 1)
53+
ORDER BY owner, object_name, line
54+
/
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
You can call your own (user-defined) functions in SQL. But you need to do so with care:
3+
* possible performance impact, due to context switches
4+
* possible read consistency issues, since SQL in a function called from SQL does not participate in "outer" SQL read consistency
5+
*/
6+
7+
ALTER SESSION SET plscope_settings='identifiers:all, statements:all'
8+
/
9+
10+
DROP TABLE my_data
11+
/
12+
13+
CREATE TABLE my_data (n NUMBER)
14+
/
15+
16+
CREATE OR REPLACE FUNCTION my_function1
17+
RETURN NUMBER
18+
AUTHID DEFINER
19+
IS
20+
BEGIN
21+
RETURN 1;
22+
END;
23+
/
24+
25+
CREATE OR REPLACE FUNCTION my_function2
26+
RETURN NUMBER
27+
AUTHID DEFINER
28+
IS
29+
BEGIN
30+
RETURN 1;
31+
END;
32+
/
33+
34+
CREATE OR REPLACE PROCEDURE my_procedure (n_in IN NUMBER)
35+
AUTHID DEFINER
36+
IS
37+
l_my_data my_data%ROWTYPE;
38+
BEGIN
39+
SELECT my_function1 ()
40+
INTO l_my_data
41+
FROM my_data
42+
WHERE n = n_in
43+
AND my_function2 () = 0
44+
AND n = (SELECT my_function1 () FROM DUAL);
45+
46+
SELECT COUNT (*)
47+
INTO l_my_data
48+
FROM my_data
49+
WHERE n = n_in;
50+
51+
UPDATE my_data
52+
SET n = my_function2 ()
53+
WHERE n = n_in;
54+
END;
55+
/
56+
57+
/* Run this query to see the full hierarchy of identifiers/statements */
58+
59+
WITH one_obj_name AS (SELECT 'MY_PROCEDURE' object_name FROM DUAL)
60+
SELECT plscope_type,
61+
usage_id,
62+
usage_context_id,
63+
LPAD (' ', 2 * (LEVEL - 1)) || usage || ' ' || name usages
64+
FROM (SELECT 'ID' plscope_type,
65+
ai.object_name,
66+
ai.usage usage,
67+
ai.usage_id,
68+
ai.usage_context_id,
69+
ai.TYPE || ' ' || ai.name name,
70+
ai.line,
71+
ai.col,
72+
signature
73+
FROM all_identifiers ai, one_obj_name
74+
WHERE ai.object_name = one_obj_name.object_name
75+
UNION ALL
76+
SELECT 'ST',
77+
st.object_name,
78+
st.TYPE,
79+
st.usage_id,
80+
st.usage_context_id,
81+
'STATEMENT',
82+
st.line,
83+
st.col,
84+
signature
85+
FROM all_statements st, one_obj_name
86+
WHERE st.object_name = one_obj_name.object_name)
87+
START WITH usage_context_id = 0
88+
CONNECT BY PRIOR usage_id = usage_context_id
89+
/
90+
91+
/*
92+
ST 7 2 SELECT - STATEMENT
93+
ID 8 7 REFERENCE - TABLE MY_DATA
94+
ID 9 7 REFERENCE - COLUMN N
95+
ID 10 7 REFERENCE - FORMAL IN N_IN
96+
ID 11 7 REFERENCE - COLUMN N
97+
ID 13 7 CALL - FUNCTION MY_FUNCTION1
98+
ID 14 7 CALL - FUNCTION MY_FUNCTION2
99+
ID 15 7 ASSIGNMENT - VARIABLE L_MY_DATA
100+
ID 16 15 CALL - FUNCTION MY_FUNCTION1
101+
*/
102+
103+
/* Now what I want to do is return a list of all SQL statements
104+
that contain within it a call to a function */
105+
106+
WITH my_prog_unit AS (SELECT USER owner, 'MY_PROCEDURE' object_name FROM DUAL),
107+
full_set
108+
AS (SELECT ai.usage,
109+
ai.usage_id,
110+
ai.usage_context_id,
111+
ai.TYPE,
112+
ai.name
113+
FROM all_identifiers ai, my_prog_unit
114+
WHERE ai.object_name = my_prog_unit.object_name
115+
AND ai.owner = my_prog_unit.owner
116+
UNION ALL
117+
SELECT st.TYPE,
118+
st.usage_id,
119+
st.usage_context_id,
120+
'type',
121+
'name'
122+
FROM all_statements st, my_prog_unit
123+
WHERE st.object_name = my_prog_unit.object_name
124+
AND st.owner = my_prog_unit.owner),
125+
dml_statements
126+
AS (SELECT st.owner,
127+
st.object_name,
128+
st.line,
129+
st.usage_id,
130+
st.TYPE
131+
FROM all_statements st, my_prog_unit
132+
WHERE st.object_name = my_prog_unit.object_name
133+
AND st.owner = my_prog_unit.owner
134+
AND st.TYPE IN ('SELECT', 'UPDATE', 'DELETE'))
135+
SELECT st.owner,
136+
st.object_name,
137+
st.line,
138+
st.TYPE,
139+
s.text
140+
FROM dml_statements st, all_source s
141+
WHERE ('CALL', 'FUNCTION') IN ( SELECT fs.usage, fs.TYPE
142+
FROM full_set fs
143+
CONNECT BY PRIOR fs.usage_id =
144+
fs.usage_context_id
145+
START WITH fs.usage_id = st.usage_id)
146+
AND st.line = s.line
147+
AND st.object_name = s.name
148+
AND st.owner = s.owner
149+
/
150+
151+
/* Across all program units, all schemas */
152+
153+
WITH full_set
154+
AS (SELECT ai.owner,
155+
ai.object_name,
156+
ai.usage,
157+
ai.usage_id,
158+
ai.usage_context_id,
159+
ai.TYPE,
160+
ai.name
161+
FROM all_identifiers ai
162+
UNION ALL
163+
SELECT st.owner,
164+
st.object_name,
165+
st.TYPE,
166+
st.usage_id,
167+
st.usage_context_id,
168+
'type',
169+
'name'
170+
FROM all_statements st),
171+
dml_statements
172+
AS (SELECT st.owner,
173+
st.object_name,
174+
st.line,
175+
st.usage_id,
176+
st.TYPE
177+
FROM all_statements st
178+
WHERE st.TYPE IN ('SELECT', 'UPDATE', 'DELETE'))
179+
SELECT st.owner,
180+
st.object_name,
181+
st.line,
182+
st.TYPE,
183+
s.text
184+
FROM dml_statements st, all_source s
185+
WHERE ('CALL', 'FUNCTION') IN ( SELECT fs.usage, fs.TYPE
186+
FROM full_set fs
187+
CONNECT BY PRIOR fs.usage_id =
188+
fs.usage_context_id
189+
START WITH fs.usage_id = st.usage_id)
190+
AND st.line = s.line
191+
AND st.object_name = s.name
192+
AND st.owner = s.owner
193+
/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ALTER SESSION SET plscope_settings='identifiers:all, statements:all'
2+
/
3+
4+
CREATE OR REPLACE PROCEDURE proc1
5+
IS
6+
l_id INTEGER;
7+
BEGIN
8+
SELECT /*+ result_cache */
9+
employee_id
10+
INTO l_id
11+
FROM hr.employees
12+
WHERE last_name = 'KING';
13+
END;
14+
/
15+
16+
CREATE OR REPLACE PROCEDURE proc2
17+
IS
18+
TYPE nt IS TABLE OF INTEGER;
19+
20+
l_ids nt;
21+
BEGIN
22+
SELECT /*+ FIRST_ROWS(10) */
23+
employee_id
24+
BULK COLLECT INTO l_ids
25+
FROM hr.employees;
26+
END;
27+
/
28+
29+
CREATE OR REPLACE PROCEDURE proc3
30+
IS
31+
TYPE nt IS TABLE OF INTEGER;
32+
33+
l_ids nt;
34+
BEGIN
35+
SELECT employee_id
36+
BULK COLLECT INTO l_ids
37+
FROM hr.employees;
38+
END;
39+
/
40+
41+
SELECT owner,
42+
object_name,
43+
line,
44+
full_text
45+
FROM all_statements
46+
WHERE has_hint = 'YES'
47+
/

0 commit comments

Comments
 (0)