Beispiel #1
0
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.Contains(".aspx") && connectInfo.Request.Query["id"] != null)
            {
                int    id = Convert.ToInt32(connectInfo.Request.Query["id"]);
                string accept_encoding = connectInfo.Request.Headers["Accept-Encoding"];
                if (!string.IsNullOrEmpty(accept_encoding) && accept_encoding.Contains("gzip"))
                {
                    handled = true;

                    connectInfo.Response.Headers["Content-Encoding"] = "gzip";
                    connectInfo.Response.Headers["Content-Type"]     = "image/png";
                    byte[] bs = null;
                    using (var bitmap = MainController.GetBitmapById(id))
                    {
                        if (bitmap != null)
                        {
                            using (var ms = new System.IO.MemoryStream())
                            {
                                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                bs = ms.ToArray();
                            }
                        }
                    }

                    bs = Helper.GZip(bs);
                    connectInfo.Response.Headers["Content-Length"] = bs.Length.ToString();
                    connectInfo.Response.Write(bs);
                }
            }
        }
Beispiel #2
0
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.Contains("/ImportCSFileHandler.aspx") == false)
            {
                return;
            }
            handled = true;

            if (connectInfo.Session["user"] == null || ((EJ.User)connectInfo.Session["user"]).Role.GetValueOrDefault().HasFlag(EJ.User_RoleEnum.数据库设计师) == false)
            {
                return;
            }
            try
            {
                int projectid = Convert.ToInt32(connectInfo.Request.Query["projectid"]);

                byte[] bs = new byte[connectInfo.Request.ContentLength];
                connectInfo.Request.Body.Read(bs, 0, bs.Length);
                ImportDesign(projectid, bs, connectInfo);

                connectInfo.Response.Write("ok\r\n");
            }
            catch (Exception ex)
            {
                connectInfo.Response.Write($"{ex.Message}\r\n");
            }
        }
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.EndsWith("Jack.JSUnitTest"))
            {
                handled = true;
                string id = connectInfo.Request.Query["id"];
                byte[] content;
                HtmlContents.TryRemove(id, out content);

                connectInfo.Response.ContentLength = content.Length;
                connectInfo.Response.Write(content);
            }
        }
Beispiel #4
0
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.Contains("/ImportTableData.aspx") == false)
            {
                return;
            }
            handled = true;

            if (connectInfo.Session["user"] == null || ((EJ.User)connectInfo.Session["user"]).Role.GetValueOrDefault().HasFlag(EJ.User_RoleEnum.数据库设计师) == false)
            {
                return;
            }
            try
            {
                int databaseid = Convert.ToInt32(connectInfo.Request.Query["dbid"]);
                System.IO.BinaryReader reader = new System.IO.BinaryReader(connectInfo.Request.Body);
                var tableNames = reader.ReadString().ToJsonObject <string[]>();

                using (EJDB ejdb = new EJDB())
                {
                    var database   = ejdb.Databases.FirstOrDefault(m => m.id == databaseid);
                    var invokingDB = DBHelper.CreateInvokeDatabase(database);
                    { //不要开事物,大数据太慢
                        try
                        {
                            if (connectInfo.Request.Query["clearDataFirst"] == "1")
                            {
                                foreach (var tableName in tableNames)
                                {
                                    invokingDB.AllowIdentityInsert(tableName, true);
                                    invokingDB.ExecSqlString("delete from [" + tableName + "]");
                                }
                            }

                            Dictionary <string, string> pkNames = new Dictionary <string, string>();
                            for (int i = 0; i < tableNames.Length; i++)
                            {
                                var tableName = tableNames[i];
                                var db_table  = ejdb.DBTable.FirstOrDefault(m => m.Name == tableName);
                                if (db_table == null)
                                {
                                    throw new Exception(string.Format("找不到{0}数据表定义", tableName));
                                }
                                var  pkcolumn      = ejdb.DBColumn.FirstOrDefault(m => m.TableID == db_table.id && m.IsPKID == true);
                                bool hasAutoColumn = ejdb.DBColumn.Count(m => m.TableID == db_table.id && m.IsAutoIncrement == true) > 0;
                                pkNames[tableName] = pkcolumn.Name;

                                if (pkcolumn == null)
                                {
                                    throw (new Exception(string.Format("{0}-{1}没有设置主键", db_table.caption, db_table.Name)));
                                }
                            }

                            while (true)
                            {
                                string _tablename = reader.ReadString();
                                if (_tablename == ":end")
                                {
                                    break;
                                }
                                string pkname = null;
                                if (pkNames.ContainsKey(_tablename))
                                {
                                    pkname = pkNames[_tablename];
                                }

                                Dictionary <string, object> data = reader.ReadString().ToJsonObject <Dictionary <string, object> >();
                                CustomDataItem newDataItem;

                                if (pkname != null && data.ContainsKey(pkname))
                                {
                                    if (Convert.ToInt32(invokingDB.ExecSqlString($"select count(*) from {invokingDB.FormatObjectName(_tablename)} where {invokingDB.FormatObjectName(pkname)}=@p0", data[pkname])) > 0)
                                    {
                                        newDataItem = new CustomDataItem(_tablename, pkname, data[pkname]);
                                        foreach (var item in data)
                                        {
                                            newDataItem.SetValue(item.Key, item.Value);
                                        }

                                        invokingDB.Update(newDataItem);
                                        continue;
                                    }
                                }

                                newDataItem = new CustomDataItem(_tablename, pkname, null);
                                foreach (var item in data)
                                {
                                    newDataItem.SetValue(item.Key, item.Value);
                                }
                                invokingDB.Insert(newDataItem);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            foreach (var tableName in tableNames)
                            {
                                invokingDB.AllowIdentityInsert(tableName, false);
                            }
                        }
                    }
                }
                connectInfo.Response.Write("ok\r\n");
            }
            catch (Exception ex)
            {
                connectInfo.Response.Write($"{ex.Message}\r\n");
            }
        }
