public ServiceModelDataReader(MappingSchema mappingSchema, LinqServiceResult result)
		{
			_mappingSchema = mappingSchema;
			_result        = result;

			for (var i = 0; i < result.FieldNames.Length; i++)
				_ordinal.Add(result.FieldNames[i], i);
		}
示例#2
0
        public ServiceModelDataReader(MappingSchema mappingSchema, LinqServiceResult result)
        {
            _mappingSchema = mappingSchema;
            _result        = result;

            for (var i = 0; i < result.FieldNames.Length; i++)
            {
                _ordinal.Add(result.FieldNames[i], i);
            }
        }
示例#3
0
        public string ExecuteReader(string configuration, string queryData)
        {
            try
            {
                var query = LinqServiceSerializer.Deserialize(queryData);

                ValidateQuery(query);

                using (var db = CreateDataContext(configuration))
                    using (db.DataProvider.ExecuteScope())
                    {
                        using (var rd = DataConnection.QueryRunner.ExecuteReader(db, new QueryContext
                        {
                            Statement = query.Statement,
                            Parameters = query.Parameters,
                            QueryHints = query.QueryHints
                        }))
                        {
                            var ret = new LinqServiceResult
                            {
                                QueryID    = Guid.NewGuid(),
                                FieldCount = rd.FieldCount,
                                FieldNames = new string[rd.FieldCount],
                                FieldTypes = new Type  [rd.FieldCount],
                                Data       = new List <string[]>(),
                            };

                            var names = new HashSet <string>();

                            for (var i = 0; i < ret.FieldCount; i++)
                            {
                                var name = rd.GetName(i);
                                var idx  = 0;

                                if (names.Contains(name))
                                {
                                    while (names.Contains(name = "c" + ++idx))
                                    {
                                    }
                                }

                                names.Add(name);

                                ret.FieldNames[i] = name;
                                ret.FieldTypes[i] = rd.GetFieldType(i);
                            }

                            var varyingTypes = new List <Type>();

                            while (rd.Read())
                            {
                                var data  = new string  [rd.FieldCount];
                                var codes = new TypeCode[rd.FieldCount];

                                for (var i = 0; i < ret.FieldCount; i++)
                                {
                                    codes[i] = Type.GetTypeCode(ret.FieldTypes[i].ToNullableUnderlying());
                                }

                                ret.RowCount++;

                                for (var i = 0; i < ret.FieldCount; i++)
                                {
                                    if (!rd.IsDBNull(i))
                                    {
                                        var code = codes[i];
                                        var type = rd.GetFieldType(i);
                                        var idx  = -1;

                                        if (type != ret.FieldTypes[i])
                                        {
                                            code = Type.GetTypeCode(type);
                                            idx  = varyingTypes.IndexOf(type);

                                            if (idx < 0)
                                            {
                                                varyingTypes.Add(type);
                                                idx = varyingTypes.Count - 1;
                                            }
                                        }

                                        switch (code)
                                        {
                                        case TypeCode.Decimal: data[i] = rd.GetDecimal(i).ToString(CultureInfo.InvariantCulture); break;

                                        case TypeCode.Double: data[i] = rd.GetDouble(i).ToString(CultureInfo.InvariantCulture); break;

                                        case TypeCode.Single: data[i] = rd.GetFloat(i).ToString(CultureInfo.InvariantCulture); break;

                                        case TypeCode.DateTime: data[i] = rd.GetDateTime(i).ToString("o");                          break;

                                        default:
                                        {
                                            if (type == typeof(DateTimeOffset))
                                            {
                                                var dt = rd.GetValue(i);

                                                if (dt is DateTime)
                                                {
                                                    data[i] = ((DateTime)dt).ToString("o");
                                                }
                                                else if (dt is DateTimeOffset)
                                                {
                                                    data[i] = ((DateTimeOffset)dt).ToString("o");
                                                }
                                                else
                                                {
                                                    data[i] = rd.GetValue(i).ToString();
                                                }
                                            }
                                            else if (type == typeof(byte[]))
                                            {
                                                data[i] = ConvertTo <string> .From((byte[])rd.GetValue(i));
                                            }
                                            else
                                            {
                                                data[i] = (rd.GetValue(i) ?? "").ToString();
                                            }

                                            break;
                                        }
                                        }

                                        if (idx >= 0)
                                        {
                                            data[i] = "\0" + (char)idx + data[i];
                                        }
                                    }
                                }

                                ret.Data.Add(data);
                            }

                            ret.VaryingTypes = varyingTypes.ToArray();

                            return(LinqServiceSerializer.Serialize(ret));
                        }
                    }
            }
            catch (Exception exception)
            {
                HandleException(exception);
                throw;
            }
        }
