Beispiel #1
0
        public async Task <(DataTable Data, string Query)> GetDataAsync(string path, IRuntimeFormatter formatter)
        {
            Debug.Assert(!(formatter is null));

            var connectionString = ConnectionString.FormatWith(formatter);
            var query            = GetQuery(path, formatter);

            var scope = Logger.BeginScope().AttachElapsed();

            try
            {
                Logger.Log(Abstraction.Layer.Database().Composite(new { properties = new { connectionString, query } }));

                using (var conn = new SqlConnection(connectionString))
                {
                    conn.Open();

                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = query;
                        cmd.CommandType = CommandType.Text;

                        using (var dataReader = await cmd.ExecuteReaderAsync())
                        {
                            var dataTable = new DataTable();
                            dataTable.Load(dataReader);

                            EvaluateAttachments(dataTable);

                            Logger.Log(Abstraction.Layer.Database().Meta(new { DataTable = new { RowCount = dataTable.Rows.Count, ColumnCount = dataTable.Columns.Count } }));
                            Logger.Log(Abstraction.Layer.Database().Routine(nameof(GetDataAsync)).Completed());

                            return(dataTable, query);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw DynamicException.Create("DataSource", $"Unable to get data for {Id}.", ex);
            }
            finally
            {
                scope.Dispose();
            }
        }
Beispiel #2
0
        private string GetQuery(string path, IRuntimeFormatter formatter)
        {
            var query = Query.FormatWith(formatter);

            if (Uri.TryCreate(query, UriKind.Absolute, out var uri))
            {
                var isAbsolutePath =
                    uri.AbsolutePath.StartsWith("/") == false &&
                    Path.IsPathRooted(uri.AbsolutePath);

                query =
                    isAbsolutePath
                        ? File.ReadAllText(uri.AbsolutePath)
                        : File.ReadAllText(Path.Combine(path, uri.AbsolutePath.TrimStart('/')));

                return(query.FormatWith(formatter));
            }
            else
            {
                return(query);
            }
        }
Beispiel #3
0
 public static string FormatWith(this string input, IRuntimeFormatter formatter)
 {
     return(formatter.Format(input));
 }