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 ;
@@ -107,13 +108,14 @@ private String compileSql(String []segments) {
107108 @ Override
108109 public ResultSet executeQuery () throws SQLException {
109110 checkClosed ();
110- return executeQuery (compileSql (sqlSegments ));
111+ return super . executeQueryImpl (compileSql (sqlSegments ), new QuerySettings (). setDatabase ( connection . getSchema () ));
111112 }
112113
113114 @ Override
114115 public int executeUpdate () throws SQLException {
115116 checkClosed ();
116- return executeUpdate (compileSql (sqlSegments ));
117+ return super .executeUpdateImpl (compileSql (sqlSegments ), statementType ,
118+ new QuerySettings ().setDatabase (connection .getSchema ()));
117119 }
118120
119121 @ Override
@@ -240,17 +242,18 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
240242 @ Override
241243 public boolean execute () throws SQLException {
242244 checkClosed ();
243- return execute (compileSql (sqlSegments ));
245+ return super .executeImpl (compileSql (sqlSegments ), statementType ,
246+ new QuerySettings ().setDatabase (connection .getSchema ()));
244247 }
245248
246249 @ Override
247250 public void addBatch () throws SQLException {
248251 checkClosed ();
249252 if (statementType == StatementType .INSERT ) {
250253 // adding values to the end of big INSERT statement.
251- addBatch (compileSql (valueSegments ));
254+ super . addBatch (compileSql (valueSegments ));
252255 } else {
253- addBatch (compileSql (sqlSegments ));
256+ super . addBatch (compileSql (sqlSegments ));
254257 }
255258 }
256259
@@ -265,7 +268,8 @@ public int[] executeBatch() throws SQLException {
265268 sb .append (sql ).append ("," );
266269 }
267270 sb .setCharAt (sb .length () - 1 , ';' );
268- int rowsInserted = executeUpdate (sb .toString ());
271+ int rowsInserted = executeUpdateImpl (sb .toString (), statementType ,
272+ new QuerySettings ().setDatabase (connection .getSchema ()));
269273 // clear batch and re-add insert into
270274 int [] results = new int [batch .size ()];
271275 if (rowsInserted == batch .size ()) {
@@ -280,18 +284,22 @@ public int[] executeBatch() throws SQLException {
280284 return results ;
281285 } else {
282286 // run executeBatch
283- return super . executeBatch ();
287+ return executeBatchImpl (). stream (). mapToInt ( Integer :: intValue ). toArray ();
284288 }
285289 }
286290
287291 @ Override
288292 public long [] executeLargeBatch () throws SQLException {
289- int [] results = executeBatch ();
290- long [] longResults = new long [results .length ];
291- for (int i = 0 ; i < results .length ; i ++) {
292- longResults [i ] = results [i ];
293+ return executeBatchImpl ().stream ().mapToLong (Integer ::longValue ).toArray ();
294+ }
295+
296+ private List <Integer > executeBatchImpl () throws SQLException {
297+ List <Integer > results = new ArrayList <>();
298+ QuerySettings settings = new QuerySettings ().setDatabase (connection .getSchema ());
299+ for (String sql : batch ) {
300+ results .add (executeUpdateImpl (sql , statementType , settings ));
293301 }
294- return longResults ;
302+ return results ;
295303 }
296304
297305 @ Override
@@ -418,7 +426,8 @@ public ParameterMetaData getParameterMetaData() throws SQLException {
418426 @ Override
419427 public void setRowId (int parameterIndex , RowId x ) throws SQLException {
420428 checkClosed ();
421- parameters [parameterIndex - 1 ] = encodeObject (x );
429+ throw new SQLException ("ROWID type is not supported by ClickHouse." ,
430+ ExceptionUtils .SQL_STATE_FEATURE_NOT_SUPPORTED );
422431 }
423432
424433 @ Override
@@ -546,6 +555,118 @@ public long executeLargeUpdate() throws SQLException {
546555 return executeUpdate ();
547556 }
548557
558+ @ Override
559+ public final void addBatch (String sql ) throws SQLException {
560+ checkClosed ();
561+ throw new SQLException (
562+ "addBatch(String) cannot be called in PreparedStatement or CallableStatement!" ,
563+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
564+ }
565+
566+ @ Override
567+ public final boolean execute (String sql ) throws SQLException {
568+ checkClosed ();
569+ throw new SQLException (
570+ "execute(String) cannot be called in PreparedStatement or CallableStatement!" ,
571+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
572+ }
573+
574+ @ Override
575+ public final boolean execute (String sql , int autoGeneratedKeys ) throws SQLException {
576+ checkClosed ();
577+ throw new SQLException (
578+ "execute(String, int) 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 , int [] columnIndexes ) throws SQLException {
584+ checkClosed ();
585+ throw new SQLException (
586+ "execute(String, int[]) 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 , String [] columnNames ) throws SQLException {
592+ checkClosed ();
593+ throw new SQLException (
594+ "execute(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
595+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
596+ }
597+
598+ @ Override
599+ public final long executeLargeUpdate (String sql ) throws SQLException {
600+ checkClosed ();
601+ throw new SQLException (
602+ "executeLargeUpdate(String) cannot be called in PreparedStatement or CallableStatement!" ,
603+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
604+ }
605+
606+ @ Override
607+ public final long executeLargeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
608+ checkClosed ();
609+ throw new SQLException (
610+ "executeLargeUpdate(String, int) 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 , int [] columnIndexes ) throws SQLException {
616+ checkClosed ();
617+ throw new SQLException (
618+ "executeLargeUpdate(String, int[]) 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 , String [] columnNames ) throws SQLException {
624+ checkClosed ();
625+ throw new SQLException (
626+ "executeLargeUpdate(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
627+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
628+ }
629+
630+ @ Override
631+ public final ResultSet executeQuery (String sql ) throws SQLException {
632+ checkClosed ();
633+ throw new SQLException (
634+ "executeQuery(String) cannot be called in PreparedStatement or CallableStatement!" ,
635+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
636+ }
637+
638+ @ Override
639+ public final int executeUpdate (String sql ) throws SQLException {
640+ checkClosed ();
641+ throw new SQLException (
642+ "executeUpdate(String) cannot be called in PreparedStatement or CallableStatement!" ,
643+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
644+ }
645+
646+ @ Override
647+ public final int executeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
648+ checkClosed ();
649+ throw new SQLException (
650+ "executeUpdate(String, int) 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 , int [] columnIndexes ) throws SQLException {
656+ checkClosed ();
657+ throw new SQLException (
658+ "executeUpdate(String, int[]) 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 , String [] columnNames ) throws SQLException {
664+ checkClosed ();
665+ throw new SQLException (
666+ "executeUpdate(String, String[]) cannot be called in PreparedStatement or CallableStatement!" ,
667+ ExceptionUtils .SQL_STATE_WRONG_OBJECT_TYPE );
668+ }
669+
549670 private static String encodeObject (Object x ) throws SQLException {
550671 LOG .trace ("Encoding object: {}" , x );
551672
0 commit comments