Add typed procedure contract result handling
This commit is contained in:
135
DynamORM.Tests/Helpers/FakeMultiResultDataReader.cs
Normal file
135
DynamORM.Tests/Helpers/FakeMultiResultDataReader.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* DynamORM - Dynamic Object-Relational Mapping library.
|
||||
* Copyright (c) 2012-2026, Grzegorz Russek (grzegorz.russek@gmail.com)
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
|
||||
namespace DynamORM.Tests.Helpers
|
||||
{
|
||||
internal sealed class FakeMultiResultDataReader : IDataReader
|
||||
{
|
||||
private sealed class ResultSet
|
||||
{
|
||||
public string[] Names;
|
||||
public Type[] Types;
|
||||
public object[][] Rows;
|
||||
}
|
||||
|
||||
private readonly List<ResultSet> _sets = new List<ResultSet>();
|
||||
private int _setIndex;
|
||||
private int _rowIndex = -1;
|
||||
|
||||
public FakeMultiResultDataReader(params Tuple<string[], Type[], object[][]>[] sets)
|
||||
{
|
||||
foreach (var set in sets)
|
||||
_sets.Add(new ResultSet
|
||||
{
|
||||
Names = set.Item1,
|
||||
Types = set.Item2,
|
||||
Rows = set.Item3
|
||||
});
|
||||
}
|
||||
|
||||
private ResultSet Current { get { return _sets[_setIndex]; } }
|
||||
|
||||
public object this[string name] { get { return GetValue(GetOrdinal(name)); } }
|
||||
public object this[int i] { get { return GetValue(i); } }
|
||||
public int Depth { get { return 0; } }
|
||||
public bool IsClosed { get; private set; }
|
||||
public int RecordsAffected { get { return 0; } }
|
||||
public int FieldCount { get { return Current.Names.Length; } }
|
||||
|
||||
public void Close() { IsClosed = true; }
|
||||
public void Dispose() { Close(); }
|
||||
public bool GetBoolean(int i) { return (bool)GetValue(i); }
|
||||
public byte GetByte(int i) { return (byte)GetValue(i); }
|
||||
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) { throw new NotSupportedException(); }
|
||||
public char GetChar(int i) { return (char)GetValue(i); }
|
||||
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) { throw new NotSupportedException(); }
|
||||
public IDataReader GetData(int i) { throw new NotSupportedException(); }
|
||||
public string GetDataTypeName(int i) { return GetFieldType(i).Name; }
|
||||
public DateTime GetDateTime(int i) { return (DateTime)GetValue(i); }
|
||||
public decimal GetDecimal(int i) { return (decimal)GetValue(i); }
|
||||
public double GetDouble(int i) { return (double)GetValue(i); }
|
||||
public Type GetFieldType(int i) { return Current.Types[i]; }
|
||||
public float GetFloat(int i) { return (float)GetValue(i); }
|
||||
public Guid GetGuid(int i) { return (Guid)GetValue(i); }
|
||||
public short GetInt16(int i) { return Convert.ToInt16(GetValue(i)); }
|
||||
public int GetInt32(int i) { return Convert.ToInt32(GetValue(i)); }
|
||||
public long GetInt64(int i) { return Convert.ToInt64(GetValue(i)); }
|
||||
public string GetName(int i) { return Current.Names[i]; }
|
||||
public int GetOrdinal(string name)
|
||||
{
|
||||
for (int i = 0; i < Current.Names.Length; i++)
|
||||
if (string.Equals(Current.Names[i], name, StringComparison.OrdinalIgnoreCase))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
public DataTable GetSchemaTable()
|
||||
{
|
||||
DataTable schema = new DataTable();
|
||||
schema.Columns.Add("ColumnName", typeof(string));
|
||||
schema.Columns.Add("ColumnOrdinal", typeof(int));
|
||||
schema.Columns.Add("ColumnSize", typeof(int));
|
||||
schema.Columns.Add("NumericPrecision", typeof(short));
|
||||
schema.Columns.Add("NumericScale", typeof(short));
|
||||
schema.Columns.Add("DataType", typeof(Type));
|
||||
schema.Columns.Add("ProviderType", typeof(int));
|
||||
schema.Columns.Add("NativeType", typeof(int));
|
||||
schema.Columns.Add("AllowDBNull", typeof(bool));
|
||||
schema.Columns.Add("IsUnique", typeof(bool));
|
||||
schema.Columns.Add("IsKey", typeof(bool));
|
||||
schema.Columns.Add("IsAutoIncrement", typeof(bool));
|
||||
|
||||
for (int i = 0; i < Current.Names.Length; i++)
|
||||
{
|
||||
DataRow row = schema.NewRow();
|
||||
row[0] = Current.Names[i];
|
||||
row[1] = i;
|
||||
row[2] = 0;
|
||||
row[3] = 0;
|
||||
row[4] = 0;
|
||||
row[5] = Current.Types[i];
|
||||
row[6] = 0;
|
||||
row[7] = 0;
|
||||
row[8] = true;
|
||||
row[9] = false;
|
||||
row[10] = false;
|
||||
row[11] = false;
|
||||
schema.Rows.Add(row);
|
||||
}
|
||||
|
||||
return schema;
|
||||
}
|
||||
public string GetString(int i) { return (string)GetValue(i); }
|
||||
public object GetValue(int i) { return Current.Rows[_rowIndex][i]; }
|
||||
public int GetValues(object[] values)
|
||||
{
|
||||
int count = Math.Min(values.Length, FieldCount);
|
||||
for (int i = 0; i < count; i++)
|
||||
values[i] = GetValue(i);
|
||||
return count;
|
||||
}
|
||||
public bool IsDBNull(int i) { return GetValue(i) == null || GetValue(i) == DBNull.Value; }
|
||||
public bool NextResult()
|
||||
{
|
||||
if (_setIndex + 1 >= _sets.Count)
|
||||
return false;
|
||||
|
||||
_setIndex++;
|
||||
_rowIndex = -1;
|
||||
return true;
|
||||
}
|
||||
public bool Read()
|
||||
{
|
||||
if (_rowIndex + 1 >= Current.Rows.Length)
|
||||
return false;
|
||||
_rowIndex++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ using System.Data;
|
||||
|
||||
namespace DynamORM.Tests.Helpers
|
||||
{
|
||||
public class ProcedureParameterObject
|
||||
public class ProcedureParameterObject : IProcedureParameters<ProcedureParameterResult>
|
||||
{
|
||||
[ProcedureParameter("code", Order = 2, DbType = DbType.String, Size = 32)]
|
||||
public string Code { get; set; }
|
||||
@@ -23,9 +23,52 @@ namespace DynamORM.Tests.Helpers
|
||||
public int Status { get; set; }
|
||||
}
|
||||
|
||||
public class ProcedureParameterColumnFallbackObject
|
||||
public class ProcedureParameterColumnFallbackObject : IProcedureParameters
|
||||
{
|
||||
[DynamORM.Mapper.Column("code", false, DbType.String, 64)]
|
||||
public string Code { get; set; }
|
||||
}
|
||||
|
||||
public class ProcedureParameterResult
|
||||
{
|
||||
[DynamORM.Mapper.Column("sp_Test")]
|
||||
public int MainResult { get; set; }
|
||||
|
||||
[DynamORM.Mapper.Column("result")]
|
||||
public int Result { get; set; }
|
||||
|
||||
[DynamORM.Mapper.Column("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[DynamORM.Mapper.Column("status")]
|
||||
public int Status { get; set; }
|
||||
}
|
||||
|
||||
public class ProcedureMultiResult : IProcedureResultReader
|
||||
{
|
||||
[DynamORM.Mapper.Column("sp_Multi")]
|
||||
public int MainResult { get; set; }
|
||||
|
||||
[DynamORM.Mapper.Column("status")]
|
||||
public int Status { get; set; }
|
||||
|
||||
public System.Collections.Generic.List<string> Codes { get; private set; } = new System.Collections.Generic.List<string>();
|
||||
public System.Collections.Generic.List<int> States { get; private set; } = new System.Collections.Generic.List<int>();
|
||||
|
||||
public void ReadResults(IDataReader reader)
|
||||
{
|
||||
while (reader.Read())
|
||||
Codes.Add(reader.GetString(0));
|
||||
|
||||
if (reader.NextResult())
|
||||
while (reader.Read())
|
||||
States.Add(reader.GetInt32(0));
|
||||
}
|
||||
}
|
||||
|
||||
public class ProcedureMultiResultArgs : IProcedureParameters<ProcedureMultiResult>
|
||||
{
|
||||
[ProcedureParameter("status", Direction = ParameterDirection.Output, Order = 1, DbType = DbType.Int32)]
|
||||
public int Status { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,5 +113,87 @@ namespace DynamORM.Tests.Procedure
|
||||
Assert.AreEqual("XYZ", p0.Value);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDeclaredResultTypeComesFromContractInterface()
|
||||
{
|
||||
Assert.AreEqual(typeof(ProcedureParameterResult), DynamicProcedureResultBinder.GetDeclaredResultType(new ProcedureParameterObject()));
|
||||
Assert.AreEqual(typeof(ProcedureMultiResult), DynamicProcedureResultBinder.GetDeclaredResultType(new ProcedureMultiResultArgs()));
|
||||
Assert.IsNull(DynamicProcedureResultBinder.GetDeclaredResultType(new object()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDeclaredResultPayloadBindingMapsMainAndOutValues()
|
||||
{
|
||||
var result = DynamicProcedureResultBinder.BindPayload(
|
||||
typeof(ProcedureParameterResult),
|
||||
"sp_Test",
|
||||
15,
|
||||
new System.Collections.Generic.Dictionary<string, object>
|
||||
{
|
||||
{ "result", 7 },
|
||||
{ "description", "done" },
|
||||
{ "status", 3 }
|
||||
}) as ProcedureParameterResult;
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.AreEqual(15, result.MainResult);
|
||||
Assert.AreEqual(7, result.Result);
|
||||
Assert.AreEqual("done", result.Description);
|
||||
Assert.AreEqual(3, result.Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDeclaredResultReaderCanConsumeMultipleResultSets()
|
||||
{
|
||||
using (var reader = new FakeMultiResultDataReader(
|
||||
Tuple.Create(
|
||||
new[] { "Code" },
|
||||
new[] { typeof(string) },
|
||||
new[]
|
||||
{
|
||||
new object[] { "A" },
|
||||
new object[] { "B" }
|
||||
}),
|
||||
Tuple.Create(
|
||||
new[] { "State" },
|
||||
new[] { typeof(int) },
|
||||
new[]
|
||||
{
|
||||
new object[] { 10 },
|
||||
new object[] { 20 }
|
||||
})))
|
||||
{
|
||||
var result = DynamicProcedureResultBinder.ReadDeclaredResult(typeof(ProcedureMultiResult), reader) as ProcedureMultiResult;
|
||||
|
||||
Assert.NotNull(result);
|
||||
CollectionAssert.AreEqual(new[] { "A", "B" }, result.Codes);
|
||||
CollectionAssert.AreEqual(new[] { 10, 20 }, result.States);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDeclaredResultPayloadCanAugmentReaderResult()
|
||||
{
|
||||
var existing = new ProcedureMultiResult();
|
||||
existing.Codes.Add("A");
|
||||
existing.States.Add(10);
|
||||
|
||||
var result = DynamicProcedureResultBinder.BindPayload(
|
||||
typeof(ProcedureMultiResult),
|
||||
"sp_Multi",
|
||||
99,
|
||||
new System.Collections.Generic.Dictionary<string, object>
|
||||
{
|
||||
{ "status", 5 }
|
||||
},
|
||||
existing) as ProcedureMultiResult;
|
||||
|
||||
Assert.AreSame(existing, result);
|
||||
Assert.AreEqual(99, result.MainResult);
|
||||
Assert.AreEqual(5, result.Status);
|
||||
CollectionAssert.AreEqual(new[] { "A" }, result.Codes);
|
||||
CollectionAssert.AreEqual(new[] { 10 }, result.States);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user