Ejemplo n.º 1
0
        void ParseLastContent(TokenStream tkstream, ref TablePart table)
        {
            Token iden = tkstream.CurrentToken;

            if (iden.TokenName == MySqlTokenName.Engine)
            {
                tkstream.ReadNext();
                Token idenNext = tkstream.CurrentToken;
                table.Engine = idenNext.OriginalText;
            }
            else if (iden.TokenName == MySqlTokenName.Auto_Increment)
            {
                tkstream.ReadNext();
                Token idenNext = tkstream.CurrentToken;
                table.Auto_increment = idenNext.OriginalText;
            }
            else if (iden.TokenName == MySqlTokenName.Default)
            {
                table.HasDefault = true;
            }
            else if (iden.TokenName == MySqlTokenName.CharacterSet)
            {
                tkstream.ReadNext();
                Token idenNext = tkstream.CurrentToken;
                table.Charset = idenNext.OriginalText;
            }
            else
            {
                //more condition
                throw new MyParserNotSupportException();
            }

            tkstream.ReadNext();
        }
Ejemplo n.º 2
0
        void ParseIden(TokenStream tkstream, ref TablePart table)
        {
            Token iden = tkstream.CurrentToken;

            if (iden.TokenName == MySqlTokenName.Iden)
            {
                //get table name here
                table.TableName = iden.ToString();

                string[] splitName = iden.OriginalText.Split('.');

                if (splitName.Length > 1)
                {
                    string[] dbName = splitName[0].Split(new char[] { '`' }, StringSplitOptions.RemoveEmptyEntries);
                    table.DatabaseName = dbName[0];
                    string[] tbName = splitName[1].Split(new char[] { '`' }, StringSplitOptions.RemoveEmptyEntries);
                    table.TableName = tbName[0];
                }
                else
                {
                    string[] tbName = splitName[0].Split(new char[] { '`' }, StringSplitOptions.RemoveEmptyEntries);
                    table.TableName = tbName[0];
                }


                tkstream.ReadNext();
            }
            else
            {
                //HANDLE
                throw new MyParserNotSupportException();
            }
        }
        public void GenerateCsCode(TablePart table, StringBuilder strb)
        {
            _table = table;
            //
            strb.AppendLine("//" + DateTime.Now.ToString("s"));
            strb.AppendLine("using System;");
            strb.AppendLine("");
            strb.AppendLine("namespace " + table.DatabaseName);
            strb.AppendLine("{");

            if (_generateAttr)
            {
                CreateDBTableAttribute(strb);

                CreateDBFieldAttribute(strb);

                CreateIndexAttribute(strb);
            }

            CreateInterfaceTable(strb, table);

            CreateInterfaceIndexKeys(strb, table.KeyList);

            strb.Append("}");
        }
        public void GenerateCsCodeAndSave(TablePart table, string saveToFilename)
        {
            _table = table;
            //
            StringBuilder output = new StringBuilder();

            GenerateCsCode(table, output);
            System.IO.File.WriteAllText(saveToFilename, output.ToString());
        }
Ejemplo n.º 5
0
        TablePart ParseCreateTable(TokenStream tkstream, TablePart table)
        {
            //TablePart table = new TablePart();
            FieldPart field = new FieldPart();
            KeyPart   key   = new KeyPart();

            ParseIden(tkstream, ref table);
            table.FieldList = new List <FieldPart>();
            table.KeyList   = new List <KeyPart>();

            if (tkstream.CurrentToken.TokenName == MySqlTokenName.ParenOpen)
            {
                ParseOpenParen(tkstream);
            }
            else
            {
                throw new MyParserNotSupportException();
            }
            //..
            //fields ...

            //Token iden = tkstream.CurrentToken;

            while (tkstream.CurrentToken.TokenName != MySqlTokenName.ParenClose)
            {
                string item = tkstream.CurrentToken.ToString();

                /*if (tkstream.CurrentToken.OriginalText == ",")
                 * {
                 *  table.FieldList.Add(field);
                 *
                 *  field = new FieldPart();
                 * }*/

                ParseContent(tkstream, ref table, ref field, ref key);
                //table.FieldList.Add(field);
            }

            //....
            ParseCloseParen(tkstream, ref table, ref key);

            while (tkstream.CurrentToken.TokenName != MySqlTokenName.ParenClose)
            {
                ParseLastContent(tkstream, ref table);
                if (tkstream.IsEnd || tkstream.CurrentToken.TokenName == MySqlTokenName.Drop)
                {
                    break;
                }
            }

            return(table);
        }
