Example #1
0
        public static void Upgrade(EntityDB.DBContext dbContext, string designData)
        {
            if (designData.IsNullOrEmpty())
            {
                return;
            }

            byte[] bs;
            if (designData.StartsWith("\r\n"))
            {
                bs = System.Convert.FromBase64String(designData.Substring(2));
                bs = UnGzip(bs);
            }
            else
            {
                bs = System.Convert.FromBase64String(designData);
            }
            using (var dset = Newtonsoft.Json.JsonConvert.DeserializeObject <WayDataSet>(System.Text.Encoding.UTF8.GetString(bs)))
            {
                if (dbContext == null)
                {
                    return;
                }

                bs = null;
                EntityDB.IDatabaseService db     = dbContext.Database;
                EntityDB.DatabaseType     dbType = dbContext.DatabaseType;

                IDatabaseDesignService dbservice = EntityDB.Design.DBHelper.CreateDatabaseDesignService(dbType);
                dbservice.CreateEasyJobTable(db);

                var dbconfig = db.ExecSqlString("select contentConfig from __wayeasyjob").ToString().ToJsonObject <DataBaseConfig>();
                if (string.IsNullOrEmpty(dbconfig.DatabaseGuid) == false && dbconfig.DatabaseGuid != dset.DataSetName)
                {
                    throw new Exception("此结构脚本并不是对应此数据库");
                }


                var dtable       = dset.Tables[0];
                int currentRowId = 0;
                try
                {
                    var query = dtable.Rows.Where(m => (long)m["id"] > dbconfig.LastUpdatedID).OrderBy(m => (long)m["id"]).ToList();

                    if (query.Count > 0)
                    {
                        var allquery = dtable.Rows.OrderBy(m => (long)m["id"]).ToList();
                        var assembly = typeof(Way.EntityDB.Design.Actions.CreateTableAction).GetTypeInfo().Assembly;
                        //因为通过cs导入结构后,column的id发生变化,所以需要重新统一一下column的id
                        for (int i = 0; i < allquery.Count; i++)
                        {
                            var datarow = allquery[i];
                            currentRowId = Convert.ToInt32(datarow["id"]);
                            var actionItem = dataRowToAction(assembly, datarow);

                            if (actionItem is EntityDB.Design.Actions.CreateTableAction createAction)
                            {
                                ResetColumnId(allquery, createAction.Table.Name, i);
                            }
                        }

                        int?lastid = Convert.ToInt32(query.Last()["id"]);
                        db.DBContext.BeginTransaction();
                        for (int i = 0; i < query.Count; i++)
                        {
                            var datarow = query[i];
                            currentRowId = Convert.ToInt32(datarow["id"]);
                            var actionItem = dataRowToAction(assembly, datarow);

                            if (actionItem is EntityDB.Design.Actions.ChangeTableAction)
                            {
                                var changeAction = (EntityDB.Design.Actions.ChangeTableAction)actionItem;

                                changeAction._getColumnsFunc = () => {
                                    return(FindAllColumnsForChangeTable(assembly, changeAction, currentRowId, dtable.Rows).ToList());
                                };
                            }
                            else if (actionItem is EntityDB.Design.Actions.CreateTableAction createAction)
                            {
                                //往下查找表的变更
                                var ret = FindCreateTableActionAllColumns(assembly, createAction, query, i + 1);

                                if (ret == false)
                                {
                                    //表后来删除了
                                    query.RemoveAt(i);
                                    i--;
                                    continue;
                                }
                            }

                            actionItem.Invoke(db);
                        }

                        SetLastUpdateID(lastid.Value, dset.DataSetName, db);
                        db.DBContext.CommitTransaction();
                    }
                }
                catch (Exception ex)
                {
                    db.DBContext.RollbackTransaction();
                    throw new Exception("发生错误,最后执行的id:" + currentRowId, ex);
                }
            }
        }
