提交 a746c3d8 编写于 作者: S serge-rider

MySQL table copy. Copy constructors in RDB model.


Former-commit-id: 0d981add
上级 d16e09c3
......@@ -30,6 +30,7 @@ import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.impl.DBSObjectCache;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableManager;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
......@@ -55,18 +56,22 @@ public class MySQLTableManager extends SQLTableManager<MySQLTableBase, MySQLCata
}
@Override
protected MySQLTable createDatabaseObject(DBECommandContext context, MySQLCatalog parent, Object copyFrom)
protected MySQLTable createDatabaseObject(DBECommandContext context, MySQLCatalog parent, Object copyFrom) throws DBException
{
final MySQLTable table = new MySQLTable(parent);
try {
DBRProgressMonitor monitor = VoidProgressMonitor.INSTANCE;
final MySQLTable table;
if (copyFrom instanceof MySQLTable) {
table = new MySQLTable(monitor, (MySQLTable)copyFrom);
table.setName(getTableName(parent, ((MySQLTable) copyFrom).getName()));
} else {
table = new MySQLTable(parent);
setTableName(parent, table);
final MySQLTable.AdditionalInfo additionalInfo = table.getAdditionalInfo(VoidProgressMonitor.INSTANCE);
final MySQLTable.AdditionalInfo additionalInfo = table.getAdditionalInfo(monitor);
additionalInfo.setEngine(parent.getDataSource().getDefaultEngine());
additionalInfo.setCharset(parent.getDefaultCharset());
additionalInfo.setCollation(parent.getDefaultCollation());
} catch (DBException e) {
// Never be here
log.error(e);
}
return table;
......@@ -136,26 +141,4 @@ public class MySQLTableManager extends SQLTableManager<MySQLTableBase, MySQLCata
processObjectRename(commandContext, object, newName);
}
/*
public ITabDescriptor[] getTabDescriptors(IWorkbenchWindow workbenchWindow, final IDatabaseEditor activeEditor, final MySQLTable object)
{
if (object.getContainer().isSystem()) {
return null;
}
return new ITabDescriptor[] {
new PropertyTabDescriptor(
PropertiesContributor.CATEGORY_INFO,
"table.ddl", //$NON-NLS-1$
"DDL", //$NON-NLS-1$
DBIcon.SOURCES.getImage(),
new SectionDescriptor("default", "DDL") { //$NON-NLS-1$ //$NON-NLS-2$
public ISection getSectionClass()
{
return new MySQLDDLViewEditor(activeEditor);
}
})
};
}
*/
}
......@@ -23,6 +23,7 @@ import org.jkiss.dbeaver.ext.mysql.MySQLConstants;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCTableObject;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import java.sql.ResultSet;
import java.util.ArrayList;
......@@ -92,6 +93,18 @@ public class MySQLPartition extends JDBCTableObject<MySQLTable>
super(source);
}
// Copy constructor
protected MySQLPartition(DBRProgressMonitor monitor, MySQLTable table, MySQLPartition source)
{
super(table, source.getName(), false);
this.position = source.position;
this.method = source.method;
this.expression = source.expression;
this.description = source.description;
this.comment = source.comment;
this.nodegroup = source.nodegroup;
}
private void addSubPartitions(MySQLPartition partition)
{
if (subPartitions == null) {
......
......@@ -33,7 +33,6 @@ import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectCache;
import org.jkiss.dbeaver.model.meta.*;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSEntityConstraintType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.rdb.DBSForeignKeyModifyRule;
import org.jkiss.utils.CommonUtils;
......@@ -99,6 +98,27 @@ public class MySQLTable extends MySQLTableBase
super(catalog);
}
// Copy constructor
public MySQLTable(DBRProgressMonitor monitor, MySQLTable source) throws DBException {
super(monitor, source);
additionalInfo.loaded = source.additionalInfo.loaded;
additionalInfo.description = source.additionalInfo.description;
additionalInfo.createTime = source.additionalInfo.createTime;
additionalInfo.charset = source.additionalInfo.charset;
additionalInfo.collation = source.additionalInfo.collation;
additionalInfo.engine = source.additionalInfo.engine;
// Copy partitions
for (MySQLPartition partition : source.partitionCache.getCachedObjects()) {
partitionCache.cacheObject(new MySQLPartition(monitor, this, partition));
}
// Copy indexes
// Copy constraints
// Copy FKs
for (MySQLTableForeignKey fk : foreignKeys.getCachedObjects()) {
foreignKeys.cacheObject(new MySQLTableForeignKey(monitor, this, fk));
}
}
public MySQLTable(
MySQLCatalog catalog,
ResultSet dbResult)
......
......@@ -24,11 +24,13 @@ import org.jkiss.dbeaver.model.DBPNamedObject2;
import org.jkiss.dbeaver.model.DBPRefreshableObject;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.DBSObjectCache;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCStructCache;
import org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCTable;
import org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCTableColumn;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;
import java.io.UnsupportedEncodingException;
......@@ -50,6 +52,18 @@ public abstract class MySQLTableBase extends JDBCTable<MySQLDataSource, MySQLCat
super(catalog, false);
}
// Copy constructor
protected MySQLTableBase(DBRProgressMonitor monitor, MySQLTableBase source) throws DBException {
super(source.getContainer(), false);
DBSObjectCache<MySQLTableBase, MySQLTableColumn> colCache = getContainer().getTableCache().getChildrenCache(this);
// Copy columns
for (MySQLTableColumn srcColumn : source.getAttributes(monitor)) {
MySQLTableColumn column = new MySQLTableColumn(this, srcColumn);
colCache.cacheObject(column);
}
}
protected MySQLTableBase(
MySQLCatalog catalog,
ResultSet dbResult)
......
......@@ -47,7 +47,6 @@ public class MySQLTableColumn extends JDBCTableColumn<MySQLTableBase> implements
private static final Log log = Log.getLog(MySQLTableColumn.class);
private static Pattern enumPattern = Pattern.compile("'([^']*)'");
private String fullTypeName;
public enum KeyType implements JDBCColumnKeyType {
PRI,
......@@ -73,6 +72,7 @@ public class MySQLTableColumn extends JDBCTableColumn<MySQLTableBase> implements
private KeyType keyType;
private String extraInfo;
private String fullTypeName;
private List<String> enumValues;
public MySQLTableColumn(MySQLTableBase table)
......@@ -89,6 +89,24 @@ public class MySQLTableColumn extends JDBCTableColumn<MySQLTableBase> implements
loadInfo(dbResult);
}
// Copy constructor
public MySQLTableColumn(
MySQLTableBase table,
MySQLTableColumn source)
throws DBException
{
super(table, source, false);
this.comment = source.comment;
this.charLength = source.charLength;
this.collation = source.collation;
this.keyType = source.keyType;
this.extraInfo = source.extraInfo;
this.fullTypeName = source.fullTypeName;
if (source.enumValues != null) {
this.enumValues = new ArrayList<>(source.enumValues);
}
}
private void loadInfo(ResultSet dbResult)
throws DBException
{
......@@ -139,7 +157,7 @@ public class MySQLTableColumn extends JDBCTableColumn<MySQLTableBase> implements
this.extraInfo = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_EXTRA);
this.autoGenerated = extraInfo != null && extraInfo.contains(MySQLConstants.EXTRA_AUTO_INCREMENT);
fullTypeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_TYPE);
this.fullTypeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_TYPE);
if (!CommonUtils.isEmpty(fullTypeName) && (isTypeEnum() || isTypeSet())) {
enumValues = new ArrayList<>();
Matcher enumMatcher = enumPattern.matcher(fullTypeName);
......
......@@ -18,6 +18,7 @@
package org.jkiss.dbeaver.ext.mysql.model;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCTableForeignKey;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
......@@ -45,6 +46,18 @@ public class MySQLTableForeignKey extends JDBCTableForeignKey<MySQLTable, MySQLT
super(table, name, remarks, referencedKey, deleteRule, updateRule, persisted);
}
// Copy constructor
public MySQLTableForeignKey(DBRProgressMonitor monitor, MySQLTable table, MySQLTableForeignKey source) throws DBException {
super(
table,
source.getName(),
source.getDescription(),
table.getConstraint(monitor, source.referencedKey.getName()),
source.deleteRule,
source.updateRule,
false);
}
@Override
public List<MySQLTableForeignKeyColumn> getAttributeReferences(DBRProgressMonitor monitor)
{
......
......@@ -19,6 +19,7 @@
package org.jkiss.dbeaver.model.edit;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.impl.DBSObjectCache;
import org.jkiss.dbeaver.model.struct.DBSObject;
......@@ -60,7 +61,7 @@ public interface DBEObjectMaker<OBJECT_TYPE extends DBSObject, CONTAINER_TYPE> e
OBJECT_TYPE createNewObject(
DBECommandContext commandContext,
CONTAINER_TYPE parent,
Object copyFrom);
Object copyFrom) throws DBException;
/**
* Deletes specified object.
......
......@@ -38,6 +38,12 @@ public abstract class JDBCAttribute extends AbstractAttribute implements DBSObje
super(name, typeName, valueType, ordinalPosition, maxLength, scale, precision, required, sequence);
}
// Copy constructor
protected JDBCAttribute(JDBCAttribute source)
{
super(source);
}
@Nullable
@Override
public DBPImage getObjectImage()
......
......@@ -77,6 +77,17 @@ public abstract class JDBCTableColumn<TABLE_TYPE extends DBSEntity> extends JDBC
this.persisted = persisted;
}
protected JDBCTableColumn(
TABLE_TYPE table,
JDBCTableColumn source,
boolean persisted)
{
super(source);
this.table = table;
this.persisted = persisted;
this.defaultValue = source.defaultValue;
}
public TABLE_TYPE getTable()
{
return table;
......
......@@ -78,8 +78,7 @@ public abstract class SQLObjectEditor<OBJECT_TYPE extends DBSObject, CONTAINER_T
// Commands
@Override
public final OBJECT_TYPE createNewObject(DBECommandContext commandContext, CONTAINER_TYPE parent, Object copyFrom)
{
public final OBJECT_TYPE createNewObject(DBECommandContext commandContext, CONTAINER_TYPE parent, Object copyFrom) throws DBException {
OBJECT_TYPE newObject;
try {
newObject = createDatabaseObject(commandContext, parent, copyFrom);
......@@ -114,7 +113,7 @@ public abstract class SQLObjectEditor<OBJECT_TYPE extends DBSObject, CONTAINER_T
protected abstract OBJECT_TYPE createDatabaseObject(
DBECommandContext context,
CONTAINER_TYPE parent,
Object copyFrom);
Object copyFrom) throws DBException;
//////////////////////////////////////////////////
// Actions
......
......@@ -126,8 +126,12 @@ public abstract class SQLTableManager<OBJECT_TYPE extends JDBCTable, CONTAINER_T
}
protected String getTableName(CONTAINER_TYPE container) throws DBException {
return getTableName(container, BASE_TABLE_NAME);
}
protected String getTableName(CONTAINER_TYPE container, String baseName) throws DBException {
for (int i = 0; ; i++) {
String tableName = DBObjectNameCaseTransformer.transformName(container.getDataSource(), i == 0 ? BASE_TABLE_NAME : (BASE_TABLE_NAME + "_" + i));
String tableName = DBObjectNameCaseTransformer.transformName(container.getDataSource(), i == 0 ? baseName : (baseName + "_" + i));
DBSObject child = container.getChild(VoidProgressMonitor.INSTANCE, tableName);
if (child == null) {
return tableName;
......
......@@ -42,6 +42,20 @@ public abstract class AbstractAttribute implements DBSAttributeBase
{
}
// Copy constructor
protected AbstractAttribute(AbstractAttribute source)
{
this.name = source.name;
this.valueType = source.valueType;
this.maxLength = source.maxLength;
this.scale = source.scale;
this.precision = source.precision;
this.required = source.required;
this.autoGenerated = source.autoGenerated;
this.typeName = source.typeName;
this.ordinalPosition = source.ordinalPosition;
}
protected AbstractAttribute(
String name,
String typeName,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册