Skip to content

Commit 3372df4

Browse files
committed
Some translation fine-tunings
- oprimized white spaces appending to reduce tail whitespace cut-offs, in some places whitespaces before ')' may happen though, it is not critical - reverted back some operations, e.g. having one general TranslateIdentifier instead of two - added two more methods to IOutput to make end of the line and space characters be punctuation marks so check for certain symbols in AppendWhitespaceIfNeccessary be faster. - some features checks were cached and in some cases current features are cacked too - Compiling batches from strings uses ReadOnlySpan<> for string trimings
1 parent efc1b69 commit 3372df4

19 files changed

Lines changed: 444 additions & 360 deletions

File tree

Orm/Xtensive.Orm.Firebird/Sql.Drivers.Firebird/v2_5/Compiler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected override void VisitSelectFrom(SqlSelect node)
5050
base.VisitSelectFrom(node);
5151
}
5252
else {
53-
_ = context.Output.Append("FROM RDB$DATABASE ");
53+
_ = context.Output.Append(" FROM RDB$DATABASE");
5454
}
5555
}
5656

@@ -211,8 +211,8 @@ public override void Visit(SqlFunctionCall node)
211211
/// <inheritdoc/>
212212
public override void Visit(SqlAlterSequence node)
213213
{
214-
AppendTranslatedEntry(node);
215-
AppendTranslatedExit(node);
214+
translator.Translate(context, node, NodeSection.Entry);
215+
translator.Translate(context, node, NodeSection.Exit);
216216
}
217217

