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.parser.yaml.swapper;
19  
20  import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
21  import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
22  import org.apache.shardingsphere.parser.constant.SQLParserOrder;
23  import org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
24  import org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfiguration;
25  import org.apache.shardingsphere.sql.parser.api.CacheOption;
26  
27  /**
28   * YAML SQL parser rule configuration swapper.
29   */
30  public final class YamlSQLParserRuleConfigurationSwapper implements YamlRuleConfigurationSwapper<YamlSQLParserRuleConfiguration, SQLParserRuleConfiguration> {
31      
32      private final YamlSQLParserCacheOptionConfigurationSwapper cacheOptionSwapper = new YamlSQLParserCacheOptionConfigurationSwapper();
33      
34      @Override
35      public YamlSQLParserRuleConfiguration swapToYamlConfiguration(final SQLParserRuleConfiguration data) {
36          YamlSQLParserRuleConfiguration result = new YamlSQLParserRuleConfiguration();
37          result.setParseTreeCache(cacheOptionSwapper.swapToYamlConfiguration(data.getParseTreeCache()));
38          result.setSqlStatementCache(cacheOptionSwapper.swapToYamlConfiguration(data.getSqlStatementCache()));
39          return result;
40      }
41      
42      @Override
43      public SQLParserRuleConfiguration swapToObject(final YamlSQLParserRuleConfiguration yamlConfig) {
44          CacheOption parseTreeCacheOption = null == yamlConfig.getParseTreeCache()
45                  ? DefaultSQLParserRuleConfigurationBuilder.PARSE_TREE_CACHE_OPTION
46                  : cacheOptionSwapper.swapToObject(yamlConfig.getParseTreeCache());
47          CacheOption sqlStatementCacheOption = null == yamlConfig.getSqlStatementCache()
48                  ? DefaultSQLParserRuleConfigurationBuilder.SQL_STATEMENT_CACHE_OPTION
49                  : cacheOptionSwapper.swapToObject(yamlConfig.getSqlStatementCache());
50          return new SQLParserRuleConfiguration(parseTreeCacheOption, sqlStatementCacheOption);
51      }
52      
53      @Override
54      public Class<SQLParserRuleConfiguration> getTypeClass() {
55          return SQLParserRuleConfiguration.class;
56      }
57      
58      @Override
59      public String getRuleTagName() {
60          return "SQL_PARSER";
61      }
62      
63      @Override
64      public int getOrder() {
65          return SQLParserOrder.ORDER;
66      }
67  }