如何将SQL语句进行格式化处理?
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
先看效果: 直接上代码: public Form1() { InitializeComponent(); Init(); } #region 初始化配置 ISqlTokenizer _tokenizer; ISqlTokenParser _parser; ISqlTreeFormatter _formatter; public void Init() { chk_default.Checked = true; _tokenizer = new BaiSqlFormatLib.Tokenizers.TSqlStandardTokenizer(); _parser = new BaiSqlFormatLib.Parsers.TSqlStandardParser(); } #endregion #region 格式化 private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.F6) { txt_format.DocumentText = Format(txt_sql.Text); } } private string Format(string inputSql) { SetFormatter(); var tokenizedSql = _tokenizer.TokenizeSQL(inputSql, inputSql.Length); var parsedSql = _parser.ParseSQL(tokenizedSql); string subSqlHtml = _formatter.FormatSQLTree(parsedSql); return subSqlHtml; }
/// <summary> /// 设置格式化属性 /// </summary> private void SetFormatter() { ISqlTreeFormatter innerFormatter = new BaiSqlFormatLib.Formatters.TSqlStandardFormatter(new BaiSqlFormatLib.Formatters.TsqlStandardFormatterOptions{ IndentString = "\\s\\s\\s\\s", //缩进内容 SpacesPerTab = 4, MaxLineWidth = Convert.ToInt32(txt_maxWidth.Text), //单行字符串最大长度 ExpandCommaLists = !chk_columnNotNewline.Checked, //false 字段换行 KeywordAlign = chk_keywordAlign.Checked, //字段对齐 TrailingCommas = true, //true 逗号在字段之后 SpaceAfterExpandedComma = true, //true 逗号后追加空格 ExpandBooleanExpressions = chk_conditionNewline.Checked, //true 条件换行 ExpandCaseStatements = chk_expandCase.Checked, //true case when换行 ExpandBetweenConditions = chk_expandBetween.Checked, //true between 换行 ExpandInLists = chk_expandIn.Checked, //true in 内容换行 BreakJoinOnSections = chk_expandOn.Checked, //true join on中on 条件换行 UppercaseKeywords = chk_uppercaseKeywords.Checked, //true 关键字大写 AllUpper = chk_allUpper.Checked, //true 全部大写 HTMLColoring = chk_coloring.Checked, //true HTML颜色标记 默认为true KeywordStandardization = true,//true 关键字标准化 NewStatementLineBreaks = 2, //新语句换行数 NewClauseLineBreaks = 1,//遇到关键字 换行数 AllIndent = chk_allIndent.Checked, //整体缩进一个IndentString AsAlign = chk_asAlign.Checked, //true as对齐 KeywordLengthOfAs = Convert.ToInt32(txt_asMaxWidth.Text)//as字段的最大长度 }); _formatter = new BaiSqlFormatLib.Formatters.HtmlPageWrapper(innerFormatter); } #region 页面配置 private void chk_default_CheckedChanged(object sender, EventArgs e) { if (chk_default.Checked) { chk_custom.CheckState = CheckState.Unchecked; chk_columnNotNewline.CheckState = CheckState.Unchecked; chk_keywordAlign.CheckState = CheckState.Checked; chk_conditionNewline.CheckState = CheckState.Checked; chk_expandCase.CheckState = CheckState.Checked; chk_expandBetween.CheckState = CheckState.Unchecked; chk_expandIn.CheckState = CheckState.Unchecked; chk_expandOn.CheckState = CheckState.Checked; chk_uppercaseKeywords.CheckState = CheckState.Unchecked; chk_allUpper.CheckState = CheckState.Unchecked; chk_coloring.CheckState = CheckState.Checked; chk_allIndent.CheckState = CheckState.Checked; chk_asAlign.CheckState = CheckState.Checked; txt_asMaxWidth.Text = "35"; chk_addSemicolon.CheckState = CheckState.Checked; chk_columnNotNewline.Enabled = false; chk_keywordAlign.Enabled = false; chk_conditionNewline.Enabled = false; chk_expandCase.Enabled = false; chk_expandBetween.Enabled = false; chk_expandIn.Enabled = false; chk_expandOn.Enabled = false; chk_uppercaseKeywords.Enabled = false; chk_allUpper.Enabled = false; chk_coloring.Enabled = false; chk_allIndent.Enabled = false; chk_asAlign.Enabled = false; txt_asMaxWidth.Enabled = false; chk_addSemicolon.Enabled = false; } else chk_custom.CheckState = CheckState.Checked; } private void chk_custom_CheckedChanged(object sender, EventArgs e) { if (chk_custom.Checked) { chk_default.CheckState = CheckState.Unchecked; chk_columnNotNewline.Enabled = true; chk_keywordAlign.Enabled = true; chk_conditionNewline.Enabled = true; chk_expandCase.Enabled = true; chk_expandBetween.Enabled = true; chk_expandIn.Enabled = true; chk_expandOn.Enabled = true; chk_uppercaseKeywords.Enabled = true; chk_allUpper.Enabled = true; chk_coloring.Enabled = true; chk_allIndent.Enabled = true; chk_asAlign.Enabled = true; chk_addSemicolon.Enabled = true; txt_maxWidth.Enabled = true; txt_asMaxWidth.Enabled = true; else chk_default.CheckState = CheckState.Checked; } #endregion 感兴趣的小伙伴可以自己研究研究。 该文章在 2023/9/21 16:27:57 编辑过 |
关键字查询
相关文章
正在查询... |