Exemplo n.º 1
0
        } // GetAssemblyQualifiedNoVersionName

        private static async System.Threading.Tasks.Task WriteAssociativeColumnsArray(
            Newtonsoft.Json.JsonTextWriter jsonWriter
            , System.Data.Common.DbDataReader dr, RenderType_t renderType)
        {
            //await jsonWriter.WriteStartObjectAsync();
            await jsonWriter.WriteStartObjectAsync();

            for (int i = 0; i <= dr.FieldCount - 1; i++)
            {
                await jsonWriter.WritePropertyNameAsync(dr.GetName(i));

                await jsonWriter.WriteStartObjectAsync();

                await jsonWriter.WritePropertyNameAsync("index");

                await jsonWriter.WriteValueAsync(i);

                if (renderType.HasFlag(RenderType_t.WithDetail))
                {
                    await jsonWriter.WritePropertyNameAsync("fieldType");

                    // await jsonWriter.WriteValueAsync(GetAssemblyQualifiedNoVersionName(dr.GetFieldType(i)));
                    await jsonWriter.WriteValueAsync(GetTypeName(dr.GetFieldType(i), renderType));
                }

                await jsonWriter.WriteEndObjectAsync();
            } // Next i

            await jsonWriter.WriteEndObjectAsync();
        } // WriteAssociativeArray
Exemplo n.º 2
0
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            try
            {
                System.Collections.Generic.Dictionary <string, object> pars = SqlServiceHelper.GetParameters(context);

                if (!pars.ContainsKey("sql"))
                {
                    throw new System.Exception("Parameter sql not provided....");
                }


                string sql = System.Convert.ToString(pars["sql"]);
                sql = System.IO.Path.Combine("SQL", sql);
                sql = System.IO.File.ReadAllText(sql, System.Text.Encoding.UTF8);


                RenderType_t format = RenderType_t.Array;

                if (pars.ContainsKey("format"))
                {
                    string form       = System.Convert.ToString(pars["format"]);
                    int    renderType = 1;
                    int.TryParse(form, out renderType);

                    format = (RenderType_t)renderType;
                } // End if (pars.ContainsKey("format"))

                SqlServiceJsonHelper.AnyDataReaderToAnyJson(sql, new MS_SQL_Service(), pars, context, format);

                // throw new Exception("SQL error");
                // await context.Response.WriteAsync("Howdy");
                // context.Response.StatusCode = 500;
            } // End Try
            catch (System.Exception ex)
            {
                // header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
                // header("HTTP/1.0 500 Internal Server Error");
                // header('HTTP/1.1 200 OK');

                // context.Response.Headers["HTTP/1.0 500 Internal Server Error"] = "";
                context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                context.Response.Headers["X-Error-Message"] = "Incorrect username or password";

                context.Response.ContentType = "text/plain";

                context.Response.Write(ex.Message);
                context.Response.Write(System.Environment.NewLine);
                context.Response.Write(System.Environment.NewLine);
                context.Response.Write(ex.StackTrace);
                System.Console.WriteLine();
            } // End Catch
        }
Exemplo n.º 3
0
        } // GetAssemblyQualifiedNoVersionName


        private static string GetTypeName(System.Type type, RenderType_t renderType)
        {
            if (type == null)
                return null;

            if (renderType.HasFlag(RenderType_t.ShortName))
                return type.Name;

            if (renderType.HasFlag(RenderType_t.LongName))
                return type.FullName;

            if (renderType.HasFlag(RenderType_t.AssemblyQualifiedName))
                return GetAssemblyQualifiedNoVersionName(type);

            return type.Name;
        } // GetAssemblyQualifiedNoVersionName
Exemplo n.º 4
0
        } // End Task AsJSON

        public static async System.Threading.Tasks.Task <System.Exception> AsJSON(
            this IDbConnection cnn
            , System.IO.Stream strm
            , string sql
            , RenderType_t format
            , object param = null
            , IDbTransaction transaction = null
            , int?commandTimeout         = null
            , CommandType?commandType    = null)
        {
            System.Exception ex = null;
            using (System.IO.TextWriter output = new System.IO.StreamWriter(strm, new System.Text.UTF8Encoding(false)))
            {
                ex = await AsJSON(cnn, output, sql, format, param, transaction, commandTimeout, commandType);
            } // End Using output

            return(ex);
        } // End Task AsJSON
