MySQL version of SqlToCsharp.
A C# class generator from SQL CREATE TABLE Statements (MySQLs)
Use binary from Release or dotnet tool to install.
$ dotnet tool install --global MySQLToCsharpThere 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.
# Binary
$ 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.
# dotnet tool
$ dotnet mysql2csharp --help
Usage: dotnet mysql2csharp [command]
MySQLToCsharp
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.
Options:
-h, --help Show help message
--version Show versionYou can pass a query to generate C# file.
$ 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.DataYou can pass a file to generate C# file.
$ 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.DataYou can pass a directory path to generate C# files for each *.sql file.
$ 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.DataYou can choose a converter to convert MySQL data type to C# data type.
- StandardConverter: MySQL data types will convert to C# data types.
- StandardBitAsBoolConverter: MySQL
BITwill convert to C#bool. - StandardDateTimeAsOffsetConverter: MySQL
DATETIMEwill convert to C#DateTimeOffset.
There are sample projects in the samples directory.
Run following command in repository root directory.
dotnet run --project ./src/MySQLToCsharp/MySQLToCsharp.csproj -- dir -i ./samples/tables -o ./samples/MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp
dir executed. 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 change CharacterSlot.cs as MySQL DATETIME will convert to C# DateTimeOffset.
dotnet run --project ./src/MySQLToCsharp/MySQLToCsharp.csproj -- dir -i ./samples/tables -o ./samples/MySQLToCsharpSampleConsoleApp -n MySQLToCsharpSampleConsoleApp -c StandardDateTimeAsOffsetConverter
dir executed. 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)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 upand 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 references.
- 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 and articles.
- 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.