feat(all):添加是否生成动态表名的功能,添加是否生成验证类功能,添加选择数据库功能

main
forget_the_bright 8 months ago
parent 432058ed12
commit 6cf5472a08

@ -46,6 +46,12 @@
this.label5 = new System.Windows.Forms.Label();
this.isGenOperationColumn = new System.Windows.Forms.CheckBox();
this.label6 = new System.Windows.Forms.Label();
this.txt_database = new System.Windows.Forms.ComboBox();
this.databaseLable = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.isGeneratorValidateClass = new System.Windows.Forms.CheckBox();
this.label8 = new System.Windows.Forms.Label();
this.isGeneratorDynamicTableName = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// modelGen
@ -61,7 +67,7 @@
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(357, 16);
this.label2.Location = new System.Drawing.Point(530, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 4;
@ -106,11 +112,12 @@
// txt_tablename
//
this.txt_tablename.FormattingEnabled = true;
this.txt_tablename.Location = new System.Drawing.Point(404, 12);
this.txt_tablename.Location = new System.Drawing.Point(577, 12);
this.txt_tablename.Name = "txt_tablename";
this.txt_tablename.Size = new System.Drawing.Size(218, 20);
this.txt_tablename.TabIndex = 72;
this.txt_tablename.Text = "请选择";
this.txt_tablename.SelectionChangeCommitted += new System.EventHandler(this.txt_tablename_SelectionChangeCommitted);
//
// textBox1
//
@ -214,11 +221,72 @@
this.label6.TabIndex = 83;
this.label6.Text = "是否生成操作列(生成2和3)";
//
// txt_database
//
this.txt_database.FormattingEnabled = true;
this.txt_database.Location = new System.Drawing.Point(188, 13);
this.txt_database.Name = "txt_database";
this.txt_database.Size = new System.Drawing.Size(218, 20);
this.txt_database.TabIndex = 85;
this.txt_database.Text = "请选择";
this.txt_database.SelectionChangeCommitted += new System.EventHandler(this.txt_database_SelectionChangeCommitted);
//
// databaseLable
//
this.databaseLable.AutoSize = true;
this.databaseLable.Location = new System.Drawing.Point(141, 17);
this.databaseLable.Name = "databaseLable";
this.databaseLable.Size = new System.Drawing.Size(41, 12);
this.databaseLable.TabIndex = 84;
this.databaseLable.Text = "表名:";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(54, 134);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(101, 12);
this.label7.TabIndex = 87;
this.label7.Text = "是否生成验证类:";
//
// isGeneratorValidateClass
//
this.isGeneratorValidateClass.AutoSize = true;
this.isGeneratorValidateClass.Location = new System.Drawing.Point(161, 134);
this.isGeneratorValidateClass.Name = "isGeneratorValidateClass";
this.isGeneratorValidateClass.Size = new System.Drawing.Size(15, 14);
this.isGeneratorValidateClass.TabIndex = 86;
this.isGeneratorValidateClass.UseVisualStyleBackColor = true;
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(54, 167);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(113, 12);
this.label8.TabIndex = 89;
this.label8.Text = "是否生成动态表名:";
//
// isGeneratorDynamicTableName
//
this.isGeneratorDynamicTableName.AutoSize = true;
this.isGeneratorDynamicTableName.Location = new System.Drawing.Point(173, 167);
this.isGeneratorDynamicTableName.Name = "isGeneratorDynamicTableName";
this.isGeneratorDynamicTableName.Size = new System.Drawing.Size(15, 14);
this.isGeneratorDynamicTableName.TabIndex = 88;
this.isGeneratorDynamicTableName.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1084, 778);
this.ClientSize = new System.Drawing.Size(1065, 778);
this.Controls.Add(this.label8);
this.Controls.Add(this.isGeneratorDynamicTableName);
this.Controls.Add(this.label7);
this.Controls.Add(this.isGeneratorValidateClass);
this.Controls.Add(this.txt_database);
this.Controls.Add(this.databaseLable);
this.Controls.Add(this.label6);
this.Controls.Add(this.isGenOperationColumn);
this.Controls.Add(this.label5);
@ -265,6 +333,12 @@
private System.Windows.Forms.Label label5;
private System.Windows.Forms.CheckBox isGenOperationColumn;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ComboBox txt_database;
private System.Windows.Forms.Label databaseLable;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.CheckBox isGeneratorValidateClass;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.CheckBox isGeneratorDynamicTableName;
}
}

