Пример #1
0
            /// <summary>
            /// Read worksheet and return data in class T
            /// with properties set to the values in worksheet.
            /// </summary>
            /// <typeparam name="T">
            /// Class that as properties matching column names. Properties might be decorated with
            /// a CollumnAttribute
            /// </typeparam>
            /// <returns>
            /// Returns instances of type T with the porperties set
            /// that match a column name of the sheet
            /// </returns>
            public IEnumerable <T> Entities <T>() where T : class, new()
            {
                var t         = typeof(T);
                var columnMap = new ColumnMap(t);

                foreach (var row in ElementsOfDataRows)
                {
                    var entity   = new T();
                    var hasValue = false;
                    foreach (var cell in KeyedValues(row, (XElement e, int i) => {
                        var name = i < Header.Length ? Header[i] : null;
                        if (columnMap.GetContentType(name) == ColumnAttribute.ContentTypes.Data)
                        {
                            return(this._xlsx.CellContents.Value(e));
                        }
                        return(this._xlsx.CellContents.Formula(e));
                    }))
                    {
                        var destination = columnMap.GetPropertyInfo(cell.Key);
                        if (destination == null)
                        {
                            continue;
                        }
                        destination.SetValue(entity, Convert.ChangeType(cell.Value, destination.PropertyType));
                        hasValue = true;
                    }
                    if (hasValue)
                    {
                        yield return(entity);
                    }
                }
            }
Пример #2
0
            /// <summary>
            ///     Find all vars that were referenced in the column map. Looks for VarRefColumnMap
            ///     in the ColumnMap tree, and tracks those vars
            ///     NOTE: The "vec" parameter must be supplied by the caller. The caller is responsible for
            ///     clearing out this parameter (if necessary) before calling into this function
            /// </summary>
            /// <param name="columnMap"> the column map to traverse </param>
            /// <param name="vec"> the set of referenced columns </param>
            internal static void FindVars(ColumnMap columnMap, VarVec vec)
            {
                var tracker = new ColumnMapVarTracker();

                columnMap.Accept(tracker, vec);
                return;
            }
Пример #3
0
        // <summary>
        // Returns a string uniquely identifying the given ColumnMap.
        // </summary>
        internal static string GetColumnMapKey(ColumnMap columnMap, SpanIndex spanIndex)
        {
            var builder = new ColumnMapKeyBuilder(spanIndex);

            columnMap.Accept(builder, 0);
            return(builder._builder.ToString());
        }
Пример #4
0
 internal static void Process(
     System.Data.Entity.Core.Query.PlanCompiler.PlanCompiler compilerState,
     out List <ProviderCommandInfo> childCommands,
     out ColumnMap resultColumnMap,
     out int columnCount)
 {
     new CodeGen(compilerState).Process(out childCommands, out resultColumnMap, out columnCount);
 }
Пример #5
0
 public object FromDB(ColumnMap map, object dbValue)
 {
     if (dbValue == null || dbValue == DBNull.Value)
     {
         return(null);
     }
     return(Enum.Parse(map.FieldType, (string)dbValue));
 }
 /// <summary>
 /// Registers any member with a ColumnAttribute as a ColumnMap.
 /// <param name="entityType">The entity that is being mapped.</param>
 /// <param name="member">The current member that is being inspected.</param>
 /// <param name="columnAtt">A ColumnAttribute (is null of one does not exist).</param>
 /// <param name="columnMaps">A list of ColumnMaps.</param>
 /// </summary>
 protected override void CreateColumnMap(Type entityType, MemberInfo member, ColumnAttribute columnAtt, ColumnMapCollection columnMaps)
 {
     if (columnAtt != null)
     {
         ColumnMap columnMap = new ColumnMap(member, columnAtt);
         columnMaps.Add(columnMap);
     }
 }
Пример #7
0
 /// <summary>
 /// Registers any member with a ColumnAttribute as a ColumnMap.
 /// <param name="entityType">The entity that is being mapped.</param>
 /// <param name="member">The current member that is being inspected.</param>
 /// <param name="columnAtt">A ColumnAttribute (is null of one does not exist).</param>
 /// <param name="columnMaps">A list of ColumnMaps.</param>
 /// </summary>
 protected override void CreateColumnMap(Type entityType, MemberInfo member, ColumnAttribute columnAtt, ColumnMapCollection columnMaps)
 {
     if (columnAtt != null)
     {
         ColumnMap columnMap = new ColumnMap(member, columnAtt);
         columnMaps.Add(columnMap);
     }
 }
Пример #8
0
    [Test] //testing this because it's surprising that it works. Unity magic...
    public void processing_rules_from_column_map_calls_correct_processors()
    {
        ColumnMap map = AssetDatabase.LoadAssetAtPath <ColumnMap>(columnMapPath);

        Assert.AreEqual("hello", map.rules[0].rule.Process(testVals));
        Assert.AreEqual("test12", map.rules[1].rule.Process(testVals));
        Assert.AreEqual("12", map.rules[2].rule.Process(testVals));
    }
Пример #9
0
 public object FromDB(ColumnMap map, object dbValue)
 {
     if (dbValue == null || dbValue == DBNull.Value)
     {
         return(null);
     }
     return(Enum.ToObject(map.FieldType, (int)dbValue));
 }
Пример #10
0
        /// <summary>
        /// コンストラクタ、親ノード、挿入先テーブルと列の並びを指定して初期化する
        /// </summary>
        /// <param name="parent">親ノード</param>
        /// <param name="table">挿入先テーブル</param>
        /// <param name="columnsExpression">列と設定値を指定する t => new[] { t.A == a, t.B == b } の様な式</param>
        public InsertInto(IQueryNode parent, TableDef <TColumns> table, Expression <Func <TColumns, bool[]> > columnsExpression)
        {
            this.Parent = parent;

            // new [] { bool, bool... } の様な形式以外はエラーとする
            var body = columnsExpression.Body;

            if (body.NodeType != ExpressionType.NewArrayInit)
            {
                throw new ApplicationException();
            }

            // 登録
            var owner = this.Owner;

            owner.RegisterTable(table);

            // bool[] の各要素初期化式を取得する
            var newexpr     = body as NewArrayExpression;
            var expressions = newexpr.Expressions;
            var map         = new Dictionary <Expression, object> {
                { columnsExpression.Parameters[0], table.Columns }
            };
            var availableColumns = owner.AllColumns;
            var tableColumnMap   = table.ColumnMap;
            var columnsOrder     = new ColumnMap();
            var values           = new ElementCode[expressions.Count];

            for (int i = 0; i < values.Length; i++)
            {
                // t.A == a の様に Equal を代入として扱いたいのでそれ以外はエラーとする
                var expression = expressions[i];
                if (expression.NodeType != ExpressionType.Equal)
                {
                    throw new ApplicationException();
                }

                var binary = expression as BinaryExpression;

                // 左辺は代入先の列でなければならない
                var left   = new ElementCode(ParameterReplacer.Replace(binary.Left, map), tableColumnMap);
                var column = left.FindColumns().FirstOrDefault();
                if (column == null)
                {
                    throw new ApplicationException();
                }

                // 右辺は式
                values[i] = new ElementCode(ParameterReplacer.Replace(binary.Right, map), availableColumns);

                // 列の生成元を右辺の式にして列を登録
                columnsOrder.Add(column);
            }

            this.Table     = table;
            this.ColumnMap = columnsOrder;
            this.ValueNode = new ValueSetter(this, columnsOrder, values);
        }
