Exemplo n.º 1
0
        private static bool FillSchemaFromCache(ref MDataRow row, ref DbBase dbBase, string tableName, string sourceTableName)
        {
            bool returnResult = false;

            string key = GetSchemaKey(tableName, dbBase.DataBase, dbBase.dalType);

            if (CacheManage.LocalInstance.Contains(key))//缓存里获取
            {
                try
                {
                    row          = ((MDataColumn)CacheManage.LocalInstance.Get(key)).ToRow(sourceTableName);
                    returnResult = row.Count > 0;
                }
                catch (Exception err)
                {
                    Log.WriteLogToTxt(err);
                }
            }
            else if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath))
            {
                string fullPath = AppDomain.CurrentDomain.BaseDirectory + AppConfig.DB.SchemaMapPath + key + ".ts";
                if (System.IO.File.Exists(fullPath))
                {
                    MDataColumn mdcs = MDataColumn.CreateFrom(fullPath);
                    if (mdcs.Count > 0)
                    {
                        row          = mdcs.ToRow(sourceTableName);
                        returnResult = row.Count > 0;
                        CacheManage.LocalInstance.Add(key, mdcs.Clone(), null, 1440);
                    }
                }
            }

            return(returnResult);
        }
Exemplo n.º 2
0
        private static bool FillSchemaFromDb(ref MDataRow row, ref DbBase dbBase, string tableName, string sourceTableName)
        {
            try
            {
                MDataColumn mdcs = null;
                //if (tableName.IndexOf('(') > -1 && tableName.IndexOf(')') > -1)//自定义视图table
                //{
                //    dbBase.tempSql = "view";//使用access方式加载列
                //}
                mdcs = GetColumns(tableName, ref dbBase);
                if (mdcs.Count == 0)
                {
                    return(false);
                }
                row           = mdcs.ToRow(sourceTableName);
                row.TableName = sourceTableName;
                string key = GetSchemaKey(tableName, dbBase.DataBase, dbBase.dalType);
                CacheManage.LocalInstance.Add(key, mdcs.Clone(), null, 1440);

                switch (dbBase.dalType)//文本数据库不保存。
                {
                case DalType.Access:
                case DalType.SQLite:
                case DalType.MsSql:
                case DalType.MySql:
                case DalType.Oracle:
                    if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath))
                    {
                        string folderPath = AppDomain.CurrentDomain.BaseDirectory + AppConfig.DB.SchemaMapPath;
                        if (System.IO.Directory.Exists(folderPath))
                        {
                            mdcs.WriteSchema(folderPath + key + ".ts");
                        }
                    }
                    break;
                }
                return(true);
            }
            catch (Exception err)
            {
                Log.WriteLogToTxt(err);
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 初始化状态[继承此基类的实体在构造函数中需调用此方法]
        /// </summary>
        /// <param name="entityInstance">实体对象,一般写:this</param>
        /// <param name="tableName">表名,如:Users</param>
        /// <param name="conn">数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串</param>
        protected void SetInit(Object entityInstance, string tableName, string conn)
        {
            conn     = string.IsNullOrEmpty(conn) ? AppConfig.DB.DefaultConn : conn;
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + MD5.Get(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = TableSchema.GetColumns(typeInfo);
                        if (!DBTool.ExistsTable(tableName, conn))
                        {
                            if (!DBTool.CreateTable(tableName, Columns, conn))
                            {
                                Error.Throw("Create Table Error:" + tableName);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = TableSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Add(key, Columns, null, 1440);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    action.SetAopOff();
                }
                action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.WriteLogToTxt(err);
                }
                throw;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 将原有的初始化改造成延时加载。
        /// </summary>
        private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op)
        {
            if (string.IsNullOrEmpty(conn))
            {
                //不设置链接,则忽略(当成普通的实体类)
                return;
            }
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + StaticTool.GetHashKey(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = ColumnSchema.GetColumns(typeInfo);
                        ConnBean connBean = ConnBean.Create(conn);//下面指定链接,才不会在主从备时被切换到其它库。
                        if (!DBTool.ExistsTable(tableName, connBean.ConnString))
                        {
                            DBTool.ErrorMsg = null;
                            if (!DBTool.CreateTable(tableName, Columns, connBean.ConnString))
                            {
                                Error.Throw("SimpleOrmBase :Create Table " + tableName + " Error:" + DBTool.ErrorMsg);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = ColumnSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Set(key, Columns, 1440, null);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                _Action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    _Action.SetAopState(Aop.AopOp.CloseAll);
                }
                else
                {
                    _Action.SetAopState(op);
                }
                _Action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.Write(err, LogType.DataBase);
                }
                throw;
            }
        }