示例#4
0
        public string ExecuteReader(string configuration, string queryData)
        {
            try
            {
                var query = LinqServiceSerializer.Deserialize(queryData);

                ValidateQuery(query);

                using (IDataContext db = CreateDataContext(configuration))
                {
                    var obj = db.SetQuery(new QueryContext { SqlQuery = query.Query, Parameters = query.Parameters });

                    using (var rd = db.ExecuteReader(obj))
                    {
                        var ret = new LinqServiceResult
                        {
                            QueryID    = Guid.NewGuid(),
                            FieldCount = rd.FieldCount,
                            FieldNames = new string[rd.FieldCount],
                            FieldTypes = new Type  [rd.FieldCount],
                            Data       = new List<string[]>(),
                        };

                        for (var i = 0; i < ret.FieldCount; i++)
                        {
                            ret.FieldNames[i] = rd.GetName(i);
                            ret.FieldTypes[i] = rd.GetFieldType(i);
                        }

                        var varyingTypes = new List<Type>();

                        while (rd.Read())
                        {
                            var data  = new string  [rd.FieldCount];
                            var codes = new TypeCode[rd.FieldCount];

                            for (var i = 0; i < ret.FieldCount; i++)
                                codes[i] = Type.GetTypeCode(ret.FieldTypes[i]);

                            ret.RowCount++;

                            for (var i = 0; i < ret.FieldCount; i++)
                            {
                                if (!rd.IsDBNull(i))
                                {
                                    var code = codes[i];
                                    var type = rd.GetFieldType(i);
                                    var idx = -1;

                                    if (type != ret.FieldTypes[i])
                                    {
                                        code = Type.GetTypeCode(type);
                                        idx  = varyingTypes.IndexOf(type);

                                        if (idx < 0)
                                        {
                                            varyingTypes.Add(type);
                                            idx = varyingTypes.Count - 1;
                                        }
                                    }

                                    switch (code)
                                    {
                                        case TypeCode.Decimal  : data[i] = rd.GetDecimal (i).ToString(CultureInfo.InvariantCulture); break;
                                        case TypeCode.Double   : data[i] = rd.GetDouble  (i).ToString(CultureInfo.InvariantCulture); break;
                                        case TypeCode.Single   : data[i] = rd.GetFloat   (i).ToString(CultureInfo.InvariantCulture); break;
                                        case TypeCode.DateTime : data[i] = rd.GetDateTime(i).ToString("o");                          break;
                                        default                :
                                            {
                                                if (type == typeof(DateTimeOffset))
                                                {
                                                    var dt = rd.GetValue(i);

                                                    if (dt is DateTime)
                                                        data[i] = ((DateTime)dt).ToString("o");
                                                    else if (dt is DateTimeOffset)
                                                        data[i] = ((DateTimeOffset)dt).ToString("o");
                                                    else
                                                        data[i] = rd.GetValue(i).ToString();
                                                }
                                                else if (ret.FieldTypes[i] == typeof(byte[]))
                                                    data[i] = ConvertTo<string>.From((byte[])rd.GetValue(i));
                                                else
                                                    data[i] = (rd.GetValue(i) ?? "").ToString();

                                                break;
                                            }
                                    }

                                    if (idx >= 0)
                                        data[i] = "\0" + (char)idx + data[i];
                                }
                            }

                            ret.Data.Add(data);
                        }

                        ret.VaryingTypes = varyingTypes.ToArray();

                        return LinqServiceSerializer.Serialize(ret);
                    }
                }
            }
            catch (Exception exception)
            {
                HandleException(exception);
                throw;
            }
        }