示例#1
0
文件: Mwb.cs 项目: lovefc3s/M2E4Mono
        private void GetUserDatatype(List <XElement> chd)
        {
            MyUserDatatype typ = new MyUserDatatype();

            foreach (var ere in chd)
            {
                if (ere.HasAttributes)
                {
                    XAttribute atr = ere.Attribute((XName)"key");
                    switch (atr.Value)
                    {
                    case "actualType": typ.actualType = ere.Value; break;

                    case "flags": typ.flags = ere.Value; break;

                    case "sqlDefinition": typ.sqlDefinition = ere.Value; break;

                    case "name": typ.name = ere.Value; break;

                    default: break;
                    }
                }
            }
            Usertype.Add(typ);
        }
示例#2
0
文件: Mwb.cs 项目: lovefc3s/M2E4Mono
        public string cstype(MyUserDatatype type)
        {
            string ret = "int";

            if (type == null)
            {
                return(ret);
            }
            switch (type.name)
            {
            case "NUMERIC": ret = "decimal"; break;

            case "DECIMAL": ret = "decimal"; break;

            case "INT1": ret = "bool"; break;

            case "INT2": ret = "short"; break;

            case "MIDDLEINT": break;

            case "INTEGER": break;

            case "INT3": break;

            case "INT4": break;

            case "INT8": ret = "long"; break;

            case "FLOAT4": ret = "float"; break;

            case "FLOAT8": ret = "double"; break;

            case "LONG": ret = "string"; break;

            case "LONG VARCHAR": ret = "string"; break;

            case "VARCHAR": ret = "string"; break;

            case "TEXT": ret = "string"; break;

            case "CHARACTER": ret = "byte"; break;

            default: ret = ""; break;
            }
            return(ret);
        }
示例#3
0
文件: Mwb.cs 项目: lovefc3s/M2E4Mono
        public Mwb()
        {
            _table   = null;
            _column  = null;
            tab      = Resources.Tab;
            nl       = Resources.Nl;
            Columns  = new List <MyColumn>();
            Tables   = new List <MyTable>();
            Usertype = new List <MyUserDatatype>();
            MyUserDatatype typ = new MyUserDatatype();

            Usertype.Add(typ);
            typ.id            = 1;
            typ.name          = "VARCHAR";
            typ.actualType    = "com.mysql.rdbms.mysql.datatype.varchar";
            typ.flags         = "";
            typ.sqlDefinition = "verchar";
            Routines          = new List <MyRoutine>();
            Indexes           = new List <MyIndex>();
            tit      = "";
            Database = "";
        }
示例#4
0
文件: Mwb.cs 项目: lovefc3s/M2E4Mono
        //
        //	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);
        }