Beispiel #5
0
        public bool ImportDesign(int projectid, byte[] csFileContent, HttpConnectInformation connectInfo)
        {
            using (MemoryStream ms = new MemoryStream(csFileContent))
                using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
                {
                    StringBuilder content       = new StringBuilder();
                    bool          bufferStarted = false;
                    while (true)
                    {
                        var line = sr.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        if (line.StartsWith("<design>*/"))
                        {
                            break;
                        }
                        if (bufferStarted)
                        {
                            content.Append(line);
                        }
                        if (line.StartsWith("/*<design>"))
                        {
                            bufferStarted = true;
                        }
                    }
                    //result.Append(\"\\r\\n\");

                    StringBuilder actioncontent = new StringBuilder();
                    bufferStarted = false;
                    ms.Position   = 0;
                    while (true)
                    {
                        var line = sr.ReadLine().Trim();
                        if (line == null)
                        {
                            break;
                        }
                        if (line.StartsWith("return result.ToString()"))
                        {
                            break;
                        }
                        if (bufferStarted)
                        {
                            var index = line.IndexOf("result.Append(");
                            line = line.Substring(index + "result.Append(".Length + 1);
                            line = line.Substring(0, line.LastIndexOf("\""));
                            actioncontent.Append(line);
                        }
                        if (line.StartsWith("result.Append(\"\\r\\n\");"))
                        {
                            bufferStarted = true;
                        }
                    }

                    var bs = Convert.FromBase64String(content.ToString());
                    bs = CodeBuilder.UnGzip(bs);
                    var designData = Encoding.UTF8.GetString(bs).FromJson <DesignData>();

                    bs = Convert.FromBase64String(actioncontent.ToString());
                    bs = CodeBuilder.UnGzip(bs);
                    var dset = Encoding.UTF8.GetString(bs).FromJson <WayDataSet>();

                    int oldDatabaseid;
                    using (EJDB_Check db = new EJDB_Check())
                    {
                        if (db.Databases.Any(m => m.Name == designData.Database.Name))
                        {
                            throw new Exception("数据库名称有重复");
                        }

                        try
                        {
                            db.BeginTransaction();

                            oldDatabaseid = designData.Database.id.Value;
                            designData.Database.ProjectID = projectid;
                            designData.Database.conStr    = "";
                            db.Insert(designData.Database);



                            #region Modules
                            foreach (var module in designData.Modules)
                            {
                                module.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = module.id.Value
                                });
                                module.DatabaseID = designData.Database.id;
                                db.Insert(module);
                            }

                            foreach (var module in designData.Modules)
                            {
                                if (module.parentID != null && module.parentID != 0)
                                {
                                    var parentModule = designData.Modules.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == module.parentID.Value);
                                    if (parentModule != null)
                                    {
                                        module.parentID = parentModule.id;
                                        db.Update(module);
                                    }
                                }
                            }
                            #endregion

                            #region Tables
                            foreach (var table in designData.Tables)
                            {
                                table.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = table.id.Value
                                });
                                table.DatabaseID = designData.Database.id;
                                db.Insert(table);
                            }
                            #endregion

                            #region Columns
                            foreach (var column in designData.DBColumns)
                            {
                                column.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = column.id.Value
                                });
                                column.TableID = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == column.TableID.Value).id;
                                db.Insert(column);
                            }
                            #endregion

                            #region IDXIndexes
                            foreach (var idxindex in designData.IDXIndexes)
                            {
                                idxindex.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = idxindex.id.Value
                                });
                                idxindex.TableID = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == idxindex.TableID.Value).id;
                                db.Insert(idxindex);
                            }
                            #endregion

                            #region classproperties
                            foreach (var classpro in designData.classproperties)
                            {
                                classpro.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = classpro.id.Value
                                });
                                if (classpro.tableid != null)
                                {
                                    classpro.tableid = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == classpro.tableid.Value).id;
                                }
                                if (classpro.foreignkey_tableid != null)
                                {
                                    classpro.foreignkey_tableid = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == classpro.foreignkey_tableid.Value).id;
                                }
                                db.Insert(classpro);
                            }
                            #endregion

                            #region DBDeleteConfigs
                            foreach (var delconfig in designData.DBDeleteConfigs)
                            {
                                delconfig.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = delconfig.id.Value
                                });
                                delconfig.TableID     = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == delconfig.TableID.Value).id;
                                delconfig.RelaColumID = designData.DBColumns.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == delconfig.RelaColumID.Value).id;
                                delconfig.RelaTableID = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == delconfig.RelaTableID.Value).id;
                                db.Insert(delconfig);
                            }
                            #endregion

                            #region TableInModules
                            foreach (var tableInModule in designData.TableInModules)
                            {
                                tableInModule.BackupChangedProperties.Add("id", new DataValueChangedItem()
                                {
                                    OriginalValue = tableInModule.id.Value
                                });
                                tableInModule.TableID  = designData.Tables.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == tableInModule.TableID.Value).id;
                                tableInModule.ModuleID = designData.Modules.FirstOrDefault(m => (int)m.BackupChangedProperties["id"].OriginalValue == tableInModule.ModuleID.Value).id;
                                db.Insert(tableInModule);
                            }
                            #endregion


                            var assembly = typeof(Way.EntityDB.Design.Actions.CreateTableAction).GetTypeInfo().Assembly;
                            var dtable   = dset.Tables[0];
                            var rows     = dtable.Rows.OrderBy(m => (long)m["id"]).ToList();
                            foreach (var datarow in rows)
                            {
                                EJ.DesignHistory action = new EJ.DesignHistory()
                                {
                                    ActionId   = Convert.ToInt32(datarow["id"]),
                                    Content    = datarow["content"].ToString(),
                                    DatabaseId = designData.Database.id,
                                    Type       = datarow["type"].ToString()
                                };
                                db.Insert(action);
                            }


                            db.Insert(new EJ.SysLog()
                            {
                                UserId     = ((EJ.User)connectInfo.Session["user"]).id,
                                Type       = "从cs导入数据库设计模型",
                                DatabaseId = designData.Database.id,
                                Time       = DateTime.Now,
                            });

                            //try
                            //{
                            //    //变更数据库类型
                            //    IDatabaseDesignService dbservice = Way.EntityDB.Design.DBHelper.CreateDatabaseDesignService((Way.EntityDB.DatabaseType)(int)designData.Database.dbType);
                            //    dbservice.Create(designData.Database);
                            //    db.Update(designData.Database);

                            //    //更新到现在的数据结构

                            //    var invokeDB = Way.EntityDB.Design.DBHelper.CreateInvokeDatabase(designData.Database);
                            //    var designDataStr = CodeBuilder.GetDesignData(db, designData.Database);
                            //    DBUpgrade.Upgrade(invokeDB.DBContext, "\r\n" + designDataStr);
                            //}
                            //catch
                            //{

                            //}

                            db.CommitTransaction();
                        }
                        catch (Exception)
                        {
                            db.RollbackTransaction();
                            throw;
                        }

                        return(true);
                    }
                }
        }
