diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-11-14 22:02:45 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-11-14 22:02:45 +0200 |
| commit | 76b22e4d05cbd8d771f678e4b5adc2dc5159afa8 (patch) | |
| tree | 815892d26cbf716d9fae9e01d46109299875d94d /Software/Visual_Studio/DataStore/Tango.DataStore.EF | |
| parent | f838a715af54ef7fc35bf9d99fee95dd8ac6533f (diff) | |
| download | Tango-76b22e4d05cbd8d771f678e4b5adc2dc5159afa8.tar.gz Tango-76b22e4d05cbd8d771f678e4b5adc2dc5159afa8.zip | |
Moved data store projects to DataStore folder.
Diffstat (limited to 'Software/Visual_Studio/DataStore/Tango.DataStore.EF')
8 files changed, 451 insertions, 0 deletions
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreCollection.cs b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreCollection.cs new file mode 100644 index 000000000..0baf5bb19 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreCollection.cs @@ -0,0 +1,156 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; +using Tango.Core.ExtensionMethods; + +namespace Tango.DataStore.EF +{ + public class EFDataStoreCollection : IDataStoreCollection + { + public string Name { get; } + + public EFDataStoreCollection(String name) + { + Name = name; + } + + public void Put<T>(string key, T value) + { + Put(key, (object)value); + } + + public void Put(string key, object value) + { + Put(key, DataStoreHelper.GetDataType(value), value); + } + + public void Put(string key, DataType type, object value) + { + using (var db = ObservablesContext.CreateDefault()) + { + DataStoreItem item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key); + + if (item == null) + { + item = new DataStoreItem(); + db.DataStoreItems.Add(item); + } + + item.CollectionName = Name; + item.Key = key; + item.DataType = (int)type; + item.Value = EFDataStoreHelper.CreateBytes(type, value); + item.IsSynchronized = false; + item.LastUpdated = DateTime.UtcNow; + + db.SaveChanges(); + } + } + + public T Get<T>(string key) + { + return (T)Get(key, null); + } + + public T Get<T>(string key, T defaultValue) + { + return (T)Get(key, (object)defaultValue); + } + + public object Get(string key) + { + return Get(key, null); + } + + public object Get(string key, object defaultValue) + { + return GetItem(key, defaultValue).Value; + } + + public IDataStoreItem GetItem(string key) + { + return GetItem(key, null); + } + + public IDataStoreItem GetItem(string key, object defaultValue) + { + using (var db = ObservablesContext.CreateDefault()) + { + var item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key); + + if (item == null) + { + var globalItem = db.GlobalDataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key); + + if (globalItem == null) + { + if (defaultValue == null) + { + throw new KeyNotFoundException("The specified data store key was not found."); + } + else + { + Put(key, defaultValue); + return GetItem(key); + } + } + else + { + return globalItem.ToDataStoreItem(); + } + } + + return item.ToDataStoreItem(); + } + } + + public List<IDataStoreItem> GetAll() + { + using (var db = ObservablesContext.CreateDefault()) + { + var items = db.DataStoreItems.Where(x => x.CollectionName == Name).ToList().Select(x => x.ToDataStoreItem()).ToList(); + var globalItems = db.GlobalDataStoreItems.Where(x => x.CollectionName == Name).ToList().Select(x => x.ToDataStoreItem()).ToList(); + + + return globalItems.Concat(items).ToList(); + } + } + + public List<IDataStoreItem> GetUnsynchronized() + { + using (var db = ObservablesContext.CreateDefault()) + { + return db.DataStoreItems.Where(x => x.CollectionName == Name && !x.IsSynchronized).ToList().Select(x => x.ToDataStoreItem()).ToList(); + } + } + + public void Delete(string key) + { + using (var db = ObservablesContext.CreateDefault()) + { + db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}' AND [KEY] = '{key}'"); + } + } + + public void DeleteAll() + { + using (var db = ObservablesContext.CreateDefault()) + { + db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}'"); + } + } + + public int Count() + { + using (var db = ObservablesContext.CreateDefault()) + { + return db.DataStoreItems.Count(x => x.CollectionName == Name); + } + } + } +} diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreHelper.cs b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreHelper.cs new file mode 100644 index 000000000..85b3b6731 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreHelper.cs @@ -0,0 +1,105 @@ +using Google.Protobuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; + +namespace Tango.DataStore.EF +{ + public static class EFDataStoreHelper + { + public static byte[] CreateBytes(DataType type, Object obj) + { + switch (type) + { + case DataType.Int32: + return BitConverter.GetBytes((int)obj); + case DataType.Float: + return BitConverter.GetBytes((float)obj); + case DataType.Double: + return BitConverter.GetBytes((double)obj); + case DataType.Boolean: + return BitConverter.GetBytes((bool)obj); + case DataType.String: + return Encoding.Default.GetBytes(obj.ToString()); + case DataType.Bytes: + return (byte[])obj; + case DataType.Proto: + if (obj is DataStoreProtoObject protoMessage) + { + return protoMessage.ToBytes(); + } + else + { + throw new NotSupportedException($"Data type is 'Proto' but object is not of type '{nameof(DataStoreProtoObject)}'."); + } + } + + throw new NotSupportedException("The specified type is not supported."); + } + + public static Object CreateObject(DataType type, byte[] bytes) + { + switch (type) + { + case DataType.Int32: + return BitConverter.ToInt32(bytes, 0); + case DataType.Float: + return BitConverter.ToSingle(bytes, 0); + case DataType.Double: + return BitConverter.ToDouble(bytes, 0); + case DataType.Boolean: + return BitConverter.ToBoolean(bytes, 0); + case DataType.String: + return Encoding.Default.GetString(bytes); + case DataType.Bytes: + return bytes; + case DataType.Proto: + return DataStoreProtoObject.FromBytes(bytes); + } + + throw new NotSupportedException("The specified type is not supported."); + } + + public static IDataStoreItem CreateDataStoreItem(DataStoreItem item) + { + return new EFDataStoreItem() + { + Guid = item.Guid, + Date = item.LastUpdated, + IsSynchronized = item.IsSynchronized, + Key = item.Key, + Type = (DataType)item.DataType, + Value = CreateObject((DataType)item.DataType, item.Value) + }; + } + + public static IDataStoreItem CreateDataStoreItem(GlobalDataStoreItem item) + { + return new EFDataStoreItem() + { + Guid = item.Guid, + Date = item.LastUpdated, + IsSynchronized = true, + Key = item.Key, + Type = (DataType)item.DataType, + Value = CreateObject((DataType)item.DataType, item.Value) + }; + } + + public static DataStoreItem CreateDbDataStoreItem(IDataStoreItem item) + { + return new DataStoreItem() + { + Guid = item.Guid, + LastUpdated = item.Date, + IsSynchronized = item.IsSynchronized, + Key = item.Key, + DataType = (int)item.Type, + Value = CreateBytes(item.Type, item.Value) + }; + } + } +} diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreItem.cs b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreItem.cs new file mode 100644 index 000000000..6bcb97d17 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreItem.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.DataStore.EF +{ + public class EFDataStoreItem : IDataStoreItem + { + public string Guid { get; set; } + public string Key { get; set; } + public DataType Type { get; set; } + public object Value { get; set; } + public DateTime Date { get; set; } + public bool IsSynchronized { get; set; } + + public EFDataStoreItem() + { + Guid = System.Guid.NewGuid().ToString(); + } + + public override string ToString() + { + return DataStoreHelper.FormatDataStoreItem(this); + } + } +} diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreManager.cs b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreManager.cs new file mode 100644 index 000000000..4314a25f9 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreManager.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; + +namespace Tango.DataStore.EF +{ + public class EFDataStoreManager : IDataStoreManager + { + public IDataStoreCollection GetCollection(string name) + { + using (var db = ObservablesContext.CreateDefault()) + { + return new EFDataStoreCollection(name); + } + } + + public List<string> GetCollectionNames() + { + using (var db = ObservablesContext.CreateDefault()) + { + return db.DataStoreItems.Select(x => x.CollectionName).Distinct().ToList(); + } + } + + public void Dispose() + { + //DO Nothing. + } + } +} diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/ExtensionMethods.cs b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/ExtensionMethods.cs new file mode 100644 index 000000000..9f09ae658 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/ExtensionMethods.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.DataStore; +using Tango.DataStore.EF; + +public static class ExtensionMethods +{ + public static IDataStoreItem ToDataStoreItem(this DataStoreItem item) + { + return EFDataStoreHelper.CreateDataStoreItem(item); + } + + public static IDataStoreItem ToDataStoreItem(this GlobalDataStoreItem item) + { + return EFDataStoreHelper.CreateDataStoreItem(item); + } + + public static DataStoreItem ToDbDataStoreItem(this IDataStoreItem item) + { + return EFDataStoreHelper.CreateDbDataStoreItem(item); + } +} + diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/Properties/AssemblyInfo.cs b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..c59e45461 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/Properties/AssemblyInfo.cs @@ -0,0 +1,7 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Tango - Data Store Entity Framework Implementation")] +[assembly: AssemblyVersion("2.0.4.1608")] +[assembly: ComVisible(false)]
\ No newline at end of file diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/Tango.DataStore.EF.csproj b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/Tango.DataStore.EF.csproj new file mode 100644 index 000000000..491c710e0 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/Tango.DataStore.EF.csproj @@ -0,0 +1,89 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{88D9906B-8FC4-4FE0-B7EB-127A0A8FCEE4}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Tango.DataStore.EF</RootNamespace> + <AssemblyName>Tango.DataStore.EF</AssemblyName> + <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <Deterministic>true</Deterministic> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> + <HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath> + </Reference> + <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> + <HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath> + </Reference> + <Reference Include="Google.Protobuf, Version=3.4.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL"> + <HintPath>..\packages\Google.Protobuf.3.4.1\lib\net45\Google.Protobuf.dll</HintPath> + </Reference> + <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> + <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.ComponentModel.DataAnnotations" /> + <Reference Include="System.Core" /> + <Reference Include="System.Xml.Linq" /> + <Reference Include="System.Data.DataSetExtensions" /> + <Reference Include="Microsoft.CSharp" /> + <Reference Include="System.Data" /> + <Reference Include="System.Net.Http" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="..\..\Versioning\GlobalVersionInfo.cs"> + <Link>GlobalVersionInfo.cs</Link> + </Compile> + <Compile Include="EFDataStoreCollection.cs" /> + <Compile Include="EFDataStoreHelper.cs" /> + <Compile Include="EFDataStoreItem.cs" /> + <Compile Include="EFDataStoreManager.cs" /> + <Compile Include="ExtensionMethods.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\Tango.BL\Tango.BL.csproj"> + <Project>{f441feee-322a-4943-b566-110e12fd3b72}</Project> + <Name>Tango.BL</Name> + </ProjectReference> + <ProjectReference Include="..\..\Tango.Core\Tango.Core.csproj"> + <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project> + <Name>Tango.Core</Name> + </ProjectReference> + <ProjectReference Include="..\Tango.DataStore\Tango.DataStore.csproj"> + <Project>{e0364dfa-0721-4637-9d32-9d22aac109d6}</Project> + <Name>Tango.DataStore</Name> + </ProjectReference> + <ProjectReference Include="..\..\Tango.PMR\Tango.PMR.csproj"> + <Project>{e4927038-348d-4295-aaf4-861c58cb3943}</Project> + <Name>Tango.PMR</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> +</Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore.EF/packages.config b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/packages.config new file mode 100644 index 000000000..13be55da1 --- /dev/null +++ b/Software/Visual_Studio/DataStore/Tango.DataStore.EF/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="EntityFramework" version="6.2.0" targetFramework="net461" /> + <package id="Google.Protobuf" version="3.4.1" targetFramework="net461" /> + <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" /> +</packages>
\ No newline at end of file |
