Skip to content

Commit 8926202

Browse files
committed
Replace SqlNodeActualizer with special placeholder
1 parent 4f30e7f commit 8926202

8 files changed

Lines changed: 111 additions & 15 deletions

File tree

Orm/Xtensive.Orm/Sql/Compiler/Internals/Nodes/ContainerNode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Globalization;
99
using System.Linq;
1010
using System.Text;
11+
using Xtensive.Sql.Model;
1112

1213
namespace Xtensive.Sql.Compiler
1314
{
@@ -135,6 +136,9 @@ public StringBuilder StringBuilder
135136

136137
public void AppendPlaceholderWithId(object id) => Add(new PlaceholderNode(id));
137138

139+
public void AppendSchemaNodePlaceholder(SchemaNode schemaNode, SqlHelper.EscapeSetup escapeSetup, bool databaseNameRequired) =>
140+
Add(new SchemaNodePlaceholderNode(schemaNode, escapeSetup, databaseNameRequired));
141+
138142
public void AppendIndent()
139143
{
140144
if (Indent > 0) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2003-2010 Xtensive LLC.
2+
// All rights reserved.
3+
// For conditions of distribution and use, see license.
4+
// Created by: Denis Krjuchkov
5+
// Created: 2009.07.15
6+
7+
using Xtensive.Sql.Model;
8+
9+
namespace Xtensive.Sql.Compiler
10+
{
11+
internal sealed class SchemaNodePlaceholderNode : PlaceholderNode
12+
{
13+
public readonly SchemaNode SchemaNode;
14+
15+
public readonly SqlHelper.EscapeSetup EscapeSetup;
16+
17+
public readonly bool DbQualified;
18+
19+
internal override void AcceptVisitor(NodeVisitor visitor) => base.AcceptVisitor(visitor);
20+
21+
public SchemaNodePlaceholderNode(SchemaNode table, in SqlHelper.EscapeSetup escapeSetup, bool requireDatabaseName)
22+
: base(table)
23+
{
24+
SchemaNode = table;
25+
EscapeSetup = escapeSetup;
26+
DbQualified = requireDatabaseName;
27+
}
28+
}
29+
}

Orm/Xtensive.Orm/Sql/Compiler/Internals/PostCompiler.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,25 @@ public override void Visit(VariantNode node)
4747

4848
public override void Visit(PlaceholderNode node)
4949
{
50-
string value;
51-
if (!configuration.PlaceholderValues.TryGetValue(node.Id, out value))
52-
throw new InvalidOperationException(string.Format(Strings.ExValueForPlaceholderXIsNotSet, node.Id));
53-
result.Append(value);
50+
if (node is SchemaNodePlaceholderNode schemaPlaceHolder) {
51+
Visit(schemaPlaceHolder);
52+
}
53+
else {
54+
if (!configuration.PlaceholderValues.TryGetValue(node.Id, out var value))
55+
throw new InvalidOperationException(string.Format(Strings.ExValueForPlaceholderXIsNotSet, node.Id));
56+
_ = result.Append(value);
57+
}
58+
}
59+
60+
private void Visit(SchemaNodePlaceholderNode node)
61+
{
62+
var schema = node.SchemaNode.Schema;
63+
64+
var names = (node.DbQualified)
65+
? new string[] { schema.Catalog.GetActualDbName(configuration.DatabaseMapping), schema.GetActualDbName(configuration.SchemaMapping), node.SchemaNode.DbName }
66+
: new string[] { schema.GetActualDbName(configuration.SchemaMapping), node.SchemaNode.DbName };
67+
68+
_ = result.Append(SqlHelper.Quote(node.EscapeSetup, names));
5469
}
5570

5671
public override void Visit(CycleItemNode node)

Orm/Xtensive.Orm/Sql/Compiler/SqlCompilerConfiguration.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Created by: Denis Krjuchkov
55
// Created: 2009.07.15
66

7+
using System;
78
using System.Collections.Generic;
89
using System.Collections.ObjectModel;
910
using JetBrains.Annotations;
@@ -34,14 +35,21 @@ public sealed class SqlCompilerConfiguration
3435
/// </summary>
3536
public SqlCommentLocation CommentLocation { get; set; } = SqlCommentLocation.Nowhere;
3637

38+
/// <summary>
39+
/// Indicated whether storage schema was shared over several storage nodes.
40+
/// </summary>
41+
public bool SharedStorageSchema { get; init; } = false;
42+
3743
/// <summary>
3844
/// Gets database mapping.
3945
/// </summary>
46+
[Obsolete("No longer in use")]
4047
public IReadOnlyDictionary<string, string> SchemaMapping { get; private set; }
4148

4249
/// <summary>
4350
/// Gets database mapping.
4451
/// </summary>
52+
[Obsolete("No longer in use")]
4553
public IReadOnlyDictionary<string, string> DatabaseMapping { get; private set; }
4654

4755
/// <summary>
@@ -58,11 +66,14 @@ public SqlCompilerConfiguration Clone()
5866

5967
public SqlCompilerConfiguration()
6068
{
69+
#pragma warning disable CS0612 // Type or member is obsolete
6170
SchemaMapping = null;
6271
DatabaseMapping = null;
72+
#pragma warning restore CS0612 // Type or member is obsolete
6373
}
6474

65-
public SqlCompilerConfiguration([NotNull]IDictionary<string, string> databaseMapping, [NotNull]IDictionary<string, string> schemaMapping)
75+
[Obsolete]
76+
public SqlCompilerConfiguration([NotNull] IDictionary<string, string> databaseMapping, [NotNull] IDictionary<string, string> schemaMapping)
6677
{
6778
ArgumentValidator.EnsureArgumentNotNull(databaseMapping, "databaseMapping");
6879
ArgumentValidator.EnsureArgumentNotNull(schemaMapping, "schemaMapping");

Orm/Xtensive.Orm/Sql/Compiler/SqlCompilerContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ public class SqlCompilerContext
2424

2525
public SqlCompilerNamingOptions NamingOptions { get; private set; }
2626

27+
[Obsolete("No longer in use")]
2728
public SqlNodeActualizer SqlNodeActualizer { get; private set; }
2829

2930
public SqlNode[] GetTraversalPath() =>
3031
traversalPath ??= traversalStack.ToArray();
3132

33+
public bool SharedStorageSchema { get; }
34+
3235
public bool HasOptions(SqlCompilerNamingOptions requiredOptions)
3336
{
3437
return (NamingOptions & requiredOptions)==requiredOptions;
@@ -143,10 +146,14 @@ internal SqlCompilerContext(SqlCompilerConfiguration configuration)
143146
if (configuration.DatabaseQualifiedObjects)
144147
NamingOptions |= SqlCompilerNamingOptions.DatabaseQualifiedObjects;
145148

149+
SharedStorageSchema = configuration.SharedStorageSchema;
150+
146151
TableNameProvider = new SqlTableNameProvider(this);
147152
ParameterNameProvider = new SqlParameterNameProvider(configuration);
148153
Output = new ContainerNode();
154+
#pragma warning disable CS0618 // Type or member is obsolete
149155
SqlNodeActualizer = new SqlNodeActualizer(configuration.DatabaseMapping, configuration.SchemaMapping);
156+
#pragma warning restore CS0618 // Type or member is obsolete
150157
}
151158
}
152159
}

Orm/Xtensive.Orm/Sql/Compiler/SqlNodeActualizer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Created by: Alexey Kulakov
55
// Created: 2017.03.24
66

7+
using System;
78
using System.Collections.Generic;
89
using Xtensive.Sql.Model;
910

@@ -12,6 +13,7 @@ namespace Xtensive.Sql.Compiler
1213
/// <summary>
1314
/// Applies configured mappings to <see cref="Catalog"/>'s or <see cref="Schema"/>'s name to get actual name.
1415
/// </summary>
16+
[Obsolete]
1517
public sealed class SqlNodeActualizer
1618
{
1719
private readonly IReadOnlyDictionary<string, string> databaseMapping;

Orm/Xtensive.Orm/Sql/Compiler/SqlPostCompilerConfiguration.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// Created: 2009.11.07
66

77
using System.Collections.Generic;
8+
using System.Collections.ObjectModel;
9+
using JetBrains.Annotations;
10+
using Xtensive.Core;
811

912
namespace Xtensive.Sql.Compiler
1013
{
@@ -13,20 +16,39 @@ namespace Xtensive.Sql.Compiler
1316
/// </summary>
1417
public sealed class SqlPostCompilerConfiguration
1518
{
16-
public HashSet<object> AlternativeBranches { get; private set; }
19+
public HashSet<object> AlternativeBranches { get; private set; } = new HashSet<object>();
1720

18-
public Dictionary<object, string> PlaceholderValues { get; private set; }
21+
public Dictionary<object, string> PlaceholderValues { get; private set; } = new Dictionary<object, string>();
1922

20-
public Dictionary<object, List<string[]>> DynamicFilterValues { get; private set; }
23+
public Dictionary<object, List<string[]>> DynamicFilterValues { get; private set; } = new Dictionary<object, List<string[]>>();
24+
25+
/// <summary>
26+
/// Gets database mapping.
27+
/// </summary>
28+
public IReadOnlyDictionary<string, string> SchemaMapping { get; private set; }
29+
30+
/// <summary>
31+
/// Gets database mapping.
32+
/// </summary>
33+
public IReadOnlyDictionary<string, string> DatabaseMapping { get; private set; }
2134

2235

2336
// Constructors
2437

2538
public SqlPostCompilerConfiguration()
2639
{
27-
AlternativeBranches = new HashSet<object>();
28-
PlaceholderValues = new Dictionary<object, string>();
29-
DynamicFilterValues = new Dictionary<object, List<string[]>>();
40+
// this prevents wrong query to be executed.
41+
SchemaMapping = null;
42+
DatabaseMapping = null;
43+
}
44+
45+
public SqlPostCompilerConfiguration([NotNull] IReadOnlyDictionary<string, string> databaseMapping, [NotNull] IReadOnlyDictionary<string, string> schemaMapping)
46+
{
47+
ArgumentValidator.EnsureArgumentNotNull(databaseMapping, "databaseMapping");
48+
ArgumentValidator.EnsureArgumentNotNull(schemaMapping, "schemaMapping");
49+
50+
DatabaseMapping = databaseMapping;
51+
SchemaMapping = schemaMapping;
3052
}
3153
}
3254
}

Orm/Xtensive.Orm/Sql/Compiler/SqlTranslator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,13 +1954,19 @@ public virtual void Translate(SqlCompilerContext context, SchemaNode node)
19541954

19551955
var dbQualified = node.Schema.Catalog != null
19561956
&& context.HasOptions(SqlCompilerNamingOptions.DatabaseQualifiedObjects);
1957-
var actualizer = context.SqlNodeActualizer;
19581957

1959-
if (dbQualified) {
1960-
TranslateIdentifier(output, actualizer.Actualize(node.Schema.Catalog), actualizer.Actualize(node.Schema), node.GetDbNameInternal());
1958+
if (context.SharedStorageSchema) {
1959+
// if schema is shared we use placeholders to translate
1960+
// schema node in PostCompiler
1961+
output.AppendSchemaNodePlaceholder(node, EscapeSetup, dbQualified);
19611962
}
19621963
else {
1963-
TranslateIdentifier(output, actualizer.Actualize(node.Schema), node.DbName);
1964+
if (dbQualified) {
1965+
TranslateIdentifier(output, node.Schema.Catalog.DbName, node.Schema.DbName, node.DbName);
1966+
}
1967+
else {
1968+
TranslateIdentifier(output, node.Schema.DbName, node.DbName);
1969+
}
19641970
}
19651971
}
19661972

0 commit comments

Comments
 (0)