Ejemplo n.º 1
0
        private static void InitVars(TableDefinition table, SimpleDataColumn[] fields)
        {
            int num = 0;

            foreach (VariableDefinition variable in table.Variables)
            {
                IDataField dataField;
                if (!table.MainDataHolder.Fields.TryGetValue(variable.Name, out dataField) ||
                    !(dataField is IFlatDataFieldAccessor))
                {
                    throw new DataHolderException(
                              "Table \"{0}\" defined invalid Variable {1}. Name does not refer to an actual property within DataHolder {2}.",
                              (object)table, (object)variable, (object)table.MainDataHolder);
                }
                try
                {
                    object defaultValue = variable.Eval(dataField.MappedMember.GetVariableType());
                    fields[num++] = new SimpleDataColumn(variable.Name, defaultValue)
                    {
                        FieldList =
                        {
                            (IFlatDataFieldAccessor)dataField
                        }
                    };
                }
                catch (Exception ex)
                {
                    throw new DataHolderException(ex,
                                                  "Unable to parse default-value \"{0}\" to Type \"{1}\" from Variable \"{2}\"",
                                                  (object)variable.StringValue, (object)dataField.MappedMember.Name, (object)variable.Name);
                }
            }
        }
Ejemplo n.º 2
0
        private List <T> GetKeyValuePairs <T>(IDataHolder obj, Func <TableDefinition, T> listCreator)
            where T : KeyValueListBase
        {
            DataHolderDefinition holderDefinition = this.m_mapping.GetDataHolderDefinition(obj.GetType());
            List <T>             source           = new List <T>(this.m_mapping.TableDefinitions.Length);

            for (int index1 = 0; index1 < this.m_mapping.TableDefinitions.Length; ++index1)
            {
                TableDefinition table = this.m_mapping.TableDefinitions[index1];
                for (int index2 = 0; index2 < table.ColumnDefinitions.Length; ++index2)
                {
                    SimpleDataColumn columnDefinition = table.ColumnDefinitions[index2];
                    for (int index3 = 0; index3 < columnDefinition.FieldList.Count; ++index3)
                    {
                        IFlatDataFieldAccessor field = columnDefinition.FieldList[index3];
                        if (field.DataHolderDefinition == holderDefinition)
                        {
                            T obj1 = source.FirstOrDefault <T>((Func <T, bool>)(l => l.TableName == table.Name));
                            if ((object)obj1 == null)
                            {
                                source.Add(obj1 = listCreator(table));
                            }
                            object obj2 = field.Get(obj);
                            obj1.AddPair(columnDefinition.ColumnName, (object)obj2.ToString());
                        }
                    }
                }
            }

            return(source);
        }
Ejemplo n.º 3
0
        private static void InitVars(TableDefinition table, SimpleDataColumn[] fields)
        {
            var i = 0;

            foreach (var variable in table.Variables)
            {
                IDataField field;
                if (!table.MainDataHolder.Fields.TryGetValue(variable.Name, out field) || !(field is IFlatDataFieldAccessor))
                {
                    throw new DataHolderException("Table \"{0}\" defined invalid Variable {1}. Name does not refer to an actual property within DataHolder {2}.",
                                                  table, variable, table.MainDataHolder);
                }

                try
                {
                    var defaultValue = variable.Eval(field.MappedMember.GetVariableType());
                    var fld          = new SimpleDataColumn(variable.Name, defaultValue);
                    fld.FieldList.Add((IFlatDataFieldAccessor)field);
                    fields[i++] = fld;
                }
                catch (Exception e)
                {
                    throw new DataHolderException(e, "Unable to parse default-value \"{0}\" to Type \"{1}\" from Variable \"{2}\"",
                                                  variable.StringValue, field.MappedMember.Name, variable.Name);
                }
            }
        }
Ejemplo n.º 4
0
        static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
                               Dictionary <string, List <SimpleDataColumn> > mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
        {
            var column = fieldDef.Column;

            var    tables = defs.EnsureTables(fieldDef.Table, defaultTables);
            object defaultValue;

            if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
            {
                defaultValue = StringParser.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
            }
            else
            {
                if (String.IsNullOrEmpty(column))
                {
                    return;
                }
                defaultValue = null;
            }

            foreach (var table in tables)
            {
                var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
                if (!dataHolders.Contains(accessor.DataHolderDefinition))
                {
                    dataHolders.Add(accessor.DataHolderDefinition);
                }

                var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

                SimpleDataColumn dataColumn;
                var dbAttrs = member.GetCustomAttributes <DBAttribute>();
                var attr    = dbAttrs.Where(attribute => attribute is PersistentAttribute).FirstOrDefault() as PersistentAttribute;

                if (String.IsNullOrEmpty(column))
                {
                    // use default value
                    mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
                }
                else
                {
                    dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
                    if (dataColumn == null)
                    {
                        var type = member.GetActualType();
                        if (attr != null)
                        {
                            type = attr.ReadType ?? type;
                        }
                        var reader = Converters.GetReader(type);
                        mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, reader));
                    }
                }

                dataColumn.FieldList.Add(accessor);
            }
        }
