public override int GetSeed(string tableName, string pkColumn, int SeedPoolSize = 100)
        {
            SeedPool       pool = null;
            DatabaseEngine db   = DatabaseEngineFactory.GetDatabaseEngine();

            lock (this)
            {
                string PoolName = string.Format("{0}_{1}", db.DatabaseSession.DatabaseName, db.GetTableName(tableName));
                if (SeedPoolDict.ContainsKey(PoolName))
                {
                    pool = SeedPoolDict[PoolName];      //从种子缓存加载种子值
                }
                else
                {
                    //初始化种子缓存
                    int SeedValue = db.GetMaxSeed(tableName, pkColumn, 0);
                    pool                   = new SeedPool();
                    pool.TableName         = tableName;
                    pool.SeedValue         = SeedValue;
                    SeedPoolDict[PoolName] = pool;
                }
            }

            lock (pool)
            {
                return(++pool.SeedValue);
            }
        }
Example #2
0
        public static List <T> ExecuteQuery(string commandText)
        {
            var      db   = DatabaseEngineFactory.GetDatabaseEngine();
            List <T> list = new List <T>();

            using (IDataReader dr = db.ExecuteReader(commandText))
            {
                db.Load(typeof(T), list, dr);
            }
            return(list);
        }
        public override void ClearSeed(string tableName)
        {
            DatabaseEngine db       = DatabaseEngineFactory.GetDatabaseEngine();
            string         PoolName = string.Format("{0}_{1}", db.DatabaseSession.DatabaseName, db.GetTableName(tableName));

            lock (this)
            {
                if (SeedPoolDict.ContainsKey(PoolName))
                {
                    SeedPoolDict.Remove(PoolName);
                }
            }
        }
Example #4
0
        public IList ToList()
        {
            IList          list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(TableMapping.ObjectType));
            DatabaseEngine db   = DatabaseEngineFactory.GetDatabaseEngine();

            if (this.PageIndex == -1)
            {
                db.Load(this, list);
            }
            else
            {
                db.LoadPage(this, list);
            }
            return(list);
        }
Example #5
0
        public object FirstOrDefault()
        {
            List <object>  list         = new List <object>();
            DatabaseEngine db           = DatabaseEngineFactory.GetDatabaseEngine();
            int            bakTakeCount = this.TakeCount;

            try
            {
                this.TakeCount = 1;
                db.Load(this, list);
            }
            finally
            {
                this.TakeCount = bakTakeCount;
            }
            return(list.FirstOrDefault());
        }
Example #6
0
        public override void ClearSeed(string tableName)
        {
            DatabaseEngine db       = DatabaseEngineFactory.GetDatabaseEngine();
            string         PoolName = string.Format("{0}_{1}", db.DatabaseSession.DatabaseName, db.GetTableName(tableName));

            lock (this)
            {
                using (DatabaseScope ds = new DatabaseScope().BeginTransaction(TransactionScopeOption.RequiresNew))
                {
                    DatabaseEngine newdb = DatabaseEngineFactory.GetNewDatabaseEngine(db.DatabaseSession.DatabaseName);
                    newdb.DatabaseSession.MappingTable = db.DatabaseSession.MappingTable;
                    newdb.ExecuteNonQuery(string.Format("delete from tableseed where tablename='{0}'", db.GetTableName(tableName)));

                    if (SeedPoolDict.ContainsKey(PoolName))
                    {
                        SeedPoolDict.Remove(PoolName);
                    }

                    ds.Complete();
                }
            }
        }
Example #7
0
        public int Update(Expression <Func <object> > expression)
        {
            HashSet <string> props = ExpressionHelper.GetExpression_PropertyList(expression);

            if (props.Count == 0)
            {
                throw new ObjectMappingException("not any property updated!");
            }

            object obj     = expression.Compile()();
            Type   objType = obj.GetType();

            foreach (var prop in props)
            {
                var           val    = objType.GetProperty(prop).GetValue(obj, null);
                ColumnMapping column = TableMapping.GetColumnMappingByProperty(prop);
                this.UpdateParameters.Add(new SqlParameter(column != null ? column.Name : prop, val));
            }

            DatabaseEngine db = DatabaseEngineFactory.GetDatabaseEngine();

            return(db.Update(this));
        }
