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 Xugudb #1429

Open
wants to merge 5 commits into
base: dev
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
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 @@ -2080,4 +2080,125 @@ export const dataSourceFormConfigs: IConnectionConfig[] = [
extendInfo: [],
type: DatabaseTypeCode.MONGODB
},
//MONGODB
{
baseInfo: {
items: [
{
defaultValue: '@localhost',
inputType: InputType.INPUT,
labelNameCN: '名称',
labelNameEN: 'Name',
name: 'alias',
required: true,
styles: {
width: '100%',
}
},
envItem,
{
defaultValue: 'localhost',
inputType: InputType.INPUT,
labelNameCN: '主机',
labelNameEN: 'Host',
name: 'host',
required: true,
styles: {
width: '70%',
}
},
{
defaultValue: '5138',
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:xugu://localhost:5138',
inputType: InputType.INPUT,
labelNameCN: 'URL',
labelNameEN: 'URL',
name: 'url',
required: true,
styles: {
width: '100%',
}
},

],
pattern: /jdbc:xugu:\/\/(.*):(\d+)(\/(\w+))?/,
template: 'jdbc:xugu://{host}:{port}/{database}',
//excludes: [OperationColumn.ViewDDL, OperationColumn.CreateTable,OperationColumn.EditTable]
},
ssh: sshConfig,
extendInfo: [],
type: DatabaseTypeCode.XUGUDB
},
];
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",
XUGUDB = "XUGUDB",
}

export enum ConsoleStatus {
Expand Down
7 changes: 7 additions & 0 deletions chat2db-client/src/constants/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ export const databaseMap: {
// port: 27017,
icon: '\uec21',
},
[DatabaseTypeCode.XUGUDB]: {
name: 'XuguDB',
img: moreDBLogo,
code: DatabaseTypeCode.XUGUDB,
// port: 5138,
icon: '\uec21',
},
// [DatabaseTypeCode.REDIS]: {
// name: 'Redis',
// img: moreDBLogo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String tableDDL(Connection connection, @NotEmpty String databaseName, Str

@Override
public String getMetaDataName(String... names) {
return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining("."));
return Arrays.stream(names).skip(1).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining("."));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@

import ai.chat2db.plugin.hive.type.HiveColumnTypeEnum;
import ai.chat2db.plugin.hive.type.HiveIndexTypeEnum;
import ai.chat2db.spi.SqlBuilder;
import ai.chat2db.spi.jdbc.DefaultSqlBuilder;
import ai.chat2db.spi.model.Database;
import ai.chat2db.spi.model.Table;
import ai.chat2db.spi.model.TableColumn;
import ai.chat2db.spi.model.TableIndex;
import cn.hutool.core.util.ArrayUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class HiveSqlBuilder extends DefaultSqlBuilder implements SqlBuilder {
public class HiveSqlBuilder extends DefaultSqlBuilder {
@Override
public String buildCreateTableSql(Table table) {
StringBuilder script = new StringBuilder();
Expand Down Expand Up @@ -117,7 +111,7 @@ public String buildModifyTaleSql(Table oldTable, Table newTable) {
}

// append reorder column
script.append(buildGenerateReorderColumnSql(oldTable, newTable));
// script.append(buildGenerateReorderColumnSql(oldTable, newTable));

if (script.length() > 2) {
script = new StringBuilder(script.substring(0, script.length() - 2));
Expand Down Expand Up @@ -156,142 +150,4 @@ public String buildCreateDatabaseSql(Database database) {
return sqlBuilder.toString();
}

public String buildGenerateReorderColumnSql(Table oldTable, Table newTable) {
StringBuilder sql = new StringBuilder();
int n = 0;
// Create a map to store the index of each column in the old table's column list
Map<String, Integer> oldColumnIndexMap = new HashMap<>();
for (int i = 0; i < oldTable.getColumnList().size(); i++) {
oldColumnIndexMap.put(oldTable.getColumnList().get(i).getName(), i);
}
String[] oldColumnArray = oldTable.getColumnList().stream().map(TableColumn::getName).toArray(String[]::new);
String[] newColumnArray = newTable.getColumnList().stream().map(TableColumn::getName).toArray(String[]::new);

buildSql(oldColumnArray, newColumnArray, sql, oldTable, newTable, n);

return sql.toString();
}

private String[] buildSql(String[] originalArray, String[] targetArray, StringBuilder sql, Table oldTable, Table newTable, int n) {
// 先完成首位移动
if (!originalArray[0].equals(targetArray[0])) {
int a = findIndex(originalArray, targetArray[0]);
TableColumn column = oldTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getName(), originalArray[a])).findFirst().get();
String[] newArray = moveElement(originalArray, a, 0);
System.out.println(ArrayUtil.toString(newArray));
sql.append(" MODIFY COLUMN ");
HiveColumnTypeEnum typeEnum = HiveColumnTypeEnum.getByType(column.getColumnType());
sql.append(typeEnum.buildColumn(column));
sql.append(" FIRST;\n");
n++;
if (Arrays.equals(newArray, targetArray)) {
return newArray;
}
String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n);
if (Arrays.equals(resultArray, targetArray)) {
return resultArray;
}
}

// 在完成最后一位移动
int max = originalArray.length - 1;
if (!originalArray[max].equals(targetArray[max])) {
int a = findIndex(originalArray, targetArray[max]);
//System.out.println("Move " + originalArray[a] + " after " + (a > 0 ? originalArray[max] : "start"));
TableColumn column = oldTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getName(), originalArray[a])).findFirst().get();
String[] newArray = moveElement(originalArray, a, max);
System.out.println(ArrayUtil.toString(newArray));
if (n > 0) {
sql.append("ALTER TABLE ");
if (StringUtils.isNotBlank(oldTable.getDatabaseName())) {
sql.append("`").append(oldTable.getDatabaseName()).append("`").append(".");
}
sql.append("`").append(oldTable.getName()).append("`").append("\n");
}
sql.append(" MODIFY COLUMN ");
HiveColumnTypeEnum typeEnum = HiveColumnTypeEnum.getByType(column.getColumnType());
sql.append(typeEnum.buildColumn(column));
sql.append(" ");
sql.append(" AFTER ");
sql.append(oldTable.getColumnList().get(max).getName());
sql.append(";\n");
n++;
if (Arrays.equals(newArray, targetArray)) {
return newArray;
}
String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n);
if (Arrays.equals(resultArray, targetArray)) {
return resultArray;
}
}


