Skip to content

Commit bea48d2

Browse files
committed
Mysql sql compiler and translators revised
- no overload for BatchItemDelimiter, seems to be base delimiter works just fine - no usage of QuoteIndetifier, TranslateIdentifier is used - current formatting applied
1 parent f133b50 commit bea48d2

3 files changed

Lines changed: 255 additions & 350 deletions

File tree

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

Lines changed: 60 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2011-2021 Xtensive LLC.
1+
// Copyright (C) 2011-2022 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Malisa Ncube
@@ -27,7 +27,7 @@ public override void Visit(SqlSelect node)
2727
using (context.EnterScope(node)) {
2828
var comment = node.Comment;
2929
VisitCommentIfBefore(comment);
30-
AppendTranslated(node, SelectSection.Entry);
30+
AppendTranslatedEntry(node);
3131
VisitCommentIfWithin(comment);
3232
VisitSelectColumns(node);
3333
VisitSelectFrom(node);
@@ -37,16 +37,15 @@ public override void Visit(SqlSelect node)
3737
VisitSelectOrderBy(node);
3838
VisitSelectLimitOffset(node);
3939
VisitSelectLock(node);
40-
AppendTranslated(node, SelectSection.Exit);
40+
AppendTranslatedExit(node);
4141
VisitCommentIfAfter(comment);
4242
}
4343
}
4444

4545
/// <inheritdoc/>
4646
public override void Visit(SqlAlterTable node)
4747
{
48-
var renameColumnAction = node.Action as SqlRenameColumn;
49-
if (renameColumnAction!=null)
48+
if (node.Action is SqlRenameColumn renameColumnAction)
5049
((Translator) translator).Translate(context, renameColumnAction);
5150
else if (node.Action is SqlDropConstraint) {
5251
using (context.EnterScope(node)) {
@@ -56,19 +55,22 @@ public override void Visit(SqlAlterTable node)
5655
var constraint = action.Constraint as TableConstraint;
5756
AppendTranslated(node, AlterTableSection.DropConstraint);
5857
if (constraint is ForeignKey) {
59-
context.Output.Append("FOREIGN KEY ");
58+
_ = context.Output.Append("FOREIGN KEY ");
6059
translator.TranslateIdentifier(context.Output, constraint.DbName);
6160
}
6261
else if (constraint is PrimaryKey)
63-
context.Output.Append("PRIMARY KEY ");
64-
else
62+
_ = context.Output.Append("PRIMARY KEY ");
63+
else {
6564
AppendTranslated(constraint, ConstraintSection.Entry);
65+
}
66+
6667
AppendTranslated(node, AlterTableSection.DropBehavior);
6768
AppendTranslated(node, AlterTableSection.Exit);
6869
}
6970
}
70-
else
71+
else {
7172
base.Visit(node);
73+
}
7274
}
7375

