public RootContext GetRootContext(string content, out SqlSyntaxError error) { error = null; MySqlParser parser = this.GetParser(content) as MySqlParser; SqlSyntaxErrorListener errorListener = this.AddParserErrorListener(parser); RootContext context = parser.root(); error = errorListener.Error; return(context); }
public override void Parse(FileInfo file) { Lexer lexer = new MySqlLexer(CharStreams.fromPath(file.FullName)); CommonTokenStream tokens = new CommonTokenStream(lexer); MySqlParser parser = new MySqlParser(tokens); ParserRuleContext context = parser.root(); var children = context.children; foreach (IParseTree child in children) { this.ParseNode(child); } }
public void Parse(string query, IParseTreeListener[] listeners) { ICharStream stream = CharStreams.fromstring(query); stream = new ToUpperStream(stream); ITokenSource lexer = new MySqlLexer(stream); ITokenStream tokens = new CommonTokenStream(lexer); var parser = new MySqlParser(tokens) { BuildParseTree = true, }; // both is possible, let's detect every type of sql this.context = parser.sqlStatement(); //var statement = parser.dmlStatement(); //var statement = parser.selectStatement(); // listener pattern RegisterListener(listeners); }
private static SqlVisitor?CreateVisitor(string query) { try { var lexer = new MySqlLexer(new CaseChangingCharStream(new AntlrInputStream(query), true)); var tokens = new CommonTokenStream(lexer); var parser = new MySqlParser(tokens); parser.BuildParseTree = true; parser.RemoveErrorListeners(); var visitor = new SqlVisitor(); visitor.Visit(parser.root()); return(visitor); } catch (Exception e) { Console.WriteLine(e); return(null); } }
private void tlstrpGenCsCode_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) { return; } // MySqlTableInfo selectedTableInfo = treeView1.SelectedNode.Tag as MySqlTableInfo; if (selectedTableInfo == null) { return; } //--------------- //create data from selected tree node //show create table string db = selectedTableInfo.OwnerDatabase.Name; string tb = selectedTableInfo.Name; MySqlConnection mySqlConn = new MySqlConnection(GetLocalConnString()); mySqlConn.Open(); string createSql = ""; string sql = "SHOW CREATE TABLE " + db + "." + tb; var cmd = new MySqlCommand(sql, mySqlConn); var reader = cmd.ExecuteReader(); if (reader.Read()) { createSql = reader.GetString(1); } reader.Close(); //----------------------- if (createSql == "") { return; } MySqlParser parser = new MySqlParser(); parser.ParseSql(createSql); MySqlInfoToCsCodeGenerator tableInfoToCsCodeGen = new MySqlInfoToCsCodeGenerator(); StringBuilder result = new StringBuilder(); List <TablePart> tables = parser.ResultTables; foreach (TablePart table in tables) { if (table.DatabaseName == null) { table.DatabaseName = db; } // tableInfoToCsCodeGen.GenerateCsCode(table, result); //tableInfoToCsCodeGen.GenerateSqlAndSave(table, "code." + db + "." + table + ".cs"); } mySqlConn.Close(); // this.textBox1.Text = result.ToString(); }