-
Notifications
You must be signed in to change notification settings - Fork 16
/
genddl
executable file
·270 lines (242 loc) · 12.7 KB
/
genddl
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/env python
##################################################################################################
# Name: genddl #
# Author: Randy Johnson #
# Description: Dumps DDL for an object using DBMS_METADATA.GET_DDL #
# Notes: The following object types are supported: #
# - CONSTRAINT (Constraints) SND #
# - DB_LINK (Database links) SN #
# - DEFAULT_ROLE (Default roles) G #
# - FUNCTION (Stored functions) SN #
# - INDEX (Indexes) SND #
# - MATERIALIZED_VIEW (Materialized views) SN #
# - MATERIALIZED_VIEW_LOG (Materialized view logs) D #
# - OBJECT_GRANT (Object grants) DG #
# - PACKAGE (Stored packages) SN #
# - PACKAGE_SPEC (Package specifications) SN #
# - PACKAGE_BODY (Package bodies) SN #
# - PROCEDURE (Stored procedures) SN #
# - ROLE (Roles) N #
# - ROLE_GRANT (Role grants) G #
# - SEQUENCE (Sequences) SN #
# - SYNONYM (Synonyms) S #
# - SYSTEM_GRANT (System privilege grants) G #
# - TABLE (Tables) SN #
# - TABLESPACE (Tablespaces) N #
# - TRIGGER (Triggers) SND #
# - TYPE (User-defined types) SN #
# - TYPE_SPEC (Type specifications) SN #
# - TYPE_BODY (Type bodies) SN #
# - USER (Users) N #
# - VIEW (Views) SN #
# #
# Common database objects classified as: #
# (N)amed #
# (D)ependent #
# (G)ranted #
# (S)chema #
# #
# Usage: genddl [options] #
# #
# Options: #
# -h, --help show this help message and exit #
# -n NAME object name #
# -t TYPE object_type #
# -r report objects #
# -s SCHEMA schema #
# --v print version info. #
# #
# History: #
# #
# Date Ver. Who Change Description #
# ---------- ---- ---------------- ------------------------------------------------------------- #
# 07/07/2016 1.00 Randy Johnson Initial write. #
# 03/01/2017 2.00 Randy Johnson Added support for extracting DDL for grants. #
##################################################################################################
# --------------------------------------
# ---- Import Python Modules -----------
# --------------------------------------
from optparse import OptionParser
from os import environ
from os.path import basename
from sys import argv
from sys import exit
from sys import version_info
from signal import SIGPIPE
from signal import SIG_DFL
from signal import signal
from Oracle import RunSqlplus
from Oracle import SetOracleEnv
from Oracle import ParseConnectString
# --------------------------------------
# ---- Main Program --------------------
# --------------------------------------
if (__name__ == '__main__'):
Cmd = basename(argv[0]).split('.')[0]
CmdDesc = 'Generate DDL'
Version = '2.00'
VersionDate = 'Wed Mar 1 09:41:41 CST 2017'
DevState = 'Production'
Banner = CmdDesc + ': Release ' + Version + ' ' + DevState + '. Last updated: ' + VersionDate
Sql = ''
SqlHeader = '/***** ' + CmdDesc.upper() + ' *****/'
ErrChk = False
ArgParser = OptionParser()
InStr = ''
ConnStr = ''
# For handling termination in stdout pipe; ex: when you run: oerrdump | head
signal(SIGPIPE, SIG_DFL)
ArgParser.add_option('-n', dest='Name', default='', type=str, help="object name")
ArgParser.add_option('-t', dest='Type', default='', type=str, help="object type")
ArgParser.add_option('-r', dest='Report', action='store_true', default=False, help="report objects")
ArgParser.add_option('-s', dest='Schema', default='', type=str, help="schema")
ArgParser.add_option('--v', dest='ShowVer', action='store_true', default=False, help="print version info.")
# Parse command line arguments
Options, args = ArgParser.parse_args()
Name = Options.Name
Type = Options.Type
Report = Options.Report
Schema = Options.Schema
ShowVer = Options.ShowVer
if (ShowVer):
print('\n%s' % Banner)
exit()
# Check/setup the Oracle environment
if (not('ORACLE_SID' in list(environ.keys()))):
print('ORACLE_SID is required.')
exit(1)
else:
# Set the ORACLE_HOME just in case it isn't set already.
if (not('ORACLE_HOME' in list(environ.keys()))):
(OracleSid, OracleHome) = SetOracleEnv(environ['ORACLE_SID'])
# Parse the connect string if any, prompt for username, password if needed.
if (len(args) > 0 and Show == False):
InStr = args[0]
ConnStr = ParseConnectString(InStr)
if (Report == True):
Sql = "column owner format a30 heading 'Owner'\n"
Sql += "column object_type format a30 heading 'Object Type'\n"
Sql += "column object_name format a30 heading 'Object Name'\n"
Sql += "column created format a19 heading 'Created'\n"
Sql += "\n"
Sql += "SELECT " + SqlHeader + "\n"
Sql += " OWNER\n"
Sql += " , OBJECT_TYPE\n"
Sql += " , OBJECT_NAME\n"
Sql += " , TO_CHAR(CREATED, 'YYYY-MM-DD HH24:MI:SS') created\n"
Sql += " FROM DBA_OBJECTS\n"
Sql += " WHERE 1=1\n"
if (Schema != ''):
Sql += " AND UPPER(owner) = '" + Schema.upper() + "'\n"
if (Name != ''):
Sql += " AND UPPER(object_name) = '" + Name.upper() + "'\n"
if (Type != ''):
Sql += " AND UPPER(object_type) = '" + Type.upper() + "'\n"
Sql += "ORDER BY owner, object_type, object_name;"
# Execute the report
if (ConnStr != ''):
(Stdout) = RunSqlplus(Sql, ErrChk, ConnStr)
else:
(Stdout) = RunSqlplus(Sql, ErrChk)
# Print the report
if (Stdout != ''):
print("\n\n--------------------------")
print("-- Object Detail Report --")
print("--------------------------")
print('\n%s' % Stdout)
Sql = "column owner format a30 heading 'Owner'\n"
Sql += "column object_type format a30 heading 'Object Type'\n"
Sql += "column count format 999,999,999 heading 'Count'\n"
Sql += "\n"
Sql += "SELECT " + SqlHeader + "\n"
Sql += " OWNER\n"
Sql += " , OBJECT_TYPE\n"
Sql += " , COUNT(*) count\n"
Sql += " FROM DBA_OBJECTS\n"
Sql += " WHERE 1=1\n"
if (Schema != ''):
Sql += " AND UPPER(owner) = '" + Schema.upper() + "'\n"
if (Name != ''):
Sql += " AND UPPER(object_name) = '" + Name.upper() + "'\n"
if (Type != ''):
Sql += " AND UPPER(object_type) = '" + Type.upper() + "'\n"
Sql += "GROUP BY owner, object_type\n"
Sql += "ORDER BY owner, object_type;"
# Execute the report
if (ConnStr != ''):
(Stdout) = RunSqlplus(Sql, ErrChk, ConnStr)
else:
(Stdout) = RunSqlplus(Sql, ErrChk)
# Print the report
if (Stdout != ''):
print("\n\n---------------------------")
print("-- Object Summary Report --")
print("---------------------------")
print('\n%s' % Stdout)
exit()
# If Type was not passed on the command line...
if (Type == ''):
if (version_info[0] >= 3):
Type = input('\nEnter object type: ')
else:
Type = raw_input('\nEnter object type: ')
if (Type == ''):
print('Object type is required.')
exit(1)
Type = Type.strip()
# If Name was not passed on the command line...
if (Name == ''):
if (version_info[0] >= 3):
Name = input('\nEnter object name: ')
else:
Name = raw_input('\nEnter object name: ')
if (Name == ''):
print('Object name is required.')
exit(1)
Name = Name.strip()
Sql = "set heading off\n"
Sql += "set feedback off\n"
Sql += "set echo off\n"
Sql += "set pagesize 0\n\n"
Sql += "SELECT DBMS_METADATA.GET_DDL(\n"
Sql += " OBJECT_TYPE => '" + Type.upper() + "',\n"
if (Schema != ''):
Sql += " SCHEMA => '" + Schema.upper() + "',\n"
Sql += " NAME => '" + Name.upper() + "'\n"
Sql += " )\n"
Sql += " FROM DUAL;\n\n"
Sql += "SELECT DBMS_METADATA.GET_DEPENDENT_DDL(\n"
Sql += " OBJECT_TYPE => '" + Type.upper() + "',\n"
if (Schema != ''):
Sql += " BASE_OBJECT_SCHEMA => '" + Schema.upper() + "',\n"
Sql += " BASE_OBJECT_NAME => '" + Name.upper() + "'\n"
Sql += " )\n"
Sql += " FROM DUAL;"
print(Sql)
#exit()
# Check/setup the Oracle environment
if (not('ORACLE_SID' in list(environ.keys()))):
print('ORACLE_SID is required.')
exit(1)
else:
# Set the ORACLE_HOME just in case it isn't set already.
if (not('ORACLE_HOME' in list(environ.keys()))):
(OracleSid, OracleHome) = SetOracleEnv(environ['ORACLE_SID'])
# Parse the connect string if any, prompt for username, password if needed.
if (len(args) > 0 and Show == False):
InStr = args[0]
ConnStr = ParseConnectString(InStr)
# Execute the report
if (ConnStr != ''):
(Stdout) = RunSqlplus(Sql, ErrChk, ConnStr)
else:
(Stdout) = RunSqlplus(Sql, ErrChk)
# Print the report
if (Stdout != ''):
print('\n-----------cut-----------cut-----------cut-----------cut-----------cut-----------')
print(Stdout)
print('-----------cut-----------cut-----------cut-----------cut-----------cut-----------')
exit(0)
# --------------------------------------
# ---- End Main Program ----------------
# --------------------------------------