Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new database Snowflake #1202

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions chat2db-client/src/components/ConnectionEdit/config/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
];
1 change: 1 addition & 0 deletions chat2db-client/src/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum DatabaseTypeCode {
PRESTO = "PRESTO",
HIVE = "HIVE",
KINGBASE = "KINGBASE",
SNOWFLAKE = "SNOWFLAKE",
}

export enum ConsoleStatus {
Expand Down
8 changes: 8 additions & 0 deletions chat2db-client/src/constants/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mysqlLogo from '@/assets/img/databaseImg/mysql.png';
import redisLogo from '@/assets/img/databaseImg/redis.png';
import h2Logo from '@/assets/img/databaseImg/h2.png';
import moreDBLogo from '@/assets/img/databaseImg/other.png';
import snowflakeLogo from '@/assets/img/databaseImg/snowflake.png';
import { IDatabase } from '@/typings';
import { DatabaseTypeCode } from '@/constants'

Expand Down Expand Up @@ -118,6 +119,13 @@ export const databaseMap: {
// port: 27017,
icon: '\uec21',
},
[DatabaseTypeCode.SNOWFLAKE]: {
name: 'Snowflake',
img: snowflakeLogo,
code: DatabaseTypeCode.SNOWFLAKE,
// port: 443,
icon: '\uec21',
},
// [DatabaseTypeCode.REDIS]: {
// name: 'Redis',
// img: moreDBLogo,
Expand Down
35 changes: 35 additions & 0 deletions chat2db-server/chat2db-plugins/chat2db-snowflake/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ai.chat2db</groupId>
<artifactId>chat2db-plugins</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>ai.chat2db</groupId>
<artifactId>chat2db-spi</artifactId>
</dependency>
</dependencies>

<artifactId>chat2db-snowflake</artifactId>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<!--properties的配置文件会和编译后的class文件放在一起-->
<include>**/*.json</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ai.chat2db.plugin.snowflake;

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<KeyValue> 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);
}
}
}

@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 + "\"";
}

}
Loading