Jelajahi Sumber

#4 support schema name of table for mysql

Lith 8 bulan lalu
induk
melakukan
a000402ccd

+ 10 - 0
Vitorm.sln

@@ -45,6 +45,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Vitorm.EntityGenerate.MsTes
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Vitorm.MsTest", "test\Vitorm.MsTest\Vitorm.MsTest.csproj", "{2A3FCB50-6F13-4CC9-B4B8-658FC7EA65A9}"
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IssuesTest", "IssuesTest", "{39D31813-1E27-4383-ADF9-01C0F9EA1D4B}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Vitorm.MsTest.Issue000_099", "test\IssuesTest\Vitorm.MsTest.Issue000_099\Vitorm.MsTest.Issue000_099.csproj", "{3E370054-F451-4C20-91C3-FD180C1EEF91}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -111,6 +115,10 @@ Global
 		{2A3FCB50-6F13-4CC9-B4B8-658FC7EA65A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2A3FCB50-6F13-4CC9-B4B8-658FC7EA65A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{2A3FCB50-6F13-4CC9-B4B8-658FC7EA65A9}.Release|Any CPU.Build.0 = Release|Any CPU
+		{3E370054-F451-4C20-91C3-FD180C1EEF91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{3E370054-F451-4C20-91C3-FD180C1EEF91}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{3E370054-F451-4C20-91C3-FD180C1EEF91}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{3E370054-F451-4C20-91C3-FD180C1EEF91}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -131,6 +139,8 @@ Global
 		{5A189C1D-E909-4164-BFF1-67BDE2C37D08} = {05176905-A2A5-4015-9F04-2904506C902F}
 		{BAC8E2E5-23DE-4BCF-829F-E6B04C86FEEF} = {7904FE51-04FF-4477-8E3A-CC340389EE32}
 		{2A3FCB50-6F13-4CC9-B4B8-658FC7EA65A9} = {7904FE51-04FF-4477-8E3A-CC340389EE32}
+		{39D31813-1E27-4383-ADF9-01C0F9EA1D4B} = {7904FE51-04FF-4477-8E3A-CC340389EE32}
+		{3E370054-F451-4C20-91C3-FD180C1EEF91} = {39D31813-1E27-4383-ADF9-01C0F9EA1D4B}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {C7DA16E3-9949-49FA-B0B4-F830636DE60F}

+ 0 - 8
src/Vitorm.SqlServer/SqlTranslateService.cs

@@ -44,14 +44,6 @@ namespace Vitorm.SqlServer
         /// </returns>
         public override string EscapeIdentifier(string identifier) => identifier?.Replace("[", "\"[").Replace("]", "\"]");
 
-        public override string DelimitTableName(IEntityDescriptor entityDescriptor)
-        {
-            if (entityDescriptor.schema == null) return DelimitIdentifier(entityDescriptor.tableName);
-
-            return $"{DelimitIdentifier(entityDescriptor.schema)}.{DelimitIdentifier(entityDescriptor.tableName)}";
-        }
-
-
 
         #region EvalExpression
 

+ 2 - 2
src/Vitorm/Sql/SqlTranslate/SqlTranslateArgument.cs

@@ -6,10 +6,10 @@ namespace Vitorm.Sql.SqlTranslate
 {
     public class SqlTranslateArgument
     {
-        public DbContext dbContext { get; protected set; }
+        public SqlDbContext dbContext { get; protected set; }
         public IEntityDescriptor entityDescriptor { get; protected set; }
 
-        public SqlTranslateArgument(DbContext dbContext, IEntityDescriptor entityDescriptor)
+        public SqlTranslateArgument(SqlDbContext dbContext, IEntityDescriptor entityDescriptor)
         {
             this.dbContext = dbContext;
             this.entityDescriptor = entityDescriptor;

+ 9 - 2
src/Vitorm/Sql/SqlTranslate/SqlTranslateService.cs

@@ -381,13 +381,20 @@ namespace Vitorm.Sql.SqlTranslate
 
 
         #region #1 Create :  PrepareAdd
+
+        public Func<SqlTranslateArgument, object, bool> hasKeyValue = Entity_HasKeyValue;
+        public static bool Entity_HasKeyValue(SqlTranslateArgument arg, object entity)
+        {
+            var keyValue = arg.entityDescriptor?.key.GetValue(entity);
+            return keyValue is null || keyValue.Equals(TypeUtil.DefaultValue(arg.entityDescriptor.key.type));
+        }
+        public virtual bool HasKeyValue(SqlTranslateArgument arg, object entity) => hasKeyValue?.Invoke(arg, entity) == true;
         public virtual EAddType Entity_GetAddType(SqlTranslateArgument arg, object entity)
         {
             var key = arg.entityDescriptor.key;
             if (key == null) return EAddType.noKeyColumn;
 
-            var keyValue = key.GetValue(entity);
-            if (keyValue is not null && !keyValue.Equals(TypeUtil.DefaultValue(arg.entityDescriptor.key.type))) return EAddType.keyWithValue;
+            if (HasKeyValue(arg, entity)) return EAddType.keyWithValue;
 
             if (key.isIdentity) return EAddType.identityKey;
 

+ 51 - 0
test/IssuesTest/Vitorm.MsTest.Issue000_099/Issues/Issue_4_Test.cs

@@ -0,0 +1,51 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using Vitorm.MsTest.MySql.Issue_004;
+
+
+namespace Vitorm.MsTest.Issue000_099.Issues
+{
+    /// <summary>
+    /// https://github.com/VitormLib/Vitorm/issues/4
+    /// support schema name of table for mysql
+    /// </summary>
+    [TestClass]
+    public class Issue_004_Test
+    {
+        [TestMethod]
+        public void Test()
+        {
+            var name = Guid.NewGuid().ToString();
+
+            // #1 Init
+            {
+                using var dbContext = Data.DataProvider<MyUser>().CreateSqlDbContext();
+                dbContext.Execute(@"
+create schema IF NOT EXISTS `schemaTest`;
+use `schemaTest`;
+drop table if exists `schemaTest`.`MyUser`;
+CREATE TABLE IF NOT EXISTS `schemaTest`.`MyUser` (`id` int NOT NULL primary key,  `name` varchar(1000) DEFAULT NULL);
+insert into `schemaTest`.`MyUser`(`id`,name) values(1,@name);
+", param: new Dictionary<string, object> { ["name"] = name });
+            }
+
+            // #2 Assert
+            {
+                var user = Data.Get<MyUser>(1);
+                Assert.AreEqual(name, user.name);
+            }
+        }
+    }
+}
+
+// Entity Definition
+namespace Vitorm.MsTest.MySql.Issue_004
+{
+    [System.ComponentModel.DataAnnotations.Schema.Table("MyUser", Schema = "schemaTest")]
+    public class MyUser
+    {
+        [System.ComponentModel.DataAnnotations.Key]
+        public int id { get; set; }
+        public string name { get; set; } 
+    }
+}

+ 35 - 0
test/IssuesTest/Vitorm.MsTest.Issue000_099/Vitorm.MsTest.Issue000_099.csproj

@@ -0,0 +1,35 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>net6.0</TargetFramework>
+        <ImplicitUsings>enable</ImplicitUsings>
+
+        <IsPackable>false</IsPackable>
+        <IsTestProject>true</IsTestProject>
+        <RootNamespace>Vitorm.MsTest</RootNamespace>
+    </PropertyGroup>
+
+    <ItemGroup>
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
+        <PackageReference Include="MSTest.TestAdapter" Version="3.5.1" />
+        <PackageReference Include="MSTest.TestFramework" Version="3.5.1" />
+
+        <PackageReference Include="Vit.Core" Version="2.2.0" />
+    </ItemGroup>
+
+    <ItemGroup>
+        <ProjectReference Include="..\..\..\src\Vitorm\Vitorm.csproj" />
+        <ProjectReference Include="..\..\..\src\Vitorm.Data\Vitorm.Data.csproj" />
+        <ProjectReference Include="..\..\..\src\Vitorm.EntityGenerate\Vitorm.EntityGenerate.csproj" />
+        <ProjectReference Include="..\..\..\src\Vitorm.MySql\Vitorm.MySql.csproj" />
+        <ProjectReference Include="..\..\..\src\Vitorm.Sqlite\Vitorm.Sqlite.csproj" />
+        <ProjectReference Include="..\..\..\src\Vitorm.SqlServer\Vitorm.SqlServer.csproj" />
+    </ItemGroup>
+
+    <ItemGroup>
+        <None Update="appsettings.json">
+            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+        </None>
+    </ItemGroup>
+
+</Project>

+ 22 - 0
test/IssuesTest/Vitorm.MsTest.Issue000_099/appsettings.json

@@ -0,0 +1,22 @@
+{
+  "Vitorm": {
+    "Data": [
+      {
+        "provider": "Sqlite",
+        "namespace": "Vitorm.MsTest.Sqlite",
+        "connectionString": "data source=sqlite.db;"
+      },
+      {
+        "provider": "MySql",
+        "namespace": "Vitorm.MsTest.MySql",
+        "connectionString": "Data Source=localhost;Port=13306;Database=dev-orm;SslMode=none;User Id=root;Password=123456;CharSet=utf8;allowPublicKeyRetrieval=true;"
+      },
+      {
+        "provider": "SqlServer",
+        "namespace": "Vitorm.MsTest.SqlServer",
+        "connectionString": "Server=localhost;Database=dev-orm;User ID=sa;Password=Admin0123;TrustServerCertificate=true;"
+      }
+
+    ]
+  }
+}