예제 #1
0
 protected FieldMapping(Type type, string fieldName, string indexName, DataMapping mapping, bool isNullable, string dbType)
 {
     ObjectType = type;
     if (type != null)
     {
         _typeCode = Type.GetTypeCode(type);
     }
     _name        = fieldName;
     _indexName   = indexName;
     _typeMapping = mapping;
     _isNullable  = isNullable;
     if (dbType != null)
     {
         _dbType = dbType.Trim();
     }
 }
예제 #2
0
        /// <summary>
        /// 生成数据新增命令
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>新增命令对象</returns>
        public virtual IDbCommand CreateInsertCommand(object entity)
        {
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(entity.GetType());
            List <FieldMapping>    fields  = new List <FieldMapping> ();

            fields.AddRange(mapping.GetFieldMappings());
            if (mapping.IdentityField != null)
            {
                fields.Remove(mapping.IdentityField);
            }
            List <DataParameter> paramList = GetDataParameters(fields, entity);
            StringBuilder        insert    = new StringBuilder();
            StringBuilder        values    = new StringBuilder();

            IDataParameter[] dataParameters = new IDataParameter[paramList.Count];
            int count = 0;

            foreach (DataParameter dataParameter in paramList)
            {
                IDataParameter param = _database.CreateParameter("P" + count, dataParameter.Value, dataParameter.DbType, dataParameter.Direction);
                insert.AppendFormat("{0},", CreateDataFieldSql(dataParameter.ParameterName));
                values.AppendFormat("{0},", param.ParameterName);
                dataParameters [count] = param;
                count++;
            }
            insert.Remove(insert.Length - 1, 1);
            values.Remove(values.Length - 1, 1);
            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("insert into {0}({1})values({2})", CreateDataTableSql(mapping.TableName), insert, values);
            //string identitysql = CreateIdentitySql(mapping);
            //if (!string.IsNullOrEmpty(identitysql))
            //{
            //    sql.AppendFormat(";{0}", identitysql);
            //}
            IDbCommand command = _database.CreateCommand(sql.ToString());

            command.CommandType = CommandType.Text;
            foreach (IDataParameter param in dataParameters)
            {
                command.Parameters.Add(param);
            }
            return(command);
        }
예제 #3
0
        public ObjectFieldMapping(Type type, string fieldName, string indexName, DataMapping mapping, bool isNullable, string dbType, bool isIdentity, bool isPrimaryKey)
            : base(type, fieldName, indexName, mapping, isNullable, dbType)
        {
            if (isIdentity)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType, fieldName, type));
            }

            if (isPrimaryKey)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportPrimaryKeyFieldType, ObjectType, fieldName, type));
            }

            if (!isNullable)
            {
                var value = Activator.CreateInstance(type);
                _defaultValue = JsonConvert.SerializeObject(value);
            }
        }
예제 #4
0
        /// <summary>
        /// 设置数据类型的别名
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="tableName">别名</param>
        public void Set <T> (string tableName) where T : class, new()
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName");
            }
            Type type = typeof(T);
            DataEntityMapping mapping = null;

            if (_aliaslist.ContainsKey(type))
            {
                mapping = _aliaslist [type];
            }
            else
            {
                mapping           = DataMapping.GetEntityMapping(type);
                _aliaslist [type] = mapping;
            }
            mapping.SetAliasName(tableName);
        }
