Skip to content

Commit 8ca4248

Browse files
committed
Api development startup
1 parent 1e25bc5 commit 8ca4248

File tree

12 files changed

+369
-48
lines changed

12 files changed

+369
-48
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
1517
</properties>
1618

1719
<dependencies>
@@ -28,6 +30,24 @@
2830
</dependency>
2931
</dependencies>
3032

33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>net.orfjackal.retrolambda</groupId>
37+
<artifactId>retrolambda-maven-plugin</artifactId>
38+
<version>2.5.1</version>
39+
<executions>
40+
<execution>
41+
<goals>
42+
<goal>process-main</goal>
43+
<goal>process-test</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
3151
<repositories>
3252
<repository>
3353
<id>maven.oracle.com</id>

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

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.utplsql;
2+
3+
import oracle.jdbc.OracleTypes;
4+
5+
import java.sql.CallableStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Created by Vinicius on 13/04/2017.
13+
*/
14+
public final class OutputBuffer {
15+
16+
private OutputBuffer() {}
17+
18+
public static List<String> getAllLines(String reporterId) throws SQLException {
19+
CallableStatement callableStatement = null;
20+
ResultSet resultSet = null;
21+
try {
22+
callableStatement = UTPLSQL.getConnection()
23+
.prepareCall("BEGIN :lines := ut_output_buffer.get_lines_cursor(:reporter_id); END;");
24+
25+
callableStatement.registerOutParameter(":lines", OracleTypes.CURSOR);
26+
callableStatement.setString(":reporter_id", reporterId);
27+
callableStatement.execute();
28+
29+
resultSet = (ResultSet) callableStatement.getObject(":lines");
30+
31+
List<String> outputLines = new ArrayList<>();
32+
while (resultSet.next()) {
33+
outputLines.add(resultSet.getString(0));
34+
}
35+
return outputLines;
36+
} finally {
37+
if (resultSet != null)
38+
resultSet.close();
39+
if (callableStatement != null)
40+
callableStatement.close();
41+
}
42+
}
43+
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.utplsql;
2+
3+
import io.github.utplsql.types.BaseReporter;
4+
5+
import java.sql.CallableStatement;
6+
import java.sql.SQLException;
7+
8+
/**
9+
* Created by Vinicius Avellar on 12/04/2017.
10+
*/
11+
public final class TestRunner {
12+
13+
private TestRunner() {}
14+
15+
public static void run() throws SQLException {
16+
CallableStatement callableStatement = null;
17+
try {
18+
callableStatement = UTPLSQL.getConnection()
19+
.prepareCall("BEGIN ut.run(); END;");
20+
callableStatement.execute();
21+
} finally {
22+
if (callableStatement != null)
23+
callableStatement.close();
24+
}
25+
}
26+
27+
public static void run(String path, BaseReporter reporter) throws SQLException {
28+
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty()) {
29+
reporter.setReporterId(UTPLSQL.newSysGuid());
30+
}
31+
32+
CallableStatement callableStatement = null;
33+
try {
34+
callableStatement = UTPLSQL.getConnection()
35+
.prepareCall("BEGIN ut.run(:path, :reporter); END;");
36+
callableStatement.setString(":path", path);
37+
callableStatement.setObject(":reporter", reporter);
38+
callableStatement.execute();
39+
} finally {
40+
if (callableStatement != null)
41+
callableStatement.close();
42+
}
43+
}
44+
45+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.utplsql;
2+
3+
import io.github.utplsql.types.DocumentationReporter;
4+
import io.github.utplsql.types.CustomTypes;
5+
import oracle.jdbc.OracleTypes;
6+
7+
import java.sql.*;
8+
import java.util.Map;
9+
10+
/**
11+
* Created by Vinicius Avellar on 12/04/2017.
12+
*/
13+
public final class UTPLSQL {
14+
15+
private static Connection sConnection;
16+
17+
private UTPLSQL() {}
18+
19+
public static void init(String url, String user, String password) throws SQLException {
20+
UTPLSQL.init(DriverManager.getConnection(url, user, password));
21+
}
22+
23+
public static void init(Connection conn) throws SQLException {
24+
sConnection = conn;
25+
createTypeMap();
26+
}
27+
28+
public static void close() {
29+
if (sConnection != null)
30+
try { sConnection.close(); } catch (SQLException ignored) {}
31+
}
32+
33+
protected static Connection getConnection() {
34+
if (sConnection == null)
35+
throw new RuntimeException("Connection not initialized.");
36+
37+
return sConnection;
38+
}
39+
40+
public static String newSysGuid() throws SQLException {
41+
CallableStatement callableStatement = null;
42+
try {
43+
callableStatement = sConnection.prepareCall("BEGIN :id := sys_guid(); END;");
44+
callableStatement.registerOutParameter(":id", OracleTypes.RAW);
45+
callableStatement.executeUpdate();
46+
return callableStatement.getString(":id");
47+
} finally {
48+
if (callableStatement != null)
49+
callableStatement.close();
50+
}
51+
}
52+
53+
private static void createTypeMap() throws SQLException {
54+
Map typeMap = sConnection.getTypeMap();
55+
typeMap.put(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), DocumentationReporter.class);
56+
sConnection.setTypeMap(typeMap);
57+
}
58+
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.utplsql.types;
2+
3+
import java.sql.SQLData;
4+
import java.sql.SQLException;
5+
import java.sql.SQLInput;
6+
import java.sql.SQLOutput;
7+
import java.util.Calendar;
8+
9+
/**
10+
* Created by Vinicius on 13/04/2017.
11+
*/
12+
public abstract class BaseReporter implements SQLData {
13+
14+
private String selfType;
15+
private String reporterId;
16+
private java.sql.Date startDate;
17+
18+
public BaseReporter() {
19+
startDate = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
20+
}
21+
22+
public String getSelfType() {
23+
return this.selfType;
24+
}
25+
26+
public void setSelfType(String selfType) {
27+
this.selfType = selfType;
28+
}
29+
30+
public String getReporterId() {
31+
return this.reporterId;
32+
}
33+
34+
public void setReporterId(String reporterId) {
35+
this.reporterId = reporterId;
36+
}
37+
38+
public java.sql.Date getStartDate() {
39+
return this.startDate;
40+
}
41+
42+
public void setStartDate(java.sql.Date startDate) {
43+
this.startDate = startDate;
44+
}
45+
46+
@Override
47+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
48+
setSelfType(stream.readString());
49+
setReporterId(stream.readString());
50+
setStartDate(stream.readDate());
51+
}
52+
53+
@Override
54+
public void writeSQL(SQLOutput stream) throws SQLException {
55+
stream.writeString(getSelfType());
56+
stream.writeString(getReporterId());
57+
stream.writeDate(getStartDate());
58+
}
59+
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.utplsql.types;
2+
3+
/**
4+
* Created by Vinicius on 13/04/2017.
5+
* utPLSQL custom data types.
6+
*/
7+
public enum CustomTypes {
8+
// Object names must be upper case.
9+
UT_DOCUMENTATION_REPORTER("UT_DOCUMENTATION_REPORTER"),
10+
UT_VARCHAF2_LIST("UT_VARCHAF2_LIST");
11+
12+
private String typeName;
13+
14+
CustomTypes(String typeName) {
15+
this.typeName = typeName;
16+
}
17+
18+
public String getName() {
19+
return this.typeName;
20+
}
21+
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.utplsql.types;
2+
3+
import java.sql.SQLException;
4+
5+
/**
6+
* Created by Vinicius on 13/04/2017.
7+
*/
8+
public class DocumentationReporter extends BaseReporter {
9+
10+
@Override
11+
public String getSQLTypeName() throws SQLException {
12+
return CustomTypes.UT_DOCUMENTATION_REPORTER.getName();
13+
}
14+
15+
}

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

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.utplsql;
2+
3+
import io.github.utplsql.rules.DatabaseRule;
4+
import io.github.utplsql.types.BaseReporter;
5+
import io.github.utplsql.types.DocumentationReporter;
6+
import org.junit.Assert;
7+
import org.junit.ClassRule;
8+
import org.junit.Test;
9+
10+
import java.sql.Connection;
11+
import java.sql.SQLException;
12+
import java.util.List;
13+
14+
/**
15+
* Created by Vinicius on 13/04/2017.
16+
*/
17+
public class OutputBufferTest {
18+
19+
@ClassRule
20+
public static final DatabaseRule sInitialization = new DatabaseRule();
21+
22+
@Test
23+
public void getLinesFromOutputBuffer() {
24+
try {
25+
Connection conn = UTPLSQL.getConnection();
26+
conn.setAutoCommit(false);
27+
28+
BaseReporter reporter = new DocumentationReporter();
29+
reporter.setReporterId(UTPLSQL.newSysGuid());
30+
TestRunner.run("", reporter);
31+
32+
List<String> outputLines = OutputBuffer.getAllLines(reporter.getReporterId());
33+
for (int i = 0; i < outputLines.size(); i++) {
34+
System.out.println(outputLines.get(0));
35+
}
36+
37+
conn.commit();
38+
} catch (SQLException e) {
39+
Assert.fail(e.getMessage());
40+
}
41+
}
42+
43+
}

0 commit comments

Comments
 (0)