Exemplo n.º 5
0
        } // GetAssemblyQualifiedNoVersionName

        private static string GetTypeName(System.Type type, RenderType_t renderType)
        {
            if (type == null)
            {
                return(null);
            }

            if (renderType.HasFlag(RenderType_t.AssemblyQualifiedName))
            {
                return(GetAssemblyQualifiedNoVersionName(type));
            }

            if (renderType.HasFlag(RenderType_t.LongName))
            {
                return(type.FullName);
            }

            return(type.Name);
        } // GetAssemblyQualifiedNoVersionName
Exemplo n.º 6
0
        } // End Sub WriteArray

        public static void AnyDataReaderToAnyJson(
            string sql
            , SqlService service
            , System.Collections.Generic.Dictionary <string, object> pars
            , System.Web.HttpContext context
            , RenderType_t format)
        {
            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;
                    service.AddParameterList(pars, cmd);

                    // cmd.ExecuteNonQuery
                    // cmd.ExecuteReader
                    // cmd.ExecuteScalar

                    using (System.Data.Common.DbDataReader dr = cmd.ExecuteReader(
                               System.Data.CommandBehavior.SequentialAccess
                               | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (System.IO.StreamWriter output = new System.IO.StreamWriter(context.Response.OutputStream))
                        {
                            using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                       new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                            {
                                if (format.HasFlag(RenderType_t.Indented))
                                {
                                    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                                }

                                context.Response.StatusCode  = (int)System.Net.HttpStatusCode.OK;
                                context.Response.ContentType = "application/json";


                                jsonWriter.WriteStartObject();

                                jsonWriter.WritePropertyName("tables");
                                jsonWriter.WriteStartArray();

                                do
                                {
                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        jsonWriter.WriteStartObject();
                                        jsonWriter.WritePropertyName("columns");

                                        if (format.HasFlag(RenderType_t.Columns_Associative))
                                        {
                                            WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                        }
                                        else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                        {
                                            WriteComplexArray(jsonWriter, dr, format);
                                        }
                                        else // (format.HasFlag(RenderType_t.Array))
                                        {
                                            WriteArray(jsonWriter, dr);
                                        }
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only))



                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        jsonWriter.WritePropertyName("rows");
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                    jsonWriter.WriteStartArray();

                                    if (dr.HasRows)
                                    {
                                        string[] columns = null;
                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            columns = new string[dr.FieldCount];
                                            for (int i = 0; i < dr.FieldCount; i++)
                                            {
                                                columns[i] = dr.GetName(i);
                                            } // Next i
                                        }     // End if (format.HasFlag(RenderType_t.DataTable))

                                        while (dr.Read())
                                        {
                                            if (format.HasFlag(RenderType_t.DataTable))
                                            {
                                                jsonWriter.WriteStartObject();
                                            }
                                            else
                                            {
                                                jsonWriter.WriteStartArray();
                                            }

                                            for (int i = 0; i <= dr.FieldCount - 1; i++)
                                            {
                                                object obj = dr.GetValue(i);
                                                if (obj == System.DBNull.Value)
                                                {
                                                    obj = null;
                                                }

                                                if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                                {
                                                    jsonWriter.WritePropertyName(columns[i]);
                                                }

                                                jsonWriter.WriteValue(obj);
                                            } // Next i

                                            if (format.HasFlag(RenderType_t.DataTable))
                                            {
                                                jsonWriter.WriteEndObject();
                                            }
                                            else
                                            {
                                                jsonWriter.WriteEndArray();
                                            }
                                        } // Whend
                                    }     // End if (dr.HasRows)

                                    jsonWriter.WriteEndArray();

                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        jsonWriter.WriteEndObject();
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only))
                                } while (dr.NextResult());

                                jsonWriter.WriteEndArray();
                                jsonWriter.WriteEndObject();

                                jsonWriter.Flush();
                                output.Flush();
                            } // jsonWriter
                        }     // output
                    }         // dr
                }             // End Using cmd

                if (con.State != System.Data.ConnectionState.Closed)
                {
                    con.Close();
                }
            } // con
        }     // End Sub WriteArray
