Skip to content

Commit 83f5a6d

Browse files
authored
Add files via upload
1 parent f2badaf commit 83f5a6d

File tree

7 files changed

+407
-0
lines changed

7 files changed

+407
-0
lines changed

javascript/nashorn/ReadMe-JavaScript-OJVM.rtf

Lines changed: 201 additions & 0 deletions
Large diffs are not rendered by default.

javascript/nashorn/hello.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function hello()
2+
{
3+
/*
4+
*This is a sample Javascript file that prints "Hello World".
5+
*/
6+
var hellow = "Hello World";
7+
return hellow;
8+
}
9+
var output = hello();
10+
print(output);

javascript/nashorn/hello.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
REM
2+
REM Script for running hello.js in the database
3+
REM
4+
SQL>set serveroutput on
5+
SQL>call dbms_java.set_output(20000);
6+
SQL>call dbms_javascript.run("hello.js");
7+

javascript/nashorn/select.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var selectQuery = function(id)
2+
{
3+
var Driver = Packages.oracle.jdbc.OracleDriver;
4+
var oracleDriver = new Driver();
5+
var url = "jdbc:default:connection:";
6+
var query = "";
7+
var output = "";
8+
if(id == 'all') {
9+
query ="SELECT a.data FROM employees a";
10+
} else {
11+
query ="SELECT a.data FROM employees a WHERE a.data.EmpId=" + id;
12+
}
13+
var connection = oracleDriver.defaultConnection();
14+
// Prepare statement
15+
var preparedStatement = connection.prepareStatement(query);
16+
// execute Query
17+
var resultSet = preparedStatement.executeQuery();
18+
// display results
19+
while(resultSet.next()) {
20+
output = output + resultSet.getString(1) + " ";
21+
}
22+
// cleanup
23+
resultSet.close();
24+
preparedStatement.close();
25+
connection.close();
26+
return output;
27+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
REM
2+
REM Running JavaScript using Javax API in OJVM
3+
REM
4+
REM -------------------------------------------------------------
5+
REM Copyright (c) 2017, Oracle and/or its affiliates. All rights
6+
REM reserved.
7+
REM
8+
REM Author Kuassi Mensah
9+
REM
10+
REM Turn steps i, ii and iii in ReadeMe-JavaScript-OJVM.txt into a Java
11+
REM wrapper class in OJVM.
12+
REM
13+
REM This description is based on select.js
14+
REM For your own JavaScript function, the signature may differ depending
15+
REM on your input parameters and return values.
16+
REM
17+
REM The following script generates the Java wrapper class;
18+
REM you may paste this direcly in a SQL session
19+
REM or put it in a script file and invoke it.
20+
REM
21+
create or replace and compile java resource named "InvokeScript" as
22+
import javax.script.*;
23+
import java.net.*;
24+
import java.io.*;
25+
public class InvokeScript {
26+
public static String eval(String inputId) throws Exception {
27+
String output = new String();
28+
try {
29+
// create a script engine manager
30+
ScriptEngineManager factory = new ScriptEngineManager();
31+
// create a JavaScript engine
32+
ScriptEngine engine = factory.getEngineByName("javascript");
33+
//read the script as a java resource
34+
engine.eval(new InputStreamReader
35+
(InvokeScript.class.getResourceAsStream("select.js")));
36+
/*
37+
* Alternative approach
38+
* engine.eval(Thread.currentThread().getContextClassLoader().getResource("select.js"));
39+
*/
40+
Invocable invocable = (Invocable) engine;
41+
Object selectResult =
42+
invocable.invokeFunction("selectQuery", inputId);
43+
output = selectResult.toString();
44+
} catch(Exception e) {
45+
output =e.getMessage();
46+
}
47+
return output;
48+
}
49+
}
50+
/
51+
52+
53+
REM Create a SQL wrapper for the eval function
54+
REM Create function
55+
REM
56+
CREATE OR REPLACE FUNCTION invokeScriptEval(inputId varchar2) return varchar2 as language java
57+
name 'InvokeScript.eval(java.lang.String) return java.lang.String';
58+
/
59+
60+
REM
61+
REM Allow calling InvokeScriptEval() from SQL or PL/SQL
62+
REM
63+
CREATE OR REPLACE PROCEDURE sqldemo(id IN varchar2)
64+
IS
65+
output varchar2(10000);
66+
BEGIN
67+
SELECT invokeScriptEval(id) INTO output from dual;
68+
dbms_output.put_line(output);
69+
END;
70+
/
71+
SHOW ERRORS;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
REM
2+
REM Create the EMPLOYEES table of JSON documents
3+
REM
4+
DROP TABLE employees PURGE;
5+
6+
CREATE TABLE employees (
7+
id RAW(16) NOT NULL,
8+
data CLOB,
9+
CONSTRAINT employees_pk PRIMARY KEY (id),
10+
CONSTRAINT employees_json_chk CHECK (data IS JSON)
11+
);
12+
13+
TRUNCATE TABLE employees;
14+
15+
INSERT INTO employees (id, data)
16+
VALUES (SYS_GUID(),
17+
'{
18+
"EmpId" : "100",
19+
"FirstName" : "Kuassi",
20+
"LastName" : "Mensah",
21+
"Job" : "Manager",
22+
"Email" : "kuassi@oracle.com",
23+
"Address" : {
24+
"City" : "Redwood",
25+
"Country" : "US"
26+
}
27+
}');
28+
INSERT INTO employees (id, data)
29+
VALUES (SYS_GUID(),
30+
'{
31+
"EmpId" : "200",
32+
"FirstName" : "Nancy",
33+
"LastName" : "Greenberg",
34+
"Job" : "Manager",
35+
"Email" : "Nancy@oracle.com",
36+
"Address" : {
37+
"City" : "Boston",
38+
"Country" : "US"
39+
}
40+
}');
41+
INSERT INTO employees (id, data)
42+
VALUES (SYS_GUID(),
43+
'{
44+
"EmpId" : "300",
45+
"FirstName" : "Suresh",
46+
"LastName" : "Mohan",
47+
"Job" : "Developer",
48+
"Email" : "Suresh@oracle.com",
49+
"Address" : {
50+
"City" : "Bangalore",
51+
"Country" : "India"
52+
}
53+
}');
54+
55+
INSERT INTO employees (id, data)
56+
VALUES (SYS_GUID(),
57+
'{
58+
"EmpId" : "400",
59+
"FirstName" : "Nirmala",
60+
"LastName" : "Sundarappa",
61+
"Job" : "Manager",
62+
"Email" : "Nirmala@oracle.com",
63+
"Address" : {
64+
"City" : "Redwood",
65+
"Country" : "US"
66+
}
67+
}');
68+
69+
INSERT INTO employees (id, data)
70+
VALUES (SYS_GUID(),
71+
'{
72+
"EmpId" : "500",
73+
"FirstName" : "Amarnath",
74+
"LastName" : "Chandana",
75+
"Job" : "Test Devloper",
76+
"Email" : "amarnath@oracle.com",
77+
"Address" : {
78+
"City" : "Bangalore",
79+
"Country" : "India"
80+
}
81+
}');
82+
COMMIT;
83+

javascript/nashorn/selectJS.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
REM
2+
REM Calling SelectQuery (in select.js) using the Javax APi and wrappers (see Javax-wrapper.sql)
3+
REM thru SQLDEMO procedure
4+
REM
5+
set serveroutput on
6+
call dbms_java.set_output(5000);
7+
call sqldemo('100');
8+
REM You may use different parameter values.

0 commit comments

Comments
 (0)