Commit e3200fbf authored by cherrylzhao's avatar cherrylzhao
Browse files

for #2273, remove unused rewrite hook.

parent 8f69df34
Loading
Loading
Loading
Loading
+1 −15
Original line number Diff line number Diff line
@@ -57,8 +57,6 @@ import org.apache.shardingsphere.core.parse.old.parser.expression.SQLNumberExpre
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLTextExpression;
import org.apache.shardingsphere.core.parse.util.SQLUtil;
import org.apache.shardingsphere.core.rewrite.hook.RewriteHook;
import org.apache.shardingsphere.core.rewrite.hook.SPIRewriteHook;
import org.apache.shardingsphere.core.rewrite.placeholder.AggregationDistinctPlaceholder;
import org.apache.shardingsphere.core.rewrite.placeholder.EncryptUpdateItemColumnPlaceholder;
import org.apache.shardingsphere.core.rewrite.placeholder.EncryptWhereColumnPlaceholder;
@@ -116,8 +114,6 @@ public final class SQLRewriteEngine {
    
    private final OptimizeResult optimizeResult;
    
    private final RewriteHook rewriteHook = new SPIRewriteHook();
    
    /**
     * Constructs SQL rewrite engine.
     * 
@@ -516,17 +512,7 @@ public final class SQLRewriteEngine {
     * @return SQL unit
     */
    public SQLUnit generateSQL(final TableUnit tableUnit, final SQLBuilder sqlBuilder, final ShardingDataSourceMetaData shardingDataSourceMetaData) {
        rewriteHook.start(tableUnit);
        try {
            SQLUnit result = sqlBuilder.toSQL(tableUnit, getTableTokens(tableUnit), shardingRule, shardingDataSourceMetaData);
            rewriteHook.finishSuccess(result);
            return result;
            // CHECKSTYLE:OFF
        } catch (final Exception ex) {
            // CHECKSTYLE:ON
            rewriteHook.finishFailure(ex);
            throw ex;
        }
        return sqlBuilder.toSQL(tableUnit, getTableTokens(tableUnit), shardingRule, shardingDataSourceMetaData);
    }
   
    private Map<String, String> getTableTokens(final TableUnit tableUnit) {
+0 −50
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.core.rewrite.hook;

import org.apache.shardingsphere.core.route.SQLUnit;
import org.apache.shardingsphere.core.route.type.TableUnit;

/**
 * Rewrite Hook.
 *
 * @author yangyi
 */
public interface RewriteHook {
    
    /**
     * Handle when rewrite started.
     *
     * @param tableUnit table unit
     */
    void start(TableUnit tableUnit);
    
    /**
     * Handle when rewrite finished success.
     *
     * @param sqlUnit sql unit
     */
    void finishSuccess(SQLUnit sqlUnit);
    
    /**
     * Handle when rewrite finished failure.
     *
     * @param cause failure cause
     */
    void finishFailure(Exception cause);
}
+0 −59
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.core.rewrite.hook;

import org.apache.shardingsphere.core.route.SQLUnit;
import org.apache.shardingsphere.core.route.type.TableUnit;
import org.apache.shardingsphere.core.spi.NewInstanceServiceLoader;

import java.util.Collection;

/**
 * Rewrite hook for SPI.
 *
 * @author yangyi
 */
public final class SPIRewriteHook implements RewriteHook {
    
    private final Collection<RewriteHook> rewriteHooks = NewInstanceServiceLoader.newServiceInstances(RewriteHook.class);
    
    static {
        NewInstanceServiceLoader.register(RewriteHook.class);
    }
    
    @Override
    public void start(final TableUnit tableUnit) {
        for (RewriteHook each : rewriteHooks) {
            each.start(tableUnit);
        }
    }
    
    @Override
    public void finishSuccess(final SQLUnit sqlUnit) {
        for (RewriteHook each : rewriteHooks) {
            each.finishSuccess(sqlUnit);
        }
    }
    
    @Override
    public void finishFailure(final Exception cause) {
        for (RewriteHook each : rewriteHooks) {
            each.finishFailure(cause);
        }
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@

package org.apache.shardingsphere.core.rewrite;

import org.apache.shardingsphere.core.rewrite.hook.SPIRewriteHookTest;
import org.apache.shardingsphere.core.rewrite.placeholder.AllPlaceholderTests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@@ -27,7 +26,6 @@ import org.junit.runners.Suite.SuiteClasses;
@SuiteClasses({
        SQLBuilderTest.class, 
        SQLRewriteEngineTest.class,
        SPIRewriteHookTest.class,
        AllPlaceholderTests.class
    })
public final class AllRewriteTests {
+0 −53
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.core.rewrite.hook;

import org.apache.shardingsphere.core.rewrite.hook.fixture.RewriteHookFixture;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public final class SPIRewriteHookTest {
    
    private SPIRewriteHook spiRewriteHook;
    
    @Before
    public void setUp() {
        RewriteHookFixture.clearActions();
        spiRewriteHook = new SPIRewriteHook();
    }
    
    @Test
    public void assertStart() {
        spiRewriteHook.start(null);
        assertTrue(RewriteHookFixture.containsAction("start"));
    }
    
    @Test
    public void assertFinishSuccess() {
        spiRewriteHook.finishSuccess(null);
        assertTrue(RewriteHookFixture.containsAction("finishSuccess"));
    }
    
    @Test
    public void assertFinishFailure() {
        spiRewriteHook.finishFailure(null);
        assertTrue(RewriteHookFixture.containsAction("finishFailure"));
    }
}
Loading