Skip to content

Adds support for MySQL v5.7 and v8.0 #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/DriverFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2011-2021 Xtensive LLC.
// Copyright (C) 2011-2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Malisa Ncube
Expand Down Expand Up @@ -122,7 +122,10 @@ private static SqlDriver CreateDriverInstance(string connectionString, Version v
5 when version.Minor == 1 => new v5_1.Driver(coreServerInfo),
5 when version.Minor == 5 => new v5_5.Driver(coreServerInfo),
5 when version.Minor == 6 => new v5_6.Driver(coreServerInfo),
_ => new v5_6.Driver(coreServerInfo)
5 when version.Minor == 7 => new v5_7.Driver(coreServerInfo),
6 or 7 => throw new NotSupportedException(string.Format(Strings.ExVersionXOfMySQLIsNotSupported, version)),
8 => new v8_0.Driver(coreServerInfo),
_ => new v8_0.Driver(coreServerInfo)
};
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,7 @@
<data name="ExUserNameRequired" xml:space="preserve">
<value>MySQL Username is required.</value>
</data>
<data name="ExVersionXOfMySQLIsNotSupported" xml:space="preserve">
<value>Version {0} of MySQL is not supported.</value>
</data>
</root>
71 changes: 21 additions & 50 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Extractor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2011-2020 Xtensive LLC.
// Copyright (C) 2011-2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Malisa Ncube
Expand Down Expand Up @@ -400,7 +400,7 @@ private static void ReadTableData(DbDataReader reader, Catalog catalog)
var schemaName = reader.GetString(0);
var schema = catalog.Schemas[schemaName];
var tableName = reader.GetString(1);
schema.CreateTable(tableName);
_ = schema.CreateTable(tableName);
}

// ---- ReadTableColumnData
Expand Down Expand Up @@ -451,8 +451,8 @@ private void ReadTableColumnData(DbDataReader reader, ref ColumnReaderState<Tabl
}

// AutoIncrement
if (ReadAutoIncrement(reader, 13)) {
column.SequenceDescriptor = new SequenceDescriptor(column, ReadInt(reader, 14), 1);
if (ReadIfAutoIncrement(reader, 13)) {
column.SequenceDescriptor = new SequenceDescriptor(column, ReadAutoIncrementValue(reader, 14), 1);
}

//Column number.
Expand All @@ -468,12 +468,9 @@ private static void ReadViewData(DbDataReader reader, Catalog catalog)
var schema = catalog.Schemas[reader.GetString(0)];
var view = reader.GetString(1);
var definition = ReadStringOrNull(reader, 2);
if (string.IsNullOrEmpty(definition)) {
schema.CreateView(view);
}
else {
schema.CreateView(view, SqlDml.Native(definition));
}
_ = string.IsNullOrEmpty(definition)
? schema.CreateView(view)
: schema.CreateView(view, SqlDml.Native(definition));
}

//---- ReadViewColumnData
Expand All @@ -490,7 +487,7 @@ private static void ReadViewColumnData(DbDataReader reader, ref ColumnReaderStat
state.Owner = schema.Views[reader.GetString(1)];
}

state.Owner.CreateColumn(reader.GetString(2));
_ = state.Owner.CreateColumn(reader.GetString(2));
state.LastColumnIndex = columnIndex;
}

