Fix [https://git.tompit.com/Connected/Connected.Host/issues/1] #1

Merged
pit merged 1 commits from features/tableStorageClient into develop 2 years ago

@ -7,9 +7,9 @@ root = true
#### Core EditorConfig Options #### #### Core EditorConfig Options ####
# Indentation and spacing # Indentation and spacing
indent_size = 4 indent_size = 3
indent_style = tab indent_style = tab
tab_width = 4 tab_width = 3
# New line preferences # New line preferences
end_of_line = crlf end_of_line = crlf

@ -1,61 +1,125 @@
using System.Data; using System.Collections;
using System.Data.Common;
namespace Connected.ServiceModel.Client.Data; namespace Connected.ServiceModel.Client.Data;
internal sealed class DataParameterCollection : List<IDbDataParameter>, IDataParameterCollection internal sealed class DataParameterCollection : DbParameterCollection
{ {
public object this[string parameterName] private readonly object _syncRoot = new();
public DataParameterCollection()
{ {
get Items = new();
}
private List<DbParameter> Items { get; }
public override int Count => Items.Count;
public override object SyncRoot => _syncRoot;
public override int Add(object value)
{ {
foreach (var parameter in this) if (value is not DbParameter parameter)
throw new InvalidCastException(nameof(DbParameter));
Items.Add(parameter);
return Items.Count - 1;
}
public override void AddRange(Array values)
{ {
if (string.Equals(parameter.ParameterName, parameterName, StringComparison.OrdinalIgnoreCase)) foreach (var item in values)
return parameter; Add(item);
} }
throw new NullReferenceException(nameof(IDbDataParameter)); public override void Clear()
{
Items.Clear();
} }
set
public override bool Contains(object value)
{ {
if (value is not IDbDataParameter parameter) return Items.Contains(value);
throw new InvalidCastException(nameof(IDbDataParameter)); }
var idx = IndexOf(parameterName); public override bool Contains(string value)
{
return Items.FirstOrDefault(f => string.Equals(f.ParameterName, value, StringComparison.OrdinalIgnoreCase)) is not null;
}
if (idx < 0) public override void CopyTo(Array array, int index)
throw new NullReferenceException(nameof(IDbDataParameter)); {
foreach (var item in array)
{
Insert(index, item);
index++;
}
}
this[idx] = parameter; public override IEnumerator GetEnumerator()
{
return Items.GetEnumerator();
} }
public override int IndexOf(object value)
{
if (value is not DbParameter parameter)
throw new InvalidCastException(nameof(DbParameter));
return Items.IndexOf(parameter);
} }
public bool Contains(string parameterName) public override int IndexOf(string parameterName)
{ {
foreach (var parameter in this) if (Items.FirstOrDefault(f => string.Equals(f.ParameterName, parameterName, StringComparison.OrdinalIgnoreCase)) is DbParameter parameter)
return Items.IndexOf(parameter);
return -1;
}
public override void Insert(int index, object value)
{
if (value is not DbParameter parameter)
throw new InvalidCastException(nameof(DbParameter));
Items.Insert(index, parameter);
}
public override void Remove(object value)
{ {
if (string.Equals(parameter.ParameterName, parameterName, StringComparison.OrdinalIgnoreCase)) if (value is not DbParameter parameter)
return true; throw new InvalidCastException(nameof(DbParameter));
Items.Remove(parameter);
} }
return false; public override void RemoveAt(int index)
{
Items.RemoveAt(index);
} }
public int IndexOf(string parameterName) public override void RemoveAt(string parameterName)
{ {
for (var i = 0; i < Count; i++) if (Items.FirstOrDefault(f => string.Equals(f.ParameterName, parameterName, StringComparison.OrdinalIgnoreCase)) is DbParameter parameter)
Items.Remove(parameter);
}
protected override DbParameter GetParameter(int index)
{ {
var current = this[i]; return Items[index];
}
if (string.Equals(current.ParameterName, parameterName, StringComparison.OrdinalIgnoreCase)) protected override DbParameter GetParameter(string parameterName)
return i; {
return Items.First(f => string.Equals(f.ParameterName, parameterName, StringComparison.OrdinalIgnoreCase));
} }
return -1; protected override void SetParameter(int index, DbParameter value)
{
Items[index] = value;
} }
public void RemoveAt(string parameterName) protected override void SetParameter(string parameterName, DbParameter value)
{ {
if (this[parameterName] is IDbDataParameter target) Items[IndexOf(parameterName)] = value;
Remove(target);
} }
} }

@ -1,4 +1,5 @@
using System.Data; using System.Data;
using System.Diagnostics.CodeAnalysis;
using Connected.ServiceModel.Client.Data.Remote; using Connected.ServiceModel.Client.Data.Remote;
namespace Connected.ServiceModel.Client.Data; namespace Connected.ServiceModel.Client.Data;
@ -18,6 +19,7 @@ internal sealed class TableConnection : IDbConnection
/// <summary> /// <summary>
/// The connection string (URL) used when performing the requests. /// The connection string (URL) used when performing the requests.
/// </summary> /// </summary>
[AllowNull]
public string ConnectionString { get; set; } public string ConnectionString { get; set; }
/// <summary> /// <summary>
/// The request timeout in seconds. /// The request timeout in seconds.

@ -1,64 +1,96 @@
using System.Data; using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Connected.ServiceModel.Client.Data.Remote; using Connected.ServiceModel.Client.Data.Remote;
namespace Connected.ServiceModel.Client.Data; namespace Connected.ServiceModel.Client.Data;
internal sealed class TableDataCommand : IDbCommand internal sealed class TableDataCommand : DbCommand
{ {
public TableDataCommand(RemoteTableService tables) public TableDataCommand(RemoteTableService tables)
{ {
Parameters = new DataParameterCollection(); Parameters = new DataParameterCollection();
Tables = tables; Tables = tables;
} }
public string CommandText { get; set; }
public int CommandTimeout { get; set; }
public CommandType CommandType { get; set; }
public IDbConnection? Connection { get; set; }
public IDataParameterCollection Parameters { get; } public new DataParameterCollection Parameters { get; }
public IDbTransaction? Transaction { get; set; }
public UpdateRowSource UpdatedRowSource { get; set; }
public RemoteTableService Tables { get; } public RemoteTableService Tables { get; }
[AllowNull]
public override string CommandText { get; set; }
public override int CommandTimeout { get; set; } = 120;
public override CommandType CommandType { get; set; } = CommandType.Text;
protected override DbConnection? DbConnection { get; set; }
protected override DbParameterCollection DbParameterCollection => Parameters;
protected override DbTransaction? DbTransaction { get; set; }
public override bool DesignTimeVisible { get; set; }
public override UpdateRowSource UpdatedRowSource { get; set; }
protected override DbParameter CreateDbParameter()
{
return new TableDataParameter();
}
public void Cancel() public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{ {
await Tables.Update(ParseCommandText());
return 0;
} }
public IDbDataParameter CreateParameter() public override int ExecuteNonQuery()
{ {
return new TableDataParameter(); AsyncUtils.RunSync(() => Tables.Update(ParseCommandText()));
return 0;
} }
public void Dispose() protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{ {
return new TableDataReader(await Tables.Query(ParseCommandText()));
}
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
return new TableDataReader(AsyncUtils.RunSync(() => Tables.Query(ParseCommandText())));
} }
public int ExecuteNonQuery() private string ParseCommandText()
{ {
AsyncUtils.RunSync(() => Tables.Update(CommandText)); var text = new StringBuilder(CommandText);
return 0; foreach (IDbDataParameter parameter in Parameters)
text = text.Replace(parameter.ParameterName, ParseValue(parameter));
return text.ToString();
} }
public IDataReader ExecuteReader() private static string? ParseValue(IDbDataParameter parameter)
{ {
return new TableDataReader(AsyncUtils.RunSync(() => Tables.Query(CommandText))); if (parameter.Value is null || parameter.Value == DBNull.Value)
return "null";
return parameter.DbType switch
{
DbType.AnsiString or DbType.String or DbType.AnsiStringFixedLength or DbType.StringFixedLength or DbType.Xml => $"'{parameter.Value}'",
_ => parameter.Value.ToString(),
};
} }
public IDataReader ExecuteReader(CommandBehavior behavior) public override void Cancel()
{ {
return new TableDataReader(AsyncUtils.RunSync(() => Tables.Query(CommandText)));
} }
public object? ExecuteScalar() public override object? ExecuteScalar()
{ {
throw new NotImplementedException(); var reader = ExecuteReader();
if (reader.Read())
return reader[0];
return null;
} }
public void Prepare() public override void Prepare()
{ {
throw new NotImplementedException();
} }
} }

