Beispiel #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));
        }
Beispiel #2
0
        private static void _BuildClassOuterSelect(Type type, ref Type clrType, ref QueryTalkException exception, List <ViewColumnInfo> columns,
                                                   bool isEmpty, PropertyInfo[] properties, out List <IPropertyAccessor> getters, out int numberOfProperties,
                                                   StringBuilder sqlOuter, StringBuilder sqlEmpty)
        {
            numberOfProperties = properties.Length;
            if (numberOfProperties == 0)
            {
                throw new QueryTalkException("ViewConverter.ToView<T>", QueryTalkExceptionType.InvalidDataClass,
                                             String.Format("data class = {0}", type));
            }

            bool cached = Cache.PropertyAccessors.TryGetValue(type, out getters);

            if (!cached)
            {
                getters = new List <IPropertyAccessor>();
            }

            NodeMap rowMap  = null;
            bool    isDbRow = type.IsDbRow();

            if (isDbRow)
            {
                rowMap = DbMapping.GetNodeMap(type);
                if (rowMap.ID.Equals(DB3.Default))
                {
                    DbRow.ThrowInvalidDbRowException(type);
                }
            }

            // outer select:
            int i = 0;

            foreach (var property in properties)
            {
                string column;

                var clrTypeMatch = Mapping.CheckClrCompliance(property.PropertyType, out clrType, out exception);
                if (clrTypeMatch != Mapping.ClrTypeMatch.ClrMatch)
                {
                    continue;
                }

                ViewColumnInfo columnInfo;
                if (isDbRow)
                {
                    var rowColumn = rowMap.Columns.Where(a => a.ID.ColumnZ == i + 1).First();
                    column     = rowColumn.Name.Part1;
                    columnInfo = new ViewColumnInfo(column, rowColumn.DataType, rowColumn.IsNullable);
                    columns.Add(columnInfo);
                }
                else
                {
                    column     = property.Name;
                    columnInfo = new ViewColumnInfo(column, property.PropertyType, clrType);
                    columns.Add(columnInfo);
                }

                if (i != 0)
                {
                    sqlOuter.NewLineIndent(Text.Comma);
                    sqlEmpty.Append(Text.Comma);
                }

                var dataType = Mapping.ProvideDataType(columnInfo.DataType, clrType);
                AppendOuterColumn(sqlOuter, dataType, i + 1, column);

                if (isEmpty)
                {
                    AppendNullValueColumn(sqlEmpty, i + 1);
                }

                if (!cached)
                {
                    getters.Add(PropertyAccessor.Create(type, property));
                }

                ++i;
            }

            numberOfProperties = i;
            if (numberOfProperties == 0)
            {
                ThrowInvalidDataClassException(type);
            }

            if (!cached)
            {
                Cache.PropertyAccessors[type] = getters;
            }
        }
