Skip to content

Commit b7c136e

Browse files
committed
Merge pull request #27 from arno82/add-filtering
Added Filtering
2 parents 7b365cf + 0bc01fd commit b7c136e

File tree

4 files changed

+125
-34
lines changed

4 files changed

+125
-34
lines changed

src/main/java/com/googlecode/scheme2ddl/Main.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ public class Main {
3939
private static String schemas;
4040
private static boolean isLaunchedByDBA;
4141
private static List<String> schemaList;
42+
private static String objectFilter = "%";
43+
private static String typeFilter = "";
44+
private static String typeFilterMode = "include";
4245

4346
public static void main(String[] args) throws Exception {
47+
typeFilterMode = "include"; //default is to include any type filter
48+
4449
collectArgs(args);
4550
if (justPrintUsage) {
4651
printUsage();
@@ -60,7 +65,10 @@ public static void main(String[] args) throws Exception {
6065
if (justTestConnection) {
6166
testDBConnection(context);
6267
} else {
63-
new UserObjectJobRunner().start(context, isLaunchedByDBA);
68+
System.out.println("DDL object filter: " + objectFilter);
69+
System.out.println("DDL type filter: " + typeFilter);
70+
System.out.println("DDL type filter mode: " + typeFilterMode);
71+
new UserObjectJobRunner().start(context, isLaunchedByDBA, objectFilter.toLowerCase(), typeFilter.toUpperCase(), typeFilterMode.toLowerCase());
6472
}
6573
}
6674

@@ -232,19 +240,23 @@ private static void printUsage() {
232240
msg.append("internally call to dbms_metadata.get_ddl " + lSep);
233241
msg.append("more config options in scheme2ddl.config.xml " + lSep);
234242
msg.append("Options: " + lSep);
235-
msg.append(" -help, -h print this message" + lSep);
243+
msg.append(" -help, -h print this message" + lSep);
236244
// msg.append(" -verbose, -v be extra verbose" + lSep);
237-
msg.append(" -url, DB connection URL" + lSep);
238-
msg.append(" example: scott/tiger@localhost:1521:ORCL" + lSep);
245+
msg.append(" -url, DB connection URL" + lSep);
246+
msg.append(" example: scott/tiger@localhost:1521:ORCL" + lSep);
239247

240-
msg.append(" -o, --output, output dir" + lSep);
241-
msg.append(" -p, --parallel, number of parallel thread (default 4)" + lSep);
242-
msg.append(" -s, --schemas, a comma separated list of schemas for processing" + lSep);
243-
msg.append(" (works only if connected to oracle as sysdba)" + lSep);
244-
msg.append(" -c, --config, path to scheme2ddl config file (xml)" + lSep);
245-
msg.append(" --stop-on-warning, stop on getting DDL error (skip by default)" + lSep);
246-
msg.append(" -tc,--test-connection, test db connection available" + lSep);
247-
msg.append(" -version, print version info and exit" + lSep);
248+
msg.append(" -o, --output, output dir" + lSep);
249+
msg.append(" -p, --parallel, number of parallel thread (default 4)" + lSep);
250+
msg.append(" -s, --schemas, a comma separated list of schemas for processing" + lSep);
251+
msg.append(" (works only if connected to oracle as sysdba)" + lSep);
252+
msg.append(" -c, --config, path to scheme2ddl config file (xml)" + lSep);
253+
msg.append(" -f, --filter, filter for specific DDL objects" + lSep);
254+
msg.append(" every LIKE wildcard can be used" + lSep);
255+
msg.append(" -tf, --type-filter, filter for specific DDL object types" + lSep);
256+
msg.append(" -tfm, --type-filtermode, mode for type filter: include(default) or exclude" + lSep);
257+
msg.append(" --stop-on-warning, stop on getting DDL error (skip by default)" + lSep);
258+
msg.append(" -tc,--test-connection, test db connection available" + lSep);
259+
msg.append(" -version, print version info and exit" + lSep);
248260
System.out.println(msg.toString());
249261
}
250262

@@ -290,6 +302,19 @@ private static void collectArgs(String[] args) throws Exception {
290302
} else if (arg.equals("-c") || arg.equals("--config")) {
291303
customConfigLocation = args[i + 1];
292304
i++;
305+
} else if (arg.equals("-f") || arg.equals("--filter")) {
306+
objectFilter = args[i + 1];
307+
i++;
308+
} else if (arg.equals("-tf") || arg.equals("--type-filter")) {
309+
typeFilter = args[i + 1];
310+
i++;
311+
} else if (arg.equals("-tfm") || arg.equals("--type-filtermode")) {
312+
typeFilterMode = args[i + 1];
313+
i++;
314+
//default to include if anything except include or exclude is given as argument
315+
if (!typeFilterMode.equals("include") && !typeFilterMode.equals("exclude")) {
316+
typeFilterMode = "include";
317+
}
293318
} else if (arg.equals("-version")) {
294319
justPrintVersion = true;
295320
} else if (arg.startsWith("-")) {

src/main/java/com/googlecode/scheme2ddl/TypeNamesUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public static String map2TypeForDBMS(String type) {
1919
return "DB_LINK";
2020
if (type.equals("JOB"))
2121
return "PROCOBJ";
22+
if (type.equals("SCHEDULE"))
23+
return "PROCOBJ";
2224
return type.replace(" ", "_");
2325
}
2426

src/main/java/com/googlecode/scheme2ddl/UserObjectJobRunner.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class UserObjectJobRunner {
2020
protected static final Log logger = LogFactory.getLog(UserObjectJobRunner.class);
2121
private JobLauncher launcher;
2222

23-
int start(ConfigurableApplicationContext context, boolean launchedByDBA) throws Exception {
23+
int start(ConfigurableApplicationContext context, boolean launchedByDBA, String objectFilter, String typeFilter, String typeFilterMode) throws Exception {
2424
try {
2525
context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
2626
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
@@ -37,6 +37,9 @@ int start(ConfigurableApplicationContext context, boolean launchedByDBA) throws
3737
JobParametersBuilder parametersBuilder = new JobParametersBuilder();
3838
parametersBuilder.addString("schemaName", schemaName);
3939
parametersBuilder.addString("launchedByDBA", Boolean.toString(launchedByDBA));
40+
parametersBuilder.addString("objectFilter", objectFilter);
41+
parametersBuilder.addString("typeFilter", typeFilter);
42+
parametersBuilder.addString("typeFilterMode", typeFilterMode);
4043
JobParameters jobParameters = parametersBuilder.toJobParameters();
4144
logger.trace(String.format("Start spring batch job with parameters %s", jobParameters));
4245
JobExecution jobExecution = launcher.run(job, jobParameters);

src/main/java/com/googlecode/scheme2ddl/dao/UserObjectDaoImpl.java

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.util.List;
1717
import java.util.Map;
1818

19-
import static com.googlecode.scheme2ddl.TypeNamesUtil.map2TypeForConfig;
19+
//import static com.googlecode.scheme2ddl.TypeNamesUtil.map2TypeForConfig;
2020

2121
/**
2222
* @author A_Reshetnikov
@@ -30,32 +30,63 @@ public class UserObjectDaoImpl extends JdbcDaoSupport implements UserObjectDao {
3030
private String schemaName;
3131
@Value("#{jobParameters['launchedByDBA']}")
3232
private boolean isLaunchedByDBA = false;
33+
@Value("#{jobParameters['objectFilter']}")
34+
private String objectFilter;
35+
@Value("#{jobParameters['typeFilter']}")
36+
private String typeFilter;
37+
@Value("#{jobParameters['typeFilterMode']}")
38+
private String typeFilterMode = "include";
3339

3440
public List<UserObject> findListForProccessing() {
3541
String sql;
36-
if (isLaunchedByDBA)
37-
sql = "select t.object_name, t.object_type " +
42+
if (isLaunchedByDBA) {
43+
sql = "select t.object_name, object_type " +
3844
" from dba_objects t " +
3945
" where t.generated = 'N' " +
46+
" and lower(t.object_name) like '" + objectFilter + "' " +
4047
" and t.owner = '" + schemaName + "' " +
4148
" and not exists (select 1 " +
4249
" from user_nested_tables unt" +
43-
" where t.object_name = unt.table_name)" +
44-
" UNION ALL " +
45-
" select rname as object_name, 'REFRESH_GROUP' as object_type " +
46-
" from dba_refresh a " +
47-
" where a.rowner = '" + schemaName + "' ";
48-
else
49-
sql = "select t.object_name, t.object_type " +
50+
" where t.object_name = unt.table_name)";
51+
if (!typeFilter.isEmpty()) { //type filter is filled
52+
sql += " and upper(t.object_type) ";
53+
54+
if (typeFilterMode.equals("exclude")) //exclude types
55+
sql += " NOT ";
56+
57+
sql += " IN (" + typeFilter + ") ";
58+
}
59+
if (isTypeAllowed("'REFRESH GROUP'")) {
60+
sql += " UNION ALL " +
61+
" select rname as object_name, 'REFRESH_GROUP' as object_type " +
62+
" from dba_refresh a " +
63+
" where a.rowner = '" + schemaName + "' " +
64+
" and lower(a.rname) like '" + objectFilter + "' ";
65+
}
66+
} else {
67+
sql = "select t.object_name, object_type " +
5068
" from user_objects t " +
5169
" where t.generated = 'N' " +
70+
" and lower(t.object_name) like '" + objectFilter + "' " +
5271
" and not exists (select 1 " +
5372
" from user_nested_tables unt" +
54-
" where t.object_name = unt.table_name)" +
55-
" UNION ALL " +
56-
" select rname as object_name, 'REFRESH GROUP' as object_type " +
57-
" from user_refresh ";
58-
return getJdbcTemplate().query(sql, new UserObjectRowMapper());
73+
" where t.object_name = unt.table_name)";
74+
if (!typeFilter.isEmpty()) {
75+
sql += " and upper(t.object_type) ";
76+
77+
if (typeFilterMode.equals("exclude")) //exclude types
78+
sql += " NOT ";
79+
80+
sql += " IN (" + typeFilter + ") ";
81+
}
82+
if (isTypeAllowed("'REFRESH GROUP'")) {
83+
sql += " UNION ALL " +
84+
" select rname as object_name, 'REFRESH_GROUP' as object_type " +
85+
" from user_refresh " +
86+
" where lower(rname) like '" + objectFilter + "' ";
87+
}
88+
}
89+
return getJdbcTemplate().query(sql, new UserObjectRowMapper());
5990
}
6091

6192
public List<UserObject> findPublicDbLinks() {
@@ -64,7 +95,8 @@ public List<UserObject> findPublicDbLinks() {
6495
list = getJdbcTemplate().query(
6596
"select db_link as object_name, 'PUBLIC DATABASE LINK' as object_type " +
6697
"from DBA_DB_LINKS " +
67-
"where owner='PUBLIC'",
98+
"where owner='PUBLIC'" +
99+
" and lower(db_link) like '" + objectFilter + "' ",
68100
new UserObjectRowMapper());
69101
} catch (BadSqlGrammarException sqlGrammarException) {
70102
if (sqlGrammarException.getSQLException().getErrorCode() == 942) {
@@ -97,26 +129,44 @@ public List<UserObject> findDmbsJobs() {
97129
String tableName = isLaunchedByDBA ? "dba_jobs" : "user_jobs";
98130
String whereClause = isLaunchedByDBA ? "schema_user = '" + schemaName + "'" : "schema_user != 'SYSMAN'";
99131
String sql = "select job || '' as object_name, 'DBMS JOB' as object_type " +
100-
"from " + tableName + " where " + whereClause;
101-
return getJdbcTemplate().query(sql, new UserObjectRowMapper());
132+
"from " + tableName + " where " + whereClause + " and to_char(job) like '" + objectFilter + "' ";
133+
// a little bit ugly, but this prevents an output from jobs if dbms job is not in typeFilter
134+
if (!isTypeAllowed("'DBMS JOB'")) {
135+
sql += " and 1 = 2 ";
136+
}
137+
return getJdbcTemplate().query(sql, new UserObjectRowMapper());
102138
}
103139

104140
public List<UserObject> findConstaints() {
105141
String sql;
142+
String prevent_constraint = new String("");
143+
String prevent_refconstraint = new String("");
144+
145+
if (!isTypeAllowed("'CONSTRAINT'")) {
146+
prevent_constraint = " and 1 = 2 ";
147+
}
148+
if (!isTypeAllowed("'REF_CONSTRAINT'")) {
149+
prevent_refconstraint = " and 1 = 2 ";
150+
}
106151
if (isLaunchedByDBA)
107152
sql = " select constraint_name as object_name, 'CONSTRAINT' as object_type" +
108153
" from all_constraints " +
109154
" where constraint_type != 'R' and owner = '" + schemaName + "'" +
155+
" and lower(constraint_name) like '" + objectFilter + "' " + prevent_constraint +
110156
" UNION ALL " +
111157
" select constraint_name as object_name, 'REF_CONSTRAINT' as object_type" +
112158
" from all_constraints " +
113-
" where constraint_type = 'R' and owner = '" + schemaName + "'";
159+
" where constraint_type = 'R' and owner = '" + schemaName + "'" +
160+
" and lower(constraint_name) like '" + objectFilter + "' " + prevent_refconstraint;
114161
else
115162
sql = " select constraint_name as object_name, 'CONSTRAINT' as object_type" +
116163
" from user_constraints where constraint_type != 'R'" +
164+
" and lower(constraint_name) like '" + objectFilter + "' " + prevent_constraint +
117165
" UNION ALL " +
118166
" select constraint_name as object_name, 'REF_CONSTRAINT' as object_type" +
119-
" from user_constraints where constraint_type = 'R'";
167+
" from user_constraints where constraint_type = 'R'" +
168+
" and lower(constraint_name) like '" + objectFilter + "' " + prevent_refconstraint;
169+
120170
return getJdbcTemplate().query(sql, new UserObjectRowMapper());
121171
}
122172

@@ -260,9 +310,20 @@ private class UserObjectRowMapper implements RowMapper {
260310
public UserObject mapRow(ResultSet rs, int rowNum) throws SQLException {
261311
UserObject userObject = new UserObject();
262312
userObject.setName(rs.getString("object_name"));
263-
userObject.setType(rs.getString("object_type"));
313+
userObject.setType(rs.getString("object_type"));
264314
userObject.setSchema(schemaName == null ? "" : schemaName);
265315
return userObject;
266316
}
267317
}
318+
319+
private boolean isTypeAllowed (String typeName) {
320+
if (typeFilter.isEmpty()) // empty type filter means all types are allowed
321+
return true;
322+
if (typeFilterMode.equals("include") && typeFilter.contains(typeName)) // given typeName is in the typeFilter
323+
return true;
324+
if (typeFilterMode.equals("exclude") && !typeFilter.contains(typeName)) // given typeName is not in the typeFilter
325+
return true;
326+
327+
return false;
328+
}
268329
}

0 commit comments

Comments
 (0)