2525import org .apache .arrow .util .Preconditions ;
2626import org .apache .arrow .vector .types .pojo .Schema ;
2727import org .apache .calcite .avatica .AvaticaPreparedStatement ;
28+ import org .apache .calcite .avatica .AvaticaStatement ;
2829import org .apache .calcite .avatica .Meta .ExecuteBatchResult ;
2930import org .apache .calcite .avatica .Meta .ExecuteResult ;
3031import org .apache .calcite .avatica .Meta .MetaResultSet ;
@@ -44,39 +45,19 @@ private ArrowFlightPreparedStatement(
4445 final ArrowFlightConnection connection ,
4546 final StatementHandle handle ,
4647 final Signature signature ,
48+ final ArrowFlightSqlClientHandler .PreparedStatement preparedStatement ,
4749 final int resultSetType ,
4850 final int resultSetConcurrency ,
4951 final int resultSetHoldability )
5052 throws SQLException {
5153 super (connection , handle , signature , resultSetType , resultSetConcurrency , resultSetHoldability );
54+ this .preparedStatement = Preconditions .checkNotNull (preparedStatement );
55+ this .handle .signature = signature ;
56+ setSignature (signature );
5257 }
5358
54- static ArrowFlightPreparedStatement createPrepared (
55- final ArrowFlightConnection connection ,
56- final StatementHandle statementHandle ,
57- final String query ,
58- final int resultSetType ,
59- final int resultSetConcurrency ,
60- final int resultSetHoldability )
61- throws SQLException {
62- final ArrowFlightSqlClientHandler .PreparedStatement preparedStatement =
63- connection .getClientHandler ().prepare (query );
64- final Signature signature =
65- ArrowFlightMetaImpl .buildSignature (
66- query , preparedStatement .getDataSetSchema (), preparedStatement .getParameterSchema ());
67- statementHandle .signature = signature ;
68-
69- final ArrowFlightPreparedStatement statement =
70- new ArrowFlightPreparedStatement (
71- connection ,
72- statementHandle ,
73- signature ,
74- resultSetType ,
75- resultSetConcurrency ,
76- resultSetHoldability );
77- statement .preparedStatement = Preconditions .checkNotNull (preparedStatement );
78- statement .setSignature (signature );
79- return statement ;
59+ static Builder builder (final ArrowFlightConnection connection ) {
60+ return new Builder (connection );
8061 }
8162
8263 @ Override
@@ -173,4 +154,79 @@ private void ensurePrepared() {
173154 throw new IllegalStateException ("PreparedStatement is already closed." );
174155 }
175156 }
157+
158+ static final class Builder {
159+ private final ArrowFlightConnection connection ;
160+ private StatementHandle handle ;
161+ private String query ;
162+ private Integer resultSetType ;
163+ private Integer resultSetConcurrency ;
164+ private Integer resultSetHoldability ;
165+ private boolean generateHandle ;
166+
167+ private Builder (final ArrowFlightConnection connection ) {
168+ this .connection = Preconditions .checkNotNull (connection );
169+ }
170+
171+ Builder withQuery (final String query ) {
172+ this .query = Preconditions .checkNotNull (query );
173+ return this ;
174+ }
175+
176+ Builder withGeneratedHandle () {
177+ this .generateHandle = true ;
178+ this .handle = null ;
179+ return this ;
180+ }
181+
182+ Builder withExistingStatement (final AvaticaStatement statement ) throws SQLException {
183+ Preconditions .checkNotNull (statement );
184+ this .generateHandle = false ;
185+ this .handle = Preconditions .checkNotNull (statement .handle );
186+ this .resultSetType = statement .getResultSetType ();
187+ this .resultSetConcurrency = statement .getResultSetConcurrency ();
188+ this .resultSetHoldability = statement .getResultSetHoldability ();
189+ return this ;
190+ }
191+
192+ Builder withResultSetType (final int resultSetType ) {
193+ this .resultSetType = resultSetType ;
194+ return this ;
195+ }
196+
197+ Builder withResultSetConcurrency (final int resultSetConcurrency ) {
198+ this .resultSetConcurrency = resultSetConcurrency ;
199+ return this ;
200+ }
201+
202+ Builder withResultSetHoldability (final int resultSetHoldability ) {
203+ this .resultSetHoldability = resultSetHoldability ;
204+ return this ;
205+ }
206+
207+ ArrowFlightPreparedStatement build () throws SQLException {
208+ Preconditions .checkNotNull (query );
209+ Preconditions .checkNotNull (resultSetType );
210+ Preconditions .checkNotNull (resultSetConcurrency );
211+ Preconditions .checkNotNull (resultSetHoldability );
212+ if (!generateHandle && handle == null ) {
213+ throw new IllegalStateException ("PreparedStatement builder requires a handle." );
214+ }
215+
216+ final ArrowFlightSqlClientHandler .PreparedStatement preparedStatement =
217+ connection .getClientHandler ().prepare (query );
218+ final Signature signature =
219+ ArrowFlightMetaImpl .buildSignature (
220+ query , preparedStatement .getDataSetSchema (), preparedStatement .getParameterSchema ());
221+
222+ return new ArrowFlightPreparedStatement (
223+ connection ,
224+ generateHandle ? null : handle ,
225+ signature ,
226+ preparedStatement ,
227+ resultSetType ,
228+ resultSetConcurrency ,
229+ resultSetHoldability );
230+ }
231+ }
176232}
0 commit comments