Search in sources :

Example 1 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class ProxyMetaManager method createIndex.

private void createIndex(String schema, String sql, SQLCreateIndexStatement statement, boolean isSuccess, boolean needNotifyOther) {
    SQLTableSource tableSource = statement.getTable();
    if (tableSource instanceof SQLExprTableSource) {
        SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
        SchemaInfo schemaInfo = getSchemaInfo(schema, exprTableSource);
        try {
            if (!isSuccess) {
                return;
            }
            StructureMeta.TableMeta orgTbMeta = getTableMeta(schemaInfo.getSchema(), schemaInfo.getTable());
            if (orgTbMeta == null)
                return;
            String indexName = StringUtil.removeBackQuote(statement.getName().getSimpleName());
            StructureMeta.TableMeta.Builder tmBuilder = orgTbMeta.toBuilder();
            if (statement.getType() == null) {
                addIndex(indexName, tmBuilder, IndexType.MUL, itemsToColumns(statement.getItems()));
            } else if (statement.getType().equals("UNIQUE")) {
                addIndex(indexName, tmBuilder, IndexType.UNI, itemsToColumns(statement.getItems()));
            }
        } catch (Exception e) {
            LOGGER.info("updateMetaData failed,sql is" + statement.toString(), e);
        } finally {
            try {
                notifyResponseClusterDDL(schemaInfo.getSchema(), schemaInfo.getTable(), sql, isSuccess ? DDLInfo.DDLStatus.SUCCESS : DDLInfo.DDLStatus.FAILED, needNotifyOther);
            } catch (Exception e) {
                LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + "notifyResponseZKDdl error", e);
            }
            removeMetaLock(schemaInfo.getSchema(), schemaInfo.getTable());
        }
    }
}
Also used : StructureMeta(com.actiontech.dble.meta.protocol.StructureMeta) SQLException(java.sql.SQLException) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 2 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class ProxyMetaManager method truncateTable.

private void truncateTable(String schema, String sql, SQLTruncateStatement statement, boolean isSuccess, boolean needNotifyOther) {
    // TODO:reset Sequence?
    SQLExprTableSource exprTableSource = statement.getTableSources().get(0);
    SchemaInfo schemaInfo = getSchemaInfo(schema, exprTableSource);
    try {
        notifyResponseClusterDDL(schemaInfo.getSchema(), schemaInfo.getTable(), sql, isSuccess ? DDLInfo.DDLStatus.SUCCESS : DDLInfo.DDLStatus.FAILED, needNotifyOther);
    } catch (Exception e) {
        LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + "notifyResponseZKDdl error", e);
    }
    removeMetaLock(schemaInfo.getSchema(), schemaInfo.getTable());
}
Also used : SQLException(java.sql.SQLException) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 3 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class ProxyMetaManager method alterTable.

private void alterTable(String schema, String sql, SQLAlterTableStatement alterStatement, boolean isSuccess, boolean needNotifyOther) {
    SchemaInfo schemaInfo = getSchemaInfo(schema, alterStatement.getTableSource());
    try {
        if (!isSuccess) {
            return;
        }
        StructureMeta.TableMeta orgTbMeta = getTableMeta(schemaInfo.getSchema(), schemaInfo.getTable());
        if (orgTbMeta == null)
            return;
        StructureMeta.TableMeta.Builder tmBuilder = orgTbMeta.toBuilder();
        List<StructureMeta.ColumnMeta> cols = new ArrayList<>();
        cols.addAll(orgTbMeta.getColumnsList());
        int autoColumnIndex = -1;
        Set<String> indexNames = new HashSet<>();
        for (StructureMeta.IndexMeta index : tmBuilder.getIndexList()) {
            indexNames.add(index.getName());
        }
        for (SQLAlterTableItem alterItem : alterStatement.getItems()) {
            if (alterItem instanceof SQLAlterTableAddColumn) {
                autoColumnIndex = addColumn(tmBuilder, cols, (SQLAlterTableAddColumn) alterItem, indexNames);
            } else if (alterItem instanceof SQLAlterTableAddIndex) {
                addIndex(tmBuilder, (SQLAlterTableAddIndex) alterItem, indexNames);
            } else if (alterItem instanceof SQLAlterTableAddConstraint) {
                SQLAlterTableAddConstraint addConstraint = (SQLAlterTableAddConstraint) alterItem;
                SQLConstraint constraint = addConstraint.getConstraint();
                if (constraint instanceof MySqlPrimaryKey) {
                    MySqlPrimaryKey primaryKey = (MySqlPrimaryKey) constraint;
                    StructureMeta.IndexMeta indexMeta = MetaHelper.makeIndexMeta(MetaHelper.PRIMARY, IndexType.PRI, primaryKey.getColumns());
                    tmBuilder.setPrimary(indexMeta);
                } else {
                // NOT SUPPORT
                }
            } else if (alterItem instanceof SQLAlterTableDropIndex) {
                SQLAlterTableDropIndex dropIndex = (SQLAlterTableDropIndex) alterItem;
                String dropName = StringUtil.removeBackQuote(dropIndex.getIndexName().getSimpleName());
                dropIndex(tmBuilder, dropName);
            } else if (alterItem instanceof SQLAlterTableDropKey) {
                SQLAlterTableDropKey dropIndex = (SQLAlterTableDropKey) alterItem;
                String dropName = StringUtil.removeBackQuote(dropIndex.getKeyName().getSimpleName());
                dropIndex(tmBuilder, dropName);
            } else if (alterItem instanceof MySqlAlterTableChangeColumn) {
                autoColumnIndex = changeColumn(tmBuilder, cols, (MySqlAlterTableChangeColumn) alterItem, indexNames);
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                autoColumnIndex = modifyColumn(tmBuilder, cols, (MySqlAlterTableModifyColumn) alterItem, indexNames);
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                dropColumn(cols, (SQLAlterTableDropColumnItem) alterItem);
            } else if (alterItem instanceof SQLAlterTableDropPrimaryKey) {
                tmBuilder.clearPrimary();
            } else {
            // TODO: further
            }
        }
        tmBuilder.clearColumns().addAllColumns(cols);
        if (autoColumnIndex != -1) {
            tmBuilder.setAiColPos(autoColumnIndex);
        }
        tmBuilder.setVersion(System.currentTimeMillis());
        StructureMeta.TableMeta newTblMeta = tmBuilder.build();
        addTable(schemaInfo.getSchema(), newTblMeta);
    } catch (Exception e) {
        LOGGER.info("updateMetaData alterTable failed,sql is" + alterStatement.toString(), e);
    } finally {
        try {
            notifyResponseClusterDDL(schemaInfo.getSchema(), schemaInfo.getTable(), sql, isSuccess ? DDLInfo.DDLStatus.SUCCESS : DDLInfo.DDLStatus.FAILED, needNotifyOther);
        } catch (Exception e) {
            LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + "notifyResponseZKDdl error", e);
        }
        removeMetaLock(schemaInfo.getSchema(), schemaInfo.getTable());
    }
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo) StructureMeta(com.actiontech.dble.meta.protocol.StructureMeta) SQLException(java.sql.SQLException) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)

