private IDataReader GetRelatedTableReader(string table_name)
        {
            var metadata = (TMappingInfo)MappingFactory.GetMapping(typeof(TMappingInfo));
            var item     = metadata.Associations.Where(x => x.Name == table_name).FirstOrDefault();

            if (item == null)
            {
                return(null);
            }

            var omapping = MappingFactory.GetMapping(item.OtherMappingType);

            var qry = (new SelectStatement())
                      .Column(omapping.Columns.Where(x => x.Name == item.OtherKeys[0]).Single().ColumnName)
                      .From(omapping.TableName);

            var lblCount = item.OtherLabels.Count;

            if (lblCount == 0)
            {
                return(null);
            }
            else
            {
                if (lblCount == 1)
                {
                    qry.Column(omapping.Columns.Where(x => x.Name == item.OtherLabels[0]).Single().ColumnName);
                }
                else
                {
                    var lblConcat = new ConcatFunction(" | ");
                    foreach (var lbl in item.OtherLabels)
                    {
                        lblConcat.Append(new SqlColumnName(omapping.Columns.Where(x => x.Name == lbl).Single().ColumnName));
                    }
                    qry.Column(lblConcat);
                }
            }

            return(qry.ExecuteReader());
        }
        private void AddDataTable(string table_name, string table_alias, IMapping mapping)
        {
            if (this.Tables.Contains(table_alias))
            {
                return;
            }

            var table = new DataTable(table_alias);
            var keys  = new List <DataColumn>();

            this.Tables.Add(table);
            var columns = table.Columns;
            var select  = (new SelectStatement()).From(table_name, "t0");

            table.ExtendedProperties["Storage"] = table_name;
            foreach (var item in mapping.Columns)
            {
                if (IgnoreMemoFields && item.IsMemo)
                {
                    continue;
                }

                var        pinfo = (PropertyInfo)item.Property;
                DataColumn column;
                if (pinfo.PropertyType.IsGenericType && pinfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    column = columns.Add(item.Name, pinfo.PropertyType.GetGenericArguments()[0]);
                }
                else if (pinfo.PropertyType.IsEnum)
                {
                    column = columns.Add(item.Name, typeof(string));
                }
                else
                {
                    column = columns.Add(item.Name, pinfo.PropertyType);
                }
                select.Column(new SqlColumnName("t0", item.ColumnName), item.Name);

                if (item.IsDbGenerated)
                {
                    column.AutoIncrement     = true;
                    column.AutoIncrementSeed = 1;
                    column.AutoIncrementStep = 1;
                    column.Unique            = true;
                }
                if (item.IsPrimaryKey)
                {
                    column.ExtendedProperties["PrimaryKey"] = true;
                    keys.Add(column);
                }
                column.AllowDBNull = item.IsNullable;
                //if (item.IsNullable && item.DefaultValue != null)
                //    column.DefaultValue = item.DefaultValue;

                if (!item.IsDbGenerated)
                {
                    if (item.DefaultFunctionValue == null)
                    {
                        column.DefaultValue = item.DefaultValue;
                    }
                    else
                    {
                        column.DefaultValue = item.DefaultFunctionValue();
                    }
                }

                column.ExtendedProperties["Storage"] = item.ColumnName;
                if (item.IsMemo)
                {
                    column.ExtendedProperties["Memo"] = true;
                }
            }
            table.ExtendedProperties["SelectStatement"] = select;

            if (keys.Count > 0)
            {
                table.PrimaryKey = keys.ToArray();
            }


            /* Procedimiento para añadir las columnas descriptivas de las columnas foreanas */
            var items = mapping.Associations.Where(x => x.GetType().Name == "MakeReferenceToMapping`2" || x.GetType().Name == "HasOneMapping`2");

            if (items.Count() == 0)
            {
                return;
            }

            //Debes poner las otras columnas con el nombre fisico alias el nombre de la propiedad
            int    t  = 1;
            string tp = "t" + t;

            foreach (var item in items)
            {
                var omapping = MappingFactory.GetMapping(item.OtherMappingType);

                // extra columns for criteria support
                for (var x = 0; x < omapping.Columns.Count; x++)
                {
                    var col = omapping.Columns[x];
                    select.Column(new SqlColumnName(tp, col.ColumnName), item.Name + "." + col.Name);
                }

                /* build joins */

                //for (var k = 0; k < item.ThisKeys.Count; k++ )
                //{
                var keyName      = item.ThisKeys[0];
                var otherKeyName = item.OtherKeys[0];
                var tcol         = mapping.Columns.Where(x => x.Name == keyName).Single();
                var ocol         = omapping.Columns.Where(x => x.Name == otherKeyName).Single();
                if (tcol.IsNullable || item.GetType().Name == "HasOneMapping`2")
                {
                    select.LeftJoin(omapping.TableName, tp, Condition.IsEqual(new SqlColumnName("t0", tcol.ColumnName), new SqlColumnName(tp, ocol.ColumnName)));
                }
                else
                {
                    select.InnerJoin(omapping.TableName, tp, Condition.IsEqual(new SqlColumnName("t0", tcol.ColumnName), new SqlColumnName(tp, ocol.ColumnName)));
                }
                //}
                /* build foreign row name */
                var lblCount = item.OtherLabels.Count;
                if (lblCount > 0)
                {
                    var lbl0 = item.OtherLabels[0];
                    //var colName = omapping.Columns.Where(x => x.Name == lbl0).Single().ColumnName;

                    ISqlExpression lblExpr;
                    if (lblCount == 1)
                    {
                        lblExpr = new SqlColumnName(item.Name + "." + item.OtherLabels[0]);
                    }
                    else
                    {
                        var lblConcat = new ConcatFunction(" | ");
                        foreach (var lbl in item.OtherLabels)
                        {
                            lblConcat.Append(new SqlColumnName(item.Name, lbl));
                        }
                        lblExpr = lblConcat;
                    }
                    var column = columns.Add(item.Name, typeof(string));
                    column.ExtendedProperties["SelectExpression"] = lblExpr;
                    column.ExtendedProperties["NonStorable"]      = true;
                }

                // Include OtherColumns in DataTable
                foreach (string ocn in item.OtherColumns)
                {
                    var column = columns.Add(omapping.EntityName + "." + ocn, typeof(string));
                    column.ExtendedProperties["NonStorable"] = true;
                }

                t++;
                tp = "t" + t;
            }
        }