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 ;
@@ -101,13 +102,14 @@ private String compileSql(String []segments) {
101102 @ Override
102103 public ResultSet executeQuery () throws SQLException {
103104 checkClosed ();
104- return executeQuery (compileSql (sqlSegments ));
105+ return super . executeQueryImpl (compileSql (sqlSegments ), new QuerySettings (). setDatabase ( connection . getSchema () ));
105106 }
106107
107108 @ Override
108109 public int executeUpdate () throws SQLException {
109110 checkClosed ();
110- return executeUpdate (compileSql (sqlSegments ));
111+ return super .executeUpdateImpl (compileSql (sqlSegments ), statementType ,
112+ new QuerySettings ().setDatabase (connection .getSchema ()));
111113 }
112114
113115 @ Override
@@ -234,17 +236,18 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
234236 @ Override
235237 public boolean execute () throws SQLException {
236238 checkClosed ();
237- return execute (compileSql (sqlSegments ));
239+ return super .executeImpl (compileSql (sqlSegments ), statementType ,
240+ new QuerySettings ().setDatabase (connection .getSchema ()));
238241 }
239242
240243 @ Override
241244 public void addBatch () throws SQLException {
242245 checkClosed ();
243246 if (statementType == StatementType .INSERT ) {
244247 // adding values to the end of big INSERT statement.
245- addBatch (compileSql (valueSegments ));
248+ super . addBatch (compileSql (valueSegments ));
246249 } else {
247- addBatch (compileSql (sqlSegments ));
250+ super . addBatch (compileSql (sqlSegments ));
248251 }
249252 }
250253
@@ -259,7 +262,8 @@ public int[] executeBatch() throws SQLException {
259262 sb .append (sql ).append ("," );
260263 }
261264 sb .setCharAt (sb .length () - 1 , ';' );
262- int rowsInserted = executeUpdate (sb .toString ());
265+ int rowsInserted = executeUpdateImpl (sb .toString (), statementType ,
266+ new QuerySettings ().setDatabase (connection .getSchema ()));
263267 // clear batch and re-add insert into
264268 int [] results = new int [batch .size ()];
265269 if (rowsInserted == batch .size ()) {
@@ -274,18 +278,22 @@ public int[] executeBatch() throws SQLException {
274278 return results ;
275279 } else {
276280 // run executeBatch
277- return super . executeBatch ();
281+ return executeBatchImpl (). stream (). mapToInt ( Integer :: intValue ). toArray ();
278282 }
279283 }
280284
281285 @ Override
282286 public long [] executeLargeBatch () throws SQLException {
283- int [] results = executeBatch ();
284- long [] longResults = new long [results .length ];
285- for (int i = 0 ; i < results .length ; i ++) {
286- longResults [i ] = results [i ];
287+ return executeBatchImpl ().stream ().mapToLong (Integer ::longValue ).toArray ();
288+ }
289+
290+ private List <Integer > executeBatchImpl () throws SQLException {
291+ List <Integer > results = new ArrayList <>();
292+ QuerySettings settings = new QuerySettings ().setDatabase (connection .getSchema ());
293+ for (String sql : batch ) {
294+ results .add (executeUpdateImpl (sql , statementType , settings ));
287295 }
288- return longResults ;
296+ return results ;
289297 }
290298
291299 @ Override
@@ -412,7 +420,8 @@ public ParameterMetaData getParameterMetaData() throws SQLException {
412420 @ Override
413421 public void setRowId (int parameterIndex , RowId x ) throws SQLException {
414422 checkClosed ();
415- parameters [parameterIndex - 1 ] = encodeObject (x );
423+ throw new SQLException ("ROWID type is not supported by ClickHouse." ,
424+ ExceptionUtils .SQL_STATE_FEATURE_NOT_SUPPORTED );
416425 }
417426
418427 @ Override
@@ -540,6 +549,118 @@ public long executeLargeUpdate() throws SQLException {
540549 return executeUpdate ();
541550 }
542551
552+ @ Override
553+ public final void addBatch (String sql ) throws SQLException {
554+ checkClosed ();
555+ throw new SQLException (
556+ "addBatch(String) cannot be called in PreparedStatement or CallableStatement!" ,
557+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
558+ }
559+
560+ @ Override
561+ public final boolean execute (String sql ) throws SQLException {
562+ checkClosed ();
563+ throw new SQLException (
564+ "execute(String) cannot be called in PreparedStatement or CallableStatement!" ,
565+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
566+ }
567+
568+ @ Override
569+ public final boolean execute (String sql , int autoGeneratedKeys ) throws SQLException {
570+ checkClosed ();
571+ throw new SQLException (
572+ "execute(String, int) cannot be called in PreparedStatement or CallableStatement!" ,
573+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
574+ }
575+
576+ @ Override
577+ public final boolean execute (String sql , int [] columnIndexes ) throws SQLException {
578+ checkClosed ();
579+ throw new SQLException (
580+ "execute(String, int[]) cannot be called in PreparedStatement or CallableStatement!" ,
581+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
582+ }
583+
584+ @ Override
585+ public final boolean execute (String sql , String [] columnNames ) throws SQLException {
586+ checkClosed ();
587+ throw new SQLException (
588+ "execute(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
589+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
590+ }
591+
592+ @ Override
593+ public final long executeLargeUpdate (String sql ) throws SQLException {
594+ checkClosed ();
595+ throw new SQLException (
596+ "executeLargeUpdate(String) cannot be called in PreparedStatement or CallableStatement!" ,
597+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
598+ }
599+
600+ @ Override
601+ public final long executeLargeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
602+ checkClosed ();
603+ throw new SQLException (
604+ "executeLargeUpdate(String, int) cannot be called in PreparedStatement or CallableStatement!" ,
605+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
606+ }
607+
608+ @ Override
609+ public final long executeLargeUpdate (String sql , int [] columnIndexes ) throws SQLException {
610+ checkClosed ();
611+ throw new SQLException (
612+ "executeLargeUpdate(String, int[]) cannot be called in PreparedStatement or CallableStatement!" ,
613+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
614+ }
615+
616+ @ Override
617+ public final long executeLargeUpdate (String sql , String [] columnNames ) throws SQLException {
618+ checkClosed ();
619+ throw new SQLException (
620+ "executeLargeUpdate(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
621+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
622+ }
623+
624+ @ Override
625+ public final ResultSet executeQuery (String sql ) throws SQLException {
626+ checkClosed ();
627+ throw new SQLException (
628+ "executeQuery(String) cannot be called in PreparedStatement or CallableStatement!" ,
629+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
630+ }
631+
632+ @ Override
633+ public final int executeUpdate (String sql ) throws SQLException {
634+ checkClosed ();
635+ throw new SQLException (
636+ "executeUpdate(String) cannot be called in PreparedStatement or CallableStatement!" ,
637+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
638+ }
639+
640+ @ Override
641+ public final int executeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
642+ checkClosed ();
643+ throw new SQLException (
644+ "executeUpdate(String, int) cannot be called in PreparedStatement or CallableStatement!" ,
645+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
646+ }
647+
648+ @ Override
649+ public final int executeUpdate (String sql , int [] columnIndexes ) throws SQLException {
650+ checkClosed ();
651+ throw new SQLException (
652+ "executeUpdate(String, int[]) cannot be called in PreparedStatement or CallableStatement!" ,
653+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
654+ }
655+
656+ @ Override
657+ public final int executeUpdate (String sql , String [] columnNames ) throws SQLException {
658+ checkClosed ();
659+ throw new SQLException (
660+ "executeUpdate(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
661+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
662+ }
663+
543664 private static String encodeObject (Object x ) throws SQLException {
544665 LOG .trace ("Encoding object: {}" , x );
545666
@@ -590,7 +711,9 @@ private static String encodeObject(Object x) throws SQLException {
590711 for (Object item : (Collection <?>) x ) {
591712 listString .append (encodeObject (item )).append (", " );
592713 }
593- listString .delete (listString .length () - 2 , listString .length ());
714+ if (listString .length () > 1 ) {
715+ listString .delete (listString .length () - 2 , listString .length ());
716+ }
594717 listString .append ("]" );
595718
596719 return listString .toString ();
0 commit comments