use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.
the class SQLIndexManager method addObjectCreateActions.
@Override
protected void addObjectCreateActions(List<DBEPersistAction> actions, ObjectCreateCommand command) {
final TABLE_TYPE table = command.getObject().getTable();
final OBJECT_TYPE index = command.getObject();
// Create index
final String indexName = DBUtils.getQuotedIdentifier(index.getDataSource(), index.getName());
index.setName(indexName);
StringBuilder decl = new StringBuilder(40);
decl.append("CREATE ");
if (index.isUnique()) {
decl.append("UNIQUE ");
}
//$NON-NLS-1$
decl.append("INDEX ").append(indexName).append(" ON ").append(//$NON-NLS-1$
table.getFullyQualifiedName(DBPEvaluationContext.DDL)).append(//$NON-NLS-1$
" (");
try {
// Get columns using void monitor
boolean firstColumn = true;
for (DBSTableIndexColumn indexColumn : CommonUtils.safeCollection(command.getObject().getAttributeReferences(VoidProgressMonitor.INSTANCE))) {
//$NON-NLS-1$
if (!firstColumn)
decl.append(",");
firstColumn = false;
decl.append(indexColumn.getName());
appendIndexColumnModifiers(decl, indexColumn);
}
} catch (DBException e) {
log.error(e);
}
//$NON-NLS-1$
decl.append(")");
actions.add(new SQLDatabasePersistAction(ModelMessages.model_jdbc_create_new_index, decl.toString()));
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.
the class SQLTableColumnManager method addObjectCreateActions.
@Override
protected void addObjectCreateActions(List<DBEPersistAction> actions, ObjectCreateCommand command) {
final TABLE_TYPE table = command.getObject().getTable();
actions.add(new SQLDatabasePersistAction(ModelMessages.model_jdbc_create_new_table_column, "ALTER TABLE " + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + " ADD " + getNestedDeclaration(table, command)));
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.
the class SQLiteTableManager method addObjectRenameActions.
@Override
protected void addObjectRenameActions(List<DBEPersistAction> actions, ObjectRenameCommand command) {
final GenericDataSource dataSource = command.getObject().getDataSource();
actions.add(new SQLDatabasePersistAction("Rename table", //$NON-NLS-1$
"ALTER TABLE " + command.getObject().getFullyQualifiedName(DBPEvaluationContext.DDL) + " RENAME TO " + //$NON-NLS-1$
DBUtils.getQuotedIdentifier(dataSource, command.getNewName())));
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.
the class ExecuteBatchImpl method processBatch.
/**
* Execute batch OR generate batch script.
* @param session session
* @param actions script actions. If not null then no execution will be done
* @return execution statistics
* @throws DBCException
*/
@NotNull
private DBCStatistics processBatch(@NotNull DBCSession session, @Nullable List<DBEPersistAction> actions) throws DBCException {
DBDValueHandler[] handlers = new DBDValueHandler[attributes.length];
for (int i = 0; i < attributes.length; i++) {
if (attributes[i] instanceof DBDAttributeBinding) {
handlers[i] = ((DBDAttributeBinding) attributes[i]).getValueHandler();
} else {
handlers[i] = DBUtils.findValueHandler(session, attributes[i]);
}
}
boolean useBatch = session.getDataSource().getInfo().supportsBatchUpdates() && reuseStatement;
if (values.size() <= 1) {
useBatch = false;
}
DBCStatistics statistics = new DBCStatistics();
DBCStatement statement = null;
try {
// Here we'll try to reuse prepared statement.
// It makes a great sense in case of data transfer where we need millions of inserts.
// We must be aware of nulls because actual insert statements may differ depending on null values.
// So if row nulls aren't the same as in previous row we need to prepare new statement and restart batch.
// Quite complicated but works.
boolean[] prevNulls = new boolean[attributes.length];
boolean[] nulls = new boolean[attributes.length];
int statementsInBatch = 0;
for (Object[] rowValues : values) {
boolean reuse = reuseStatement;
if (reuse) {
for (int i = 0; i < rowValues.length; i++) {
nulls[i] = DBUtils.isNullValue(rowValues[i]);
}
if (!Arrays.equals(prevNulls, nulls) && statementsInBatch > 0) {
reuse = false;
}
System.arraycopy(nulls, 0, prevNulls, 0, nulls.length);
if (!reuse && statementsInBatch > 0) {
// Flush batch
if (actions == null) {
flushBatch(statistics, statement);
}
statement.close();
statement = null;
statementsInBatch = 0;
reuse = true;
}
}
if (statement == null || !reuse) {
statement = prepareStatement(session, rowValues);
statistics.setQueryText(statement.getQueryString());
}
try {
bindStatement(handlers, statement, rowValues);
if (actions == null) {
if (useBatch) {
statement.addToBatch();
statementsInBatch++;
} else {
// Execute each row separately
long startTime = System.currentTimeMillis();
executeStatement(statement);
statistics.addExecuteTime(System.currentTimeMillis() - startTime);
long rowCount = statement.getUpdateRowCount();
if (rowCount > 0) {
statistics.addRowsUpdated(rowCount);
}
// Read keys
if (keysReceiver != null) {
readKeys(statement.getSession(), statement, keysReceiver);
}
}
} else {
String queryString;
if (statement instanceof DBCParameterizedStatement) {
queryString = ((DBCParameterizedStatement) statement).getFormattedQuery();
} else {
queryString = statement.getQueryString();
}
actions.add(new SQLDatabasePersistAction("Execute statement", queryString));
}
} finally {
if (!reuse) {
statement.close();
}
}
}
values.clear();
if (statementsInBatch > 0) {
if (actions == null) {
flushBatch(statistics, statement);
}
statement.close();
statement = null;
}
} finally {
if (reuseStatement && statement != null) {
statement.close();
}
}
return statistics;
}
use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction in project dbeaver by serge-rider.
the class DB2TableColumnManager method addObjectRenameActions.
@Override
protected void addObjectRenameActions(List<DBEPersistAction> actions, ObjectRenameCommand command) {
final DB2TableColumn column = command.getObject();
actions.add(new SQLDatabasePersistAction("Rename column", "ALTER TABLE " + column.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL) + " RENAME COLUMN " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getOldName()) + " TO " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getNewName())));
}
Aggregations