Exemplo n.º 1
0
        internal static View ConvertCollection <T>(IEnumerable data)
        {
            var ctype = typeof(T);
            var type  = Common.TryGetSerializationItemType(ctype, data, Text.Method.ToView);

            if (type == null)
            {
                throw new QueryTalkException("Common.TryGetSerializationItemType",
                                             QueryTalkExceptionType.EmptyDynamicResult, String.Format("type = {0}", ctype), Text.Method.ToView);
            }

            Type clrType = null;
            QueryTalkException    exception;
            List <ViewColumnInfo> columns = new List <ViewColumnInfo>();

            bool isScalar = (Mapping.CheckClrCompliance(type, out clrType, out exception) == Mapping.ClrTypeMatch.ClrMatch);

            bool isDataValue = type == typeof(Value);

            if (isDataValue)
            {
                isScalar = true;
                clrType  = typeof(System.Object);
            }

            bool isEmpty    = _CheckIfEmpty(data, isScalar);
            var  properties = type.GetReadableProperties();
            List <IPropertyAccessor> getters = null;
            int numberOfProperties           = 0;
            var sqlOuter = Text.GenerateSql(500);

            var sqlEmpty = Text.GenerateSql(200)
                           .NewLineIndent(Text.Select).S();

            // outer select:
            if (isScalar)
            {
                _BuildScalarOuterSelect(type, clrType, columns, sqlOuter);
            }
            else
            {
                _BuildClassOuterSelect(type, ref clrType, ref exception, columns, isEmpty, properties, out getters,
                                       out numberOfProperties, sqlOuter, sqlEmpty);
            }

            // inner select:
            var sqlInner = Text.GenerateSql(500);
            int rowCount = 0;

            if (!isEmpty)
            {
                bool firstRow = true;
                foreach (var row in data)
                {
                    if (!isScalar && row == null)
                    {
                        continue;
                    }

                    _BuildSelect(sqlInner, firstRow);

                    if (!isScalar)
                    {
                        _BuildClassValues(columns, properties, getters, numberOfProperties, sqlInner, firstRow, row);
                    }
                    else
                    {
                        AppendColumn(sqlInner, Mapping.BuildUnchecked(row), Text.SingleColumnShortName);
                    }

                    firstRow = false;
                    ++rowCount;
                }
            }
            else
            {
                if (isScalar)
                {
                    AppendColumn(sqlEmpty, Text.Null, Text.SingleColumnShortName);
                }

                sqlInner.Append(sqlEmpty.ToString());
            }

            return(Finalizer(type, sqlOuter, sqlInner, columns.ToArray(), rowCount, isEmpty));
        }