Пример #11
0
        public void MapDataTableToTable(string tableName, DataTable dataTable, ColumnMapping columnMapping)
        {
            DataTable emptyDataTable = _databaseCommander.ExecuteSql($"SELECT * FROM {tableName} WHERE 1 = 0");

            List <string> databaseColumnNames = emptyDataTable.Columns.Cast <DataColumn>().Select(dc => dc.ColumnName).ToList();
            List <string> fileColumnNames     = dataTable.Columns.Cast <DataColumn>().Select(dc => dc.ColumnName).ToList();

            // Ensure proper casing of source columns
            foreach (string fileColumnName in fileColumnNames)
            {
                ColumnMap columnMap = columnMapping.ColumnMaps.FirstOrDefault(cm => cm.Source.Equals(fileColumnName, StringComparison.InvariantCultureIgnoreCase));

                if (columnMap != null && columnMap.Source != fileColumnName)
                {
                    columnMap.Source = fileColumnName;
                }
            }

            // Ensure proper casing of destination columns
            foreach (string databaseColumnName in databaseColumnNames)
            {
                ColumnMap columnMap = columnMapping.ColumnMaps.FirstOrDefault(cm => cm.Destination.Equals(databaseColumnName, StringComparison.InvariantCultureIgnoreCase));

                if (columnMap != null && columnMap.Destination != databaseColumnName)
                {
                    columnMap.Destination = databaseColumnName;
                }
            }

            // Ensure existence of source columns
            foreach (string sourceColumnName in columnMapping.ColumnMaps.Select(cm => cm.Source))
            {
                if (!fileColumnNames.Contains(sourceColumnName))
                {
                    throw new InvalidOperationException($"The source column '{sourceColumnName}' does not exist");
                }
            }

            // Ensure existence of destination columns
            foreach (string destinationColumnName in columnMapping.ColumnMaps.Select(cm => cm.Destination))
            {
                if (!databaseColumnNames.Contains(destinationColumnName))
                {
                    throw new InvalidOperationException($"The destination column '{destinationColumnName}' does not exist");
                }
            }

            // AutoMap any columns that are not already mapped from the source to the destination
            foreach (string fileColumnName in fileColumnNames)
            {
                string databaseColumnName = databaseColumnNames.FirstOrDefault(s => s.Equals(fileColumnName, StringComparison.InvariantCultureIgnoreCase));

                if (databaseColumnName != null && columnMapping.ColumnMaps.All(cm => cm.Source != fileColumnName) && columnMapping.ColumnMaps.All(cm => cm.Destination != databaseColumnName))
                {
                    columnMapping.ColumnMaps.Add(new ColumnMap(fileColumnName, databaseColumnName));
                }
            }
        }
Пример #12
0
        public static string FindFKValueInOther(string lookupValue, ColumnMap cmap, SqlConnection connection, bool genNewFK, string lookupColName, Guid NKDProjectID)
        {
            string fkTable     = cmap.fkRelationTable;
            string fkColumnKey = cmap.fkRelationColumn;

            if (lookupColName == null)
            {
                lookupColName = cmap.targetColumnName;
            }


            string statement1 = "SELECT " + fkColumnKey + " FROM " + fkTable + " WHERE " + lookupColName + " = \'" + lookupValue + "\' AND ProjectID = \'" + NKDProjectID.ToString() + "\'";

            SqlCommand    sqc     = new SqlCommand(statement1, connection);
            SqlDataReader reader  = sqc.ExecuteReader();
            List <string> results = new List <string>();

            while (reader.Read())
            {
                string fkName = reader[0].ToString();
                results.Add(fkName);
            }
            reader.Close();

            if (results.Count == 0 && genNewFK == true)
            {
                //    // there is no matching entry in this dictionary.  Make a new entry
                //    Guid gg = Guid.NewGuid();

                //    string p1 = gg.ToString();
                //    results.Add(p1);

                //    char[] splitters = { '-' };
                //    string[] items = p1.Split(splitters);
                //    string p2 = "";
                //    foreach (string it in items)
                //    {
                //        p2 += it;
                //    }
                //    string stdValMock = p2;
                //    if (p2.Length > 15)
                //    {
                //        p2 = p2.Substring(0, 15);
                //    }
                //    string query = "INSERT INTO " + fkTable + " (" + fkColumnKey + "," + stdLookupColumnPrediction + "," + nameLookupColumnPrediction + ") VALUES " +
                //                    " (\'" + gg.ToString() + "\',\'" + p2 + "\',\'" + columnValue + "\' )";

                //    SqlCommand sqc2 = new SqlCommand(query, connection);
                //    sqc2.ExecuteNonQuery();
            }
            string res = null;

            if (results.Count > 0)
            {
                res = results.First();
            }
            return(res);
        }
Пример #13
0
        public object FromDB(ColumnMap map, object dbValue)
        {
            if (dbValue != null && dbValue != DBNull.Value)
            {
                return(Enum.ToObject(map.FieldType, (Int64)dbValue));
            }

            return(null);
        }
Пример #14
0
        public object FromDB(ColumnMap map, object dbValue)
        {
            if (dbValue is string version)
            {
                return(Version.Parse(version));
            }

            return(null);
        }
Пример #15
0
        private ColumnMap BuildResultColumnMap(PhysicalProjectOp projectOp)
        {
            // convert the column map into a real column map
            // build up a dictionary mapping Vars to their real positions in the commands
            Dictionary <Var, KeyValuePair <int, int> > varMap = BuildVarMap();
            ColumnMap realColumnMap = ColumnMapTranslator.Translate(projectOp.ColumnMap, varMap);

            return(realColumnMap);
        }
Пример #16
0
        private KeyValuePair <Shaper <RecordState>, CoordinatorFactory <RecordState> > CreateShaperInfo(
            DbDataReader storeDataReader,
            ColumnMap columnMap,
            MetadataWorkspace workspace)
        {
            Shaper <RecordState> key = this._translator.TranslateColumnMap <RecordState>(columnMap, workspace, (SpanIndex)null, MergeOption.NoTracking, true, true).Create(storeDataReader, (ObjectContext)null, workspace, MergeOption.NoTracking, true, true);

            return(new KeyValuePair <Shaper <RecordState>, CoordinatorFactory <RecordState> >(key, key.RootCoordinator.TypedCoordinatorFactory));
        }
Пример #17
0
        public virtual DbDataReader Create(
            DbDataReader storeDataReader,
            ColumnMap columnMap,
            MetadataWorkspace workspace,
            IEnumerable <ColumnMap> nextResultColumnMaps)
        {
            KeyValuePair <Shaper <RecordState>, CoordinatorFactory <RecordState> > shaperInfo = this.CreateShaperInfo(storeDataReader, columnMap, workspace);

            return((DbDataReader) new BridgeDataReader(shaperInfo.Key, shaperInfo.Value, 0, this.GetNextResultShaperInfo(storeDataReader, workspace, nextResultColumnMaps).GetEnumerator()));
        }
Пример #18
0
 internal static void Compile(
     DbCommandTree ctree,
     out List <ProviderCommandInfo> providerCommands,
     out ColumnMap resultColumnMap,
     out int columnCount,
     out Set <EntitySet> entitySets)
 {
     System.Data.Entity.Core.Query.PlanCompiler.PlanCompiler.Assert(ctree != null, "Expected a valid, non-null Command Tree input");
     new System.Data.Entity.Core.Query.PlanCompiler.PlanCompiler(ctree).Compile(out providerCommands, out resultColumnMap, out columnCount, out entitySets);
 }