Example 4 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class ProxyMetaManager method createTable.

private void createTable(String schema, String sql, MySqlCreateTableStatement statement, boolean isSuccess, boolean needNotifyOther) {
    SchemaInfo schemaInfo = getSchemaInfo(schema, statement.getTableSource());
    try {
        if (!isSuccess) {
            return;
        }
        StructureMeta.TableMeta tblMeta = MetaHelper.initTableMeta(schemaInfo.getTable(), statement, System.currentTimeMillis());
        addTable(schemaInfo.getSchema(), tblMeta);
    } catch (Exception e) {
        LOGGER.info("updateMetaData failed,sql is" + statement.toString(), e);
    } finally {
        try {
            notifyResponseClusterDDL(schemaInfo.getSchema(), schemaInfo.getTable(), sql, isSuccess ? DDLInfo.DDLStatus.SUCCESS : DDLInfo.DDLStatus.FAILED, needNotifyOther);
        } catch (Exception e) {
            LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + "notifyResponseZKDdl error", e);
        }
        removeMetaLock(schemaInfo.getSchema(), schemaInfo.getTable());
    }
}
Also used : StructureMeta(com.actiontech.dble.meta.protocol.StructureMeta) SQLException(java.sql.SQLException) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 5 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class ProxyMetaManager method dropIndex.

private void dropIndex(String schema, String sql, SQLDropIndexStatement dropIndexStatement, boolean isSuccess, boolean needNotifyOther) {
    SchemaInfo schemaInfo = getSchemaInfo(schema, dropIndexStatement.getTableName());
    StructureMeta.TableMeta orgTbMeta = getTableMeta(schemaInfo.getSchema(), schemaInfo.getTable());
    try {
        if (!isSuccess) {
            return;
        }
        if (orgTbMeta != null) {
            StructureMeta.TableMeta.Builder tmBuilder = orgTbMeta.toBuilder();
            String dropName = StringUtil.removeBackQuote(((SQLIdentifierExpr) dropIndexStatement.getIndexName()).getName());
            dropIndex(tmBuilder, dropName);
        }
    } catch (Exception e) {
        LOGGER.info("updateMetaData failed,sql is" + dropIndexStatement.toString(), e);
    } finally {
        try {
            notifyResponseClusterDDL(schemaInfo.getSchema(), schemaInfo.getTable(), sql, isSuccess ? DDLInfo.DDLStatus.SUCCESS : DDLInfo.DDLStatus.FAILED, needNotifyOther);
        } catch (Exception e) {
            LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + "notifyResponseZKDdl error", e);
        }
        removeMetaLock(schemaInfo.getSchema(), schemaInfo.getTable());
    }
}
Also used : StructureMeta(com.actiontech.dble.meta.protocol.StructureMeta) SQLException(java.sql.SQLException) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Aggregations

SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)20 SQLNonTransientException (java.sql.SQLNonTransientException)10 SQLException (java.sql.SQLException)9 TableConfig (com.actiontech.dble.config.model.TableConfig)5 StructureMeta (com.actiontech.dble.meta.protocol.StructureMeta)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)3 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)2 MySqlAlterTableChangeColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)2 MySqlAlterTableModifyColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlUnionQuery (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLCreateIndexStatement (com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)1 SQLDropIndexStatement (com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement)1