Exemple #1
0
        /// <summary>
        /// 根据MyId的主键获取拆分表名
        /// </summary>
        /// <param name="type">对应的数据库映射信息</param>
        /// <param name="myId">MyId主键值</param>
        /// <returns></returns>
        internal static string GetTableName(DBMap.DBTable table, object myId)
        {
            if (table.SeparateType == SeparateType.None)
            {
                return(table.Name);
            }

            return(GetTableName(table, MyIdMake.GetMyIdDate(myId)));
        }
Exemple #2
0
        /// <summary>
        /// 根据MyId的主键获取拆分表名
        /// </summary>
        /// <param name="type">映射类型,比如:typeof(SMSendRecord)</param>
        /// <param name="myId">MyId主键值</param>
        /// <returns></returns>
        public static string GetTableName(Type type, object myId)
        {
            DBTable table = MapHelper.GetDBTable(type);

            if (table.SeparateType == SeparateType.None)
            {
                return(table.Name);
            }

            return(GetTableName(table, MyIdMake.GetMyIdDate(myId)));
        }
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="type">映射表类型</param>
        /// <param name="date">要写入表的拆分日期。注意该日期必须与Id中的日期对应。比如按年拆分的表,Id中的年份与该日期中的年份必须相同</param>
        public void Insert(object instance, Type type, DateTime date)
        {
            DBTable table = MapHelper.GetDBTable(type);

            //取主键值
            object primaryVal = null;

            if (table.PrimaryKey[0].DBPrimaryType != DBPrimaryType.Identity)
            {
                primaryVal = this.GetValue(table.PrimaryKey[0], instance);
                if (primaryVal == null)
                {
                    throw new MyDBException("新增对象,非自增长表主键不能为空");
                }
            }

            //取表名。如果是拆分表,则获取拆分表名
            string tbName = table.Name;

            if (table.SeparateType != SeparateType.None)
            {
                //如果传入时间为空,则取myId中的时间
                if (date == DateTime.MinValue)
                {
                    date = MyIdMake.GetMyIdDate(primaryVal);
                }

                //获取数据库表名
                tbName = TableSeparate.GetTableName(table, date);
            }

            //获取该数据库表 对应的DBSql中的Insert语句
            DBSql dbsql = DBSqlHelper.GetDBSql(tbName, _dbContext.DataType);

            if (string.IsNullOrEmpty(dbsql.InsertSql))//如果该表的新增语句为空,则生成该表的Insert语句
            {
                dbsql.InsertSql = GetInsertSql(table, tbName);
            }

            //将数据写入数据库
            Insert(instance, primaryVal, table, dbsql.InsertSql);
        }