Example #1
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);
        }
        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);
        }