using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using GenerateClass.Util;
using JinianNet.JNTemplate;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace GenerateClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadControl();
}
private string tableName;
#region 加载控件
private void loadControl()
{
loadTableNameControl();
loadDataBaseNameControl();
}
///
/// 加载表名控件
///
private void loadTableNameControl()
{
this.txt_tablename.Items.AddRange(DbUtil.GetTableName().ToArray());
this.txt_tablename.TextUpdate += (childSender, childE) =>
{
string str = this.txt_tablename.Text; //获取输入内容
List sList = DbUtil.GetTableName(str); //存放数据库查询结果
//提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容)
this.txt_tablename.DroppedDown = true; //显示下拉列表,但是显示后鼠标指针就不见了
Cursor.Current = Cursors.Default; //将指针显示出来
//在表中已录入名字中寻找包含输入内容的项 有则添加到comboBox项中
this.txt_tablename.Items.Clear();
this.txt_tablename.Items.AddRange(sList.ToArray());
this.txt_tablename.Select(this.txt_tablename.Text.Length, 0);
};
}
///
/// 加载数据库名控件
///
private void loadDataBaseNameControl()
{
this.txt_database.Items.AddRange(DbUtil.GetDataBaseName().ToArray());
this.txt_database.TextUpdate += (childSender, childE) =>
{
string str = this.txt_database.Text; //获取输入内容
List 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();//要生成的表名
string namespaceName = namespace_name.Text.Trim();//名称空间名称
List> filedInfos = DbUtil.getTableInfo(tbname, textBox3.Text.Trim());
using (StreamReader reader = new StreamReader(@".\Template\ModelFile.template"))
{
string fileContent = reader.ReadToEnd();
var template = Engine.CreateTemplate(fileContent);
template.Set("namespace_name", namespaceName);
template.Set("table_comment", DbUtil.GetTableComment(tbname));
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;
DbUtil.openFile(tbname, result);
}
}
private void fillDgv1Template()
{
string tbname = txt_tablename.Text.Trim();//要生成的表名
string dgvName = textBox2.Text.Trim(); //dgv空间名称
List> filedInfos = DbUtil.getTableInfo(tbname, textBox3.Text.Trim());
List timeFileds = DbUtil.getTimeFields(filedInfos);
using (StreamReader reader = new StreamReader(@".\Template\DataGridView_1.template"))
{
string fileContent = reader.ReadToEnd();
var template = Engine.CreateTemplate(fileContent);
template.Set("table_comment", DbUtil.GetTableComment(tbname));
template.Set("sql_table_name", tbname);
template.Set("table_name", DbUtil.GenerateClassName(tbname));
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;
}
}
private void fillDgv2Template()
{
string tbname = txt_tablename.Text.Trim();//要生成的表名
string dgvName = textBox2.Text.Trim(); //dgv空间名称
List timeFileds = DbUtil.getTimeFields(DbUtil.getTableInfo(tbname, textBox3.Text.Trim()));
using (StreamReader reader = new StreamReader(@".\Template\DataGridView_2.template"))
{
string fileContent = reader.ReadToEnd();
var template = Engine.CreateTemplate(fileContent);
template.Set("table_comment", DbUtil.GetTableComment(tbname));
template.Set("sql_table_name", tbname);
template.Set("table_name", DbUtil.GenerateClassName(tbname));
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;
}
}
private void fillDgv3Template()
{
string tbname = txt_tablename.Text.Trim();//要生成的表名
string dgvName = textBox2.Text.Trim(); //dgv空间名称
List> filedInfos = DbUtil.getTableInfo(tbname, textBox3.Text.Trim());
List timeFileds = DbUtil.getTimeFields(filedInfos);
using (StreamReader reader = new StreamReader(@".\Template\DataGridView_3.template"))
{
string fileContent = reader.ReadToEnd();
var template = Engine.CreateTemplate(fileContent);
template.Set("table_comment", DbUtil.GetTableComment(tbname));
template.Set("sql_table_name", tbname);
template.Set("table_name", DbUtil.GenerateClassName(tbname));
template.Set("dgv_name", dgvName);
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());
}
}
}