Skip to content

Commit 5d9e81b

Browse files
author
Oren (electricessence)
committed
Updates.
1 parent 5ad3707 commit 5d9e81b

7 files changed

Lines changed: 60 additions & 47 deletions

Documentation.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ExpressiveCommandBase.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,11 @@ public void Execute(Action<TCommand> action)
382382
{
383383
using (var cmd = con.CreateCommand(Type, Command, Timeout))
384384
{
385-
var c = cmd as TCommand;
386-
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
387-
if (t != null) c.Transaction = t;
385+
if (!(cmd is TCommand c))
386+
throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
387+
if (t != null)
388+
c.Transaction = t;
389+
388390
AddParams(c);
389391
var state = con.EnsureOpen();
390392
try
@@ -415,9 +417,10 @@ public T Execute<T>(Func<TCommand, T> transform)
415417
{
416418
using (var cmd = con.CreateCommand(Type, Command, Timeout))
417419
{
418-
var c = cmd as TCommand;
419-
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
420-
if (t != null) c.Transaction = t;
420+
if (!(cmd is TCommand c))
421+
throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
422+
if (t != null)
423+
c.Transaction = t;
421424
AddParams(c);
422425
var state = con.EnsureOpen();
423426
try
@@ -442,9 +445,11 @@ public object ExecuteReturn()
442445
{
443446
using (var cmd = con.CreateCommand(Type, Command, Timeout))
444447
{
445-
var c = cmd as TCommand;
446-
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
447-
if (t != null) c.Transaction = t;
448+
if (!(cmd is TCommand c))
449+
throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
450+
if (t != null)
451+
c.Transaction = t;
452+
448453
AddParams(c);
449454
var returnParameter = c.AddReturnParameter();
450455
var state = con.EnsureOpen();
@@ -797,10 +802,8 @@ public ISourceBlock<T> AsSourceBlock<T>(IEnumerable<(string Field, string Column
797802

798803
void i() => ExecuteReader(reader =>
799804
{
800-
// Ignores fields that don't match.
801-
var columns = reader.GetMatchingOrdinals(cn)
802-
.OrderBy(c => c.Ordinal)
803-
.ToArray();
805+
// Ignores fields that don't match.
806+
var columns = reader.GetMatchingOrdinals(cn, true);
804807

805808
var ordinalValues = columns.Select(c => c.Ordinal).ToArray();
806809
deferred(new QueryResult<IEnumerable<object[]>>(

ExpressiveDbCommandBase.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ public Task ExecuteAsync(Func<TCommand, Task> handler)
9999
{
100100
using (var cmd = (TCommand)con.CreateCommand(Type, Command, Timeout))
101101
{
102-
var c = cmd as TCommand;
103-
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
104-
AddParams(c);
102+
if (!(cmd is TCommand c))
103+
throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
104+
105+
AddParams(c);
105106
var state = await con.EnsureOpenAsync(CancellationToken);
106107
try
107108
{
@@ -132,9 +133,10 @@ public Task<T> ExecuteAsync<T>(Func<TCommand, Task<T>> transform)
132133
{
133134
using (var cmd = (TCommand)con.CreateCommand(Type, Command, Timeout))
134135
{
135-
var c = cmd as TCommand;
136-
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
137-
AddParams(c);
136+
if (!(cmd is TCommand c))
137+
throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
138+
139+
AddParams(c);
138140
var state = await con.EnsureOpenAsync(CancellationToken);
139141
try
140142
{
@@ -161,9 +163,10 @@ public Task<object> ExecuteReturnAsync()
161163
{
162164
using (var cmd = con.CreateCommand(Type, Command, Timeout))
163165
{
164-
var c = cmd as TCommand;
165-
if (c == null) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
166-
AddParams(c);
166+
if (!(cmd is TCommand c))
167+
throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)}).");
168+
169+
AddParams(c);
167170
var returnParameter = c.AddReturnParameter();
168171
var state = await con.EnsureOpenAsync(CancellationToken, false).ConfigureAwait(false);
169172
try

Extensions.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ public static string[] GetNames(this IDataRecord record)
296296
/// Returns the (name,ordinal) mapping for current result set.
297297
/// </summary>
298298
/// <param name="record">The reader to get column names from.</param>
299-
/// <returns>The array of mappings.</returns>
300-
public static (string Name, int Ordinal)[] GetOrdinalMapping(this IDataRecord record)
301-
=> record.GetNames().Select((n, o) => (Name: n, Ordinal: o)).ToArray();
299+
/// <returns>An enumerable of the mappings.</returns>
300+
public static IEnumerable<(string Name, int Ordinal)> GetOrdinalMapping(this IDataRecord record)
301+
=> record.GetNames().Select((n, o) => (Name: n, Ordinal: o));
302302

303303
/// <summary>
304304
/// Returns an array of name to ordinal mappings.
@@ -323,15 +323,14 @@ public static (string Name, int Ordinal)[] GetOrdinalMapping(this IDataRecord re
323323
q = q.OrderBy(m => m.Ordinal);
324324

325325
return q.ToArray();
326-
327326
}
328327
catch (IndexOutOfRangeException iorex)
329328
{
330329
var mismatch = new HashSet<string>(cn);
331330
mismatch.ExceptWith(record.GetNames());
332331

333332
// Columns not mapped correctly. Report all columns that are mismatched/missing.
334-
throw new IndexOutOfRangeException($"Invalid columns: {String.Join(", ", mismatch.OrderBy(c => c).ToArray())}", iorex);
333+
throw new IndexOutOfRangeException($"Invalid columns: {string.Join(", ", mismatch.OrderBy(c => c).ToArray())}", iorex);
335334
}
336335
}
337336

@@ -349,6 +348,7 @@ public static object[] GetValuesFromOrdinals(this IDataRecord record, params int
349348
var values = new object[count];
350349
for (var i = 0; i < count; i++)
351350
values[i] = record.GetValue(ordinals[i]);
351+
352352
return values;
353353
}
354354

@@ -378,18 +378,24 @@ public static (string Name, int Ordinal)[] GetMatchingOrdinals(this IDataRecord
378378
var requested = new HashSet<string>(columnNames);
379379
// Return actual values based upon if their lower-case counterparts exist in the requested.
380380
return actual
381-
.Where(m => columnNames.Contains(m.Name.ToLowerInvariant()))
382-
.ToArray();
381+
.Where(m => requested.Contains(m.Name.ToLowerInvariant()))
382+
.ToArray();
383383
}
384384
else
385385
{
386386
// Create a map of lower-case keys to acutal.
387387
var actualColumns = actual.ToDictionary(m => m.Name.ToLowerInvariant(), m => m);
388-
return columnNames
389-
.Where(c => actualColumns.ContainsKey(c)) // Select lower case column names if they exist in the dictionary.
390-
.Select(c => actualColumns[c]) // Then select the actual values based upon that key.
391-
.ToArray();
388+
return columnNames
389+
.Where(c => actualColumns.ContainsKey(c)) // Select lower case column names if they exist in the dictionary.
390+
.Select(c => actualColumns[c]) // Then select the actual values based upon that key.
391+
.ToArray();
392392
}
393+
394+
/* Note:
395+
* It is not necessary to call .ToArray() because the names are already pulled from the reader,
396+
* and this method could return IEnumerable,
397+
* but it's more common use is to have an array so best to keep it an array.
398+
*/
393399
}
394400

395401
/// <summary>
@@ -1459,7 +1465,7 @@ public static IEnumerable<T0> FirstOrdinalResults<T0>(this DbDataReader reader)
14591465
{
14601466
results.Enqueue(
14611467
reader.IsDBNull(0)
1462-
? default(T0)
1468+
? default
14631469
: reader.GetFieldValue<T0>(0)
14641470
);
14651471
}
@@ -1534,7 +1540,7 @@ public static async Task<IEnumerable<T0>> FirstOrdinalResultsAsync<T0>(this DbDa
15341540
{
15351541
results.Enqueue(
15361542
await reader.IsDBNullAsync(0, t).ConfigureAwait(false)
1537-
? default(T0)
1543+
? default
15381544
: await reader.GetFieldValueAsync<T0>(0, t).ConfigureAwait(false)
15391545
);
15401546
}
@@ -1545,7 +1551,7 @@ await reader.IsDBNullAsync(0, t).ConfigureAwait(false)
15451551
{
15461552
results.Enqueue(
15471553
reader.IsDBNull(0)
1548-
? default(T0)
1554+
? default
15491555
: reader.GetFieldValue<T0>(0)
15501556
);
15511557
}

Open.Database.Extensions.csproj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
<Description>Useful set of utilities and abstractions for simplifying modern data-access operations and ensuring DI compatibility.</Description>
1212
<RepositoryUrl>https://github.com/electricessence/Open.Database.Extensions</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
14-
<Version>5.12.2</Version>
15-
<AssemblyVersion>5.12.2.0</AssemblyVersion>
16-
<FileVersion>5.12.2.0</FileVersion>
17-
<PackageReleaseNotes></PackageReleaseNotes>
14+
<Version>5.12.3</Version>
15+
<AssemblyVersion>5.12.3.0</AssemblyVersion>
16+
<FileVersion>5.12.3.0</FileVersion>
17+
<PackageReleaseNotes>Minor tweaks and polish.</PackageReleaseNotes>
18+
<LangVersion>latest</LangVersion>
1819
</PropertyGroup>
1920

2021
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@@ -46,9 +47,9 @@
4647
</ItemGroup>
4748

4849
<ItemGroup>
49-
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
50-
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.8.0" />
51-
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.4.0" />
50+
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
51+
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.9.0" />
52+
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
5253
</ItemGroup>
5354

5455
</Project>

Transformer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Transformer<T>
1616
// Allow mapping key = object property, value = column name.
1717
readonly Dictionary<string, string> PropertyMap;
1818
readonly Dictionary<string, PropertyInfo> ColumnToPropertyMap;
19-
public HashSet<string> PropertyNames => new HashSet<string>(PropertyMap.Keys);
20-
public HashSet<string> ColumnNames => new HashSet<string>(PropertyMap.Values);
19+
public IEnumerable<string> PropertyNames => PropertyMap.Keys;
20+
public IEnumerable<string> ColumnNames => PropertyMap.Values;
2121

2222
public Transformer(IEnumerable<(string Field, string Column)> overrides = null)
2323
{

docs/Documentation.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)