예제 #5
0
        public EnumFieldMapping(Type type, string fieldName, string indexName, DataMapping mapping, bool isNullable,
                                string dbType, object defaultValue, bool isIdentity, bool isPrimaryKey)
            : base(type, fieldName, indexName, mapping, isNullable, dbType)
        {
            if (isIdentity)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType,
                                                           fieldName, type));
            }

            if (isPrimaryKey)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportPrimaryKeyFieldType, ObjectType,
                                                           fieldName, type));
            }

            var nullType = Type.GetType("System.Nullable`1", true);

            NullableType = nullType.MakeGenericType(type);
            var values = Enum.GetValues(ObjectType);
            var value  = values.GetValue(0);

            _min      = value;
            _minValue = Convert.ChangeType(value, _typeCode, null);

            if (defaultValue != null)
            {
                var defaultValueType = defaultValue.GetType();
                if (defaultValueType == type)
                {
                    _default      = defaultValue;
                    _defaultValue = Convert.ChangeType(defaultValue, _typeCode, null);
                }
                else
                {
                    throw new LightDataException(string.Format(SR.EnumDefaultValueType, mapping.ObjectType, fieldName,
                                                               defaultValue));
                }
            }
        }
        public PrimitiveFieldMapping(Type type, string fieldName, string indexName, DataMapping mapping,
                                     bool isNullable, string dbType, object defaultValue, bool isIdentity, bool isPrimaryKey)
            : base(type, fieldName, indexName, mapping, isNullable, dbType)
        {
            if (isIdentity)
            {
                if (_typeCode == TypeCode.Int32 || _typeCode == TypeCode.Int64 || _typeCode == TypeCode.UInt32 ||
                    _typeCode == TypeCode.UInt64)
                {
                    IsIdentity = true;
                }
                else
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType,
                                                               fieldName, type));
                }
            }

            IsPrimaryKey = isPrimaryKey;

            var nullType = Type.GetType("System.Nullable`1", true);

            NullableType = nullType.MakeGenericType(type);

            _minValue = type.GetDefaultValue();

            if (Equals(_minValue, null))
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, ObjectType, fieldName,
                                                           type));
            }


            if (defaultValue != null)
            {
                var defaultValueType = defaultValue.GetType();
                _defaultValue = defaultValueType == type ? defaultValue : Convert.ChangeType(defaultValue, type);
            }
        }
예제 #7
0
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="data">数据对象</param>
        /// <returns>受影响行数</returns>
        public int Insert(object data)
        {
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(data.GetType());
            object obj;
            int    rInt = 0;

            using (IDbCommand command = _dataBase.Factory.CreateInsertCommand(data))
            {
                IDbCommand identityCommand = _dataBase.Factory.CreateIdentityCommand(mapping);
                obj = ExecuteInsertCommand(command, identityCommand, SafeLevel.Default);
                if (identityCommand != null)
                {
                    identityCommand.Dispose();
                }
            }
            rInt = 1;
            if (mapping.IdentityField != null && !Object.Equals(obj, null))
            {
                object id = Convert.ChangeType(obj, mapping.IdentityField.ObjectType);
                mapping.IdentityField.Handler.Set(data, id);
            }
            return(rInt);
        }
예제 #8
0
        public virtual QueryCommand QueryJoinData(DataContext context, DataMapping mapping, ISelector selector, List <IJoinModel> models, QueryExpression query, OrderExpression order, bool distinct, Region region)
        {
            CreateSqlState state       = new CreateSqlState(context);
            CommandData    commandData = _factory.CreateSelectJoinTableCommand(selector, models, query, order, distinct, region, state);
            DbCommand      command     = commandData.CreateCommand(this, state);
            QueryState     queryState  = new QueryState();

            foreach (IJoinModel model in models)
            {
                if (model.NoDataSetEntityNull)
                {
                    queryState.SetNoDataSetNull(model.AliasTableName);
                }
            }
            queryState.SetSelector(selector);
            QueryCommand queryCommand = new QueryCommand()
            {
                Command   = command,
                InnerPage = commandData.InnerPage,
                State     = queryState
            };

            return(queryCommand);
        }
예제 #9
0
        public DecimalFieldMapping(string fieldName, string indexName, DataMapping mapping,
                                   bool isNullable, string dbType, object defaultValue, bool isIdentity, bool isPrimaryKey)
            : base(typeof(decimal), fieldName, indexName, mapping, isNullable, dbType)
        {
            if (isIdentity)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType,
                                                           fieldName, ObjectType));
            }

            IsPrimaryKey = isPrimaryKey;

            var nullType = Type.GetType("System.Nullable`1", true);

            NullableType = nullType.MakeGenericType(ObjectType);

            _minValue = ObjectType.GetDefaultValue();

            if (defaultValue != null)
            {
                var defaultValueType = defaultValue.GetType();
                _defaultValue = defaultValueType == ObjectType ? defaultValue : Convert.ChangeType(defaultValue, ObjectType);
            }
        }