@ -4,6 +4,7 @@ using System.IO;
using System.Windows.Forms;
using GenerateClass.Util;
using JinianNet.JNTemplate;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace GenerateClass
{
@ -14,9 +15,25 @@ namespace GenerateClass
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadControl();
}
private string tableName;
#region 加载控件
private void loadControl()
{
loadTableNameControl();
loadDataBaseNameControl();
}
/// <summary>
/// 加载表名控件
/// </summary>
private void loadTableNameControl()
{
this.txt_tablename.Items.AddRange(DbUtil.GetTableName().ToArray());
this.txt_tablename.TextUpdate += (childSender, childE) => {
this.txt_tablename.TextUpdate += (childSender, childE) =>
{
string str = this.txt_tablename.Text; //获取输入内容
List<string> sList = DbUtil.GetTableName(str); //存放数据库查询结果
//提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容)
@ -27,24 +44,76 @@ namespace GenerateClass
this.txt_tablename.Items.AddRange(sList.ToArray());
this.txt_tablename.Select(this.txt_tablename.Text.Length, 0);
};
}
/// <summary>
/// 加载数据库名控件
/// </summary>
private void loadDataBaseNameControl()
{
this.txt_database.Items.AddRange(DbUtil.GetDataBaseName().ToArray());
this.txt_database.TextUpdate += (childSender, childE) =>
{
string str = this.txt_database.Text; //获取输入内容
List<string> sList = DbUtil.GetDataBaseName(str); //存放数据库查询结果
//提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容)
this.txt_database.DroppedDown = true; //显示下拉列表,但是显示后鼠标指针就不见了
Cursor.Current = Cursors.Default; //将指针显示出来
//在表中已录入名字中寻找包含输入内容的项 有则添加到comboBox项中
this.txt_database.Items.Clear();
this.txt_database.Items.AddRange(sList.ToArray());
this.txt_database.Select(this.txt_database.Text.Length, 0);
this.txt_tablename.Items.Clear();
};
this.txt_database.Text = DbHelperMySQL.DbName;
}
#endregion
private void modelGen_Click(object sender, EventArgs e)
{
if (validateTableName())
{
return;
}
fillModelTemplate();
}
private void dgvGen1_Click(object sender, EventArgs e)
{
if (validateTableName())
{
return;
}
fillDgv1Template();
}
private void dgvGen2_Click(object sender, EventArgs e)
{
if (validateTableName())
{
return;
}
fillDgv2Template();
}
private void dgvGen3_Click(object sender, EventArgs e)
{
if (validateTableName())
{
return;
}
fillDgv3Template();
}
private bool validateTableName()
{
string tbname = txt_tablename.Text.Trim();//要生成的表名
if (string.IsNullOrEmpty(tbname))
{
MessageBox.Show("请选择表");
return true;
}
if (tableName != tbname)
{
MessageBox.Show("请选择正确的表");
return true;
}
return false;
}
private void fillModelTemplate()
{
string tbname = txt_tablename.Text.Trim();//要生成的表名
@ -59,6 +128,9 @@ namespace GenerateClass
template.Set("sql_table_name", tbname);
template.Set("table_name", DbUtil.GenerateClassName(tbname));
template.Set("filedInfos", filedInfos);
template.Set("dataBaseName", DbHelperMySQL.DbName);
template.Set("isGeneratorValidateClass", isGeneratorValidateClass.Checked);
template.Set("isGeneratorDynamicTableName", isGeneratorDynamicTableName.Checked);
template.SetStaticType("string", typeof(string));
var result = template.Render();
this.txt_ret.Text = result;
@ -82,6 +154,7 @@ namespace GenerateClass
template.Set("dgv_name", dgvName);
template.Set("filedInfos", filedInfos);
template.Set("timeFiledStr", DbUtil.joinList(timeFileds));
template.Set("dataBaseName", DbHelperMySQL.DbName);
template.SetStaticType("string", typeof(string));
var result = template.Render();
this.textBox1.Text = result;
@ -102,6 +175,7 @@ namespace GenerateClass
template.Set("dgv_name", dgvName);
template.Set("isGenOperationColumn", isGenOperationColumn.Checked);
template.Set("timeFiledStr", DbUtil.joinList(timeFileds));
template.Set("dataBaseName", DbHelperMySQL.DbName);
template.SetStaticType("string", typeof(string));
var result = template.Render();
this.textBox1.Text = result;
@ -124,10 +198,29 @@ namespace GenerateClass
template.Set("filedInfos", filedInfos);
template.Set("isGenOperationColumn", isGenOperationColumn.Checked);
template.Set("timeFiledStr", DbUtil.joinList(timeFileds));
template.Set("dataBaseName", DbHelperMySQL.DbName);
template.SetStaticType("string", typeof(string));
var result = template.Render();
this.textBox1.Text = result;
}
}
private void txt_tablename_SelectionChangeCommitted(object sender, EventArgs e)
{
// 获取选中的值
string selectedValue = txt_tablename.SelectedItem.ToString();
tableName = selectedValue;
}
private void txt_database_SelectionChangeCommitted(object sender, EventArgs e)
{
this.txt_tablename.Items.Clear();
// 获取选中的值
string selectedValue = txt_database.SelectedItem.ToString();
DbHelperMySQL.DbName = selectedValue;
this.txt_tablename.SelectedIndex = -1;
this.txt_tablename.Text = "";
this.txt_tablename.Items.AddRange(DbUtil.GetTableName().ToArray());
}
}
}