Example #2
0
        public static void Upgrade(EntityDB.DBContext dbContext, string designData)
        {
            if (designData.IsNullOrEmpty())
            {
                return;
            }

            byte[] bs;
            if (designData.StartsWith("\r\n"))
            {
                bs = System.Convert.FromBase64String(designData.Substring(2));
                bs = UnGzip(bs);
            }
            else
            {
                bs = System.Convert.FromBase64String(designData);
            }
            using (var dset = Newtonsoft.Json.JsonConvert.DeserializeObject <WayDataSet>(System.Text.Encoding.UTF8.GetString(bs)))
            {
                if (dbContext == null)
                {
                    return;
                }

                bs = null;
                EntityDB.IDatabaseService db     = dbContext.Database;
                EntityDB.DatabaseType     dbType = dbContext.DatabaseType;

                IDatabaseDesignService dbservice = EntityDB.Design.DBHelper.CreateDatabaseDesignService(dbType);
                dbservice.CreateEasyJobTable(db);

                var dbconfig = db.ExecSqlString("select contentConfig from __wayeasyjob").ToString().ToJsonObject <DataBaseConfig>();
                if (string.IsNullOrEmpty(dbconfig.DatabaseGuid) == false && dbconfig.DatabaseGuid != dset.DataSetName)
                {
                    throw new Exception("此结构脚本并不是对应此数据库");
                }


                var dtable = dset.Tables[0];
                try
                {
                    var query = dtable.Rows.Where(m => (long)m["id"] > dbconfig.LastUpdatedID).OrderBy(m => (long)m["id"]).ToList();

                    if (query.Count > 0)
                    {
                        int?lastid   = Convert.ToInt32(query.Last()["id"]);
                        var assembly = typeof(Way.EntityDB.Design.Actions.CreateTableAction).GetTypeInfo().Assembly;
                        db.DBContext.BeginTransaction();
                        foreach (var datarow in query)
                        {
                            var rowid      = Convert.ToInt32(datarow["id"]);
                            var actionItem = dataRowToAction(assembly, datarow);

                            if (actionItem is EntityDB.Design.Actions.ChangeTableAction)
                            {
                                var changeAction = (EntityDB.Design.Actions.ChangeTableAction)actionItem;
                                changeAction._getColumnsFunc = () => {
                                    List <EJ.DBColumn> allcolumns = new List <EJ.DBColumn>();

                                    //往上逆推,查找字段信息
                                    var        datarows         = dtable.Rows.Where(m => (long)m["id"] < rowid).OrderByDescending(m => (long)m["id"]).ToList();
                                    var        curTableName     = changeAction.OldTableName;
                                    List <int> deletedColumnids = new List <int>();

                                    foreach (var preRow in datarows)
                                    {
                                        var preAction = dataRowToAction(assembly, preRow);
                                        if (preAction is EntityDB.Design.Actions.CreateTableAction)
                                        {
                                            var tableAction = preAction as EntityDB.Design.Actions.CreateTableAction;
                                            if (tableAction.Table.Name != curTableName)
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                foreach (var c in tableAction.Columns)
                                                {
                                                    if (allcolumns.Any(m => m.id == c.id) == false)
                                                    {
                                                        allcolumns.Add(c);
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        else if (preAction is EntityDB.Design.Actions.ChangeTableAction)
                                        {
                                            var preChangeAction = preAction as EntityDB.Design.Actions.ChangeTableAction;
                                            if (preChangeAction.NewTableName != curTableName)
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                curTableName = preChangeAction.OldTableName;
                                                foreach (var c in preChangeAction.newColumns)
                                                {
                                                    if (deletedColumnids.Contains(c.id.Value) == false && allcolumns.Any(m => m.id == c.id) == false)
                                                    {
                                                        allcolumns.Add(c);
                                                    }
                                                }
                                                foreach (var c in preChangeAction.changedColumns)
                                                {
                                                    if (deletedColumnids.Contains(c.id.Value) == false && allcolumns.Any(m => m.id == c.id) == false)
                                                    {
                                                        allcolumns.Add(c);
                                                    }
                                                }
                                                foreach (var c in preChangeAction.deletedColumns)
                                                {
                                                    deletedColumnids.Add(c.id.Value);
                                                }
                                            }
                                        }
                                    }
                                    return(allcolumns);
                                };
                            }

                            actionItem.Invoke(db);
                        }

                        SetLastUpdateID(lastid.Value, dset.DataSetName, db);
                        db.DBContext.CommitTransaction();
                    }
                }
                catch
                {
                    db.DBContext.RollbackTransaction();
                    throw;
                }
            }
        }
Example #3
0
        public static void Upgrade(EntityDB.DBContext dbContext, string designData)
        {
            if (designData.IsNullOrEmpty())
            {
                return;
            }

            byte[] bs;
            if (designData.StartsWith("\r\n"))
            {
                bs = System.Convert.FromBase64String(designData.Substring(2));
                bs = UnGzip(bs);
            }
            else
            {
                bs = System.Convert.FromBase64String(designData);
            }
            using (var dset = Newtonsoft.Json.JsonConvert.DeserializeObject <WayDataSet>(System.Text.Encoding.UTF8.GetString(bs)))
            {
                if (dbContext == null)
                {
                    return;
                }

                bs = null;
                EntityDB.IDatabaseService db     = dbContext.Database;
                EntityDB.DatabaseType     dbType = dbContext.DatabaseType;

                IDatabaseDesignService dbservice = EntityDB.Design.DBHelper.CreateDatabaseDesignService(dbType);
                dbservice.CreateEasyJobTable(db);

                var dbconfig = db.ExecSqlString("select contentConfig from __wayeasyjob").ToString().ToJsonObject <DataBaseConfig>();
                if (string.IsNullOrEmpty(dbconfig.DatabaseGuid) == false && dbconfig.DatabaseGuid != dset.DataSetName)
                {
                    throw new Exception("此结构脚本并不是对应此数据库");
                }


                var dtable = dset.Tables[0];
                try
                {
                    var query = dtable.Rows.Where(m => (long)m["id"] > dbconfig.LastUpdatedID).OrderBy(m => (long)m["id"]).ToList();

                    if (query.Count > 0)
                    {
                        int?lastid   = Convert.ToInt32(query.Last()["id"]);
                        var assembly = typeof(Way.EntityDB.Design.Actions.CreateTableAction).GetTypeInfo().Assembly;
                        db.DBContext.BeginTransaction();
                        foreach (var datarow in query)
                        {
                            string actionType = datarow["type"].ToString();

                            string json = datarow["content"].ToString();

                            Type type       = assembly.GetType($"Way.EntityDB.Design.Actions.{actionType}");
                            var  actionItem = (EntityDB.Design.Actions.Action)Newtonsoft.Json.JsonConvert.DeserializeObject(json, type);

                            actionItem.Invoke(db);
                        }

                        SetLastUpdateID(lastid.Value, dset.DataSetName, db);
                        db.DBContext.CommitTransaction();
                    }
                }
                catch
                {
                    db.DBContext.RollbackTransaction();
                    throw;
                }
            }
        }