Beispiel #6
0
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.Contains("/DownloadTableData.aspx") == false)
            {
                return;
            }
            handled = true;

            if (connectInfo.Session["user"] == null || ((EJ.User)connectInfo.Session["user"]).Role.GetValueOrDefault().HasFlag(EJ.User_RoleEnum.数据库设计师) == false)
            {
                return;
            }

            var tableids = connectInfo.Request.Query["tableids"].ToJsonObject <int[]>();

            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(connectInfo.Response);
            using (EJDB db = new EJDB())
            {
                int tid        = tableids[0];
                var dbtable    = db.DBTable.FirstOrDefault(m => m.id == tid);
                var database   = db.Databases.FirstOrDefault(m => m.id == dbtable.DatabaseID);
                var invokingDB = DBHelper.CreateInvokeDatabase(database);
                {
                    string[] tableNames = new string[tableids.Length];
                    int[]    rowCounts  = new int[tableids.Length];
                    for (int i = 0; i < tableids.Length; i++)
                    {
                        int tableid = tableids[i];
                        dbtable       = db.DBTable.FirstOrDefault(m => m.id == tableid);
                        tableNames[i] = dbtable.Name;
                        string sql = $"select count(*) from {invokingDB.FormatObjectName(dbtable.Name)}";
                        rowCounts[i] = Convert.ToInt32(invokingDB.ExecSqlString(sql));
                    }
                    bw.Write(Newtonsoft.Json.JsonConvert.SerializeObject(tableNames));
                    bw.Write(Newtonsoft.Json.JsonConvert.SerializeObject(rowCounts));

                    for (int i = 0; i < tableids.Length; i++)
                    {
                        int tableid = tableids[i];
                        dbtable = db.DBTable.FirstOrDefault(m => m.id == tableid);
                        string sql = $"select * from {invokingDB.FormatObjectName(dbtable.Name)}";
                        invokingDB.ExecuteReader((reader) => {
                            Dictionary <string, object> data = new Dictionary <string, object>();
                            for (int j = 0; j < reader.FieldCount; j++)
                            {
                                string key   = reader.GetName(j);
                                object value = reader[j];
                                if (value != null && value != DBNull.Value)
                                {
                                    data[key] = value;
                                }
                            }
                            if (data.Count > 0)
                            {
                                bw.Write(dbtable.Name);
                                bw.Write(Newtonsoft.Json.JsonConvert.SerializeObject(data));
                            }
                            return(true);
                        }, sql);
                    }
                    bw.Write(":end");
                }
            }
        }
