public void WillReturnSelectedFieldsWithInstance()
        {
            TableObjectMapping expected = null;
            TestClass          model    = null;

            throw new NotImplementedException();
//            model = ObjectCreator.CreateNew<TestClass>();
//
//            expected = new TableObjectMapping
//            {
//                TableName = "dbo.TestTable",
//                FieldMappings = new Dictionary<string, FieldParameterMapping>
//                    {
//                        {"Id", new FieldParameterMapping("TestClassId", "@id", SqlDbType.Int, model.Id, true)},
//                        {"Bar", new FieldParameterMapping("PioneerSquareBar", "@itsFridayLetsGoToTheBar", SqlDbType.NVarChar, model.Bar)}
//                    }
//            };
//
//            var actual = SystemUnderTest.BuildFields(new[] {"Id", "Bar"}, model: model);
//            Expression<Action<KeyValuePair<string, FieldParameterMapping>, KeyValuePair<string, FieldParameterMapping>>> expression =
//                (e, a) => CompareFieldParameterInfos(e, a, f => Assert.IsNotNull(f, null));
//
//            Asserter.AssertEquality(expected, actual, new[] {"FieldMappings", "Joins", "JoinType"});
//
//            Asserter.AssertEquality(expected.FieldMappings, actual.FieldMappings, additionalParameters:
//                new Dictionary<string, object>
//                {
//                    {Norml.Tests.Common.Constants.ParameterNames.ComparisonDelegate, expression}
//                });
        }
        private void BuildAndLinkChild <TParent>(IDataReader dataSource, PropertyInfo primaryKeyProperty, IDictionary <object, TParent> parents,
                                                 TableObjectMapping tableObjectMapping)
        {
            var childPropertyName          = tableObjectMapping.ChildKey;
            var parentPropertyName         = tableObjectMapping.ParentKey;
            var childType                  = tableObjectMapping.JoinType;
            var prefix                     = tableObjectMapping.Prefix;
            var parentInstancePropertyName = tableObjectMapping.InstancePropertyName;

            //VerifyParentPropertyName(primaryKeyProperty, parentPropertyName);

            var child = BuildConvertedChild(dataSource, childType, prefix);
            var childPropertyValue = GetChildPropertyValue(childType, childPropertyName, child);

            // TODO: Change to KeyNotFoundException as time permits.
            if (!parents.ContainsKey(childPropertyValue))
            {
                throw new InvalidOperationException("Could not locate parent key from child.");
            }

            var parent = parents[childPropertyValue];
            var parentInstanceProperty = GetParentInstanceProperty(parentInstancePropertyName, parent);

            LinkChildToParent(child, parent, parentInstanceProperty);

            parents[childPropertyValue] = parent;
        }
        private TableObjectMapping AddJoins(TypeMapping mapping, TableObjectMapping parent, TableObjectMapping child)
        {
            throw new NotImplementedException();
            //if (attribute.JoinTable.IsNotNullOrEmpty())
            //{
            //    BuildComplexJoin(attribute, parent, child);
            //}
            //else // No need to join through Network table.
            //{
            //    BuildSimpleJoin(attribute, parent, child);
            //}

            //return parent;
        }
 private static void BuildSimpleJoin(TypeMapping mapping, TableObjectMapping parent, TableObjectMapping child)
 {
     throw new NotImplementedException();
     //parent.Do(() => parent.Joins.Add(new Join
     //{
     //    JoinType = attribute.JoinType,
     //    LeftJoinField = attribute.LeftKey,
     //    LeftTableName = parent.TableName,
     //    LeftTableAlias = parent.Alias,
     //    LeftJoinFieldPrefix = parent.Alias.IsNullOrEmpty() ? String.Empty : "{0}_".FormatString(parent.Alias),
     //    RightJoinField = attribute.RightKey,
     //    RightTableName = child.TableName,
     //    RightTableAlias = child.Alias,
     //    RightJoinFieldPrefix = child.Alias.IsNullOrEmpty() ? String.Empty : "{0}_".FormatString(child.Alias)
     //}));
 }
        public void WillReturnAllParametersIfIgnoreIdentityIsFalse()
        {
            IEnumerable <IDbDataParameter> expected = null;
            TableObjectMapping             values   = null;

            var id        = DataGenerator.GenerateInteger();
            var barRescue = DataGenerator.GenerateDateTime();
            var baz       = DataGenerator.GenerateString();

            throw new NotImplementedException();

//            values = new TableObjectMapping
//            {
//                TableName = DataGenerator.GenerateString(),
//                FieldMappings = new Dictionary<string, FieldParameterMapping>
//                    {
//                        {"Id", new FieldParameterMapping("TestId", "@theId", SqlDbType.Int, id, true)},
//                        {"Bar", new FieldParameterMapping("TestBar", "@barRescue", SqlDbType.DateTime, barRescue)},
//                        {"Baz", new FieldParameterMapping("TestBaz", "@baz", SqlDbType.NVarChar, baz)},
//                        {"Bak", new FieldParameterMapping("TestBak", "@bak", SqlDbType.NVarChar)}
//                    }
//            };

//            expected = new List<IDbDataParameter>
//            {
//                new SqlParameter("@theId", SqlDbType.Int) {Value = id},
//                new SqlParameter("@barRescue", SqlDbType.DateTime) {Value = barRescue},
//                new SqlParameter("@baz", SqlDbType.NVarChar) {Value = baz},
//                new SqlParameter("@bak", SqlDbType.NVarChar) {Value = DBNull.Value}
//            };
//
//            var actual = SystemUnderTest.ExtractParameters(values, false);
//            Expression<Action<IDbDataParameter, IDbDataParameter>> expression =
//                (e, a) => CompareParameters(e, a);
//
//            Asserter.AssertEquality(expected, actual, additionalParameters:
//                new Dictionary<string, object>
//                {
//                    {
//                        Norml.Tests.Common.Constants.ParameterNames.ComparisonDelegate,
//                        expression
//                    }
//                });
        }
        protected string BuildCommonTableExpression(
            TableObjectMapping tableFieldInfo, string orderByClause,
            string countClause, bool allowDirtyRead = false, string whereClause = null)
        {
            var queryBuilder = new StringBuilder();

            queryBuilder.AppendFormat("WITH {0} AS ", ItemNames.SortedPageCteName);
            queryBuilder.Append("( ");
            queryBuilder.Append("SELECT ");
            queryBuilder.Append(tableFieldInfo
                                .FieldMappings
                                .Select(f => f.Value.FieldName)
                                .ToDelimitedString(", "));
            queryBuilder.Append(", ");
            queryBuilder.Append("ROW_NUMBER() ");
            queryBuilder.Append("OVER ( ");
            queryBuilder.Append(orderByClause);
            queryBuilder.Append(" ");
            queryBuilder.Append(") AS RowNumber, ");
            queryBuilder.Append(countClause);
            queryBuilder.AppendFormat("FROM {0} ", tableFieldInfo.TableName);

            if (allowDirtyRead)
            {
                queryBuilder.Append("(NOLOCK) ");
            }

            if (whereClause.IsNotNullOrEmpty())
            {
                queryBuilder.AppendFormat("WHERE {0} ", whereClause);
            }

            queryBuilder.Append(") ");

            return(queryBuilder.ToString());
        }
        private static void BuildComplexJoin(TypeMapping mapping, TableObjectMapping parent, TableObjectMapping child)
        {
            throw new NotImplementedException();
            //var parentTableName = parent.TableName;
            //var parentAlias = parent.Alias;
            //var leftKey = attribute.LeftKey;

            //var joinTableName = attribute.JoinTable;
            //var joinTableLeftKey = attribute.JoinTableLeftKey;
            //var joinTableRightKey = attribute.JoinTableRightKey;
            //var joinTableJoinType = attribute.JoinTableJoinType;

            //var childTableName = child.TableName;
            //var childAlias = child.Alias;
            //var rightKey = attribute.RightKey;

            //parent.Joins.Add(new Join
            //{
            //    JoinType = joinTableJoinType,
            //    LeftJoinField = "{0}_{1}".FormatString(parent.Alias, leftKey),
            //    LeftTableName = parentTableName,
            //    LeftTableAlias = parentAlias,
            //    RightJoinField = joinTableLeftKey,
            //    RightTableName = joinTableName,
            //});

            //parent.Joins.Add(new Join
            //{
            //    JoinType = joinTableJoinType,
            //    LeftJoinField = joinTableRightKey,
            //    LeftTableName = joinTableName,
            //    RightJoinField = "{0}_{1}".FormatString(childAlias, rightKey),
            //    RightTableAlias = childAlias,
            //    RightTableName = childTableName
            //});
        }