/// <summary>
        /// Builds the options.
        /// </summary>
        /// <param name="options">Options.</param>
        protected void BuildOptions(DataContextOptions options)
        {
            if (_func == null)
            {
                throw new LightDataException(SR.DataContextOptionsError);
            }
            var paramSet = new ConfigParamSet();

            foreach (var item in _dict)
            {
                if (item.Value != null)
                {
                    paramSet.SetParamValue(item.Key, item.Value);
                }
            }
            var database = _func(Guid.NewGuid().ToString("N"), paramSet);

            if (database == null)
            {
                throw new LightDataException(SR.DataContextOptionsError);
            }
            options.Connection    = _connection;
            options.Database      = database;
            options.CommandOutput = _commandOutput;
        }
Ejemplo n.º 2
0
 private void Internal_DataContextConfiguration(LightDataOptions optionList)
 {
     if (optionList != null && optionList.Connections != null && optionList.Connections.Length > 0)
     {
         foreach (var connection in optionList.Connections)
         {
             var setting = new ConnectionSetting()
             {
                 Name             = connection.Name,
                 ConnectionString = connection.ConnectionString,
                 ProviderName     = connection.ProviderName
             };
             var configParam = new ConfigParamSet();
             if (connection.ConfigParams != null && connection.ConfigParams.Count > 0)
             {
                 foreach (var param in connection.ConfigParams)
                 {
                     configParam.SetParamValue(param.Key, param.Value);
                 }
             }
             setting.ConfigParam = configParam;
             var options = DataContextOptions.CreateOptions(setting);
             if (options != null)
             {
                 if (defaultOptions == null)
                 {
                     defaultOptions = options;
                 }
                 optionsDict.Add(setting.Name, options);
             }
         }
     }
 }
Ejemplo n.º 3
0
        public static DataTableMapperConfig LoadDataTableConfig(Type type)
        {
            if (DataMapperConfiguration.TryGetSetting(type, out var setting))
            {
                return(setting.DataTableMapConfig);
            }

            var attributes = AttributeCore.GetTypeAttributes <DataTableAttribute>(type, true);

            if (attributes.Length > 0)
            {
                var attribute       = attributes[0];
                var paramAttributes = AttributeCore.GetTypeAttributes <ConfigParamAttribute>(type, true);
                var configParam     = new ConfigParamSet();
                if (paramAttributes != null && paramAttributes.Length > 0)
                {
                    foreach (var extendAttribute in paramAttributes)
                    {
                        configParam.SetParamValue(extendAttribute.Name, extendAttribute.Value);
                    }
                }
                var config = new DataTableMapperConfig(type)
                {
                    TableName     = attribute.TableName,
                    IsEntityTable = attribute.IsEntityTable,
                    ConfigParams  = configParam
                };
                return(config);
            }

            return(null);
        }
Ejemplo n.º 4
0
        protected DatabaseProvider(string configName, ConfigParamSet configParams)
        {
            ConfigName = configName;
            var batchInsertCount = configParams.GetParamValue("batchInsertCount");

            if (batchInsertCount != null)
            {
                if (int.TryParse(batchInsertCount, out var value) && value > 0)
                {
                    _batchInsertCount = value;
                }
            }

            var batchUpdateCount = configParams.GetParamValue("batchUpdateCount");

            if (batchUpdateCount != null)
            {
                if (int.TryParse(batchUpdateCount, out var value) && value > 0)
                {
                    _batchUpdateCount = value;
                }
            }

            var batchDeleteCount = configParams.GetParamValue("batchDeleteCount");

            if (batchDeleteCount != null)
            {
                if (int.TryParse(batchDeleteCount, out var value) && value > 0)
                {
                    _batchDeleteCount = value;
                }
            }

            var timeout = configParams.GetParamValue("timeout");

            if (timeout != null)
            {
                if (int.TryParse(batchInsertCount, out var value) && value > 0)
                {
                    _commandTimeout = value;
                }
            }
        }
