Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f7f626

Browse files
added DAO for units (subprograms) and testables (objects for unit tests)
units to be used for #10 - to generate units for a single object (package, type, procedure, function) testables to be used for #11 - to populate nodes in oddgen view
1 parent a8aabea commit 8f7f626

2 files changed

Lines changed: 189 additions & 2 deletions

File tree

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.sql.Connection
1919
import java.util.List
20+
import org.oddgen.sqldev.generators.model.Node
2021
import org.springframework.dao.DataAccessException
2122
import org.springframework.dao.EmptyResultDataAccessException
2223
import org.springframework.jdbc.core.BeanPropertyRowMapper
@@ -175,5 +176,90 @@ class UtplsqlDao {
175176
val result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Annotation>(Annotation), #[owner, objectName])
176177
return result
177178
}
179+
180+
/**
181+
* Gets a list of public units in the object type
182+
*
183+
* @param objectType expected object types are PACKAGE, TYPE, FUNCTION, PROCEDURE
184+
* @param objectName name of the object
185+
* @return list of the public units in the object type
186+
* @throws DataAccessException if there is a problem
187+
*/
188+
def List<String> units(String objectType, String objectName) {
189+
if (objectType == "PACKAGE" || objectType == "TYPE") {
190+
val sql = '''
191+
SELECT procedure_name
192+
FROM user_procedures
193+
WHERE object_type = ?
194+
AND object_name = ?
195+
AND procedure_name IS NOT NULL
196+
GROUP BY procedure_name
197+
ORDER BY min(subprogram_id)
198+
'''
199+
val result = jdbcTemplate.queryForList(sql, String, #[objectType, objectName])
200+
return result
201+
} else {
202+
return #[objectName]
203+
}
204+
}
178205

206+
/**
207+
* Gets a list of oddgen's nodes as candidates to create utPLSQL test packages.
208+
* Candidates are packages, types, functions and procedures in the current user.
209+
*
210+
* This functions must be called from an oddgen generator only, since the Node is not
211+
* defined in the utPLSQL extension.
212+
*
213+
* @param objectType expected object types are PACKAGE, TYPE, FUNCTION, PROCEDURE
214+
* @return list of the oddgen nodes for the requested object type
215+
* @throws DataAccessException if there is a problem
216+
*/
217+
def List<Node> testables(String objectType) {
218+
var String sql;
219+
if (objectType == "PACKAGE") {
220+
sql = '''
221+
SELECT DISTINCT
222+
object_type || '.' || object_name AS id,
223+
object_type AS parent_id,
224+
1 AS leaf,
225+
1 AS generatable,
226+
1 AS multiselectable
227+
FROM user_procedures
228+
WHERE object_type = ?
229+
AND procedure_name IS NOT NULL
230+
AND object_name NOT IN (
231+
SELECT object_name
232+
FROM TABLE(«utplsqlSchema».ut_annotation_manager.get_annotated_objects(USER, 'PACKAGE'))
233+
)
234+
'''
235+
}
236+
else if (objectType == "TYPE") {
237+
sql = '''
238+
SELECT DISTINCT
239+
object_type || '.' || object_name AS id,
240+
object_type AS parent_id,
241+
1 AS leaf,
242+
1 AS generatable,
243+
1 AS multiselectable
244+
FROM user_procedures
245+
WHERE object_type = ?
246+
AND procedure_name IS NOT NULL
247+
'''
248+
}
249+
else {
250+
sql = '''
251+
SELECT object_type || '.' || object_name AS id,
252+
object_type AS parent_id,
253+
1 AS leaf,
254+
1 AS generatable,
255+
1 AS multiselectable
256+
FROM user_objects
257+
WHERE object_type = ?
258+
AND generated = 'N'
259+
'''
260+
}
261+
val jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(conn, true))
262+
val nodes = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Node>(Node), #[objectType])
263+
return nodes
264+
}
179265
}

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

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ class DalTest extends AbstractJdbcTest {
3535
} catch (BadSqlGrammarException e) {
3636
// ignore
3737
}
38+
try {
39+
jdbcTemplate.execute("DROP PACKAGE junit_no_test_pkg")
40+
} catch (BadSqlGrammarException e) {
41+
// ignore
42+
}
43+
try {
44+
jdbcTemplate.execute("DROP TYPE junit_tab1_ot")
45+
} catch (BadSqlGrammarException e) {
46+
// ignore
47+
}
48+
try {
49+
jdbcTemplate.execute("DROP TYPE junit_tab2_ot")
50+
} catch (BadSqlGrammarException e) {
51+
// ignore
52+
}
53+
try {
54+
jdbcTemplate.execute("DROP FUNCTION junit_f")
55+
} catch (BadSqlGrammarException e) {
56+
// ignore
57+
}
58+
try {
59+
jdbcTemplate.execute("DROP PROCEDURE junit_p")
60+
} catch (BadSqlGrammarException e) {
61+
// ignore
62+
}
3863
}
3964