예제 #10
0
        /// <summary>
        /// 生成数据更新命令
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <param name="updatefieldNames">需更新的数据字段</param>
        /// <returns>更新命令对象</returns>
        public virtual IDbCommand CreateUpdateCommand(object entity, string[] updatefieldNames)
        {
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(entity.GetType());

            if (mapping.PrimaryKeyFields == null || mapping.PrimaryKeyFields.Length == 0)
            {
                throw new LightDataException(RE.PrimaryKeyIsNotExist);
            }

            List <FieldMapping> fields = null;

            if (updatefieldNames != null && updatefieldNames.Length > 0)
            {
                List <FieldMapping> updateFields = new List <FieldMapping> ();
                foreach (string name in updatefieldNames)
                {
                    FieldMapping fm = mapping.FindFieldMapping(name);

                    if (fm != null && !updateFields.Contains(fm))
                    {
                        updateFields.Add(fm);
                    }
                }
                fields = updateFields;
            }

            if (fields == null)
            {
                fields = new List <FieldMapping> ();
                fields.AddRange(mapping.GetFieldMappings());
            }

            if (mapping.IdentityField != null)
            {
                fields.Remove(mapping.IdentityField);
            }
            foreach (DataFieldMapping primaryField in mapping.PrimaryKeyFields)
            {
                fields.Remove(primaryField);
            }

            List <DataParameter> columnList  = GetDataParameters(fields, entity);
            List <DataParameter> primaryList = GetDataParameters(mapping.PrimaryKeyFields, entity);

            if (columnList.Count == 0)
            {
                throw new LightDataException(RE.UpdateFieldIsNotExists);
            }
            StringBuilder update = new StringBuilder();

            StringBuilder where = new StringBuilder();
            IDataParameter[] dataParameters = new IDataParameter[columnList.Count + primaryList.Count];

            int count = 0;

            foreach (DataParameter dataParameter in columnList)
            {
                IDataParameter param = _database.CreateParameter("P" + count, dataParameter.Value, dataParameter.DbType, dataParameter.Direction);
                update.AppendFormat("{0}={1},", CreateDataFieldSql(dataParameter.ParameterName), param.ParameterName);
                dataParameters [count] = param;
                count++;
            }
            foreach (DataParameter dataParameter in primaryList)
            {
                IDataParameter param = _database.CreateParameter("P" + count, dataParameter.Value, dataParameter.DbType, dataParameter.Direction);
                where.AppendFormat("{0}={1} and ", CreateDataFieldSql(dataParameter.ParameterName), param.ParameterName);
                dataParameters [count] = param;
                count++;
            }
            update.Remove(update.Length - 1, 1);
            where.Remove(where.Length - 5, 5);
            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("update {0} set {1} where {2}", CreateDataTableSql(mapping.TableName), update, where);
            IDbCommand command = _database.CreateCommand(sql.ToString());

            command.CommandType = CommandType.Text;
            foreach (IDataParameter param in dataParameters)
            {
                command.Parameters.Add(param);
            }
            return(command);
        }
예제 #11
0
 internal LEnumerable(DataContext dataContext)
 {
     _context = dataContext;
     _mapping = DataMapping.GetEntityMapping(typeof(T));
 }