Пример #19
0
 private void Append(string prefix, ColumnMap columnMap)
 {
     Append(prefix);
     Append("[");
     if (null != columnMap)
     {
         columnMap.Accept(this, 0);
     }
     Append("]");
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">datatype of column</param>
        /// <param name="name">column name</param>
        /// <param name="elementMap">column map for collection element</param>
        /// <param name="keys">List of keys</param>
        /// <param name="foreignKeys">List of foreign keys</param>
        internal CollectionColumnMap(
            TypeUsage type, string name, ColumnMap elementMap, SimpleColumnMap[] keys, SimpleColumnMap[] foreignKeys)
            : base(type, name)
        {
            Debug.Assert(elementMap != null, "Must specify column map for element");

            m_element = elementMap;
            m_keys = keys ?? new SimpleColumnMap[0];
            m_foreignKeys = foreignKeys ?? new SimpleColumnMap[0];
        }
        /// <summary>
        ///     Constructor
        /// </summary>
        /// <param name="type"> datatype of column </param>
        /// <param name="name"> column name </param>
        /// <param name="elementMap"> column map for collection element </param>
        /// <param name="keys"> List of keys </param>
        /// <param name="foreignKeys"> List of foreign keys </param>
        internal CollectionColumnMap(
            TypeUsage type, string name, ColumnMap elementMap, SimpleColumnMap[] keys, SimpleColumnMap[] foreignKeys)
            : base(type, name)
        {
            DebugCheck.NotNull(elementMap);

            m_element = elementMap;
            m_keys = keys ?? new SimpleColumnMap[0];
            m_foreignKeys = foreignKeys ?? new SimpleColumnMap[0];
        }
Пример #22
0
        /// <summary>
        /// Create a column map for a complextype column
        /// </summary>
        /// <param name="typeInfo">Type information for the type</param>
        /// <param name="name">column name</param>
        /// <param name="superTypeColumnMap">Supertype info if any</param>
        /// <param name="discriminatorMap">Dictionary of typeidvalue->column map</param>
        /// <param name="allMaps">List of all maps</param>
        /// <returns></returns>
        private ComplexTypeColumnMap CreateComplexTypeColumnMap(TypeInfo typeInfo, string name, ComplexTypeColumnMap superTypeColumnMap,
                                                                Dictionary <object, TypedColumnMap> discriminatorMap, List <TypedColumnMap> allMaps)
        {
            List <ColumnMap> propertyColumnMapList = new List <ColumnMap>();
            IEnumerable      myProperties          = null;

            SimpleColumnMap nullSentinelColumnMap = null;

            if (typeInfo.HasNullSentinelProperty)
            {
                nullSentinelColumnMap = CreateSimpleColumnMap(md.Helper.GetModelTypeUsage(typeInfo.NullSentinelProperty), c_NullSentinelColumnName);
            }

            // Copy over information from my supertype if it already exists
            if (superTypeColumnMap != null)
            {
                foreach (ColumnMap c in superTypeColumnMap.Properties)
                {
                    propertyColumnMapList.Add(c);
                }
                myProperties = TypeHelpers.GetDeclaredStructuralMembers(typeInfo.Type);
            }
            else
            {
                // need to get all members otherwise
                myProperties = TypeHelpers.GetAllStructuralMembers(typeInfo.Type);
            }

            // Now add on all of my "specific" properties
            foreach (md.EdmMember property in myProperties)
            {
                ColumnMap propertyColumnMap = CreateColumnMap(md.Helper.GetModelTypeUsage(property), property.Name);
                propertyColumnMapList.Add(propertyColumnMap);
            }

            // Create a map for myself
            ComplexTypeColumnMap columnMap = new ComplexTypeColumnMap(typeInfo.Type, name, propertyColumnMapList.ToArray(), nullSentinelColumnMap);

            // if a dictionary is supplied, add myself to the dictionary
            if (discriminatorMap != null)
            {
                discriminatorMap[typeInfo.TypeId] = columnMap;
            }
            if (allMaps != null)
            {
                allMaps.Add(columnMap);
            }
            // Finally walk through my subtypes - use the same column name
            foreach (TypeInfo subTypeInfo in typeInfo.ImmediateSubTypes)
            {
                CreateComplexTypeColumnMap(subTypeInfo, name, columnMap, discriminatorMap, allMaps);
            }

            return(columnMap);
        }
Пример #23
0
 internal CollectionInfo(
     Var collectionVar, ColumnMap columnMap, VarList flattenedElementVars, VarVec keys, List<SortKey> sortKeys,
     object discriminatorValue)
 {
     m_collectionVar = collectionVar;
     m_columnMap = columnMap;
     m_flattenedElementVars = flattenedElementVars;
     m_keys = keys;
     m_sortKeys = sortKeys;
     m_discriminatorValue = discriminatorValue;
 }
Пример #24
0
        protected void InitializeColumnMap(DataReaderWrapperParameters parameters)
        {
            var columns = new List <string>();

            if (parameters?.Columns != null)
            {
                columns.AddRange(parameters.Columns);
            }

            if (columns.Count <= 0)
            {
                for (int i = 0; i < _Reader.FieldCount; i++)
                {
                    columns.Add(_Reader.GetName(i));
                }
            }

            if ((parameters.SkipColumns?.Length ?? 0) > 0)
            {
                var dictSkip = new StringNoCaseDictionary <int>();

                foreach (string skipColumn in parameters.SkipColumns)
                {
                    if (!dictSkip.ContainsKey(skipColumn))
                    {
                        dictSkip.Add(skipColumn, 1);
                    }
                    else
                    {
                        dictSkip[skipColumn]++;
                    }
                }

                for (int i = columns.Count - 1; i >= 0; i--)
                {
                    if (dictSkip.ContainsKey(columns[i]))
                    {
                        columns.RemoveAt(i);
                    }
                }
            }

            for (int i = 0; i < columns.Count; i++)
            {
                string columnName = columns[i];
                int    ordinal    = _Reader.GetOrdinal(columnName);
                if (ordinal < 0)
                {
                    throw new Exception($"Cannot find column '{columnName}'");
                }
                ColumnMap.Add(i, ordinal);
                ColumnMapReverse.Add(ordinal, i);
            }
        }
        /// <summary>
        /// The primary factory method to produce the BridgeDataReader; given a store data
        /// reader and a column map, create the BridgeDataReader, hooking up the IteratorSources
        /// and ResultColumn Hierarchy.  All construction of top level data readers go through
        /// this method.
        /// </summary>
        /// <param name="storeDataReader"></param>
        /// <param name="columnMap">column map of the first result set</param>
        /// <param name="nextResultColumnMaps">enumerable of the column maps for NextResult() calls.</param>
        /// <returns></returns>
        static internal DbDataReader Create(DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace, IEnumerable <ColumnMap> nextResultColumnMaps)
        {
            Debug.Assert(storeDataReader != null, "null storeDataReaders?");
            Debug.Assert(columnMap != null, "null columnMap?");
            Debug.Assert(workspace != null, "null workspace?");

            var          shaperInfo = CreateShaperInfo(storeDataReader, columnMap, workspace);
            DbDataReader result     = new BridgeDataReader(shaperInfo.Key, shaperInfo.Value, /*depth:*/ 0, GetNextResultShaperInfo(storeDataReader, workspace, nextResultColumnMaps).GetEnumerator());

            return(result);
        }
Пример #26
0
 private void RenamePoco(TInput row, IDictionary <string, object> resultAsDict)
 {
     for (int i = 0; i < TypeInfo.PropertyLength; i++)
     {
         ColumnMap cm = MappingDict.ContainsKey(TypeInfo.Properties[i].Name) ? MappingDict[TypeInfo.Properties[i].Name] : null;
         if (cm == null || cm?.RemoveColumn == false)
         {
             resultAsDict.Add(cm?.NewName ?? TypeInfo.Properties[i].Name, TypeInfo.Properties[i].GetValue(row));
         }
     }
 }
Пример #27
0
        public object FromDB(ColumnMap map, object dbValue)
        {
            if (dbValue == DBNull.Value)
            {
                return(Quality.Unknown);
            }

            var val = Convert.ToInt32(dbValue);

            return((Quality)val);
        }
Пример #28
0
        private bool IsDefaultValueDisabled(string column, string property, string[] disabledFieldNames)
        {
            if (disabledFieldNames.Contains(property))
                return true;

            string columnWithoutIndex, columnIndex;
            if (ColumnMap.ParseSourceName(property, out columnWithoutIndex, out columnIndex))
                return disabledFieldNames.Contains(columnWithoutIndex);

            return false;
        }
Пример #29
0
        public GLib.Value GetColumnValue(ITreeGridItem item, int dataColumn, int row)
        {
            int column;

            if (ColumnMap.TryGetValue(dataColumn, out column))
            {
                var colHandler = (IGridColumnHandler)Widget.Columns[column].Handler;
                return(colHandler.GetValue(item, dataColumn, row));
            }
            return(new GLib.Value((string)null));
        }
Пример #30
0
 public IActionResult Post(ColumnMap columnMaps)
 {
     try
     {
         ExcelHelper.Instance.SaveDataToDb(columnMaps, _settings.Value.DefaultConnection);
         return(Content("success"));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex.Message));
     }
 }