Ejemplo n.º 5
0
        public object GetPrimaryId(IDataReader rs)
        {
            if (this.m_singlePrimaryCol)
            {
                return(this.m_primaryColumns[0].DataColumn.ReadValue(rs));
            }
            int length = ((IEnumerable <PrimaryColumn>) this.m_primaryColumns).Count <PrimaryColumn>();

            object[] objArray = new object[length];
            for (int index = 0; index < length; ++index)
            {
                SimpleDataColumn dataColumn = this.m_primaryColumns[index].DataColumn;
                objArray[index] = dataColumn.ReadValue(rs);
            }

            return((object)objArray);
        }
Ejemplo n.º 6
0
        public object GetPrimaryId(IDataReader rs)
        {
            if (m_singlePrimaryCol)
            {
                return(m_primaryColumns[0].DataColumn.ReadValue(rs));
            }
            int length = m_primaryColumns.Count();

            object[] objArray = new object[length];
            for (int index = 0; index < length; ++index)
            {
                SimpleDataColumn dataColumn = m_primaryColumns[index].DataColumn;
                objArray[index] = dataColumn.ReadValue(rs);
            }

            return(objArray);
        }
Ejemplo n.º 7
0
        private void FinishLoading(Dictionary<string, List<SimpleDataColumn>> fieldMap)
        {
            // bind mapping-objects to tables
            foreach (var pair in fieldMap)
            {
                var table = GetTable(pair.Key);
                var fields = pair.Value.ToArray();

                if (!string.IsNullOrEmpty(table.MainDataHolderName))
                {
                    DataHolderDefinition defaultDef = DataHolderDefinitions.Where(dataHolderDef =>
                        dataHolderDef.Name.Equals(table.MainDataHolderName,
                        StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

                    if (defaultDef == null)
                    {
                        throw new DataHolderException("Table \"{0}\" refered to invalid MainDataHolder: {1}", table,
                                                      table.MainDataHolderName);
                    }
                    table.SetMainDataHolder(defaultDef, false);
                }

                try
                {
                    var primCols = new List<PrimaryColumn>();
                    primCols.AddRange(table.PrimaryColumns);

                    // find Primary Columns, set defaults and consider depending fields
                    for (var i = 0; i < fields.Length; i++)
                    {
                        var field = fields[i];
                        var col = primCols.Where(primCol => primCol.Name == field.ColumnName).FirstOrDefault();
                        if (col != null)
                        {
                            col.DataColumn = field;
                            field.IsPrimaryKey = true;
                            primCols.Remove(col);
                            //var tmp = fields[0];
                            //fields[0] = field;
                            //fields[i] = tmp;
                        }

                        if (field.ColumnName == null && field.DefaultValue == null)
                        {
                            LightDBMgr.OnInvalidData("Field-definition \"{0}\" did not define a Column nor a DefaultValue.", field);
                        }
                    }

                    // put the depending Fields first (if any)
                    for (var i = 0; i < fields.Length; i++)
                    {
                        var field = fields[i];
                        for (var j = 0; j < field.FieldList.Count; j++)
                        {
                            var dataField = field.FieldList[j];
                            var dataHolder = dataField.DataHolderDefinition;
                            if (dataHolder.DependingField == dataField)
                            {
                                var tmp = fields[0];
                                fields[0] = field;
                                fields[i] = tmp;
                            }
                        }
                    }

                    //if (primCols.Count() > 0)
                    //{
                    //    throw new DataHolderException("Table-definition \"{0}\" refered to non-existing PrimaryKey(s): {1}",
                    //        pair.Key, primCols.ToString(", "));
                    //}

                    if (primCols.Count > 0 && table.MainDataHolder == null)
                    {
                        throw new DataHolderException("Table \"{0}\" referenced PrimaryColumn(s) ({1}) but did not define a MainDataHolder explicitely.",
                            table, primCols.ToString(", "));
                    }

                    // custom primary columns and variables
                    if (primCols.Count > 0 || (table.Variables != null && table.Variables.Length > 0))
                    {
                        var varCount = table.Variables != null ? table.Variables.Length : 0;
                        var extraFields = varCount + primCols.Count;
                        var tmpArr = new SimpleDataColumn[fields.Length + extraFields];
                        Array.Copy(fields, 0, tmpArr, extraFields, fields.Length);
                        fields = tmpArr;

                        if (varCount > 0)
                        {
                            InitVars(table, fields);
                        }

                        var i = varCount;
                        foreach (var primaryCol in primCols)
                        {
                            // PrimaryKey-field is not explicitely mapped but only used as a reference
                            var proxyField = new DataFieldProxy(primaryCol.Name, table.MainDataHolder);

                            var reader = Converters.GetReader(primaryCol.TypeName);
                            if (reader == null)
                            {
                                throw new DataHolderException("Invalid Type \"" + primaryCol.TypeName
                                                              + "\" for PrimaryColumn \"" + primaryCol.Name + "\" in definition for Table: " + table + " - " +
                                                              "You must explicitely define the TypeName attribute inside the PrimaryColumn node, if it is only an additional table for a DataHolder!");
                            }
                            var col = new SimpleDataColumn(primaryCol.Name, reader, 0) { IsPrimaryKey = true };
                            col.FieldList.Add(proxyField);
                            fields[i++] = primaryCol.DataColumn = col;
                        }
                    }

                    var index = 0;
                    for (var i = 0; i < fields.Length; i++)
                    {
                        var field = fields[i];

                        if (field.DefaultValue == null)
                        {
                            field.m_index = index++;
                        }
                        else
                        {
                            field.Reader = null;
                        }
                    }

                    TableDefinitionMap[pair.Key].ColumnDefinitions = fields;
                }
                catch (Exception ex)
                {
                    throw new DataHolderException(ex, "Unable to setup Table \"{0}\" (MainDataHolder: \"{1}\")", table, table.MainDataHolder);
                }
            }

            m_mappings = CreateDataHolderTableMappings(m_tableDataHolderMap, DataHolderDefinitions);
        }
Ejemplo n.º 8
0
        private static void InitVars(TableDefinition table, SimpleDataColumn[] fields)
        {
            var i = 0;
            foreach (var variable in table.Variables)
            {
                IDataField field;
                if (!table.MainDataHolder.Fields.TryGetValue(variable.Name, out field) || !(field is IFlatDataFieldAccessor))
                {
                    throw new DataHolderException("Table \"{0}\" defined invalid Variable {1}. Name does not refer to an actual property within DataHolder {2}.",
                        table, variable, table.MainDataHolder);
                }

                try
                {
                    var defaultValue = variable.Eval(field.MappedMember.GetVariableType());
                    var fld = new SimpleDataColumn(variable.Name, defaultValue);
                    fld.FieldList.Add((IFlatDataFieldAccessor)field);
                    fields[i++] = fld;
                }
                catch (Exception e)
                {
                    throw new DataHolderException(e, "Unable to parse default-value \"{0}\" to Type \"{1}\" from Variable \"{2}\"",
                                                  variable.StringValue, field.MappedMember.Name, variable.Name);
                }
            }
        }
Ejemplo n.º 9
0
		static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
			Dictionary<string, List<SimpleDataColumn>> mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
		{
			var column = fieldDef.Column;

			var tables = defs.EnsureTables(fieldDef.Table, defaultTables);
			object defaultValue;
			if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
			{
				defaultValue = StringParser.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
			}
			else
			{
				if (String.IsNullOrEmpty(column))
				{
					return;
				}
				defaultValue = null;
			}

			foreach (var table in tables)
			{
				var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
				if (!dataHolders.Contains(accessor.DataHolderDefinition))
				{
					dataHolders.Add(accessor.DataHolderDefinition);
				}

				var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

				SimpleDataColumn dataColumn;
                var dbAttrs = member.GetCustomAttributes<DBAttribute>();
                var attr = dbAttrs.Where(attribute => attribute is PersistentAttribute).FirstOrDefault() as PersistentAttribute;

				if (String.IsNullOrEmpty(column))
				{
					// use default value
					mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
				}
				else
				{
					dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
					if (dataColumn == null)
					{
                        var type = member.GetActualType();
                        if (attr != null)
                            type = attr.ReadType ?? type;
                        var reader = Converters.GetReader(type);
                        mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, reader));
					}
				}

				dataColumn.FieldList.Add(accessor);
			}
		}
