Esempio n. 1
0
 //
 //	ノードの再帰的処理2
 //
 private void NodeProcess2(XElement ere, int level)
 {
     //IEnumerable<XNode> nodes = nod.Nodes();
     //List<XNode> nod2 = nodes.ToList();
     if (ere.HasAttributes == true)
     {
         List <XAttribute> latr = ere.Attributes().ToList();
         foreach (XAttribute atr in latr)
         {
             XName nam = atr.Name;
             if ((nam.ToString() == "struct-name") && (atr.Value == "db.UserDatatype"))
             {
                 if (ere.HasElements)
                 {
                     GetUserDatatype(ere.Elements().ToList());
                 }
             }
             else if ((nam.ToString() == "struct-name") && (atr.Value == "db.mysql.Table"))
             {
                 XName      id2    = XName.Get("id");
                 XAttribute atr2   = ere.Attribute(id2);
                 string     linkid = "";
                 if (atr2 != null)
                 {
                     linkid = atr2.Value;
                 }
                 if (ere.HasElements)
                 {
                     GetTable(ere.Elements().ToList(), linkid);
                 }
             }
             else if ((nam.ToString() == "struct-name") && (atr.Value == "db.mysql.Column"))
             {
                 _table = Tables[Tables.Count - 1];
                 XName      id2    = XName.Get("id");
                 XAttribute atr2   = ere.Attribute(id2);
                 string     linkid = "";
                 if (atr2 != null)
                 {
                     linkid = atr2.Value;
                 }
                 if (ere.HasElements)
                 {
                     GetColumn(ere.Elements().ToList(), linkid);
                 }
             }
             else if ((nam.ToString() == "struct-name") && (atr.Value == "db.mysql.Routine"))
             {
                 MyRoutine rot = new MyRoutine();
                 Routines.Add(rot);
                 rot.id = Routines.Count;
                 IEnumerable <XElement> els = ere.Elements();
                 if (els != null)
                 {
                     foreach (XElement ere3 in els)
                     {
                         if (ere3.HasAttributes)
                         {
                             XAttribute atr2 = ere3.Attribute(XName.Get("key"));
                             string     val  = "";
                             if (atr2 != null)
                             {
                                 val = atr2.Value;
                             }
                             if (val == "name")
                             {
                                 rot.name = ere3.Value;
                             }
                             else if (val == "sqlBody")
                             {
                                 rot.sqlBody = ere3.Value;
                             }
                         }
                     }
                 }
             }
             else if ((nam.ToString() == "struct-name") && (atr.Value == "db.mysql.RoutineParam"))
             {
                 MyRoutineParam prm = new MyRoutineParam();
                 MyRoutine      rot = Routines[Routines.Count - 1];
                 if (rot != null)
                 {
                     rot.Params.Add(prm);
                     prm.id = rot.Params.Count;
                     IEnumerable <XElement> els = ere.Elements();
                     if (els != null)
                     {
                         foreach (XElement ele4 in els)
                         {
                             if (ele4.HasAttributes)
                             {
                                 XAttribute atr3 = ele4.Attribute(XName.Get("key"));
                                 string     val  = "";
                                 if (atr3 != null)
                                 {
                                     val = atr3.Value;
                                 }
                                 if (val == "name")
                                 {
                                     prm.name = ele4.Value;
                                 }
                                 else if (val == "datatype")
                                 {
                                     prm.datatype = ele4.Value;
                                 }
                                 else if (val == "paramType")
                                 {
                                     prm.paramType = ele4.Value;
                                 }
                             }
                         }
                     }
                 }
             }
             else if ((ere.Name == "value") && (nam.ToString() == "struct-name") && (atr.Value == "db.mysql.Index"))
             {
                 MyIndex idx = new MyIndex();
                 Indexes.Add(idx);
                 idx.id = Indexes.Count;
                 IEnumerable <XElement> els = ere.Elements();
                 if (els != null)
                 {
                     foreach (XElement ele4 in els)
                     {
                         if (ele4.HasAttributes)
                         {
                             XAttribute atr3 = ele4.Attribute(XName.Get("key"));
                             string     val  = "";
                             if (atr3 != null)
                             {
                                 val = atr3.Value;
                             }
                             if (val == "name")
                             {
                                 idx.name = ele4.Value;
                             }
                             else if (val == "indexKind")
                             {
                                 idx.indexKind = ele4.Value;
                             }
                             else if (val == "keyBlockSize")
                             {
                                 idx.keyBlockSize = int.Parse(ele4.Value);
                             }
                             else if (val == "withParser")
                             {
                                 idx.withParser = ele4.Value;
                             }
                             else if (val == "deferability")
                             {
                                 idx.deferability = int.Parse(ele4.Value);
                             }
                             else if (val == "comment")
                             {
                                 idx.comment = ele4.Value;
                             }
                             else if (val == "indexType")
                             {
                                 idx.indexType = ele4.Value;
                             }
                             else if (val == "isPrimary")
                             {
                                 idx.isPrimary = int.Parse(ele4.Value);
                             }
                             else if (val == "unique")
                             {
                                 idx.unique = int.Parse(ele4.Value);
                             }
                             else if (val == "oldName")
                             {
                                 idx.oldName = ele4.Value;
                             }
                             else if (val == "owner")
                             {
                                 idx.owner = ele4.Value;
                             }
                         }
                     }
                 }
             }
             else if ((ere.Name == "value") && (nam.ToString() == "struct-name") && (atr.Value == "db.mysql.IndexColumn"))
             {
                 MyIndexColumn col = new MyIndexColumn();
                 MyIndex       idx = Indexes[Indexes.Count - 1];
                 if (idx != null)
                 {
                     idx.IndexColumn.Add(col);
                     IEnumerable <XElement> els = ere.Elements();
                     if (els != null)
                     {
                         foreach (XElement ele4 in els)
                         {
                             if (ele4.HasAttributes)
                             {
                                 XAttribute atr3 = ele4.Attribute(XName.Get("key"));
                                 string     val  = "";
                                 if (atr3 != null)
                                 {
                                     val = atr3.Value;
                                 }
                                 if (val == "name")
                                 {
                                     col.name = ele4.Value;
                                 }
                                 else if (val == "columnLength")
                                 {
                                     col.columnLength = int.Parse(ele4.Value);
                                 }
                                 else if (val == "comment")
                                 {
                                     col.comment = ele4.Value;
                                 }
                                 else if (val == "descend")
                                 {
                                     col.descend = int.Parse(ele4.Value);
                                 }
                                 else if (val == "referencedColumn")
                                 {
                                     col.referencedColumn = ele4.Value;
                                 }
                                 else if (val == "owner")
                                 {
                                     col.owner = ele4.Value;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (ere.HasElements == true)
     {
         List <XElement> chd = ere.Elements().ToList();
         foreach (XElement ere1 in chd)
         {
             NodeProcess2(ere1, level + 1);
         }
     }
 }
Esempio n. 2
0
        //
        //	Entity Code Gen
        //
        public string GetSourceCode()
        {
            tit = Filename;
            tit = tit.Substring(tit.LastIndexOf(Resources.DirSp) + 1, tit.LastIndexOf(".") - tit.LastIndexOf("/") - 1);
            string ret = Resources.Header1;

            ret = ret + Resources.ComSp + tab + tit + ".cs" + nl + nl;
            ret = ret + "NameSpace " + Name + Resources.SB + nl
                  + tab + "[DbConfigurationType(typeof(MySqlEFConfiguration))]" + nl
                  + tab + "public partial class " + tit + "DB : DbContext {" + nl
                  + tab + tab + "public " + tit + "DB() : base(" + bd + "name=" + tit + "ConnectionS" + bd + ") {" + nl
                  + tab + tab + tab + _init + ";" + nl
                  + tab + tab + "}" + nl
                  + tab + tab + "public " + tit + "DB(string ConnectionString) : base( ConnectionString ){ " + nl
                  + tab + tab + tab + _init + ";" + nl
                  + tab + tab + "}" + nl
                  + tab + tab + "public " + tit + "DB(MySqlConnection Connection) : base(Connection.ConnectionString) {" + nl
                  + tab + tab + tab + _init + ";" + nl
                  + tab + tab + "}" + nl
                  + tab + tab + "private void " + _init + " {" + nl;
            if (Routines.Count > 0)
            {
                ret = ret + tab + tab + tab + tit + "Connection = new MySqlConnection();" + nl;
            }
            foreach (MyRoutine rtn in Routines)
            {
                ret = ret + tab + tab + tab + rtn.name + " = new MySqlCommand();" + nl;
                ret = ret + tab + tab + tab + rtn.name + ".Connection = (MySqlConnection)"
                      + "Database.Connection;" + nl;
                ret = ret + tab + tab + tab + rtn.name + ".CommandText = \"CALL " + rtn.name;
                if (rtn.Params.Count > 0)
                {
                    ret = ret + " (\"" + nl;
                }
                else
                {
                    ret = ret + ";\";" + nl;
                }
                int wk1 = 0;
                foreach (MyRoutineParam prm in rtn.Params)
                {
                    wk1++;
                    ret = ret + tab + tab + tab + tab + "+ \"`" + prm.name + "`";
                    if (wk1 != rtn.Params.Count)
                    {
                        ret = ret + ",\"" + nl;
                    }
                    else
                    {
                        ret = ret + "\"" + nl + tab + tab + tab + tab + "+ \");\";" + nl;
                    }
                }
                foreach (MyRoutineParam prm in rtn.Params)
                {
                    ret = ret + tab + tab + tab + rtn.name + ".Parameters.Add(new MySqlParameter(\""
                          + prm.name + "\",";
                    int len = prm.datatype.LastIndexOf("(");
                    if (len < 1)
                    {
                        len = prm.datatype.Length;
                    }
                    //len--;
                    string type = prm.datatype.Substring(0, len);
                    type = type.ToLower();
                    type = paramtype(type);
                    ret  = ret + type + "));" + nl;
                }
            }
            ret = ret + tab + tab + Resources.EB + nl;
            foreach (MyTable tbl in Tables)
            {
                ret = ret + tab + tab + "public DbSet<" + tbl.name + "> " + tbl.name + "s " + prop + nl;
            }
            ret = ret + nl;
            if (Routines.Count > 0)
            {
                ret = ret + tab + tab + "private MySqlConnection " + tit + "Connection;" + nl;
            }
            foreach (MyRoutine rtn_name in Routines)
            {
                ret = ret + tab + tab + "public MySqlCommand " + rtn_name.name + ";" + nl;
            }
            ret = ret + nl + tab + "}" + nl + nl;
            foreach (MyTable tbl in Tables)
            {
                ret = ret + tab + "public partial class " + tbl.name + " {" + nl;
                MyIndexColumn icx = null;
                MyIndex       ixx = null;
                foreach (MyColumn col in tbl.Columns)
                {
                    icx = null;
                    ixx = null;
                    foreach (MyIndex idx in Indexes)
                    {
                        foreach (MyIndexColumn icl in idx.IndexColumn)
                        {
                            if (col.linkid == icl.referencedColumn)
                            {
                                icx = icl;
                                break;
                            }
                        }
                        if (icx != null)
                        {
                            ixx = idx;
                            break;
                        }
                    }
                    if ((icx != null) && (ixx != null))
                    {
                        if (ixx.isPrimary > 0)
                        {
                            if (col.autoIncrement > 0)
                            {
                                ret = ret + tab + tab + "[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]" + nl;
                            }
                            else
                            {
                                ret = ret + tab + tab + "[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]" + nl;
                            }
                        }
                    }
                    MyUserDatatype typ  = Usertype.Find(x => x.actualType == col.SimpleDatatype);
                    string         styp = "";
                    if (typ != null)
                    {
                        styp = cstype(typ);
                    }
                    if (styp.Length < 1)
                    {
                        styp = col.SimpleDatatype.Substring(col.SimpleDatatype.LastIndexOf(".") + 1, col.SimpleDatatype.Length - col.SimpleDatatype.LastIndexOf(".") - 1);
                        styp = cstype(styp);
                    }
                    if (styp != "string")
                    {
                        if (col.isNotNull == 0)
                        {
                            ret = ret + tab + tab + "public Nullable<" + styp + "> " + col.name + prop + nl;
                        }
                        else
                        {
                            ret = ret + tab + tab + "public " + styp + " " + col.name + prop + nl;
                        }
                    }
                    else
                    {
                        ret = ret + tab + tab + "public " + styp + " " + col.name + prop + nl;
                    }
                }
                ret = ret + tab + Resources.EB + nl;
            }
            ret = ret + tab + "public partial class " + tit + "CreateDatabaseIfNotExists : CreateDatabaseIfNotExists<" + tit + "> {" + nl
                  + tab + tab + "public " + tit + "CreateDatabaseIfNotExists() :base() {" + nl
                  + tab + tab + Resources.EB + nl
                  + tab + tab + "protected override void Seed(" + tit + " context) {" + nl
                  + tab + tab + tab + "base.Seed(context);" + nl
                  + tab + tab + Resources.EB + nl
                  + tab + Resources.EB + nl
                  + Resources.EB + nl;
            string savename = Filename;

            savename = savename.Substring(0, savename.LastIndexOf(Resources.DirSp));
            savename = savename + Resources.DirSp + tit + ".cs";
            StreamWriter wrt = File.CreateText(savename);

            wrt.Write(ret);
            wrt.Close();
            savename = savename.Substring(0, savename.LastIndexOf(Resources.DirSp));
            savename = savename + Resources.DirSp + "RreadmeAppconfig.txt";
            wrt      = File.CreateText(savename);
            wrt.Write(Resources.Config1);
            wrt.Write("\"" + tit + "ConnectionS\" ");
            wrt.Write(Resources.Config2);
            MySql.Data.MySqlClient.MySqlConnectionStringBuilder bld = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder();
            bld.Server   = Server;
            bld.UserID   = UserID;
            bld.Password = Password;
            bld.Database = Database;
            wrt.Write("\"" + bld.ConnectionString + "\" ");
            wrt.Write(Resources.Config3);
            wrt.Close();
            return(ret);
        }