Пример #31
0
 void PopulateTableColumns()
 {
     if (SelectedConnection != null)
     {
         ColumnMap CM = new ColumnMap(this.DataSourceId, SelectedConnection, ConnectionStringKeys, DatabaseTableName, TotalRecordsInBatch);
         _ColumnMapInfos.Clear();
         foreach (ColumnMapInfo ci in CM)
         {
             _ColumnMapInfos.Add(ci);
         }
     }
 }
Пример #32
0
 public ValueSetter(IQueryNode parent, ColumnMap columns, ElementCode[] values)
 {
     if (columns.Count != values.Length)
     {
         throw new ApplicationException();
     }
     this.Parent = parent;
     for (int i = 0; i < values.Length; i++)
     {
         this.ColumnMap.Add(columns[i].Clone(values[i]));
     }
 }
Пример #33
0
            public ColumnMap GetMap()
            {
                MemberExpression temp        = _memberAccess;
                string           propertyMap = "";

                while (temp != null)
                {
                    propertyMap = "." + temp.Member.Name + propertyMap;
                    temp        = temp.Expression as MemberExpression;
                }
                propertyMap   = propertyMap.TrimStart('.');
                _propertyName = propertyMap;
                if (_mapType == MapType.UseMap && propertyMap.Contains("."))
                {
                    throw new Exception("Nested Property And UseMap Is Not Compatible");
                }
                ColumnMap columnMap = null;

                if (_mapType == MapType.ColumnName)
                {
                    string columnName = _columnName;
                    if (propertyMap.Contains("."))
                    {
                        string[] strings = propertyMap.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);


                        RowMap tempRm = RowMapper.GetTempRowMap(strings, columnName);
                        columnMap = new ColumnMap()
                        {
                            PropertyName   = strings[0],
                            InnerType      = InnerMapType.Internal,
                            InternalRowMap = tempRm
                        };
                    }
                    else
                    {
                        columnMap = new ColumnMap()
                        {
                            ColumnName = columnName, PropertyName = propertyMap
                        }
                    };
                }
                else if (_mapType == MapType.UseMap)
                {
                    columnMap = new ColumnMap()
                    {
                        PropertyName = propertyMap,
                        InnerType    = InnerMapType.Internal,
                    };
                }
                return(columnMap);
            }
Пример #34
0
        internal virtual void Visit(ComplexTypeColumnMap columnMap, TArgType arg)
        {
            ColumnMap nullSentinel = columnMap.NullSentinel;

            if (null != nullSentinel)
            {
                nullSentinel.Accept(this, arg);
            }
            foreach (var p in columnMap.Properties)
            {
                p.Accept(this, arg);
            }
        }
        private KeyValuePair<Shaper<RecordState>, CoordinatorFactory<RecordState>> CreateShaperInfo(
            DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace)
        {
            DebugCheck.NotNull(storeDataReader);
            DebugCheck.NotNull(columnMap);
            DebugCheck.NotNull(workspace);

            var shaperFactory = _translator.TranslateColumnMap<RecordState>(columnMap, workspace, null, MergeOption.NoTracking, streaming: true, valueLayer: true);
            var recordShaper = shaperFactory.Create(
                storeDataReader, null, workspace, MergeOption.NoTracking, readerOwned: true, streaming: true);

            return new KeyValuePair<Shaper<RecordState>, CoordinatorFactory<RecordState>>(
                recordShaper, recordShaper.RootCoordinator.TypedCoordinatorFactory);
        }
        /// <summary>
        ///     The primary factory method to produce the BridgeDataReader; given a store data
        ///     reader and a column map, create the BridgeDataReader, hooking up the IteratorSources
        ///     and ResultColumn Hierarchy.  All construction of top level data readers go through
        ///     this method.
        /// </summary>
        /// <param name="storeDataReader"> </param>
        /// <param name="columnMap"> column map of the first result set </param>
        /// <param name="nextResultColumnMaps"> enumerable of the column maps for NextResult() calls. </param>
        /// <returns> </returns>
        public virtual DbDataReader Create(
            DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace, IEnumerable<ColumnMap> nextResultColumnMaps)
        {
            DebugCheck.NotNull(storeDataReader);
            DebugCheck.NotNull(columnMap);
            DebugCheck.NotNull(workspace);
            DebugCheck.NotNull(nextResultColumnMaps);

            var shaperInfo = CreateShaperInfo(storeDataReader, columnMap, workspace);
            DbDataReader result = new BridgeDataReader(
                shaperInfo.Key, shaperInfo.Value, /*depth:*/ 0,
                GetNextResultShaperInfo(storeDataReader, workspace, nextResultColumnMaps).GetEnumerator());
            return result;
        }
        /// <summary>
        /// The primary factory method to produce the BridgeDataReader; given a store data 
        /// reader and a column map, create the BridgeDataReader, hooking up the IteratorSources  
        /// and ResultColumn Hierarchy.  All construction of top level data readers go through
        /// this method.
        /// </summary>
        /// <param name="storeDataReader"></param>
        /// <param name="columnMap">column map of the first result set</param>
        /// <param name="nextResultColumnMaps">enumerable of the column maps for NextResult() calls.</param>
        /// <returns></returns>
        public virtual DbDataReader Create(
            DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace, IEnumerable<ColumnMap> nextResultColumnMaps)
        {
            Contract.Requires(storeDataReader != null);
            Contract.Requires(columnMap != null);
            Contract.Requires(workspace != null);
            Contract.Requires(nextResultColumnMaps != null);

            var shaperInfo = CreateShaperInfo(storeDataReader, columnMap, workspace);
            DbDataReader result = new BridgeDataReader(
                shaperInfo.Key, shaperInfo.Value, /*depth:*/ 0,
                GetNextResultShaperInfo(storeDataReader, workspace, nextResultColumnMaps).GetEnumerator());
            return result;
        }
        private KeyValuePair<Shaper<RecordState>, CoordinatorFactory<RecordState>> CreateShaperInfo(
            DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace)
        {
            Contract.Requires(storeDataReader != null);
            Contract.Requires(columnMap != null);
            Contract.Requires(workspace != null);

            var cacheManager = workspace.GetQueryCacheManager();
            const MergeOption NoTracking = MergeOption.NoTracking;

            var shaperFactory = _translator.TranslateColumnMap<RecordState>(cacheManager, columnMap, workspace, null, NoTracking, true);
            var recordShaper = shaperFactory.Create(storeDataReader, null, workspace, MergeOption.NoTracking, true);

            return new KeyValuePair<Shaper<RecordState>, CoordinatorFactory<RecordState>>(
                recordShaper, recordShaper.RootCoordinator.TypedCoordinatorFactory);
        }