예제 #12
0
        /// <summary>
        /// 读取关联数据
        /// </summary>
        /// <param name="keyName">关联字段属性名称</param>
        /// <param name="extendQuery">扩展查询</param>
        /// <param name="extendOrder">扩展排序</param>
        /// <returns>关联数据</returns>
        private object LoadRelationData(string keyName, QueryExpression extendQuery, OrderExpression extendOrder)
        {
            DataEntityMapping    selfMapping     = DataMapping.GetEntityMapping(this.GetType());
            RelationFieldMapping relationMapping = selfMapping.FindRelateionMapping(keyName);
            QueryExpression      expression      = null;

            foreach (RelationFieldMapping.RelationKeyValue rt in relationMapping.GetRelationKeyValues())
            {
                DataFieldInfo info   = new DataFieldInfo(relationMapping.RelateTableMapping.ObjectType, rt.RelateField.Name);
                object        objkey = rt.MasterField.Handler.Get(this);
                if (Object.Equals(objkey, null))
                {
                    expression &= info.IsNull();
                }
                else
                {
                    expression &= info == objkey;
                }
            }
            if (extendQuery != null)
            {
                expression = QueryExpression.And(expression, extendQuery);
            }
            //判断与关联表的关联关系
            if (relationMapping.Kind == RelationFieldMapping.RelationKind.OneToOne)
            {
                //一对一关系,直接查询单个对象
                object     obj = this.Context.SelectSingle(relationMapping.RelateTableMapping, expression, extendOrder, 0, SafeLevel.None);
                DataEntity de  = obj as DataEntity;
                if (de == null)
                {
                    return(obj);
                }
                if (relationMapping.RelateRelationMapping != null)                  //判断是否相互关联
                {
                    if (relationMapping.RelateRelationMapping.Kind == RelationFieldMapping.RelationKind.OneToOne)
                    {
                        de.TempRelationDatas.Add(relationMapping.RelateRelationMapping.RelationName, this);
                    }
                }
                return(de);
            }
            else
            {
                IList list = this.Context.QueryDataList(relationMapping.RelateTableMapping, expression, extendOrder, null, SafeLevel.Default) as IList;
                //if (list.Count == 0) return list;

                if (relationMapping.RelateRelationMapping != null && relationMapping.RelateRelationMapping.MasterTableMapping.IsDataEntity)
                {
                    if (relationMapping.RelateRelationMapping.Kind == RelationFieldMapping.RelationKind.OneToOne)
                    {
                        foreach (object obj in list)
                        {
                            DataEntity de = obj as DataEntity;
                            de.TempRelationDatas.Add(relationMapping.RelateRelationMapping.RelationName, this);
                        }
                    }
                }
                if (relationMapping.ResultDataKind == RelationFieldMapping.DataKind.IList)
                {
                    return(list);
                }
                else
                {
                    Array array = Array.CreateInstance(relationMapping.RelateTableMapping.ObjectType, list.Count);
                    list.CopyTo(array, 0);
                    return(array);
                }
            }
        }
예제 #13
0
        public static DataFieldMapping CreateCustomFieldMapping(PropertyInfo property, DataMapping mapping)
        {
            var type      = property.PropertyType;
            var typeInfo  = type.GetTypeInfo();
            var indexName = property.Name;
            var fieldName = property.Name;

            DataFieldMapping fieldMapping;
            var isNullable = false;

            if (typeInfo.IsGenericType)
            {
                var frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    var arguments = typeInfo.GetGenericArguments();
                    type       = arguments[0];
                    typeInfo   = type.GetTypeInfo();
                    isNullable = true;
                }
            }

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    fieldMapping = new BytesFieldMapping(fieldName, indexName, mapping, isNullable, null, false,
                                                         false);
                }
                else
                {
                    fieldMapping =
                        new ObjectFieldMapping(type, fieldName, indexName, mapping, isNullable, null, false, false);
                }
            }

            else if (type == typeof(Guid))
            {
                fieldMapping =
                    new GuidFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (type == typeof(string))
            {
                fieldMapping =
                    new StringFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (type == typeof(DateTime))
            {
                fieldMapping =
                    new DateTimeFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (type == typeof(decimal))
            {
                fieldMapping =
                    new DecimalFieldMapping(fieldName, indexName, mapping, isNullable, null, null, false, false);
            }
            else if (typeInfo.IsEnum)
            {
                fieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable, null, null, false,
                                                    false);
            }
            else if (type.IsPrimitive)
            {
                fieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping,
                                                         isNullable, null, null, false, false);
            }
            else if (type == typeof(DBNull))
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType,
                                                           property.Name, type));
            }
            else
            {
                fieldMapping =
                    new ObjectFieldMapping(type, fieldName, indexName, mapping, isNullable, null, false, false);
            }

            fieldMapping.Handler = new PropertyHandler(property);
            return(fieldMapping);
        }
예제 #14
0
 /// <summary>
 /// 查询并返回指定类型的枚举数据
 /// </summary>
 /// <typeparam name="T">数据类型</typeparam>
 /// <param name="region">查询范围</param>
 /// <returns>枚举数据</returns>
 public IEnumerable LQuery <T>(Region region) where T : class, new()
 {
     return(_context.QueryDataReader(DataMapping.GetMapping(typeof(T)), _command, region, _level));
 }