Ejemplo n.º 5
0
        static void LoadData(string configFilePath)
        {
            FileInfo fileInfo;

            if (UseEntryAssemblyDirectory)
            {
                fileInfo = FileHelper.GetFileInfo(configFilePath, out bool absolute);
                if (!fileInfo.Exists && !absolute)
                {
                    fileInfo = new FileInfo(configFilePath);
                }
            }
            else
            {
                fileInfo = new FileInfo(configFilePath);
            }
            if (fileInfo.Exists)
            {
                using (StreamReader reader = fileInfo.OpenText()) {
                    string  content = reader.ReadToEnd();
                    JObject dom     = JObject.Parse(content);
                    var     section = dom.GetValue("lightDataMapper");
                    if (section == null)
                    {
                        return;
                    }
                    var optionList = section.ToObject <LightMapperOptions>();
                    if (optionList != null && optionList.DataTypes != null && optionList.DataTypes.Length > 0)
                    {
                        int typeIndex = 0;
                        foreach (DataTypeSection typeConfig in optionList.DataTypes)
                        {
                            typeIndex++;
                            var typeName = typeConfig.Type;
                            if (typeName == null)
                            {
                                throw new LightDataException(string.Format(SR.ConfigDataTypeNameIsNull, typeIndex));
                            }
                            var dataType     = Type.GetType(typeName, true);
                            var dataTypeInfo = dataType.GetTypeInfo();
                            var dataTableMap = new DataTableMapperConfig(dataType);
                            var setting      = new DataTableMapperSetting(dataTableMap);

                            dataTableMap.TableName     = typeConfig.TableName;
                            dataTableMap.IsEntityTable = typeConfig.IsEntityTable.HasValue ? typeConfig.IsEntityTable.Value : true;
                            var configParam  = new ConfigParamSet();
                            var paramConfigs = typeConfig.ConfigParams;
                            if (paramConfigs != null && paramConfigs.Count > 0)
                            {
                                foreach (var paramConfig in paramConfigs)
                                {
                                    configParam.SetParamValue(paramConfig.Value, paramConfig.Value);
                                }
                            }
                            dataTableMap.ConfigParams = configParam;
                            var dataFieldConfigs = typeConfig.DataFields;

                            if (dataFieldConfigs != null && dataFieldConfigs.Length > 0)
                            {
                                int fieldIndex = 0;
                                foreach (var fieldConfig in dataFieldConfigs)
                                {
                                    fieldIndex++;
                                    var fieldName = fieldConfig.FieldName;
                                    if (fieldName == null)
                                    {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldNameIsNull, typeName, fieldIndex));
                                    }
                                    var property = dataTypeInfo.GetProperty(fieldName);
                                    if (property == null)
                                    {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldIsNotExists, typeName, fieldName));
                                    }

                                    object defaultValue;
                                    try {
                                        defaultValue = CreateDefaultValue(property.PropertyType, fieldConfig.DefaultValue);
                                    }
                                    catch (Exception ex) {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldLoadError, typeName, fieldName, ex.Message));
                                    }
                                    FunctionControl functionControl;
                                    try {
                                        functionControl = CreateFunctionControl(fieldConfig);
                                    }
                                    catch (Exception ex) {
                                        throw new LightDataException(string.Format(SR.ConfigDataFieldLoadError, typeName, fieldName, ex.Message));
                                    }
                                    var dataFieldMap = new DataFieldMapperConfig(fieldName)
                                    {
                                        Name            = fieldConfig.Name,
                                        IsPrimaryKey    = fieldConfig.IsPrimaryKey,
                                        IsIdentity      = fieldConfig.IsIdentity,
                                        DbType          = fieldConfig.DbType,
                                        DataOrder       = fieldConfig.DataOrder,
                                        IsNullable      = fieldConfig.IsNullable,
                                        DefaultValue    = defaultValue,
                                        FunctionControl = functionControl
                                    };
                                    setting.AddDataFieldMapConfig(fieldName, dataFieldMap);
                                }
                            }
                            var relationFieldConfigs = typeConfig.RelationFields;
                            if (relationFieldConfigs != null && relationFieldConfigs.Length > 0)
                            {
                                int fieldIndex = 0;
                                foreach (var fieldConfig in relationFieldConfigs)
                                {
                                    fieldIndex++;
                                    if (fieldConfig.RelationPairs != null && fieldConfig.RelationPairs.Length > 0)
                                    {
                                        var fieldName = fieldConfig.FieldName;
                                        if (fieldName == null)
                                        {
                                            throw new LightDataException(string.Format(SR.ConfigDataFieldNameIsNull, typeName, fieldIndex));
                                        }
                                        var property = dataTypeInfo.GetProperty(fieldName);
                                        if (property == null)
                                        {
                                            throw new LightDataException(string.Format(SR.ConfigDataFieldIsNotExists, typeName, fieldName));
                                        }
                                        var dataFieldMap = new RelationFieldMapConfig(fieldName);
                                        foreach (var pair in fieldConfig.RelationPairs)
                                        {
                                            dataFieldMap.AddRelationKeys(pair.MasterKey, pair.RelateKey);
                                        }
                                        setting.AddRelationFieldMapConfig(fieldName, dataFieldMap);
                                    }
                                }
                            }
                            settingDict[dataType] = setting;
                        }
                    }
                }
            }
        }