Пример #39
0
        /// <summary>
        /// The real driver. This routine walks the tree, converts each subcommand
        /// into a CTree, and converts the columnmap into a real column map. 
        /// Finally, it produces a "real" plan that can be used by the bridge execution, and
        /// returns this plan
        /// 
        /// The root of the tree must be a PhysicalProjectOp. Each child of this Op
        /// represents a command to be executed, and the ColumnMap of this Op represents
        /// the eventual columnMap to be used for result assembly
        /// </summary>
        /// <param name="childCommands">CQTs for store commands</param>
        /// <param name="resultColumnMap">column map for result assembly</param>
        private void Process(out List<ProviderCommandInfo> childCommands, out ColumnMap resultColumnMap, out int columnCount)
        {
            var projectOp = (PhysicalProjectOp)Command.Root.Op;

            m_subCommands = new List<Node>(new[] { Command.Root });
            childCommands = new List<ProviderCommandInfo>(
                new[]
                    {
                        ProviderCommandInfoUtils.Create(
                            Command,
                            Command.Root // input node
                            )
                    });

            // Build the final column map, and count the columns we expect for it.
            resultColumnMap = BuildResultColumnMap(projectOp);

            columnCount = projectOp.Outputs.Count;
        }
Пример #40
0
        static void Main(string[] args)
        {
            // Read a table from sql server via a query, then make an exact copy
            // of that table.
            // Step 1. Read source table schema
            // Step 2. Create exact same schema on destination
            // Step 3. Copy Data

            // Prepare reflow engine
            ReflowEngine engine = new ReflowEngine();
            // Test DB Path

            string test = "text"; // "access";
            string sourceConnectionString = string.Empty;

            ILinkProvider sourceProvider = null;
            if (test == "access")
            {
                string accessDBPath = AppDomain.CurrentDomain.BaseDirectory + "db1.accdb";
                sourceConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;",
                    accessDBPath);
                sourceProvider = new Provider.Access.AccessLinkProvider();
            }
            else if (test == "text")
            {
                // TODO: Test and implement text driver
                string txtDBPath = AppDomain.CurrentDomain.BaseDirectory + "text\\HumanResources_Employee.txt";
                sourceConnectionString = "@File="+txtDBPath + ";@Type=Delimited;RowSeperator=\r\n;ColumnSeperator=,;FirstRowHasNames=True";
                // TODO: Fixed length - Need to implement
                //sourceConnectionString = "@File="+txtDBPath + ";@Type=Fixed;";
                sourceProvider = new Provider.Text.TextProvider();
            }

            IDataLink linkSource = sourceProvider.CreateLink ( sourceConnectionString);

            ILinkProvider sqlProvider = new Provider.SqlServer.SqlLinkProvider();
            string destConnectionString = "Server=localhost;Database=REflow;Trusted_Connection=True;";
            IDataLink linkDestination = sqlProvider.CreateLink(destConnectionString);

            // Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\txtFilesFolder\;Extensions=asc,csv,tab,txt;

            string selectQuery = "Select * from HumanResources_Employee";

            // First lets discover the schema of the query [source table]
            DiscoverSchemaTask task1 = new DiscoverSchemaTask();
            task1.Name = "DiscoverSchema";
            task1.Query = selectQuery;
            task1.Link = linkSource;

            // Then we need to create a table in the destinaton database.
            TableCreateTask task2 = new TableCreateTask();
            task2.Name = "CreateTableAbc";
            task2.TableName = "abc";
            task2.Link = linkDestination;
            task2.ShouldDropExisting = true;
            // Now we need to map task1 output to be the input of task2 since
            // task1 will discover column names and we need to map discovered
            // columns to the Columns property of task 2. We can do this one of
            // two way.

            // First, we can use a delegate to call onbeforeexecute for task2 and
            // assign task result of task 1 to the column property of task 2. But that
            // would be problematic for the scenario where we want to serialize the task
            // and deserialize.

            // Second, We can we task link object map output of task1 to go into certain
            // properties of task 2. This can be serialized. We would use this technique

            TaskLink link = new TaskLink();
            link.LastTask = task1;
            link.NextTask = task2;
            // Map the output called DiscoveredColumns ( found in TaskResult.Output["DiscoveredColumns"]) to
            // task2.Columns once task1 has been executed
            link.TaskPipe["Columns"] = "Columns";
            link.Bind();

            // Now we will execute the data copy task. Will need to do the column mapping
            // Interestingly, since our output columns are same as input columns at source,
            // we can use the automap property to map the columns automatically.

            DataFlowTask task3 = new DataFlowTask() { Name = "DataCopyTask" };
            ILinkReader reader = sourceProvider.CreateReader(linkSource, selectQuery); // Since we are using same query
            ILinkWriter writer = sqlProvider.CreateWriter(linkDestination, task2.TableName); //  Dest table
            task3.Input = reader;
            task3.Output = writer;
            /* We would notmally create column mapping here and and map source and
             * destination and put custom expressions if needed. But since we are doing a
             * direct table copy, we can just use Automap property. */
            // ColumnMappings maps = new ColumnMappings();
            task3.IsAutoMap = true;
            task3.TableName = task2.TableName;

            // TODO: Add column mapping support
            // Add scripting transformation
            ColumnMap map = new ColumnMap();
            map.Destination = "Title";
            Expression exp = new Expression() { Code = "NationalIDNumber & \" \" &  UCASE(Title) & CSTR(LEN(Title))"};
            map.TransformExpression = exp;
            task3.Mapping.Add(map);

            engine.Tasks.Add(task1);
            engine.Tasks.Add(task2);
            engine.Tasks.Add(task3);

            ExecutionEventListener eventListener = new ExecutionEventListener();
            eventListener.OnTaskExecutionEvent+= delegate (string taskname, string eventName, string description)
            {
                Console.WriteLine(string.Format("{0,15} |{1,10} | {2}", taskname, eventName, description));
            };
            eventListener.LoggingLevel = ExecutionEventListener.LogLevel.Verbose;
            engine.Execute(eventListener);
        }
        /// <summary>
        /// Create a column map for a record type. Simply iterates through the
        /// list of fields, and produces a column map for each field
        /// </summary>
        /// <param name="typeInfo">Type information for the record type</param>
        /// <param name="name">column name</param>
        /// <returns></returns>
        private RecordColumnMap CreateRecordColumnMap(TypeInfo typeInfo, string name)
        {
            PlanCompiler.Assert(typeInfo.Type.EdmType is md.RowType, "not RowType");
            SimpleColumnMap nullSentinelColumnMap = null;
            if (typeInfo.HasNullSentinelProperty)
            {
                nullSentinelColumnMap = CreateSimpleColumnMap(md.Helper.GetModelTypeUsage(typeInfo.NullSentinelProperty), c_NullSentinelColumnName);
            }

            md.ReadOnlyMetadataCollection<md.EdmProperty> properties = TypeHelpers.GetProperties(typeInfo.Type);
            ColumnMap[] propertyColumnMapList = new ColumnMap[properties.Count];
            for (int i = 0; i < propertyColumnMapList.Length; ++i)
            {
                md.EdmMember property = properties[i];
                propertyColumnMapList[i] = CreateColumnMap(md.Helper.GetModelTypeUsage(property), property.Name);
            }

            RecordColumnMap result = new RecordColumnMap(typeInfo.Type, name, propertyColumnMapList, nullSentinelColumnMap);
            return result;
        }