@ -27,7 +27,16 @@ $end
public ${filedInfo["type_name"]} ${filedInfo["field_name"]} { get; set; }
$end
$if(isGeneratorDynamicTableName)
static ${table_name}(){
${table_name}.DynamicTableName += () =>
{
return $"${dataBaseName}.{_tableName}";
};
}
$end
}
$if(isGeneratorValidateClass)
/// <summary>
/// 验证类 采用FluentValidation
/// 项目地址 https://github.com/FluentValidation/FluentValidation
@ -44,4 +53,5 @@ $end
$end
}
}
$end
}

@ -65,7 +65,8 @@ namespace GenerateClass.Util
public static List<string> getTimeFields(List<Dictionary<string, string>> filedInfos)
{
var list = new List<string>();
filedInfos.ForEach(info => {
filedInfos.ForEach(info =>
{
if (info["type_name"] == "DateTime") list.Add(info["sql_field_name"]);
});
return list;
@ -100,7 +101,14 @@ namespace GenerateClass.Util
var dt = DbHelperMySQL.Query(String.Format(sql, tabname, DbHelperMySQL.DbName));
return dt;
}
public static string GetTableComment(string tableName)
{
string sql = $"SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME ='{tableName}';";
var dt = DbHelperMySQL.Query(sql);
if (dt.Rows.Count < 1) return tableName;
return dt.Rows[0]["TABLE_COMMENT"].ToString();
}
public static List<string> GetTableName(string tableName)
{
@ -115,14 +123,6 @@ namespace GenerateClass.Util
return sList;
//return dt;
}
public static string GetTableComment(string tableName)
{
string sql = $"SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME ='{tableName}';";
var dt = DbHelperMySQL.Query(sql);
if (dt.Rows.Count < 1) return tableName;
return dt.Rows[0]["TABLE_COMMENT"].ToString();
}
public static List<string> GetTableName()
{
@ -136,6 +136,18 @@ namespace GenerateClass.Util
}
return sList;
}
public static List<string> GetDataBaseName(string dataBaseName = null)
{
string sql = $"SHOW DATABASES {(string.IsNullOrEmpty(dataBaseName) ? "" : $"LIKE '%{dataBaseName}%'")} ;";
var dt = DbHelperMySQL.Query(sql);
//查询数据库表中所有已录入人员
List<string> sList = new List<string>(); //存放数据库查询结果
for (int i = 0; i < dt.Rows.Count; i++)
{
sList.Add(dt.Rows[i][$"Database{(string.IsNullOrEmpty(dataBaseName) ? "" : $" (%{dataBaseName}%)")}"].ToString());
}
return sList;
}
public static Dictionary<string, Type> DbTypeDic { get; } = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
{

Loading…
Cancel
Save