All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databend.client.DatabendSession Maven / Gradle / Ivy

/*
 * Licensed 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 com.databend.client;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.google.common.base.MoreObjects.toStringHelper;

/**
 * Databend client session configuration.
 */
public class DatabendSession {
    private static final String DEFAULT_DATABASE = "default";
    private static final String AUTO_COMMIT = "AutoCommit";

    private final String database;
    private final AtomicBoolean autoCommit = new AtomicBoolean(false);


    private final Map settings;

    // txn
    private String txnState;
    private final ServerInfo lastServerInfo;
    private final List lastQueryIds;


    @JsonCreator
    public DatabendSession(
            @JsonProperty("database") String database,
            @JsonProperty("settings") Map settings,
            @JsonProperty("txn_state") String txnState,
            @JsonProperty("last_server_info") ServerInfo lastServerInfo,
            @JsonProperty("last_query_ids") List lastQueryIds) {
        this.database = database;
        this.settings = settings;
        this.txnState = txnState;
        this.lastServerInfo = lastServerInfo;
//        this.lastQueryIds = lastQueryIds;
        this.lastQueryIds = lastQueryIds == null ? new ArrayList<>() : lastQueryIds;
    }

    // default
    public static DatabendSession createDefault() {
        return new DatabendSession(DEFAULT_DATABASE, null, null, null, null);
    }

    public static Builder builder() {
        return new Builder();
    }

    @JsonProperty
    public String getDatabase() {
        return database;
    }


    @JsonProperty
    public Map getSettings() {
        return settings;
    }

    @JsonProperty("txn_state")
    public String getTxnState() {
        return txnState;
    }

    @JsonProperty("last_server_info")
    public ServerInfo getLastServerInfo() {
        return lastServerInfo;
    }

    @JsonProperty("last_query_ids")
    public List getLastQueryIds() {
        return lastQueryIds;
    }

    @Override
    public String toString() {
        return toStringHelper(this).add("database", database).add("settings", settings).toString();
    }

    public boolean getAutoCommit() {
        return autoCommit.get();
    }

    public void setAutoCommit(boolean autoCommit) {
        this.autoCommit.set(autoCommit);
        if (autoCommit) {
            this.txnState = AUTO_COMMIT;
        }
    }

    public static final class Builder {
        private URI host;
        private String database;
        private final AtomicBoolean autoCommit = new AtomicBoolean(false);
        private Map settings;

        // txn
        private String txnState;
        private ServerInfo lastServerInfo;
        private List lastQueryIds;

        public Builder setHost(URI host) {
            this.host = host;
            return this;
        }

        public Builder setDatabase(String database) {
            this.database = database;
            return this;
        }

        public Builder setSettings(Map settings) {
            this.settings = settings;
            return this;
        }

        public Builder setTxnState(String txnState) {
            this.txnState = txnState;
            return this;
        }

        public Builder setLastServerInfo(ServerInfo lastServerInfo) {
            this.lastServerInfo = lastServerInfo;
            return this;
        }

        public Builder setLastQueryIds(List lastQueryIds) {
            this.lastQueryIds = lastQueryIds;
            return this;
        }

        public boolean getAutoCommit() {
            return autoCommit.get();
        }

        public void setAutoCommit(boolean autoCommit) {
            this.autoCommit.set(autoCommit);
            if (autoCommit) {
                this.txnState = AUTO_COMMIT;
            }
        }

        public DatabendSession build() {
            return new DatabendSession(database, settings, txnState, lastServerInfo, lastQueryIds);
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy