|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner.connection; |
| 18 | + |
| 19 | +import com.google.cloud.spanner.ErrorCode; |
| 20 | +import com.google.cloud.spanner.SpannerExceptionFactory; |
| 21 | +import com.google.cloud.spanner.Value; |
| 22 | +import com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement; |
| 23 | +import com.google.cloud.spanner.connection.ClientSideStatementImpl.CompileException; |
| 24 | +import com.google.common.base.Strings; |
| 25 | +import java.lang.reflect.Method; |
| 26 | +import java.util.regex.Matcher; |
| 27 | + |
| 28 | +/** Executor for <code>RUN PARTITION <partition_id></code> statements. */ |
| 29 | +class ClientSideStatementRunPartitionExecutor implements ClientSideStatementExecutor { |
| 30 | + private final ClientSideStatementImpl statement; |
| 31 | + private final Method method; |
| 32 | + |
| 33 | + ClientSideStatementRunPartitionExecutor(ClientSideStatementImpl statement) |
| 34 | + throws CompileException { |
| 35 | + try { |
| 36 | + this.statement = statement; |
| 37 | + this.method = |
| 38 | + ConnectionStatementExecutor.class.getDeclaredMethod( |
| 39 | + statement.getMethodName(), String.class); |
| 40 | + } catch (Exception e) { |
| 41 | + throw new CompileException(e, statement); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public StatementResult execute( |
| 47 | + ConnectionStatementExecutor connection, ParsedStatement parsedStatement) throws Exception { |
| 48 | + String partitionId = getParameterValue(parsedStatement); |
| 49 | + if (partitionId == null) { |
| 50 | + throw SpannerExceptionFactory.newSpannerException( |
| 51 | + ErrorCode.INVALID_ARGUMENT, |
| 52 | + "No valid partition id found in statement: " + parsedStatement.getStatement().getSql()); |
| 53 | + } |
| 54 | + return (StatementResult) method.invoke(connection, partitionId); |
| 55 | + } |
| 56 | + |
| 57 | + String getParameterValue(ParsedStatement parsedStatement) { |
| 58 | + // The statement has the form `RUN PARTITION ['partition-id']`. |
| 59 | + // The regex that is defined for this statement is (simplified) `run\s+partition(?:\s*'(.*)')?` |
| 60 | + // This regex has one capturing group, which captures the partition-id inside the single quotes. |
| 61 | + // That capturing group is however inside a non-capturing optional group. |
| 62 | + // That means that: |
| 63 | + // 1. If the matcher matches and returns one or more groups, we know that we have a partition-id |
| 64 | + // in the SQL statement itself, as that is the only thing that can be in a capturing group. |
| 65 | + // 2. If the matcher matches and returns zero groups, we know that the statement is valid, but |
| 66 | + // that it does not contain a partition-id in the SQL statement. The partition-id must then |
| 67 | + // be included in the statement as a query parameter. |
| 68 | + Matcher matcher = statement.getPattern().matcher(parsedStatement.getSqlWithoutComments()); |
| 69 | + if (matcher.find() && matcher.groupCount() >= 1) { |
| 70 | + String value = matcher.group(1); |
| 71 | + if (!Strings.isNullOrEmpty(value)) { |
| 72 | + return value; |
| 73 | + } |
| 74 | + } |
| 75 | + if (parsedStatement.getStatement().getParameters().size() == 1) { |
| 76 | + Value value = parsedStatement.getStatement().getParameters().values().iterator().next(); |
| 77 | + return value.getAsString(); |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | +} |
0 commit comments