Skip to content

Commit 82176c9

Browse files
committed
Changing connection behavior and outputbuffer changes
1 parent 11065ee commit 82176c9

File tree

14 files changed

+282
-193
lines changed

14 files changed

+282
-193
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<groupId>com.oracle.jdbc</groupId>
2222
<artifactId>ojdbc7</artifactId>
2323
<version>12.1.0.2</version>
24+
<scope>compile</scope>
2425
</dependency>
2526
<dependency>
2627
<groupId>junit</groupId>

src/main/java/io/github/utplsql/UTPLSQL.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/main/java/io/github/utplsql/OutputBuffer.java renamed to src/main/java/io/github/utplsql/api/OutputBuffer.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package io.github.utplsql;
1+
package io.github.utplsql.api;
22

33
import oracle.jdbc.OracleTypes;
44

55
import java.sql.CallableStatement;
6-
import java.sql.PreparedStatement;
6+
import java.sql.Connection;
77
import java.sql.ResultSet;
88
import java.sql.SQLException;
9-
import java.util.ArrayList;
10-
import java.util.List;
119

1210
/**
1311
* Created by Vinicius on 13/04/2017.
@@ -28,43 +26,47 @@ public void setReporterId(String reporterId) {
2826
this.reporterId = reporterId;
2927
}
3028

31-
public List<String> getLines() throws SQLException {
32-
PreparedStatement preparedStatement = null;
29+
public OutputBufferLines fetchAvailable(Connection conn) throws SQLException {
30+
CallableStatement callableStatement = null;
3331
ResultSet resultSet = null;
3432
try {
35-
preparedStatement = UTPLSQL.getConnection()
36-
.prepareStatement("SELECT * FROM TABLE(ut_output_buffer.get_lines(?, 1))");
33+
callableStatement = conn.prepareCall("BEGIN ? := ut_output_buffer.get_available_lines(?); END;");
3734

38-
preparedStatement.setString(1, getReporterId());
39-
resultSet = preparedStatement.executeQuery();
35+
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
36+
callableStatement.setString(2, getReporterId());
37+
callableStatement.execute();
4038

41-
List<String> outputLines = new ArrayList<>();
39+
resultSet = (ResultSet) callableStatement.getObject(1);
40+
41+
OutputBufferLines outputLines = new OutputBufferLines();
4242
while (resultSet.next()) {
43-
outputLines.add(resultSet.getString(1));
43+
outputLines.add(resultSet.getString("text"));
44+
if (resultSet.getInt("is_finished") == 1)
45+
outputLines.setFinished(true);
4446
}
4547
return outputLines;
4648
} finally {
4749
if (resultSet != null)
4850
resultSet.close();
49-
if (preparedStatement != null)
50-
preparedStatement.close();
51+
if (callableStatement != null)
52+
callableStatement.close();
5153
}
5254
}
5355

54-
public List<String> getAllLines() throws SQLException {
56+
public OutputBufferLines fetchAll(Connection conn) throws SQLException {
5557
CallableStatement callableStatement = null;
5658
ResultSet resultSet = null;
5759
try {
58-
callableStatement = UTPLSQL.getConnection()
59-
.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
60+
callableStatement = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
6061

6162
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
6263
callableStatement.setString(2, getReporterId());
6364
callableStatement.execute();
6465

6566
resultSet = (ResultSet) callableStatement.getObject(1);
6667

67-
List<String> outputLines = new ArrayList<>();
68+
OutputBufferLines outputLines = new OutputBufferLines();
69+
outputLines.setFinished(true);
6870
while (resultSet.next()) {
6971
outputLines.add(resultSet.getString("text"));
7072
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.utplsql.api;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by Vinicius on 22/04/2017.
8+
*/
9+
public class OutputBufferLines {
10+
11+
private List<String> bufferLines;
12+
private boolean isFinished;
13+
14+
public OutputBufferLines() {
15+
this.bufferLines = new ArrayList<>();
16+
this.isFinished = false;
17+
}
18+
19+
public List<String> getLines() {
20+
return this.bufferLines;
21+
}
22+
23+
public void add(String s) {
24+
this.bufferLines.add(s);
25+
}
26+
27+
public boolean isFinished() {
28+
return this.isFinished;
29+
}
30+
31+
public void setFinished(boolean isFinished) {
32+
this.isFinished = isFinished;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
StringBuilder sb = new StringBuilder(this.bufferLines.size());
38+
39+
for (String bufferLine : this.bufferLines) {
40+
sb.append(bufferLine);
41+
sb.append("\n");
42+
}
43+
44+
return sb.toString();
45+
}
46+
}

src/main/java/io/github/utplsql/TestRunner.java renamed to src/main/java/io/github/utplsql/api/TestRunner.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package io.github.utplsql;
1+
package io.github.utplsql.api;
22

