Skip to content

Commit

Permalink
Merge branch 'devel' into CB-4264-password-policy
Browse files Browse the repository at this point in the history
  • Loading branch information
dariamarutkina authored Jan 17, 2024
2 parents 04637ba + 5db30be commit 6a6b281
Show file tree
Hide file tree
Showing 18 changed files with 493 additions and 208 deletions.
24 changes: 23 additions & 1 deletion server/bundles/io.cloudbeaver.server/schema/service.sql.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ extend type Mutation {
addedRows: [ SQLResultRow! ],
): String!

#Return BLOB name
# Returns BLOB name
@deprecated(reason: "23.3.3") # use sqlReadLobValue
readLobValue(
projectId: ID,
connectionId: ID!,
Expand All @@ -341,6 +342,27 @@ extend type Mutation {
row: [ SQLResultRow! ]!
): String!

@since(version: "23.3.3")
sqlReadLobValue(
projectId: ID,
connectionId: ID!,
contextId: ID!,
resultsId: ID!,
lobColumnIndex: Int!,
row: SQLResultRow!
): String!

# Returns full string value ignoring any limits
@since(version: "23.3.3")
sqlReadStringValue(
projectId: ID,
connectionId: ID!,
contextId: ID!,
resultsId: ID!,
columnIndex: Int!,
row: SQLResultRow!
): String!

# Returns SQLExecuteInfo
asyncSqlExecuteResults(taskId: ID!): SQLExecuteInfo !

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,18 @@ WebSQLExecuteInfo updateResultsDataBatch(

@WebAction
String readLobValue(
@NotNull WebSQLContextInfo contextInfo,
@NotNull String resultsId,
@NotNull Integer lobColumnIndex,
@Nullable List<WebSQLResultsRow> row) throws DBWebException;
@NotNull WebSQLContextInfo contextInfo,
@NotNull String resultsId,
@NotNull Integer lobColumnIndex,
@NotNull WebSQLResultsRow row) throws DBWebException;

@NotNull
@WebAction
String getCellValue(
@NotNull WebSQLContextInfo contextInfo,
@NotNull String resultsId,
@NotNull Integer lobColumnIndex,
@NotNull WebSQLResultsRow row) throws DBWebException;

@WebAction
String updateResultsDataBatchScript(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudbeaver.service.sql;

import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.data.DBDAttributeBindingMeta;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.data.DBDDataReceiver;
import org.jkiss.dbeaver.model.data.DBDValue;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataContainer;
import org.jkiss.dbeaver.utils.ContentUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class WebSQLCellValueReceiver implements DBDDataReceiver {
protected final DBSDataContainer dataContainer;
protected int rowIndex;
protected DBDAttributeBindingMeta binding;
protected Object value;

public WebSQLCellValueReceiver(DBSDataContainer dataContainer, int rowIndex) {
this.dataContainer = dataContainer;
this.rowIndex = rowIndex;
}

@Override
public void fetchStart(DBCSession session, DBCResultSet resultSet, long offset, long maxRows) throws DBCException {
DBCResultSetMetaData meta = resultSet.getMeta();
List<DBCAttributeMetaData> attributes = meta.getAttributes();
DBCAttributeMetaData attrMeta = attributes.get(rowIndex);
binding = new DBDAttributeBindingMeta(dataContainer, resultSet.getSession(), attrMeta);
}

@Override
public void fetchRow(DBCSession session, DBCResultSet resultSet) throws DBCException {
value = binding.getValueHandler().fetchValueObject(
resultSet.getSession(),
resultSet,
binding.getMetaAttribute(),
rowIndex);
}

@Override
public void fetchEnd(DBCSession session, DBCResultSet resultSet) throws DBCException {

}

@Override
public void close() {

}

@NotNull
public byte[] getBinaryValue(DBRProgressMonitor monitor) throws DBCException {
byte[] binaryValue;
if (value instanceof DBDContent dbdContent) {
binaryValue = ContentUtils.getContentBinaryValue(monitor, dbdContent);
} else if (value instanceof DBDValue dbdValue) {
binaryValue = dbdValue.getRawValue().toString().getBytes();
} else {
binaryValue = value.toString().getBytes(StandardCharsets.UTF_8);
}
if (binaryValue == null) {
throw new DBCException("Lob value is null");
}
return binaryValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,29 @@
import io.cloudbeaver.server.CBConstants;
import io.cloudbeaver.server.CBPlatform;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.sql.DBQuotaException;
import org.jkiss.dbeaver.model.struct.DBSDataContainer;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;


public class WebSQLDataLOBReceiver implements DBDDataReceiver {
public class WebSQLDataLOBReceiver extends WebSQLCellValueReceiver {
private static final Log log = Log.getLog(WebSQLDataLOBReceiver.class);
public static final Path DATA_EXPORT_FOLDER = CBPlatform.getInstance().getTempFolder(new VoidProgressMonitor(), "sql-lob-files");

private final String tableName;
private final DBSDataContainer dataContainer;

private DBDAttributeBinding binding;
private Object lobValue;
private int rowIndex;

WebSQLDataLOBReceiver(String tableName, DBSDataContainer dataContainer, int rowIndex) {
super(dataContainer, rowIndex);
this.tableName = tableName;
this.dataContainer = dataContainer;
this.rowIndex = rowIndex;
if (!Files.exists(DATA_EXPORT_FOLDER)){
if (!Files.exists(DATA_EXPORT_FOLDER)) {
try {
Files.createDirectories(DATA_EXPORT_FOLDER);
} catch (IOException e) {
Expand All @@ -62,64 +52,28 @@ public class WebSQLDataLOBReceiver implements DBDDataReceiver {

}

public String createLobFile(DBCSession session) throws DBCException, IOException {
public String createLobFile(DBRProgressMonitor monitor) throws DBCException, IOException {
String exportFileName = CommonUtils.truncateString(tableName, 32);
StringBuilder fileName = new StringBuilder(exportFileName);
fileName.append("_")
.append(binding.getName())
.append("_");
.append(binding.getName())
.append("_");
Timestamp ts = new Timestamp(System.currentTimeMillis());
String s = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(ts);
fileName.append(s);
exportFileName = CommonUtils.escapeFileName(fileName.toString());
byte[] binaryValue;
byte[] binaryValue = getBinaryValue(monitor);
Number fileSizeLimit = CBApplication.getInstance().getAppConfiguration().getResourceQuota(CBConstants.QUOTA_PROP_FILE_LIMIT);
if (lobValue instanceof DBDContent) {
binaryValue = ContentUtils.getContentBinaryValue(session.getProgressMonitor(), (DBDContent) lobValue);
} else if (lobValue instanceof DBDValue) {
binaryValue = ((DBDValue) lobValue).getRawValue().toString().getBytes();
} else {
binaryValue = lobValue.toString().getBytes(StandardCharsets.UTF_8);
}
if (binaryValue == null) {
throw new DBCException("Lob value is null");
}
if (binaryValue.length > fileSizeLimit.longValue()) {
throw new DBQuotaException(
"Data export quota exceeded \n Please increase the resourceQuotas parameter in configuration",
"Data export quota exceeded \n Please increase the resourceQuotas parameter in configuration",
CBConstants.QUOTA_PROP_FILE_LIMIT, fileSizeLimit.longValue(), binaryValue.length
);
}
Path file = DATA_EXPORT_FOLDER.resolve(exportFileName);
Path file = WebSQLDataLOBReceiver.DATA_EXPORT_FOLDER.resolve(exportFileName);
Files.write(file, binaryValue);
return exportFileName;
}



@Override
public void fetchStart(DBCSession session, DBCResultSet resultSet, long offset, long maxRows) throws DBCException {
DBCResultSetMetaData meta = resultSet.getMeta();
List<DBCAttributeMetaData> attributes = meta.getAttributes();
DBCAttributeMetaData attrMeta = attributes.get(rowIndex);
binding = new DBDAttributeBindingMeta(dataContainer, resultSet.getSession(), attrMeta);
}
@Override
public void fetchRow(DBCSession session, DBCResultSet resultSet) throws DBCException {
lobValue = binding.getValueHandler().fetchValueObject(
resultSet.getSession(),
resultSet,
binding.getMetaAttribute(),
rowIndex);
}

@Override
public void fetchEnd(DBCSession session, DBCResultSet resultSet) throws DBCException {

}

@Override
public void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -797,14 +798,27 @@ public String readLobValue(

DBDRowIdentifier rowIdentifier = resultsInfo.getDefaultRowIdentifier();
checkRowIdentifier(resultsInfo, rowIdentifier);
String tableName = rowIdentifier.getEntity().getName();
WebSQLDataLOBReceiver dataReceiver = new WebSQLDataLOBReceiver(tableName, resultsInfo.getDataContainer(), lobColumnIndex);
readCellDataValue(monitor, resultsInfo, row, dataReceiver);
try {
return dataReceiver.createLobFile(monitor);
} catch (Exception e) {
throw new DBWebException("Error creating temporary lob file ", e);
}
}

private void readCellDataValue(
@NotNull DBRProgressMonitor monitor,
@NotNull WebSQLResultsInfo resultsInfo,
@Nullable WebSQLResultsRow row,
@NotNull WebSQLCellValueReceiver dataReceiver) throws DBException {
DBSDataContainer dataContainer = resultsInfo.getDataContainer();
DBCExecutionContext executionContext = getExecutionContext(dataContainer);
String tableName = rowIdentifier.getEntity().getName();
WebSQLDataLOBReceiver dataReceiver = new WebSQLDataLOBReceiver(tableName, dataContainer, lobColumnIndex);
try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Generate data update batches")) {
WebExecutionSource executionSource = new WebExecutionSource(dataContainer, executionContext, this);
DBDDataFilter dataFilter = new DBDDataFilter();
DBDAttributeBinding[] keyAttributes = rowIdentifier.getAttributes().toArray(new DBDAttributeBinding[0]);
DBDAttributeBinding[] keyAttributes = resultsInfo.getDefaultRowIdentifier().getAttributes().toArray(new DBDAttributeBinding[0]);
Object[] rowValues = new Object[keyAttributes.length];
List<DBDAttributeConstraint> constraints = new ArrayList<>();
for (int i = 0; i < keyAttributes.length; i++) {
Expand Down Expand Up @@ -833,14 +847,25 @@ public String readLobValue(
DBCStatistics statistics = dataContainer.readData(
executionSource, session, dataReceiver, dataFilter,
0, 1, DBSDataContainer.FLAG_NONE, 1);
try {
return dataReceiver.createLobFile(session);
} catch (Exception e) {
throw new DBWebException("Error creating temporary lob file ", e);
}
}
}

@NotNull
public String readStringValue(
@NotNull DBRProgressMonitor monitor,
@NotNull WebSQLContextInfo contextInfo,
@NotNull String resultsId,
@NotNull Integer columnIndex,
@Nullable WebSQLResultsRow row
) throws DBException {
WebSQLResultsInfo resultsInfo = contextInfo.getResults(resultsId);
DBDRowIdentifier rowIdentifier = resultsInfo.getDefaultRowIdentifier();
checkRowIdentifier(resultsInfo, rowIdentifier);
WebSQLCellValueReceiver dataReceiver = new WebSQLCellValueReceiver(resultsInfo.getDataContainer(), columnIndex);
readCellDataValue(monitor, resultsInfo, row, dataReceiver);
return new String(dataReceiver.getBinaryValue(monitor), StandardCharsets.UTF_8);
}

////////////////////////////////////////////////
// Misc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,24 @@ public void bindWiring(DBWBindingContext model) throws DBWebException {
getSQLContext(env),
env.getArgument("resultId"));
})
.dataFetcher("readLobValue", env ->
getService(env).readLobValue(
getSQLContext(env),
env.getArgument("resultsId"),
env.getArgument("lobColumnIndex"),
getResultsRow(env, "row")))
.dataFetcher("readLobValue", env -> // deprecated
getService(env).readLobValue(
getSQLContext(env),
env.getArgument("resultsId"),
env.getArgument("lobColumnIndex"),
getResultsRow(env, "row").get(0)))
.dataFetcher("sqlReadLobValue", env ->
getService(env).readLobValue(
getSQLContext(env),
env.getArgument("resultsId"),
env.getArgument("lobColumnIndex"),
new WebSQLResultsRow(env.getArgument("row"))))
.dataFetcher("sqlReadStringValue", env ->
getService(env).getCellValue(
getSQLContext(env),
env.getArgument("resultsId"),
env.getArgument("columnIndex"),
new WebSQLResultsRow(env.getArgument("row"))))
.dataFetcher("updateResultsDataBatch", env ->
getService(env).updateResultsDataBatch(
getSQLContext(env),
Expand Down
Loading

0 comments on commit 6a6b281

Please sign in to comment.