Search in sources :

Example 1 with GenericTableBase

use of org.jkiss.dbeaver.ext.generic.model.GenericTableBase in project dbeaver by serge-rider.

the class SQLiteTableColumnManager method addObjectDeleteActions.

@Override
protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) throws DBException {
    final GenericTableColumn column = command.getObject();
    final GenericTableBase table = column.getTable();
    final List<? extends GenericTableColumn> attributes = table.getAttributes(monitor);
    if (CommonUtils.isEmpty(attributes)) {
        throw new DBException("Table was deleted");
    }
    final String tableColumns = attributes.stream().filter(x -> !x.getName().equals(column.getName()) && x.isPersisted()).map(DBUtils::getQuotedIdentifier).collect(Collectors.joining(",\n  "));
    final String tableName = DBUtils.getQuotedIdentifier(table);
    actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop column " + DBUtils.getQuotedIdentifier(column)));
    actions.add(new SQLDatabasePersistAction("Create temporary table from original table", "CREATE TEMPORARY TABLE temp AS\nSELECT\n  " + tableColumns + "\nFROM " + tableName));
    actions.add(new SQLDatabasePersistAction("Drop original table", "\nDROP TABLE " + tableName + ";\n"));
    actions.add(new SQLDatabasePersistAction("Create new table", DBStructUtils.generateTableDDL(monitor, table, Collections.emptyMap(), false)));
    actions.add(new SQLDatabasePersistAction("Insert values from temporary table to new table", "INSERT INTO " + tableName + "\n (" + tableColumns + ")\nSELECT\n  " + tableColumns + "\nFROM temp"));
    actions.add(new SQLDatabasePersistAction("Drop temporary table", "\nDROP TABLE temp"));
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) GenericTableColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableColumn) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 2 with GenericTableBase

use of org.jkiss.dbeaver.ext.generic.model.GenericTableBase in project dbeaver by serge-rider.

the class GenericForeignKeyManager method createDatabaseObject.

@Override
protected GenericTableForeignKey createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, final Object container, Object from, Map<String, Object> options) {
    GenericTableBase tableBase = (GenericTableBase) container;
    GenericTableForeignKey foreignKey = new GenericTableForeignKey(tableBase, null, null, null, DBSForeignKeyModifyRule.NO_ACTION, DBSForeignKeyModifyRule.NO_ACTION, DBSForeignKeyDeferability.NOT_DEFERRABLE, false);
    foreignKey.setName(getNewConstraintName(monitor, foreignKey));
    return foreignKey;
}
Also used : GenericTableForeignKey(org.jkiss.dbeaver.ext.generic.model.GenericTableForeignKey) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase)

Example 3 with GenericTableBase

use of org.jkiss.dbeaver.ext.generic.model.GenericTableBase in project dbeaver by serge-rider.

the class ClickhouseSchema method collectObjectStatistics.