3-
import io.github.utplsql.types.BaseReporter;
3+
import io.github.utplsql.api.types.BaseReporter;
44

55
import java.sql.CallableStatement;
6+
import java.sql.Connection;
67
import java.sql.SQLException;
78

89
/**
@@ -12,27 +13,25 @@ public class TestRunner {
1213

1314
public TestRunner() {}
1415

15-
public void run() throws SQLException {
16+
public void run(Connection conn) throws SQLException {
1617
CallableStatement callableStatement = null;
1718
try {
18-
callableStatement = UTPLSQL.getConnection()
19-
.prepareCall("BEGIN ut_runner.run(); END;");
19+
callableStatement = conn.prepareCall("BEGIN ut_runner.run(); END;");
2020
callableStatement.execute();
2121
} finally {
2222
if (callableStatement != null)
2323
callableStatement.close();
2424
}
2525
}
2626

27-
public void run(String path, BaseReporter reporter) throws SQLException {
27+
public void run(Connection conn, String path, BaseReporter reporter) throws SQLException {
2828
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty()) {
29-
reporter.setReporterId(UTPLSQL.newSysGuid());
29+
reporter.setReporterId(utPLSQL.newSysGuid(conn));
3030
}
3131

3232
CallableStatement callableStatement = null;
3333
try {
34-
callableStatement = UTPLSQL.getConnection()
35-
.prepareCall("BEGIN ut_runner.run(:path, :reporter); END;");
34+
callableStatement = conn.prepareCall("BEGIN ut_runner.run(:path, :reporter); END;");
3635
callableStatement.setString(":path", path);
3736
callableStatement.setObject(":reporter", reporter);
3837
callableStatement.execute();

src/main/java/io/github/utplsql/types/BaseReporter.java renamed to src/main/java/io/github/utplsql/api/types/BaseReporter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
package io.github.utplsql.types;
1+
package io.github.utplsql.api.types;
22

3-
import java.sql.SQLData;
4-
import java.sql.SQLException;
5-
import java.sql.SQLInput;
6-
import java.sql.SQLOutput;
3+
import java.sql.*;
74
import java.util.Calendar;
85

96
/**
@@ -16,7 +13,7 @@ public abstract class BaseReporter implements SQLData {
1613
private java.sql.Date startDate;
1714

1815
public BaseReporter() {
19-
startDate = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
16+
this.startDate = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
2017
}
2118

2219
public String getSelfType() {

src/main/java/io/github/utplsql/types/CustomTypes.java renamed to src/main/java/io/github/utplsql/api/types/CustomTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.utplsql.types;
1+
package io.github.utplsql.api.types;
22

33
/**
44
* Created by Vinicius on 13/04/2017.

src/main/java/io/github/utplsql/types/DocumentationReporter.java renamed to src/main/java/io/github/utplsql/api/types/DocumentationReporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.utplsql.types;
1+
package io.github.utplsql.api.types;
22

33
import java.sql.SQLException;
44

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.utplsql.api;
2+
3+
import io.github.utplsql.api.types.CustomTypes;
4+
import io.github.utplsql.api.types.DocumentationReporter;
5+
import oracle.jdbc.OracleTypes;
6+
7+
import java.sql.CallableStatement;
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.SQLException;
11+
import java.util.Map;
12+
13+
/**
14+
* Created by Vinicius Avellar on 12/04/2017.
15+
*/
16+
public final class utPLSQL {
17+
18+
private static String sUrl;
19+
private static String sUser;
20+
private static String sPassword;
21+
22+
private utPLSQL() {}
23+
24+
public static void init(String url, String user, String password) throws SQLException {
25+
sUrl = url;
26+
sUser = user;
27+
sPassword = password;
28+
}
29+
30+
public static Connection getConnection() throws SQLException {
31+
Connection conn = DriverManager.getConnection(sUrl, sUser, sPassword);
32+
createTypeMap(conn);
33+
return conn;
34+
}
35+
36+
public static String newSysGuid(Connection conn) throws SQLException {
37+
CallableStatement callableStatement = null;
38+
try {
39+
callableStatement = conn.prepareCall("BEGIN :id := sys_guid(); END;");
40+
callableStatement.registerOutParameter(":id", OracleTypes.RAW);
41+
callableStatement.executeUpdate();
42+
return callableStatement.getString(":id");
43+
} finally {
44+
if (callableStatement != null)
45+
callableStatement.close();
46+
}
47+
}
48+
49+
private static void createTypeMap(Connection conn) throws SQLException {
50+
Map typeMap = conn.getTypeMap();
51+
typeMap.put(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), DocumentationReporter.class);
52+
conn.setTypeMap(typeMap);
53+
}
54+
55+
}

src/test/java/io/github/utplsql/OutputBufferTest.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)