Files
DynamORM/DynamORM.Tests/Helpers/FakeDbCommand.cs

136 lines
4.2 KiB
C#

/*
* DynamORM - Dynamic Object-Relational Mapping library.
* Copyright (c) 2012-2026, Grzegorz Russek (grzegorz.russek@gmail.com)
* All rights reserved.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
namespace DynamORM.Tests.Helpers
{
internal sealed class FakeDbCommand : IDbCommand
{
private readonly FakeParameterCollection _parameters = new FakeParameterCollection();
public string CommandText { get; set; }
public int CommandTimeout { get; set; }
public CommandType CommandType { get; set; }
public IDbConnection Connection { get; set; }
public IDataParameterCollection Parameters { get { return _parameters; } }
public IDbTransaction Transaction { get; set; }
public UpdateRowSource UpdatedRowSource { get; set; }
public void Cancel() { }
public IDbDataParameter CreateParameter() { return new FakeDbParameter(); }
public void Dispose() { }
public int ExecuteNonQuery() { throw new NotSupportedException(); }
public IDataReader ExecuteReader() { throw new NotSupportedException(); }
public IDataReader ExecuteReader(CommandBehavior behavior) { throw new NotSupportedException(); }
public object ExecuteScalar() { throw new NotSupportedException(); }
public void Prepare() { }
}
internal sealed class FakeDbParameter : IDbDataParameter
{
public byte Precision { get; set; }
public byte Scale { get; set; }
public int Size { get; set; }
public DbType DbType { get; set; }
public ParameterDirection Direction { get; set; }
public bool IsNullable { get { return true; } }
public string ParameterName { get; set; }
public string SourceColumn { get; set; }
public DataRowVersion SourceVersion { get; set; }
public object Value { get; set; }
}
internal sealed class FakeParameterCollection : IDataParameterCollection
{
private readonly List<object> _items = new List<object>();
public object this[string parameterName]
{
get { return _items.Find(x => string.Equals(((IDbDataParameter)x).ParameterName, parameterName, StringComparison.Ordinal)); }
set { throw new NotSupportedException(); }
}
public object this[int index]
{
get { return _items[index]; }
set { _items[index] = value; }
}
public bool IsFixedSize { get { return false; } }
public bool IsReadOnly { get { return false; } }
public int Count { get { return _items.Count; } }
public bool IsSynchronized { get { return false; } }
public object SyncRoot { get { return this; } }
public int Add(object value)
{
_items.Add(value);
return _items.Count - 1;
}
public void Clear()
{
_items.Clear();
}
public bool Contains(string parameterName)
{
return _items.Exists(x => string.Equals(((IDbDataParameter)x).ParameterName, parameterName, StringComparison.Ordinal));
}
public bool Contains(object value)
{
return _items.Contains(value);
}
public void CopyTo(Array array, int index)
{
_items.ToArray().CopyTo(array, index);
}
public IEnumerator GetEnumerator()
{
return _items.GetEnumerator();
}
public int IndexOf(string parameterName)
{
return _items.FindIndex(x => string.Equals(((IDbDataParameter)x).ParameterName, parameterName, StringComparison.Ordinal));
}
public int IndexOf(object value)
{
return _items.IndexOf(value);
}
public void Insert(int index, object value)
{
_items.Insert(index, value);
}
public void Remove(object value)
{
_items.Remove(value);
}
public void RemoveAt(string parameterName)
{
int index = IndexOf(parameterName);
if (index >= 0)
_items.RemoveAt(index);
}
public void RemoveAt(int index)
{
_items.RemoveAt(index);
}
}
}