@Override
public void collectObjectStatistics(DBRProgressMonitor monitor, boolean totalSizeOnly, boolean forceRefresh) throws DBException {
    if (hasStatistics && !forceRefresh) {
        return;
    }
    try (DBCSession session = DBUtils.openMetaSession(monitor, this, "Read relation statistics")) {
        try (JDBCPreparedStatement dbStat = ((JDBCSession) session).prepareStatement("select table," + "sum(bytes) as table_size, " + "sum(rows) as table_rows, " + "max(modification_time) as latest_modification," + "min(min_date) AS min_date," + "max(max_date) AS max_date," + "any(engine) as engine\n" + "FROM system.parts\n" + "WHERE database=? AND active\n" + "GROUP BY table")) {
            dbStat.setString(1, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String tableName = dbResult.getString(1);
                    GenericTableBase table = getTable(monitor, tableName);
                    if (table instanceof ClickhouseTable) {
                        ((ClickhouseTable) table).fetchStatistics(dbResult);
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBCException("Error reading schema statistics", e);
        }
    } finally {
        hasStatistics = true;
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)

Example 4 with GenericTableBase

use of org.jkiss.dbeaver.ext.generic.model.GenericTableBase in project dbeaver by serge-rider.

the class GenericTableIndexConfigurator method configureObject.

@Override
public GenericTableIndex configureObject(DBRProgressMonitor monitor, Object table, GenericTableIndex index) {
    GenericTableBase tableBase = (GenericTableBase) table;
    boolean supportUniqueIndexes = tableBase.supportUniqueIndexes();
    Collection<DBSIndexType> tableIndexTypes = tableBase.getTableIndexTypes();
    return new UITask<GenericTableIndex>() {

        @Override
        protected GenericTableIndex runTask() {
            EditIndexPage editPage = new EditIndexPage("Create index", index, tableIndexTypes, supportUniqueIndexes);
            if (!editPage.edit()) {
                return null;
            }
            index.setIndexType(editPage.getIndexType());
            StringBuilder idxName = new StringBuilder(64);
            idxName.append(CommonUtils.escapeIdentifier(index.getTable().getName()));
            int colIndex = 1;
            for (DBSEntityAttribute tableColumn : editPage.getSelectedAttributes()) {
                if (colIndex == 1) {
                    idxName.append("_").append(CommonUtils.escapeIdentifier(tableColumn.getName()));
                }
                index.addColumn(new GenericTableIndexColumn(index, (GenericTableColumn) tableColumn, colIndex++, !Boolean.TRUE.equals(editPage.getAttributeProperty(tableColumn, EditIndexPage.PROP_DESC))));
            }
            idxName.append("_IDX");
            index.setName(DBObjectNameCaseTransformer.transformObjectName(index, idxName.toString()));
            index.setUnique(editPage.isUnique());
            return index;
        }
    }.execute();
}
Also used : DBSIndexType(org.jkiss.dbeaver.model.struct.rdb.DBSIndexType) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) GenericTableIndexColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableIndexColumn) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase) GenericTableIndex(org.jkiss.dbeaver.ext.generic.model.GenericTableIndex) EditIndexPage(org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)

Example 5 with GenericTableBase

use of org.jkiss.dbeaver.ext.generic.model.GenericTableBase in project dbeaver by serge-rider.

the class GenericTableColumnManager method createDatabaseObject.

@Override
protected GenericTableColumn createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, Object container, Object copyFrom, Map<String, Object> options) throws DBException {
    GenericTableBase tableBase = (GenericTableBase) container;
    DBSDataType columnType = findBestDataType(tableBase.getDataSource(), DBConstants.DEFAULT_DATATYPE_NAMES);
    int columnSize = columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0;
    GenericTableColumn column = tableBase.getDataSource().getMetaModel().createTableColumnImpl(monitor, null, tableBase, getNewColumnName(monitor, context, tableBase), columnType == null ? "INTEGER" : columnType.getName(), columnType == null ? Types.INTEGER : columnType.getTypeID(), columnType == null ? Types.INTEGER : columnType.getTypeID(), -1, columnSize, columnSize, null, null, 10, false, null, null, false, false);
    column.setPersisted(false);
    return column;
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) GenericTableColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableColumn) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase)

Aggregations

GenericTableBase (org.jkiss.dbeaver.ext.generic.model.GenericTableBase)6 GenericTableColumn (org.jkiss.dbeaver.ext.generic.model.GenericTableColumn)2 SQLException (java.sql.SQLException)1 DBException (org.jkiss.dbeaver.DBException)1 GenericStructContainer (org.jkiss.dbeaver.ext.generic.model.GenericStructContainer)1 GenericTableForeignKey (org.jkiss.dbeaver.ext.generic.model.GenericTableForeignKey)1 GenericTableIndex (org.jkiss.dbeaver.ext.generic.model.GenericTableIndex)1 GenericTableIndexColumn (org.jkiss.dbeaver.ext.generic.model.GenericTableIndexColumn)1 GenericView (org.jkiss.dbeaver.ext.generic.model.GenericView)1 DBCException (org.jkiss.dbeaver.model.exec.DBCException)1 DBCSession (org.jkiss.dbeaver.model.exec.DBCSession)1 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)1 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)1 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)1 SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)1 SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)1 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)1 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)1 DBSIndexType (org.jkiss.dbeaver.model.struct.rdb.DBSIndexType)1 EditIndexPage (org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)1