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.infra.session.query;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
23  import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
24  import org.apache.shardingsphere.infra.hint.HintValueContext;
25  
26  import java.util.List;
27  import java.util.Optional;
28  
29  /**
30   * Query context.
31   */
32  @Getter
33  public final class QueryContext {
34      
35      private final SQLStatementContext sqlStatementContext;
36      
37      private final String sql;
38      
39      private final List<Object> parameters;
40      
41      @Getter(AccessLevel.NONE)
42      private final String databaseName;
43      
44      @Getter(AccessLevel.NONE)
45      private final String schemaName;
46      
47      private final HintValueContext hintValueContext;
48      
49      private final boolean useCache;
50      
51      public QueryContext(final SQLStatementContext sqlStatementContext, final String sql, final List<Object> params, final HintValueContext hintValueContext) {
52          this(sqlStatementContext, sql, params, hintValueContext, false);
53      }
54      
55      public QueryContext(final SQLStatementContext sqlStatementContext, final String sql, final List<Object> params, final HintValueContext hintValueContext, final boolean useCache) {
56          this.sqlStatementContext = sqlStatementContext;
57          this.sql = sql;
58          parameters = params;
59          databaseName = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getDatabaseName().orElse(null) : null;
60          schemaName = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getSchemaName().orElse(null) : null;
61          this.hintValueContext = hintValueContext;
62          this.useCache = useCache;
63      }
64      
65      /**
66       * Get database name from SQL statement.
67       * 
68       * @return got database name
69       */
70      public Optional<String> getDatabaseNameFromSQLStatement() {
71          return Optional.ofNullable(databaseName);
72      }
73      
74      /**
75       * Get schema name from SQL statement.
76       *
77       * @return got schema name
78       */
79      public Optional<String> getSchemaNameFromSQLStatement() {
80          return Optional.ofNullable(schemaName);
81      }
82  }