Exemplo n.º 7
0
        } // WriteAssociativeArray

        private static void WriteComplexArray(Newtonsoft.Json.JsonTextWriter jsonWriter, System.Data.Common.DbDataReader dr, RenderType_t renderType)
        {
            //jsonWriter.WriteStartObject();
            jsonWriter.WriteStartArray();

            for (int i = 0; i <= dr.FieldCount - 1; i++)
            {
                jsonWriter.WriteStartObject();

                jsonWriter.WritePropertyName("name");
                jsonWriter.WriteValue(dr.GetName(i));

                jsonWriter.WritePropertyName("index");
                jsonWriter.WriteValue(i);

                if (renderType.HasFlag(RenderType_t.WithDetail))
                {
                    jsonWriter.WritePropertyName("fieldType");
                    //jsonWriter.WriteValue(GetAssemblyQualifiedNoVersionName(dr.GetFieldType(i)));
                    jsonWriter.WriteValue(GetTypeName(dr.GetFieldType(i), renderType));
                }

                jsonWriter.WriteEndObject();
            }

            // jsonWriter.WriteEndObject();
            jsonWriter.WriteEndArray();
        } // WriteAssociativeArray
Exemplo n.º 8
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            System.Data.Common.DbConnection cnn
            , string sql
            , RenderType_t format
            , Microsoft.AspNetCore.Http.HttpContext context
            , System.Text.Encoding encoding
            , object parameters = null
            , System.Data.IDbTransaction transaction = null
            , int?commandTimeout = null
            , System.Data.CommandType?commandType = null
            )
        {
            // DynamicParameters dbArgs = new DynamicParameters();


            using (System.IO.StreamWriter responseWriter = new System.IO.StreamWriter(context.Response.Body, encoding))
            {
                using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                           new Newtonsoft.Json.JsonTextWriter(responseWriter))
                {
                    if (format.HasFlag(RenderType_t.Indented))
                    {
                        jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                    }


                    try
                    {
                        using (System.Data.Common.DbDataReader dr = await cnn.ExecuteDbReaderAsync(sql, parameters, transaction, commandTimeout, commandType))
                        {
                            context.Response.StatusCode  = (int)System.Net.HttpStatusCode.OK;
                            context.Response.ContentType = "application/json; charset=" + encoding.WebName;

                            await jsonWriter.WriteStartObjectAsync();

                            await jsonWriter.WritePropertyNameAsync("tables");

                            await jsonWriter.WriteStartArrayAsync();

                            do
                            {
                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();

                                    await jsonWriter.WritePropertyNameAsync("columns");

                                    if (format.HasFlag(RenderType_t.Columns_Associative))
                                    {
                                        await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                    }
                                    else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                    {
                                        await WriteComplexArray(jsonWriter, dr, format);
                                    }
                                    else // (format.HasFlag(RenderType_t.Array))
                                    {
                                        await WriteArray(jsonWriter, dr);
                                    }
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))


                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WritePropertyNameAsync("rows");
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                await jsonWriter.WriteStartArrayAsync();

                                string[] columns = null;
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    columns = new string[dr.FieldCount];
                                    for (int i = 0; i < dr.FieldCount; i++)
                                    {
                                        columns[i] = dr.GetName(i);
                                    } // Next i
                                }     // End if (format.HasFlag(RenderType_t.DataTable))

                                while (await dr.ReadAsync())
                                {
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteStartObjectAsync();
                                    }
                                    else
                                    {
                                        await jsonWriter.WriteStartArrayAsync();
                                    }

                                    for (int i = 0; i <= dr.FieldCount - 1; i++)
                                    {
                                        object obj = await dr.GetFieldValueAsync <object>(i);

                                        if (obj == System.DBNull.Value)
                                        {
                                            obj = null;
                                        }

                                        if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WritePropertyNameAsync(columns[i]);
                                        }

                                        await jsonWriter.WriteValueAsync(obj);
                                    } // Next i

                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteEndObjectAsync();
                                    }
                                    else
                                    {
                                        await jsonWriter.WriteEndArrayAsync();
                                    }
                                } // Whend

                                await jsonWriter.WriteEndArrayAsync();

                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                await jsonWriter.FlushAsync();

                                await responseWriter.FlushAsync();
                            } while (await dr.NextResultAsync());

                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.WriteEndObjectAsync();

                            await jsonWriter.FlushAsync();

                            await responseWriter.FlushAsync();
                        } // End Using dr
                    }
                    catch (System.Exception ex)
                    {
                        context.Response.StatusCode  = (int)System.Net.HttpStatusCode.InternalServerError;
                        context.Response.ContentType = "application/json; charset=" + encoding.WebName;

                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("Error");

                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("Message");

                        await jsonWriter.WriteValueAsync(ex.Message);

                        await jsonWriter.WritePropertyNameAsync("StackTrace");

                        await jsonWriter.WriteValueAsync(ex.StackTrace);

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.WriteEndObjectAsync();
                    }

                    await jsonWriter.FlushAsync();

                    await responseWriter.FlushAsync();

                    await context.Response.CompleteAsync();
                } // End Using jsonWriter
            }     // End Using responseWriter
        }         // End Sub AnyDataReaderToJson
