View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.prepare;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
23  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
24  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
25  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
26  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
27  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
28  import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
29  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.ListIterator;
35  import java.util.stream.Collectors;
36  
37  /**
38   * Parameter marker extractor for MySQL COM_STMT_PREPARE.
39   */
40  @NoArgsConstructor(access = AccessLevel.PRIVATE)
41  public final class MySQLComStmtPrepareParameterMarkerExtractor {
42      
43      /**
44       * TODO Support more statements and syntax.
45       * Find corresponding columns of parameter markers.
46       *
47       * @param sqlStatement SQL statement
48       * @param schema schema
49       * @return corresponding columns of parameter markers
50       */
51      public static List<ShardingSphereColumn> findColumnsOfParameterMarkers(final SQLStatement sqlStatement, final ShardingSphereSchema schema) {
52          return sqlStatement instanceof InsertStatement ? findColumnsOfParameterMarkersForInsert((InsertStatement) sqlStatement, schema) : Collections.emptyList();
53      }
54      
55      private static List<ShardingSphereColumn> findColumnsOfParameterMarkersForInsert(final InsertStatement insertStatement, final ShardingSphereSchema schema) {
56          ShardingSphereTable table = schema.getTable(insertStatement.getTable().getTableName().getIdentifier().getValue());
57          List<String> columnNamesOfInsert = getColumnNamesOfInsertStatement(insertStatement, table);
58          List<ShardingSphereColumn> result = new ArrayList<>(insertStatement.getParameterMarkerSegments().size());
59          for (InsertValuesSegment each : insertStatement.getValues()) {
60              ListIterator<ExpressionSegment> listIterator = each.getValues().listIterator();
61              for (int columnIndex = listIterator.nextIndex(); listIterator.hasNext(); columnIndex = listIterator.nextIndex()) {
62                  ExpressionSegment value = listIterator.next();
63                  if (!(value instanceof ParameterMarkerExpressionSegment)) {
64                      continue;
65                  }
66                  String columnName = columnNamesOfInsert.get(columnIndex);
67                  ShardingSphereColumn column = table.getColumn(columnName);
68                  result.add(column);
69              }
70          }
71          return result;
72      }
73      
74      private static List<String> getColumnNamesOfInsertStatement(final InsertStatement insertStatement, final ShardingSphereTable table) {
75          return insertStatement.getColumns().isEmpty() ? table.getColumnNames() : insertStatement.getColumns().stream().map(each -> each.getIdentifier().getValue()).collect(Collectors.toList());
76      }
77  }