Skip to content

Commit 1c36550

Browse files
committed
Initial commit
1 parent 8a41ba2 commit 1c36550

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

MySQL-To-CSharp/App.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
<system.data>
7+
<DbProviderFactories>
8+
<remove invariant="MySql.Data.MySqlClient" />
9+
<add description=".Net Framework Data Provider for MySQL" invariant="MySql.Data.MySqlClient" name="MySQL Data Provider" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
10+
</DbProviderFactories>
11+
</system.data></configuration>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{8673C77C-EACF-4140-8357-1A2BEA96ED8E}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>MySQL_To_CSharp</RootNamespace>
10+
<AssemblyName>MySQL-To-CSharp</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="FluentCommandLineParser, Version=1.4.3.0, Culture=neutral, processorArchitecture=MSIL">
36+
<HintPath>..\packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll</HintPath>
37+
</Reference>
38+
<Reference Include="MySql.Data, Version=6.10.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
39+
<HintPath>..\packages\MySql.Data.6.10.4\lib\net452\MySql.Data.dll</HintPath>
40+
</Reference>
41+
<Reference Include="System" />
42+
<Reference Include="System.Core" />
43+
<Reference Include="System.Xml.Linq" />
44+
<Reference Include="System.Data.DataSetExtensions" />
45+
<Reference Include="Microsoft.CSharp" />
46+
<Reference Include="System.Data" />
47+
<Reference Include="System.Net.Http" />
48+
<Reference Include="System.Xml" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Compile Include="Program.cs" />
52+
<Compile Include="Properties\AssemblyInfo.cs" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<None Include="App.config" />
56+
<None Include="packages.config" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Content Include="docs\MySqlCommand.xml" />
60+
<Content Include="docs\MySqlCommandBuilder.xml" />
61+
<Content Include="docs\MySqlConnection.xml" />
62+
<Content Include="docs\MySqlConnectionStringBuilder.xml" />
63+
<Content Include="docs\MySqlDataAdapter.xml" />
64+
<Content Include="docs\MySqlDataReader.xml" />
65+
<Content Include="docs\MySqlException.xml" />
66+
<Content Include="docs\MySqlHelper.xml" />
67+
<Content Include="docs\MySqlParameter.xml" />
68+
<Content Include="docs\MySqlParameterCollection.xml" />
69+
<Content Include="docs\MySqlTransaction.xml" />
70+
</ItemGroup>
71+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
72+
</Project>