Exemplo n.º 9
0
        } // End Sub WriteArray 


        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            string sql
            , System.Collections.Generic.Dictionary<string, object> pars
            , Microsoft.AspNetCore.Http.HttpContext context
            , RenderType_t format)
        {
            SqlService service = (SqlService) context.RequestServices.GetService(typeof(SqlService));

            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;
                    // cmd.CommandText = "SELECT  * FROM T_Benutzer; SELECT * FROM T_Benutzergruppen;";

                    service.AddParameterList(pars, cmd);
                    // cmd.ExecuteNonQueryAsync
                    // cmd.ExecuteReaderAsync
                    // cmd.ExecuteScalarAsync

                    using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync(
                        System.Data.CommandBehavior.SequentialAccess
                        | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (System.IO.StreamWriter output = new System.IO.StreamWriter(context.Response.Body))
                        {
                            using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                            {
                                if (format.HasFlag(RenderType_t.Indented))
                                    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;


                                context.Response.StatusCode = (int) System.Net.HttpStatusCode.OK;
                                context.Response.ContentType = "application/json";


                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("tables");
                                await jsonWriter.WriteStartArrayAsync();

                                do
                                {
                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteStartObjectAsync();
                                        await jsonWriter.WritePropertyNameAsync("columns");

                                        if (format.HasFlag(RenderType_t.Columns_Associative))
                                        {
                                            await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                        }
                                        else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                        {
                                            await WriteComplexArray(jsonWriter, dr, format);
                                        }
                                        else // (format.HasFlag(RenderType_t.Array))
                                        {
                                            await WriteArray(jsonWriter, dr);
                                        }
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only)) 


                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync("rows");
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only))
                                    
                                    await jsonWriter.WriteStartArrayAsync();
                                    
                                    string[] columns = null;
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        columns = new string[dr.FieldCount];
                                        for (int i = 0; i < dr.FieldCount; i++)
                                        {
                                            columns[i] = dr.GetName(i);
                                        } // Next i 
                                    } // End if (format.HasFlag(RenderType_t.DataTable)) 

                                    while (await dr.ReadAsync())
                                    {
                                        if (format.HasFlag(RenderType_t.DataTable))
                                            await jsonWriter.WriteStartObjectAsync();
                                        else
                                            await jsonWriter.WriteStartArrayAsync();

                                        for (int i = 0; i <= dr.FieldCount - 1; i++)
                                        {
                                            object obj = await dr.GetFieldValueAsync<object>(i);
                                            if (obj == System.DBNull.Value)
                                                obj = null;

                                            if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                            {
                                                await jsonWriter.WritePropertyNameAsync(columns[i]);
                                            }

                                            await jsonWriter.WriteValueAsync(obj);
                                        } // Next i 

                                        if (format.HasFlag(RenderType_t.DataTable))
                                            await jsonWriter.WriteEndObjectAsync();
                                        else
                                            await jsonWriter.WriteEndArrayAsync();
                                    } // Whend 

                                    await jsonWriter.WriteEndArrayAsync();

                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteEndObjectAsync();
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only)) 
                                } while (await dr.NextResultAsync());

                                await jsonWriter.WriteEndArrayAsync();
                                await jsonWriter.WriteEndObjectAsync();

                                await jsonWriter.FlushAsync();
                                await output.FlushAsync();
                            } // jsonWriter 
                        } // output 
                    } // dr 
                } // End Using cmd 

                if (con.State != System.Data.ConnectionState.Closed)
                    con.Close();
            } // con 
        } // End Sub WriteArray 
Exemplo n.º 10
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task <System.Exception> AsJSON(
            this IDbConnection cnn
            , System.IO.TextWriter output
            , string sql
            , RenderType_t format
            , object param = null
            , IDbTransaction transaction = null
            , int?commandTimeout         = null
            , CommandType?commandType    = null)
        {
            try
            {
                using (System.Data.Common.DbDataReader dr = cnn.ExecuteDbReader(sql, param, transaction, commandTimeout, commandType))
                {
                    using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                               new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                    {
                        if (format.HasFlag(RenderType_t.Indented))
                        {
                            jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                        }


                        // https://stackoverflow.com/questions/15305203/what-to-do-with-errors-when-streaming-the-body-of-an-http-request
                        // https://tools.ietf.org/html/rfc2616#section-14.40
                        // https://stackoverflow.com/questions/50299300/how-to-write-malformed-http-response-to-guarantee-something-akin-to-http-500
                        // https://stackoverflow.com/questions/50298999/write-http-trailer-headers-manually
                        // How do I write a useful trail header to the response that can be displayed well by the browser?
                        // You don’t. Mainstream Web browsers do not care about trailers.
                        // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                        // context.Response.ContentType = "application/json";


                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("tables");

                        await jsonWriter.WriteStartArrayAsync();

                        do
                        {
                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("columns");

                                if (format.HasFlag(RenderType_t.Columns_Associative))
                                {
                                    await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                }
                                else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                {
                                    await WriteComplexArray(jsonWriter, dr, format);
                                }
                                else // (format.HasFlag(RenderType_t.Array))
                                {
                                    await WriteArray(jsonWriter, dr);
                                }
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))


                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WritePropertyNameAsync("rows");
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))

                            await jsonWriter.WriteStartArrayAsync();

                            string[] columns = null;
                            if (format.HasFlag(RenderType_t.DataTable))
                            {
                                columns = new string[dr.FieldCount];
                                for (int i = 0; i < dr.FieldCount; i++)
                                {
                                    columns[i] = dr.GetName(i);
                                } // Next i
                            }     // End if (format.HasFlag(RenderType_t.DataTable))

                            while (await dr.ReadAsync())
                            {
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteStartArrayAsync();
                                }

                                for (int i = 0; i <= dr.FieldCount - 1; i++)
                                {
                                    object obj = await dr.GetFieldValueAsync <object>(i);

                                    if (obj == System.DBNull.Value)
                                    {
                                        obj = null;
                                    }

                                    if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync(columns[i]);
                                    }

                                    await jsonWriter.WriteValueAsync(obj);
                                } // Next i

                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteEndArrayAsync();
                                }
                            } // Whend

                            await jsonWriter.WriteEndArrayAsync();

                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteEndObjectAsync();
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))
                        } while (await dr.NextResultAsync());

                        await jsonWriter.WriteEndArrayAsync();

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.FlushAsync();

                        await output.FlushAsync();
                    } // jsonWriter
                }     // End Using dr
            }         // End Try
            catch (System.Exception ex)
            {
                return(ex);
            } // End Catch

            return(null);
        } // End Task AsJSON
Exemplo n.º 11
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            string sql
            , System.Collections.Generic.Dictionary <string, object> pars
            , SqlService service
            , WebSocketTextWriter output
            , RenderType_t format)
        {
            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;

                    service.AddParameterList(pars, cmd);
                    // cmd.ExecuteNonQueryAsync
                    // cmd.ExecuteReaderAsync
                    // cmd.ExecuteScalarAsync

                    using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync(
                               System.Data.CommandBehavior.SequentialAccess
                               | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                   new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                        {
                            if (format.HasFlag(RenderType_t.Indented))
                            {
                                jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                            }


                            // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                            // context.Response.ContentType = "application/json";


                            await jsonWriter.WriteStartObjectAsync();

                            await jsonWriter.WritePropertyNameAsync("tables");

                            await jsonWriter.WriteStartArrayAsync();

                            do
                            {
                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();

                                    await jsonWriter.WritePropertyNameAsync("columns");

                                    if (format.HasFlag(RenderType_t.Columns_Associative))
                                    {
                                        await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                    }
                                    else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                    {
                                        await WriteComplexArray(jsonWriter, dr, format);
                                    }
                                    else // (format.HasFlag(RenderType_t.Array))
                                    {
                                        await WriteArray(jsonWriter, dr);
                                    }
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))



                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WritePropertyNameAsync("rows");
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))


                                await output.TransmitAsync();

                                await jsonWriter.WriteStartArrayAsync();

                                if (dr.HasRows)
                                {
                                    string[] columns = null;
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        columns = new string[dr.FieldCount];
                                        for (int i = 0; i < dr.FieldCount; i++)
                                        {
                                            columns[i] = dr.GetName(i);
                                        } // Next i
                                    }     // End if (format.HasFlag(RenderType_t.DataTable))

                                    int rowCount = 0;

                                    while (await dr.ReadAsync())
                                    {
                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WriteStartObjectAsync();
                                        }
                                        else
                                        {
                                            await jsonWriter.WriteStartArrayAsync();
                                        }

                                        for (int i = 0; i <= dr.FieldCount - 1; i++)
                                        {
                                            object obj = await dr.GetFieldValueAsync <object>(i);

                                            if (obj == System.DBNull.Value)
                                            {
                                                obj = null;
                                            }

                                            if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                            {
                                                await jsonWriter.WritePropertyNameAsync(columns[i]);
                                            }

                                            await jsonWriter.WriteValueAsync(obj);
                                        } // Next i

                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WriteEndObjectAsync();
                                        }
                                        else
                                        {
                                            await jsonWriter.WriteEndArrayAsync();
                                        }

                                        rowCount++;

                                        await jsonWriter.FlushAsync();

                                        if (rowCount % 5 == 0)
                                        {
                                            await output.TransmitAsync();
                                        }
                                        else
                                        {
                                            await output.FlushAsync();
                                        }
                                    } // Whend

                                    await jsonWriter.FlushAsync();

                                    await output.TransmitAsync();
                                } // End if (dr.HasRows)

                                await jsonWriter.WriteEndArrayAsync();

                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))
                            } while (await dr.NextResultAsync());

                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.WriteEndObjectAsync();

                            try
                            {
                                await jsonWriter.FlushAsync();

                                await output.TransmitAsync();
                            }
                            catch (System.Exception ex)
                            {
                                System.Console.WriteLine(ex.Message);
                            }
                        } // jsonWriter
                    }     // dr
                }         // End Using cmd

                if (con.State != System.Data.ConnectionState.Closed)
                {
                    con.Close();
                }
            } // con
        }     // End Sub WriteArray