Expand All @@ -512,7 +509,7 @@ private static void ReadIndexColumnData(DbDataReader reader, ref IndexColumnRead
var schema = state.Catalog.Schemas[reader.GetString(0)];
state.Table = schema.Tables[reader.GetString(1)];
if (IsFullTextIndex(reader, 4)) {
state.Table.CreateFullTextIndex(reader.GetString(2));
_ = state.Table.CreateFullTextIndex(reader.GetString(2));
}
else {
state.Index = state.Table.CreateIndex(reader.GetString(2));
Expand All @@ -521,7 +518,7 @@ private static void ReadIndexColumnData(DbDataReader reader, ref IndexColumnRead
}

var column = state.Table.TableColumns[reader.GetString(6)];
state.Index.CreateIndexColumn(column);
_ = state.Index.CreateIndexColumn(column);

state.LastColumnIndex = columnIndex;
}
Expand Down Expand Up @@ -710,47 +707,21 @@ private static void CreateIndexBasedConstraint(ref IndexBasedConstraintReaderSta
private static bool ReadBool(IDataRecord row, int index)
{
var value = row.GetString(index);
switch (value) {
case "Y":
case "YES":
case "ENABLED":
case "UNIQUE":
return true;
case "N":
case "NO":
case "DISABLED":
case "NONUNIQUE":
return false;
default:
throw new ArgumentOutOfRangeException(string.Format(Strings.ExInvalidBooleanStringX, value));
}
return value switch {
"Y" or "YES" or "ENABLED" or "UNIQUE" => true,
"N" or "NO" or "DISABLED" or "NONUNIQUE" => false,
_ => throw new ArgumentOutOfRangeException(string.Format(Strings.ExInvalidBooleanStringX, value)),
};
}

private static bool IsFullTextIndex(IDataRecord row, int index)
{
var value = ReadStringOrNull(row, index);
if (!string.IsNullOrEmpty(value)) {
value = value.ToUpperInvariant();
return value == "FULLTEXT";
}
return false;
}
private static bool IsFullTextIndex(IDataRecord row, int index) =>
ReadStringOrNull(row, index).Equals("FULLTEXT", StringComparison.OrdinalIgnoreCase);

private static bool ReadAutoIncrement(IDataRecord row, int index)
{
var value = ReadStringOrNull(row, index);
if (!string.IsNullOrEmpty(value)) {
value = value.ToUpperInvariant();
return value == "AUTO_INCREMENT";
}
return false;
}
private static bool ReadIfAutoIncrement(IDataRecord row, int index) =>
ReadStringOrNull(row, index).Equals("AUTO_INCREMENT", StringComparison.OrdinalIgnoreCase);

private static long ReadLong(IDataRecord row, int index)
{
var value = row.GetDecimal(index);
return value > long.MaxValue ? long.MaxValue : (long) value;
}
private static int ReadAutoIncrementValue(IDataRecord row, int index) =>
row.IsDBNull(index) ? 1 : ReadInt(row, index);

private static int ReadInt(IDataRecord row, int index)
{
Expand Down
55 changes: 55 additions & 0 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_7/Compiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2022.02.03

using Xtensive.Sql.Compiler;
using Xtensive.Sql.Dml;

namespace Xtensive.Sql.Drivers.MySql.v5_7
{
internal class Compiler : v5_6.Compiler
{
/// <inheritdoc/>
public override void Visit(SqlQueryExpression node)
{
using (context.EnterScope(node)) {
var wrapLeft = node.Left is SqlSelect sL
&& (sL.OrderBy.Count > 0 || sL.HasLimit || sL.Lock != SqlLockType.Empty);
var wrapRight = node.Left is SqlSelect sR
&& (sR.OrderBy.Count > 0 || sR.HasLimit || sR.Lock != SqlLockType.Empty);

AppendTranslatedEntry(node);
if (wrapLeft) {
_ = context.Output.Append("(");
node.Left.AcceptVisitor(this);
_ = context.Output.Append(")");
}
else {
node.Left.AcceptVisitor(this);
}

AppendTranslated(node.NodeType);
AppendTranslated(node, QueryExpressionSection.All);

if (wrapRight) {
_ = context.Output.Append("(");
node.Right.AcceptVisitor(this);
_ = context.Output.Append(")");
}
else {
node.Right.AcceptVisitor(this);
}
AppendTranslatedExit(node);
}
}

// Constructors

public Compiler(SqlDriver driver)
: base(driver)
{
}
}
}
46 changes: 46 additions & 0 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_7/Driver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2022.02.03

using Xtensive.Sql.Compiler;
using Xtensive.Sql.Info;

namespace Xtensive.Sql.Drivers.MySql.v5_7
{
internal class Driver : MySql.Driver
{
protected override Sql.TypeMapper CreateTypeMapper()
{
return new TypeMapper(this);
}

protected override SqlCompiler CreateCompiler()
{
return new Compiler(this);
}

protected override SqlTranslator CreateTranslator()
{
return new Translator(this);
}

protected override Model.Extractor CreateExtractor()
{
return new Extractor(this);
}

protected override Info.ServerInfoProvider CreateServerInfoProvider()
{
return new ServerInfoProvider(this);
}

// Constructors

public Driver(CoreServerInfo coreServerInfo)
: base(coreServerInfo)
{
}
}
}
18 changes: 18 additions & 0 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_7/Extractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2022.02.03

namespace Xtensive.Sql.Drivers.MySql.v5_7
{
internal class Extractor : v5_6.Extractor
{
// Constructors

public Extractor(SqlDriver driver)
: base(driver)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2022.02.03

namespace Xtensive.Sql.Drivers.MySql.v5_7
{
internal class ServerInfoProvider : v5_6.ServerInfoProvider
{
// Constructors

public ServerInfoProvider(SqlDriver driver)
: base(driver)
{
}
}
}
38 changes: 38 additions & 0 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_7/Translator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2022.02.03

using System;
using Xtensive.Sql.Compiler;
using Xtensive.Sql.Dml;

namespace Xtensive.Sql.Drivers.MySql.v5_7
{
internal class Translator : v5_6.Translator
{
/// <inheritdoc/>
public override void Translate(SqlCompilerContext context, SqlCast node, NodeSection section)
{
if (node.Type.Type == SqlType.DateTime) {
var output = context.Output;
_ = section switch {
NodeSection.Entry => output.AppendOpeningPunctuation("CAST("),
NodeSection.Exit => output.Append("AS ")
.Append(Translate(node.Type))
.AppendClosingPunctuation("(6))"),
_ => throw new ArgumentOutOfRangeException(nameof(section)),
};
}
base.Translate(context, node, section);
}

// Constructors

public Translator(SqlDriver driver)
: base(driver)
{
}
}
}
18 changes: 18 additions & 0 deletions Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_7/TypeMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alexey Kulakov
// Created: 2022.02.03

namespace Xtensive.Sql.Drivers.MySql.v5_7
{
internal class TypeMapper : v5_6.TypeMapper
{
// Constructors

public TypeMapper(SqlDriver driver)
: base(driver)
{
}
}
}
Loading