77import oracle .sql .ORAData ;
88import oracle .sql .STRUCT ;
99import oracle .sql .StructDescriptor ;
10- import org .utplsql .api .DBHelper ;
1110
1211import java .sql .*;
13- import java .util .Calendar ;
1412
15- /**
16- * Created by Vinicius on 13/04/2017.
13+ /** This is a basic Reporter implementation, using ORAData interface
14+ *
15+ * @author pesse
1716 */
1817public class Reporter implements ORAData {
1918
20- protected String selfType ;
21- protected String id ;
22- protected Object [] attributes ;
19+ private String selfType ;
20+ private String id ;
21+ private Object [] attributes ;
22+ private boolean hasOutput = false ;
23+ private boolean init = false ;
2324
2425 public Reporter ( String typeName , Object [] attributes ) {
25- selfType = typeName ;
26-
27- if ( attributes != null ) {
28- this .id = String .valueOf (attributes [1 ]);
29- }
30- this .attributes = attributes ;
26+ setTypeName (typeName );
27+ setAttributes ( attributes );
3128 }
3229
3330 private void setTypeName ( String typeName ) {
@@ -42,19 +39,69 @@ public Reporter init( Connection con ) throws SQLException {
4239
4340 OracleConnection oraConn = con .unwrap (OracleConnection .class );
4441
42+ initDbReporter ( oraConn );
43+ initHasOutput ( oraConn );
44+
45+ init = true ;
46+
47+ return this ;
48+ }
49+
50+ /** Initializes the Reporter from database
51+ * This is necessary because we set up OutputBuffer (and maybe other stuff) we don't want to know and care about
52+ * in the java API. Let's just do the instantiation of the Reporter in the database and map it into this object.
53+ *
54+ * @param oraConn
55+ * @throws SQLException
56+ */
57+ private void initDbReporter ( OracleConnection oraConn ) throws SQLException {
4558 OracleCallableStatement callableStatement = (OracleCallableStatement ) oraConn .prepareCall ("{? = call " + selfType + "()}" );
4659 callableStatement .registerOutParameter (1 , OracleTypes .STRUCT , "UT_REPORTER_BASE" );
4760 callableStatement .execute ();
4861
4962 Reporter obj = (Reporter ) callableStatement .getORAData (1 , ReporterFactory .getInstance ());
5063
51- // TODO: Really override things
52- this .attributes = obj .attributes ;
64+ setAttributes (obj .getAttributes ());
65+ }
66+
67+ /** Checks whether the Reporter has an output or not
68+ *
69+ * @param oraConn
70+ * @throws SQLException
71+ */
72+ private void initHasOutput ( OracleConnection oraConn ) throws SQLException {
73+ OracleCallableStatement cstmt = (OracleCallableStatement )oraConn .prepareCall ("{? = call ?.has_output()}" );
74+
75+ cstmt .registerOutParameter (1 , OracleTypes .INTEGER );
76+ cstmt .setORAData (2 , this );
77+ cstmt .execute ();
78+
79+ Integer i = cstmt .getInt (1 );
80+ if ( i != null && i == 1 ) {
81+ hasOutput = true ;
82+ }
83+ else {
84+ hasOutput = false ;
85+ }
86+ }
5387
54- // Check whether we have output or not
88+ protected void setAttributes (Object [] attributes ) {
89+ if (attributes != null ) {
90+ this .id = String .valueOf (attributes [1 ]);
91+ }
92+ this .attributes = attributes ;
93+ }
5594
95+ protected Object [] getAttributes () {
96+ return attributes ;
97+ }
5698
57- return this ;
99+ public boolean hasOutput () {
100+ return hasOutput ;
101+ }
102+
103+ public boolean isInit () {
104+ return init ;
58105 }
59106
60107 public String getTypeName () {
@@ -69,8 +116,8 @@ public String getId() {
69116 public Datum toDatum (Connection c ) throws SQLException
70117 {
71118 StructDescriptor sd =
72- StructDescriptor .createDescriptor (selfType , c );
73- return new STRUCT (sd , c , attributes );
119+ StructDescriptor .createDescriptor (getTypeName () , c );
120+ return new STRUCT (sd , c , getAttributes () );
74121 }
75122
76123}
0 commit comments