예제 #15
0
        public static DataFieldMapping CreateDataFieldMapping(PropertyInfo property, DataFieldMapperConfig config,
                                                              int positionOrder, DataMapping mapping)
        {
            var type      = property.PropertyType;
            var typeInfo  = type.GetTypeInfo();
            var indexName = property.Name;
            var fieldName = string.IsNullOrEmpty(config.Name) ? property.Name : config.Name;

            DataFieldMapping fieldMapping;

            if (typeInfo.IsGenericType)
            {
                var frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    var arguments = typeInfo.GetGenericArguments();
                    type     = arguments[0];
                    typeInfo = type.GetTypeInfo();
                }
            }

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    fieldMapping = new BytesFieldMapping(fieldName, indexName, mapping,
                                                         config.IsNullable, config.DbType, config.IsIdentity, config.IsPrimaryKey);
                }
                else
                {
                    fieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping,
                                                          config.IsNullable, config.DbType, config.IsIdentity, config.IsPrimaryKey);
                }
            }
            // else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            // {
            //     throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType,
            //         property.Name, type));
            // }
            else if (type == typeof(Guid))
            {
                fieldMapping = new GuidFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                    config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(string))
            {
                fieldMapping = new StringFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                      config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(DateTime))
            {
                fieldMapping = new DateTimeFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                        config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(decimal))
            {
                fieldMapping = new DecimalFieldMapping(fieldName, indexName, mapping, config.IsNullable,
                                                       config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (typeInfo.IsEnum)
            {
                fieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, config.IsNullable,
                                                    config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type.IsPrimitive)
            {
                fieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping,
                                                         config.IsNullable, config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
            }
            else if (type == typeof(DBNull))
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType,
                                                           property.Name, type));
            }
            else
            {
                fieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping,
                                                      config.IsNullable, config.DbType, config.IsIdentity, config.IsPrimaryKey);
            }

            if (config.DataOrder > 0)
            {
                fieldMapping.DataOrder = config.DataOrder;
            }

            fieldMapping.PositionOrder   = positionOrder;
            fieldMapping.FunctionControl = config.FunctionControl == FunctionControl.Default
                ? FunctionControl.Full
                : config.FunctionControl;
            fieldMapping.Handler = new PropertyHandler(property);
            return(fieldMapping);
        }
예제 #16
0
        public static DataFieldMapping CreateCustomFieldMapping(PropertyInfo property, DataMapping mapping)
        {
            Type     type      = property.PropertyType;
            TypeInfo typeInfo  = type.GetTypeInfo();
            string   indexName = property.Name;
            string   fieldName = property.Name;

            DataFieldMapping fieldMapping;
            string           dbType     = null;
            bool             isNullable = false;

            if (typeInfo.IsGenericType)
            {
                Type frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    Type[] arguments = typeInfo.GetGenericArguments();
                    type       = arguments[0];
                    typeInfo   = type.GetTypeInfo();
                    isNullable = true;
                }
            }

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    BytesFieldMapping bytesFieldMapping = new BytesFieldMapping(type, fieldName, indexName, mapping, isNullable, null);
                    fieldMapping = bytesFieldMapping;
                }
                else
                {
                    return(null);
                }
            }
            else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            {
                return(null);
            }
            else if (typeInfo.IsEnum)
            {
                EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable);
                fieldMapping = enumFieldMapping;
            }
            else
            {
                TypeCode code = Type.GetTypeCode(type);
                if (code == TypeCode.Empty)
                {
                    return(null);
                }
                else if (code == TypeCode.Object)
                {
                    ObjectFieldMapping objectFieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType);
                    fieldMapping = objectFieldMapping;
                }
                else
                {
                    PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType, null, false, false);
                    fieldMapping = primitiveFieldMapping;
                }
            }
            fieldMapping._handler = new PropertyHandler(property);
            return(fieldMapping);
        }
예제 #17
0
        /// <summary>
        /// 批量更新数据
        /// </summary>
        /// <param name="type">更新对象类型</param>
        /// <param name="updates">更新对象类型</param>
        /// <param name="expression">查询表达式</param>
        /// <returns>受影响行数</returns>
        internal int UpdateMass(Type type, UpdateSetValue[] updates, QueryExpression expression)
        {
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(type);

            return(UpdateMass(mapping, updates, expression));
        }
예제 #18
0
 internal AggregateTable(DataContext dataContext)
 {
     _context        = dataContext;
     _enetityMapping = DataMapping.GetEntityMapping(typeof(T));
 }