4065
@Test
@@ -63,7 +88,6 @@ class DalTest extends AbstractJdbcTest {
6388
@Test
6489
def void containsUtplsqlTest() {
6590
val dao = new UtplsqlDao(dataSource.connection)
66-
Assert.assertFalse(dao.containsUtplsqlTest("scott"))
6791
jdbcTemplate.execute('''
6892
CREATE OR REPLACE PACKAGE junit_utplsql_test_pkg IS
6993
-- %suite
@@ -104,7 +128,6 @@ class DalTest extends AbstractJdbcTest {
104128
@Test
105129
def void annotations() {
106130
val dao = new UtplsqlDao(dataSource.connection)
107-
Assert.assertEquals(new ArrayList<Annotation>, dao.annotations("scott", "junit_utplsql_test_pkg"))
108131
jdbcTemplate.execute('''
109132
CREATE OR REPLACE PACKAGE junit_utplsql_test_pkg IS
110133
-- %suite
@@ -143,4 +166,82 @@ class DalTest extends AbstractJdbcTest {
143166
Assert.assertEquals(expected.toString, effective.toString)
144167
jdbcTemplate.execute("DROP PACKAGE junit_utplsql_test_pkg")
145168
}
169+
170+
@Test
171+
def void testablesPackages() {
172+
val dao = new UtplsqlDao(dataSource.connection)
173+
jdbcTemplate.execute('''
174+
CREATE OR REPLACE PACKAGE junit_utplsql_test_pkg IS
175+
-- %suite
176+
177+
-- %test
178+
PROCEDURE t1;
179+
180+
-- %Test
181+
PROCEDURE t2;
182+
183+
PROCEDURE t3;
184+
END junit_utplsql_test_pkg;
185+
''')
186+
jdbcTemplate.execute('''
187+
CREATE OR REPLACE PACKAGE junit_no_test_pkg IS
188+
PROCEDURE p1;
189+
190+
PROCEDURE p2;
191+
END junit_no_test_pkg;
192+
''')
193+
val effective = dao.testables('PACKAGE')
194+
Assert.assertEquals(1, effective.size)
195+
Assert.assertEquals("PACKAGE.JUNIT_NO_TEST_PKG", effective.get(0).id)
196+
}
197+
198+
@Test
199+
def void testablesTypes() {
200+
val dao = new UtplsqlDao(dataSource.connection)
201+
jdbcTemplate.execute('''
202+
CREATE OR REPLACE TYPE junit_tab1_ot IS object (a integer, b integer);
203+
''')
204+
jdbcTemplate.execute('''
205+
CREATE OR REPLACE TYPE junit_tab2_ot IS object (
206+
a integer,
207+
b integer,
208+
member procedure c(
209+
self in out nocopy junit_tab2_ot,
210+
p integer
211+
)
212+
);
213+
''')
214+
val effective = dao.testables('TYPE')
215+
Assert.assertEquals(1, effective.size)
216+
Assert.assertEquals("TYPE.JUNIT_TAB2_OT", effective.get(0).id)
217+
}
218+
219+
@Test
220+
def void testablesFunctions() {
221+
val dao = new UtplsqlDao(dataSource.connection)
222+
jdbcTemplate.execute('''
223+
CREATE OR REPLACE FUNCTION junit_f RETURN INTEGER IS
224+
BEGIN
225+
RETURN 1;
226+
END;
227+
''')
228+
val effective = dao.testables('FUNCTION')
229+
Assert.assertEquals(1, effective.size)
230+
Assert.assertEquals("FUNCTION.JUNIT_F", effective.get(0).id)
231+
}
232+
233+
@Test
234+
def void testablesProcedures() {
235+
val dao = new UtplsqlDao(dataSource.connection)
236+
jdbcTemplate.execute('''
237+
CREATE OR REPLACE PROCEDURE junit_p RETURN INTEGER IS
238+
BEGIN
239+
NULL;
240+
END;
241+
''')
242+
val effective = dao.testables('PROCEDURE')
243+
Assert.assertEquals(1, effective.size)
244+
Assert.assertEquals("PROCEDURE.JUNIT_P", effective.get(0).id)
245+
}
246+
146247
}

0 commit comments

Comments
 (0)