Beispiel #7
0
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.Contains("/DownloadDatabaseCode.aspx") == false)
            {
                return;
            }
            handled = true;
            try
            {
                if (connectInfo.Session["user"] == null)
                {
                    throw new Exception("not arrow");
                }

                int databaseid = Convert.ToInt32(connectInfo.Request.Query["databaseid"]);
                using (EJDB db = new EJDB())
                {
                    var database = db.Databases.FirstOrDefault(m => m.id == databaseid);
                    if (database.dllPath == null || database.dllPath.StartsWith("{") == false)
                    {
                        database.dllPath = Newtonsoft.Json.JsonConvert.SerializeObject(new
                        {
                            db     = connectInfo.Request.Query["filepath"],
                            simple = "",
                        });
                    }
                    else
                    {
                        var json = (Newtonsoft.Json.Linq.JToken)Newtonsoft.Json.JsonConvert.DeserializeObject(database.dllPath);
                        json["db"]       = connectInfo.Request.Query["filepath"];
                        database.dllPath = json.ToString();
                    }
                    db.Update(database);

                    var tables = db.DBTable.Where(m => m.DatabaseID == databaseid).ToList();
                    System.IO.BinaryWriter bw = new System.IO.BinaryWriter(connectInfo.Response);
                    bw.Write("start");

                    var invokingDB = Way.EntityDB.Design.DBHelper.CreateInvokeDatabase(database);
                    IDatabaseDesignService dbservice = Way.EntityDB.Design.DBHelper.CreateDatabaseDesignService((Way.EntityDB.DatabaseType)(int) database.dbType);


                    bw.Write(1);
                    ICodeBuilder codeBuilder = new CodeBuilder();

                    NamespaceCode namespaceCode  = new NamespaceCode(database.NameSpace);
                    NamespaceCode namespaceCode2 = new NamespaceCode(database.NameSpace + ".DB");
                    namespaceCode.AddUsing("System");
                    namespaceCode.AddUsing("Microsoft.EntityFrameworkCore");
                    namespaceCode.AddUsing("System.Collections.Generic");
                    namespaceCode.AddUsing("System.ComponentModel");
                    namespaceCode.AddUsing("System.Data");
                    namespaceCode.AddUsing("System.Linq");
                    namespaceCode.AddUsing("System.Text");
                    namespaceCode.AddUsing("System.ComponentModel.DataAnnotations");
                    namespaceCode.AddUsing("System.ComponentModel.DataAnnotations.Schema");
                    namespaceCode.AddUsing("Way.EntityDB.Attributes");
                    namespaceCode.AddUsing("System.Diagnostics.CodeAnalysis");
                    namespaceCode.AddBeforeCode("");
                    codeBuilder.BuilderDB(db, database, namespaceCode2, tables);

                    List <string> foreignKeys = new List <string>();
                    foreach (var table in tables)
                    {
                        codeBuilder.BuildTable(db, namespaceCode, table, foreignKeys);
                    }

                    bw.Write("code.cs");
                    string code = namespaceCode.Build() + "\r\n" + namespaceCode2.Build();
                    byte[] bs   = System.Text.Encoding.UTF8.GetBytes(code);
                    bw.Write(bs.Length);
                    bw.Write(bs);

                    bw.Write(":end");
                }
            }
            catch (Exception ex)
            {
                new System.IO.BinaryWriter(connectInfo.Response).Write(ex.Message);
            }
        }
        public void Handle(string originalUrl, HttpConnectInformation connectInfo, ref bool handled)
        {
            if (originalUrl.Contains("/DownLoadSimpleCodeHandler.aspx") == false)
            {
                return;
            }
            handled = true;
            try
            {
                if (connectInfo.Session["user"] == null)
                {
                    throw new Exception("not arrow");
                }

                int databaseid = Convert.ToInt32(connectInfo.Request.Query["databaseid"]);
                using (EJDB db = new EJDB())
                {
                    var database = db.Databases.FirstOrDefault(m => m.id == databaseid);
                    if (database.dllPath == null || database.dllPath.StartsWith("{") == false)
                    {
                        database.dllPath = Newtonsoft.Json.JsonConvert.SerializeObject(new {
                            simple = connectInfo.Request.Query["filepath"],
                            db     = "",
                        });
                    }
                    else
                    {
                        var json = (Newtonsoft.Json.Linq.JToken)Newtonsoft.Json.JsonConvert.DeserializeObject(database.dllPath);
                        json["simple"]   = connectInfo.Request.Query["filepath"];
                        database.dllPath = json.ToString();
                    }
                    db.Update(database);

                    var tables = db.DBTable.Where(m => m.DatabaseID == databaseid).ToList();
                    System.IO.BinaryWriter bw = new System.IO.BinaryWriter(connectInfo.Response);
                    bw.Write("start");

                    var invokingDB = Way.EntityDB.Design.DBHelper.CreateInvokeDatabase(database);
                    IDatabaseDesignService dbservice = Way.EntityDB.Design.DBHelper.CreateDatabaseDesignService((Way.EntityDB.DatabaseType)(int) database.dbType);


                    bw.Write(tables.Count * 1);
                    ICodeBuilder codeBuilder = new CodeBuilder();


                    foreach (var table in tables)
                    {
                        string[] codes = codeBuilder.BuildSimpleTable(db, database.NameSpace, table);
                        for (int i = 0; i < codes.Length; i++)
                        {
                            bw.Write(table.Name + "_" + i + ".cs");
                            byte[] bs = System.Text.Encoding.UTF8.GetBytes(codes[i]);
                            bw.Write(bs.Length);
                            bw.Write(bs);
                        }
                    }


                    bw.Write(":end");
                }
            }
            catch (Exception ex)
            {
                new System.IO.BinaryWriter(connectInfo.Response).Write(ex.Message);
            }
        }