Skip to content

Repository files navigation

dotnet-buildNuGet

MySQLToCsharp

MySQL version of SqlToCsharp.

A C# class generator from SQL CREATE TABLE Statements (MySQLs)

Install

Use binary from Release or dotnet tool to install.

$ dotnet tool install --global MySQLToCsharp

How to run

There are 3 options to generate C# code from MySQL Create Table query.

  1. query: input sql string and generate a class.
  2. file: read sql file and generate a class.
  3. 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 version

query

You 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.Data

file

You 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.Data

dir

You 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.Data

Conveters

You 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 BIT will convert to C# bool.
  • StandardDateTimeAsOffsetConverter: MySQL DATETIME will convert to C# DateTimeOffset.

Samples

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)

Generate MySQL Lexer/Parser/Listener/Visitor from ANTLR4 grammer

Referencing MySQL ANTLR4 Grammer from antlr/grammars-v4.

Follow step to update lexer and parser.

  1. Update MySqlLexer.g4 and MySqlParser.g4 to the latest.
  2. Run script to generate C# class files.
  • it calls docker compose up and generate lexer, parser, listener and visitor class.
  1. Run Build and Test and confirm what changed and actual effect.
# windows
gen.bat
# macos/linux
gen.sh

ANTLR4 References

ANTLR4 Getting started references.

ANTLR4 samples and articles.

MSSQL Parser reference.

About

A C# class generator from SQL CREATE TABLE Statements (MySQL)

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages