public ServiceModelDataReader(LinqServiceResult result)
		{
			_result = result;

			for (var i = 0; i < result.FieldNames.Length; i++)
				_ordinal.Add(result.FieldNames[i], i);
		}
        public ServiceModelDataReader(LinqServiceResult result)
        {
            _result = result;

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

                ValidateQuery(query);

                using (var db = CreateDataContext())
                {
                    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;

                                    case TypeCode.Boolean: data[i] = rd.GetBoolean(i).ToString(CultureInfo.InvariantCulture); 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] = Convert.ToBase64String((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;
            }
        }
Example #4
0
		public LinqServiceResult ExecuteReader(LinqServiceQuery query)
		{
			ValidateQuery(query);

			using (var db = CreateDataContext())
			{
				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(CultureInfo.InvariantCulture); break;
									default                :
										{
											if (ret.FieldTypes[i] == typeof(byte[]))
												data[i] = Convert.ToBase64String((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 ret;
				}
			}
		}