Пример #42
0
        public ActionResult Import()
        {
            string importJson = Request["hidImport"];
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            dynamic jsonObject = serializer.Deserialize<dynamic>(importJson);
            //ImportInfo jsonObject;
            // Do the import
            string sourceConnectionString = "@File=" + jsonObject["ImportFileLocation"] + ";@Type=Delimited;RowSeperator=" +
               jsonObject["RowDelimiter"]  +";ColumnSeperator="+ jsonObject["ColumnDelimiter"] +";FirstRowHasNames=" +
               jsonObject["FirstRowHasColumnNames"].ToString();

            string destConnectionString = ConfigurationManager.AppSettings["SqlConnection"];

            ILinkProvider sourceProvider = null;
            ILinkProvider destProvider = null;

            destProvider = new SqlLinkProvider();
            sourceProvider = new TextProvider();

            IDataLink linkSource = sourceProvider.CreateLink(sourceConnectionString);
            IDataLink linkDestination = destProvider.CreateLink(destConnectionString);

            SqlDataLink sqlLink = new SqlDataLink();
            sqlLink.Initialize(ConfigurationManager.AppSettings["SqlConnection"]);
            sqlLink.Connect();

            DataFlowTask copy = new DataFlowTask() { Name = "DataCopyTask" };
            ILinkReader reader = sourceProvider.CreateReader(linkSource, ""); // Since we are using same query
            string destTable = jsonObject["DestinationTableName"].ToString();
            ILinkWriter writer = destProvider.CreateWriter(linkDestination, destTable); //  Dest table

            copy.Input = reader;
            copy.Output = writer;
            copy.TableName = destTable;
            copy.IsAutoMap = false;

            dynamic maps = jsonObject["Maps"];
            foreach (dynamic map in maps)
            {
                ColumnMap cmap = new ColumnMap();
                cmap.Destination = map["FieldName"];
                Expression exp = new Expression() { Code = map["TargetExpression"] };
                cmap.TransformExpression = exp;
                copy.Mapping.Add(cmap);
            }

            StringBuilder sb = new StringBuilder();

            ReflowEngine engine = new ReflowEngine();
            engine.Tasks.Add(copy);
            ExecutionEventListener eventListener = new ExecutionEventListener();
            eventListener.OnTaskExecutionEvent += delegate(string taskname, string eventName, string description)
            {
                sb.Append(string.Format("{0,15} |{1,10} | {2} <br>", taskname, eventName, description));
            };
            eventListener.LoggingLevel = ExecutionEventListener.LogLevel.Verbose;
            engine.Execute(eventListener);

            linkSource.Disconnect();
            linkDestination.Disconnect();

            //System.IO.File.Delete(jsonObject["ImportFileLocation"]);
            Session.Remove("import_file_path");

            this.ViewBag.Log= sb.ToString();
            return View();
        }
 /// <summary>
 /// Structured columnmap constructor
 /// </summary>
 /// <param name="type">datatype for this column</param>
 /// <param name="name">column name</param>
 /// <param name="properties">list of properties</param>
 internal StructuredColumnMap(TypeUsage type, string name, ColumnMap[] properties)
     : base(type, name)
 {
     Debug.Assert(properties != null, "No properties (gasp!) for a structured type");
     m_properties = properties;
 }
Пример #44
0
 // <summary>
 // Typed columnMap constructor
 // </summary>
 // <param name="type"> Datatype of column </param>
 // <param name="name"> column name </param>
 // <param name="properties"> List of column maps - one for each property </param>
 internal TypedColumnMap(TypeUsage type, string name, ColumnMap[] properties)
     : base(type, name, properties)
 {
 }
        private static KeyValuePair<Shaper<RecordState>, CoordinatorFactory<RecordState>> CreateShaperInfo(DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace)
        {
            Debug.Assert(storeDataReader != null, "null storeDataReaders?");
            Debug.Assert(columnMap != null, "null columnMap?");
            Debug.Assert(workspace != null, "null workspace?");

            System.Data.Common.QueryCache.QueryCacheManager cacheManager = workspace.GetQueryCacheManager();
            const System.Data.Objects.MergeOption NoTracking = System.Data.Objects.MergeOption.NoTracking;

            ShaperFactory<RecordState> shaperFactory = Translator.TranslateColumnMap<RecordState>(cacheManager, columnMap, workspace, null, NoTracking, true);
            Shaper<RecordState> recordShaper = shaperFactory.Create(storeDataReader, null, workspace, System.Data.Objects.MergeOption.NoTracking, true);

            return new KeyValuePair<Shaper<RecordState>, CoordinatorFactory<RecordState>>(recordShaper, recordShaper.RootCoordinator.TypedCoordinatorFactory);
        }
 // <summary>
 // Structured columnmap constructor
 // </summary>
 // <param name="type"> datatype for this column </param>
 // <param name="name"> column name </param>
 // <param name="properties"> list of properties </param>
 internal StructuredColumnMap(TypeUsage type, string name, ColumnMap[] properties)
     : base(type, name)
 {
     DebugCheck.NotNull(properties);
     m_properties = properties;
 }