예제 #19
0
        public PrimitiveFieldMapping(Type type, string fieldName, string indexName, DataMapping mapping, bool isNullable, string dbType, object defaultValue, bool isIdentity, bool isPrimaryKey)
            : base(type, fieldName, indexName, mapping, isNullable, dbType)
        {
            if (type != typeof(string))
            {
                Type itemstype = Type.GetType("System.Nullable`1");
                _nullableType = itemstype.MakeGenericType(type);
            }
            else
            {
                _nullableType = type;
            }
            if (defaultValue != null)
            {
                Type defaultValueType = defaultValue.GetType();
                if (_typeCode == TypeCode.DateTime)
                {
                    if (defaultValueType == typeof(DefaultTime))
                    {
                        DefaultTime defaultTime = (DefaultTime)defaultValue;
                        this._defaultTimeFunction = DefaultTimeFunction.GetFunction(defaultTime);
                        if (defaultTime == DefaultTime.TimeStamp || defaultTime == DefaultTime.UtcTimeStamp)
                        {
                            isTimeStamp = true;
                        }
                        this._defaultValue = this._defaultTimeFunction;
                    }
                    else if (defaultValueType == typeof(DateTime))
                    {
                        this._defaultValue = defaultValue;
                    }
                    else if (defaultValueType == typeof(string))
                    {
                        string str = defaultValue as string;
                        if (DateTime.TryParse(str, out DateTime dt))
                        {
                            this._defaultValue = dt;
                        }
                    }
                }
                else if (defaultValueType == type)
                {
                    this._defaultValue = defaultValue;
                }
                else
                {
                    this._defaultValue = Convert.ChangeType(defaultValue, type);
                }
            }
            if (isIdentity)
            {
                if (_typeCode == TypeCode.Int32 || _typeCode == TypeCode.Int64 || _typeCode == TypeCode.UInt32 || _typeCode == TypeCode.UInt64)
                {
                    _isIdentity = true;
                }
                else
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType, fieldName, type));
                }
            }

            _isPrimaryKey = isPrimaryKey;
            switch (_typeCode)
            {
            case TypeCode.String:
                _minValue = string.Empty;
                break;

            case TypeCode.Boolean:
                _minValue = false;
                break;

            case TypeCode.Byte:
                _minValue = byte.MinValue;
                break;

            case TypeCode.SByte:
                _minValue = MinSByte;
                break;

            case TypeCode.DateTime:
                _minValue = DateTime.MinValue;
                break;

            case TypeCode.Char:
                _minValue = Char.MinValue;
                break;

            case TypeCode.Int16:
                _minValue = MinInt16;
                break;

            case TypeCode.Int32:
                _minValue = MinInt32;
                break;

            case TypeCode.Int64:
                _minValue = MinInt64;
                break;

            case TypeCode.UInt16:
                _minValue = UInt16.MinValue;
                break;

            case TypeCode.UInt32:
                _minValue = UInt32.MinValue;
                break;

            case TypeCode.UInt64:
                _minValue = UInt64.MinValue;
                break;

            case TypeCode.Decimal:
                _minValue = MinDecimal;
                break;

            case TypeCode.Single:
                _minValue = MinSingle;
                break;

            case TypeCode.Double:
                _minValue = MinDouble;
                break;
            }
        }
예제 #20
0
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="type">删除对象类型</param>
        /// <param name="expression">查询表达式</param>
        /// <returns></returns>
        internal int DeleteMass(Type type, QueryExpression expression)
        {
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(type);

            return(DeleteMass(mapping, expression));
        }
예제 #21
0
 public BytesFieldMapping(Type type, string fieldName, string indexName, DataMapping mapping, bool isNullable, string dbType)
     : base(type, fieldName, indexName, mapping, isNullable, dbType)
 {
 }