Exemplo n.º 12
0
        public async System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context)
        {
            // Do some request logic here.
            // await this._next.Invoke(context).ConfigureAwait(false);


            // Do some response logic here.
            // context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            // throw new Exception("YaY");
            string sql = null;

            System.Collections.Generic.Dictionary <string, object> pars = null;

            try
            {
                pars               = SqlServiceHelper.GetParameters(context);
                pars["BE_Hash"]    = 12435;
                pars["__stichtag"] = System.DateTime.Now.ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

                if (!pars.ContainsKey("sql"))
                {
                    throw new System.Exception("Parameter sql not provided....");
                }

                sql = System.Convert.ToString(pars["sql"]);
                sql = System.IO.Path.Combine("SQL", sql);
                sql = System.IO.File.ReadAllText(sql, System.Text.Encoding.UTF8);


                RenderType_t format = RenderType_t.Array;

                if (pars.ContainsKey("format"))
                {
                    string form       = System.Convert.ToString(pars["format"]);
                    int    renderType = 1;
                    int.TryParse(form, out renderType);

                    format = (RenderType_t)renderType;
                } // End if (pars.ContainsKey("format"))


                using (System.Data.Common.DbConnection cnn = this.m_service.Connection)
                {
                    System.Exception hasErrors = await cnn.AsJSON(context.Response.Body, sql, (Dapper.RenderType_t) format, pars);

                    // TOOD: Log if not NULL
                }


                // await SqlServiceJsonHelper.AnyDataReaderToJson(sql, pars, context, format);

                // throw new Exception("SQL error");
                // await context.Response.WriteAsync("Howdy");
                // context.Response.StatusCode = 500;
            } // End Try
            catch (System.Exception ex)
            {
                // header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
                // header("HTTP/1.0 500 Internal Server Error");
                // header('HTTP/1.1 200 OK');

                // context.Response.Headers["HTTP/1.0 500 Internal Server Error"] = "";
                context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                context.Response.Headers["X-Error-Message"] = "Incorrect username or password";

                context.Response.ContentType = "text/plain";

                SqlException se = new SqlException(ex.Message, sql, pars, context, ex);
                se.ToJSON(context.Response.Body);

                //await context.Response.WriteAsync(ex.Message);
                //await context.Response.WriteAsync(System.Environment.NewLine);
                //await context.Response.WriteAsync(System.Environment.NewLine);
                //await context.Response.WriteAsync(ex.StackTrace);
                //await context.Response.WriteAsync(System.Environment.NewLine);
                //await context.Response.WriteAsync(System.Environment.NewLine);
                //await context.Response.WriteAsync(sql);
                //await context.Response.WriteAsync(System.Environment.NewLine);
                //await context.Response.WriteAsync(System.Environment.NewLine);
                //await context.Response.WriteAsync(System.Convert.ToString(pars));
                System.Console.WriteLine();
            } // End Catch
        }     // End Async Invoke