Example #8
0
        public int Update()
        {
            DatabaseEngine db = DatabaseEngineFactory.GetDatabaseEngine();

            return(db.Update(this));
        }
Example #9
0
        public static void BulkCopy(IList objs, Dictionary <string, object> bulkCopyConfig = null)
        {
            TableMapping tableMapping = MappingService.Instance.GetTableMapping(typeof(T));

            DatabaseEngineFactory.GetDatabaseEngine().BulkCopy(tableMapping, objs, bulkCopyConfig);
        }
Example #10
0
 public static int Delete(T obj)
 {
     return(DatabaseEngineFactory.GetDatabaseEngine().Delete(obj));
 }
Example #11
0
 public static int Update(T obj)
 {
     return(DatabaseEngineFactory.GetDatabaseEngine().Update(obj));
 }
Example #12
0
 public int Delete()
 {
     return(DatabaseEngineFactory.GetDatabaseEngine().Delete(this));
 }
Example #13
0
        public int Count()
        {
            DatabaseEngine db = DatabaseEngineFactory.GetDatabaseEngine();

            return(db.Count(this));
        }
Example #14
0
        public object Max(string column)
        {
            DatabaseEngine db = DatabaseEngineFactory.GetDatabaseEngine();

            return(db.Max(this, column));
        }
Example #15
0
        public override int GetSeed(string tableName, string pkColumn, int SeedPoolSize = 100)
        {
            SeedPool       pool = null;
            DatabaseEngine db   = DatabaseEngineFactory.GetDatabaseEngine();

            lock (this)
            {
                string PoolName = string.Format("{0}_{1}", db.DatabaseSession.DatabaseName, db.GetTableName(tableName));
                if (SeedPoolDict.ContainsKey(PoolName))
                {
                    pool = SeedPoolDict[PoolName];      //从种子缓存加载种子值
                }
                else
                {
                    //初始化种子缓存
                    int SeedValue       = 0;
                    int CurTableMaxSeed = db.GetMaxSeed(tableName, pkColumn, 0);  //从数据库表中获取当前记录中已使用的最大种子值,如果没有记录则使用默认种子值(0)

                    using (DatabaseScope ds = new DatabaseScope().BeginTransaction(TransactionScopeOption.RequiresNew))
                    {
                        DatabaseEngine newdb = DatabaseEngineFactory.GetNewDatabaseEngine(db.DatabaseSession.DatabaseName);
                        newdb.DatabaseSession.MappingTable = db.DatabaseSession.MappingTable;
                        int?FactorySeed = GetFactorySeed(newdb, tableName);
                        if (FactorySeed.HasValue)
                        {
                            SeedValue = FactorySeed.Value;
                        }
                        else
                        {
                            SeedValue = CurTableMaxSeed;
                            newdb.ExecuteNonQuery(string.Format("insert into tableseed values('{0}',{1})", db.GetTableName(tableName), SeedValue));
                        }

                        ds.Complete();
                    }

                    pool                   = new SeedPool();
                    pool.TableName         = tableName;
                    pool.SeedValue         = pool.MaxSeedValue = SeedValue;
                    SeedPoolDict[PoolName] = pool;
                }
            }

            lock (pool)
            {
                if (pool.SeedValue >= pool.MaxSeedValue)
                {
                    using (DatabaseScope ds = new DatabaseScope().BeginTransaction(TransactionScopeOption.RequiresNew))
                    {
                        DatabaseEngine newdb = DatabaseEngineFactory.GetNewDatabaseEngine(db.DatabaseSession.DatabaseName);
                        newdb.DatabaseSession.MappingTable = db.DatabaseSession.MappingTable;
                        string strSQL = string.Format("update tableseed set seed=seed+{0} where tablename='{1}'", SeedPoolSize, newdb.GetTableName(tableName));

                        int ret = newdb.ExecuteNonQuery(strSQL);
                        if (ret != 1)
                        {
                            throw new ObjectMappingException(string.Format("GetSeed Failed -> \"{0}\"", strSQL));
                        }
                        pool.MaxSeedValue = GetFactorySeed(newdb, tableName).Value;
                        ds.Complete();
                    }
                    pool.SeedValue = pool.MaxSeedValue - SeedPoolSize;
                }

                return(++pool.SeedValue);
            }
        }