예제 #22
0
        public override IDbCommand[] CreateBulkInsertCommand(Array entitys, int batchCount)
        {
            if (entitys == null || entitys.Length == 0)
            {
                throw new ArgumentNullException("entitys");
            }
            if (batchCount <= 0)
            {
                batchCount = 10;
            }
            object tmpEntity = entitys.GetValue(0);
            DataTableEntityMapping mapping = DataMapping.GetTableMapping(tmpEntity.GetType());
            List <FieldMapping>    fields  = new List <FieldMapping> ();
            int totalCount = entitys.Length;

            fields.AddRange(mapping.GetFieldMappings());
            if (mapping.IdentityField != null)
            {
                fields.Remove(mapping.IdentityField);
            }
            StringBuilder values = new StringBuilder();
            StringBuilder insert = new StringBuilder();


            List <DataParameter> paramList = GetDataParameters(fields, tmpEntity);

            foreach (DataParameter dataParameter in paramList)
            {
                insert.AppendFormat("{0},", CreateDataFieldSql(dataParameter.ParameterName));
            }
            insert.Remove(insert.Length - 1, 1);
            string insertsql = string.Format("insert into {0}({1})", CreateDataTableSql(mapping.TableName), insert);

            int createCount      = 0;
            int totalCreateCount = 0;

            IDbCommand        command    = _database.CreateCommand();
            int               paramCount = 0;
            List <IDbCommand> commands   = new List <IDbCommand> ();

            foreach (object entity in entitys)
            {
                paramList = GetDataParameters(fields, entity);
                StringBuilder value = new StringBuilder();
                foreach (DataParameter dataParameter in paramList)
                {
                    IDataParameter param = _database.CreateParameter("P" + paramCount, dataParameter.Value, dataParameter.DbType, dataParameter.Direction);
                    value.AppendFormat("{0},", param.ParameterName);
                    command.Parameters.Add(param);
                    paramCount++;
                }
                value.Remove(value.Length - 1, 1);
                values.AppendFormat("({0}),", value);
                createCount++;
                totalCreateCount++;
                if (createCount == batchCount || totalCreateCount == totalCount)
                {
                    values.Remove(values.Length - 1, 1);
                    command.CommandText = string.Format("{0}values{1}", insertsql, values);
                    commands.Add(command);
                    if (totalCreateCount == totalCount)
                    {
                        break;
                    }
                    command     = _database.CreateCommand();
                    createCount = 0;
                    paramCount  = 0;
                    values      = new StringBuilder();
                }
            }
            return(commands.ToArray());
        }
예제 #23
0
        public static DataFieldMapping CreateDataFieldMapping(PropertyInfo property, DataFieldMapperConfig config, int positionOrder, DataMapping mapping)
        {
            Type     type      = property.PropertyType;
            TypeInfo typeInfo  = type.GetTypeInfo();
            string   indexName = property.Name;
            string   fieldName = string.IsNullOrEmpty(config.Name) ? property.Name : config.Name;
            //if (!Regex.IsMatch(fieldName, _fieldRegex, RegexOptions.IgnoreCase)) {
            //    throw new LightDataException(string.Format(SR.FieldNameIsInvalid, type, fieldName));
            //}

            DataFieldMapping fieldMapping;

            if (typeInfo.IsGenericType)
            {
                Type frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    Type[] arguments = typeInfo.GetGenericArguments();
                    type     = arguments[0];
                    typeInfo = type.GetTypeInfo();
                }
            }
            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    BytesFieldMapping bytesFieldMapping = new BytesFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType);
                    fieldMapping = bytesFieldMapping;
                }
                else
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType, property.Name, type));
                }
            }
            else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType, property.Name, type));
            }
            else if (typeInfo.IsEnum)
            {
                EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType, config.DefaultValue);
                fieldMapping = enumFieldMapping;
            }
            else
            {
                TypeCode code = Type.GetTypeCode(type);
                if (code == TypeCode.Empty)
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportFieldType, property.DeclaringType, property.Name, type));
                }
                else if (code == TypeCode.Object)
                {
                    ObjectFieldMapping objectFieldMapping = new ObjectFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType);
                    fieldMapping = objectFieldMapping;
                }
                else
                {
                    PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, config.IsNullable, config.DbType, config.DefaultValue, config.IsIdentity, config.IsPrimaryKey);
                    fieldMapping = primitiveFieldMapping;
                }
            }
            if (config.DataOrder > 0)
            {
                fieldMapping._dataOrder = config.DataOrder;
            }
            fieldMapping._positionOrder   = positionOrder;
            fieldMapping._functionControl = config.FunctionControl == FunctionControl.Default ? FunctionControl.Full : config.FunctionControl;
            fieldMapping._handler         = new PropertyHandler(property);
            return(fieldMapping);
        }