package org.springblade.flow.database.model;

import java.net.URI;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.Data;

/**
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/16 8:49
 */
@Data
@Component
public class DataSourceUtil {

	public static final String PREFIX = "spring.datasource.dynamic.datasource.master";

	@Value("${" + PREFIX + ".url}")
	private String url;

	@Value("${" + PREFIX + ".username}")
	private String userName;

	@Value("${" + PREFIX + ".password}")
	private String password;

	/**
	 * 数据库类型
	 */
	private String dataType = "MySQL";

	/**
	 * 表空间
	 */
	private String tableSpace = "";

	public String getHost() {
		URI uri = getDatabaseURI();
		return uri.getHost();
	}

	public String getPort() {
		URI uri = getDatabaseURI();
		return String.valueOf(uri.getPort());
	}

	public String getDbName() {
		URI uri = getDatabaseURI();
		return uri.getPath().substring(1);
	}

	private URI getDatabaseURI() {
		URI uri = URI.create(url.substring(5));
		return uri;
	}
}
