11package com .clickhouse .jdbc ;
22
33import com .clickhouse .client .api .metadata .TableSchema ;
4+ import com .clickhouse .client .api .query .QuerySettings ;
45import com .clickhouse .data .Tuple ;
56import com .clickhouse .jdbc .internal .ExceptionUtils ;
67import com .clickhouse .jdbc .internal .JdbcUtils ;
@@ -113,13 +114,14 @@ private String compileSql(String []segments) {
113114 @ Override
114115 public ResultSet executeQuery () throws SQLException {
115116 checkClosed ();
116- return executeQuery (compileSql (sqlSegments ));
117+ return super . executeQueryImpl (compileSql (sqlSegments ), new QuerySettings (). setDatabase ( connection . getSchema () ));
117118 }
118119
119120 @ Override
120121 public int executeUpdate () throws SQLException {
121122 checkClosed ();
122- return executeUpdate (compileSql (sqlSegments ));
123+ return super .executeUpdateImpl (compileSql (sqlSegments ), statementType ,
124+ new QuerySettings ().setDatabase (connection .getSchema ()));
123125 }
124126
125127 @ Override
@@ -246,17 +248,18 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
246248 @ Override
247249 public boolean execute () throws SQLException {
248250 checkClosed ();
249- return execute (compileSql (sqlSegments ));
251+ return super .executeImpl (compileSql (sqlSegments ), statementType ,
252+ new QuerySettings ().setDatabase (connection .getSchema ()));
250253 }
251254
252255 @ Override
253256 public void addBatch () throws SQLException {
254257 checkClosed ();
255258 if (statementType == StatementParser .StatementType .INSERT ) {
256259 // adding values to the end of big INSERT statement.
257- addBatch (compileSql (valueSegments ));
260+ super . addBatch (compileSql (valueSegments ));
258261 } else {
259- addBatch (compileSql (sqlSegments ));
262+ super . addBatch (compileSql (sqlSegments ));
260263 }
261264 }
262265
@@ -271,7 +274,8 @@ public int[] executeBatch() throws SQLException {
271274 sb .append (sql ).append ("," );
272275 }
273276 sb .setCharAt (sb .length () - 1 , ';' );
274- int rowsInserted = executeUpdate (sb .toString ());
277+ int rowsInserted = executeUpdateImpl (sb .toString (), statementType ,
278+ new QuerySettings ().setDatabase (connection .getSchema ()));
275279 // clear batch and re-add insert into
276280 int [] results = new int [batch .size ()];
277281 if (rowsInserted == batch .size ()) {
@@ -286,18 +290,22 @@ public int[] executeBatch() throws SQLException {
286290 return results ;
287291 } else {
288292 // run executeBatch
289- return super . executeBatch ();
293+ return executeBatchImpl (). stream (). mapToInt ( Integer :: intValue ). toArray ();
290294 }
291295 }
292296
293297 @ Override
294298 public long [] executeLargeBatch () throws SQLException {
295- int [] results = executeBatch ();
296- long [] longResults = new long [results .length ];
297- for (int i = 0 ; i < results .length ; i ++) {
298- longResults [i ] = results [i ];
299+ return executeBatchImpl ().stream ().mapToLong (Integer ::longValue ).toArray ();
300+ }
301+
302+ private List <Integer > executeBatchImpl () throws SQLException {
303+ List <Integer > results = new ArrayList <>();
304+ QuerySettings settings = new QuerySettings ().setDatabase (connection .getSchema ());
305+ for (String sql : batch ) {
306+ results .add (executeUpdateImpl (sql , statementType , settings ));
299307 }
300- return longResults ;
308+ return results ;
301309 }
302310
303311 @ Override
@@ -434,7 +442,8 @@ public ParameterMetaData getParameterMetaData() throws SQLException {
434442 @ Override
435443 public void setRowId (int parameterIndex , RowId x ) throws SQLException {
436444 checkClosed ();
437- parameters [parameterIndex - 1 ] = encodeObject (x );
445+ throw new SQLException ("ROWID type is not supported by ClickHouse." ,
446+ ExceptionUtils .SQL_STATE_FEATURE_NOT_SUPPORTED );
438447 }
439448
440449 @ Override
@@ -562,6 +571,118 @@ public long executeLargeUpdate() throws SQLException {
562571 return executeUpdate ();
563572 }
564573
574+ @ Override
575+ public final void addBatch (String sql ) throws SQLException {
576+ checkClosed ();
577+ throw new SQLException (
578+ "addBatch(String) cannot be called in PreparedStatement or CallableStatement!" ,
579+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
580+ }
581+
582+ @ Override
583+ public final boolean execute (String sql ) throws SQLException {
584+ checkClosed ();
585+ throw new SQLException (
586+ "execute(String) cannot be called in PreparedStatement or CallableStatement!" ,
587+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
588+ }
589+
590+ @ Override
591+ public final boolean execute (String sql , int autoGeneratedKeys ) throws SQLException {
592+ checkClosed ();
593+ throw new SQLException (
594+ "execute(String, int) cannot be called in PreparedStatement or CallableStatement!" ,
595+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
596+ }
597+
598+ @ Override
599+ public final boolean execute (String sql , int [] columnIndexes ) throws SQLException {
600+ checkClosed ();
601+ throw new SQLException (
602+ "execute(String, int[]) cannot be called in PreparedStatement or CallableStatement!" ,
603+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
604+ }
605+
606+ @ Override
607+ public final boolean execute (String sql , String [] columnNames ) throws SQLException {
608+ checkClosed ();
609+ throw new SQLException (
610+ "execute(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
611+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
612+ }
613+
614+ @ Override
615+ public final long executeLargeUpdate (String sql ) throws SQLException {
616+ checkClosed ();
617+ throw new SQLException (
618+ "executeLargeUpdate(String) cannot be called in PreparedStatement or CallableStatement!" ,
619+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
620+ }
621+
622+ @ Override
623+ public final long executeLargeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
624+ checkClosed ();
625+ throw new SQLException (
626+ "executeLargeUpdate(String, int) cannot be called in PreparedStatement or CallableStatement!" ,
627+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
628+ }
629+
630+ @ Override
631+ public final long executeLargeUpdate (String sql , int [] columnIndexes ) throws SQLException {
632+ checkClosed ();
633+ throw new SQLException (
634+ "executeLargeUpdate(String, int[]) cannot be called in PreparedStatement or CallableStatement!" ,
635+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
636+ }
637+
638+ @ Override
639+ public final long executeLargeUpdate (String sql , String [] columnNames ) throws SQLException {
640+ checkClosed ();
641+ throw new SQLException (
642+ "executeLargeUpdate(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
643+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
644+ }
645+
646+ @ Override
647+ public final ResultSet executeQuery (String sql ) throws SQLException {
648+ checkClosed ();
649+ throw new SQLException (
650+ "executeQuery(String) cannot be called in PreparedStatement or CallableStatement!" ,
651+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
652+ }
653+
654+ @ Override
655+ public final int executeUpdate (String sql ) throws SQLException {
656+ checkClosed ();
657+ throw new SQLException (
658+ "executeUpdate(String) cannot be called in PreparedStatement or CallableStatement!" ,
659+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
660+ }
661+
662+ @ Override
663+ public final int executeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
664+ checkClosed ();
665+ throw new SQLException (
666+ "executeUpdate(String, int) cannot be called in PreparedStatement or CallableStatement!" ,
667+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
668+ }
669+
670+ @ Override
671+ public final int executeUpdate (String sql , int [] columnIndexes ) throws SQLException {
672+ checkClosed ();
673+ throw new SQLException (
674+ "executeUpdate(String, int[]) cannot be called in PreparedStatement or CallableStatement!" ,
675+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
676+ }
677+
678+ @ Override
679+ public final int executeUpdate (String sql , String [] columnNames ) throws SQLException {
680+ checkClosed ();
681+ throw new SQLException (
682+ "executeUpdate(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
683+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
684+ }
685+
565686 private static String encodeObject (Object x ) throws SQLException {
566687 LOG .trace ("Encoding object: {}" , x );
567688
@@ -612,7 +733,9 @@ private static String encodeObject(Object x) throws SQLException {
612733 for (Object item : (Collection <?>) x ) {
613734 listString .append (encodeObject (item )).append (", " );
614735 }
615- listString .delete (listString .length () - 2 , listString .length ());
736+ if (listString .length () > 1 ) {
737+ listString .delete (listString .length () - 2 , listString .length ());
738+ }
616739 listString .append ("]" );
617740
618741 return listString .toString ();
0 commit comments