/*
 * 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.skywalking.apm.plugin.sjdbc;

import com.dangdang.ddframe.rdb.sharding.executor.event.AbstractExecutionEvent;
import com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEvent;
import com.dangdang.ddframe.rdb.sharding.executor.event.DQLExecutionEvent;
import com.dangdang.ddframe.rdb.sharding.executor.threadlocal.ExecutorDataMap;
import com.dangdang.ddframe.rdb.sharding.util.EventBusInstance;
import com.google.common.base.Joiner;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.sjdbc.define.AsyncExecuteInterceptor;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;

/**
 * Sharding-jdbc provides {@link EventBusInstance} to help external systems getDefault events of sql execution. {@link
 * ExecuteEventListener} can getDefault sql statement start and end events, resulting in db span.
 */
public class ExecuteEventListener {

    public static void init() {
        EventBusInstance.getInstance().register(new ExecuteEventListener());
    }

    @Subscribe
    @AllowConcurrentEvents
    public void listenDML(DMLExecutionEvent event) {
        handle(event, "MODIFY");
    }

    @Subscribe
    @AllowConcurrentEvents
    public void listenDQL(DQLExecutionEvent event) {
        handle(event, "QUERY");
    }

    private void handle(AbstractExecutionEvent event, String operation) {
        switch (event.getEventExecutionType()) {
            case BEFORE_EXECUTE:
                AbstractSpan span = ContextManager.createExitSpan("/SJDBC/BRANCH/" + operation, event.getDataSource());
                if (ExecutorDataMap.getDataMap().containsKey(AsyncExecuteInterceptor.SNAPSHOT_DATA_KEY)) {
                    ContextManager.continued((ContextSnapshot) ExecutorDataMap.getDataMap()
                                                                              .get(AsyncExecuteInterceptor.SNAPSHOT_DATA_KEY));
                }
                Tags.DB_TYPE.set(span, "sql");
                Tags.DB_INSTANCE.set(span, event.getDataSource());
                Tags.DB_STATEMENT.set(span, event.getSql());
                if (!event.getParameters().isEmpty()) {
                    Tags.DB_BIND_VARIABLES.set(span, Joiner.on(",").join(event.getParameters()));
                }
                span.setComponent(ComponentsDefine.SHARDING_JDBC);
                SpanLayer.asDB(span);
                break;
            case EXECUTE_FAILURE:
                span = ContextManager.activeSpan();
                span.errorOccurred();
                if (event.getException().isPresent()) {
                    span.log(event.getException().get());
                }
            case EXECUTE_SUCCESS:
                ContextManager.stopSpan();
        }
    }
}

@@ -1225,46 +1225,8 @@
 ce;%0A
-import com.google.common.base.Joiner;%0A
 impo
@@ -1323,16 +1323,52 @@
 scribe;%0A
+import java.util.stream.Collectors;%0A
 import o
@@ -2979,62 +2979,8 @@
 p()%0A
-                                                      
     
@@ -3322,83 +3322,250 @@
     
-Tags.DB_BIND_VARIABLES.set(span, Joiner.on(%22,%22).join(event.getParameters())
+String variables = event.getParameters()%0A                        .stream()%0A                        .map(String::valueOf)%0A                        .collect(Collectors.joining(%22,%22));%0A                    Tags.DB_BIND_VARIABLES.set(span, variables
 );%0A 

fix #4765 (#4766)