Beispiel #3
0
        internal static T SetClass <T>(T target, object source)

        {
            if (target == null)
            {
                throw new QueryTalkException("ClassConverter.ToClass", QueryTalkExceptionType.ArgumentNull,
                                             "target= null", Text.Method.Pack);
            }

            if (source == null)
            {
                throw new QueryTalkException("ClassConverter.ToClass", QueryTalkExceptionType.ArgumentNull,
                                             "source = null", Text.Method.Pack);
            }

            if (target != null && object.ReferenceEquals(target, source))
            {
                return(target);
            }

            Type targetType = target.GetType();
            Type sourceType = source.GetType();

            TryCheckClassType(targetType);
            TryCheckClassType(sourceType);

            var sourceProperties = sourceType.GetReadableProperties();
            var targetProperties = targetType.GetWritableProperties();

            if (targetProperties.Length == 0)
            {
                throw new QueryTalkException("ClassConverter.TryCheckClassType", QueryTalkExceptionType.InvalidPack,
                                             String.Format("type = {0}", targetType), Text.Method.Pack);
            }
            if (sourceProperties.Length == 0)
            {
                throw new QueryTalkException("ClassConverter.TryCheckClassType", QueryTalkExceptionType.InvalidPack,
                                             String.Format("type = {0}", sourceType), Text.Method.Pack);
            }

            bool match = false;
            Type clrType;
            QueryTalkException exception;

            foreach (var targetProperty in targetProperties)
            {
                var clrTypeMatch = Mapping.CheckClrCompliance(targetProperty.PropertyType, out clrType, out exception);
                if (clrTypeMatch != Mapping.ClrTypeMatch.ClrMatch)
                {
                    continue;
                }

                var sourceProperty = sourceProperties.Where(p => p.Name == targetProperty.Name).FirstOrDefault();
                if (sourceProperty != null)
                {
                    if (Common.GetClrType(sourceProperty.PropertyType) != Common.GetClrType(targetProperty.PropertyType))
                    {
                        throw new QueryTalkException("ClassConverter.ToClass", QueryTalkExceptionType.PackPropertyMismatch,
                                                     String.Format("target property name = {0}{1}   target property type = {2}{1}   source property type = {3}",
                                                                   targetProperty.Name, Environment.NewLine, targetProperty.PropertyType, sourceProperty.PropertyType),
                                                     Text.Method.Pack);
                    }

                    match = true;
                    targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null);
                }
            }

            if (!match)
            {
                throw new QueryTalkException("ClassConverter.TryCheckClassType", QueryTalkExceptionType.InvalidPack,
                                             String.Format("target type = {0}{1}   source type = {2}",
                                                           targetType, Environment.NewLine, sourceType),
                                             Text.Method.Pack);
            }

            return(target);
        }
Beispiel #4
0
        // build the most outer SQL wrapper
        internal string BuildOutputWrapper(string execSql)
        {
            var root            = Executable.Compilable.GetRoot();
            var outputArguments = ParameterArgument.GetOutputArguments(Executable.Arguments);
            var sql             = Text.GenerateSql(1000).Append(Text.Free.QueryTalkCode);

            // after: drop temp tables
            var sqlAfter = Text.GenerateSql(100);

            sqlAfter.Append(DropTempTables());

            if (root.IsEmbeddedTryCatch)
            {
                sqlAfter
                .NewLine(Text.EndTry)
                .NewLine(Text.BeginCatch)
                .NewLine(Text.Free.RaiserrorS)
                .NewLine(Text.EndCatch).Terminate();
            }

            sql.Append(Text.Declare).S().Append(Text.Reserved.ReturnValueOuterParam)
            .Append(Text._As_).Append(Text.Free.EnclosedInt).Terminate().S()
            .Append(Text.Set).S().Append(Text.Reserved.ReturnValueOuterParam).Append(Text._Equal_)
            .Append(Text.Zero).Terminate();

            // TRY outer wrapper
            if (root.IsEmbeddedTryCatch)
            {
                sql.NewLine(Text.BeginTry);
            }

            // output arguments
            string outputValues = String.Empty;

            foreach (var argument in outputArguments)
            {
                // param in outer wrapper that holds the outer reference
                string paramOuterName = String.Format("{0}{1}{2}", argument.ParamName, Text.Underscore, Text.Output);

                // before
                sql.NewLine(Text.Declare).S()
                .Append(paramOuterName).Append(Text._As_)
                .Append(Executable.GetParamDeclaration(argument, false, true))
                .Terminate().S()
                .Append(Text.Set).S().Append(paramOuterName).Append(Text._Equal_);

                if (argument.TestValue != null)
                {
                    Testing.AppendTestValue(sql, argument);
                }
                else
                {
                    sql.Append(Mapping.BuildUnchecked(argument.Value));
                }

                sql.TerminateSingle();

                // after: return output values
                outputValues = Text.GenerateSql(100)
                               .NewLineIndent(Text.Comma)
                               .Append(paramOuterName)
                               .Append(Text._As_)
                               .Append(Filter.Delimit(paramOuterName))
                               .ToString();
            }

            // append last sql code: return value + output values
            sqlAfter
            .NewLine(Text.Select).S()
            .Append(Text.Free.ReturnValue)
            .Append(Text._As_)
            .Append(Text.Reserved.ReturnValueColumnName)
            .Append(outputValues);

            TryThrow(Text.Method.Pass);

            sql.NewLine(execSql)
            .Append(sqlAfter.ToString())
            .TerminateSingle();

            return(sql.ToString());
        }
