From 2c9bbabbde3011ad994977cb2bedaf3e1d7b1aa9 Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Thu, 29 Feb 2024 18:22:52 +0800 Subject: [PATCH 1/7] =?UTF-8?q?snowflake=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConnectionEdit/config/dataSource.ts | 121 +++++++ chat2db-client/src/constants/common.ts | 1 + chat2db-client/src/constants/database.ts | 7 + .../chat2db-plugins/chat2db-snowflake/pom.xml | 35 ++ .../plugin/snowflake/SnowFlakeDBManage.java | 8 + .../plugin/snowflake/SnowFlakeMetaData.java | 9 + .../plugin/snowflake/SnowFlakePlugin.java | 25 ++ .../builder/SnowFlakeSqlBuilder.java | 151 ++++++++ .../chat2db/plugin/snowflake/snowflake.json | 18 + .../type/OceanBaseColumnTypeEnum.java | 336 ++++++++++++++++++ .../type/OceanBaseIndexTypeEnum.java | 133 +++++++ .../META-INF/services/ai.chat2db.spi.Plugin | 1 + chat2db-server/chat2db-plugins/pom.xml | 1 + .../chat2db-server-domain-core/pom.xml | 5 + 14 files changed, 851 insertions(+) create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/pom.xml create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin diff --git a/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts b/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts index 115aa9403..9f87b9be5 100644 --- a/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts +++ b/chat2db-client/src/components/ConnectionEdit/config/dataSource.ts @@ -2076,4 +2076,125 @@ export const dataSourceFormConfigs: IConnectionConfig[] = [ extendInfo: [], type: DatabaseTypeCode.MONGODB }, + //SNOWFLAKE + { + baseInfo: { + items: [ + { + defaultValue: '@localhost', + inputType: InputType.INPUT, + labelNameCN: '名称', + labelNameEN: 'Name', + name: 'alias', + required: true, + styles: { + width: '100%', + } + }, + envItem, + { + defaultValue: '', + inputType: InputType.INPUT, + labelNameCN: '主机', + labelNameEN: 'Host', + name: 'host', + required: true, + styles: { + width: '70%', + } + }, + { + defaultValue: '443', + inputType: InputType.INPUT, + labelNameCN: '端口', + labelNameEN: 'Port', + name: 'port', + labelTextAlign: 'right', + required: true, + styles: { + width: '30%', + labelWidthEN: '40px', + labelWidthCN: '40px', + labelAlign: 'right' + } + }, + { + defaultValue: AuthenticationType.USERANDPASSWORD, + inputType: InputType.SELECT, + labelNameCN: '身份验证', + labelNameEN: 'Authentication', + name: 'authenticationType', + required: true, + selects: [ + { + items: [ + { + defaultValue: 'root', + inputType: InputType.INPUT, + labelNameCN: '用户名', + labelNameEN: 'User', + name: 'user', + required: true, + styles: { + width: '100%', + } + }, + { + defaultValue: '', + inputType: InputType.PASSWORD, + labelNameCN: '密码', + labelNameEN: 'Password', + name: 'password', + required: true, + styles: { + width: '100%', + } + }, + ], + label: 'User&Password', + value: AuthenticationType.USERANDPASSWORD, + }, + { + label: 'NONE', + value: AuthenticationType.NONE, + items: [], + + }, + ], + styles: { + width: '50%', + } + }, + { + defaultValue: '', + inputType: InputType.INPUT, + labelNameCN: '数据库', + labelNameEN: 'Database', + name: 'database', + required: false, + styles: { + width: '100%', + } + }, + { + defaultValue: 'jdbc:snowflake://:443', + inputType: InputType.INPUT, + labelNameCN: 'URL', + labelNameEN: 'URL', + name: 'url', + required: true, + styles: { + width: '100%', + } + }, + + ], + pattern: /jdbc:snowflake:\/\/(.*):(\d+)(\/\?db=(\w+))?/, + template: 'jdbc:snowflake://{host}:{port}/?db={database}', + //excludes: [OperationColumn.ViewDDL, OperationColumn.CreateTable,OperationColumn.EditTable] + }, + ssh: sshConfig, + extendInfo: [], + type: DatabaseTypeCode.SNOWFLAKE + }, ]; diff --git a/chat2db-client/src/constants/common.ts b/chat2db-client/src/constants/common.ts index b6f2339be..f847b4ffc 100644 --- a/chat2db-client/src/constants/common.ts +++ b/chat2db-client/src/constants/common.ts @@ -15,6 +15,7 @@ export enum DatabaseTypeCode { PRESTO = "PRESTO", HIVE = "HIVE", KINGBASE = "KINGBASE", + SNOWFLAKE = "SNOWFLAKE", } export enum ConsoleStatus { diff --git a/chat2db-client/src/constants/database.ts b/chat2db-client/src/constants/database.ts index ba7c3c75c..877023211 100644 --- a/chat2db-client/src/constants/database.ts +++ b/chat2db-client/src/constants/database.ts @@ -118,6 +118,13 @@ export const databaseMap: { // port: 27017, icon: '\uec21', }, + [DatabaseTypeCode.SNOWFLAKE]: { + name: 'Snowflake', + img: moreDBLogo, + code: DatabaseTypeCode.SNOWFLAKE, + // port: 443, + icon: '\uec21', + }, // [DatabaseTypeCode.REDIS]: { // name: 'Redis', // img: moreDBLogo, diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/pom.xml b/chat2db-server/chat2db-plugins/chat2db-snowflake/pom.xml new file mode 100644 index 000000000..b45999537 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + ai.chat2db + chat2db-plugins + ${revision} + ../pom.xml + + + + + ai.chat2db + chat2db-spi + + + + chat2db-snowflake + + + + src/main/java + + + **/*.json + + + + src/main/resources + + + + \ No newline at end of file diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java new file mode 100644 index 000000000..606fccfb0 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java @@ -0,0 +1,8 @@ +package ai.chat2db.plugin.snowflake; + +import ai.chat2db.spi.DBManage; +import ai.chat2db.spi.jdbc.DefaultDBManage; + +public class SnowFlakeDBManage extends DefaultDBManage implements DBManage { + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java new file mode 100644 index 000000000..70c490827 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java @@ -0,0 +1,9 @@ +package ai.chat2db.plugin.snowflake; + +import ai.chat2db.spi.MetaData; +import ai.chat2db.spi.jdbc.DefaultMetaService; + +public class SnowFlakeMetaData extends DefaultMetaService implements MetaData { + + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java new file mode 100644 index 000000000..f1e5555d7 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java @@ -0,0 +1,25 @@ +package ai.chat2db.plugin.snowflake; + +import ai.chat2db.spi.DBManage; +import ai.chat2db.spi.MetaData; + +import ai.chat2db.spi.Plugin; +import ai.chat2db.spi.config.DBConfig; +import ai.chat2db.spi.util.FileUtils; + +public class SnowFlakePlugin implements Plugin { + @Override + public DBConfig getDBConfig() { + return FileUtils.readJsonValue(this.getClass(),"snowflake.json", DBConfig.class); + } + + @Override + public MetaData getMetaData() { + return new SnowFlakeMetaData(); + } + + @Override + public DBManage getDBManage() { + return new SnowFlakeDBManage(); + } +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java new file mode 100644 index 000000000..df7df2100 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java @@ -0,0 +1,151 @@ +package ai.chat2db.plugin.snowflake.builder; +import ai.chat2db.spi.SqlBuilder; +import ai.chat2db.spi.jdbc.DefaultSqlBuilder; + +//package ai.chat2db.plugin.oceanbase.builder; +// +//import ai.chat2db.plugin.oceanbase.type.OceanBaseColumnTypeEnum; +//import ai.chat2db.plugin.oceanbase.type.OceanBaseIndexTypeEnum; +//import ai.chat2db.spi.SqlBuilder; +//import ai.chat2db.spi.jdbc.DefaultSqlBuilder; +//import ai.chat2db.spi.model.*; +//import org.apache.commons.lang3.StringUtils; +// +public class SnowFlakeSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { + +} +// +// @Override +// public String buildCreateTableSql(Table table) { +// StringBuilder script = new StringBuilder(); +// script.append("CREATE TABLE "); +// if(StringUtils.isNotBlank(table.getDatabaseName())) { +// script.append("`").append(table.getName()).append("`").append("."); +// } +// script.append("`").append(table.getName()).append("`").append(" (").append("\n"); +// +// // append column +// for (TableColumn column : table.getColumnList()) { +// if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType())){ +// continue; +// } +// OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(column.getColumnType()); +// script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n"); +// } +// +// // append primary key and index +// for (TableIndex tableIndex : table.getIndexList()) { +// if(StringUtils.isBlank(tableIndex.getName())|| StringUtils.isBlank(tableIndex.getType())){ +// continue; +// } +// OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); +// script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n"); +// } +// +// script = new StringBuilder(script.substring(0, script.length() - 2)); +// script.append("\n)"); +// +// +// if (StringUtils.isNotBlank(table.getEngine())) { +// script.append(" ENGINE=").append(table.getEngine()); +// } +// +// if (StringUtils.isNotBlank(table.getCharset())) { +// script.append(" DEFAULT CHARACTER SET=").append(table.getCharset()); +// } +// +// if (StringUtils.isNotBlank(table.getCollate())) { +// script.append(" COLLATE=").append(table.getCollate()); +// } +// +// if (table.getIncrementValue() != null) { +// script.append(" AUTO_INCREMENT=").append(table.getIncrementValue()); +// } +// +// if (StringUtils.isNotBlank(table.getComment())) { +// script.append(" COMMENT='").append(table.getComment()).append("'"); +// } +// +// if (StringUtils.isNotBlank(table.getPartition())) { +// script.append(" \n").append(table.getPartition()); +// } +// script.append(";"); +// +// return script.toString(); +// } +// +// @Override +// public String buildModifyTaleSql(Table oldTable, Table newTable) { +// StringBuilder script = new StringBuilder(); +// script.append("ALTER TABLE "); +// if(StringUtils.isNotBlank(oldTable.getDatabaseName())) { +// script.append("`").append(oldTable.getDatabaseName()).append("`").append("."); +// } +// script.append("`").append(oldTable.getName()).append("`").append("\n"); +// if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) { +// script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(",\n"); +// } +// if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) { +// script.append("\t").append("COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n"); +// } +// if (oldTable.getIncrementValue() != newTable.getIncrementValue()) { +// script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n"); +// } +// +// // append modify column +// for (TableColumn tableColumn : newTable.getColumnList()) { +// if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType())&& StringUtils.isNotBlank(tableColumn.getName())){ +// OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(tableColumn.getColumnType()); +// script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n"); +// } +// } +// +// // append modify index +// for (TableIndex tableIndex : newTable.getIndexList()) { +// if (StringUtils.isNotBlank(tableIndex.getEditStatus()) && StringUtils.isNotBlank(tableIndex.getType())) { +// OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); +// script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n"); +// } +// } +// if(script.length()>2) { +// script = new StringBuilder(script.substring(0, script.length() - 2)); +// script.append(";"); +// } +// +// return script.toString(); +// } +// +// +// +// @Override +// public String pageLimit(String sql, int offset, int pageNo, int pageSize) { +// StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14); +// sqlBuilder.append(sql); +// if (offset == 0) { +// sqlBuilder.append("\n LIMIT "); +// sqlBuilder.append(pageSize); +// } else { +// sqlBuilder.append("\n LIMIT "); +// sqlBuilder.append(offset); +// sqlBuilder.append(","); +// sqlBuilder.append(pageSize); +// } +// return sqlBuilder.toString(); +// } +// +// +// +// +// @Override +// public String buildCreateDatabaseSql(Database database) { +// StringBuilder sqlBuilder = new StringBuilder(); +// sqlBuilder.append("CREATE DATABASE `"+database.getName()+"`"); +// if (StringUtils.isNotBlank(database.getCharset())) { +// sqlBuilder.append(" DEFAULT CHARACTER SET=").append(database.getCharset()); +// } +// if (StringUtils.isNotBlank(database.getCollation())) { +// sqlBuilder.append(" COLLATE=").append(database.getCollation()); +// } +// return sqlBuilder.toString(); +// } +//} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json new file mode 100644 index 000000000..f7ed9a19c --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json @@ -0,0 +1,18 @@ +{ + "dbType": "SNOWFLAKE", + "supportDatabase": true, + "supportSchema": false, + "driverConfigList": [ + { + "url": "jdbc:snowflake://localhost:443/", + "custom": false, + "defaultDriver": true, + "downloadJdbcDriverUrls": [ + "https://oss.sqlgpt.cn/lib/oceanbase-client-2.4.2.jar" + ], + "jdbcDriver": "oceanbase-client-2.4.2.jar", + "jdbcDriverClass": "com.oceanbase.jdbc.Driver" + } + ], + "name": "SnowFlake" +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java new file mode 100644 index 000000000..baca359b1 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java @@ -0,0 +1,336 @@ +//package ai.chat2db.plugin.oceanbase.type; +// +//import ai.chat2db.spi.ColumnBuilder; +//import ai.chat2db.spi.enums.EditStatus; +//import ai.chat2db.spi.model.ColumnType; +//import ai.chat2db.spi.model.TableColumn; +//import com.google.common.collect.Maps; +//import org.apache.commons.lang3.StringUtils; +// +//import java.util.Arrays; +//import java.util.List; +//import java.util.Map; +// +//public enum OceanBaseColumnTypeEnum implements ColumnBuilder { +// +// BIT("BIT", true, false, true, false, false, false, true, true, false, false), +// +// TINYINT("TINYINT", false, false, true, true, false, false, true, true, false, false), +// +// TINYINT_UNSIGNED("TINYINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), +// +// SMALLINT("SMALLINT", false, false, true, true, false, false, true, true, false, false), +// +// SMALLINT_UNSIGNED("SMALLINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), +// +// MEDIUMINT("MEDIUMINT", false, false, true, true, false, false, true, true, false, false), +// +// MEDIUMINT_UNSIGNED("MEDIUMINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), +// +// INT("INT", false, false, true, true, false, false, true, true, false, false), +// +// +// INT_UNSIGNED("INT UNSIGNED", false, false, true, true, false, false, true, true, false, false), +// +// BIGINT("BIGINT", false, false, true, true, false, false, true, true, false, false), +// +// +// BIGINT_UNSIGNED("BIGINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), +// +// +// DECIMAL("DECIMAL", true, true, true, false, false, false, true, true, false, false), +// +// DECIMAL_UNSIGNED("DECIMAL UNSIGNED", true, true, true, false, false, false, true, true, false, false), +// +// +// FLOAT("FLOAT", true, true, true, false, false, false, true, true, false, false), +// +// FLOAT_UNSIGNED("FLOAT UNSIGNED", true, true, true, false, false, false, true, true, false, false), +// +// DOUBLE("DOUBLE", true, true, true, false, false, false, true, true, false, false), +// +// DOUBLE_UNSIGNED("DOUBLE UNSIGNED", true, true, true, false, false, false, true, true, false, false), +// DATE("DATE", false, false, true, false, false, false, true, true, false, false), +// DATETIME("DATETIME", true, false, true, false, false, false, true, true, true, false), +// +// TIMESTAMP("TIMESTAMP", true, false, true, false, false, false, true, true, true, false), +// TIME("TIME", true, false, true, false, false, false, true, true, false, false), +// YEAR("YEAR", false, false, true, false, false, false, true, true, false, false), +// CHAR("CHAR", true, false, true, false, true, true, true, true, false, false), +// +// VARCHAR("VARCHAR", true, false, true, false, true, true, true, true, false, false), +// +// BINARY("BINARY", true, false, true, false, false, false, true, true, false, false), +// +// VARBINARY("VARBINARY", true, false, true, false, false, false, true, true, false, false), +// +// TINYBLOB("TINYBLOB", false, false, true, false, false, false, true, false, false, false), +// +// BLOB("BLOB", false, false, true, false, false, false, true, false, false, false), +// +// MEDIUMBLOB("MEDIUMBLOB", false, false, true, false, false, false, true, false, false, false), +// +// LONGBLOB("LONGBLOB", false, false, true, false, false, false, true, false, false, false), +// +// TINYTEXT("TINYTEXT", false, false, true, false, true, true, true, false, false, false), +// +// TEXT("TEXT", false, false, true, false, true, true, true, false, false, false), +// +// MEDIUMTEXT("MEDIUMTEXT", false, false, true, false, true, true, true, false, false, false), +// +// LONGTEXT("LONGTEXT", false, false, true, false, true, true, true, false, false, false), +// +// +// ENUM("ENUM", false, false, true, false, true, true, true, true, true, true), +// +// +// BOOL("BOOL", false, false, true, true, false, false, true, true, false, false), +// +// INTEGER("INTEGER", false, false, true, true, false, false, true, true, false, false), +// +// INTEGER_UNSIGNED("INTEGER UNSIGNED", false, false, true, true, false, false, true, true, false, false), +// +// REAL("REAL", true, true, true, false, false, false, true, true, false, false), +// +// SET("SET", false, false, true, false, true, true, true, true, true, true), +// +// +// GEOMETRY("GEOMETRY", false, false, true, false, false, false, true, false, false, false), +// +// POINT("POINT", false, false, true, false, false, false, true, false, false, false), +// +// LINESTRING("LINESTRING", false, false, true, false, false, false, true, false, false, false), +// +// POLYGON("POLYGON", false, false, true, false, false, false, true, false, false, false), +// +// MULTIPOINT("MULTIPOINT", false, false, true, false, false, false, true, false, false, false), +// +// MULTILINESTRING("MULTILINESTRING", false, false, true, false, false, false, true, false, false, false), +// +// MULTIPOLYGON("MULTIPOLYGON", false, false, true, false, false, false, true, false, false, false), +// +// GEOMETRYCOLLECTION("GEOMETRYCOLLECTION", false, false, true, false, false, false, true, false, false, false), +// +// JSON("JSON", false, false, true, false, false, false, true, false, false, false); +// +// private ColumnType columnType; +// +// public static OceanBaseColumnTypeEnum getByType(String dataType) { +// return COLUMN_TYPE_MAP.get(dataType.toUpperCase()); +// } +// +// public ColumnType getColumnType() { +// return columnType; +// } +// +// +// OceanBaseColumnTypeEnum(String dataTypeName, boolean supportLength, boolean supportScale, boolean supportNullable, boolean supportAutoIncrement, boolean supportCharset, boolean supportCollation, boolean supportComments, boolean supportDefaultValue, boolean supportExtent, boolean supportValue) { +// this.columnType = new ColumnType(dataTypeName, supportLength, supportScale, supportNullable, supportAutoIncrement, supportCharset, supportCollation, supportComments, supportDefaultValue, supportExtent,supportValue,false); +// } +// +// private static Map COLUMN_TYPE_MAP = Maps.newHashMap(); +// +// static { +// for (OceanBaseColumnTypeEnum value : OceanBaseColumnTypeEnum.values()) { +// COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value); +// } +// } +// +// +// @Override +// public String buildCreateColumnSql(TableColumn column) { +// OceanBaseColumnTypeEnum type = COLUMN_TYPE_MAP.get(column.getColumnType().toUpperCase()); +// if (type == null) { +// return ""; +// } +// StringBuilder script = new StringBuilder(); +// +// script.append("`").append(column.getName()).append("`").append(" "); +// +// script.append(buildDataType(column, type)).append(" "); +// +// script.append(buildCharset(column,type)).append(" "); +// +// script.append(buildCollation(column,type)).append(" "); +// +// script.append(buildNullable(column,type)).append(" "); +// +// script.append(buildDefaultValue(column,type)).append(" "); +// +// script.append(buildExt(column,type)).append(" "); +// +// script.append(buildAutoIncrement(column,type)).append(" "); +// +// script.append(buildComment(column,type)).append(" "); +// +// return script.toString(); +// } +// +// private String buildCharset(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.getColumnType().isSupportCharset() || StringUtils.isEmpty(column.getCharSetName())){ +// return ""; +// } +// return StringUtils.join("CHARACTER SET ",column.getCharSetName()); +// } +// +// private String buildCollation(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.getColumnType().isSupportCollation() || StringUtils.isEmpty(column.getCollationName())){ +// return ""; +// } +// return StringUtils.join("COLLATE ",column.getCollationName()); +// } +// +// @Override +// public String buildModifyColumn(TableColumn tableColumn) { +// +// if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) { +// return StringUtils.join("DROP COLUMN `", tableColumn.getName() + "`"); +// } +// if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) { +// return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn)); +// } +// if (EditStatus.MODIFY.name().equals(tableColumn.getEditStatus())) { +// if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) { +// return StringUtils.join("CHANGE COLUMN `", tableColumn.getOldName(), "` ", buildCreateColumnSql(tableColumn)); +// } else { +// return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn)); +// } +// } +// return ""; +// } +// +// private String buildAutoIncrement(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.getColumnType().isSupportAutoIncrement()){ +// return ""; +// } +// if (column.getAutoIncrement() != null && column.getAutoIncrement()) { +// return "AUTO_INCREMENT"; +// } +// return ""; +// } +// +// private String buildComment(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())){ +// return ""; +// } +// return StringUtils.join("COMMENT '",column.getComment(),"'"); +// } +// +// private String buildExt(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.columnType.isSupportExtent() || StringUtils.isEmpty(column.getExtent())){ +// return ""; +// } +// return column.getComment(); +// } +// +// private String buildDefaultValue(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())){ +// return ""; +// } +// +// if("EMPTY_STRING".equalsIgnoreCase(column.getDefaultValue().trim())){ +// return StringUtils.join("DEFAULT ''"); +// } +// +// if("NULL".equalsIgnoreCase(column.getDefaultValue().trim())){ +// return StringUtils.join("DEFAULT NULL"); +// } +// +// if(Arrays.asList(CHAR,VARCHAR,BINARY,VARBINARY, SET,ENUM).contains(type)){ +// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); +// } +// +// if(Arrays.asList(DATE,TIME,YEAR).contains(type)){ +// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); +// } +// +// if(Arrays.asList(DATETIME,TIMESTAMP).contains(type)){ +// if("CURRENT_TIMESTAMP".equalsIgnoreCase(column.getDefaultValue().trim())){ +// return StringUtils.join("DEFAULT ",column.getDefaultValue()); +// } +// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); +// } +// +// return StringUtils.join("DEFAULT ",column.getDefaultValue()); +// } +// +// private String buildNullable(TableColumn column, OceanBaseColumnTypeEnum type) { +// if(!type.getColumnType().isSupportNullable()){ +// return ""; +// } +// if (column.getNullable()!=null && 1==column.getNullable()) { +// return "NULL"; +// } else { +// return "NOT NULL"; +// } +// } +// +// private String buildDataType(TableColumn column, OceanBaseColumnTypeEnum type) { +// String columnType = type.columnType.getTypeName(); +// if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR).contains(type)) { +// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); +// } +// +// if (BIT.equals(type)) { +// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); +// } +// +// if (Arrays.asList(TIME, DATETIME, TIMESTAMP).contains(type)) { +// if (column.getColumnSize() == null || column.getColumnSize() == 0) { +// return columnType; +// } else { +// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); +// } +// } +// +// +// if (Arrays.asList(DECIMAL, FLOAT, DOUBLE).contains(type)) { +// if (column.getColumnSize() == null || column.getDecimalDigits() == null) { +// return columnType; +// } +// if (column.getColumnSize() != null && column.getDecimalDigits() == null) { +// return StringUtils.join(columnType, "(", column.getColumnSize() + ")"); +// } +// if (column.getColumnSize() != null && column.getDecimalDigits() != null) { +// return StringUtils.join(columnType, "(", column.getColumnSize() + "," + column.getDecimalDigits() + ")"); +// } +// } +// +// if (Arrays.asList(DECIMAL_UNSIGNED, FLOAT_UNSIGNED, DECIMAL_UNSIGNED).contains(type)) { +// if (column.getColumnSize() == null || column.getDecimalDigits() == null) { +// return columnType; +// } +// if (column.getColumnSize() != null && column.getDecimalDigits() == null) { +// return unsignedDataType(columnType, "(" + column.getColumnSize() + ")"); +// } +// if (column.getColumnSize() != null && column.getDecimalDigits() != null) { +// return unsignedDataType(columnType, "(" + column.getColumnSize() + "," + column.getDecimalDigits() + ")"); +// } +// } +// +// if(Arrays.asList(SET,ENUM).contains(type)){ +// if(!StringUtils.isEmpty( column.getValue())){ +// return StringUtils.join(columnType,"(",column.getValue(),")"); +// } +// //List enumList = column. +// } +// +// return columnType; +// } +// +// private String unsignedDataType(String dataTypeName, String middle) { +// String[] split = dataTypeName.split(" "); +// if (split.length == 2) { +// return StringUtils.join(split[0], middle, split[1]); +// } +// return StringUtils.join(dataTypeName, middle); +// } +// +// public static List getTypes(){ +// return Arrays.stream(OceanBaseColumnTypeEnum.values()).map(columnTypeEnum -> +// columnTypeEnum.getColumnType() +// ).toList(); +// } +// +// +//} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java new file mode 100644 index 000000000..be9268d67 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java @@ -0,0 +1,133 @@ +//package ai.chat2db.plugin.oceanbase.type; +// +//import ai.chat2db.spi.enums.EditStatus; +//import ai.chat2db.spi.model.IndexType; +//import ai.chat2db.spi.model.TableIndex; +//import ai.chat2db.spi.model.TableIndexColumn; +//import org.apache.commons.lang3.StringUtils; +// +//import java.util.Arrays; +//import java.util.List; +// +//public enum OceanBaseIndexTypeEnum { +// +// PRIMARY_KEY("Primary", "PRIMARY KEY"), +// +// NORMAL("Normal", "INDEX"), +// +// UNIQUE("Unique", "UNIQUE INDEX"), +// +// FULLTEXT("Fulltext", "FULLTEXT INDEX"), +// +// SPATIAL("Spatial", "SPATIAL INDEX"); +// +// public String getName() { +// return name; +// } +// +// private String name; +// +// +// public String getKeyword() { +// return keyword; +// } +// +// private String keyword; +// +// public IndexType getIndexType() { +// return indexType; +// } +// +// public void setIndexType(IndexType indexType) { +// this.indexType = indexType; +// } +// +// private IndexType indexType; +// +// OceanBaseIndexTypeEnum(String name, String keyword) { +// this.name = name; +// this.keyword = keyword; +// this.indexType = new IndexType(name); +// } +// +// +// public static OceanBaseIndexTypeEnum getByType(String type) { +// for (OceanBaseIndexTypeEnum value : OceanBaseIndexTypeEnum.values()) { +// if (value.name.equalsIgnoreCase(type)) { +// return value; +// } +// } +// return null; +// } +// +// public String buildIndexScript(TableIndex tableIndex) { +// StringBuilder script = new StringBuilder(); +// +// script.append(keyword).append(" "); +// +// script.append(buildIndexName(tableIndex)).append(" "); +// +// script.append(buildIndexColumn(tableIndex)).append(" "); +// +// script.append(buildIndexComment(tableIndex)).append(" "); +// +// return script.toString(); +// } +// +// private String buildIndexComment(TableIndex tableIndex) { +// if(StringUtils.isBlank(tableIndex.getComment())){ +// return ""; +// }else { +// return StringUtils.join("COMMENT '",tableIndex.getComment(),"'"); +// } +// +// } +// +// private String buildIndexColumn(TableIndex tableIndex) { +// StringBuilder script = new StringBuilder(); +// script.append("("); +// for (TableIndexColumn column : tableIndex.getColumnList()) { +// if(StringUtils.isNotBlank(column.getColumnName())) { +// script.append("`").append(column.getColumnName()).append("`"); +// if (!StringUtils.isBlank(column.getAscOrDesc()) && !PRIMARY_KEY.equals(this)) { +// script.append(" ").append(column.getAscOrDesc()); +// } +// script.append(","); +// } +// } +// script.deleteCharAt(script.length() - 1); +// script.append(")"); +// return script.toString(); +// } +// +// private String buildIndexName(TableIndex tableIndex) { +// if(this.equals(PRIMARY_KEY)){ +// return ""; +// }else { +// return "`"+tableIndex.getName()+"`"; +// } +// } +// +// public String buildModifyIndex(TableIndex tableIndex) { +// if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) { +// return buildDropIndex(tableIndex); +// } +// if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) { +// return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex)); +// } +// if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) { +// return StringUtils.join("ADD ", buildIndexScript(tableIndex)); +// } +// return ""; +// } +// +// private String buildDropIndex(TableIndex tableIndex) { +// if (OceanBaseIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) { +// return StringUtils.join("DROP PRIMARY KEY"); +// } +// return StringUtils.join("DROP INDEX `", tableIndex.getOldName(),"`"); +// } +// public static List getIndexTypes() { +// return Arrays.asList(OceanBaseIndexTypeEnum.values()).stream().map(OceanBaseIndexTypeEnum::getIndexType).collect(java.util.stream.Collectors.toList()); +// } +//} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin new file mode 100644 index 000000000..47b46bb12 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin @@ -0,0 +1 @@ +ai.chat2db.plugin.snowflake.SnowFlakePlugin \ No newline at end of file diff --git a/chat2db-server/chat2db-plugins/pom.xml b/chat2db-server/chat2db-plugins/pom.xml index d058b04e7..6c1fc3ca1 100644 --- a/chat2db-server/chat2db-plugins/pom.xml +++ b/chat2db-server/chat2db-plugins/pom.xml @@ -30,6 +30,7 @@ chat2db-hive chat2db-redis chat2db-kingbase + chat2db-snowflake \ No newline at end of file diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml index ce19bc340..6846396b3 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/pom.xml @@ -126,5 +126,10 @@ chat2db-sqlserver ${revision} + + ai.chat2db + chat2db-snowflake + ${revision} + From 28d963f14e41b13e30bce02fdb4c7abbd3d1e395 Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Fri, 1 Mar 2024 18:19:23 +0800 Subject: [PATCH 2/7] =?UTF-8?q?snowflake=E8=A1=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/snowflake/SnowFlakeDBManage.java | 55 +++++++++++++++++++ .../plugin/snowflake/SnowFlakeMetaData.java | 19 +++++++ 2 files changed, 74 insertions(+) diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java index 606fccfb0..cf4f45e32 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java @@ -2,7 +2,62 @@ import ai.chat2db.spi.DBManage; import ai.chat2db.spi.jdbc.DefaultDBManage; +import ai.chat2db.spi.model.KeyValue; +import ai.chat2db.spi.sql.Chat2DBContext; +import ai.chat2db.spi.sql.ConnectInfo; +import ai.chat2db.spi.sql.SQLExecutor; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; public class SnowFlakeDBManage extends DefaultDBManage implements DBManage { + @Override + public Connection getConnection(ConnectInfo connectInfo) { + List extendInfo = connectInfo.getExtendInfo(); + if (StringUtils.isNotBlank(connectInfo.getDatabaseName())) { + KeyValue keyValue = new KeyValue(); + keyValue.setKey("db"); + keyValue.setValue(connectInfo.getDatabaseName()); + extendInfo.add(keyValue); + } + if (StringUtils.isNotBlank(connectInfo.getSchemaName())) { + KeyValue keyValue = new KeyValue(); + keyValue.setKey("schema"); + keyValue.setValue(connectInfo.getSchemaName()); + extendInfo.add(keyValue); + } + KeyValue keyValue = new KeyValue(); + keyValue.setKey("JDBC_QUERY_RESULT_FORMAT"); + keyValue.setValue("JSON"); + extendInfo.add(keyValue); + connectInfo.setExtendInfo(extendInfo); + return super.getConnection(connectInfo); + } + + + @Override + public void connectDatabase(Connection connection, String database) { + if (StringUtils.isEmpty(database)) { + return; + } + ConnectInfo connectInfo = Chat2DBContext.getConnectInfo(); + if (ObjectUtils.anyNull(connectInfo) || StringUtils.isEmpty(connectInfo.getSchemaName())) { + try { + SQLExecutor.getInstance().execute(connection, "USE DATABASE \"" + database + "\";"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } else { + try { + SQLExecutor.getInstance().execute(connection, "USE SCHEMA \"" + database + "\"." + connectInfo.getSchemaName() + ";"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java index 70c490827..6146f4e5a 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java @@ -2,8 +2,27 @@ import ai.chat2db.spi.MetaData; import ai.chat2db.spi.jdbc.DefaultMetaService; +import ai.chat2db.spi.model.Schema; +import ai.chat2db.spi.sql.SQLExecutor; +import ai.chat2db.spi.util.SortUtils; + +import java.sql.Connection; +import java.util.Arrays; +import java.util.List; public class SnowFlakeMetaData extends DefaultMetaService implements MetaData { + private List systemSchemas = Arrays.asList("INFORMATION_SCHEMA", "PUBLIC", "SCHEMA"); + + @Override + public List schemas(Connection connection, String databaseName) { + List schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null); + return SortUtils.sortSchema(schemas, systemSchemas); + } + + /* @Override + public String getMetaDataName(String... names) { + return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining(".")); + }*/ } From 31c3a7a1441d5cfada9fd82a1006b0f2523da58f Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Mon, 4 Mar 2024 18:28:59 +0800 Subject: [PATCH 3/7] =?UTF-8?q?snowflake=E8=A1=A8=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4-=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E7=89=88=20=E5=BE=85=E4=BF=AE=E5=A4=8DdruidSQL=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/snowflake/SnowFlakeMetaData.java | 28 --- ...keDBManage.java => SnowflakeDBManage.java} | 2 +- .../plugin/snowflake/SnowflakeMetaData.java | 73 ++++++ ...wFlakePlugin.java => SnowflakePlugin.java} | 6 +- .../builder/SnowFlakeSqlBuilder.java | 151 ------------ .../builder/SnowflakeSqlBuilder.java | 55 +++++ .../chat2db/plugin/snowflake/snowflake.json | 2 +- .../type/OceanBaseIndexTypeEnum.java | 133 ----------- .../snowflake/type/SnowflakeCharsetEnum.java | 26 ++ .../type/SnowflakeCollationEnum.java | 31 +++ ...Enum.java => SnowflakeColumnTypeEnum.java} | 225 +++++++----------- .../type/SnowflakeDefaultValueEnum.java | 30 +++ .../META-INF/services/ai.chat2db.spi.Plugin | 2 +- 13 files changed, 308 insertions(+), 456 deletions(-) delete mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java rename chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/{SnowFlakeDBManage.java => SnowflakeDBManage.java} (97%) create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java rename chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/{SnowFlakePlugin.java => SnowflakePlugin.java} (78%) delete mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java delete mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCharsetEnum.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCollationEnum.java rename chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/{OceanBaseColumnTypeEnum.java => SnowflakeColumnTypeEnum.java} (58%) create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeDefaultValueEnum.java diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java deleted file mode 100644 index 6146f4e5a..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeMetaData.java +++ /dev/null @@ -1,28 +0,0 @@ -package ai.chat2db.plugin.snowflake; - -import ai.chat2db.spi.MetaData; -import ai.chat2db.spi.jdbc.DefaultMetaService; -import ai.chat2db.spi.model.Schema; -import ai.chat2db.spi.sql.SQLExecutor; -import ai.chat2db.spi.util.SortUtils; - -import java.sql.Connection; -import java.util.Arrays; -import java.util.List; - -public class SnowFlakeMetaData extends DefaultMetaService implements MetaData { - - - private List systemSchemas = Arrays.asList("INFORMATION_SCHEMA", "PUBLIC", "SCHEMA"); - - @Override - public List schemas(Connection connection, String databaseName) { - List schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null); - return SortUtils.sortSchema(schemas, systemSchemas); - } - - /* @Override - public String getMetaDataName(String... names) { - return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining(".")); - }*/ -} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java similarity index 97% rename from chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java rename to chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java index cf4f45e32..6448c473f 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakeDBManage.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java @@ -13,7 +13,7 @@ import java.sql.SQLException; import java.util.List; -public class SnowFlakeDBManage extends DefaultDBManage implements DBManage { +public class SnowflakeDBManage extends DefaultDBManage implements DBManage { @Override public Connection getConnection(ConnectInfo connectInfo) { diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java new file mode 100644 index 000000000..bacf2ad1e --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java @@ -0,0 +1,73 @@ +package ai.chat2db.plugin.snowflake; + +import ai.chat2db.plugin.snowflake.builder.SnowflakeSqlBuilder; +import ai.chat2db.plugin.snowflake.type.SnowflakeCharsetEnum; +import ai.chat2db.plugin.snowflake.type.SnowflakeCollationEnum; +import ai.chat2db.plugin.snowflake.type.SnowflakeColumnTypeEnum; +import ai.chat2db.plugin.snowflake.type.SnowflakeDefaultValueEnum; +import ai.chat2db.spi.MetaData; +import ai.chat2db.spi.SqlBuilder; +import ai.chat2db.spi.jdbc.DefaultMetaService; +import ai.chat2db.spi.model.Schema; +import ai.chat2db.spi.model.Table; +import ai.chat2db.spi.model.TableMeta; +import ai.chat2db.spi.sql.SQLExecutor; +import ai.chat2db.spi.util.SortUtils; + +import java.sql.Connection; +import java.util.Arrays; +import java.util.List; + +public class SnowflakeMetaData extends DefaultMetaService implements MetaData { + + + private List systemSchemas = Arrays.asList("INFORMATION_SCHEMA", "PUBLIC", "SCHEMA"); + + @Override + public List schemas(Connection connection, String databaseName) { + List schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null); + return SortUtils.sortSchema(schemas, systemSchemas); + } + + /* @Override + public String getMetaDataName(String... names) { + return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining(".")); + }*/ + + private static String VIEW_SQL + = "SELECT TABLE_SCHEMA AS DatabaseName, TABLE_NAME AS ViewName, VIEW_DEFINITION AS DEFINITION, CHECK_OPTION, " + + "IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_CATALOG = '%s' AND TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';"; + + + @Override + public Table view(Connection connection, String databaseName, String schemaName, String viewName) { + String sql = String.format(VIEW_SQL, databaseName, schemaName , viewName); + return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { + Table table = new Table(); + table.setDatabaseName(databaseName); + table.setSchemaName(schemaName); + table.setName(viewName); + if (resultSet.next()) { + table.setDdl(resultSet.getString("DEFINITION").substring(resultSet.getString("DEFINITION").indexOf("as")+3)); + } + return table; + }); + } + + + @Override + public TableMeta getTableMeta(String databaseName, String schemaName, String tableName) { + return TableMeta.builder() + .columnTypes(SnowflakeColumnTypeEnum.getTypes()) + .charsets(SnowflakeCharsetEnum.getCharsets()) + .collations(SnowflakeCollationEnum.getCollations()) + .defaultValues(SnowflakeDefaultValueEnum.getDefaultValues()) + .build(); + } + + @Override + public SqlBuilder getSqlBuilder() { + return new SnowflakeSqlBuilder(); + } + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakePlugin.java similarity index 78% rename from chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java rename to chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakePlugin.java index f1e5555d7..79af0449e 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowFlakePlugin.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakePlugin.java @@ -7,7 +7,7 @@ import ai.chat2db.spi.config.DBConfig; import ai.chat2db.spi.util.FileUtils; -public class SnowFlakePlugin implements Plugin { +public class SnowflakePlugin implements Plugin { @Override public DBConfig getDBConfig() { return FileUtils.readJsonValue(this.getClass(),"snowflake.json", DBConfig.class); @@ -15,11 +15,11 @@ public DBConfig getDBConfig() { @Override public MetaData getMetaData() { - return new SnowFlakeMetaData(); + return new SnowflakeMetaData(); } @Override public DBManage getDBManage() { - return new SnowFlakeDBManage(); + return new SnowflakeDBManage(); } } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java deleted file mode 100644 index df7df2100..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowFlakeSqlBuilder.java +++ /dev/null @@ -1,151 +0,0 @@ -package ai.chat2db.plugin.snowflake.builder; -import ai.chat2db.spi.SqlBuilder; -import ai.chat2db.spi.jdbc.DefaultSqlBuilder; - -//package ai.chat2db.plugin.oceanbase.builder; -// -//import ai.chat2db.plugin.oceanbase.type.OceanBaseColumnTypeEnum; -//import ai.chat2db.plugin.oceanbase.type.OceanBaseIndexTypeEnum; -//import ai.chat2db.spi.SqlBuilder; -//import ai.chat2db.spi.jdbc.DefaultSqlBuilder; -//import ai.chat2db.spi.model.*; -//import org.apache.commons.lang3.StringUtils; -// -public class SnowFlakeSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { - -} -// -// @Override -// public String buildCreateTableSql(Table table) { -// StringBuilder script = new StringBuilder(); -// script.append("CREATE TABLE "); -// if(StringUtils.isNotBlank(table.getDatabaseName())) { -// script.append("`").append(table.getName()).append("`").append("."); -// } -// script.append("`").append(table.getName()).append("`").append(" (").append("\n"); -// -// // append column -// for (TableColumn column : table.getColumnList()) { -// if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType())){ -// continue; -// } -// OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(column.getColumnType()); -// script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n"); -// } -// -// // append primary key and index -// for (TableIndex tableIndex : table.getIndexList()) { -// if(StringUtils.isBlank(tableIndex.getName())|| StringUtils.isBlank(tableIndex.getType())){ -// continue; -// } -// OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); -// script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n"); -// } -// -// script = new StringBuilder(script.substring(0, script.length() - 2)); -// script.append("\n)"); -// -// -// if (StringUtils.isNotBlank(table.getEngine())) { -// script.append(" ENGINE=").append(table.getEngine()); -// } -// -// if (StringUtils.isNotBlank(table.getCharset())) { -// script.append(" DEFAULT CHARACTER SET=").append(table.getCharset()); -// } -// -// if (StringUtils.isNotBlank(table.getCollate())) { -// script.append(" COLLATE=").append(table.getCollate()); -// } -// -// if (table.getIncrementValue() != null) { -// script.append(" AUTO_INCREMENT=").append(table.getIncrementValue()); -// } -// -// if (StringUtils.isNotBlank(table.getComment())) { -// script.append(" COMMENT='").append(table.getComment()).append("'"); -// } -// -// if (StringUtils.isNotBlank(table.getPartition())) { -// script.append(" \n").append(table.getPartition()); -// } -// script.append(";"); -// -// return script.toString(); -// } -// -// @Override -// public String buildModifyTaleSql(Table oldTable, Table newTable) { -// StringBuilder script = new StringBuilder(); -// script.append("ALTER TABLE "); -// if(StringUtils.isNotBlank(oldTable.getDatabaseName())) { -// script.append("`").append(oldTable.getDatabaseName()).append("`").append("."); -// } -// script.append("`").append(oldTable.getName()).append("`").append("\n"); -// if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) { -// script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(",\n"); -// } -// if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) { -// script.append("\t").append("COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n"); -// } -// if (oldTable.getIncrementValue() != newTable.getIncrementValue()) { -// script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n"); -// } -// -// // append modify column -// for (TableColumn tableColumn : newTable.getColumnList()) { -// if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType())&& StringUtils.isNotBlank(tableColumn.getName())){ -// OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(tableColumn.getColumnType()); -// script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n"); -// } -// } -// -// // append modify index -// for (TableIndex tableIndex : newTable.getIndexList()) { -// if (StringUtils.isNotBlank(tableIndex.getEditStatus()) && StringUtils.isNotBlank(tableIndex.getType())) { -// OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); -// script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n"); -// } -// } -// if(script.length()>2) { -// script = new StringBuilder(script.substring(0, script.length() - 2)); -// script.append(";"); -// } -// -// return script.toString(); -// } -// -// -// -// @Override -// public String pageLimit(String sql, int offset, int pageNo, int pageSize) { -// StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14); -// sqlBuilder.append(sql); -// if (offset == 0) { -// sqlBuilder.append("\n LIMIT "); -// sqlBuilder.append(pageSize); -// } else { -// sqlBuilder.append("\n LIMIT "); -// sqlBuilder.append(offset); -// sqlBuilder.append(","); -// sqlBuilder.append(pageSize); -// } -// return sqlBuilder.toString(); -// } -// -// -// -// -// @Override -// public String buildCreateDatabaseSql(Database database) { -// StringBuilder sqlBuilder = new StringBuilder(); -// sqlBuilder.append("CREATE DATABASE `"+database.getName()+"`"); -// if (StringUtils.isNotBlank(database.getCharset())) { -// sqlBuilder.append(" DEFAULT CHARACTER SET=").append(database.getCharset()); -// } -// if (StringUtils.isNotBlank(database.getCollation())) { -// sqlBuilder.append(" COLLATE=").append(database.getCollation()); -// } -// return sqlBuilder.toString(); -// } -//} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java new file mode 100644 index 000000000..8ba5a2643 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java @@ -0,0 +1,55 @@ +package ai.chat2db.plugin.snowflake.builder; +import ai.chat2db.plugin.snowflake.type.SnowflakeColumnTypeEnum; +import ai.chat2db.spi.SqlBuilder; +import ai.chat2db.spi.jdbc.DefaultSqlBuilder; +import ai.chat2db.spi.model.Table; +import ai.chat2db.spi.model.TableColumn; +import ai.chat2db.spi.model.TableIndex; +import org.apache.commons.lang3.StringUtils; + +public class SnowflakeSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { + + @Override + public String buildModifyTaleSql(Table oldTable, Table newTable) { + StringBuilder script = new StringBuilder(); + script.append("ALTER TABLE "); + script.append(oldTable.getName()).append("\n"); + boolean isChangeTableName = false; + if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) { + script.append("RENAME TO ").append(newTable.getName()).append(";\n"); + isChangeTableName = true; + } + if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) { + if (isChangeTableName) { + script.append("ALTER TABLE "); + script.append("\"").append(newTable.getName()).append("\"").append("\n"); + script.append("\t").append("set COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n"); + } else { + script.append("\t").append("set COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n"); + } + } + if (oldTable.getIncrementValue() != newTable.getIncrementValue()) { + script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n"); + } + + // append modify column + for (TableColumn tableColumn : newTable.getColumnList()) { + if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType()) && StringUtils.isNotBlank(tableColumn.getName())) { + SnowflakeColumnTypeEnum typeEnum = SnowflakeColumnTypeEnum.getByType(tableColumn.getColumnType()); + script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n"); + } + } + + // append reorder column + //script.append(buildGenerateReorderColumnSql(oldTable, newTable)); + + if (script.length() > 2) { + script = new StringBuilder(script.substring(0, script.length() - 2)); + script.append(";"); + } + + return script.toString(); + } + +} + diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json index f7ed9a19c..a3f34bc72 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json @@ -14,5 +14,5 @@ "jdbcDriverClass": "com.oceanbase.jdbc.Driver" } ], - "name": "SnowFlake" + "name": "Snowflake" } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java deleted file mode 100644 index be9268d67..000000000 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseIndexTypeEnum.java +++ /dev/null @@ -1,133 +0,0 @@ -//package ai.chat2db.plugin.oceanbase.type; -// -//import ai.chat2db.spi.enums.EditStatus; -//import ai.chat2db.spi.model.IndexType; -//import ai.chat2db.spi.model.TableIndex; -//import ai.chat2db.spi.model.TableIndexColumn; -//import org.apache.commons.lang3.StringUtils; -// -//import java.util.Arrays; -//import java.util.List; -// -//public enum OceanBaseIndexTypeEnum { -// -// PRIMARY_KEY("Primary", "PRIMARY KEY"), -// -// NORMAL("Normal", "INDEX"), -// -// UNIQUE("Unique", "UNIQUE INDEX"), -// -// FULLTEXT("Fulltext", "FULLTEXT INDEX"), -// -// SPATIAL("Spatial", "SPATIAL INDEX"); -// -// public String getName() { -// return name; -// } -// -// private String name; -// -// -// public String getKeyword() { -// return keyword; -// } -// -// private String keyword; -// -// public IndexType getIndexType() { -// return indexType; -// } -// -// public void setIndexType(IndexType indexType) { -// this.indexType = indexType; -// } -// -// private IndexType indexType; -// -// OceanBaseIndexTypeEnum(String name, String keyword) { -// this.name = name; -// this.keyword = keyword; -// this.indexType = new IndexType(name); -// } -// -// -// public static OceanBaseIndexTypeEnum getByType(String type) { -// for (OceanBaseIndexTypeEnum value : OceanBaseIndexTypeEnum.values()) { -// if (value.name.equalsIgnoreCase(type)) { -// return value; -// } -// } -// return null; -// } -// -// public String buildIndexScript(TableIndex tableIndex) { -// StringBuilder script = new StringBuilder(); -// -// script.append(keyword).append(" "); -// -// script.append(buildIndexName(tableIndex)).append(" "); -// -// script.append(buildIndexColumn(tableIndex)).append(" "); -// -// script.append(buildIndexComment(tableIndex)).append(" "); -// -// return script.toString(); -// } -// -// private String buildIndexComment(TableIndex tableIndex) { -// if(StringUtils.isBlank(tableIndex.getComment())){ -// return ""; -// }else { -// return StringUtils.join("COMMENT '",tableIndex.getComment(),"'"); -// } -// -// } -// -// private String buildIndexColumn(TableIndex tableIndex) { -// StringBuilder script = new StringBuilder(); -// script.append("("); -// for (TableIndexColumn column : tableIndex.getColumnList()) { -// if(StringUtils.isNotBlank(column.getColumnName())) { -// script.append("`").append(column.getColumnName()).append("`"); -// if (!StringUtils.isBlank(column.getAscOrDesc()) && !PRIMARY_KEY.equals(this)) { -// script.append(" ").append(column.getAscOrDesc()); -// } -// script.append(","); -// } -// } -// script.deleteCharAt(script.length() - 1); -// script.append(")"); -// return script.toString(); -// } -// -// private String buildIndexName(TableIndex tableIndex) { -// if(this.equals(PRIMARY_KEY)){ -// return ""; -// }else { -// return "`"+tableIndex.getName()+"`"; -// } -// } -// -// public String buildModifyIndex(TableIndex tableIndex) { -// if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) { -// return buildDropIndex(tableIndex); -// } -// if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) { -// return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex)); -// } -// if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) { -// return StringUtils.join("ADD ", buildIndexScript(tableIndex)); -// } -// return ""; -// } -// -// private String buildDropIndex(TableIndex tableIndex) { -// if (OceanBaseIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) { -// return StringUtils.join("DROP PRIMARY KEY"); -// } -// return StringUtils.join("DROP INDEX `", tableIndex.getOldName(),"`"); -// } -// public static List getIndexTypes() { -// return Arrays.asList(OceanBaseIndexTypeEnum.values()).stream().map(OceanBaseIndexTypeEnum::getIndexType).collect(java.util.stream.Collectors.toList()); -// } -//} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCharsetEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCharsetEnum.java new file mode 100644 index 000000000..71ef84870 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCharsetEnum.java @@ -0,0 +1,26 @@ +package ai.chat2db.plugin.snowflake.type; + +import ai.chat2db.spi.model.Charset; + +import java.util.Arrays; +import java.util.List; + +public enum SnowflakeCharsetEnum { + + UTF8("utf8", "utf8_general_ci"), + ; + + private Charset charset; + SnowflakeCharsetEnum(String charsetName, String defaultCollationName) { + this.charset = new Charset(charsetName, defaultCollationName); + } + + + public Charset getCharset() { + return charset; + } + + public static List getCharsets() { + return Arrays.stream(SnowflakeCharsetEnum.values()).map(SnowflakeCharsetEnum::getCharset).collect(java.util.stream.Collectors.toList()); + } +} \ No newline at end of file diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCollationEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCollationEnum.java new file mode 100644 index 000000000..604378840 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeCollationEnum.java @@ -0,0 +1,31 @@ +package ai.chat2db.plugin.snowflake.type; + +import ai.chat2db.spi.model.Collation; + +import java.util.Arrays; +import java.util.List; + +public enum SnowflakeCollationEnum { + + BINARY("BINARY"), + + CASE_INSENSITIVE("CASE_INSENSITIVE"), + + CASE_SENSITIVE("CASE_SENSITIVE"), + ; + private Collation collation; + + SnowflakeCollationEnum(String collationName) { + this.collation = new Collation(collationName); + } + + public Collation getCollation() { + return collation; + } + + + public static List getCollations() { + return Arrays.asList(SnowflakeCollationEnum.values()).stream().map(SnowflakeCollationEnum::getCollation).collect(java.util.stream.Collectors.toList()); + } + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java similarity index 58% rename from chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java rename to chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java index baca359b1..6a24a0808 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/OceanBaseColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java @@ -1,140 +1,89 @@ -//package ai.chat2db.plugin.oceanbase.type; -// -//import ai.chat2db.spi.ColumnBuilder; -//import ai.chat2db.spi.enums.EditStatus; -//import ai.chat2db.spi.model.ColumnType; -//import ai.chat2db.spi.model.TableColumn; -//import com.google.common.collect.Maps; -//import org.apache.commons.lang3.StringUtils; -// -//import java.util.Arrays; -//import java.util.List; -//import java.util.Map; -// -//public enum OceanBaseColumnTypeEnum implements ColumnBuilder { -// -// BIT("BIT", true, false, true, false, false, false, true, true, false, false), -// -// TINYINT("TINYINT", false, false, true, true, false, false, true, true, false, false), -// -// TINYINT_UNSIGNED("TINYINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// SMALLINT("SMALLINT", false, false, true, true, false, false, true, true, false, false), -// -// SMALLINT_UNSIGNED("SMALLINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// MEDIUMINT("MEDIUMINT", false, false, true, true, false, false, true, true, false, false), -// -// MEDIUMINT_UNSIGNED("MEDIUMINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// INT("INT", false, false, true, true, false, false, true, true, false, false), -// -// -// INT_UNSIGNED("INT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// BIGINT("BIGINT", false, false, true, true, false, false, true, true, false, false), -// -// -// BIGINT_UNSIGNED("BIGINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// -// DECIMAL("DECIMAL", true, true, true, false, false, false, true, true, false, false), -// -// DECIMAL_UNSIGNED("DECIMAL UNSIGNED", true, true, true, false, false, false, true, true, false, false), -// -// -// FLOAT("FLOAT", true, true, true, false, false, false, true, true, false, false), -// -// FLOAT_UNSIGNED("FLOAT UNSIGNED", true, true, true, false, false, false, true, true, false, false), -// -// DOUBLE("DOUBLE", true, true, true, false, false, false, true, true, false, false), -// -// DOUBLE_UNSIGNED("DOUBLE UNSIGNED", true, true, true, false, false, false, true, true, false, false), -// DATE("DATE", false, false, true, false, false, false, true, true, false, false), -// DATETIME("DATETIME", true, false, true, false, false, false, true, true, true, false), -// -// TIMESTAMP("TIMESTAMP", true, false, true, false, false, false, true, true, true, false), -// TIME("TIME", true, false, true, false, false, false, true, true, false, false), -// YEAR("YEAR", false, false, true, false, false, false, true, true, false, false), -// CHAR("CHAR", true, false, true, false, true, true, true, true, false, false), -// -// VARCHAR("VARCHAR", true, false, true, false, true, true, true, true, false, false), -// -// BINARY("BINARY", true, false, true, false, false, false, true, true, false, false), -// -// VARBINARY("VARBINARY", true, false, true, false, false, false, true, true, false, false), -// -// TINYBLOB("TINYBLOB", false, false, true, false, false, false, true, false, false, false), -// -// BLOB("BLOB", false, false, true, false, false, false, true, false, false, false), -// -// MEDIUMBLOB("MEDIUMBLOB", false, false, true, false, false, false, true, false, false, false), -// -// LONGBLOB("LONGBLOB", false, false, true, false, false, false, true, false, false, false), -// -// TINYTEXT("TINYTEXT", false, false, true, false, true, true, true, false, false, false), -// -// TEXT("TEXT", false, false, true, false, true, true, true, false, false, false), -// -// MEDIUMTEXT("MEDIUMTEXT", false, false, true, false, true, true, true, false, false, false), -// -// LONGTEXT("LONGTEXT", false, false, true, false, true, true, true, false, false, false), -// -// -// ENUM("ENUM", false, false, true, false, true, true, true, true, true, true), -// -// -// BOOL("BOOL", false, false, true, true, false, false, true, true, false, false), -// -// INTEGER("INTEGER", false, false, true, true, false, false, true, true, false, false), -// -// INTEGER_UNSIGNED("INTEGER UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// REAL("REAL", true, true, true, false, false, false, true, true, false, false), -// -// SET("SET", false, false, true, false, true, true, true, true, true, true), -// -// -// GEOMETRY("GEOMETRY", false, false, true, false, false, false, true, false, false, false), -// -// POINT("POINT", false, false, true, false, false, false, true, false, false, false), -// -// LINESTRING("LINESTRING", false, false, true, false, false, false, true, false, false, false), -// -// POLYGON("POLYGON", false, false, true, false, false, false, true, false, false, false), -// -// MULTIPOINT("MULTIPOINT", false, false, true, false, false, false, true, false, false, false), -// -// MULTILINESTRING("MULTILINESTRING", false, false, true, false, false, false, true, false, false, false), -// -// MULTIPOLYGON("MULTIPOLYGON", false, false, true, false, false, false, true, false, false, false), -// -// GEOMETRYCOLLECTION("GEOMETRYCOLLECTION", false, false, true, false, false, false, true, false, false, false), -// -// JSON("JSON", false, false, true, false, false, false, true, false, false, false); -// -// private ColumnType columnType; -// -// public static OceanBaseColumnTypeEnum getByType(String dataType) { -// return COLUMN_TYPE_MAP.get(dataType.toUpperCase()); -// } -// -// public ColumnType getColumnType() { -// return columnType; -// } -// -// -// OceanBaseColumnTypeEnum(String dataTypeName, boolean supportLength, boolean supportScale, boolean supportNullable, boolean supportAutoIncrement, boolean supportCharset, boolean supportCollation, boolean supportComments, boolean supportDefaultValue, boolean supportExtent, boolean supportValue) { -// this.columnType = new ColumnType(dataTypeName, supportLength, supportScale, supportNullable, supportAutoIncrement, supportCharset, supportCollation, supportComments, supportDefaultValue, supportExtent,supportValue,false); -// } -// -// private static Map COLUMN_TYPE_MAP = Maps.newHashMap(); -// -// static { -// for (OceanBaseColumnTypeEnum value : OceanBaseColumnTypeEnum.values()) { -// COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value); -// } -// } +package ai.chat2db.plugin.snowflake.type; + +import ai.chat2db.spi.ColumnBuilder; +import ai.chat2db.spi.model.ColumnType; +import ai.chat2db.spi.model.TableColumn; +import com.google.common.collect.Maps; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +public enum SnowflakeColumnTypeEnum implements ColumnBuilder { + + NUMBER("NUMBER", true, true, true, true, false, false, true, true, false, false), + DECIMAL("DECIMAL", true, true, true, true, false, false, true, true, false, false), + NUMERIC("NUMERIC", true, true, true, true, false, false, true, true, false, false), + INT("INT", false, false, true, true, false, false, true, true, false, false), + INTEGER("INTEGER", false, false, true, true, false, false, true, true, false, false), + BIGINT("BIGINT", false, false, true, true, false, false, true, true, false, false), + SMALLINT("SMALLINT", false, false, true, true, false, false, true, true, false, false), + TINYINT("TINYINT", false, false, true, true, false, false, true, true, false, false), + BYTEINT("BYTEINT", false, false, true, true, false, false, true, true, false, false), + FLOAT("FLOAT", true, true, true, true, false, false, true, true, false, false), + DOUBLE("DOUBLE", true, true, true, true, false, false, true, true, false, false), + + VARCHAR("VARCHAR", true, false, true, false, true, true, true, true, false, false), + CHAR("CHAR", true, false, true, false, true, true, true, true, false, false), + STRING("STRING", true, false, true, false, true, true, true, true, false, false), + TEXT("TEXT", true, false, true, false, true, true, true, true, false, false), + BINARY("BINARY", true, false, true, false, true, true, true, true, false, false), + VARBINARY("VARBINARY", true, false, true, false, true, true, true, true, false, false), + + BOOLEAN("BOOLEAN", false, false, true, false, false, false, true, true, false, false), + + DATE("DATE", false, false, true, false, false, false, true, true, false, false), + DATETIME("DATETIME", true, false, true, false, false, false, true, true, true, false), + TIME("TIME", true, false, true, false, false, false, true, true, false, false), + TIMESTAMP("TIMESTAMP", true, false, true, false, false, false, true, true, true, false), + TIMESTAMP_LTZ("TIMESTAMPLTZ", true, false, true, false, false, false, true, true, true, false), + TIMESTAMP_NTZ("TIMESTAMPNTZ", true, false, true, false, false, false, true, true, true, false), + TIMESTAMP_TZ("TIMESTAMPTZ", true, false, true, false, false, false, true, true, true, false), + VARIANT("VARIANT", true, false, true, false, false, false, true, true, false, false), + OBJECT("OBJECT", true, false, true, false, false, false, true, true, false, false), + ARRAY("ARRAY", true, false, true, false, false, false, true, true, false, false), + GEOGRAPHY("GEOGRAPHY", true, false, true, false, false, false, true, true, false, false), + + ; + + private ColumnType columnType; + + public static SnowflakeColumnTypeEnum getByType(String dataType) { + return COLUMN_TYPE_MAP.get(dataType.toUpperCase()); + } + + public ColumnType getColumnType() { + return columnType; + } + + SnowflakeColumnTypeEnum(String dataTypeName, boolean supportLength, boolean supportScale, boolean supportNullable, boolean supportAutoIncrement, boolean supportCharset, boolean supportCollation, boolean supportComments, boolean supportDefaultValue, boolean supportExtent, boolean supportValue) { + this.columnType = new ColumnType(dataTypeName, supportLength, supportScale, supportNullable, supportAutoIncrement, supportCharset, supportCollation, supportComments, supportDefaultValue, supportExtent,supportValue,false); + } + + private static Map COLUMN_TYPE_MAP = Maps.newHashMap(); + + static { + for (SnowflakeColumnTypeEnum value : SnowflakeColumnTypeEnum.values()) { + COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value); + } + } + @Override + public String buildCreateColumnSql(TableColumn column) { + return null; + } + + @Override + public String buildModifyColumn(TableColumn tableColumn) { + return null; + } + + public static List getTypes(){ + return Arrays.stream(SnowflakeColumnTypeEnum.values()).map(columnTypeEnum -> + columnTypeEnum.getColumnType() + ).toList(); + } + + // // // @Override @@ -333,4 +282,4 @@ // } // // -//} +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeDefaultValueEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeDefaultValueEnum.java new file mode 100644 index 000000000..2e1279107 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeDefaultValueEnum.java @@ -0,0 +1,30 @@ +package ai.chat2db.plugin.snowflake.type; + +import ai.chat2db.spi.model.DefaultValue; + +import java.util.Arrays; +import java.util.List; + +public enum SnowflakeDefaultValueEnum { + + + NULL("NULL"), + CURRENT_DATE("CURRENT_DATE"), + CURRENT_TIMESTAMP("CURRENT_TIMESTAMP"), + ; + private DefaultValue defaultValue; + + SnowflakeDefaultValueEnum(String defaultValue) { + this.defaultValue = new DefaultValue(defaultValue); + } + + + public DefaultValue getDefaultValue() { + return defaultValue; + } + + public static List getDefaultValues() { + return Arrays.stream(SnowflakeDefaultValueEnum.values()).map(SnowflakeDefaultValueEnum::getDefaultValue).collect(java.util.stream.Collectors.toList()); + } + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin index 47b46bb12..69eb8df57 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/resources/META-INF/services/ai.chat2db.spi.Plugin @@ -1 +1 @@ -ai.chat2db.plugin.snowflake.SnowFlakePlugin \ No newline at end of file +ai.chat2db.plugin.snowflake.SnowflakePlugin \ No newline at end of file From 617f478d4ffbe9830d258d143a05cc24108ea49b Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Tue, 5 Mar 2024 18:40:46 +0800 Subject: [PATCH 4/7] =?UTF-8?q?snowflake=E5=88=9B=E5=BB=BA=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5=E3=80=81=E4=BF=AE=E6=94=B9=E8=A1=A8=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E3=80=81=E4=B8=BB=E9=94=AE=E7=AD=89=E4=BB=A3=E7=A0=81=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/snowflake/SnowflakeMetaData.java | 80 +++- .../builder/SnowflakeSqlBuilder.java | 64 +++- .../type/SnowflakeColumnTypeEnum.java | 348 ++++++++---------- .../type/SnowflakeIndexTypeEnum.java | 133 +++++++ 4 files changed, 409 insertions(+), 216 deletions(-) create mode 100644 chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeIndexTypeEnum.java diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java index bacf2ad1e..b29cd4cea 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java @@ -1,22 +1,20 @@ package ai.chat2db.plugin.snowflake; import ai.chat2db.plugin.snowflake.builder.SnowflakeSqlBuilder; -import ai.chat2db.plugin.snowflake.type.SnowflakeCharsetEnum; -import ai.chat2db.plugin.snowflake.type.SnowflakeCollationEnum; -import ai.chat2db.plugin.snowflake.type.SnowflakeColumnTypeEnum; -import ai.chat2db.plugin.snowflake.type.SnowflakeDefaultValueEnum; +import ai.chat2db.plugin.snowflake.type.*; import ai.chat2db.spi.MetaData; import ai.chat2db.spi.SqlBuilder; import ai.chat2db.spi.jdbc.DefaultMetaService; -import ai.chat2db.spi.model.Schema; -import ai.chat2db.spi.model.Table; -import ai.chat2db.spi.model.TableMeta; +import ai.chat2db.spi.model.*; import ai.chat2db.spi.sql.SQLExecutor; import ai.chat2db.spi.util.SortUtils; +import org.apache.commons.lang3.StringUtils; import java.sql.Connection; -import java.util.Arrays; -import java.util.List; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; +import java.util.stream.Collectors; public class SnowflakeMetaData extends DefaultMetaService implements MetaData { @@ -29,11 +27,6 @@ public List schemas(Connection connection, String databaseName) { return SortUtils.sortSchema(schemas, systemSchemas); } - /* @Override - public String getMetaDataName(String... names) { - return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining(".")); - }*/ - private static String VIEW_SQL = "SELECT TABLE_SCHEMA AS DatabaseName, TABLE_NAME AS ViewName, VIEW_DEFINITION AS DEFINITION, CHECK_OPTION, " + "IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_CATALOG = '%s' AND TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';"; @@ -61,6 +54,7 @@ public TableMeta getTableMeta(String databaseName, String schemaName, String tab .columnTypes(SnowflakeColumnTypeEnum.getTypes()) .charsets(SnowflakeCharsetEnum.getCharsets()) .collations(SnowflakeCollationEnum.getCollations()) + .indexTypes(SnowflakeIndexTypeEnum.getIndexTypes()) .defaultValues(SnowflakeDefaultValueEnum.getDefaultValues()) .build(); } @@ -70,4 +64,62 @@ public SqlBuilder getSqlBuilder() { return new SnowflakeSqlBuilder(); } + @Override + public String getMetaDataName(String... names) { + return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "\"" + name + "\"").collect(Collectors.joining(".")); + } + + @Override + public List indexes(Connection connection, String databaseName, String schemaName, String tableName) { + // 目前仅能查看主键 + StringBuilder queryBuf = new StringBuilder("SHOW PRIMARY KEYS in "); + queryBuf.append("\"").append(tableName).append("\""); + return SQLExecutor.getInstance().execute(connection, queryBuf.toString(), resultSet -> { + LinkedHashMap map = new LinkedHashMap(); + while (resultSet.next()) { + String keyName = resultSet.getString("constraint_name"); + TableIndex tableIndex = map.get(keyName); + if (tableIndex != null) { + List columnList = tableIndex.getColumnList(); + columnList.add(getTableIndexColumn(resultSet)); + columnList = columnList.stream().sorted(Comparator.comparing(TableIndexColumn::getOrdinalPosition)) + .collect(Collectors.toList()); + tableIndex.setColumnList(columnList); + } else { + TableIndex index = new TableIndex(); + index.setDatabaseName(databaseName); + index.setSchemaName(schemaName); + index.setTableName(tableName); + index.setName(keyName); + //index.setUnique(!resultSet.getBoolean("Non_unique")); + index.setType(SnowflakeIndexTypeEnum.PRIMARY_KEY.getName()); + index.setComment(resultSet.getString("comment")); + List tableIndexColumns = new ArrayList<>(); + tableIndexColumns.add(getTableIndexColumn(resultSet)); + index.setColumnList(tableIndexColumns); + if ("PRIMARY".equalsIgnoreCase(keyName)) { + index.setType(SnowflakeIndexTypeEnum.PRIMARY_KEY.getName()); + } + map.put(keyName, index); + } + } + return map.values().stream().collect(Collectors.toList()); + }); + } + + private TableIndexColumn getTableIndexColumn(ResultSet resultSet) throws SQLException { + TableIndexColumn tableIndexColumn = new TableIndexColumn(); + tableIndexColumn.setColumnName(resultSet.getString("column_name")); + tableIndexColumn.setOrdinalPosition(resultSet.getShort("key_sequence")); + //tableIndexColumn.setCollation(resultSet.getString("Collation")); + //tableIndexColumn.setCardinality(resultSet.getLong("Cardinality")); + //tableIndexColumn.setSubPart(resultSet.getLong("Sub_part")); + /*String collation = resultSet.getString("Collation"); + if ("a".equalsIgnoreCase(collation)) { + tableIndexColumn.setAscOrDesc("ASC"); + } else if ("d".equalsIgnoreCase(collation)) { + tableIndexColumn.setAscOrDesc("DESC"); + }*/ + return tableIndexColumn; + } } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java index 8ba5a2643..0bc6f301b 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/builder/SnowflakeSqlBuilder.java @@ -1,5 +1,6 @@ package ai.chat2db.plugin.snowflake.builder; import ai.chat2db.plugin.snowflake.type.SnowflakeColumnTypeEnum; +import ai.chat2db.plugin.snowflake.type.SnowflakeIndexTypeEnum; import ai.chat2db.spi.SqlBuilder; import ai.chat2db.spi.jdbc.DefaultSqlBuilder; import ai.chat2db.spi.model.Table; @@ -9,14 +10,73 @@ public class SnowflakeSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { + @Override + public String buildCreateTableSql(Table table){ + StringBuilder script = new StringBuilder(); + script.append("CREATE TABLE "); + if (StringUtils.isNotBlank(table.getSchemaName())) { + script.append(table.getSchemaName()).append("."); + } + script.append("\"").append(table.getName()).append("\"").append(" (").append("\n"); + + // append column + for (TableColumn column : table.getColumnList()) { + if (StringUtils.isBlank(column.getName()) || StringUtils.isBlank(column.getColumnType())) { + continue; + } + SnowflakeColumnTypeEnum typeEnum = SnowflakeColumnTypeEnum.getByType(column.getColumnType()); + script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n"); + } + + // append primary key and index + for (TableIndex tableIndex : table.getIndexList()) { + if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) { + continue; + } + SnowflakeIndexTypeEnum mysqlIndexTypeEnum = SnowflakeIndexTypeEnum.getByType(tableIndex.getType()); + script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n"); + } + + script = new StringBuilder(script.substring(0, script.length() - 2)); + script.append("\n)"); + + + if (StringUtils.isNotBlank(table.getEngine())) { + script.append(" ENGINE=").append(table.getEngine()); + } + + if (StringUtils.isNotBlank(table.getCharset())) { + script.append(" DEFAULT CHARACTER SET=").append(table.getCharset()); + } + + if (StringUtils.isNotBlank(table.getCollate())) { + script.append(" COLLATE=").append(table.getCollate()); + } + + if (table.getIncrementValue() != null) { + script.append(" AUTO_INCREMENT=").append(table.getIncrementValue()); + } + + if (StringUtils.isNotBlank(table.getComment())) { + script.append(" COMMENT='").append(table.getComment()).append("'"); + } + + if (StringUtils.isNotBlank(table.getPartition())) { + script.append(" \n").append(table.getPartition()); + } + script.append(";"); + + return script.toString(); + } + @Override public String buildModifyTaleSql(Table oldTable, Table newTable) { StringBuilder script = new StringBuilder(); script.append("ALTER TABLE "); - script.append(oldTable.getName()).append("\n"); + script.append("\"").append(oldTable.getName()).append("\"").append("\n"); boolean isChangeTableName = false; if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) { - script.append("RENAME TO ").append(newTable.getName()).append(";\n"); + script.append("RENAME TO ").append("\"").append(newTable.getName()).append("\"").append(";\n"); isChangeTableName = true; } if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) { diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java index 6a24a0808..dc8e69fba 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java @@ -1,9 +1,11 @@ package ai.chat2db.plugin.snowflake.type; import ai.chat2db.spi.ColumnBuilder; +import ai.chat2db.spi.enums.EditStatus; import ai.chat2db.spi.model.ColumnType; import ai.chat2db.spi.model.TableColumn; import com.google.common.collect.Maps; +import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; @@ -69,12 +71,156 @@ public ColumnType getColumnType() { } @Override public String buildCreateColumnSql(TableColumn column) { - return null; + + SnowflakeColumnTypeEnum type = COLUMN_TYPE_MAP.get(column.getColumnType().toUpperCase()); + if (type == null) { + return ""; + } + StringBuilder script = new StringBuilder(); + + script.append("\"").append(column.getName()).append("\"").append(" "); + + script.append(buildDataType(column, type)).append(" "); + + script.append(buildCollation(column,type)).append(" "); + + script.append(buildNullable(column,type)).append(" "); + + script.append(buildDefaultValue(column,type)).append(" "); + + script.append(buildAutoIncrement(column,type)).append(" "); + + script.append(buildComment(column,type)).append(" "); + + return script.toString(); } @Override public String buildModifyColumn(TableColumn tableColumn) { - return null; + if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) { + return StringUtils.join("DROP COLUMN ", tableColumn.getName()); + } + if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) { + return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn)); + } + if (EditStatus.MODIFY.name().equals(tableColumn.getEditStatus())) { + if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) { + return StringUtils.join("CHANGE COLUMN ", tableColumn.getOldName(), " ", buildCreateColumnSql(tableColumn)); + } else { + return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn)); + } + } + return ""; + } + + private String buildDataType(TableColumn column, SnowflakeColumnTypeEnum type) { + String columnType = type.columnType.getTypeName(); + if (EditStatus.MODIFY.name().equals(column.getEditStatus()) + && StringUtils.equalsIgnoreCase(column.getOldColumn().getTableName(), column.getTableName()) + && column.getOldColumn().getColumnSize().equals(column.getColumnSize()) + && column.getOldColumn().getDecimalDigits().equals(column.getDecimalDigits())) { + return ""; + } + if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR, STRING, TEXT).contains(type)) { + return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + } + + if (Arrays.asList(DATE, TIME, DATETIME, TIMESTAMP, TIMESTAMP_TZ, TIMESTAMP_LTZ, TIMESTAMP_LTZ).contains(type)) { + if (column.getColumnSize() == null || column.getColumnSize() == 0) { + return columnType; + } else { + return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + } + } + + if (Arrays.asList(DECIMAL, FLOAT, DOUBLE,TINYINT, INT, NUMBER, NUMERIC, INTEGER, BIGINT, SMALLINT, BYTEINT).contains(type)) { + if (column.getColumnSize() == null || column.getDecimalDigits() == null) { + return columnType; + } + if (column.getColumnSize() != null && column.getDecimalDigits() == null) { + return StringUtils.join(columnType, "(", column.getColumnSize() + ")"); + } + if (column.getColumnSize() != null && column.getDecimalDigits() != null) { + return StringUtils.join(columnType, "(", column.getColumnSize() + "," + column.getDecimalDigits() + ")"); + } + } + + return columnType; + } + + private String buildCollation(TableColumn column, SnowflakeColumnTypeEnum type) { + if(!type.getColumnType().isSupportCollation() || StringUtils.isEmpty(column.getCollationName())){ + return ""; + } + return StringUtils.join("COLLATE ", "'", column.getCollationName(), "'"); + } + + private String buildNullable(TableColumn column, SnowflakeColumnTypeEnum type) { + if(!type.getColumnType().isSupportNullable()){ + return ""; + } + if (EditStatus.MODIFY.name().equals(column.getEditStatus()) && !column.getNullable().equals(column.getOldColumn().getNullable())) { + if (column.getNullable()!=null && 1==column.getNullable()) { + return "DROP NOT NULL"; + } else { + return "NOT NULL"; + } + } else if (EditStatus.ADD.name().equals(column.getEditStatus())) { + if (column.getNullable()!=null && 1==column.getNullable()) { + return ""; + } else { + return "NOT NULL"; + } + } + return ""; + } + + private String buildDefaultValue(TableColumn column, SnowflakeColumnTypeEnum type) { + if(!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())){ + return ""; + } + + if("EMPTY_STRING".equalsIgnoreCase(column.getDefaultValue().trim())){ + return StringUtils.join("SET DEFAULT ''"); + } + + if("NULL".equalsIgnoreCase(column.getDefaultValue().trim())){ + return StringUtils.join("SET DEFAULT NULL"); + } + + if(Arrays.asList(CHAR,VARCHAR,BINARY,VARBINARY).contains(type)){ + return StringUtils.join("SET DEFAULT '",column.getDefaultValue(),"'"); + } + + if(Arrays.asList(DATE,TIME).contains(type)){ + return StringUtils.join("SET DEFAULT '",column.getDefaultValue(),"'"); + } + + if(Arrays.asList(DATETIME,TIMESTAMP).contains(type)){ + if("CURRENT_TIMESTAMP".equalsIgnoreCase(column.getDefaultValue().trim())){ + return StringUtils.join("SET DEFAULT ",column.getDefaultValue()); + } + return StringUtils.join("SET DEFAULT '",column.getDefaultValue(),"'"); + } + + return StringUtils.join("SET DEFAULT ",column.getDefaultValue()); + } + + private String buildAutoIncrement(TableColumn column, SnowflakeColumnTypeEnum type) { + if(!type.getColumnType().isSupportAutoIncrement()){ + return ""; + } + if (column.getAutoIncrement() != null && column.getAutoIncrement()) { + return "identity"; + } + return ""; + } + + private String buildComment(TableColumn column, SnowflakeColumnTypeEnum type) { + if(!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())){ + return ""; + } + return StringUtils.join("COMMENT '",column.getComment(),"'"); } public static List getTypes(){ @@ -84,202 +230,4 @@ public static List getTypes(){ } -// -// -// @Override -// public String buildCreateColumnSql(TableColumn column) { -// OceanBaseColumnTypeEnum type = COLUMN_TYPE_MAP.get(column.getColumnType().toUpperCase()); -// if (type == null) { -// return ""; -// } -// StringBuilder script = new StringBuilder(); -// -// script.append("`").append(column.getName()).append("`").append(" "); -// -// script.append(buildDataType(column, type)).append(" "); -// -// script.append(buildCharset(column,type)).append(" "); -// -// script.append(buildCollation(column,type)).append(" "); -// -// script.append(buildNullable(column,type)).append(" "); -// -// script.append(buildDefaultValue(column,type)).append(" "); -// -// script.append(buildExt(column,type)).append(" "); -// -// script.append(buildAutoIncrement(column,type)).append(" "); -// -// script.append(buildComment(column,type)).append(" "); -// -// return script.toString(); -// } -// -// private String buildCharset(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportCharset() || StringUtils.isEmpty(column.getCharSetName())){ -// return ""; -// } -// return StringUtils.join("CHARACTER SET ",column.getCharSetName()); -// } -// -// private String buildCollation(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportCollation() || StringUtils.isEmpty(column.getCollationName())){ -// return ""; -// } -// return StringUtils.join("COLLATE ",column.getCollationName()); -// } -// -// @Override -// public String buildModifyColumn(TableColumn tableColumn) { -// -// if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) { -// return StringUtils.join("DROP COLUMN `", tableColumn.getName() + "`"); -// } -// if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) { -// return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn)); -// } -// if (EditStatus.MODIFY.name().equals(tableColumn.getEditStatus())) { -// if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) { -// return StringUtils.join("CHANGE COLUMN `", tableColumn.getOldName(), "` ", buildCreateColumnSql(tableColumn)); -// } else { -// return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn)); -// } -// } -// return ""; -// } -// -// private String buildAutoIncrement(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportAutoIncrement()){ -// return ""; -// } -// if (column.getAutoIncrement() != null && column.getAutoIncrement()) { -// return "AUTO_INCREMENT"; -// } -// return ""; -// } -// -// private String buildComment(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())){ -// return ""; -// } -// return StringUtils.join("COMMENT '",column.getComment(),"'"); -// } -// -// private String buildExt(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.columnType.isSupportExtent() || StringUtils.isEmpty(column.getExtent())){ -// return ""; -// } -// return column.getComment(); -// } -// -// private String buildDefaultValue(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())){ -// return ""; -// } -// -// if("EMPTY_STRING".equalsIgnoreCase(column.getDefaultValue().trim())){ -// return StringUtils.join("DEFAULT ''"); -// } -// -// if("NULL".equalsIgnoreCase(column.getDefaultValue().trim())){ -// return StringUtils.join("DEFAULT NULL"); -// } -// -// if(Arrays.asList(CHAR,VARCHAR,BINARY,VARBINARY, SET,ENUM).contains(type)){ -// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); -// } -// -// if(Arrays.asList(DATE,TIME,YEAR).contains(type)){ -// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); -// } -// -// if(Arrays.asList(DATETIME,TIMESTAMP).contains(type)){ -// if("CURRENT_TIMESTAMP".equalsIgnoreCase(column.getDefaultValue().trim())){ -// return StringUtils.join("DEFAULT ",column.getDefaultValue()); -// } -// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); -// } -// -// return StringUtils.join("DEFAULT ",column.getDefaultValue()); -// } -// -// private String buildNullable(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportNullable()){ -// return ""; -// } -// if (column.getNullable()!=null && 1==column.getNullable()) { -// return "NULL"; -// } else { -// return "NOT NULL"; -// } -// } -// -// private String buildDataType(TableColumn column, OceanBaseColumnTypeEnum type) { -// String columnType = type.columnType.getTypeName(); -// if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR).contains(type)) { -// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); -// } -// -// if (BIT.equals(type)) { -// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); -// } -// -// if (Arrays.asList(TIME, DATETIME, TIMESTAMP).contains(type)) { -// if (column.getColumnSize() == null || column.getColumnSize() == 0) { -// return columnType; -// } else { -// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); -// } -// } -// -// -// if (Arrays.asList(DECIMAL, FLOAT, DOUBLE).contains(type)) { -// if (column.getColumnSize() == null || column.getDecimalDigits() == null) { -// return columnType; -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() == null) { -// return StringUtils.join(columnType, "(", column.getColumnSize() + ")"); -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() != null) { -// return StringUtils.join(columnType, "(", column.getColumnSize() + "," + column.getDecimalDigits() + ")"); -// } -// } -// -// if (Arrays.asList(DECIMAL_UNSIGNED, FLOAT_UNSIGNED, DECIMAL_UNSIGNED).contains(type)) { -// if (column.getColumnSize() == null || column.getDecimalDigits() == null) { -// return columnType; -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() == null) { -// return unsignedDataType(columnType, "(" + column.getColumnSize() + ")"); -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() != null) { -// return unsignedDataType(columnType, "(" + column.getColumnSize() + "," + column.getDecimalDigits() + ")"); -// } -// } -// -// if(Arrays.asList(SET,ENUM).contains(type)){ -// if(!StringUtils.isEmpty( column.getValue())){ -// return StringUtils.join(columnType,"(",column.getValue(),")"); -// } -// //List enumList = column. -// } -// -// return columnType; -// } -// -// private String unsignedDataType(String dataTypeName, String middle) { -// String[] split = dataTypeName.split(" "); -// if (split.length == 2) { -// return StringUtils.join(split[0], middle, split[1]); -// } -// return StringUtils.join(dataTypeName, middle); -// } -// -// public static List getTypes(){ -// return Arrays.stream(OceanBaseColumnTypeEnum.values()).map(columnTypeEnum -> -// columnTypeEnum.getColumnType() -// ).toList(); -// } -// -// } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeIndexTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeIndexTypeEnum.java new file mode 100644 index 000000000..e93bb1f42 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeIndexTypeEnum.java @@ -0,0 +1,133 @@ +package ai.chat2db.plugin.snowflake.type; + +import ai.chat2db.spi.enums.EditStatus; +import ai.chat2db.spi.model.IndexType; +import ai.chat2db.spi.model.TableIndex; +import ai.chat2db.spi.model.TableIndexColumn; +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.List; + +public enum SnowflakeIndexTypeEnum { + + PRIMARY_KEY("Primary", "PRIMARY KEY"), + + NORMAL("Normal", "INDEX"), + + UNIQUE("Unique", "UNIQUE INDEX"), + + FULLTEXT("Fulltext", "FULLTEXT INDEX"), + + SPATIAL("Spatial", "SPATIAL INDEX"); + + public String getName() { + return name; + } + + private String name; + + + public String getKeyword() { + return keyword; + } + + private String keyword; + + public IndexType getIndexType() { + return indexType; + } + + public void setIndexType(IndexType indexType) { + this.indexType = indexType; + } + + private IndexType indexType; + + SnowflakeIndexTypeEnum(String name, String keyword) { + this.name = name; + this.keyword = keyword; + this.indexType = new IndexType(name); + } + + + public static SnowflakeIndexTypeEnum getByType(String type) { + for (SnowflakeIndexTypeEnum value : SnowflakeIndexTypeEnum.values()) { + if (value.name.equalsIgnoreCase(type)) { + return value; + } + } + return null; + } + + public String buildIndexScript(TableIndex tableIndex) { + StringBuilder script = new StringBuilder(); + + script.append(keyword).append(" "); + + script.append(buildIndexName(tableIndex)).append(" "); + + script.append(buildIndexColumn(tableIndex)).append(" "); + + script.append(buildIndexComment(tableIndex)).append(" "); + + return script.toString(); + } + + private String buildIndexComment(TableIndex tableIndex) { + if(StringUtils.isBlank(tableIndex.getComment())){ + return ""; + }else { + return StringUtils.join("COMMENT '",tableIndex.getComment(),"'"); + } + + } + + private String buildIndexColumn(TableIndex tableIndex) { + StringBuilder script = new StringBuilder(); + script.append("("); + for (TableIndexColumn column : tableIndex.getColumnList()) { + if(StringUtils.isNotBlank(column.getColumnName())) { + script.append("\"").append(column.getColumnName()).append("\""); + if (!StringUtils.isBlank(column.getAscOrDesc()) && !PRIMARY_KEY.equals(this)) { + script.append(" ").append(column.getAscOrDesc()); + } + script.append(","); + } + } + script.deleteCharAt(script.length() - 1); + script.append(")"); + return script.toString(); + } + + private String buildIndexName(TableIndex tableIndex) { + if(this.equals(PRIMARY_KEY)){ + return ""; + }else { + return "\""+tableIndex.getName()+"\""; + } + } + + public String buildModifyIndex(TableIndex tableIndex) { + if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) { + return buildDropIndex(tableIndex); + } + if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) { + return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex)); + } + if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) { + return StringUtils.join("ADD ", buildIndexScript(tableIndex)); + } + return ""; + } + + private String buildDropIndex(TableIndex tableIndex) { + if (SnowflakeIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) { + return StringUtils.join("DROP PRIMARY KEY"); + } + return StringUtils.join("DROP INDEX \"", tableIndex.getOldName(),"\""); + } + public static List getIndexTypes() { + return Arrays.asList(SnowflakeIndexTypeEnum.values()).stream().map(SnowflakeIndexTypeEnum::getIndexType).collect(java.util.stream.Collectors.toList()); + } +} From c75f16869557c4cc736b84d09acff1df23565495 Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Tue, 5 Mar 2024 23:30:48 +0800 Subject: [PATCH 5/7] =?UTF-8?q?snowflake=E4=BF=AE=E6=94=B9=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E9=A9=B1=E5=8A=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/assets/img/databaseImg/snowflake.png | Bin 0 -> 11791 bytes chat2db-client/src/constants/database.ts | 3 ++- .../plugin/snowflake/SnowflakeMetaData.java | 18 ++++++++++++++++++ .../chat2db/plugin/snowflake/snowflake.json | 6 +++--- 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 chat2db-client/src/assets/img/databaseImg/snowflake.png diff --git a/chat2db-client/src/assets/img/databaseImg/snowflake.png b/chat2db-client/src/assets/img/databaseImg/snowflake.png new file mode 100644 index 0000000000000000000000000000000000000000..b215e1cb279fc0fdcd85d86103df2b3b9f5b8f40 GIT binary patch literal 11791 zcmX}Sb9`J~v_Bk7W80ZHX{^SW7>#Y)4H`9#ZL6`_*l29qw(b1px%a;BA9K!p=FHhx zd!2pO7h#I>lE?@I2oMku$kI|`%HZ?Re;+s)@Uc>}DhqsqbXJxWfvB1wI);D%LP(2& zRNeK@^{jk})EYkI=P4=C+=g9TF#rHEJ>ouC{;Xz+HHNM+3`uBn$jK_Fc$np4%(>Iv!@7#a@6I|;dvT-f~k3Hvd;W^LRB{V zq4(jM^W|dGMWxpy8Ap~8u3y-w!7SDwk1rO{R${A=aCqyAQ3aX$!rrV&L~qqUvTvax zefGkVM1FnoYj}~%r(dD|@T6LqGmtgB+Nh5Pa#6TdGmUe?Dd(V zq4yO)zLoS&V1|oK9w=Y8+2kH#F1wOaFbO~y7R!9FC_+8WwWD`c#-Bxhe8PZ&@gQ5Z zQjw?L5CZndXivVEo1I0>6MToSb42j7zqihoes>q-d_dK|ccXlU0l^%di}W;^&Qh&Avin+D2(+#U+kZchnoaWFMtTh{zy~Ptya~rECZ)_ zKxM&1S>5cP38f>)w^o@6)s5bd5tD^bsa0aJZ3#o znXs`|Jf-1>+|~w!U0eD}MwCc)BuE0l@nWfTog;sMI^fs>rfPz4MI6Mkb|TLa@tNyO z>P#=xOyt_Z-s;iCnP4UASg{>61sqZ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|t?V_@_MP zIvP>GNLg`kzb7ahAN1cEK(QYHkDT%}E}s5yrE*GPO}oPSu%tfI_b&aI<5BD1soY>o zBIbIamZn^euykD7^MW_;M?VW+m5|$iLR8eUR|mkWYJ^nkuL>7AeWp9?O={Zf_^nUobMM*FoR=r+Re&4^kH)03TTPKc)&S&l$;X(t4zGO;S%~)&FrPozb7>Knpr0 zp`&^q%gBgY!xeyHLA;`mS*!wyeUtiwGBfpytXt&g2C9dNiG)trcd#0C>?kK+Kbq!um@MKX-PLSwc>91XUqHXqGzw5v!$pnYMAp&S^6 z5J2Fb { + if (resultSet.next()) { + return resultSet.getString("Create Table"); + } + return null; + }); + } + + public static String format(String tableName) { + return "\"" + tableName + "\""; + } } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json index a3f34bc72..82aea66c0 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/snowflake.json @@ -8,10 +8,10 @@ "custom": false, "defaultDriver": true, "downloadJdbcDriverUrls": [ - "https://oss.sqlgpt.cn/lib/oceanbase-client-2.4.2.jar" + "https://oss.sqlgpt.cn/lib/snowflake-jdbc-3.15.0.jar" ], - "jdbcDriver": "oceanbase-client-2.4.2.jar", - "jdbcDriverClass": "com.oceanbase.jdbc.Driver" + "jdbcDriver": "snowflake-jdbc-3.15.0.jar", + "jdbcDriverClass": "net.snowflake.client.jdbc.SnowflakeDriver" } ], "name": "Snowflake" From 307a54d68aff2a949c49f68ba29293039d88c3ab Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Wed, 6 Mar 2024 16:31:13 +0800 Subject: [PATCH 6/7] =?UTF-8?q?snowflake=E5=A2=9E=E5=8A=A0funcitons?= =?UTF-8?q?=E3=80=81=E5=88=A0=E9=99=A4=E8=A1=A8=E5=8A=9F=E8=83=BD=E3=80=82?= =?UTF-8?q?=20=E8=A7=A3=E6=9E=90SQL=E6=97=B6=E5=A2=9E=E5=8A=A0=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=B8=BAMONGODB=E3=80=81SN?= =?UTF-8?q?OWFLAKE=E5=88=99=E4=B8=8D=E8=B5=B0druid=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E3=80=82=E9=81=BF=E5=85=8D=E6=97=A5=E5=BF=97=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=8A=A5=E9=94=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/snowflake/SnowflakeDBManage.java | 10 ++++ .../plugin/snowflake/SnowflakeMetaData.java | 48 ++++++++++++++++++- .../core/impl/DlTemplateServiceImpl.java | 4 +- .../tools/base/enums/DataSourceTypeEnum.java | 5 ++ 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java index 6448c473f..603a77ab4 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManage.java @@ -60,4 +60,14 @@ public void connectDatabase(Connection connection, String database) { } } + @Override + public void dropTable(Connection connection, String databaseName, String schemaName, String tableName) { + String sql = "DROP TABLE "+ format(tableName); + SQLExecutor.getInstance().executeSql(connection,sql, resultSet -> null); + } + + public static String format(String tableName) { + return "\"" + tableName + "\""; + } + } diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java index 08b2b5842..5baa38e2d 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeMetaData.java @@ -127,14 +127,60 @@ private TableIndexColumn getTableIndexColumn(ResultSet resultSet) throws SQLExce @Override public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName, @NotEmpty String tableName) { - String sql = "SHOW CREATE TABLE " + format(schemaName) + "." + // 需要后续自己实现。目前没有办法直接获取建表语句。 + return ""; + /*String sql = "SHOW CREATE TABLE " + format(schemaName) + "." + format(tableName); return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { if (resultSet.next()) { return resultSet.getString("Create Table"); } return null; + });*/ + } + + private static String OBJECT_SQL + = "SHOW USER FUNCTIONS IN SCHEMA \"%s\""; + + @Override + public List functions(Connection connection, String databaseName, String schemaName) { + List functions = new ArrayList<>(); + String sql = String.format(OBJECT_SQL, schemaName); + return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { + while (resultSet.next()) { + Function function = new Function(); + function.setDatabaseName(databaseName); + function.setSchemaName(schemaName); + function.setFunctionName(resultSet.getString("name")); + functions.add(function); + } + return functions; + }); + } + + private static String ROUTINES_SQL + = + "SELECT FUNCTION_NAME, FUNCTION_DEFINITION, COMMENT " + + "FROM INFORMATION_SCHEMA.FUNCTIONS " + + "WHERE FUNCTION_SCHEMA = '%s' AND FUNCTION_NAME = '%s';"; + @Override + public Function function(Connection connection, @NotEmpty String databaseName, String schemaName, + String functionName) { + + String sql = String.format(ROUTINES_SQL, schemaName, functionName); + return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { + Function function = new Function(); + function.setDatabaseName(databaseName); + function.setSchemaName(schemaName); + function.setFunctionName(functionName); + if (resultSet.next()) { + function.setSpecificName(resultSet.getString("FUNCTION_NAME")); + function.setRemarks(resultSet.getString("COMMENT")); + function.setFunctionBody(resultSet.getString("FUNCTION_DEFINITION")); + } + return function; }); + } public static String format(String tableName) { diff --git a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java index 3e6aff9e3..1dbd093a5 100644 --- a/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java +++ b/chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/impl/DlTemplateServiceImpl.java @@ -145,7 +145,9 @@ private ExecuteResult executeSQL(String originalSql, DbType dbType, DlExecutePar String sqlType = SqlTypeEnum.UNKNOWN.getCode(); // 解析sql String type = Chat2DBContext.getConnectInfo().getDbType(); - boolean supportDruid = !DataSourceTypeEnum.MONGODB.getCode().equals(type); + //boolean supportDruid = !DataSourceTypeEnum.MONGODB.getCode().equals(type); + boolean supportDruid = !Arrays.asList(DataSourceTypeEnum.MONGODB.getCode(), DataSourceTypeEnum.SNOEFLAKE.getCode()).contains(type); + // 解析sql分页 SQLStatement sqlStatement = null; if (supportDruid) { diff --git a/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java b/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java index 7a9cbcc64..6df20198b 100644 --- a/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java +++ b/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java @@ -26,6 +26,11 @@ public enum DataSourceTypeEnum implements BaseEnum { */ MONGODB("mongo数据库连接"), + /** + * snowflake数据库连接 + */ + SNOEFLAKE("snowflake数据库连接"), + ; final String description; From 0574c4e51e0d8e45235a6be77f96442d2dce8135 Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Wed, 6 Mar 2024 18:19:17 +0800 Subject: [PATCH 7/7] =?UTF-8?q?snowflake=E5=AD=97=E7=AC=A6=E7=B1=BB?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=94=A8=E6=88=B7=E4=B8=8D=E5=A1=AB=E5=86=99?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A8=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E9=85=8D=E7=BD=AE=E9=95=BF=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/snowflake/type/SnowflakeColumnTypeEnum.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java index dc8e69fba..0b12a09f3 100644 --- a/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-snowflake/src/main/java/ai/chat2db/plugin/snowflake/type/SnowflakeColumnTypeEnum.java @@ -122,7 +122,11 @@ private String buildDataType(TableColumn column, SnowflakeColumnTypeEnum type) { return ""; } if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR, STRING, TEXT).contains(type)) { - return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + if (column.getColumnSize() == null || column.getColumnSize() == 0){ + return columnType; + } else { + return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + } } if (Arrays.asList(DATE, TIME, DATETIME, TIMESTAMP, TIMESTAMP_TZ, TIMESTAMP_LTZ, TIMESTAMP_LTZ).contains(type)) {