Skip to content

Commit 61baad2

Browse files
add getSource to get source of a package spec or body
1 parent 3bbc226 commit 61baad2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

sqldev/src/main/java/org/utplsql/sqldev/dal/UtplsqlDao.xtend

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,4 +858,43 @@ class UtplsqlDao {
858858
return deps
859859
}
860860

861+
/**
862+
* gets source of an object from the database via DBMS_METADATA
863+
*
864+
* @param owner owner of the object (schema)
865+
* @param objectType expected object types are PACKAGE, PACKAGE BODY
866+
* @param objectName name of the object
867+
* @return the source code of the object
868+
* @throws DataAccessException if there is a problem
869+
*/
870+
def getSource(String owner, String objectType, String objectName) {
871+
// dbms_metadata uses slightly different objectTypes
872+
val fixedObjectType = if (objectType == "PACKAGE") {
873+
"PACKAGE_SPEC"
874+
} else if (objectType == "PACKAGE BODY") {
875+
"PACKAGE_BODY"
876+
} else {
877+
objectType
878+
}
879+
val sql = '''
880+
BEGIN
881+
? := sys.dbms_metadata.get_ddl(
882+
schema => ?,
883+
object_type => ?,
884+
name => ?
885+
);
886+
END;
887+
'''
888+
val ret = jdbcTemplate.execute(sql, new CallableStatementCallback<String>() {
889+
override String doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
890+
cs.registerOutParameter(1, Types.CLOB);
891+
cs.setString(2, owner)
892+
cs.setString(3, fixedObjectType)
893+
cs.setString(4, objectName)
894+
cs.execute
895+
return cs.getString(1)
896+
}
897+
})
898+
return ret
899+
}
861900
}

sqldev/src/test/java/org/utplsql/sqldev/test/dal/DalTest.xtend

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class DalTest extends AbstractJdbcTest {
3737
} catch (BadSqlGrammarException e) {
3838
// ignore
3939
}
40+
try {
41+
jdbcTemplate.execute("DROP PACKAGE BODY junit_utplsql_test_pkg")
42+
} catch (BadSqlGrammarException e) {
43+
// ignore
44+
}
4045
try {
4146
jdbcTemplate.execute("DROP PACKAGE junit_no_test_pkg")
4247
} catch (BadSqlGrammarException e) {
@@ -472,5 +477,42 @@ class DalTest extends AbstractJdbcTest {
472477
Assert.assertEquals(expected, actual)
473478

474479
}
480+
481+
@Test
482+
def void getSourceOfPackage() {
483+
val dao = new UtplsqlDao(dataSource.connection)
484+
jdbcTemplate.execute('''
485+
CREATE OR REPLACE PACKAGE junit_utplsql_test_pkg IS
486+
-- %suite
487+
488+
-- %test
489+
PROCEDURE p1;
490+
END junit_utplsql_test_pkg;
491+
''')
492+
val actual = dao.getSource("SCOTT", "PACKAGE", "JUNIT_UTPLSQL_TEST_PKG")
493+
Assert.assertTrue(actual.contains("-- %suite"))
494+
Assert.assertTrue(actual.contains("PROCEDURE p1;"))
495+
jdbcTemplate.execute("DROP PACKAGE junit_utplsql_test_pkg")
496+
}
497+
498+
@Test
499+
def void getSourceOfPackageBody() {
500+
val dao = new UtplsqlDao(dataSource.connection)
501+
jdbcTemplate.execute('''
502+
CREATE OR REPLACE PACKAGE BODY junit_utplsql_test_pkg IS
503+
PROCEDURE p1 IS
504+
l_expected INTEGER := 1;
505+
l_actual INTEGER;
506+
BEGIN
507+
l_actual := junit_f;
508+
ut.expect(l_actual).to_equal(l_expected);
509+
END p1;
510+
END junit_utplsql_test_pkg;
511+
''');
512+
val actual = dao.getSource("SCOTT", "PACKAGE BODY", "JUNIT_UTPLSQL_TEST_PKG")
513+
Assert.assertTrue(actual.contains("PACKAGE BODY"))
514+
Assert.assertTrue(actual.contains("PROCEDURE p1 IS"))
515+
jdbcTemplate.execute("DROP PACKAGE BODY junit_utplsql_test_pkg")
516+
}
475517

476518
}

0 commit comments

Comments
 (0)