提交 697a8195 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #11934 from dbeaver/sql-server-unicode-string#11058

#11058 SQL Server use unicode prefix for strings

Former-commit-id: 4275aa8f
...@@ -59,9 +59,9 @@ public abstract class SQLServerBaseTableManager<OBJECT extends SQLServerTableBas ...@@ -59,9 +59,9 @@ public abstract class SQLServerBaseTableManager<OBJECT extends SQLServerTableBas
new SQLDatabasePersistAction( new SQLDatabasePersistAction(
"Add table comment", "Add table comment",
"EXEC " + SQLServerUtils.getSystemTableName(table.getDatabase(), isUpdate ? "sp_updateextendedproperty" : "sp_addextendedproperty") + "EXEC " + SQLServerUtils.getSystemTableName(table.getDatabase(), isUpdate ? "sp_updateextendedproperty" : "sp_addextendedproperty") +
" 'MS_Description', N" + SQLUtils.quoteString(command.getObject(), command.getObject().getDescription()) + "," + " 'MS_Description', " + SQLUtils.quoteString(table, table.getDescription()) + "," +
" 'schema', N'" + table.getSchema().getName() + "'," + " 'schema', " + SQLUtils.quoteString(table, table.getSchema().getName()) + "," +
" '" + (table.isView() ? "view" : "table") + "', N'" + table.getName() + "'")); " '" + (table.isView() ? "view" : "table") + "', " + SQLUtils.quoteString(table, table.getName())));
} }
} }
...@@ -74,7 +74,7 @@ public abstract class SQLServerBaseTableManager<OBJECT extends SQLServerTableBas ...@@ -74,7 +74,7 @@ public abstract class SQLServerBaseTableManager<OBJECT extends SQLServerTableBas
"Rename table", "Rename table",
"EXEC " + SQLServerUtils.getSystemTableName(object.getDatabase(), "sp_rename") + "EXEC " + SQLServerUtils.getSystemTableName(object.getDatabase(), "sp_rename") +
" N'" + object.getSchema().getFullyQualifiedName(DBPEvaluationContext.DML) + "." + DBUtils.getQuotedIdentifier(object.getDataSource(), command.getOldName()) + " N'" + object.getSchema().getFullyQualifiedName(DBPEvaluationContext.DML) + "." + DBUtils.getQuotedIdentifier(object.getDataSource(), command.getOldName()) +
"' , N'" + DBUtils.getQuotedIdentifier(object.getDataSource(), command.getNewName()) + "', 'OBJECT'") "', " + SQLUtils.quoteString(object.getDataSource(), command.getNewName()) + ", 'OBJECT'")
); );
} }
......
...@@ -120,9 +120,9 @@ public class SQLServerProcedureManager extends SQLServerObjectManager<SQLServerP ...@@ -120,9 +120,9 @@ public class SQLServerProcedureManager extends SQLServerObjectManager<SQLServerP
new SQLDatabasePersistAction( new SQLDatabasePersistAction(
"Add procedure comment", "Add procedure comment",
"EXEC " + SQLServerUtils.getSystemTableName(database, isUpdate ? "sp_updateextendedproperty" : "sp_addextendedproperty") + "EXEC " + SQLServerUtils.getSystemTableName(database, isUpdate ? "sp_updateextendedproperty" : "sp_addextendedproperty") +
" 'MS_Description', N" + SQLUtils.quoteString(command.getObject(), command.getObject().getDescription()) + "," + " 'MS_Description', " + SQLUtils.quoteString(procedure, procedure.getDescription()) + "," +
" 'schema', N'" + procedure.getContainer().getName() + "'," + " 'schema', " + SQLUtils.quoteString(procedure, procedure.getContainer().getName()) + "," +
" 'procedure', N'" + procedure.getName() + "'")); " 'procedure', " + SQLUtils.quoteString(procedure, procedure.getName())));
} }
} }
......
...@@ -170,10 +170,10 @@ public class SQLServerTableColumnManager extends SQLTableColumnManager<SQLServer ...@@ -170,10 +170,10 @@ public class SQLServerTableColumnManager extends SQLTableColumnManager<SQLServer
new SQLDatabasePersistAction( new SQLDatabasePersistAction(
"Add column comment", "Add column comment",
"EXEC " + SQLServerUtils.getSystemTableName(column.getTable().getDatabase(), isUpdate ? "sp_updateextendedproperty" : "sp_addextendedproperty") + "EXEC " + SQLServerUtils.getSystemTableName(column.getTable().getDatabase(), isUpdate ? "sp_updateextendedproperty" : "sp_addextendedproperty") +
" 'MS_Description', N" + SQLUtils.quoteString(command.getObject(), command.getObject().getDescription()) + "," + " 'MS_Description', " + SQLUtils.quoteString(column, column.getDescription()) + "," +
" 'schema', N'" + column.getTable().getSchema().getName() + "'," + " 'schema', " + SQLUtils.quoteString(column, column.getTable().getSchema().getName()) + "," +
" 'table', N'" + column.getTable().getName() + "'," + " 'table', " + SQLUtils.quoteString(column, column.getTable().getName()) + "," +
" 'column', N'" + column.getName() + "'")); " 'column', " + SQLUtils.quoteString(column, column.getName())));
} }
if (totalProps > 0) { if (totalProps > 0) {
actionList.add(new SQLDatabasePersistAction( actionList.add(new SQLDatabasePersistAction(
......
...@@ -248,4 +248,27 @@ public class SQLServerDialect extends JDBCSQLDialect { ...@@ -248,4 +248,27 @@ public class SQLServerDialect extends JDBCSQLDialect {
sql.append("\nSELECT\t'Return Value' = @return_value\n\n"); sql.append("\nSELECT\t'Return Value' = @return_value\n\n");
sql.append("GO\n\n"); sql.append("GO\n\n");
} }
@Override
public boolean isQuotedString(String string) {
if (string.length() >= 3 && string.charAt(0) == 'N') {
// https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql
return super.isQuotedString(string.substring(1));
}
return super.isQuotedString(string);
}
@Override
public String getQuotedString(String string) {
return 'N' + super.getQuotedString(string);
}
@Override
public String getUnquotedString(String string) {
if (string.length() >= 3 && string.charAt(0) == 'N') {
// https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql
return super.getUnquotedString(string.substring(1));
}
return super.getUnquotedString(string);
}
} }
...@@ -155,7 +155,7 @@ public class MySQLTableColumn extends JDBCTableColumn<MySQLTableBase> implements ...@@ -155,7 +155,7 @@ public class MySQLTableColumn extends JDBCTableColumn<MySQLTableBase> implements
case STRING: case STRING:
// Escape if it is not NULL (#1913) // Escape if it is not NULL (#1913)
// Although I didn't reproduce that locally - perhaps depends on server config. // Although I didn't reproduce that locally - perhaps depends on server config.
if (!SQLConstants.NULL_VALUE.equals(defaultValue) && !SQLUtils.isStringQuoted(defaultValue)) { if (!SQLConstants.NULL_VALUE.equals(defaultValue) && !SQLUtils.isStringQuoted(getDataSource(), defaultValue)) {
defaultValue = SQLUtils.quoteString(getDataSource(), defaultValue); defaultValue = SQLUtils.quoteString(getDataSource(), defaultValue);
} }
break; break;
......
...@@ -433,6 +433,21 @@ public abstract class AbstractSQLDialect implements SQLDialect { ...@@ -433,6 +433,21 @@ public abstract class AbstractSQLDialect implements SQLDialect {
return identifier; return identifier;
} }
@Override
public boolean isQuotedString(String string) {
return string.length() >= 2 && string.charAt(0) == '\'' && string.charAt(string.length() - 1) == '\'';
}
@Override
public String getQuotedString(String string) {
return '\'' + escapeString(string) + '\'';
}
@Override
public String getUnquotedString(String string) {
return isQuotedString(string) ? unEscapeString(string.substring(1, string.length() - 1)) : string;
}
@NotNull @NotNull
@Override @Override
public String escapeString(String string) { public String escapeString(String string) {
......
...@@ -292,6 +292,12 @@ public interface SQLDialect { ...@@ -292,6 +292,12 @@ public interface SQLDialect {
String getUnquotedIdentifier(String identifier); String getUnquotedIdentifier(String identifier);
boolean isQuotedString(String string);
String getQuotedString(String string);
String getUnquotedString(String string);
/** /**
* Escapes string to make usable inside of SQL queries. * Escapes string to make usable inside of SQL queries.
* Basically it has to escape only ' character which delimits strings. * Basically it has to escape only ' character which delimits strings.
......
...@@ -210,9 +210,8 @@ public final class SQLUtils { ...@@ -210,9 +210,8 @@ public final class SQLUtils {
} }
} }
public static boolean isStringQuoted(String string) public static boolean isStringQuoted(DBSObject object, String string) {
{ return object.getDataSource().getSQLDialect().isQuotedString(string);
return string.length() > 1 && string.startsWith("'") && string.endsWith("'");
} }
public static String quoteString(DBSObject object, String string) public static String quoteString(DBSObject object, String string)
...@@ -222,7 +221,7 @@ public final class SQLUtils { ...@@ -222,7 +221,7 @@ public final class SQLUtils {
public static String quoteString(DBPDataSource dataSource, String string) public static String quoteString(DBPDataSource dataSource, String string)
{ {
return "'" + escapeString(dataSource, string) + "'"; return dataSource.getSQLDialect().getQuotedString(string);
} }
public static String escapeString(DBPDataSource dataSource, String string) public static String escapeString(DBPDataSource dataSource, String string)
...@@ -661,12 +660,9 @@ public final class SQLUtils { ...@@ -661,12 +660,9 @@ public final class SQLUtils {
case STRING: case STRING:
case ROWID: case ROWID:
if (sqlDialect != null) { if (sqlDialect != null) {
strValue = sqlDialect.escapeString(strValue); return sqlDialect.getTypeCastClause(attribute, sqlDialect.getQuotedString(strValue));
}
if (dataKind == DBPDataKind.STRING || !(strValue.startsWith("'") && strValue.endsWith("'"))) {
strValue = '\'' + strValue + '\'';
} }
return sqlDialect.getTypeCastClause(attribute, strValue); return strValue;
default: default:
if (sqlDialect != null) { if (sqlDialect != null) {
return sqlDialect.escapeScriptValue(attribute, value, strValue); return sqlDialect.escapeScriptValue(attribute, value, strValue);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册