コード例 #1
0
        void ParseContent(TokenStream tkstream, ref TablePart table, ref FieldPart field, ref KeyPart key)
        {
            //get fields over here
            Token iden = tkstream.CurrentToken;

            switch (tkstream.CurrentToken.OriginalText)
            {
            case ",":
            {
                switch (table.PrimaryKey)
                {
                case null:
                {
                    table.FieldList.Add(field);
                    field = new FieldPart();
                }
                break;

                default:
                {
                    if (key.IndexKind != null)
                    {
                        table.KeyList.Add(key);
                        key = new KeyPart();
                    }
                }
                break;
                }
            }
            break;

            default:
            {
                switch (iden.TokenName)
                {
                case MySqlTokenName.Iden:
                {
                    string[] fieldName = iden.OriginalText.Split(new char[] { '`' }, StringSplitOptions.RemoveEmptyEntries);
                    field.FieldName = fieldName[0];
                }
                break;

                case MySqlTokenName.FieldTypeWithParen:
                {
                    CompoundToken ctk = (CompoundToken)iden;
                    field.Type   = ctk.TypeName;
                    field.Length = ctk.Content;
                }
                break;

                case MySqlTokenName.FieldType:
                {
                    field.Type = iden.OriginalText;
                }
                break;

                case MySqlTokenName.Unsigned:
                {
                    field.HasUnsign = true;
                }
                break;

                case MySqlTokenName.CharacterSet:
                {
                    tkstream.ReadNext();
                    tkstream.ReadNext();
                    Token idenNext = tkstream.CurrentToken;
                    if (idenNext.TokenName == MySqlTokenName.Unknown)
                    {
                        field.CharacterSet = idenNext.OriginalText;
                    }
                }
                break;

                case MySqlTokenName.Not:
                {
                    tkstream.ReadNext();
                    Token idenNext = tkstream.CurrentToken;
                    field.Not = idenNext.OriginalText;
                }
                break;

                case MySqlTokenName.Default:
                {
                    tkstream.ReadNext();
                    Token    idenNext = tkstream.CurrentToken;
                    string[] dfValue  = idenNext.OriginalText.Split(new string[] { "'" }, StringSplitOptions.RemoveEmptyEntries);
                    if (dfValue.Length > 0)
                    {
                        field.FieldDefault = dfValue[0];
                    }
                    else
                    {
                        field.FieldDefault = "";
                    }
                }
                break;

                case MySqlTokenName.Unknown:
                {
                    field.Other = iden.OriginalText;
                }
                break;

                case MySqlTokenName.PrimaryKey:
                {
                    tkstream.ReadNext();
                    key.IndexKind = "PRIMARY";
                }
                    goto case MySqlTokenName.IndexKey;

                case MySqlTokenName.UniqueKey:
                {
                    tkstream.ReadNext();
                    key.IndexKind = "UNIQUE";
                }
                    goto case MySqlTokenName.IndexKey;

                case MySqlTokenName.FulltextKey:
                {
                    tkstream.ReadNext();
                    key.IndexKind = "FULLTEXT";
                }
                    goto case MySqlTokenName.IndexKey;

                case MySqlTokenName.IndexKey:
                {
                    tkstream.ReadNext();
                    if (key.IndexKind == null)
                    {
                        key.IndexKind = "INDEX";
                    }
                    if (key.IndexKind == "PRIMARY")
                    {
                        string[] pkName = tkstream.CurrentToken.OriginalText.Split(new char[] { '`', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                        table.PrimaryKey = pkName[0];
                    }
                    Token    idenNext = tkstream.CurrentToken;
                    string[] ndName   = idenNext.OriginalText.Split(new char[] { '`', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                    key.IndexName = ndName[0];

                    key.IndexColumns = new List <string>();

                    if (key.IndexKind != "PRIMARY")
                    {
                        tkstream.ReadNext();
                    }
                    if (tkstream.CurrentToken.OriginalText.StartsWith("("))
                    {
                        string[] temp = tkstream.CurrentToken.OriginalText.Split('(');
                        string   pos  = temp[1];

                        while (!pos.EndsWith(")"))
                        {
                            string[] keyName = pos.Split(new char[] { '`' }, StringSplitOptions.RemoveEmptyEntries);
                            key.IndexColumns.Add(keyName[0]);
                            tkstream.ReadNext();
                            pos = tkstream.CurrentToken.OriginalText;
                        }
                        if (pos.EndsWith(")"))
                        {
                            string[] temp2   = pos.Split(')');
                            string[] keyName = temp2[0].Split(new char[] { '`' }, StringSplitOptions.RemoveEmptyEntries);
                            key.IndexColumns.Add(keyName[0]);
                            break;
                        }
                    }
                }
                break;

                case MySqlTokenName.Using:
                {
                    tkstream.ReadNext();
                    Token idenNext = tkstream.CurrentToken;
                    table.Using = idenNext.OriginalText;
                }
                break;

                case MySqlTokenName.Auto_Increment:
                {
                    field.HasAuto = true;
                }
                break;

                default:
                    field.Other = iden.OriginalText;
                    break;
                }
            }
            break;
            }

            tkstream.ReadNext();
        }
コード例 #2
0
        public void ParseSql(string createSql)
        {
            //----------------------
            //1. tokenization => tokenizer
            //2. parse => parser
            //3. semantic checking => semantic checker

            //1.1
            string[]      tokens = createSql.Split(new char[] { ' ', '\n', '\r', '=', ';' }, StringSplitOptions.RemoveEmptyEntries);
            TokenNameDict tkDict = new TokenNameDict();


            //1.2 primary tokename
            List <Token> tokenList = new List <Token>();

            foreach (string orgTk in tokens)
            {
#if DEBUG
                if (orgTk == "`Index_2`")
                {
                }
#endif
                //check iden
                if (orgTk.StartsWith("`"))
                {
                    //TODO:
                    //create iden  token here
                    Token token = new Token(orgTk);
                    token.TokenName = MySqlTokenName.Iden;
                    tokenList.Add(token);

                    continue;
                }

                if (orgTk.EndsWith(","))
                {
                    string[] temp = orgTk.Split(',');

                    foreach (string each in temp)
                    {
                        if (each != "")
                        {
                            Token tokenIden = new Token(each);
                            tokenIden.TokenName = MySqlTokenName.Iden;
                            tkDict.AssignTokenName(tokenIden);
                            tokenList.Add(tokenIden);
                        }
                        else
                        {
                            Token tokenComma = new Token(",");
                            tokenComma.TokenName = MySqlTokenName.Comma;
                            tokenList.Add(tokenComma);
                        }
                    }

                    continue;
                }

                if (orgTk.StartsWith(")"))
                {
                    Token token = new Token(orgTk);
                    token.TokenName = MySqlTokenName.ParenClose;
                    tokenList.Add(token);

                    continue;
                }

                //check paren
                int openParen_pos = orgTk.IndexOf('(');
                if (openParen_pos > -1)
                {
                    if (openParen_pos > 0)
                    {
                        int closeParen_pos = orgTk.LastIndexOf(')');
                        if (closeParen_pos < 0)
                        {
                            //TODO: check if this can occur?
                            throw new MyParserNotSupportException();
                        }
                        //-----------------
                        CompoundToken compToken = new CompoundToken();
                        compToken.TypeName  = orgTk.Substring(0, openParen_pos);
                        compToken.Content   = orgTk.Substring(openParen_pos + 1, closeParen_pos - openParen_pos - 1);
                        compToken.TokenName = MySqlTokenName.FieldTypeWithParen;
                        //tkDict.AssignTokenName(compToken);
                        tokenList.Add(compToken);
                    }
                    else //openParen_pos == 0
                    {
                        Token token = new Token(orgTk);
                        tkDict.AssignTokenName(token);

                        if (orgTk.EndsWith(")"))
                        {
                            token.TokenName = MySqlTokenName.Iden;
                        }
                        else
                        {
                            token.TokenName = MySqlTokenName.ParenOpen;
                        }

                        tokenList.Add(token);
                    }
                    continue;
                }
                else
                {
                    Token token = new Token(orgTk);
                    tkDict.AssignTokenName(token);
                    tokenList.Add(token);
                }
            }

            //2.1 ....
            //parse
            TokenStream tokenstrm = new TokenStream(tokenList);
            int         count     = tokenstrm.Count;

            TablePart tableResult;
            tableTreeList = new List <TablePart>();

            while (!tokenstrm.IsEnd)
            {
                Token tk = tokenstrm.CurrentToken;
                switch (tk.TokenName)
                {
                case MySqlTokenName.Create:
                {
                    tokenstrm.ReadNext();
                    Token nextTk = tokenstrm.CurrentToken;
                    switch (nextTk.TokenName)
                    {
                    case MySqlTokenName.Table:
                    {
                        TablePart table = new TablePart();
                        tokenstrm.ReadNext();
                        tableResult = ParseCreateTable(tokenstrm, table);
                        tableTreeList.Add(tableResult);
                    }
                    break;
                    }
                }
                break;
                }
                tokenstrm.ReadNext();
            }
        }