Beispiel #5
0
        internal static IEnumerable <T> PackRows <T>(IEnumerable source)
            where T : new()
        {
            if (source == null)
            {
                return(null);
            }

            Type targetType = typeof(T);
            Type sourceType = null;

            foreach (var row in source)
            {
                if (row == null)
                {
                    continue;
                }

                sourceType = row.GetType();
                break;
            }

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

            TryCheckClassType(targetType);
            TryCheckClassType(sourceType);

            var sourceProperties = sourceType.GetReadableProperties();
            var targetProperties = targetType.GetWritableProperties();

            if (targetProperties.Length == 0)
            {
                throw new QueryTalkException("ClassConverter.TryCheckClassType", QueryTalkExceptionType.InvalidPack,
                                             String.Format("type = {0}", targetType), Text.Method.Pack);
            }
            if (sourceProperties.Length == 0)
            {
                throw new QueryTalkException("ClassConverter.TryCheckClassType", QueryTalkExceptionType.InvalidPack,
                                             String.Format("type = {0}", sourceType), Text.Method.Pack);
            }

            var sourceGetters = new List <IPropertyAccessor>();
            var targetGetters = new List <IPropertyAccessor>();

            bool match = false;
            Type clrType;
            QueryTalkException exception;
            var numberOfPropertiesUsed = 0;

            foreach (var targetProperty in targetProperties)
            {
                var clrTypeMatch = Mapping.CheckClrCompliance(targetProperty.PropertyType, out clrType, out exception);
                if (clrTypeMatch != Mapping.ClrTypeMatch.ClrMatch)
                {
                    continue;
                }

                var sourceProperty = sourceProperties.Where(p => p.Name == targetProperty.Name).FirstOrDefault();
                if (sourceProperty != null)
                {
                    if (Common.GetClrType(sourceProperty.PropertyType) != Common.GetClrType(targetProperty.PropertyType))
                    {
                        throw new QueryTalkException("ClassConverter.ToClass", QueryTalkExceptionType.PackPropertyMismatch,
                                                     String.Format("target property name = {0}{1}   target property type = {2}{1}   source property type = {3}",
                                                                   targetProperty.Name, Environment.NewLine, targetProperty.PropertyType, sourceProperty.PropertyType),
                                                     Text.Method.Pack);
                    }

                    match = true;

                    sourceGetters.Add(PropertyAccessor.Create(sourceType, sourceProperty));
                    targetGetters.Add(PropertyAccessor.Create(targetType, targetProperty));
                    ++numberOfPropertiesUsed;
                }
            }

            if (!match)
            {
                throw new QueryTalkException("ClassConverter.TryCheckClassType", QueryTalkExceptionType.InvalidPack,
                                             String.Format("target type = {0}{1}   source type = {2}",
                                                           targetType, Environment.NewLine, sourceType),
                                             Text.Method.Pack);
            }

            var rows = new HashSet <T>();

            foreach (var sourceRow in source)
            {
                if (sourceRow == null)
                {
                    continue;
                }

                var targetRow = new T();

                for (int i = 0; i < numberOfPropertiesUsed; i++)
                {
                    var value = sourceGetters[i].GetValue(sourceRow);
                    targetGetters[i].SetValue(targetRow, value);
                }

                rows.Add(targetRow);
            }

            return(rows);
        }