Inheritance: ParserBase
Beispiel #1
0
        private void fButtonModify_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fTextBoxOriginal.Text.Trim()))
            {
                fTextBoxFinal.Text = string.Empty;
                return;
            }

            try
            {
                SqlParser myParser = new SqlParser();
                myParser.Parse(fTextBoxOriginal.Text);

                if (!string.IsNullOrEmpty(fTextBoxWhereClause.Text.Trim()))
                {
                    string myOrginalWhereClause = myParser.WhereClause;
                    if (string.IsNullOrEmpty(myOrginalWhereClause))
                        myParser.WhereClause = fTextBoxWhereClause.Text;
                    else
                        myParser.WhereClause = string.Format("({0}) AND ({1})", myOrginalWhereClause, fTextBoxWhereClause.Text);
                }

                if (!string.IsNullOrEmpty(fTextBoxOrderBy.Text.Trim()))
                {
                    string myOrginalOrderByClause = myParser.OrderByClause;

                    if (string.IsNullOrEmpty(myOrginalOrderByClause))
                        myParser.OrderByClause = fTextBoxOrderBy.Text;
                    else
                        myParser.OrderByClause = string.Format("{0}, {1}", myOrginalOrderByClause, fTextBoxOrderBy.Text);
                }

                fTextBoxFinal.Text = myParser.ToText();
            }
            catch (Exception myEx)
            {
                MessageBox.Show(myEx.Message);
            }
        }
    protected void ParseSqlFile(string sql_file)
    {
        SqlParser myParser = new SqlParser();
        string database_name = null;
        Hashtable tables = new Hashtable();

        XmlDocument doc = myParser.Parse(sql_file);
        XmlNodeList create_list = doc.SelectNodes("//Text[@Value='CREATE'] | //Text[@Value='create']");
        foreach (XmlNode create_node in create_list)
        {
            XmlNode next = create_node.NextSibling;
            if (next.Attributes[0].InnerText.ToLower() == "database")
            {
                for (int i = 0; i < 5; i++)
                {
                    next = next.NextSibling;
                    if (next.Attributes[0].InnerText.ToLower() != "if" &&
                       next.Attributes[0].InnerText.ToLower() != "not" &&
                       next.Attributes[0].InnerText.ToLower() != "exists" &&
                       next.Attributes[0].InnerText.ToLower() != "`")
                    {
                        database_name = next.Attributes[0].InnerText;
                    }
                }
            }
            else if (next.Attributes[0].InnerText.ToLower() == "table")
            {
                next = next.NextSibling.NextSibling;
                string table = next.Attributes[0].InnerText;

                //get first braces
                XmlNode brace_node = null;
                for (int i = 0; i < 5; i++)
                {
                    next = next.NextSibling;
                    if (next.Attributes[0].InnerText.ToLower() == "braces" )
                    {
                       brace_node = next;
                        break;
                    }
                }
                //get fields
                bool quote_toggle = true;
                ArrayList field_list = new ArrayList();

                foreach (XmlNode node in brace_node.ChildNodes)
                {
                    if (node.Attributes[0].InnerText == "`" && quote_toggle)
                    {
                        quote_toggle = false;
                        next = node.NextSibling;
                        Hashtable field = new Hashtable();
                        string field_name = next.Attributes[0].InnerText;
                        field["name"] = field_name;
                        next = next.NextSibling.NextSibling;
                        string field_type = next.Attributes[0].InnerText;
                        field["type"] = field_type;
                        field_list.Add(field);
                        next = next.NextSibling;
                        if (next.Attributes[0].InnerText == "BRACES")
                        {
                            string field_length = next.ChildNodes[0].Attributes[0].InnerText;
                            field["length"] = field_length;
                        }
                    }
                    else if (node.Attributes[0].InnerText == "`" && !quote_toggle)
                    {
                        quote_toggle = true;
                    }
                }

                tables[table] = field_list;
            }
        }

        //save in app xml
        Util util = new Util();
        util.SaveDatabaseSchema((Hashtable)HttpRuntime.Cache[Session.SessionID], DatabaseType.SelectedValue, DBConnectionString.Text, tables);
    }