Skip to content
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
2 changes: 2 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/BinaryData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class BinaryData
Expand Down
2 changes: 2 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/Character.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class Character
Expand Down
20 changes: 20 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/CharacterSlot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class CharacterSlot
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
public int SlotIndex { get; set; }
public int CharacterId { get; set; }
public int? WeaponId { get; set; }
public int? WeaponId2 { get; set; }
public int Exp { get; set; }
}
}
22 changes: 22 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/Multi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class Multi
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[Key]
[Column(Order = 1)]
public int UserId { get; set; }
public int MasterId { get; set; }
[Required]
[StringLength(100)]
public string Desc { get; set; }
}
}
22 changes: 22 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
/// <summary>
/// USER TABLE
/// </summary>
public partial class Player
{
/// <summary>
/// Player Id
/// </summary>
[Key]
[Column(Order = 0)]
public int Id { get; set; }
public int Exp { get; set; }
}
}
2 changes: 2 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/Room.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class Room
Expand Down
29 changes: 29 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/String.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class String
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string S { get; set; }
[StringLength(50)]
public string NS { get; set; }
[Required]
public string T { get; set; }
public string NT { get; set; }
[Required]
public string MT { get; set; }
public string NMT { get; set; }
[Required]
public string LT { get; set; }
public string NLT { get; set; }
}
}
2 changes: 2 additions & 0 deletions samples/MySQLToCsharpSampleConsoleApp/Weapon.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// auto generated by SqlToCsharp

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MySQLToCsharpSampleConsoleApp
{
public partial class Weapon
Expand Down
2 changes: 1 addition & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ generate C# code from mysql CREATE TABLE query.

```shell
dotnet publish ..
dotnet ../src/MySQLToCSharp/bin/Debug/netcoreapp2.2/publish/MySQLToCsharp.dll --dir -i ./tables -o ./MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp
dotnet ../src/MySQLToCSharp/bin/Debug/netcoreapp2.1/publish/MySQLToCsharp.dll --dir -i ./tables -o ./MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp
```
6 changes: 6 additions & 0 deletions samples/tables/Player.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `Player` (
`Id` INT NOT NULL COMMENT 'Player Id',
`Exp` INT NOT NULL,
PRIMARY KEY (`Id`)
)
COMMENT='USER TABLE';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace MySQLToCsharp.Tests
{
public class CreateTableStatementParseUnitTest
public class CreateTableParseColumnUnitTest
{
[Theory]
[MemberData(nameof(GenerateParseTestData))]
Expand All @@ -25,6 +25,33 @@ public void ParsableTest(TestItem data)
listener.TableDefinition.Should().NotBeNull();
}
[Theory]
[MemberData(nameof(SqlColumnCommentTestData))]
public void SqlColumnCommentTest(TestItem data)
{
var listener = new CreateTableStatementDetectListener();
IParser parser = new Parser();
parser.Parse(data.Statement, listener);
var definition = listener.TableDefinition;
listener.IsTargetStatement.Should().BeTrue();
listener.IsParseBegin.Should().BeTrue();
listener.IsParseCompleted.Should().BeTrue();
listener.TableDefinition.Should().NotBeNull();

for (var i = 0; i < listener.TableDefinition.Columns.Length; i++)
{
definition.Columns[i].Name.Should().Be(data.Expected[i].Name);
definition.Columns[i].Order.Should().Be(data.Expected[i].Order);
definition.Columns[i].AutoIncrement.Should().Be(data.Expected[i].AutoIncrement);
definition.Columns[i].Data.IsNullable.Should().Be(data.Expected[i].Data.IsNullable);
definition.Columns[i].Data.IsUnsigned.Should().Be(data.Expected[i].Data.IsUnsigned);
definition.Columns[i].Data.Length.Should().Be(data.Expected[i].Data.Length);
definition.Columns[i].Data.DataType.Should().Be(data.Expected[i].Data.DataType);
definition.Columns[i].HasDefault.Should().Be(data.Expected[i].HasDefault);
definition.Columns[i].Comment.Should().Be(data.Expected[i].Comment);
definition.Columns[i].DefaultValue.Should().Be(data.Expected[i].DefaultValue);
}
}
[Theory]
[MemberData(nameof(GenerateMultiplePkData))]
public void MultiplePrimaryKeyColumnTests(TestItem data)
{
Expand Down Expand Up @@ -208,6 +235,50 @@ public static IEnumerable<object[]> GenerateParseTestData()
}
}

public static IEnumerable<object[]> SqlColumnCommentTestData()
{
var statements = TestHelper.LoadSql("test_data/create_table_column_comment.sql");
foreach (var statement in statements)
{
yield return new object[]
{
new TestItem
{
Statement = statement,
Expected = new [] {
new MySqlColumnDefinition
{
AutoIncrement = true,
Data = new MySqlColumnDataDefinition
{
DataType = "BIGINT",
IsNullable = false,
IsUnsigned = false,
Length = 20,
},
Name = "Id",
Order = 0,
},
new MySqlColumnDefinition
{
AutoIncrement = false,
Data = new MySqlColumnDataDefinition
{
DataType = "INT",
IsNullable = false,
IsUnsigned = false,
Length = 11,
},
Name = "SampleId",
Order = 1,
Comment = "this is comment",
},
}
},
};
}
}

public static IEnumerable<object[]> GenerateTypeConverterTestData()
{
var statements = TestHelper.LoadSql("test_data/Strings.sql");
Expand Down
94 changes: 94 additions & 0 deletions src/MySQLToCsharp.Tests/CreateTableParseTableUnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using FluentAssertions;
using MySQLToCsharp.Listeners;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;

namespace MySQLToCsharp.Tests
{
public class CreateTableParseTableUnitTest
{
[Theory]
[MemberData(nameof(GenerateParseTestData))]
public void ParsableTest(TestItem data)
{
var listener = new CreateTableStatementDetectListener();
IParser parser = new Parser();
parser.Parse(data.Statement, listener);
var definition = listener.TableDefinition;
listener.IsTargetStatement.Should().BeTrue();
listener.IsParseBegin.Should().BeTrue();
listener.IsParseCompleted.Should().BeTrue();
listener.TableDefinition.Should().NotBeNull();

definition.Collation.Should().Be(data.Expected.Collation);
definition.Engine.Should().Be(data.Expected.Engine);
}
[Theory]
[MemberData(nameof(SqlTableCommentTestData))]
public void SqlTableCommentTest(TestItem data)
{
var listener = new CreateTableStatementDetectListener();
IParser parser = new Parser();
parser.Parse(data.Statement, listener);
var definition = listener.TableDefinition;
listener.IsTargetStatement.Should().BeTrue();
listener.IsParseBegin.Should().BeTrue();
listener.IsParseCompleted.Should().BeTrue();
listener.TableDefinition.Should().NotBeNull();

definition.Collation.Should().Be(data.Expected.Collation);
definition.Engine.Should().Be(data.Expected.Engine);
definition.Comment.Should().Be(data.Expected.Comment);
}

public static IEnumerable<object[]> GenerateParseTestData()
{
var statements = TestHelper.LoadSql("test_data/create_table.sql");
foreach (var statement in statements)
{
yield return new object[]
{
new TestItem
{
Statement = statement,
Expected = new MySqlTableDefinition
{
Collation = "utf8mb4_general_ci",
Engine = "InnoDB",
}
},
};
}
}

public static IEnumerable<object[]> SqlTableCommentTestData()
{
var statements = TestHelper.LoadSql("test_data/create_table_comment.sql");
foreach (var statement in statements)
{
yield return new object[]
{
new TestItem
{
Statement = statement,
Expected =new MySqlTableDefinition
{
Collation = "utf8mb4_general_ci",
Engine = "InnoDB",
Comment = "this is comment",
}
}
};
}
}
public class TestItem
{
public string Statement { get; set; }
public MySqlTableDefinition Expected { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE `Samples` (
`Id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`SampleId` INT(11) NOT NULL COMMENT 'this is comment',
PRIMARY KEY (`Id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
9 changes: 9 additions & 0 deletions src/MySQLToCsharp.Tests/test_data/create_table_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE `Samples` (
`Id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`SampleId` INT(11) NOT NULL,
PRIMARY KEY (`Id`)
)
COMMENT='this is comment'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
2 changes: 2 additions & 0 deletions src/MySQLToCsharp/Definitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class MySqlTableDefinition
public MySqlKeyDefinition PrimaryKey { get; set; }
public ISet<MySqlKeyDefinition> UniqueKeys { get; private set; }
public ISet<MySqlKeyDefinition> IndexKeys { get; private set; }
public string Comment { get; set; }
public string Collation { get; set; }
public string Engine { get; set; }

Expand Down Expand Up @@ -77,6 +78,7 @@ public class MySqlColumnDefinition
public bool AutoIncrement { get; set; }
public bool HasDefault { get; set; }
public string DefaultValue { get; set; }
public string Comment { get; set; }
public GeneratedColumnDefinition GeneratedColumn { get; set; }
public ReferenceColumnDefinition ReferenceColumn { get; set; }

Expand Down
Loading