7476
/// <inheritdoc/>
@@ -100,47 +102,33 @@ public override void Visit(SqlBinary node)
100102
/// <inheritdoc/>
101103
public override void Visit(SqlUnary node)
102104
{
103-
if (node.NodeType==SqlNodeType.BitNot) {
105+
if (node.NodeType == SqlNodeType.BitNot) {
104106
Visit(BitNot(node.Operand));
105107
return;
106108
}
107109
base.Visit(node);
108110
}
109111

110-
protected virtual SqlExpression DateTimeSubtractDateTime(SqlExpression date1, SqlExpression date2)
111-
{
112-
return CastToDecimal(DateDiffDay(date1, date2), 18, 0) * NanosecondsPerDay
113-
+
114-
CastToDecimal(DateDiffMicrosecond(DateAddDay(date2, DateDiffDay(date1, date2)), date1), 18, 0) * NanosecondsPerMicrosecond;
115-
}
116-
117-
protected virtual SqlExpression DateTimeAddInterval(SqlExpression date, SqlExpression interval)
118-
{
119-
return DateAddMicrosecond(
120-
DateAddDay(date, ((interval - (interval % NanosecondsPerDay)) + ((interval % NanosecondsPerDay) > (NanosecondsPerDay / 2) ? 0 : 1)) / NanosecondsPerDay),
121-
(interval / NanosecondsPerMillisecond * NanosecondsPerMicrosecond) % (MillisecondsPerDay * NanosecondsPerMicrosecond));
122-
}
123-
124112
/// <inheritdoc/>
125113
/// //Thanks to Csaba Beer.
126114
public override void Visit(SqlQueryExpression node)
127115
{
128116
using (context.EnterScope(node)) {
129-
bool needOpeningParenthesis = false;
130-
bool needClosingParenthesis = false;
117+
//bool needOpeningParenthesis = false;
118+
//bool needClosingParenthesis = false;
131119
AppendTranslated(node, QueryExpressionSection.Entry);
132-
if (needOpeningParenthesis)
133-
context.Output.Append("(");
120+
//if (needOpeningParenthesis)
121+
// context.Output.Append("(");
134122
node.Left.AcceptVisitor(this);
135-
if (needClosingParenthesis)
136-
context.Output.Append(")");
123+
//if (needClosingParenthesis)
124+
// context.Output.Append(")");
137125
AppendTranslated(node.NodeType);
138126
AppendTranslated(node, QueryExpressionSection.All);
139-
if (needOpeningParenthesis)
140-
context.Output.Append("(");
127+
//if (needOpeningParenthesis)
128+
// context.Output.Append("(");
141129
node.Right.AcceptVisitor(this);
142-
if (needClosingParenthesis)
143-
context.Output.Append(")");
130+
//if (needClosingParenthesis)
131+
// context.Output.Append(")");
144132
AppendTranslated(node, QueryExpressionSection.Exit);
145133
}
146134
}
@@ -199,6 +187,7 @@ public override void Visit(SqlFunctionCall node)
199187
base.Visit(node);
200188
}
201189

190+
/// <inheritdoc/>
202191
public override void VisitSelectLimitOffset(SqlSelect node)
203192
{
204193
if (!node.Limit.IsNullReference()) {
@@ -208,80 +197,74 @@ public override void VisitSelectLimitOffset(SqlSelect node)
208197
if (!node.Offset.IsNullReference()) {
209198
if (node.Limit.IsNullReference()) {
210199
AppendTranslated(node, SelectSection.Limit);
211-
context.Output.Append(" 18446744073709551615 "); // magic number from http://dev.mysql.com/doc/refman/5.0/en/select.html
200+
_ = context.Output.Append(" 18446744073709551615 "); // magic number from http://dev.mysql.com/doc/refman/5.0/en/select.html
212201
}
213202
AppendTranslated(node, SelectSection.Offset);
214203
node.Offset.AcceptVisitor(this);
215204
}
216205
}
217206

207+
/// <inheritdoc/>
218208
public override void Visit(SqlExtract node)
219209
{
220-
if (node.DateTimePart==SqlDateTimePart.DayOfWeek || node.DateTimePart==SqlDateTimePart.DayOfYear) {
221-
Visit(SqlDml.FunctionCall(node.DateTimePart.ToString(), node.Operand));
222-
return;
210+
if (node.DateTimePart == SqlDateTimePart.DayOfWeek || node.DateTimePart == SqlDateTimePart.DayOfYear) {
211+
Visit(SqlDml.FunctionCall(node.DateTimePart.ToString(), node.Operand));
212+
return;
223213
}
224214
base.Visit(node);
225215
}
226216

227-
#region Static helpers
228-
229-
private static SqlCast CastToLong(SqlExpression arg)
217+
protected virtual SqlExpression DateTimeSubtractDateTime(SqlExpression date1, SqlExpression date2)
230218
{
231-
return SqlDml.Cast(arg, SqlType.Int64);
219+
return (CastToDecimal(DateDiffDay(date1, date2), 18, 0) * NanosecondsPerDay)
220+
+
221+
(CastToDecimal(DateDiffMicrosecond(DateAddDay(date2, DateDiffDay(date1, date2)), date1), 18, 0) * NanosecondsPerMicrosecond);
232222
}
233223

234-
private static SqlCast CastToDecimal(SqlExpression arg, short precision, short scale)
224+
protected virtual SqlExpression DateTimeAddInterval(SqlExpression date, SqlExpression interval)
235225
{
236-
return SqlDml.Cast(arg, SqlType.Decimal, precision, scale);
226+
return DateAddMicrosecond(
227+
DateAddDay(date, ((interval - (interval % NanosecondsPerDay)) + ((interval % NanosecondsPerDay) > (NanosecondsPerDay / 2) ? 0 : 1)) / NanosecondsPerDay),
228+
(interval / NanosecondsPerMillisecond * NanosecondsPerMicrosecond) % (MillisecondsPerDay * NanosecondsPerMicrosecond));
237229
}
238230

239-
private static SqlUserFunctionCall DateDiffDay(SqlExpression date1, SqlExpression date2)
240-
{
241-
return SqlDml.FunctionCall("DATEDIFF", date1, date2);
242-
}
231+
#region Static helpers
243232

244-
private static SqlUserFunctionCall DateDiffMicrosecond(SqlExpression date1, SqlExpression date2)
245-
{
246-
return SqlDml.FunctionCall("TIMESTAMPDIFF", SqlDml.Native("MICROSECOND"), date1, date2);
247-
}
233+
private static SqlCast CastToLong(SqlExpression arg) => SqlDml.Cast(arg, SqlType.Int64);
248234

249-
private static SqlUserFunctionCall DateAddYear(SqlExpression date, SqlExpression years)
250-
{
251-
return SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("YEAR"), years, date);
252-
}
235+
private static SqlCast CastToDecimal(SqlExpression arg, short precision, short scale) =>
236+
SqlDml.Cast(arg, SqlType.Decimal, precision, scale);
253237

254-
private static SqlUserFunctionCall DateAddMonth(SqlExpression date, SqlExpression months)
255-
{
256-
return SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("MONTH"), months, date);
257-
}
238+
private static SqlUserFunctionCall DateDiffDay(SqlExpression date1, SqlExpression date2) =>
239+
SqlDml.FunctionCall("DATEDIFF", date1, date2);
258240

