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

zikai.apijson.core.orm.SQLConfig Maven / Gradle / Ivy

The newest version!
/*Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.

This source code is licensed under the Apache License Version 2.0.*/


package zikai.apijson.core.orm;

import java.util.List;
import java.util.Map;

import zikai.apijson.core.NotNull;
import zikai.apijson.core.RequestMethod;
import zikai.apijson.core.StringUtil;

/**SQL配置
 * @author Lemon
 */
public interface SQLConfig {

	String DATABASE_MYSQL = "MYSQL";
	String DATABASE_POSTGRESQL = "POSTGRESQL";
	String DATABASE_SQLSERVER = "SQLSERVER";
	String DATABASE_ORACLE = "ORACLE";
	String DATABASE_DB2 = "DB2";
	String DATABASE_DAMENG = "DAMENG";
	String DATABASE_CLICKHOUSE = "CLICKHOUSE";
	String DATABASE_HIVE = "HIVE";
	String DATABASE_TDENGINE = "TDENGINE";

	String SCHEMA_INFORMATION = "information_schema";  //MySQL, PostgreSQL, SQL Server 都有的系统模式
	String SCHEMA_SYS = "sys";  //SQL Server 系统模式
	String TABLE_SCHEMA = "table_schema";
	String TABLE_NAME = "table_name";

	int TYPE_CHILD = 0;
	int TYPE_ITEM = 1;
	int TYPE_ITEM_CHILD_0 = 2;

	boolean isMySQL();
	boolean isPostgreSQL();
	boolean isSQLServer();
	boolean isOracle();
	boolean isDb2();
	boolean isDameng();
	boolean isClickHouse();
	boolean isHive();
	boolean isTDengine();


	//暂时只兼容以上几种
	//	boolean isSQL();
	//	boolean isTSQL();
	//	boolean isPLSQL();
	//	boolean isAnsiSQL();

	boolean limitSQLCount(); //用来给 Table, Column 等系统属性表来绕过 MAX_SQL_COUNT 等限制

	@NotNull
	String getIdKey();
	@NotNull
	String getUserIdKey();


	/**获取数据库版本号,可通过判断版本号解决一些 JDBC 驱动连接数据库的兼容问题
	 * MYSQL: 8.0, 5.7, 5.6 等; PostgreSQL: 11, 10, 9.6 等
	 */
	String getDBVersion();

	@NotNull
	default int[] getDBVersionNums() {
		String dbVersion = StringUtil.getNoBlankString(getDBVersion());
		if (dbVersion.isEmpty()) {
			return new int[]{0};
		}

		int index = dbVersion.indexOf("-");
		if (index > 0) {
			dbVersion = dbVersion.substring(0, index);
		}

		String[] ss = dbVersion.split("[.]");
		int[] nums = new int[Math.max(1, ss.length)];
		for (int i = 0; i < ss.length; i++) {
			nums[i] = Integer.valueOf(ss[i]);
		}

		return nums;
	}

	/**获取数据库地址
	 */
	String getDBUri();

	/**获取数据库账号
	 * @return
	 */
	String getDBAccount();

	/**获取数据库密码
	 */
	String getDBPassword();

	/**获取SQL语句
	 */
	String getSQL(boolean prepared) throws Exception;



	boolean isTest();
	SQLConfig setTest(boolean test);

	int getType();
	SQLConfig setType(int type);

	int getCount();
	SQLConfig setCount(int count);

	int getPage();
	SQLConfig setPage(int page);

	int getQuery();
	SQLConfig setQuery(int query);

	Boolean getCompat();
	SQLConfig setCompat(Boolean compat);

	int getPosition();
	SQLConfig setPosition(int position);

	int getCache();
	SQLConfig setCache(int cache);

	boolean isExplain();
	SQLConfig setExplain(boolean explain);


	RequestMethod getMethod();
	SQLConfig setMethod(RequestMethod method);

	Object getId();
	SQLConfig setId(Object id);

	Object getIdIn();
	SQLConfig setIdIn(Object idIn);

	Object getUserId();
	SQLConfig setUserId(Object userId);

	Object getUserIdIn();
	SQLConfig setUserIdIn(Object userIdIn);

	String getRole();
	SQLConfig setRole(String role);

	public boolean isDistinct();
	public SQLConfig setDistinct(boolean distinct);

	String getDatabase();
	SQLConfig setDatabase(String database);

	String getSchema();
	SQLConfig setSchema(String schema);

	String getDatasource();
	SQLConfig setDatasource(String datasource);

	String getQuote();

	List getJson();
	SQLConfig setJson(List json);


	/**
	 * 请求传进来的Table名
	 */
	String getTable();

	SQLConfig setTable(String table);


	/**
	 * 数据库里的真实Table名
	 */
	String getSQLTable();

	String getTablePath();

	List getRaw();
	SQLConfig setRaw(List raw);

	Subquery getFrom();
	SQLConfig setFrom(Subquery from);

	List getColumn();
	SQLConfig setColumn(List column);

	List> getValues();
	SQLConfig setValues(List> values);

	Map getContent();
	SQLConfig setContent(Map content);

	Map> getCombineMap();
	SQLConfig setCombineMap(Map> combineMap);

	String getCombine();
	SQLConfig setCombine(String combine);

	Map getCast();
	SQLConfig setCast(Map cast);

	List getNull();
	SQLConfig setNull(List nulls);

	Map getWhere();
	SQLConfig setWhere(Map where);

	String getGroup();
	SQLConfig setGroup(String group);

	Map getHaving();
	SQLConfig setHaving(Map having);

	String getHavingCombine();
	SQLConfig setHavingCombine(String havingCombine);

	String getOrder();
	SQLConfig setOrder(String order);

	/**
	 * exactMatch = false
	 * @param key
	 * @return
	 */
	Object getWhere(String key);
	/**
	 * @param key
	 * @param exactMatch
	 * @return
	 */
	Object getWhere(String key, boolean exactMatch);
	/**
	 * @param key
	 * @param value
	 * @return
	 */
	SQLConfig putWhere(String key, Object value, boolean prior);


	boolean isPrepared();

	SQLConfig setPrepared(boolean prepared);

	boolean isMain();

	SQLConfig setMain(boolean main);


	List getPreparedValueList();
	SQLConfig setPreparedValueList(List preparedValueList);


	String getAlias();

	SQLConfig setAlias(String alias);

	String getWhereString(boolean hasPrefix) throws Exception;

	String getRawSQL(String key, Object value) throws Exception;
	String getRawSQL(String key, Object value, boolean throwWhenMissing) throws Exception;

	boolean isKeyPrefix();

	SQLConfig setKeyPrefix(boolean keyPrefix);


	List getJoinList();

	SQLConfig setJoinList(List joinList);

	boolean hasJoin();


	String getSubqueryString(Subquery subquery) throws Exception;

	SQLConfig setProcedure(String procedure);



}