Пример #47
0
        /// <summary>
        ///     The real driver.
        /// </summary>
        /// <param name="providerCommands"> list of provider commands </param>
        /// <param name="resultColumnMap"> column map for the result </param>
        /// <param name="entitySets"> the entity sets exposed in this query </param>
        private void Compile(
            out List<ProviderCommandInfo> providerCommands, out ColumnMap resultColumnMap, out int columnCount,
            out Set<md.EntitySet> entitySets)
        {
            Initialize(); // initialize the ITree

            var beforePreProcessor = String.Empty;
            var beforeAggregatePushdown = String.Empty;
            var beforeNormalization = String.Empty;
            var beforeNTE = String.Empty;
            var beforeProjectionPruning1 = String.Empty;
            var beforeNestPullup = String.Empty;
            var beforeProjectionPruning2 = String.Empty;
            var beforeTransformationRules1 = String.Empty;
            var beforeProjectionPruning3 = String.Empty;
            var beforeTransformationRules2 = String.Empty;
            var beforeJoinElimination1 = String.Empty;
            var beforeTransformationRules3 = String.Empty;
            var beforeJoinElimination2 = String.Empty;
            var beforeTransformationRules4 = String.Empty;
            var beforeCodeGen = String.Empty;

            //
            // We always need the pre-processor and the codegen phases.
            // It is generally a good thing to run through the transformation rules, and 
            // the projection pruning phases.
            // The "optional" phases are AggregatePushdown, Normalization, NTE, NestPullup and JoinElimination
            //
            m_neededPhases = (1 << (int)PlanCompilerPhase.PreProcessor) |
                             // (1 << (int)PlanCompilerPhase.AggregatePushdown) |
                             // (1 << (int)PlanCompilerPhase.Normalization) |
                             // (1 << (int)PlanCompilerPhase.NTE) |
                             (1 << (int)PlanCompilerPhase.ProjectionPruning) |
                             // (1 << (int)PlanCompilerPhase.NestPullup) |
                             (1 << (int)PlanCompilerPhase.Transformations) |
                             // (1 << (int)PlanCompilerPhase.JoinElimination) |
                             (1 << (int)PlanCompilerPhase.CodeGen);

            // Perform any necessary preprocessing
            StructuredTypeInfo typeInfo;
            Dictionary<md.EdmFunction, md.EdmProperty[]> tvfResultKeys;
            beforePreProcessor = SwitchToPhase(PlanCompilerPhase.PreProcessor);
            PreProcessor.Process(this, out typeInfo, out tvfResultKeys);
            entitySets = typeInfo.GetEntitySets();

            if (IsPhaseNeeded(PlanCompilerPhase.AggregatePushdown))
            {
                beforeAggregatePushdown = SwitchToPhase(PlanCompilerPhase.AggregatePushdown);
                AggregatePushdown.Process(this);
            }

            if (IsPhaseNeeded(PlanCompilerPhase.Normalization))
            {
                beforeNormalization = SwitchToPhase(PlanCompilerPhase.Normalization);
                Normalizer.Process(this);
            }

            // Eliminate "structured" types.
            if (IsPhaseNeeded(PlanCompilerPhase.NTE))
            {
                beforeNTE = SwitchToPhase(PlanCompilerPhase.NTE);
                NominalTypeEliminator.Process(this, typeInfo, tvfResultKeys);
            }

            // Projection pruning - eliminate unreferenced expressions
            if (IsPhaseNeeded(PlanCompilerPhase.ProjectionPruning))
            {
                beforeProjectionPruning1 = SwitchToPhase(PlanCompilerPhase.ProjectionPruning);
                ProjectionPruner.Process(this);
            }

            // Nest Pull-up on the ITree
            if (IsPhaseNeeded(PlanCompilerPhase.NestPullup))
            {
                beforeNestPullup = SwitchToPhase(PlanCompilerPhase.NestPullup);

                NestPullup.Process(this);

                //If we do Nest Pull-up, we should again do projection pruning
                beforeProjectionPruning2 = SwitchToPhase(PlanCompilerPhase.ProjectionPruning);
                ProjectionPruner.Process(this);
            }

            // Run transformations on the tree
            if (IsPhaseNeeded(PlanCompilerPhase.Transformations))
            {
                var projectionPrunningNeeded = ApplyTransformations(ref beforeTransformationRules1, TransformationRulesGroup.All);

                if (projectionPrunningNeeded)
                {
                    beforeProjectionPruning3 = SwitchToPhase(PlanCompilerPhase.ProjectionPruning);
                    ProjectionPruner.Process(this);
                    ApplyTransformations(ref beforeTransformationRules2, TransformationRulesGroup.Project);
                }
            }

            // Join elimination
            if (IsPhaseNeeded(PlanCompilerPhase.JoinElimination))
            {
                beforeJoinElimination1 = SwitchToPhase(PlanCompilerPhase.JoinElimination);
                var modified = JoinElimination.Process(this);
                if (modified)
                {
                    ApplyTransformations(ref beforeTransformationRules3, TransformationRulesGroup.PostJoinElimination);
                    beforeJoinElimination2 = SwitchToPhase(PlanCompilerPhase.JoinElimination);
                    modified = JoinElimination.Process(this);
                    if (modified)
                    {
                        ApplyTransformations(ref beforeTransformationRules4, TransformationRulesGroup.PostJoinElimination);
                    }
                }
            }

            // Code generation
            beforeCodeGen = SwitchToPhase(PlanCompilerPhase.CodeGen);
            CodeGen.Process(this, out providerCommands, out resultColumnMap, out columnCount);

#if DEBUG
            // GC.KeepAlive makes FxCop Grumpy.
            var size = beforePreProcessor.Length;
            size = beforeAggregatePushdown.Length;
            size = beforeNormalization.Length;
            size = beforeNTE.Length;
            size = beforeProjectionPruning1.Length;
            size = beforeNestPullup.Length;
            size = beforeProjectionPruning2.Length;
            size = beforeTransformationRules1.Length;
            size = beforeProjectionPruning3.Length;
            size = beforeTransformationRules2.Length;
            size = beforeJoinElimination1.Length;
            size = beforeTransformationRules3.Length;
            size = beforeJoinElimination2.Length;
            size = beforeTransformationRules4.Length;
            size = beforeCodeGen.Length;
#endif
            // All done
            return;
        }
Пример #48
0
 /// <summary>
 /// Another overload - with an additional discriminatorValue.
 /// Should this be a subtype instead?
 /// </summary>
 /// <param name="collectionVar">the collectionVar</param>
 /// <param name="columnMap">column map for the collection element</param>
 /// <param name="flattenedElementVars">elementVars with any nested collections pulled up</param>
 /// <param name="keys">keys specific to this collection</param>
 /// <param name="sortKeys">sort keys specific to this collecion</param>
 /// <param name="discriminatorValue">discriminator value for this collection (under the current nestOp)</param>
 /// <returns>a new CollectionInfo instance</returns>
 internal static CollectionInfo CreateCollectionInfo(Var collectionVar, ColumnMap columnMap, VarList flattenedElementVars, VarVec keys, List<InternalTrees.SortKey> sortKeys, object discriminatorValue)
 {
     return new CollectionInfo(collectionVar, columnMap, flattenedElementVars, keys, sortKeys, discriminatorValue);
 }
