11package ru .yandex .clickhouse ;
22
3- import com .google .common .base .Strings ;
3+ import java .io .ByteArrayInputStream ;
4+ import java .io .IOException ;
5+ import java .io .InputStream ;
6+ import java .net .HttpURLConnection ;
7+ import java .net .URI ;
8+ import java .net .URISyntaxException ;
9+ import java .sql .ResultSet ;
10+ import java .sql .SQLException ;
11+ import java .sql .SQLWarning ;
12+ import java .util .ArrayList ;
13+ import java .util .EnumMap ;
14+ import java .util .List ;
15+ import java .util .Map ;
16+ import java .util .TimeZone ;
17+ import java .util .UUID ;
18+
419import org .apache .http .Header ;
520import org .apache .http .HttpEntity ;
621import org .apache .http .HttpResponse ;
722import org .apache .http .NameValuePair ;
823import org .apache .http .client .methods .HttpGet ;
924import org .apache .http .client .methods .HttpPost ;
25+ import org .apache .http .client .protocol .HttpClientContext ;
1026import org .apache .http .client .utils .URIBuilder ;
1127import org .apache .http .entity .ContentType ;
1228import org .apache .http .entity .StringEntity ;
1632import org .apache .http .util .EntityUtils ;
1733import org .slf4j .Logger ;
1834import org .slf4j .LoggerFactory ;
35+
36+ import com .google .common .base .Strings ;
37+
1938import ru .yandex .clickhouse .domain .ClickHouseFormat ;
2039import ru .yandex .clickhouse .except .ClickHouseException ;
2140import ru .yandex .clickhouse .except .ClickHouseExceptionSpecifier ;
22- import ru .yandex .clickhouse .response .*;
41+ import ru .yandex .clickhouse .response .ClickHouseLZ4Stream ;
42+ import ru .yandex .clickhouse .response .ClickHouseResponse ;
43+ import ru .yandex .clickhouse .response .ClickHouseResponseSummary ;
44+ import ru .yandex .clickhouse .response .ClickHouseResultSet ;
45+ import ru .yandex .clickhouse .response .ClickHouseScrollableResultSet ;
46+ import ru .yandex .clickhouse .response .FastByteArrayOutputStream ;
2347import ru .yandex .clickhouse .settings .ClickHouseProperties ;
2448import ru .yandex .clickhouse .settings .ClickHouseQueryParam ;
49+ import ru .yandex .clickhouse .util .ClickHouseHttpClientBuilder ;
2550import ru .yandex .clickhouse .util .ClickHouseRowBinaryInputStream ;
2651import ru .yandex .clickhouse .util .ClickHouseStreamCallback ;
2752import ru .yandex .clickhouse .util .Utils ;
2853import ru .yandex .clickhouse .util .guava .StreamUtils ;
2954
30- import java .io .ByteArrayInputStream ;
31- import java .io .IOException ;
32- import java .io .InputStream ;
33- import java .net .HttpURLConnection ;
34- import java .net .URI ;
35- import java .net .URISyntaxException ;
36- import java .sql .ResultSet ;
37- import java .sql .SQLException ;
38- import java .sql .SQLWarning ;
39- import java .util .*;
40-
41-
4255public class ClickHouseStatementImpl extends ConfigurableApi <ClickHouseStatement > implements ClickHouseStatement {
4356
4457 private static final Logger log = LoggerFactory .getLogger (ClickHouseStatementImpl .class );
4558
4659 private final CloseableHttpClient client ;
4760
61+ private final HttpClientContext httpContext ;
62+
4863 protected ClickHouseProperties properties ;
4964
5065 private ClickHouseConnection connection ;
@@ -79,10 +94,12 @@ public class ClickHouseStatementImpl extends ConfigurableApi<ClickHouseStatement
7994 private static final String [] selectKeywords = new String []{"SELECT" , "WITH" , "SHOW" , "DESC" , "EXISTS" , "EXPLAIN" };
8095 private static final String databaseKeyword = "CREATE DATABASE" ;
8196
97+
8298 public ClickHouseStatementImpl (CloseableHttpClient client , ClickHouseConnection connection ,
8399 ClickHouseProperties properties , int resultSetType ) {
84100 super (null );
85101 this .client = client ;
102+ this .httpContext = ClickHouseHttpClientBuilder .createClientContext (properties );
86103 this .connection = connection ;
87104 this .properties = properties == null ? new ClickHouseProperties () : properties ;
88105 this .initialDatabase = this .properties .getDatabase ();
@@ -112,9 +129,9 @@ public ResultSet executeQuery(String sql,
112129
113130 // forcibly disable extremes for ResultSet queries
114131 if (additionalDBParams == null || additionalDBParams .isEmpty ()) {
115- additionalDBParams = new EnumMap <ClickHouseQueryParam , String >(ClickHouseQueryParam .class );
132+ additionalDBParams = new EnumMap <>(ClickHouseQueryParam .class );
116133 } else {
117- additionalDBParams = new EnumMap <ClickHouseQueryParam , String >(additionalDBParams );
134+ additionalDBParams = new EnumMap <>(additionalDBParams );
118135 }
119136 additionalDBParams .put (ClickHouseQueryParam .EXTREMES , "0" );
120137
@@ -621,7 +638,7 @@ private InputStream getInputStream(
621638 HttpPost post = new HttpPost (uri );
622639 post .setEntity (requestEntity );
623640
624- HttpResponse response = client .execute (post );
641+ HttpResponse response = client .execute (post , httpContext );
625642 entity = response .getEntity ();
626643 checkForErrorAndThrow (entity , response );
627644
@@ -687,7 +704,7 @@ private List<NameValuePair> getUrlQueryParams(
687704 Map <String , String > additionalRequestParams ,
688705 boolean ignoreDatabase
689706 ) {
690- List <NameValuePair > result = new ArrayList <NameValuePair >();
707+ List <NameValuePair > result = new ArrayList <>();
691708
692709 if (sql != null ) {
693710 result .add (new BasicNameValuePair ("query" , sql ));
@@ -755,17 +772,21 @@ private boolean isQueryParamSet(ClickHouseQueryParam param, Map<ClickHouseQueryP
755772 }
756773
757774 private String getQueryParamValue (ClickHouseQueryParam param , Map <ClickHouseQueryParam , String > additionalClickHouseDBParams , Map <String , String > additionalRequestParams ) {
758- if (additionalRequestParams != null && additionalRequestParams .containsKey (param .getKey ()) && !Strings .isNullOrEmpty (additionalRequestParams .get (param .getKey ())))
775+ if (additionalRequestParams != null && additionalRequestParams .containsKey (param .getKey ()) && !Strings .isNullOrEmpty (additionalRequestParams .get (param .getKey ()))) {
759776 return additionalRequestParams .get (param .getKey ());
777+ }
760778
761- if (getRequestParams ().containsKey (param .getKey ()) && !Strings .isNullOrEmpty (getRequestParams ().get (param .getKey ())))
779+ if (getRequestParams ().containsKey (param .getKey ()) && !Strings .isNullOrEmpty (getRequestParams ().get (param .getKey ()))) {
762780 return getRequestParams ().get (param .getKey ());
781+ }
763782
764- if (additionalClickHouseDBParams != null && additionalClickHouseDBParams .containsKey (param ) && !Strings .isNullOrEmpty (additionalClickHouseDBParams .get (param )))
783+ if (additionalClickHouseDBParams != null && additionalClickHouseDBParams .containsKey (param ) && !Strings .isNullOrEmpty (additionalClickHouseDBParams .get (param ))) {
765784 return additionalClickHouseDBParams .get (param );
785+ }
766786
767- if (getAdditionalDBParams ().containsKey (param ) && !Strings .isNullOrEmpty (getAdditionalDBParams ().get (param )))
787+ if (getAdditionalDBParams ().containsKey (param ) && !Strings .isNullOrEmpty (getAdditionalDBParams ().get (param ))) {
768788 return getAdditionalDBParams ().get (param );
789+ }
769790
770791 return properties .asProperties ().getProperty (param .getKey ());
771792 }
@@ -775,7 +796,7 @@ private URI followRedirects(URI uri) throws IOException, URISyntaxException {
775796 int redirects = 0 ;
776797 while (redirects < properties .getMaxRedirects ()) {
777798 HttpGet httpGet = new HttpGet (uri );
778- HttpResponse response = client .execute (httpGet );
799+ HttpResponse response = client .execute (httpGet , httpContext );
779800 if (response .getStatusLine ().getStatusCode () == 307 ) {
780801 uri = new URI (response .getHeaders ("Location" )[0 ].getValue ());
781802 redirects ++;
@@ -896,7 +917,7 @@ void sendStream(Writer writer, HttpEntity content) throws ClickHouseException {
896917 httpPost .addHeader ("Content-Encoding" , writer .getCompression ().name ());
897918 }
898919 httpPost .setEntity (content );
899- HttpResponse response = client .execute (httpPost );
920+ HttpResponse response = client .execute (httpPost , httpContext );
900921 entity = response .getEntity ();
901922 checkForErrorAndThrow (entity , response );
902923
@@ -932,10 +953,12 @@ private void checkForErrorAndThrow(HttpEntity entity, HttpResponse response) thr
932953 }
933954 }
934955
956+ @ Override
935957 public void closeOnCompletion () throws SQLException {
936958 closeOnCompletion = true ;
937959 }
938960
961+ @ Override
939962 public boolean isCloseOnCompletion () throws SQLException {
940963 return closeOnCompletion ;
941964 }
0 commit comments