|
| 1 | +using System.Data.Common; |
| 2 | +using System.Globalization; |
| 3 | +using System.Text.Json; |
| 4 | +using NeoReports.Abstractions; |
| 5 | + |
| 6 | +namespace NeoReports.Sources.Sql; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Config-driven SQL source for the dynamic path (<c>type: "sql"</c>). Reads its settings from the |
| 10 | +/// source <c>properties</c> (<c>connectionString</c>, <c>sql</c>, <c>key</c>, optional |
| 11 | +/// <c>pageSize</c>) and produces an <see cref="IBatchSource{T}"/> of positional |
| 12 | +/// <see cref="ReportRecord"/>s. Each row is materialized by reading the result-set column whose name |
| 13 | +/// matches each schema column (case-insensitive), reusing the v1 keyset paging engine. |
| 14 | +/// </summary> |
| 15 | +public sealed class SqlConfigSourceProvider : IConfigSourceProvider |
| 16 | +{ |
| 17 | + /// <inheritdoc /> |
| 18 | + public string Type => "sql"; |
| 19 | + |
| 20 | + /// <inheritdoc /> |
| 21 | + public IBatchSource<ReportRecord> Create(SourceConfig source, ReportSchema schema, IServiceProvider services) |
| 22 | + { |
| 23 | + ArgumentNullException.ThrowIfNull(source); |
| 24 | + ArgumentNullException.ThrowIfNull(schema); |
| 25 | + |
| 26 | + IReadOnlyDictionary<string, object?>? properties = source.Properties; |
| 27 | + string connectionString = RequireString(properties, "connectionString"); |
| 28 | + string sql = RequireString(properties, "sql"); |
| 29 | + string key = RequireString(properties, "key"); |
| 30 | + int pageSize = OptionalInt(properties, "pageSize") ?? 1000; |
| 31 | + |
| 32 | + return new SqlKeysetSource<ReportRecord>( |
| 33 | + connectionString, sql, key, pageSize, schema, |
| 34 | + parameters: null, |
| 35 | + materialize: (reader, ordinals) => Materialize(reader, ordinals, schema)); |
| 36 | + } |
| 37 | + |
| 38 | + private static ReportRecord Materialize( |
| 39 | + DbDataReader reader, IReadOnlyDictionary<string, int> ordinalByName, ReportSchema schema) |
| 40 | + { |
| 41 | + var values = new object?[schema.Count]; |
| 42 | + for (var i = 0; i < schema.Count; i++) |
| 43 | + { |
| 44 | + values[i] = ordinalByName.TryGetValue(schema.Columns[i].Name, out int ordinal) && !reader.IsDBNull(ordinal) |
| 45 | + ? reader.GetValue(ordinal) |
| 46 | + : null; |
| 47 | + } |
| 48 | + |
| 49 | + return new ReportRecord(schema, values); |
| 50 | + } |
| 51 | + |
| 52 | + private static string RequireString(IReadOnlyDictionary<string, object?>? properties, string key) |
| 53 | + { |
| 54 | + if (properties is not null |
| 55 | + && properties.TryGetValue(key, out var value) |
| 56 | + && value is string text |
| 57 | + && !string.IsNullOrWhiteSpace(text)) |
| 58 | + { |
| 59 | + return text; |
| 60 | + } |
| 61 | + |
| 62 | + throw new ConfigurationException($"The SQL source requires a non-empty '{key}' property."); |
| 63 | + } |
| 64 | + |
| 65 | + private static int? OptionalInt(IReadOnlyDictionary<string, object?>? properties, string key) |
| 66 | + { |
| 67 | + if (properties is null || !properties.TryGetValue(key, out var value) || value is null) |
| 68 | + return null; |
| 69 | + |
| 70 | + return value switch |
| 71 | + { |
| 72 | + int i => i, |
| 73 | + long l => checked((int)l), |
| 74 | + double d => (int)d, |
| 75 | + string s when int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out int parsed) => parsed, |
| 76 | + JsonElement { ValueKind: JsonValueKind.Number } e => e.GetInt32(), |
| 77 | + _ => throw new ConfigurationException( |
| 78 | + $"The SQL source property '{key}' must be an integer (was {value.GetType().Name})."), |
| 79 | + }; |
| 80 | + } |
| 81 | +} |
0 commit comments