218218
#region Static helpers

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Compiler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override void Visit(SqlAlterTable node)
4949
((Translator) translator).Translate(context, renameColumnAction);
5050
else if (node.Action is SqlDropConstraint) {
5151
using (context.EnterScope(node)) {
52-
AppendTranslated(node, AlterTableSection.Entry);
52+
AppendTranslatedEntry(node);
5353

5454
var action = node.Action as SqlDropConstraint;
5555
var constraint = action.Constraint as TableConstraint;
@@ -65,7 +65,7 @@ public override void Visit(SqlAlterTable node)
6565
}
6666

6767
AppendTranslated(node, AlterTableSection.DropBehavior);
68-
AppendTranslated(node, AlterTableSection.Exit);
68+
AppendTranslatedExit(node);
6969
}
7070
}
7171
else {
@@ -116,7 +116,7 @@ public override void Visit(SqlQueryExpression node)
116116
using (context.EnterScope(node)) {
117117
//bool needOpeningParenthesis = false;
118118
//bool needClosingParenthesis = false;
119-
AppendTranslated(node, QueryExpressionSection.Entry);
119+
AppendTranslatedEntry(node);
120120
//if (needOpeningParenthesis)
121121
// context.Output.Append("(");
122122
node.Left.AcceptVisitor(this);
@@ -129,7 +129,7 @@ public override void Visit(SqlQueryExpression node)
129129
node.Right.AcceptVisitor(this);
130130
//if (needClosingParenthesis)
131131
// context.Output.Append(")");
132-
AppendTranslated(node, QueryExpressionSection.Exit);
132+
AppendTranslatedExit(node);
133133
}
134134
}
135135

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Translator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public override void Translate(SqlCompilerContext context, SqlConcat node, NodeS
209209
_ = context.Output.AppendOpeningPunctuation("CONCAT(");
210210
break;
211211
case NodeSection.Exit:
212-
_ = context.Output.AppendClosingPunctuation(")");
212+
_ = context.Output.Append(")");
213213
break;
214214
}
215215
}
@@ -273,7 +273,7 @@ public override void Translate(SqlCompilerContext context, SqlCreateIndex node,
273273
TranslateIdentifier(output, index.Name);
274274
_ = output.Append(" USING BTREE ON ");
275275
Translate(context, index.DataTable);
276-
_ = output.AppendSpaceIfNecessary();
276+
_ = output.AppendSpace();
277277
break;
278278
case CreateIndexSection.Exit:
279279
break;
@@ -411,7 +411,7 @@ public override void Translate(SqlCompilerContext context, SqlExtract node, Extr
411411
}
412412
switch (section) {
413413
case ExtractSection.Entry:
414-
_ = context.Output.Append("(extract(");
414+
_ = context.Output.AppendOpeningPunctuation("(extract(");
415415
break;
416416
case ExtractSection.Exit:
417417
_ = context.Output.Append(isMillisecond ? ") % 1000)" : "))");
@@ -464,21 +464,21 @@ public override void Translate(SqlCompilerContext context, SqlCast node, NodeSec
464464
sqlType == SqlType.Interval ||
465465
sqlType == SqlType.DateTime) {
466466
_ = section switch {
467-
NodeSection.Entry => output.Append("CAST("),
467+
NodeSection.Entry => output.AppendOpeningPunctuation("CAST("),
468468
NodeSection.Exit => output.Append("AS ").Append(Translate(node.Type)).Append(")"),
469469
_ => throw new ArgumentOutOfRangeException("section"),
470470
};
471471
}
472472
else if (sqlType == SqlType.Int16 || sqlType == SqlType.Int32) {
473473
_ = section switch {
474-
NodeSection.Entry => output.Append("CAST("),
474+
NodeSection.Entry => output.AppendOpeningPunctuation("CAST("),
475475
NodeSection.Exit => output.Append("AS SIGNED ").Append(Translate(node.Type)).Append(")"),
476476
_ => throw new ArgumentOutOfRangeException(nameof(section)),
477477
};
478478
}
479479
else if (sqlType == SqlType.Decimal) {
480480
_ = section switch {
481-
NodeSection.Entry => output.Append("CAST("),
481+
NodeSection.Entry => output.AppendOpeningPunctuation("CAST("),
482482
NodeSection.Exit => output.Append("AS ").Append(Translate(node.Type)).Append(")"),
483483
_ => throw new ArgumentOutOfRangeException(nameof(section)),
484484
};
@@ -589,9 +589,9 @@ public virtual void Translate(SqlCompilerContext context, SqlRenameColumn action
589589
TranslateIdentifier(output, tableName);
590590
_ = output.Append(" CHANGE COLUMN ");
591591
TranslateIdentifier(output, columnName);
592-
_ = output.AppendSpaceIfNecessary();
592+
_ = output.AppendSpace();
593593
TranslateIdentifier(output, action.NewName);
594-
_ = output.AppendSpaceIfNecessary()
594+
_ = output.AppendSpace()
595595
.Append(Translate(action.Column.DataType));
596596
}
597597

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_6/Translator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public override void Translate(SqlCompilerContext context, SqlCast node, NodeSec
1818
if (node.Type.Type == SqlType.DateTime) {
1919
switch (section) {
2020
case NodeSection.Entry:
21-
_ = context.Output.Append("CAST(");
21+
_ = context.Output.AppendOpeningPunctuation("CAST(");
2222
break;
2323
case NodeSection.Exit:
2424
_ = context.Output.Append("AS ").Append(Translate(node.Type)).Append("(6))");
2525
break;
2626
default:
27-
throw new ArgumentOutOfRangeException("section");
27+
throw new ArgumentOutOfRangeException(nameof(section));
2828
}
2929
}
3030
else {

Orm/Xtensive.Orm.Oracle/Sql.Drivers.Oracle/v09/Compiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected override void VisitSelectFrom(SqlSelect node)
170170
base.VisitSelectFrom(node);
171171
}
172172
else {
173-
_ = context.Output.Append("FROM DUAL");
173+
_ = context.Output.Append(" FROM DUAL");
174174
}
175175
}
176176

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v8_0/Compiler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ public override void Visit(SqlExtract node)
285285
base.Visit(node);
286286
}
287287

288-
289288
protected SqlExpression DateTimeOffsetExtractDate(SqlExpression timestamp) =>
290289
SqlDml.FunctionCall("DATE", timestamp);
291290

@@ -301,11 +300,12 @@ protected SqlExpression DateTimeOffsetToLocalDateTime(SqlExpression timestamp) =
301300
protected void DateTimeOffsetExtractOffset(SqlExtract node)
302301
{
303302
using (context.EnterScope(node)) {
304-
AppendTranslated(node, ExtractSection.Entry);
303+
AppendTranslatedEntry(node);
305304
translator.Translate(context.Output, node.DateTimeOffsetPart);
306305
AppendTranslated(node, ExtractSection.From);
307306
node.Operand.AcceptVisitor(this);
308-
AppendTranslated(node, ExtractSection.Exit);
307+
AppendSpace();
308+
AppendTranslatedExit(node);
309309
AppendTranslated(SqlNodeType.Multiply);
310310
OneSecondInterval.AcceptVisitor(this);
311311
}

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v8_0/Translator.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public override void Translate(SqlCompilerContext context, SqlCreateTable node,
200200
{
201201
switch (section) {
202202
case CreateTableSection.Exit:
203-
_ = context.Output.Append("WITHOUT OIDS ");
203+
_ = context.Output.Append(" WITHOUT OIDS");
204204
break;
205205
}
206206
base.Translate(context, node, section);
@@ -351,7 +351,7 @@ public override void Translate(SqlCompilerContext context, SqlExtract node, Extr
351351
}
352352
switch (section) {
353353
case ExtractSection.Entry:
354-
_ = context.Output.Append(isSecond ? "(trunc(extract(" : "(extract(");
354+
_ = context.Output.AppendOpeningPunctuation(isSecond ? "(trunc(extract(" : "(extract(");
355355
break;
356356
case ExtractSection.Exit:
357357
_ = context.Output.Append(isMillisecond
@@ -383,7 +383,9 @@ public override void Translate(SqlCompilerContext context, SqlDeclareCursor node
383383
_ = output.Append("CURSOR");
384384
break;
385385
case DeclareCursorSection.Holdability:
386-
_ = output.Append(node.Cursor.WithHold ? "WITH HOLD" : "");
386+
if (node.Cursor.WithHold) {
387+
_ = output.Append("WITH HOLD");
388+
}
387389
break;
388390
case DeclareCursorSection.Returnability:
389391
case DeclareCursorSection.Updatability:
@@ -751,7 +753,7 @@ public override void Translate(SqlCompilerContext context, SqlNextValue node, No
751753
{
752754
switch (section) {
753755
case NodeSection.Entry:
754-
_ = context.Output.Append("nextval('");
756+
_ = context.Output.AppendOpeningPunctuation("nextval('");
755757
break;
756758
case NodeSection.Exit:
757759
_ = context.Output.Append("')");
@@ -765,7 +767,7 @@ public override void Translate(SqlCompilerContext context, SqlCast node, NodeSec
765767
// casting this way behaves differently: -32768::int2 is out of range ! We need (-32768)::int2
766768
switch (section) {
767769
case NodeSection.Entry:
768-
_ = context.Output.Append("(");
770+
_ = context.Output.AppendOpeningPunctuation("(");
769771
break;
770772
case NodeSection.Exit:
771773
_ = context.Output.Append(")::").Append(Translate(node.Type));

Orm/Xtensive.Orm.PostgreSql/Sql.Drivers.PostgreSql/v8_1/Translator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Translate(SqlCompilerContext context, SqlExtract node, Extr
2727
}
2828
switch (section) {
2929
case ExtractSection.From:
30-
_ = context.Output.Append("from justify_hours(");
30+
_ = context.Output.AppendOpeningPunctuation("from justify_hours(");
3131
break;
3232
case ExtractSection.Exit:
3333
_ = context.Output.Append("))");

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v09/Compiler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override void Visit(SqlSelect node)
4343
using (context.EnterScope(node)) {
4444
var comment = node.Comment;
4545
VisitCommentIfBefore(comment);
46-
AppendTranslated(node, SelectSection.Entry);
46+
AppendTranslatedEntry(node);
4747
VisitCommentIfWithin(comment);
4848
VisitSelectLimitOffset(node);
4949
VisitSelectHints(node);
@@ -53,7 +53,7 @@ public override void Visit(SqlSelect node)
5353
VisitSelectGroupBy(node);
5454
VisitSelectOrderBy(node);
5555
VisitSelectLock(node);
56-
AppendTranslated(node, SelectSection.Exit);
56+
AppendTranslatedExit(node);
5757
VisitCommentIfAfter(comment);
5858
}
5959
}
@@ -81,7 +81,7 @@ protected override void VisitUpdateLimit(SqlUpdate node)
8181
}
8282

8383
AppendTranslated(node, UpdateSection.Limit);
84-
_ = context.Output.Append("(");
84+
_ = context.Output.AppendOpeningPunctuation("(");
8585
node.Limit.AcceptVisitor(this);
8686
_ = context.Output.Append(")");
8787
}
@@ -110,7 +110,7 @@ protected override void VisitDeleteLimit(SqlDelete node)
110110
}
111111

112112
AppendTranslated(node, DeleteSection.Limit);
113-
_ = context.Output.Append("(");
113+
_ = context.Output.AppendOpeningPunctuation("(");
114114
node.Limit.AcceptVisitor(this);
115115
_ = context.Output.Append(")");
116116
}
@@ -307,7 +307,7 @@ public override void Visit(SqlContainsTable node)
307307
{
308308
var output = context.Output;
309309

310-
_ = output.Append("CONTAINSTABLE(");
310+
_ = output.AppendOpeningPunctuation("CONTAINSTABLE(");
311311
AppendTranslated(node.TargetTable.DataTable);
312312
_ = output.Append(", ");
313313

@@ -335,7 +335,7 @@ public override void Visit(SqlFreeTextTable node)
335335
{
336336
var output = context.Output;
337337

338-
_ = output.Append("FREETEXTTABLE(");
338+
_ = output.AppendOpeningPunctuation("FREETEXTTABLE(");
339339
AppendTranslated(node.TargetTable.DataTable);
340340
_ = output.Append(", ");
341341

@@ -451,7 +451,7 @@ private void TranslateSingleColumnName(SqlTableColumn column, SqlTableColumn ast
451451
private void TranslateJoinedColumnNames(SqlTableColumnCollection targetColumns)
452452
{
453453
var output = context.Output;
454-
bool first = true;
454+
var first = true;
455455
foreach (var column in targetColumns) {
456456
if (first) {
457457
first = false;

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v09/Translator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public override void Translate(SqlCompilerContext context, SqlJoinExpression nod
264264

265265
Translate(output, node.JoinType);
266266
if (joinHint != null) {
267-
_ = output.Append(" ").Append(Translate(joinHint.Method));
267+
_ = output.AppendSpace().Append(Translate(joinHint.Method));
268268
}
269269
_ = output.Append(" JOIN");
270270
break;
@@ -356,7 +356,7 @@ static void AppendHint(IOutput output, string hint, ref bool hasHints)
356356
_ = output.Append(", ");
357357
}
358358
else {
359-
_ = output.Append("OPTION (");
359+
_ = output.Append(" OPTION (");
360360
hasHints = true;
361361
}
362362
_ = output.Append(hint);
@@ -407,7 +407,6 @@ public override void Translate(SqlCompilerContext context, SqlCreateIndex node,
407407
}
408408
var index = node.Index;
409409
if (index is not FullTextIndex ftIndex) {
410-
411410
return;
412411
}
413412
if (ftIndex.FullTextCatalog != null) {
@@ -507,8 +506,8 @@ public override void Translate(SqlCompilerContext context, SqlTrim node, TrimSec
507506
_ => throw new ArgumentOutOfRangeException("node.TrimType"),
508507
},
509508
TrimSection.Exit => node.TrimType switch {
510-
SqlTrimType.Leading or SqlTrimType.Trailing => output.AppendClosingPunctuation(")"),
511-
SqlTrimType.Both => output.AppendClosingPunctuation("))"),
509+
SqlTrimType.Leading or SqlTrimType.Trailing => output.Append(")"),
510+
SqlTrimType.Both => output.Append("))"),
512511
_ => throw new ArgumentOutOfRangeException("node.TrimType"),
513512
},
514513
_ => throw new ArgumentOutOfRangeException(nameof(section)),

0 commit comments

Comments
 (0)