Ejemplo n.º 6
0
        void ParseCloseParen(TokenStream tkstream, ref TablePart table, ref KeyPart key)
        {
            //close
            if (key.IndexKind != null)
            {
                table.KeyList.Add(key);
                key = new KeyPart();
            }

            Token iden = tkstream.CurrentToken;

            tkstream.ReadNext();
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
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();
            }
        }
        void CreateInterfaceTable(StringBuilder strb, TablePart table)
        {
            strb.Append("\t[DBTable(");
            if (table.DatabaseName != null)
            {
                strb.Append("DatabaseName=" + '"' + table.DatabaseName + '"');
            }
            if (table.TableName != null)
            {
                strb.Append(", TableName=" + '"' + table.TableName + '"');
            }
            if (table.Auto_increment != null)
            {
                strb.Append(", Auto_increment=" + '"' + table.Auto_increment + '"');
            }
            if (table.Charset != null)
            {
                strb.Append(", Charset=" + '"' + table.Charset + '"');
            }
            if (table.Engine != null)
            {
                strb.Append(", Engine=" + '"' + table.Engine + '"');
            }
            if (table.HasDefault)
            {
                strb.Append(", Default=true");
            }
            if (table.Using != null)
            {
                strb.Append(", Using=" + '"' + table.Using + '"');
            }
            strb.AppendLine(")]");
            strb.AppendLine("\tinterface " + table.TableName);
            strb.AppendLine("\t{");

            string temp = strb.ToString();

            if (table.FieldList.Count != 0)
            {
                FieldPart[] fields = table.FieldList.ToArray();
                for (int i = 0; i < fields.Length; ++i)
                {
                    FieldPart field = fields[i];

                    strb.Append("\t\t[DBField(");
                    if (field.FieldName != null)
                    {
                        strb.Append("FieldName=" + '"' + field.FieldName + '"');
                    }
                    if (field.CharacterSet != null)
                    {
                        strb.Append(", CharacterSet=" + '"' + field.CharacterSet + '"');
                    }
                    if (field.FieldDefault != null)
                    {
                        strb.Append(", FieldDefault=" + '"' + field.FieldDefault + '"');
                    }
                    if (field.HasAuto)
                    {
                        strb.Append(", HasAuto=true");
                    }
                    if (field.HasUnsign)
                    {
                        strb.Append(", HasUnsign=true");
                    }
                    if (field.Type != null)
                    {
                        strb.Append(", Type=" + '"' + field.Type + '"');
                    }
                    if (field.Not != null)
                    {
                        strb.Append(", Not=" + '"' + field.Not + '"');
                    }
                    if (field.Length != null)
                    {
                        strb.Append(", Length=" + field.Length);
                    }
                    if (field.Other != null)
                    {
                        strb.Append(", Other=" + '"' + field.Other + '"');
                    }
                    strb.AppendLine(")]");

                    string kind = "";
                    switch (field.Type)
                    {
                    case "varchar": kind = "string"; break;

                    case "float": kind = "float"; break;

                    case "text": kind = "string"; break;

                    case "int": kind = "int"; break;

                    case "datetime": kind = "string"; break;

                    case "char": kind = "string"; break;

                    case "blob": kind = "string"; break;

                    case "bool": kind = "bool"; break;
                    }

                    strb.AppendLine("\t\t" + kind + " " + fields[i] + "{ get; set; }");
                }
            }
            strb.AppendLine("\t}");
            strb.AppendLine("");

            string temp2 = strb.ToString();
        }