@ -1,8 +1,8 @@
using System.Data; using System.Data;
using System.Data.Common;
using Connected.Annotations; using Connected.Annotations;
using Connected.Data.Storage; using Connected.Data.Storage;
using Connected.ServiceModel.Client.Data.Remote; using Connected.ServiceModel.Client.Data.Remote;
using Microsoft.Data.SqlClient;
namespace Connected.ServiceModel.Client.Data; namespace Connected.ServiceModel.Client.Data;
@ -19,7 +19,7 @@ internal sealed class TableDataConnection : DatabaseConnection
{ {
if (cmd.Parameters.Count > 0) if (cmd.Parameters.Count > 0)
{ {
foreach (SqlParameter i in cmd.Parameters) foreach (DbParameter i in cmd.Parameters)
i.Value = DBNull.Value; i.Value = DBNull.Value;
return; return;
@ -30,7 +30,7 @@ internal sealed class TableDataConnection : DatabaseConnection
foreach (var i in command.Operation.Parameters) foreach (var i in command.Operation.Parameters)
{ {
cmd.Parameters.Add(new SqlParameter cmd.Parameters.Add(new TableDataParameter
{ {
ParameterName = i.Name, ParameterName = i.Name,
DbType = i.Type, DbType = i.Type,
@ -39,17 +39,17 @@ internal sealed class TableDataConnection : DatabaseConnection
} }
} }
protected override object GetParameterValue(IDbCommand command, string parameterName) protected override object? GetParameterValue(IDbCommand command, string parameterName)
{ {
if (command is SqlCommand cmd) if (command is TableDataCommand cmd)
return cmd.Parameters[parameterName].Value; return cmd.Parameters[parameterName].Value;
return null; return null;
} }
protected override void SetParameterValue(IDbCommand command, string parameterName, object value) protected override void SetParameterValue(IDbCommand command, string parameterName, object? value)
{ {
if (command is SqlCommand cmd) if (command is TableDataCommand cmd)
cmd.Parameters[parameterName].Value = value; cmd.Parameters[parameterName].Value = value;
} }

@ -1,16 +1,20 @@
using System.Data; using System.Data;
using System.Data.Common;
namespace Connected.ServiceModel.Client.Data; namespace Connected.ServiceModel.Client.Data;
internal class TableDataParameter : IDbDataParameter internal class TableDataParameter : DbParameter
{ {
public byte Precision { get; set; } public override DbType DbType { get; set; }
public byte Scale { get; set; } public override ParameterDirection Direction { get; set; }
public int Size { get; set; } public override bool IsNullable { get; set; }
public DbType DbType { get; set; } public override string? ParameterName { get; set; }
public ParameterDirection Direction { get; set; } public override int Size { get; set; }
public bool IsNullable { get; set; } public override string? SourceColumn { get; set; }
public string ParameterName { get; set; } public override bool SourceColumnNullMapping { get; set; }
public string SourceColumn { get; set; } public override object? Value { get; set; }
public DataRowVersion SourceVersion { get; set; }
public object? Value { get; set; } public override void ResetDbType()
{
}
} }

@ -1,164 +1,221 @@
using System.Data; using System.Collections;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
namespace Connected.ServiceModel.Client.Data; namespace Connected.ServiceModel.Client.Data;
internal sealed class TableDataReader : IDataReader internal sealed class TableDataReader : DbDataReader
{ {
public TableDataReader(JsonArray items) public TableDataReader(JsonArray items)
{ {
Items = items; Items = items;
} }
public object this[int i] => throw new NotImplementedException(); public override object this[int ordinal] => throw new NotImplementedException();
public object this[string name] => throw new NotImplementedException(); public override object this[string name] => throw new NotImplementedException();
public int Depth => throw new NotImplementedException(); private JsonArray Items { get; }
private int Index { get; set; } = -1;
private JsonObject? Current => Index < 0 ? null : Items[Index] as JsonObject;
public override int Depth => 1;
public override int FieldCount => Current is null ? 0 : Current.Count;
public bool IsClosed => throw new NotImplementedException(); public override bool HasRows => Items.Any();
public int RecordsAffected => throw new NotImplementedException(); public override bool IsClosed => false;
public int FieldCount => throw new NotImplementedException(); public override int RecordsAffected => Items.Count;
public JsonArray Items { get; } public override bool GetBoolean(int ordinal)
public void Close()
{ {
throw new NotImplementedException(); return GetValue<bool>(ordinal);
} }
public void Dispose() public override byte GetByte(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<byte>(ordinal);
} }
public bool GetBoolean(int i) public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
{ {
throw new NotImplementedException(); var value = GetValue<string>(ordinal);
var bytes = Convert.FromBase64String(value);
buffer ??= new byte[bufferOffset + length];
for (var i = 0; i < length; i++)
buffer[bufferOffset + i] = bytes[dataOffset + i];
return length;
} }
public byte GetByte(int i) public override char GetChar(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<char>(ordinal);
} }
public long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferoffset, int length) public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
{ {
throw new NotImplementedException(); var value = GetValue<string>(ordinal);
var bytes = Convert.FromBase64String(value);
buffer ??= new char[bufferOffset + length];
for (var i = 0; i < length; i++)
buffer[bufferOffset + i] = (char)bytes[dataOffset + i];
return length;
} }
public char GetChar(int i) public override string GetDataTypeName(int ordinal)
{ {
throw new NotImplementedException(); /*
* TODO: resolve appropriate type
*/
return typeof(string).Name;
} }
public long GetChars(int i, long fieldoffset, char[]? buffer, int bufferoffset, int length) public override DateTime GetDateTime(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<DateTime>(ordinal);
} }
public IDataReader GetData(int i) public override decimal GetDecimal(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<decimal>(ordinal);
} }
public string GetDataTypeName(int i) public override double GetDouble(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<double>(ordinal);
} }
public DateTime GetDateTime(int i) public override IEnumerator GetEnumerator()
{ {
throw new NotImplementedException(); return Items.GetEnumerator();
} }
public decimal GetDecimal(int i) [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)]
public override Type GetFieldType(int ordinal)
{ {
throw new NotImplementedException(); /*
* TODO: resolve appropriate type
*/
return typeof(string);
} }
public double GetDouble(int i) public override float GetFloat(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<float>(ordinal);
} }
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] public override Guid GetGuid(int ordinal)
public Type GetFieldType(int i)
{ {
throw new NotImplementedException(); return GetValue<Guid>(ordinal);
} }
public float GetFloat(int i) public override short GetInt16(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<short>(ordinal);
} }
public Guid GetGuid(int i) public override int GetInt32(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<int>(ordinal);
} }
public short GetInt16(int i) public override long GetInt64(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<long>(ordinal);
} }
public int GetInt32(int i) public override string GetName(int ordinal)
{ {
throw new NotImplementedException(); if (Current is null)
throw new NullReferenceException();
if (Current.ElementAt(ordinal).Key is string key)
return key;
throw new NullReferenceException();
} }
public long GetInt64(int i) public override int GetOrdinal(string name)
{ {
throw new NotImplementedException(); if (Current is null)
} throw new NullReferenceException();
public string GetName(int i) for (var i = 0; i < Current.Count; i++)
{ {
throw new NotImplementedException(); if (string.Equals(Current.ElementAt(i).Key, name, StringComparison.OrdinalIgnoreCase))
return i;
}
return -1;
} }
public int GetOrdinal(string name) public override string GetString(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<string>(ordinal);
} }
public DataTable? GetSchemaTable() public override object GetValue(int ordinal)
{ {
throw new NotImplementedException(); return GetValue<object>(ordinal);
} }
public string GetString(int i) public override int GetValues(object[] values)
{ {
throw new NotImplementedException(); if (Current is null)
throw new NullReferenceException();
for (var i = 0; i < Current.Count; i++)
{
var value = Current.ElementAt(i);
if (value.Value is not null)
values[i] = value.Value.AsValue().GetValue<object>();
} }
public object GetValue(int i) return Current.Count;
}
public override bool IsDBNull(int ordinal)
{ {
throw new NotImplementedException(); return false;
} }
public int GetValues(object[] values) public override bool NextResult()
{ {
throw new NotImplementedException(); return false;
} }
public bool IsDBNull(int i) public override bool Read()
{ {
throw new NotImplementedException(); if (Items.Count >= Index + 1)
{
Index++;
return true;
} }
public bool NextResult() return false;
}
private T GetValue<T>(int ordinal)
{ {
throw new NotImplementedException(); return GetCurrentValue(ordinal).GetValue<T>();
} }
public bool Read() private JsonValue GetCurrentValue(int ordinal)
{ {
//var items = AsyncUtils.RunSync(() => Service.Query(CommandText)); if (Current is null)
throw new NullReferenceException();
return false; if (Current.ElementAt(ordinal).Value is not JsonValue value)
throw new NullReferenceException();
return value;
} }
} }

@ -67,7 +67,7 @@ internal sealed class TableReader<T> : TableCommand, IStorageReader<T>
public async Task<IDataReader> OpenReader() public async Task<IDataReader> OpenReader()
{ {
if (Connection is null) if (Connection is null)
return default; throw new NullReferenceException(nameof(Connection));
return await Connection.OpenReader(this); return await Connection.OpenReader(this);
} }

@ -2,9 +2,9 @@
namespace Connected.ServiceModel.Client.Net; namespace Connected.ServiceModel.Client.Net;
internal sealed class SelectUrl : ServiceFunction<ConnectedServerUrlArgs, string> internal sealed class SelectUrl : ServiceFunction<ConnectedServerUrlArgs, string?>
{ {
private const string Root = "https://localhost:61599";//"https://connected.tompit.com"; private const string Root = "https://localhost:7069";//"https://connected.tompit.com";
protected override async Task<string?> OnInvoke() protected override async Task<string?> OnInvoke()
{ {
await Task.CompletedTask; await Task.CompletedTask;

Loading…
Cancel
Save