Пример #49
0
 internal ConstantColumnMapGenerator(ColumnMap columnMap, int fieldsRequired) {
     _columnMap = columnMap;
     _fieldsRequired = fieldsRequired;
 }
        /// <summary>
        /// For a given edmType, build an array of scalarColumnMaps that map to the columns
        /// in the store datareader provided.  Note that we're hooking things up by name, not
        /// by ordinal position.
        /// </summary>
        /// <param name="storeDataReader"></param>
        /// <param name="edmType"></param>
        /// <returns></returns>
        private static ColumnMap[] GetColumnMapsForType(DbDataReader storeDataReader, EdmType edmType, Dictionary<string, FunctionImportReturnTypeStructuralTypeColumnRenameMapping> renameList)
        {
            // First get the list of properties; NOTE: we need to hook up the column by name, 
            // not by position.
            IBaseList<EdmMember> members = TypeHelpers.GetAllStructuralMembers(edmType);
            ColumnMap[] propertyColumnMaps = new ColumnMap[members.Count];

            int index = 0;
            foreach (EdmMember member in members)
            {
                if (!Helper.IsScalarType(member.TypeUsage.EdmType))
                {
                    throw EntityUtil.InvalidOperation(Strings.ADP_InvalidDataReaderUnableToMaterializeNonScalarType(member.Name, member.TypeUsage.EdmType.FullName));
                }

                int ordinal = GetMemberOrdinalFromReader(storeDataReader, member, edmType, renameList);

                propertyColumnMaps[index] = new ScalarColumnMap(member.TypeUsage, member.Name, 0, ordinal);
                index++;
            }
            return propertyColumnMaps;
        }
        /// <summary>
        /// Build the entityColumnMap from a store datareader, a type and an entitySet and 
        /// a list ofproperties.
        /// </summary>
        /// <param name="storeDataReader"></param>
        /// <param name="edmType"></param>
        /// <param name="entitySet"></param>
        /// <param name="propertyColumnMaps"></param>
        /// <returns></returns>
        private static EntityColumnMap CreateEntityTypeElementColumnMap(
            DbDataReader storeDataReader, EdmType edmType, EntitySet entitySet,
            ColumnMap[] propertyColumnMaps, Dictionary<string, FunctionImportReturnTypeStructuralTypeColumnRenameMapping> renameList)
        {
            EntityType entityType = (EntityType)edmType;

            // The tricky part here is
            // that the KeyColumns list must point at the same ColumnMap(s) that 
            // the properties list points to, so we build a quick array of 
            // ColumnMap(s) that are indexed by their ordinal; then we can walk
            // the list of keyMembers, and find the ordinal in the reader, and 
            // pick the same ColumnMap for it.

            // Build the ordinal -> ColumnMap index
            ColumnMap[] ordinalToColumnMap = new ColumnMap[storeDataReader.FieldCount];

            foreach (ColumnMap propertyColumnMap in propertyColumnMaps)
            {
                int ordinal = ((ScalarColumnMap)propertyColumnMap).ColumnPos;
                ordinalToColumnMap[ordinal] = propertyColumnMap;
            }

            // Now build the list of KeyColumns;
            IList<EdmMember> keyMembers = entityType.KeyMembers;
            SimpleColumnMap[] keyColumns = new SimpleColumnMap[keyMembers.Count];

            int keyMemberIndex = 0;
            foreach (EdmMember keyMember in keyMembers)
            {
                int keyOrdinal = GetMemberOrdinalFromReader(storeDataReader, keyMember, edmType, renameList);

                Debug.Assert(keyOrdinal >= 0, "keyMember for entity is not found by name in the data reader?");

                ColumnMap keyColumnMap = ordinalToColumnMap[keyOrdinal];

                Debug.Assert(null != keyColumnMap, "keyMember for entity isn't in properties collection for the entity?");
                keyColumns[keyMemberIndex] = (SimpleColumnMap)keyColumnMap;
                keyMemberIndex++;
            }

            SimpleEntityIdentity entityIdentity = new SimpleEntityIdentity(entitySet, keyColumns);

            EntityColumnMap result = new EntityColumnMap(TypeUsage.Create(edmType), edmType.Name, propertyColumnMaps, entityIdentity);
            return result;
        }
        /// <summary>
        /// Requires: a public type with a public, default constructor. Returns a column map initializing the type
        /// and all properties of the type with a public setter taking a primitive type and having a corresponding 
        /// column in the reader.
        /// </summary>
        internal static CollectionColumnMap CreateColumnMapFromReaderAndClrType(DbDataReader reader, Type type, MetadataWorkspace workspace)
        {
            Debug.Assert(null != reader);
            Debug.Assert(null != type);
            Debug.Assert(null != workspace);

            // we require a default constructor
            ConstructorInfo constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                null, Type.EmptyTypes, null);
            if (type.IsAbstract || (null == constructor && !type.IsValueType))
            {
                throw EntityUtil.InvalidOperation(
                    Strings.ObjectContext_InvalidTypeForStoreQuery(type));
            }

            // build a LINQ expression used by result assembly to create results
            var memberInfo = new List<Tuple<MemberAssignment, int, EdmProperty>>();
            foreach (PropertyInfo prop in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                // for enums unwrap the type if nullable
                var propertyUnderlyingType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                Type propType = propertyUnderlyingType.IsEnum ? propertyUnderlyingType.GetEnumUnderlyingType() : prop.PropertyType;

                EdmType modelType;
                int ordinal;
                if (TryGetColumnOrdinalFromReader(reader, prop.Name, out ordinal) &&
                    MetadataHelper.TryDetermineCSpaceModelType(propType, workspace, out modelType) &&
                    (Helper.IsScalarType(modelType)) &&
                    prop.CanWrite && prop.GetIndexParameters().Length == 0 && null != prop.GetSetMethod(/* nonPublic */true))
                {
                    memberInfo.Add(Tuple.Create(
                        Expression.Bind(prop, Expression.Parameter(prop.PropertyType, "placeholder")),
                        ordinal,
                        new EdmProperty(prop.Name, TypeUsage.Create(modelType))));
                }
            }
            // initialize members in the order in which they appear in the reader
            MemberInfo[] members = new MemberInfo[memberInfo.Count];
            MemberBinding[] memberBindings = new MemberBinding[memberInfo.Count];
            ColumnMap[] propertyMaps = new ColumnMap[memberInfo.Count];
            EdmProperty[] modelProperties = new EdmProperty[memberInfo.Count];
            int i = 0;
            foreach (var memberGroup in memberInfo.GroupBy(tuple => tuple.Item2).OrderBy(tuple => tuple.Key))
            {
                // make sure that a single column isn't contributing to multiple properties
                if (memberGroup.Count() != 1)
                {
                    throw EntityUtil.InvalidOperation(Strings.ObjectContext_TwoPropertiesMappedToSameColumn(
                        reader.GetName(memberGroup.Key), 
                        String.Join(", ", memberGroup.Select(tuple => tuple.Item3.Name).ToArray())));
                }

                var member = memberGroup.Single();
                MemberAssignment assignment = member.Item1;
                int ordinal = member.Item2;
                EdmProperty modelProp = member.Item3;
                
                members[i] = assignment.Member;
                memberBindings[i] = assignment;
                propertyMaps[i] = new ScalarColumnMap(modelProp.TypeUsage, modelProp.Name, 0, ordinal);
                modelProperties[i] = modelProp;
                i++;
            }
            NewExpression newExpr = null == constructor ? Expression.New(type) : Expression.New(constructor);
            MemberInitExpression init = Expression.MemberInit(newExpr, memberBindings);
            InitializerMetadata initMetadata = InitializerMetadata.CreateProjectionInitializer(
                (EdmItemCollection)workspace.GetItemCollection(DataSpace.CSpace), init, members);

            // column map (a collection of rows with InitializerMetadata markup)
            RowType rowType = new RowType(modelProperties, initMetadata);
            RecordColumnMap rowMap = new RecordColumnMap(TypeUsage.Create(rowType),
                "DefaultTypeProjection", propertyMaps, null);
            CollectionColumnMap collectionMap = new SimpleCollectionColumnMap(rowType.GetCollectionType().TypeUsage, 
                rowType.Name, rowMap, null, null);
            return collectionMap;
        }
Пример #53
0
 /// <summary>
 /// This involves 
 ///   * Converting the ITree into a set of ProviderCommandInfo objects
 ///   * Creating a column map to enable result assembly
 /// Currently, we only produce a single ITree, and correspondingly, the 
 /// following steps are trivial
 /// </summary>
 /// <param name="compilerState">current compiler state</param>
 /// <param name="childCommands">CQTs for each store command</param>
 /// <param name="resultColumnMap">column map to help in result assembly</param>
 internal static void Process(
     PlanCompiler compilerState, out List<ProviderCommandInfo> childCommands, out ColumnMap resultColumnMap, out int columnCount)
 {
     var codeGen = new CodeGen(compilerState);
     codeGen.Process(out childCommands, out resultColumnMap, out columnCount);
 }
Пример #54
0
 private ColumnMap Copy(ColumnMap columnMap)
 {
     return ColumnMapCopier.Copy(columnMap, m_varMap);
 }
        /// <summary>
        /// The primary factory method to produce the BridgeDataReader; given a store data 
        /// reader and a column map, create the BridgeDataReader, hooking up the IteratorSources  
        /// and ResultColumn Hierarchy.  All construction of top level data readers go through
        /// this method.
        /// </summary>
        /// <param name="storeDataReader"></param>
        /// <param name="columnMap">column map of the first result set</param>
        /// <param name="nextResultColumnMaps">enumerable of the column maps for NextResult() calls.</param>
        /// <returns></returns>
        static internal DbDataReader Create(DbDataReader storeDataReader, ColumnMap columnMap, MetadataWorkspace workspace, IEnumerable<ColumnMap> nextResultColumnMaps) {
            Debug.Assert(storeDataReader != null, "null storeDataReaders?");
            Debug.Assert(columnMap != null, "null columnMap?");
            Debug.Assert(workspace != null, "null workspace?");

            var shaperInfo = CreateShaperInfo(storeDataReader, columnMap, workspace);
            DbDataReader result = new BridgeDataReader(shaperInfo.Key, shaperInfo.Value, /*depth:*/ 0, GetNextResultShaperInfo(storeDataReader, workspace, nextResultColumnMaps).GetEnumerator());
            return result;
        }
Пример #56
0
 internal static void Compile(
     cqt.DbCommandTree ctree, out List<ProviderCommandInfo> providerCommands, out ColumnMap resultColumnMap, out int columnCount,
     out Set<md.EntitySet> entitySets)
 {
     Assert(ctree != null, "Expected a valid, non-null Command Tree input");
     var pc = new PlanCompiler(ctree);
     pc.Compile(out providerCommands, out resultColumnMap, out columnCount, out entitySets);
 }