Ejemplo n.º 10
0
        /// <summary>
        /// Fetches all sets of objects, defined through the given mapping.
        /// </summary>
        /// <returns></returns>
        public void Fetch()
        {
            // Map of objects, mapped by DataHolder-Type, DataHolder-Id and field-Name
            //var objects = new Dictionary<Type, Dictionary<object, Dictionary<string, object>>>();
            var actions = new List <Action>(10000);
            var tables  = m_mapping.TableDefinitions;
            DataHolderDefinition holderDef = null;

            for (var t = 0; t < tables.Length; t++)
            {
                var table = tables[t];
                try
                {
                    using (var rs = m_db.CreateReader(table, t))
                    {
                        var mappedFields = table.ColumnDefinitions;
                        while (rs.Read())
                        {
                            var id = table.GetId(rs);

                            DataHolderDefinition lastDataHolderDef = null;
                            IDataHolder          lastDataHolder    = null;
                            object           value  = null;
                            SimpleDataColumn column = null;
                            int columnN;
                            try
                            {
                                for (columnN = 0; columnN < mappedFields.Length; columnN++)
                                {
                                    column = mappedFields[columnN];
                                    value  = null;
                                    value  = column.ReadValue(rs);
                                    for (var i = 0; i < column.FieldList.Count; i++)
                                    {
                                        var dataField = column.FieldList[i];
                                        holderDef = dataField.DataHolderDefinition;
                                        if (holderDef == null)
                                        {
                                            throw new DataHolderException(
                                                      "Invalid DataField did not have a DataHolderDefinition: " +
                                                      dataField);
                                        }

                                        IDataHolder holder;
                                        if (lastDataHolderDef != dataField.DataHolderDefinition || lastDataHolder == null)
                                        {
                                            // only get the DataHolder, if its different from the last one
                                            var dataHolders = dataHolderMap.GetOrCreate(holderDef.Type);
                                            if (!dataHolders.TryGetValue(id, out holder))
                                            {
                                                // create new DataHolder object if this is one of the DataHolder's default tables
                                                if (!table.IsDefaultTable)
                                                {
                                                    // ignore DataHolders that are not defined in their DefaultTables
                                                    LightDBMgr.OnInvalidData(
                                                        "Invalid DataHolder was not defined in its Default-Tables - " +
                                                        "DataHolder: {0}; Id(s): {1}; Table: {2}", holderDef, id, table);
                                                    // little trick to skip the parent loop
                                                    columnN = mappedFields.Length;
                                                    break;
                                                }

                                                // init with Id(s)
                                                dataHolders.Add(id, holder = (IDataHolder)holderDef.CreateHolder(value));
                                                //table.SetDefaults(id, holder);
                                                actions.Add(holder.FinalizeDataHolder);
                                            }

                                            lastDataHolder    = holder;
                                            lastDataHolderDef = holderDef;
                                        }
                                        else
                                        {
                                            holder = lastDataHolder;
                                        }

                                        // set value
                                        dataField.Set(holder, value);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                var ids = (id is Array ? ((object[])id) : new[] { id });
                                throw new DataHolderException(e, "Unable to parse data-cell (Column: {0}, Id(s): {1}{2})", column,
                                                              ids.ToString(", ", (idObj) => {
                                    return(idObj.GetType().IsEnum ? Convert.ChangeType(idObj, Enum.GetUnderlyingType(idObj.GetType())) : idObj);
                                }), value != null ? (", Value: " + value) : "");
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new DataHolderException(e, "Failed to read from Table \"{0}\" {1}", table,
                                                  holderDef != null ? ("DataHolder: " + holderDef) : "");
                }
            }

            for (var i = 0; i < actions.Count; i++)
            {
                var action = actions[i];
                action();
            }

            dataHolderMap.Clear();
            m_fetched = true;
        }
Ejemplo n.º 11
0
        private void FinishLoading(Dictionary <string, List <SimpleDataColumn> > fieldMap)
        {
            foreach (KeyValuePair <string, List <SimpleDataColumn> > field1 in fieldMap)
            {
                TableDefinition    table  = GetTable(field1.Key);
                SimpleDataColumn[] fields = field1.Value.ToArray();
                if (!string.IsNullOrEmpty(table.MainDataHolderName))
                {
                    DataHolderDefinition dataDef = DataHolderDefinitions
                                                   .Where(dataHolderDef =>
                                                          dataHolderDef.Name.Equals(table.MainDataHolderName,
                                                                                    StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (dataDef == null)
                    {
                        throw new DataHolderException("Table \"{0}\" refered to invalid MainDataHolder: {1}", (object)table,
                                                      (object)table.MainDataHolderName);
                    }
                    table.SetMainDataHolder(dataDef, false);
                }

                try
                {
                    List <PrimaryColumn> primaryColumnList = new List <PrimaryColumn>();
                    primaryColumnList.AddRange(table.PrimaryColumns);
                    for (int index = 0; index < fields.Length; ++index)
                    {
                        SimpleDataColumn field         = fields[index];
                        PrimaryColumn    primaryColumn = primaryColumnList
                                                         .Where(
                            primCol => primCol.Name == field.ColumnName)
                                                         .FirstOrDefault();
                        if (primaryColumn != null)
                        {
                            primaryColumn.DataColumn = field;
                            field.IsPrimaryKey       = true;
                            primaryColumnList.Remove(primaryColumn);
                        }

                        if (field.ColumnName == null && field.DefaultValue == null)
                        {
                            LightDBMgr.OnInvalidData(
                                "Field-definition \"{0}\" did not define a Column nor a DefaultValue.", (object)field);
                        }
                    }

                    for (int index1 = 0; index1 < fields.Length; ++index1)
                    {
                        SimpleDataColumn simpleDataColumn1 = fields[index1];
                        for (int index2 = 0; index2 < simpleDataColumn1.FieldList.Count; ++index2)
                        {
                            IFlatDataFieldAccessor field2 = simpleDataColumn1.FieldList[index2];
                            if (field2.DataHolderDefinition.DependingField == field2)
                            {
                                SimpleDataColumn simpleDataColumn2 = fields[0];
                                fields[0]      = simpleDataColumn1;
                                fields[index1] = simpleDataColumn2;
                            }
                        }
                    }

                    if (primaryColumnList.Count > 0 && table.MainDataHolder == null)
                    {
                        throw new DataHolderException(
                                  "Table \"{0}\" referenced PrimaryColumn(s) ({1}) but did not define a MainDataHolder explicitely.",
                                  (object)table, (object)primaryColumnList.ToString(", "));
                    }
                    if (primaryColumnList.Count > 0 || table.Variables != null && table.Variables.Length > 0)
                    {
                        int num1             = table.Variables != null ? table.Variables.Length : 0;
                        int destinationIndex = num1 + primaryColumnList.Count;
                        SimpleDataColumn[] simpleDataColumnArray =
                            new SimpleDataColumn[fields.Length + destinationIndex];
                        Array.Copy(fields, 0, simpleDataColumnArray, destinationIndex, fields.Length);
                        fields = simpleDataColumnArray;
                        if (num1 > 0)
                        {
                            InitVars(table, fields);
                        }
                        int num2 = num1;
                        foreach (PrimaryColumn primaryColumn in primaryColumnList)
                        {
                            DataFieldProxy dataFieldProxy =
                                new DataFieldProxy(primaryColumn.Name, table.MainDataHolder);
                            IFieldReader reader = Converters.GetReader(primaryColumn.TypeName);
                            if (reader == null)
                            {
                                throw new DataHolderException(
                                          "Invalid Type \"" + primaryColumn.TypeName + "\" for PrimaryColumn \"" +
                                          primaryColumn.Name + "\" in definition for Table: " + table +
                                          " - You must explicitely define the TypeName attribute inside the PrimaryColumn node, if it is only an additional table for a DataHolder!");
                            }
                            SimpleDataColumn simpleDataColumn = new SimpleDataColumn(primaryColumn.Name, reader, 0)
                            {
                                IsPrimaryKey = true
                            };
                            simpleDataColumn.FieldList.Add(dataFieldProxy);
                            fields[num2++] = primaryColumn.DataColumn = simpleDataColumn;
                        }
                    }

                    int num = 0;
                    for (int index = 0; index < fields.Length; ++index)
                    {
                        SimpleDataColumn simpleDataColumn = fields[index];
                        if (simpleDataColumn.DefaultValue == null)
                        {
                            simpleDataColumn.m_index = num++;
                        }
                        else
                        {
                            simpleDataColumn.Reader = null;
                        }
                    }

                    TableDefinitionMap[field1.Key].ColumnDefinitions = fields;
                }
                catch (Exception ex)
                {
                    throw new DataHolderException(ex, "Unable to setup Table \"{0}\" (MainDataHolder: \"{1}\")", (object)table,
                                                  (object)table.MainDataHolder);
                }
            }

            m_mappings =
                CreateDataHolderTableMappings(m_tableDataHolderMap,
                                              DataHolderDefinitions);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Fetches all sets of objects, defined through the given mapping.
        /// </summary>
        /// <returns></returns>
        public void Fetch()
        {
            List <Action> actionList = new List <Action>(10000);

            TableDefinition[]    tableDefinitions  = this.m_mapping.TableDefinitions;
            DataHolderDefinition holderDefinition1 = (DataHolderDefinition)null;

            for (int tableIndex = 0; tableIndex < tableDefinitions.Length; ++tableIndex)
            {
                TableDefinition def = tableDefinitions[tableIndex];
                try
                {
                    using (IDataReader reader = this.m_db.CreateReader(def, tableIndex))
                    {
                        SimpleDataColumn[] columnDefinitions = def.ColumnDefinitions;
                        while (reader.Read())
                        {
                            object key = def.GetId(reader);
                            DataHolderDefinition holderDefinition2 = (DataHolderDefinition)null;
                            IDataHolder          dataHolder1       = (IDataHolder)null;
                            object           firstValue            = (object)null;
                            SimpleDataColumn simpleDataColumn      = (SimpleDataColumn)null;
                            try
                            {
                                for (int index1 = 0; index1 < columnDefinitions.Length; ++index1)
                                {
                                    simpleDataColumn = columnDefinitions[index1];
                                    firstValue       = (object)null;
                                    firstValue       = simpleDataColumn.ReadValue(reader);
                                    for (int index2 = 0; index2 < simpleDataColumn.FieldList.Count; ++index2)
                                    {
                                        IFlatDataFieldAccessor field = simpleDataColumn.FieldList[index2];
                                        holderDefinition1 = field.DataHolderDefinition;
                                        if (holderDefinition1 == null)
                                        {
                                            throw new DataHolderException(
                                                      "Invalid DataField did not have a DataHolderDefinition: " +
                                                      (object)field, new object[0]);
                                        }
                                        IDataHolder dataHolder2;
                                        if (holderDefinition2 != field.DataHolderDefinition || dataHolder1 == null)
                                        {
                                            Dictionary <object, IDataHolder> dictionary =
                                                this.dataHolderMap.GetOrCreate <Type, object, IDataHolder>(
                                                    holderDefinition1.Type);
                                            if (!dictionary.TryGetValue(key, out dataHolder2))
                                            {
                                                if (!def.IsDefaultTable)
                                                {
                                                    LightDBMgr.OnInvalidData(
                                                        "Invalid DataHolder was not defined in its Default-Tables - DataHolder: {0}; Id(s): {1}; Table: {2}",
                                                        (object)holderDefinition1, key, (object)def);
                                                    index1 = columnDefinitions.Length;
                                                    break;
                                                }

                                                dictionary.Add(key,
                                                               dataHolder2 =
                                                                   (IDataHolder)holderDefinition1.CreateHolder(firstValue));
                                                actionList.Add(new Action(dataHolder2.FinalizeDataHolder));
                                            }

                                            dataHolder1       = dataHolder2;
                                            holderDefinition2 = holderDefinition1;
                                        }
                                        else
                                        {
                                            dataHolder2 = dataHolder1;
                                        }

                                        field.Set(dataHolder2, firstValue);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                object[] objArray1;
                                if (!(key is Array))
                                {
                                    objArray1 = new object[1] {
                                        key
                                    }
                                }
                                ;
                                else
                                {
                                    objArray1 = (object[])key;
                                }
                                object[] objArray2 = objArray1;
                                throw new DataHolderException(ex,
                                                              "Unable to parse data-cell (Column: {0}, Id(s): {1}{2})", new object[3]
                                {
                                    (object)simpleDataColumn,
                                    (object)((IEnumerable <object>)objArray2).ToString <object>(", ",
                                                                                                (Func <object, object>)(idObj =>
                                                                                                                        idObj.GetType().IsEnum
                                                    ? Convert.ChangeType(idObj, Enum.GetUnderlyingType(idObj.GetType()))
                                                    : idObj)),
                                    firstValue != null ? (object)(", Value: " + firstValue) : (object)""
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new DataHolderException(ex, "Failed to read from Table \"{0}\" {1}", new object[2]
                    {
                        (object)def,
                        holderDefinition1 != null ? (object)("DataHolder: " + (object)holderDefinition1) : (object)""
                    });
                }
            }

            for (int index = 0; index < actionList.Count; ++index)
            {
                actionList[index]();
            }
            this.dataHolderMap.Clear();
            this.m_fetched = true;
        }
Ejemplo n.º 13
0
        private static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables,
                                       SimpleFlatFieldDefinition fieldDef, Dictionary <string, List <SimpleDataColumn> > mappedFields,
                                       IFlatDataFieldAccessor accessor, MemberInfo member)
        {
            string column = fieldDef.Column;

            TableDefinition[] tableDefinitionArray = defs.EnsureTables(fieldDef.Table, defaultTables);
            object            defaultValue;

            if (!string.IsNullOrEmpty(fieldDef.DefaultStringValue))
            {
                defaultValue = StringParser.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
            }
            else
            {
                if (string.IsNullOrEmpty(column))
                {
                    return;
                }
                defaultValue = (object)null;
            }

            foreach (TableDefinition key in tableDefinitionArray)
            {
                List <DataHolderDefinition> holderDefinitionList =
                    defs.m_tableDataHolderMap.GetOrCreate <TableDefinition, DataHolderDefinition>(key);
                if (!holderDefinitionList.Contains(accessor.DataHolderDefinition))
                {
                    holderDefinitionList.Add(accessor.DataHolderDefinition);
                }
                List <SimpleDataColumn> simpleDataColumnList =
                    mappedFields.GetOrCreate <string, SimpleDataColumn>(key.Name);
                PersistentAttribute persistentAttribute =
                    ((IEnumerable <DBAttribute>)member.GetCustomAttributes <DBAttribute>())
                    .Where <DBAttribute>((Func <DBAttribute, bool>)(attribute => attribute is PersistentAttribute))
                    .FirstOrDefault <DBAttribute>() as PersistentAttribute;
                SimpleDataColumn simpleDataColumn;
                if (string.IsNullOrEmpty(column))
                {
                    simpleDataColumnList.Add(simpleDataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
                }
                else
                {
                    simpleDataColumn =
                        simpleDataColumnList.Find(
                            (Predicate <SimpleDataColumn>)(cmpField => cmpField.ColumnName == column));
                    if (simpleDataColumn == null)
                    {
                        Type type1 = member.GetActualType();
                        if (persistentAttribute != null)
                        {
                            Type type2 = persistentAttribute.ReadType;
                            if ((object)type2 == null)
                            {
                                type2 = type1;
                            }
                            type1 = type2;
                        }

                        IFieldReader reader = Converters.GetReader(type1);
                        simpleDataColumnList.Add(simpleDataColumn = new SimpleDataColumn(column, reader));
                    }
                }

                simpleDataColumn.FieldList.Add(accessor);
            }
        }
Ejemplo n.º 14
0
        private void FinishLoading(Dictionary <string, List <SimpleDataColumn> > fieldMap)
        {
            // bind mapping-objects to tables
            foreach (var pair in fieldMap)
            {
                var table  = GetTable(pair.Key);
                var fields = pair.Value.ToArray();

                if (!string.IsNullOrEmpty(table.MainDataHolderName))
                {
                    DataHolderDefinition defaultDef = DataHolderDefinitions.Where(dataHolderDef =>
                                                                                  dataHolderDef.Name.Equals(table.MainDataHolderName,
                                                                                                            StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

                    if (defaultDef == null)
                    {
                        throw new DataHolderException("Table \"{0}\" refered to invalid MainDataHolder: {1}", table,
                                                      table.MainDataHolderName);
                    }
                    table.SetMainDataHolder(defaultDef, false);
                }

                try
                {
                    var primCols = new List <PrimaryColumn>();
                    primCols.AddRange(table.PrimaryColumns);

                    // find Primary Columns, set defaults and consider depending fields
                    for (var i = 0; i < fields.Length; i++)
                    {
                        var field = fields[i];
                        var col   = primCols.Where(primCol => primCol.Name == field.ColumnName).FirstOrDefault();
                        if (col != null)
                        {
                            col.DataColumn     = field;
                            field.IsPrimaryKey = true;
                            primCols.Remove(col);
                            //var tmp = fields[0];
                            //fields[0] = field;
                            //fields[i] = tmp;
                        }

                        if (field.ColumnName == null && field.DefaultValue == null)
                        {
                            LightDBMgr.OnInvalidData("Field-definition \"{0}\" did not define a Column nor a DefaultValue.", field);
                        }
                    }

                    // put the depending Fields first (if any)
                    for (var i = 0; i < fields.Length; i++)
                    {
                        var field = fields[i];
                        for (var j = 0; j < field.FieldList.Count; j++)
                        {
                            var dataField  = field.FieldList[j];
                            var dataHolder = dataField.DataHolderDefinition;
                            if (dataHolder.DependingField == dataField)
                            {
                                var tmp = fields[0];
                                fields[0] = field;
                                fields[i] = tmp;
                            }
                        }
                    }

                    //if (primCols.Count() > 0)
                    //{
                    //    throw new DataHolderException("Table-definition \"{0}\" refered to non-existing PrimaryKey(s): {1}",
                    //        pair.Key, primCols.ToString(", "));
                    //}

                    if (primCols.Count > 0 && table.MainDataHolder == null)
                    {
                        throw new DataHolderException("Table \"{0}\" referenced PrimaryColumn(s) ({1}) but did not define a MainDataHolder explicitely.",
                                                      table, primCols.ToString(", "));
                    }

                    // custom primary columns and variables
                    if (primCols.Count > 0 || (table.Variables != null && table.Variables.Length > 0))
                    {
                        var varCount    = table.Variables != null ? table.Variables.Length : 0;
                        var extraFields = varCount + primCols.Count;
                        var tmpArr      = new SimpleDataColumn[fields.Length + extraFields];
                        Array.Copy(fields, 0, tmpArr, extraFields, fields.Length);
                        fields = tmpArr;

                        if (varCount > 0)
                        {
                            InitVars(table, fields);
                        }

                        var i = varCount;
                        foreach (var primaryCol in primCols)
                        {
                            // PrimaryKey-field is not explicitely mapped but only used as a reference
                            var proxyField = new DataFieldProxy(primaryCol.Name, table.MainDataHolder);

                            var reader = Converters.GetReader(primaryCol.TypeName);
                            if (reader == null)
                            {
                                throw new DataHolderException("Invalid Type \"" + primaryCol.TypeName
                                                              + "\" for PrimaryColumn \"" + primaryCol.Name + "\" in definition for Table: " + table + " - " +
                                                              "You must explicitely define the TypeName attribute inside the PrimaryColumn node, if it is only an additional table for a DataHolder!");
                            }
                            var col = new SimpleDataColumn(primaryCol.Name, reader, 0)
                            {
                                IsPrimaryKey = true
                            };
                            col.FieldList.Add(proxyField);
                            fields[i++] = primaryCol.DataColumn = col;
                        }
                    }

                    var index = 0;
                    for (var i = 0; i < fields.Length; i++)
                    {
                        var field = fields[i];

                        if (field.DefaultValue == null)
                        {
                            field.m_index = index++;
                        }
                        else
                        {
                            field.Reader = null;
                        }
                    }

                    TableDefinitionMap[pair.Key].ColumnDefinitions = fields;
                }
                catch (Exception ex)
                {
                    throw new DataHolderException(ex, "Unable to setup Table \"{0}\" (MainDataHolder: \"{1}\")", table, table.MainDataHolder);
                }
            }

            m_mappings = CreateDataHolderTableMappings(m_tableDataHolderMap, DataHolderDefinitions);
        }
Ejemplo n.º 15
0
		static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
			Dictionary<string, List<SimpleDataColumn>> mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
		{
			var column = fieldDef.Column;

			var tables = defs.EnsureTables(fieldDef.Table, defaultTables);
			object defaultValue;
			if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
			{
				defaultValue = Utility.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
			}
			else
			{
				if (String.IsNullOrEmpty(column))
				{
					return;
				}
				defaultValue = null;
			}

			if (accessor.DataHolderDefinition.Type.Name.EndsWith("TrainerEntry"))
			{
				defs.ToString();
			}

			foreach (var table in tables)
			{
				var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
				if (!dataHolders.Contains(accessor.DataHolderDefinition))
				{
					dataHolders.Add(accessor.DataHolderDefinition);
				}

				var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

				SimpleDataColumn dataColumn;

				if (String.IsNullOrEmpty(column))
				{
					// use default value
					mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
				}
				else
				{
					dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
					if (dataColumn == null)
					{
						mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, Converters.GetReader(member.GetActualType())));
					}
				}

				dataColumn.FieldList.Add(accessor);
			}
		}