Skip to content

Commit b61512f

Browse files
part 2 of #5. closes #5
enable/disable "Run utPLSQL test" in PL/SQL editor
1 parent e95e0cc commit b61512f

File tree

5 files changed

+169
-50
lines changed

5 files changed

+169
-50
lines changed

sqldev/src/main/java/org/utplsql/sqldev/menu/UtplsqlController.xtend

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ class UtplsqlController implements Controller {
5757
if (view instanceof Editor) {
5858
val component = view.defaultFocusComponent
5959
if (component instanceof JEditorPane) {
60-
val parser = new UtplsqlParser(component.text)
60+
val node = context.node
61+
var String connectionName = null;
62+
if (node instanceof DatabaseSourceNode) {
63+
connectionName = node.connectionName
64+
} else if (view instanceof Worksheet) {
65+
connectionName = view.connectionName
66+
}
67+
logger.fine('''connectionName: «connectionName»''')
68+
val parser = new UtplsqlParser(component.text, Connections.instance.getConnection(connectionName))
6169
if (!parser.getPathAt(component.caretPosition).empty) {
6270
action.enabled = true
6371
}
@@ -127,16 +135,16 @@ class UtplsqlController implements Controller {
127135
if (view instanceof Editor) {
128136
val component = view.defaultFocusComponent
129137
if (component instanceof JEditorPane) {
130-
val parser = new UtplsqlParser(component.text)
131-
val position = component.caretPosition
132-
val path = parser.getPathAt(position)
133-
var String connectionName = null;
138+
var String connectionName = null;
134139
if (node instanceof DatabaseSourceNode) {
135140
connectionName = node.connectionName
136141
} else if (view instanceof Worksheet) {
137142
connectionName = view.connectionName
138143
}
139144
logger.fine('''connectionName: «connectionName»''')
145+
val parser = new UtplsqlParser(component.text, Connections.instance.getConnection(connectionName))
146+
val position = component.caretPosition
147+
val path = parser.getPathAt(position)
140148
val utPlsqlWorksheet = new UtplsqlWorksheet(path, connectionName)
141149
utPlsqlWorksheet.runTestAsync
142150
}

sqldev/src/main/java/org/utplsql/sqldev/model/parser/PlsqlObject.xtend

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
*/
1616
package org.utplsql.sqldev.model.parser
1717

18+
import java.util.List
1819
import org.eclipse.xtend.lib.annotations.Accessors
1920
import org.utplsql.sqldev.model.AbstractModel
21+
import org.utplsql.sqldev.model.ut.Annotation
2022

2123
@Accessors
2224
class PlsqlObject extends AbstractModel {
2325
String name
2426
Integer position
27+
List<Annotation> annotations
2528
}

sqldev/src/main/java/org/utplsql/sqldev/parser/UtplsqlParser.xtend

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
*/
1515
package org.utplsql.sqldev.parser
1616

17+
import java.sql.Connection
1718
import java.util.ArrayList
19+
import java.util.Arrays
1820
import java.util.regex.Pattern
1921
import javax.swing.text.JTextComponent
22+
import org.utplsql.sqldev.dal.UtplsqlDao
2023
import org.utplsql.sqldev.model.parser.PlsqlObject
2124
import org.utplsql.sqldev.model.parser.Unit
2225

@@ -26,11 +29,16 @@ class UtplsqlParser {
2629
private ArrayList<PlsqlObject> objects = new ArrayList<PlsqlObject>
2730
private ArrayList<Unit> units = new ArrayList<Unit>
2831

29-
new(String plsql) {
32+
new(String plsql, Connection conn) {
3033
setPlsql(plsql)
3134
setPlsqlReduced
3235
populateObjects
3336
populateUnits
37+
processAnnotations(conn)
38+
}
39+
40+
new(String plsql) {
41+
this(plsql, null)
3442
}
3543

3644
/**
@@ -104,14 +112,51 @@ class UtplsqlParser {
104112
}
105113
}
106114

107-
private def getObjectNameAt(int position) {
108-
var name = ""
115+
private def processAnnotations(Connection conn) {
116+
if (conn !== null) {
117+
val dao = new UtplsqlDao(conn)
118+
if (dao.utAnnotationManagerInstalled) {
119+
for (o : objects) {
120+
val segments = Arrays.asList(o.name.fixName.split("\\."))
121+
val annotations = dao.annotations(conn.schema, segments.last.toUpperCase)
122+
if (annotations.findFirst[it.name == "suite"] !== null) {
123+
o.annotations = annotations
124+
}
125+
}
126+
val fixedUnits = new ArrayList<Unit>
127+
for (u : units) {
128+
val o = getObjectAt(u.position)
129+
if (o?.annotations !== null && o.annotations.findFirst [
130+
it.name == "test" && it.subobjectName.equalsIgnoreCase(u.name.fixName)
131+
] !== null) {
132+
fixedUnits.add(u)
133+
}
134+
}
135+
units = fixedUnits
136+
val fixedObjects = new ArrayList<PlsqlObject>
137+
for (o : objects) {
138+
if (o.annotations !== null) {
139+
fixedObjects.add(o)
140+
}
141+
}
142+
objects = fixedObjects
143+
}
144+
}
145+
}
146+
147+
private def getObjectAt(int position) {
148+
var PlsqlObject obj
109149
for (o : objects) {
110150
if (o.position <= position) {
111-
name = o.name
151+
obj = o
112152
}
113153
}
114-
return name
154+
return obj
155+
}
156+
157+
private def getObjectNameAt(int position) {
158+
val o = getObjectAt(position)
159+
return if (o !== null) {o.name} else {""}
115160
}
116161

117162
private def getUnitNameAt(int position) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
*/
1616
package org.utplsql.sqldev.tests
1717

18+
import java.io.StringReader
19+
import java.util.ArrayList
1820
import java.util.Properties
21+
import oracle.dbtools.raptor.newscriptrunner.SQLCommand.StmtType
22+
import oracle.dbtools.worksheet.scriptparser.sqlplus.SQLPlusScriptParser
1923
import org.springframework.jdbc.core.JdbcTemplate
2024
import org.springframework.jdbc.datasource.SingleConnectionDataSource
2125

@@ -43,4 +47,17 @@ abstract class AbstractJdbcTest {
4347
sysDataSource.password = p.getProperty("sys_password")
4448
sysJdbcTemplate = new JdbcTemplate(AbstractJdbcTest.sysDataSource)
4549
}
50+
51+
def static getStatements(String sqlplusScript) {
52+
var SQLPlusScriptParser p = new SQLPlusScriptParser(new StringReader(sqlplusScript))
53+
val stmts = new ArrayList<String>
54+
while (p.hasNext) {
55+
val stmt = p.next
56+
if ((stmt.executable || stmt.runnable) && stmt.stmtType != StmtType.G_C_COMMENT &&
57+
stmt.stmtType != StmtType.G_C_MULTILINECOMMENT && stmt.stmtType != StmtType.G_C_SQLPLUS) {
58+
stmts.add(stmt.sql)
59+
}
60+
}
61+
return stmts;
62+
}
4663
}

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

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,66 @@
1515
*/
1616
package org.utplsql.sqldev.tests
1717

18+
import org.junit.AfterClass
1819
import org.junit.Assert
20+
import org.junit.BeforeClass
1921
import org.junit.Test
22+
import org.springframework.jdbc.BadSqlGrammarException
2023
import org.utplsql.sqldev.parser.UtplsqlParser
2124

22-
class UtplsqlParserTest {
25+
class UtplsqlParserTest extends AbstractJdbcTest {
26+
27+
private static val sqlScript = '''
28+
PROMPT
29+
PROMPT Install utPLSQL test package
30+
PROMPT
31+
32+
/*
33+
* some comment
34+
*/
35+
CREATE OR REPLACE PACKAGE pkg IS
36+
-- %suite
37+
-- %rollback(manual)
38+
-- %test
39+
PROCEDURE p (in_p1 INTEGER);
40+
FUNCTION f (in_p1 INTEGER) RETURN INTEGER;
41+
END pkg;
42+
/
43+
SHOW ERRORS
44+
45+
CREATE OR REPLACE PACKAGE BODY "SCOTT"."PKG" IS
46+
PROCEDURE "P" (in_p1 INTEGER) IS
47+
BEGIN
48+
NULL;
49+
END p;
50+
51+
/* comment 1 */
52+
-- comment 2
53+
/* comment 3 */
54+
-- comment 4
55+
56+
FUNCTION "F" (in_p1 INTEGER) RETURN INTEGER IS
57+
BEGIN
58+
RETURN 1;
59+
END f;
60+
END pkg;
61+
/
62+
SHOW ERRORS
63+
'''
64+
65+
@BeforeClass
66+
@AfterClass
67+
def static void setupAndTeardown() {
68+
try {
69+
jdbcTemplate.execute("DROP PACKAGE pkg")
70+
} catch (BadSqlGrammarException e) {
71+
// ignore
72+
}
73+
}
2374

2475
@Test
2576
def testPackage() {
26-
val plsql = '''
27-
PROMPT
28-
PROMPT Install utPLSQL test package
29-
PROMPT
30-
31-
/*
32-
* some comment
33-
*/
34-
-- %suite
35-
-- %rollback(manual)
36-
CREATE OR REPLACE PACKAGE pkg IS
37-
-- %test
38-
PROCEDURE p (in_p1 INTEGER);
39-
FUNCTION f (in_p1 INTEGER) RETURN INTEGER;
40-
END pkg;
41-
/
42-
SHOW ERRORS
43-
44-
CREATE OR REPLACE PACKAGE BODY "SCOTT"."PKG" IS
45-
PROCEDURE "P" (in_p1 INTEGER) IS
46-
BEGIN
47-
NULL;
48-
END p;
49-
50-
/* comment 1 */
51-
-- comment 2
52-
/* comment 3 */
53-
-- comment 4
54-
55-
FUNCTION "F" (in_p1 INTEGER) RETURN INTEGER IS
56-
BEGIN
57-
RETURN 1;
58-
END f;
59-
END pkg;
60-
/
61-
SHOW ERRORS
62-
'''
63-
val parser = new UtplsqlParser(plsql)
77+
val parser = new UtplsqlParser(sqlScript)
6478
val objects = parser.getObjects
6579
Assert.assertEquals(2, objects.size)
6680
Assert.assertEquals("pkg", objects.get(0).name)
@@ -81,6 +95,39 @@ class UtplsqlParserTest {
8195
Assert.assertEquals("SCOTT.PKG.P", parser.getPathAt(22,10))
8296
Assert.assertEquals("SCOTT.PKG.P", parser.getPathAt(29,1))
8397
}
98+
99+
@Test
100+
def testPackageWithConnection() {
101+
val plsql = '''
102+
/*
103+
* some comment
104+
*/
105+
CREATE OR REPLACE PACKAGE pkg IS
106+
-- %suite
107+
-- %rollback(manual)
108+
109+
-- %test
110+
PROCEDURE p (in_p1 INTEGER);
111+
FUNCTION f (in_p1 INTEGER) RETURN INTEGER;
112+
END pkg;
113+
'''
114+
var parser = new UtplsqlParser(plsql, dataSource.connection)
115+
Assert.assertEquals(0, parser.getObjects.size)
116+
Assert.assertEquals(0, parser.getUnits.size)
117+
jdbcTemplate.execute(plsql)
118+
parser = new UtplsqlParser(plsql, dataSource.connection)
119+
Assert.assertEquals(1, parser.getObjects.size)
120+
Assert.assertEquals(1, parser.getUnits.size)
121+
for (stmt : getStatements(sqlScript)) {
122+
jdbcTemplate.execute(stmt)
123+
}
124+
parser = new UtplsqlParser(sqlScript, dataSource.connection)
125+
Assert.assertEquals(2, parser.getObjects.size)
126+
Assert.assertEquals(2, parser.getUnits.size)
127+
Assert.assertEquals("pkg.p", parser.getPathAt(13,1))
128+
Assert.assertEquals("SCOTT.PKG.P", parser.getPathAt(19,1))
129+
setupAndTeardown
130+
}
84131

85132
@Test
86133
def issue_1() {
@@ -171,5 +218,4 @@ class UtplsqlParserTest {
171218
Assert.assertEquals("test_expect_not_to_be_null.blob_not_null", parser.getPathAt(13,26))
172219
}
173220

174-
175221
}

0 commit comments

Comments
 (0)