Esempio n. 1
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;
        }
Esempio n. 2
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;
        }