コード例 #1
0
        /// <summary>
        /// Created IActionResult object that contains the processed response result.
        /// </summary>
        /// <param name="pipe">Sql Pipe that will be used to fetch the data.</param>
        /// <returns>ContentResult with the data processed by request.</returns>
        public virtual async Task <IActionResult> GetResult(IQueryMapper mapper)
        {
            IActionResult result = null;

            try
            {
                var json = await mapper
                           .GetString(cmd)
                           .ConfigureAwait(false);

                result = new ContentResult()
                {
                    Content     = json,
                    StatusCode  = StatusCodes.Status200OK,
                    ContentType = "application/json"
                };
            } catch (Exception ex)
            {
                result = new ContentResult()
                {
                    Content    = ex.Message,
                    StatusCode = StatusCodes.Status500InternalServerError
                };
            }

            return(result);
        }
コード例 #2
0
        public async Task ReturnsEmptyString()
        {
            // Action
            var response = await mapper.GetString("select * from sys.all_objects where 1 = 0");

            // Assert
            Assert.Equal("", response);
        }
コード例 #3
0
        /// <summary>
        /// Executes sql statement and returns concatenated result as string.
        /// </summary>
        /// <param name="sql">SQL query that will be executed.</param>
        /// <returns>Task</returns>
        public static async Task <string> GetString(this IQueryMapper mapper, string sql)
        {
            var cmd = new SqlCommand(sql);

            return(await mapper.GetString(cmd));
        }
コード例 #4
0
        public async Task ReturnsXml([CombinatorialValues("stream", "writer", "mapper", "command")] string client,
                                     [CombinatorialValues(1, 5, 500, 1000)] string top,
                                     [CombinatorialValues("auto", "path", "raw")] string mode,
                                     [CombinatorialValues("", "test")] string rootmode,
                                     [CombinatorialValues(true, false)] bool useCommand)
        {
            // Arrange
            string sql = "select top " + top + " 'CONST_ID' as const, o.object_id tid, o.name t_name, o.schema_id, o.type t, o.type_desc, o.create_date cd, o.modify_date md from sys.objects o for xml " + mode + ", root" + (rootmode != ""?"('" + rootmode + "')":"");
            var    xml = new XmlDocument();

            // Action
            if (client == "stream")
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    if (useCommand)
                    {
                        await pipe.Sql(new SqlCommand(sql)).Stream(ms);
                    }
                    else
                    {
                        await pipe.Sql(sql).Stream(ms);
                    }
                    ms.Position = 0;
                    xml.Load(ms);
                }
            }
            else if (client == "writer")
            {
                using (var sw = new StringWriter())
                {
                    if (useCommand)
                    {
                        await pipe.Sql(new SqlCommand(sql)).Stream(sw, "<root/>");
                    }
                    else
                    {
                        await pipe.Sql(sql).Stream(sw, "<root/>");
                    }
                    xml.LoadXml(sw.ToString());
                }
            }
            else if (client == "command")
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    if (useCommand)
                    {
                        await command.Sql(new SqlCommand(sql)).Stream(ms, "<root/>");
                    }
                    else
                    {
                        await command.Sql(sql).Stream(ms, "<root/>");
                    }
                    ms.Position = 0;
                    xml.LoadXml(new StreamReader(ms).ReadToEnd());
                }
            }
            else
            {
                if (useCommand)
                {
                    xml.LoadXml(await mapper.GetString(new SqlCommand(sql)));
                }
                else
                {
                    xml.LoadXml(await mapper.GetString(sql));
                }
            }

            // Assert

            //-- auto, root             root / o / @const
            //-- auto, root('test')     test / o / @const
            //-- raw, root('test')      test / row / @const
            //-- raw, root              root / row / @const
            //-- path, root             root / row /const
            //-- path, root('test')     test / row /const

            string prefix = (rootmode == "")?"root":"test";

            if (mode == "auto")
            {
                prefix += "/o/";
            }
            else
            {
                prefix += "/row/";
            }

            if (mode == "raw" || mode == "auto")
            {
                prefix += "@";
            }

            Assert.True(xml.ChildNodes[0].ChildNodes.Count > 0);
            Assert.Equal("CONST_ID", xml.SelectSingleNode("//" + prefix + "const").InnerText);
            Assert.NotEmpty(xml.SelectSingleNode("//" + prefix + "tid").InnerText);
        }