Repository: dbeaver/dbeaver
Issue: Advanced Copy as SQL doesn't copy table name as case sensitive

#### System information: 
- Operating system (distribution) and version: MacOS Monterey
- DBeaver version: 21.3.2 Community

#### Connection specification:
- Database name and version: Postgres 12
- Driver name: PostgreSQL JDBC Driver
- Do you use tunnels or proxies (SSH, SOCKS, etc)? No

#### Describe the problem you're observing:
When copying result row in order to get an insert SQL statement the table name is not copied with double quotes and proper (case sensitive) table name.

Example table name "investmentRounds" is copied as "investmentrounds" and without quotes. This kind of statement doesn't work and the name of the table must be corrected.

#### Steps to reproduce, if exist:
1. Select some data
2. Highlight a row in the results tab
3. Right click -> Advanced Copy -> Copy as SQL
4. Paste in editor and notice the table name to be all lower case

Changed files:
plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/SQLServerUtils.java
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/exec/DBExecUtils.java
plugins/org.jkiss.dbeaver.data.transfer/src/org/jkiss/dbeaver/tools/transfer/DTUtils.java
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLQuery.java
plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java

diff --git a/plugins/org.jkiss.dbeaver.data.transfer/src/org/jkiss/dbeaver/tools/transfer/DTUtils.java b/plugins/org.jkiss.dbeaver.data.transfer/src/org/jkiss/dbeaver/tools/transfer/DTUtils.java
index 1b0824c937..6c5acdcd86 100644
--- a/plugins/org.jkiss.dbeaver.data.transfer/src/org/jkiss/dbeaver/tools/transfer/DTUtils.java
+++ b/plugins/org.jkiss.dbeaver.data.transfer/src/org/jkiss/dbeaver/tools/transfer/DTUtils.java
@@ -103,7 +103,7 @@ public class DTUtils {
     public static String getTableNameFromQuery(DBPDataSource dataSource, SQLQueryContainer queryContainer, boolean shortName) {
         SQLScriptElement query = queryContainer.getQuery();
         if (query instanceof SQLQuery) {
-            DBCEntityMetaData singleSource = ((SQLQuery) query).getSingleSource();
+            DBCEntityMetaData singleSource = ((SQLQuery) query).getEntityMetadata(true);
             if (singleSource != null) {
                 SQLDialect dialect = dataSource.getSQLDialect();
                 String entity = transformName(dialect, singleSource.getEntityName());
diff --git a/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/SQLServerUtils.java b/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/SQLServerUtils.java
index dca19c4b62..0a916b991e 100644
--- a/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/SQLServerUtils.java
+++ b/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/SQLServerUtils.java
@@ -312,7 +312,7 @@ public class SQLServerUtils {
     }
 
     public static SQLServerTableBase getTableFromQuery(DBCSession session, SQLQuery sqlQuery, SQLServerDataSource dataSource) throws DBException, SQLException {
-        DBCEntityMetaData singleSource = sqlQuery.getSingleSource();
+        DBCEntityMetaData singleSource = sqlQuery.getEntityMetadata(false);
         String catalogName = null;
         if (singleSource != null) {
             catalogName = singleSource.getCatalogName();
diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/exec/DBExecUtils.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/exec/DBExecUtils.java
index bc75ed5d9d..d9e1ec995d 100644
--- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/exec/DBExecUtils.java
+++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/exec/DBExecUtils.java
@@ -640,7 +640,7 @@ public class DBExecUtils {
                         Object sourceDescriptor = executionSource.getSourceDescriptor();
                         if (sourceDescriptor instanceof SQLQuery) {
                             sqlQuery = (SQLQuery) sourceDescriptor;
-                            entityMeta = sqlQuery.getSingleSource();
+                            entityMeta = sqlQuery.getEntityMetadata(false);
                         }
                         if (entityMeta != null) {
                             entity = DBUtils.getEntityFromMetaData(monitor, session.getExecutionContext(), entityMeta);
diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLQuery.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLQuery.java
index 202b585fe1..26300584f1 100644
--- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLQuery.java
+++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLQuery.java
@@ -73,7 +73,7 @@ public class SQLQuery implements SQLScriptElement {
     @NotNull
     private SQLQueryType type;
     private Statement statement;
-    private SingleTableMeta singleTableMeta;
+    private SingleTableMeta singleTableMeta, rawSingleTableMetadata;
     private List<SQLSelectItem> selectItems;
     private String queryTitle;
 
@@ -214,18 +214,27 @@ public class SQLQuery implements SQLScriptElement {
     }
 
     private void fillSingleSource(Table fromItem) {
-        singleTableMeta = createTableMetaData(fromItem);
+        rawSingleTableMetadata = createOriginalSourceTableMetaData(fromItem);
+        singleTableMeta = createUnquotedTableMetaData(rawSingleTableMetadata);
     }
 
     SingleTableMeta createTableMetaData(Table fromItem) {
+        return createUnquotedTableMetaData(createOriginalSourceTableMetaData(fromItem));
+    }
+
+    private SingleTableMeta createOriginalSourceTableMetaData(Table fromItem) {
         Database database = fromItem.getDatabase();
         String catalogName = database == null ? null : database.getDatabaseName();
         String schemaName = fromItem.getSchemaName();
         String tableName = fromItem.getName();
+        return new SingleTableMeta(catalogName, schemaName, tableName);
+    }
+
+    private SingleTableMeta createUnquotedTableMetaData(SingleTableMeta tableMeta) {
         return new SingleTableMeta(
-            unquoteIdentifier(catalogName),
-            unquoteIdentifier(schemaName),
-            unquoteIdentifier(tableName));
+            unquoteIdentifier(tableMeta.getCatalogName()),
+            unquoteIdentifier(tableMeta.getSchemaName()),
+            unquoteIdentifier(tableMeta.getEntityName()));
     }
 
     private String unquoteIdentifier(String name) {
@@ -345,9 +354,9 @@ public class SQLQuery implements SQLScriptElement {
         return type;
     }
 
-    public DBCEntityMetaData getSingleSource() {
+    public DBCEntityMetaData getEntityMetadata(boolean raw) {
         parseQuery();
-        return singleTableMeta;
+        return raw? rawSingleTableMetadata : singleTableMeta;
     }
 
     public void setParameters(List<SQLQueryParameter> parameters) {
diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java
index d74ccca8ee..71d5be0d3a 100644
--- a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java
+++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java
@@ -2295,8 +2295,8 @@ public class SQLEditor extends SQLEditorBase implements
             SQLQuery query = (SQLQuery) queries.get(0);
             if (query.isDeleteUpdateDangerous()) {
                 String targetName = "multiple tables";
-                if (query.getSingleSource() != null) {
-                    targetName = query.getSingleSource().getEntityName();
+                if (query.getEntityMetadata(false) != null) {
+                    targetName = query.getEntityMetadata(false).getEntityName();
                 }
                 if (ConfirmationDialog.showConfirmDialogEx(
                     ResourceBundle.getBundle(SQLEditorMessages.BUNDLE_NAME),