Skip to content

Commit efec37e

Browse files
committed
Generate update, insert and delete queries as functions
1 parent 26dfa9d commit efec37e

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

MySQL-To-CSharp/Program.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,46 @@ private static void DbToClasses(string dbName, Dictionary<string, List<Column>>
5656
foreach (var column in table.Value)
5757
sb.AppendLine(column.ToString());
5858

59-
// constructor
60-
sb.AppendLine($"{Environment.NewLine}public {table.Key}(MySqlDataReader reader)");
61-
sb.AppendLine("{");
62-
foreach (var column in table.Value)
59+
if (generateConstructorAndOutput)
6360
{
64-
// check which type and use correct get method instead of casting
65-
if (column.Type != typeof(string))
66-
sb.AppendLine($"{column.Name} = Convert.To{column.Type.Name}(reader[\"{column.Name}\"].ToString());");
67-
else
68-
sb.AppendLine($"{column.Name} = reader[\"{column.Name}\"].ToString();");
61+
// constructor
62+
sb.AppendLine($"{Environment.NewLine}public {table.Key}(MySqlDataReader reader)");
63+
sb.AppendLine("{");
64+
foreach (var column in table.Value)
65+
{
66+
// check which type and use correct get method instead of casting
67+
if (column.Type != typeof(string))
68+
sb.AppendLine($"{column.Name} = Convert.To{column.Type.Name}(reader[\"{column.Name}\"].ToString());");
69+
else
70+
sb.AppendLine($"{column.Name} = reader[\"{column.Name}\"].ToString();");
71+
}
72+
sb.AppendLine($"}}{Environment.NewLine}");
73+
74+
// update query
75+
sb.AppendLine($"public string UpdateQuery()");
76+
sb.AppendLine("{");
77+
sb.Append($"return $\"UPDATE {table.Key} SET");
78+
foreach (var column in table.Value)
79+
sb.Append($" {column.Name} = {{{column.Name}}},");
80+
sb.Remove(sb.ToString().LastIndexOf(','), 1);
81+
sb.AppendLine($" WHERE {table.Value[0].Name} = {{{table.Value[0].Name}}};\"");
82+
sb.AppendLine($"}}{Environment.NewLine}");
83+
84+
// insert query
85+
sb.AppendLine($"public string InsertQuery()");
86+
sb.AppendLine("{");
87+
sb.Append($"return $\"INSERT INTO {table.Key} VALUES (");
88+
foreach (var column in table.Value)
89+
sb.Append($" {{{column.Name}}},");
90+
sb.Remove(sb.ToString().LastIndexOf(','), 1);
91+
sb.AppendLine($");\";{Environment.NewLine}}}{Environment.NewLine}");
92+
93+
// delete query
94+
sb.AppendLine($"public string DeleteQuery()");
95+
sb.AppendLine("{");
96+
sb.AppendLine($"return $\"DELETE FROM {table.Key} WHERE {table.Value[0].Name} = {{{table.Value[0].Name}}};\";");
97+
sb.AppendLine("}");
6998
}
70-
71-
sb.AppendLine("}");
7299

73100
// class closing
74101
sb.AppendLine("}");

0 commit comments

Comments
 (0)