for (int i = 0; i < originalArray.length; i++) {
int a = findIndex(targetArray, originalArray[i]);
if (i != a && isMoveValid(originalArray, targetArray, i, a)) {
// oldTable.getColumnList中查找name为a
int finalI = i;
TableColumn column = oldTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getName(), originalArray[finalI])).findFirst().get();
if (n > 0) {
sql.append("ALTER TABLE ");
if (StringUtils.isNotBlank(oldTable.getDatabaseName())) {
sql.append("`").append(oldTable.getDatabaseName()).append("`").append(".");
}
sql.append("`").append(oldTable.getName()).append("`").append("\n");
}
sql.append(" MODIFY COLUMN ");
HiveColumnTypeEnum typeEnum = HiveColumnTypeEnum.getByType(column.getColumnType());
sql.append(typeEnum.buildColumn(column));
sql.append(" ");
sql.append(" AFTER ");
if (i < a) {
sql.append(originalArray[a]);
} else {
sql.append(originalArray[a - 1]);
}

sql.append(";\n");
n++;
String[] newArray = moveElement(originalArray, i, a);
if (Arrays.equals(newArray, targetArray)) {
return newArray;
}
String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n);
if (Arrays.equals(resultArray, targetArray)) {
return resultArray;
}
}
}
return null;
}

private static int findIndex(String[] array, String element) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(element)) {
return i;
}
}
return -1;
}

private static boolean isMoveValid(String[] originalArray, String[] targetArray, int i, int a) {
System.out.println("i : " + i + " a:" + a);
return (i == 0 || a == 0 || !originalArray[i - 1].equals(targetArray[a - 1])) &&
(i >= originalArray.length - 1 || a >= targetArray.length - 1 || !originalArray[i + 1].equals(targetArray[a + 1]));
}

private static String[] moveElement(String[] originalArray, int from, int to) {
String[] newArray = new String[originalArray.length];
System.arraycopy(originalArray, 0, newArray, 0, originalArray.length);
String temp = newArray[from];
if (from < to) {
System.arraycopy(originalArray, from + 1, newArray, from, to - from);
} else {
System.arraycopy(originalArray, to, newArray, to + 1, from - to);
}
newArray[to] = temp;
System.out.println(ArrayUtil.toString(newArray));
return newArray;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public String buildModifyColumn(TableColumn tableColumn) {
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 StringUtils.join("CHANGE `", tableColumn.getOldName(), "` ", buildCreateColumnSql(tableColumn));
}
}
return "";
Expand Down
35 changes: 35 additions & 0 deletions chat2db-server/chat2db-plugins/chat2db-xugudb/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-xugudb</artifactId>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<!--The properties configuration file will be placed together with the compiled class file-->
<include>**/*.json</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
Loading