-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfdmee_snippet_execute_plsql_storedproc.py
142 lines (120 loc) · 4.84 KB
/
fdmee_snippet_execute_plsql_storedproc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'''
Snippet: Execute a PL/SQL stored procedures IN/OUT params
Author: Francisco Amores
Date: 24/11/2017
Blog: http://fishingwithfdmee.blogspot.com
Notes: This snippet can be pasted in any event script.
Content of fdmContext object will be logged in the
FDMEE process log (...\outbox\logs\)
This snippet executes the stored procedure via dblink
Local stored procedures are executed in a similar way
Instructions: Set log level (global or application settings) to > 4
Hints: You can implement also code to get db connection details
instead of hard-coding
FDMEE Version: 11.1.2.3 and later
----------------------------------------------------------------------
Change:
Author:
Date:
'''
try:
# Import Java libraries
import java.sql.SQLException as SQLException
import java.sql.DriverManager as SQLDriverMgr
import java.sql.CallableStatement as SQLCallableStmt
import java.sql.Types as SQLTypes
import java.sql.Date as SQLDate # needed for DATE parameters
import java.text.SimpleDateFormat as SimpleDateFormat
# Note: import any other class you need
except ImportError, err:
errMsg = "Error importing libraries: %s" % err
fdmAPI.logFatal(errMsg)
raise RuntimeError(errMsg)
# ----------------------------------------
# Connect to FDMEE or External database
# ----------------------------------------
# Connection details
dbConn = "the jdbc url"
dbUser = "the db user"
dbPasswd = "the db password"
try:
# get connection to database for callable statements
conn = SQLDriverMgr.getConnection(dbConn, dbUser, dbPasswd)
fdmAPI.logInfo("Connected to the database")
except SQLException, ex:
errMsg = "Error executing SQL: %s" % (ex)
raise RuntimeError("Error generated from FDMEE script\n%s" % errMsg)
# ----------------------------------------
# Execute PL/SQL Stored Procedure
# ----------------------------------------
# Get dblink
dbLink = "your dblink"
# PL/SQL Block Code (via DBLINK)
'''
Procedure implemeted as:
PROCEDURE CARGA_TABLA(P1 OUT VARCHAR2,
P2 OUT NUMBER,
P3 IN NUMBER,
P4 IN DATETIME,
P5 IN VARCHAR2,
P6 IN VARCHAR2,
P7 IN VARCHAR2,
P8 IN VARCHAR2,
P9 IN VARCHAR2)
'''
# Each ? represents one stored proc parameter
# Ex: schema.package.storedproc if your stored proc is in a package
plSqlBlock = "{CALL schema.package.storedproc@%s(?, ?, ?, ?, ?, ?, ?, ?, ?)}" % dbLink
# Get parameters for the statement
p3 = "valuep3"
# parameter p4 must be passed as java.sql.Date
sdf = SimpleDateFormat("dd/MM/yyyy")
dtParsed = sdf.parse("date value")
p4 = SQLDate(dtParsed.getTime())
p5 = "this param is passed as null"
p6 = "valuep6"
p7 = "valuep7"
p8 = "valuep8"
p9 = "valuep9"
# Prepare and execute call
try:
# Callable Statement
callableStmt = conn.prepareCall(plSqlBlock)
fdmAPI.logInfo("Callable statement successfully prepared")
# Set IN parameters
callableStmt.setBigDecimal("p3", p3)
callableStmt.setDate("p4", p4)
callableStmt.setNull("p5", SQLTypes.VARCHAR) # NULL
callableStmt.setString("p6", p6)
callableStmt.setString("p7", p7)
callableStmt.setString("p8", p7)
callableStmt.setString("p9", p7)
fdmAPI.logInfo("Parameters IN set")
# Register OUT parameters
callableStmt.registerOutParameter("p1", SQLTypes.VARCHAR)
callableStmt.registerOutParameter("p2", SQLTypes.NUMERIC)
fdmAPI.logInfo("Parameters OUT registered")
# Execute PL/SQL Stored Procedure
result = callableStmt.execute()
conn.commit()
fdmAPI.logInfo("Stored Proceedure successfully executed: %s" % result)
# Get OUT parameters
p1 = callableStmt.getString("p1")
p2 = callableStmt.getInt("p2")
# Log OUT parameters
fdmAPI.logInfo("OUT p1: %s" % p1)
fdmAPI.logInfo("OUT p2: %s" % p2)
except (Exception, SQLException), ex:
errMsg = "Error when executing the stored procedure: %s" % ex
fdmAPI.logFatal(errMsg)
if len(errMsg) <= 1000:
fdmAPI.showCustomMessage(errMsg)
raise RuntimeError(errMsg)
# ----------------------------------------
# Close connection
# ----------------------------------------
if callableStmt is not None:
callableStmt.close()
if conn is not None:
conn.close()
fdmAPI.logInfo("DB connection closed")