MySQL version of SqlToCsharp.
A C# class generator from SQL CREATE TABLE Statements (MySQLs)
dotnet tool install --global MySQLToCsharp
Open MySQLToCsharp.sln and set following.
MySQLToCsharp project > Properties > Debug > Application arguments
dir -i ../../../../../samples/tables -o ../../../../../samples/MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp
Debug Run MySQLToCsharp.
Output Directory: ../../../../../samples/MySQLToCsharpSampleConsoleApp
[-] skipped: BinaryData.cs (no change)
[-] skipped: Character.cs (no change)
[-] skipped: CharacterSlot.cs (no change)
[-] skipped: Multi.cs (no change)
[-] skipped: Player.cs (no change)
[-] skipped: Room.cs (no change)
[-] skipped: String.cs (no change)
[-] skipped: Weapon.cs (no change)
change Converter to StandardDateTimeAsOffsetConverter
and Debug Run.
dir -i ../../../../../samples/tables -o ../../../../../samples/MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp -c StandardDateTimeAsOffsetConverter
This change CharacterSlot.cs as MySQL DATETIME
will convert to C# DateTimeOffset
.
Output Directory: ../../../../../samples/MySQLToCsharpSampleConsoleApp
[-] skipped: BinaryData.cs (no change)
[-] skipped: Character.cs (no change)
[o] generate: CharacterSlot.cs
[-] skipped: Multi.cs (no change)
[-] skipped: Player.cs (no change)
[-] skipped: Room.cs (no change)
[-] skipped: String.cs (no change)
[-] skipped: Weapon.cs (no change)
There are 3 options to generate C# code from MySQL Create Table query.
- query: input sql string and generate a class.
- file: read sql file and generate a class.
- dir: read directory path and generate class for each *.sql file.
$ MySQLToCsharp --help
Usage: MySQLToCsharp <Command>
Commands:
query Convert DDL sql query and generate C# class.
file Convert DDL sql file and generate C# class.
dir Convert DDL sql files in the folder and generate C# class.
help.
$ MySQLToCsharp query --help
Usage: MySQLToCsharp query [options...]
Convert DDL sql query and generate C# class.
Options:
-i, -input <String> input mysql ddl query to parse (Required)
-o, -output <String> output directory path of generated C# class file (Required)
-n, -namespace <String> namespace to write (Required)
-c, -converter <String> converter name to use (Default: StandardConverter)
-addbom <Boolean> (Default: False)
-dry <Boolean> (Default: False)
sample
dotnet mysql2csharp query -i "CREATE TABLE sercol1 (id INT, val INT);" -o bin/out -n MyNameSpace.Data
help.
$ MySQLToCsharp file --help
Usage: MySQLToCsharp file [options...]
Convert DDL sql file and generate C# class.
Options:
-i, -input <String> input file path to parse mysql ddl query (Required)
-o, -output <String> output directory path of generated C# class file (Required)
-n, -namespace <String> namespace to write (Required)
-c, -converter <String> converter name to use (Default: StandardConverter)
-addbom <Boolean> (Default: False)
-dry <Boolean> (Default: False)
sample
dotnet mysql2csharp file -i "./MySQLToCsharp.Tests/test_data/sql/create_table.sql" -o bin/out -n MyNameSpace.Data
help.
$ MySQLToCsharp dir --help
Usage: MySQLToCsharp dir [options...]
Convert DDL sql files in the folder and generate C# class.
Options:
-i, -input <String> input folder path to parse mysql ddl query (Required)
-o, -output <String> output directory path of generated C# class files (Required)
-n, -namespace <String> namespace to write (Required)
-c, -converter <String> converter name to use (Default: StandardConverter)
-addbom <Boolean> (Default: False)
-dry <Boolean> (Default: False)
sample
dotnet mysql2csharp dir -i "./MySQLToCsharp.Tests/test_data/sql/" -o bin/out -n MyNameSpace.Data
- StandardConverter
- StandardBitAsBoolConverter
- StandardDateTimeAsOffsetConverter
$ dotnet mysql2csharp query --help
$ dotnet mysql2csharp dir --help
$ dotnet mysql2csharp file --help
Referencing MySQL ANTLR4 Grammer from antlr/grammars-v4.
Follow step to update lexer and parser.
- Update MySqlLexer.g4 and MySqlParser.g4 to the latest.
- Run script to generate C# class files.
- it calls
docker-compose up
and generate lexer, parser, listener and visitor class.
- Run Build and Test and confirm what changed and actual effect.
# windows
gen.bat
# macos/linux
gen.sh
ANTLR4 Getting started
- antlr4/csharp-target.md at master · antlr/antlr4
- antlr4/runtime/CSharp at master · antlr/antlr4
- antlr4cs/Readme.md at master · sharwell/antlr4cs
- antlr-mega-tutorial/README.md at master · unosviluppatore/antlr-mega-tutorial
- antlr-mega-tutorial/antlr-csharp/antlr-csharp at master · unosviluppatore/antlr-mega-tutorial
ANTLR4 samples
- pyparsingをAntlr4で置き換えて性能を5倍にした - Qiita
- TreePatternTest in C#
- Antlr4 - Visitor vs Listener Pattern - Saumitra's blog
- java - Parsing mysql using ANTLR4 simple example - Stack Overflow
MSSQL Parser reference