mirror of
https://gitee.com/dromara/dbswitch.git
synced 2025-10-14 05:40:23 +00:00
修复sr无主键情况
This commit is contained in:
@@ -81,16 +81,19 @@ public final class GenerateSqlUtils {
|
||||
// StarRocks 当中,字段主键的情况下,必须将字段放在最前面,并且顺序一致。
|
||||
if (type.isPrimaryKeyShouldAtFirst()) {
|
||||
List<ColumnDescription> copyFieldNames = new ArrayList<>();
|
||||
List<String> copyPrimaryKeys = new ArrayList<>();
|
||||
Integer fieldIndex = 0;
|
||||
for (int i = 0; i < fieldNames.size(); i++) {
|
||||
ColumnDescription cd = fieldNames.get(i);
|
||||
if (primaryKeys.contains(cd.getFieldName())) {
|
||||
copyFieldNames.add(fieldIndex++, cd);
|
||||
copyPrimaryKeys.add(cd.getFieldName());
|
||||
} else {
|
||||
copyFieldNames.add(cd);
|
||||
}
|
||||
}
|
||||
fieldNames = copyFieldNames;
|
||||
pks = copyPrimaryKeys;
|
||||
}
|
||||
|
||||
for (int i = 0; i < fieldNames.size(); i++) {
|
||||
|
@@ -9,14 +9,6 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package org.dromara.dbswitch.product.doris;
|
||||
|
||||
import org.dromara.dbswitch.common.consts.Constants;
|
||||
import org.dromara.dbswitch.core.provider.ProductFactoryProvider;
|
||||
import org.dromara.dbswitch.core.provider.meta.AbstractMetadataProvider;
|
||||
import org.dromara.dbswitch.core.schema.ColumnDescription;
|
||||
import org.dromara.dbswitch.core.schema.ColumnMetaData;
|
||||
import org.dromara.dbswitch.core.schema.IndexDescription;
|
||||
import org.dromara.dbswitch.core.schema.SourceProperties;
|
||||
import org.dromara.dbswitch.core.schema.TableDescription;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@@ -30,6 +22,14 @@ import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dromara.dbswitch.common.consts.Constants;
|
||||
import org.dromara.dbswitch.core.provider.ProductFactoryProvider;
|
||||
import org.dromara.dbswitch.core.provider.meta.AbstractMetadataProvider;
|
||||
import org.dromara.dbswitch.core.schema.ColumnDescription;
|
||||
import org.dromara.dbswitch.core.schema.ColumnMetaData;
|
||||
import org.dromara.dbswitch.core.schema.IndexDescription;
|
||||
import org.dromara.dbswitch.core.schema.SourceProperties;
|
||||
import org.dromara.dbswitch.core.schema.TableDescription;
|
||||
|
||||
@Slf4j
|
||||
public class DorisMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
@@ -251,12 +251,13 @@ public class DorisMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
|
||||
String retval = " `" + fieldname + "` ";
|
||||
|
||||
// https://doris.apache.org/zh-CN/docs/table-design/data-type
|
||||
switch (type) {
|
||||
case ColumnMetaData.TYPE_TIMESTAMP:
|
||||
retval += "DATETIME";
|
||||
break;
|
||||
case ColumnMetaData.TYPE_TIME:
|
||||
retval += "TIME";
|
||||
retval += "DATETIME";
|
||||
break;
|
||||
case ColumnMetaData.TYPE_DATE:
|
||||
retval += "DATE";
|
||||
@@ -282,7 +283,7 @@ public class DorisMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
// 18 significant digits
|
||||
retval += "BIGINT";
|
||||
} else {
|
||||
retval += "DECIMAL(" + length + ")";
|
||||
retval += "DECIMAL(" + (length > 38 ? 38 : length) + ")";
|
||||
}
|
||||
} else {
|
||||
retval += "INT";
|
||||
@@ -290,9 +291,10 @@ public class DorisMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
} else {
|
||||
// Floating point values...
|
||||
if (length > 15) {
|
||||
retval += "DECIMAL(" + length;
|
||||
int p = (length > 38 ? 38 : length);
|
||||
retval += "DECIMAL(" + p;
|
||||
if (precision > 0) {
|
||||
retval += ", " + precision;
|
||||
retval += ", " + (precision > p ? p : precision);
|
||||
}
|
||||
retval += ")";
|
||||
} else {
|
||||
@@ -307,7 +309,7 @@ public class DorisMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
case ColumnMetaData.TYPE_STRING:
|
||||
long newLength = length * 3;
|
||||
if (newLength < 255) {
|
||||
retval += "CHAR(" + newLength + ")";
|
||||
retval += "VARCHAR(" + newLength + ")";
|
||||
} else if (newLength < 65533) {
|
||||
retval += "VARCHAR(" + newLength + ")";
|
||||
} else {
|
||||
|
@@ -9,15 +9,6 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
package org.dromara.dbswitch.product.sr;
|
||||
|
||||
import org.dromara.dbswitch.common.consts.Constants;
|
||||
import org.dromara.dbswitch.common.type.ProductTypeEnum;
|
||||
import org.dromara.dbswitch.core.provider.ProductFactoryProvider;
|
||||
import org.dromara.dbswitch.core.provider.meta.AbstractMetadataProvider;
|
||||
import org.dromara.dbswitch.core.schema.ColumnDescription;
|
||||
import org.dromara.dbswitch.core.schema.ColumnMetaData;
|
||||
import org.dromara.dbswitch.core.schema.IndexDescription;
|
||||
import org.dromara.dbswitch.core.schema.TableDescription;
|
||||
import org.dromara.dbswitch.core.schema.SourceProperties;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@@ -29,7 +20,17 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dromara.dbswitch.common.consts.Constants;
|
||||
import org.dromara.dbswitch.common.type.ProductTypeEnum;
|
||||
import org.dromara.dbswitch.core.provider.ProductFactoryProvider;
|
||||
import org.dromara.dbswitch.core.provider.meta.AbstractMetadataProvider;
|
||||
import org.dromara.dbswitch.core.schema.ColumnDescription;
|
||||
import org.dromara.dbswitch.core.schema.ColumnMetaData;
|
||||
import org.dromara.dbswitch.core.schema.IndexDescription;
|
||||
import org.dromara.dbswitch.core.schema.SourceProperties;
|
||||
import org.dromara.dbswitch.core.schema.TableDescription;
|
||||
|
||||
/**
|
||||
* https://docs.starrocks.io/zh/docs/3.1/quick_start/Create_table/
|
||||
@@ -269,7 +270,7 @@ public class StarrocksMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
retval += "DATETIME";
|
||||
break;
|
||||
case ColumnMetaData.TYPE_TIME:
|
||||
retval += "TIME";
|
||||
retval += "DATETIME";
|
||||
break;
|
||||
case ColumnMetaData.TYPE_DATE:
|
||||
retval += "DATE";
|
||||
@@ -297,7 +298,7 @@ public class StarrocksMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
// 18 significant digits
|
||||
retval += "BIGINT";
|
||||
} else {
|
||||
retval += "DECIMAL(" + length + ")";
|
||||
retval += "DECIMAL(" + (length > 38 ? 38 : length) + ")";
|
||||
}
|
||||
} else {
|
||||
retval += "INT";
|
||||
@@ -305,9 +306,10 @@ public class StarrocksMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
} else {
|
||||
// Floating point values...
|
||||
if (length > 15) {
|
||||
retval += "DECIMAL(" + length;
|
||||
int p = (length > 38 ? 38 : length);
|
||||
retval += "DECIMAL(" + p;
|
||||
if (precision > 0) {
|
||||
retval += ", " + precision;
|
||||
retval += ", " + (precision > p ? p : precision);
|
||||
}
|
||||
retval += ")";
|
||||
} else {
|
||||
@@ -321,21 +323,22 @@ public class StarrocksMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
break;
|
||||
case ColumnMetaData.TYPE_STRING:
|
||||
//see: https://docs.starrocks.io/zh/docs/category/string/
|
||||
if (length <= 255) {
|
||||
retval += "CHAR(" + length + ")";
|
||||
} else if (length <= 65533) {
|
||||
long newLength = length * 3;
|
||||
if (newLength < 255) {
|
||||
retval += "VARCHAR(" + newLength + ")";
|
||||
} else if (newLength <= 65533) {
|
||||
retval += "STRING";
|
||||
} else if (length <= 1048576){
|
||||
retval += "VARCHAR(" + length + ")";
|
||||
} else if (newLength <= 1048576) {
|
||||
retval += "VARCHAR(" + newLength + ")";
|
||||
} else {
|
||||
retval += "VARBINARY";
|
||||
retval += "VARCHAR(1048576)";
|
||||
}
|
||||
break;
|
||||
case ColumnMetaData.TYPE_BINARY:
|
||||
retval += "LONGBLOB";
|
||||
retval += "VARBINARY ";
|
||||
break;
|
||||
default:
|
||||
retval += "LONGTEXT";
|
||||
retval += "VARCHAR(1048576)";
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -363,8 +366,11 @@ public class StarrocksMetadataQueryProvider extends AbstractMetadataProvider {
|
||||
@Override
|
||||
public void postAppendCreateTableSql(StringBuilder builder, String tblComment, List<String> primaryKeys,
|
||||
SourceProperties tblProperties) {
|
||||
String pk = getPrimaryKeyAsString(primaryKeys);
|
||||
builder.append("PRIMARY KEY (").append(pk).append(")");
|
||||
builder.append("\n DISTRIBUTED BY HASH(").append(pk).append(")");
|
||||
// https://docs.mirrorship.cn/zh/docs/table_design/table_types/primary_key_table/
|
||||
if (CollectionUtils.isNotEmpty(primaryKeys)) {
|
||||
String pk = getPrimaryKeyAsString(primaryKeys);
|
||||
builder.append("PRIMARY KEY (").append(pk).append(")");
|
||||
builder.append("\n DISTRIBUTED BY HASH(").append(pk).append(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user