MySQL-To-CSharp/Program.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Linq.Expressions;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Fclp;
10+
using MySql.Data.MySqlClient;
11+
12+
namespace MySQL_To_CSharp
13+
{
14+
15+
public class ApplicationArguments
16+
{
17+
public string IP { get; set; }
18+
public int Port { get; set; }
19+
public string User { get; set; }
20+
public string Password { get; set; }
21+
public string Database { get; set; }
22+
public string Table { get; set; }
23+
}
24+
25+
public class Column
26+
{
27+
public string Name { get; set; }
28+
public Type Type { get; set; }
29+
30+
public Column(MySqlDataReader reader)
31+
{
32+
this.Name = reader.GetString(1);
33+
}
34+
35+
public override string ToString()
36+
{
37+
return $"public {this.Type.Name} {this.Name} {{ get; set; }}";
38+
}
39+
}
40+
41+
class Program
42+
{
43+
private static void DbToClasses(string dbName, Dictionary<string, List<Column>> db)
44+
{
45+
if (!Directory.Exists(dbName))
46+
Directory.CreateDirectory(dbName);
47+
48+
var sb = new StringBuilder();
49+
foreach (var table in db)
50+
{
51+
sb.AppendLine($"public class {table.Key}");
52+
sb.AppendLine("{");
53+
foreach (var column in table.Value)
54+
{
55+
sb.AppendLine(column.ToString());
56+
}
57+
sb.AppendLine("}");
58+
59+
var sw = new StreamWriter($"{dbName}/{table.Key}.cs", false);
60+
sw.Write(sb.ToString());
61+
sw.Close();
62+
sb.Clear();
63+
}
64+
}
65+
66+
static void Main(string[] args)
67+
{
68+
var parser = new FluentCommandLineParser<ApplicationArguments>();
69+
parser.Setup(arg => arg.IP).As('i', "ip").SetDefault("127.0.0.1").WithDescription("(optional) IP address of the MySQL server, will use 127.0.0.1 if not specified");
70+
parser.Setup(arg => arg.Port).As('n', "port").SetDefault(3306).WithDescription("(optional) Port number of the MySQL server, will use 3306 if not specified");
71+
parser.Setup(arg => arg.User).As('u', "user").SetDefault("root").WithDescription("(optional) Username, will use root if not specified");
72+
parser.Setup(arg => arg.Password).As('p', "password").SetDefault(String.Empty).WithDescription("(optional) Password, will use empty password if not specified");
73+
parser.Setup(arg => arg.Database).As('d', "database").Required().WithDescription("Database name");
74+
parser.Setup(arg => arg.Table).As('t', "table").SetDefault(String.Empty).WithDescription("(optional) Table name, will generate entire database if not specified");
75+
parser.SetupHelp("?", "help").Callback(text => Console.WriteLine(text));
76+
77+
#if DEBUG
78+
args = new [] { "-p", "123", "-d", "az_world"};
79+
#endif
80+
81+
var result = parser.Parse(args);
82+
if (!result.HasErrors)
83+
{
84+
var conf = parser.Object as ApplicationArguments;
85+
if (conf.Database is null)
86+
{
87+
Console.WriteLine("You didn't specify a database");
88+
return;
89+
}
90+
91+
var confString =
92+
$"Server={conf.IP};Port={conf.Port};Uid={conf.User};Pwd={conf.Password};Database={conf.Database}";
93+
Console.WriteLine(confString);
94+
95+
var database = new Dictionary<string, List<Column>>();
96+
97+
using (var con = new MySqlConnection(confString))
98+
{
99+
con.Open();
100+
101+
using (var cmd = con.CreateCommand())
102+
{
103+
cmd.CommandText =
104+
$"SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{conf.Database}'";
105+
if (!conf.Table.Equals(string.Empty))
106+
cmd.CommandText += $" AND TABLE_NAME = '{conf.Table}'";
107+
108+
var reader = cmd.ExecuteReader();
109+
if (!reader.HasRows)
110+
return;
111+
112+
while (reader.Read())
113+
if (database.ContainsKey(reader.GetString(0)))
114+
database[reader.GetString(0)].Add(new Column(reader));
115+
else
116+
database.Add(reader.GetString(0), new List<Column>() { new Column(reader) });
117+
}
118+
119+
foreach (var table in database)
120+
{
121+
using (var cmd = con.CreateCommand())
122+
{
123+
// lul - is there a way to do this without this senseless statement?
124+
cmd.CommandText = $"SELECT * FROM {table.Key} LIMIT 0";
125+
var reader = cmd.ExecuteReader();
126+
var schema = reader.GetSchemaTable();
127+
foreach (var column in table.Value)
128+
column.Type = schema.Select($"ColumnName = '{column.Name}'")[0]["DataType"] as Type;
129+
}
130+
}
131+
132+
con.Close();
133+
}
134+
135+
DbToClasses(conf.Database, database);
136+
}
137+
Console.WriteLine("Successfully generated C# classes!");
138+
Console.ReadLine();
139+
}
140+
}
141+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// Allgemeine Informationen über eine Assembly werden über die folgenden
6+
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
7+
// die einer Assembly zugeordnet sind.
8+
[assembly: AssemblyTitle("MySQL-To-CSharp")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("MySQL-To-CSharp")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
18+
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
19+
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
20+
[assembly: ComVisible(false)]
21+
22+
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
23+
[assembly: Guid("8673c77c-eacf-4140-8357-1a2bea96ed8e")]
24+
25+
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
26+
//
27+
// Hauptversion
28+
// Nebenversion
29+
// Buildnummer
30+
// Revision
31+
//
32+
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
33+
// übernehmen, indem Sie "*" eingeben:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

MySQL-To-CSharp/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="FluentCommandLineParser" version="1.4.3" targetFramework="net461" />
4+
<package id="MySql.Data" version="6.10.4" targetFramework="net461" />
5+
</packages>

0 commit comments

Comments
 (0)