607 lines
20 KiB
C#
607 lines
20 KiB
C#
using MES.Utility.Core;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using 常用工具集.Base;
|
|
using NHibernate.AdoNet.Util;
|
|
using System.Xml;
|
|
using System.IO;
|
|
|
|
namespace 常用工具集.ViewModel._05其他
|
|
{
|
|
public class SQLStringBuilder封装ViewModel : ViewModelBase
|
|
{
|
|
public string Button1Text { get; set; } = "生成";
|
|
public string TextBox1 { get; set; } = "";
|
|
public string TextBox2 { get; set; } = "";
|
|
public string TextBox3 { get; set; } = "strSql";
|
|
public string Text3Title { get; set; } = "StringBuilder参数:";
|
|
|
|
public bool TextBox3Visiable { get; set; } = true;
|
|
public List<string> FuncList { get; set; }
|
|
|
|
public int funcIndex = 0;
|
|
public int FuncIndex
|
|
{
|
|
get
|
|
{
|
|
return funcIndex;
|
|
}
|
|
set
|
|
{
|
|
funcIndex = value;
|
|
SelectChanged();
|
|
NotifyPropertyChanged();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public bool RadioButton1 { get; set; } = true;
|
|
public bool RadioButton2 { get; set; } = false;
|
|
public bool RadioButton3 { get; set; } = false;
|
|
public string RadioButton1Text { get; set; } = "Java";
|
|
public string RadioButton2Text { get; set; } = "C#";
|
|
public string RadioButton3Text { get; set; } = "JS";
|
|
public bool RadioButton1Visiable { get; set; } = true;
|
|
public bool RadioButton2Visiable { get; set; } = true;
|
|
public bool RadioButton3Visiable { get; set; } = true;
|
|
|
|
|
|
|
|
public bool CheckBox1 { get; set; } = false;
|
|
public bool CheckBox3 { get; set; } = true;
|
|
public bool CheckBox2 { get; set; } = true;
|
|
public bool CheckBox1Visiable { get; set; } = true;
|
|
public bool CheckBox3Visiable { get; set; } = true;
|
|
public bool CheckBox2Visiable { get; set; } = true;
|
|
|
|
public DelegateCommand GenerateCmd { get; set; }
|
|
public DelegateCommand ClearCmd { get; set; }
|
|
|
|
public SQLStringBuilder封装ViewModel()
|
|
{
|
|
FuncList = new List<string>() {
|
|
"SQL转StringBuilder",
|
|
"XML格式化",
|
|
"SQL格式化",
|
|
"StringBuilder转SQL",
|
|
"大小写转换",
|
|
"JSON格式化"
|
|
};
|
|
FuncIndex = 0;
|
|
GenerateCmd = new DelegateCommand(GenerateCmdFunc);
|
|
ClearCmd = new DelegateCommand(ClearCmdFunc);
|
|
}
|
|
|
|
private void GenerateCmdFunc(object obj)
|
|
{
|
|
//SQL转StringBuilder
|
|
if (FuncIndex == 0)
|
|
{
|
|
if (CheckBox1)
|
|
{
|
|
string text2 = FormatStyle.Basic.Formatter.Format(TextBox1);
|
|
TextBox1 = text2;
|
|
}
|
|
List<string> first = GetFirstContext();
|
|
List<string> second = new List<string>();
|
|
if (first.Count == 0)
|
|
{
|
|
TextBox2 = string.Empty;
|
|
return;
|
|
}
|
|
int maxLength = GetMaxLength(GetNewList(first));
|
|
|
|
if (RadioButton3)
|
|
{
|
|
second.Add("var " + GetParmName() + " = \"\";");
|
|
}
|
|
else
|
|
{
|
|
second.Add("StringBuilder " + GetParmName() + " = new StringBuilder();");
|
|
}
|
|
//循环处理每一行
|
|
foreach (string str in first)
|
|
{
|
|
if (!CheckBox3)
|
|
{
|
|
if (str.Trim().StartsWith("--"))
|
|
{
|
|
second.Add(str.Trim().Replace("--", "//"));
|
|
continue;
|
|
}
|
|
if (str.Contains("--"))
|
|
{
|
|
//分割
|
|
int index = str.IndexOf("--");
|
|
string str1 = str.Substring(0, index).TrimEnd();
|
|
string str2 = str.Substring(index, str.Length - index).Trim();
|
|
if (!CheckBox2)
|
|
{
|
|
second.Add(GetString2(str1, 0, CheckBox3) + " " + str2.Replace("--", "//"));
|
|
}
|
|
else
|
|
{
|
|
string retStr = GetString(str1, maxLength - System.Text.Encoding.Default.GetByteCount(str1), CheckBox3) + " " + str2.Replace("--", "//");
|
|
second.Add(retStr);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
}
|
|
if (!CheckBox2)
|
|
{
|
|
second.Add(GetString2(str, 0, CheckBox3));
|
|
}
|
|
else
|
|
{
|
|
second.Add(GetString(str, maxLength - System.Text.Encoding.Default.GetByteCount(str), CheckBox3));
|
|
}
|
|
}
|
|
|
|
TextBox2 = second.GetStrArray("\r\n");
|
|
}
|
|
//XML格式化
|
|
else if (FuncIndex == 1)
|
|
{
|
|
if (TextBox1.IsNullOrEmpty())
|
|
{
|
|
TextBox2 = string.Empty;
|
|
return;
|
|
}
|
|
TextBox2 = FormatXml(TextBox1);
|
|
}
|
|
//SQL格式化
|
|
else if (FuncIndex == 2)
|
|
{
|
|
if (TextBox1.IsNullOrEmpty())
|
|
{
|
|
TextBox2 = string.Empty;
|
|
return;
|
|
}
|
|
TextBox2 = FormatStyle.Basic.Formatter.Format(TextBox1);
|
|
}
|
|
//StringBuilder转SQL
|
|
else if (FuncIndex == 3)
|
|
{
|
|
List<string> first = GetFirstContext();
|
|
List<string> second = new List<string>();
|
|
if (first.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
//判断是否每一行是否以StringBuilder开头
|
|
string parmName = GetStringBuilderParmName(first[0]);
|
|
if (parmName.IsNullOrEmpty())
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 1; i < first.Count; i++)
|
|
{
|
|
string str = first[i];
|
|
string handlerStr = HandlerString(str, parmName);
|
|
if (!String.Empty.Equals(handlerStr))
|
|
{
|
|
second.Add(handlerStr);
|
|
}
|
|
}
|
|
if (second.Count != 0)
|
|
{
|
|
int spaceNum = second[0].IndexOf(second[0].Trim());
|
|
for (int i = 0; i < second.Count; i++)
|
|
{
|
|
second[i] = QuSpace(second[i], spaceNum);
|
|
}
|
|
}
|
|
TextBox2 = second.GetStrArray("\r\n");
|
|
}
|
|
//大小写转换
|
|
else if (FuncIndex == 4)
|
|
{
|
|
if (TextBox1.IsNullOrEmpty())
|
|
{
|
|
TextBox2 = string.Empty;
|
|
return;
|
|
}
|
|
List<string> first = GetFirstContext();
|
|
List<string> second = new List<string>();
|
|
if (RadioButton1)
|
|
{
|
|
foreach (string str in first)
|
|
{
|
|
second.Add(str.ToUpper());
|
|
}
|
|
}
|
|
else if (RadioButton2)
|
|
{
|
|
foreach (string str in first)
|
|
{
|
|
second.Add(str.ToLower());
|
|
}
|
|
}
|
|
TextBox2 = second.GetStrArray("\r\n");
|
|
|
|
}
|
|
//JSON格式化
|
|
else if (FuncIndex == 5)
|
|
{
|
|
if (TextBox1.IsNullOrEmpty())
|
|
{
|
|
TextBox2 = string.Empty;
|
|
return;
|
|
}
|
|
TextBox2 = TextBox1.FormatJson();
|
|
}
|
|
}
|
|
|
|
private void ClearCmdFunc(object obj)
|
|
{
|
|
TextBox1 = string.Empty;
|
|
TextBox2 = string.Empty;
|
|
}
|
|
|
|
private void SelectChanged()
|
|
{
|
|
//SQL转StringBuilder
|
|
if (FuncIndex == 0)
|
|
{
|
|
RadioButton1Visiable = true;
|
|
RadioButton2Visiable = true;
|
|
RadioButton3Visiable = true;
|
|
RadioButton1Text = "Java";
|
|
RadioButton2Text = "C#";
|
|
RadioButton3Text = "JS";
|
|
CheckBox1Visiable = true;
|
|
CheckBox2Visiable = true;
|
|
CheckBox3Visiable = true;
|
|
TextBox3Visiable = true;
|
|
Button1Text = "生成";
|
|
Text3Title = "StringBuilder参数";
|
|
TextBox3 = "strSql";
|
|
}
|
|
//StringBuilder转SQL
|
|
else if (FuncIndex == 3)
|
|
{
|
|
RadioButton1Visiable = false;
|
|
RadioButton2Visiable = false;
|
|
RadioButton3Visiable = false;
|
|
TextBox3Visiable = false;
|
|
CheckBox1Visiable =false;
|
|
CheckBox2Visiable =false;
|
|
CheckBox3Visiable = false;
|
|
Button1Text = "生成";
|
|
}
|
|
//XML格式化
|
|
else if (FuncIndex == 1)
|
|
{
|
|
RadioButton1Visiable = false;
|
|
RadioButton2Visiable = false;
|
|
RadioButton3Visiable = false;
|
|
TextBox3Visiable = false;
|
|
CheckBox1Visiable = false;
|
|
CheckBox2Visiable = false;
|
|
CheckBox3Visiable = false;
|
|
Button1Text = "格式化";
|
|
}
|
|
//JSON格式化
|
|
else if (FuncIndex == 5)
|
|
{
|
|
RadioButton1Visiable = false;
|
|
RadioButton2Visiable = false;
|
|
RadioButton3Visiable = false;
|
|
TextBox3Visiable = false;
|
|
CheckBox1Visiable = false;
|
|
CheckBox2Visiable = false;
|
|
CheckBox3Visiable = false;
|
|
Button1Text = "格式化";
|
|
}
|
|
//SQL格式化
|
|
else if (FuncIndex == 2)
|
|
{
|
|
RadioButton1Visiable = false;
|
|
RadioButton2Visiable = false;
|
|
RadioButton3Visiable = false;
|
|
TextBox3Visiable = false;
|
|
CheckBox1Visiable =false;
|
|
CheckBox2Visiable =false;
|
|
CheckBox3Visiable = false;
|
|
Button1Text = "格式化";
|
|
}
|
|
//大小写转换
|
|
else if (FuncIndex == 4)
|
|
{
|
|
RadioButton1Visiable = true;
|
|
RadioButton2Visiable = true;
|
|
RadioButton3Visiable = false;
|
|
RadioButton1Text = "转大写";
|
|
RadioButton2Text = "转小写";
|
|
RadioButton1 = true;
|
|
TextBox3Visiable = false;
|
|
CheckBox1Visiable = false;
|
|
CheckBox2Visiable = false;
|
|
CheckBox3Visiable = false;
|
|
Button1Text = "转换";
|
|
}
|
|
}
|
|
|
|
private string FormatXml(string sUnformattedXml)
|
|
{
|
|
XmlDocument xd = new XmlDocument();
|
|
xd.LoadXml(sUnformattedXml);
|
|
StringBuilder sb = new StringBuilder();
|
|
StringWriter sw = new StringWriter(sb);
|
|
XmlTextWriter xtw = null;
|
|
try
|
|
{
|
|
xtw = new XmlTextWriter(sw);
|
|
xtw.Formatting = Formatting.Indented;
|
|
xtw.Indentation = 1;
|
|
xtw.IndentChar = '\t';
|
|
xd.WriteTo(xtw);
|
|
}
|
|
finally
|
|
{
|
|
if (xtw != null)
|
|
xtw.Close();
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
|
|
public string GetParmName()
|
|
{
|
|
string parm = TextBox3.Trim();
|
|
if (parm.IsNullOrEmpty())
|
|
{
|
|
return "strSql";
|
|
}
|
|
return parm;
|
|
}
|
|
|
|
#region 给定一个集合得到去注释后的集合
|
|
public List<string> GetNewList(List<string> list)
|
|
{
|
|
List<string> list2 = new List<string>();
|
|
foreach (string str in list)
|
|
{
|
|
//if (str.Trim().StartsWith("--"))
|
|
//{
|
|
// continue;
|
|
//}
|
|
//if (str.Contains("--"))
|
|
//{
|
|
// int index = str.IndexOf("--");
|
|
// list2.Add(str.Substring(0, index));
|
|
// continue;
|
|
//}
|
|
list2.Add(str.TrimEnd());
|
|
}
|
|
return list2;
|
|
}
|
|
#endregion
|
|
|
|
#region 给定一个List得到其中最长的长度
|
|
public int GetMaxLength(List<string> list)
|
|
{
|
|
int maxLength = 0;
|
|
foreach (string str in list)
|
|
{
|
|
if (System.Text.Encoding.Default.GetByteCount(str) > maxLength)
|
|
{
|
|
maxLength = System.Text.Encoding.Default.GetByteCount(str);
|
|
}
|
|
}
|
|
return maxLength;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 获得每一行值
|
|
public string GetString(string strInfo, int spaceNum, bool appendLine)
|
|
{
|
|
string str = string.Empty;
|
|
if (RadioButton1)
|
|
{
|
|
if (appendLine)
|
|
{
|
|
str += GetParmName() + ".append(\" " + strInfo + GetSpace(spaceNum) + " \").append(\"\\n\");";
|
|
}
|
|
else
|
|
{
|
|
str += GetParmName() + ".append(\" " + strInfo + GetSpace(spaceNum) + " \");";
|
|
}
|
|
}
|
|
if (RadioButton2)
|
|
{
|
|
if (appendLine)
|
|
{
|
|
str += GetParmName() + ".Append(\" " + strInfo + GetSpace(spaceNum) + " \").AppendLine();";
|
|
}
|
|
else
|
|
{
|
|
str += GetParmName() + ".Append(\" " + strInfo + GetSpace(spaceNum) + " \");";
|
|
}
|
|
}
|
|
if (RadioButton3)
|
|
{
|
|
if (appendLine)
|
|
{
|
|
str += GetParmName() + " += \" " + strInfo + GetSpace(spaceNum) + " \\n\";";
|
|
}
|
|
else
|
|
{
|
|
str += GetParmName() + " += \" " + strInfo + GetSpace(spaceNum) + " \";";
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
public string GetString2(string strInfo, int spaceNum, bool appendLine)
|
|
{
|
|
string str = string.Empty;
|
|
if (RadioButton1)
|
|
{
|
|
if (appendLine)
|
|
{
|
|
str += GetParmName() + ".append(\" " + strInfo + GetSpace(spaceNum) + "\").append(\"\\n\");";
|
|
}
|
|
else
|
|
{
|
|
str += GetParmName() + ".append(\" " + strInfo + GetSpace(spaceNum) + "\");";
|
|
}
|
|
}
|
|
if (RadioButton2)
|
|
{
|
|
if (appendLine)
|
|
{
|
|
str += GetParmName() + ".Append(\" " + strInfo + GetSpace(spaceNum) + "\").AppendLine();";
|
|
}
|
|
else
|
|
{
|
|
str += GetParmName() + ".Append(\" " + strInfo + GetSpace(spaceNum) + "\");";
|
|
}
|
|
}
|
|
if (RadioButton3)
|
|
{
|
|
if (appendLine)
|
|
{
|
|
str += GetParmName() + " += \" " + strInfo + GetSpace(spaceNum) + "\\n\";";
|
|
}
|
|
else
|
|
{
|
|
str += GetParmName() + " += \" " + strInfo + GetSpace(spaceNum) + "\";";
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
#endregion
|
|
|
|
#region 补空格
|
|
public string GetSpace(int num)
|
|
{
|
|
string str = string.Empty;
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
str += " ";
|
|
}
|
|
return str;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 得到上面文本的每一行值
|
|
public List<string> GetFirstContext()
|
|
{
|
|
List<string> list = new List<string>();
|
|
string content = TextBox1;
|
|
if (string.Empty.Equals(content.Trim()))
|
|
{
|
|
return list;
|
|
}
|
|
if (content.Contains("\t"))
|
|
{
|
|
content = content.Replace("\t", " ");
|
|
}
|
|
content = content.Replace("\r", "");
|
|
string[] str = content.Split('\n');
|
|
foreach (string strings in str)
|
|
{
|
|
if (!string.Empty.Equals(strings.Trim()))
|
|
{
|
|
list.Add(strings.TrimEnd());
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
private string QuSpace(string str, int spaceNum)
|
|
{
|
|
string space = string.Empty;
|
|
for (int i = 0; i < spaceNum; i++)
|
|
{
|
|
space += " ";
|
|
}
|
|
if (!str.Contains(space))
|
|
{
|
|
return str;
|
|
}
|
|
return str.Substring(str.IndexOf(space) + spaceNum, str.Length - str.IndexOf(space) - spaceNum);
|
|
}
|
|
|
|
private string HandlerString(string str, string parmName)
|
|
{
|
|
if (str.Contains(parmName + ".Append"))
|
|
{
|
|
//得到StringBuilder中间的词
|
|
string sqlStr = GetStringBuilderText(str, parmName + ".Append");
|
|
//得到注释
|
|
string zhushi = GetZhuShi(str);
|
|
//拼接
|
|
if (string.Empty.Equals(zhushi))
|
|
{
|
|
return sqlStr.TrimEnd();
|
|
}
|
|
else
|
|
{
|
|
return sqlStr.TrimEnd() + " --" + zhushi;
|
|
}
|
|
}
|
|
if (str.Contains(parmName + ".append"))
|
|
{
|
|
//得到StringBuilder中间的词
|
|
string sqlStr = GetStringBuilderText(str, parmName + ".append");
|
|
//得到注释
|
|
string zhushi = GetZhuShi(str);
|
|
//拼接
|
|
if (string.Empty.Equals(zhushi))
|
|
{
|
|
return sqlStr.TrimEnd();
|
|
}
|
|
else
|
|
{
|
|
return sqlStr.TrimEnd() + " --" + zhushi;
|
|
}
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
private string GetStringBuilderText(string str, string startStr)
|
|
{
|
|
startStr = startStr + "(\"";
|
|
str = str.Substring(str.IndexOf(startStr), str.IndexOf("\")") - str.IndexOf(startStr));
|
|
str = str.Substring(startStr.Length, str.Length - startStr.Length);
|
|
return str;
|
|
}
|
|
|
|
private string GetZhuShi(string str)
|
|
{
|
|
string retStr = string.Empty;
|
|
if (str.Contains(@"//"))
|
|
{
|
|
retStr = str.Substring(str.IndexOf("//") + 2, str.Length - str.IndexOf("//") - 2);
|
|
}
|
|
return retStr;
|
|
}
|
|
|
|
private string GetStringBuilderParmName(string str)
|
|
{
|
|
string parmName = String.Empty;
|
|
if (!str.Contains("StringBuilder"))
|
|
{
|
|
return parmName;
|
|
}
|
|
str = str.Substring(str.IndexOf("StringBuilder"), str.IndexOf("=") - str.IndexOf("StringBuilder"));
|
|
str = str.Substring(str.IndexOf(" "), str.Length - str.IndexOf(" "));
|
|
parmName = str.Trim();
|
|
return parmName;
|
|
}
|
|
}
|
|
}
|