예제 #1
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);
        }
예제 #2
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();
        }
        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();
        }