259-
private static SqlUserFunctionCall DateAddDay(SqlExpression date, SqlExpression days)
260-
{
261-
return SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("DAY"), days, date);
262-
}
241+
private static SqlUserFunctionCall DateDiffMicrosecond(SqlExpression date1, SqlExpression date2) =>
242+
SqlDml.FunctionCall("TIMESTAMPDIFF", SqlDml.Native("MICROSECOND"), date1, date2);
263243

264-
private static SqlUserFunctionCall DateAddMicrosecond(SqlExpression date, SqlExpression microseconds)
265-
{
266-
return SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("MICROSECOND"), microseconds, date);
267-
}
244+
private static SqlUserFunctionCall DateAddYear(SqlExpression date, SqlExpression years) =>
245+
SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("YEAR"), years, date);
268246

247+
private static SqlUserFunctionCall DateAddMonth(SqlExpression date, SqlExpression months) =>
248+
SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("MONTH"), months, date);
269249

270-
protected static SqlUserFunctionCall DateTimeToStringIso(SqlExpression dateTime)
271-
{
272-
return SqlDml.FunctionCall("DATE_FORMAT", dateTime, "%Y-%m-%dT%T");
273-
}
274-
275-
protected static SqlUserFunctionCall BitNot(SqlExpression operand)
276-
{
277-
return SqlDml.FunctionCall(
250+
private static SqlUserFunctionCall DateAddDay(SqlExpression date, SqlExpression days) =>
251+
SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("DAY"), days, date);
252+
253+
private static SqlUserFunctionCall DateAddMicrosecond(SqlExpression date, SqlExpression microseconds) =>
254+
SqlDml.FunctionCall("TIMESTAMPADD", SqlDml.Native("MICROSECOND"), microseconds, date);
255+
256+
protected static SqlUserFunctionCall DateTimeToStringIso(SqlExpression dateTime) =>
257+
SqlDml.FunctionCall("DATE_FORMAT", dateTime, "%Y-%m-%dT%T");
258+
259+
protected static SqlUserFunctionCall BitNot(SqlExpression operand) =>
260+
SqlDml.FunctionCall(
278261
"CAST",
279262
SqlDml.RawConcat(
280263
SqlDml.Native("~"),
281264
SqlDml.RawConcat(
282265
operand,
283266
SqlDml.Native("AS SIGNED"))));
284-
}
267+
285268

286269
#endregion
287270

0 commit comments

Comments
 (0)