Lith hai 11 meses
pai
achega
0f3217121c

+ 2 - 2
src/Vit.Orm.Sqlite/Extensions/DbContext_Extensions.cs → src/Vit.Orm.Sqlite/DbContext_Extensions.cs

@@ -4,7 +4,7 @@ using System.Data;
 using Vit.Orm.Entity;
 using Vit.Orm.Entity.Dapper;
 using Vit.Orm.Sql;
-using Vit.Orm.Sqlite.Sql;
+using Vit.Orm.Sqlite;
 
 namespace Vit.Extensions
 {
@@ -24,7 +24,7 @@ namespace Vit.Extensions
             return dbContext;
         }
 
-        
+
 
     }
 }

+ 59 - 23
src/Vit.Orm.Sqlite/Sql/SqlTranslator.cs → src/Vit.Orm.Sqlite/SqlTranslator.cs

@@ -1,15 +1,14 @@
 using System;
 using System.Collections.Generic;
-using System.IO;
 
 using Vit.Linq.ExpressionTree.ComponentModel;
 using Vit.Linq.ExpressionTree.CollectionsQuery;
 using Vit.Orm.Entity;
 using Vit.Orm.Sql;
-using Vit.Linq.ExpressionTree.ExpressionConvertor;
 using System.Linq;
+using Vit.Orm.Sqlite.Sql;
 
-namespace Vit.Orm.Sqlite.Sql
+namespace Vit.Orm.Sqlite
 {
     public class SqlTranslator : ISqlTranslator
     {
@@ -22,6 +21,10 @@ namespace Vit.Orm.Sqlite.Sql
         }
 
 
+        public IEntityDescriptor GetEntityDescriptor(Type entityType) => dbContext.GetEntityDescriptor(entityType);
+
+
+
         public string PrepareCreate(IEntityDescriptor entityDescriptor)
         {
             /* //sql
@@ -48,7 +51,7 @@ CREATE TABLE `user` (
             }
 
             return $@"
-CREATE TABLE `{entityDescriptor.tableName}` (
+CREATE TABLE {DelimitIdentifier(entityDescriptor.tableName)} (
 {string.Join(",\r\n", sqlFields)}
 )";
 
@@ -64,8 +67,8 @@ CREATE TABLE `{entityDescriptor.tableName}` (
                     nullable = true;
                     type = type.GetGenericArguments()[0];
                 }
-                // `name` varchar(100) DEFAULT NULL
-                return $"  `{column.name}` {GetDbType(type)} {(nullable ? "DEFAULT NULL" : "NOT NULL")}";
+                // name varchar(100) DEFAULT NULL
+                return $"  {DelimitIdentifier(column.name)} {GetDbType(type)} {(nullable ? "DEFAULT NULL" : "NOT NULL")}";
             }
             string GetDbType(Type type)
             {
@@ -93,7 +96,7 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             var entityDescriptor = dbSet.entityDescriptor;
 
             // #2 build sql
-            string sql = $@"select * from `{entityDescriptor.tableName}` where `{entityDescriptor.keyName}`=@{entityDescriptor.keyName};";
+            string sql = $@"select * from {DelimitIdentifier(entityDescriptor.tableName)} where {DelimitIdentifier(entityDescriptor.keyName)}={GenerateParameterName(entityDescriptor.keyName)};";
 
             return sql;
         }
@@ -128,7 +131,7 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             var entityDescriptor = dbSet.entityDescriptor;
 
             // #1 GetSqlParams 
-            Func<Entity, Dictionary<string, object>> GetSqlParams = (Entity entity) =>
+            Func<Entity, Dictionary<string, object>> GetSqlParams = (entity) =>
                 {
                     var sqlParam = new Dictionary<string, object>();
                     foreach (var column in entityDescriptor.allColumns)
@@ -150,13 +153,13 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             {
                 columnName = column.name;
 
-                columnNames.Add($"`{columnName}`");
-                valueParams.Add($"@{columnName}");
+                columnNames.Add( DelimitIdentifier(columnName));
+                valueParams.Add(GenerateParameterName(columnName));
             }
             #endregion
 
             // #3 build sql
-            string sql = $@"insert into `{entityDescriptor.tableName}`({string.Join(",", columnNames)}) values({string.Join(",", valueParams)});";
+            string sql = $@"insert into {DelimitIdentifier(entityDescriptor.tableName)}({string.Join(",", columnNames)}) values({string.Join(",", valueParams)});";
             //sql+=$"select seq from sqlite_sequence where name = '{tableName}'; ";
 
             return (sql, GetSqlParams);
@@ -172,7 +175,7 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             var sqlParam = new Dictionary<string, object>();
 
             // #1 GetSqlParams
-            Func<Entity, Dictionary<string, object>> GetSqlParams = (Entity entity) =>
+            Func<Entity, Dictionary<string, object>> GetSqlParams = (entity) =>
             {
                 var sqlParam = new Dictionary<string, object>();
                 foreach (var column in entityDescriptor.allColumns)
@@ -192,11 +195,11 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             foreach (var column in entityDescriptor.columns)
             {
                 columnName = column.name;
-                columnsToUpdate.Add($"`{columnName}`=@{columnName}");
+                columnsToUpdate.Add($"{DelimitIdentifier(columnName)}={GenerateParameterName(columnName)}");
             }
 
             // #3 build sql
-            string sql = $@"update `{entityDescriptor.tableName}` set {string.Join(",", columnsToUpdate)} where `{entityDescriptor.keyName}`=@{entityDescriptor.keyName};";
+            string sql = $@"update {DelimitIdentifier(entityDescriptor.tableName)} set {string.Join(",", columnsToUpdate)} where {DelimitIdentifier(entityDescriptor.keyName)}={GenerateParameterName(entityDescriptor.keyName)};";
 
             return (sql, GetSqlParams);
         }
@@ -210,7 +213,7 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             var entityDescriptor = dbSet.entityDescriptor;
 
             // #2 build sql
-            string sql = $@"delete from `{entityDescriptor.tableName}` where `{entityDescriptor.keyName}`=@{entityDescriptor.keyName};";
+            string sql = $@"delete from {DelimitIdentifier(entityDescriptor.tableName)} where {DelimitIdentifier(entityDescriptor.keyName)}={GenerateParameterName(entityDescriptor.keyName)};";
 
             return sql;
         }
@@ -223,7 +226,7 @@ CREATE TABLE `{entityDescriptor.tableName}` (
             var entityDescriptor = dbSet.entityDescriptor;
 
             // #2 build sql
-            string sql = $@"delete from `{entityDescriptor.tableName}` where `{entityDescriptor.keyName}` in @keys;";
+            string sql = $@"delete from {DelimitIdentifier(entityDescriptor.tableName)} where {DelimitIdentifier(entityDescriptor.keyName)} in {GenerateParameterName("keys")};";
 
             return sql;
         }
@@ -234,13 +237,16 @@ CREATE TABLE `{entityDescriptor.tableName}` (
         }
 
 
-        public string GetSqlField(string tableName, string columnName)
+       
+
+
+        public virtual string GetSqlField(string tableName, string columnName)
         {
-            return $"`{tableName}`.`{columnName}`";
+            return $"{DelimitIdentifier(tableName)}.{DelimitIdentifier(columnName)}";
         }
 
 
-        public string GetSqlField(ExpressionNode_Member member)
+        public virtual string GetSqlField(ExpressionNode_Member member)
         {
             var memberName = member.memberName;
             if (string.IsNullOrWhiteSpace(memberName))
@@ -268,22 +274,52 @@ CREATE TABLE `{entityDescriptor.tableName}` (
                 case nameof(Enumerable.Count):
                     {
                         if (columnName == null) return $"{functionName}(*)";
-                        return $"{functionName}(`{tableName}`.`{columnName}`)";
+                        return $"{functionName}({GetSqlField(tableName, columnName)})";
                     }
                 case nameof(Enumerable.Max) or nameof(Enumerable.Min) or nameof(Enumerable.Sum):
                     {
-                        return $"{functionName}(`{tableName}`.`{columnName}`)";
+                        return $"{functionName}({GetSqlField(tableName, columnName)})";
                     }
                 case nameof(Enumerable.Average):
                     {
-                        return $"AVG(`{tableName}`.`{columnName}`)";
+                        return $"AVG({GetSqlField(tableName, columnName)})";
                     }
             }
             throw new NotSupportedException("[SqlTranslator] unsupported aggregate function : " + functionName);
         }
 
 
-        public IEntityDescriptor GetEntityDescriptor(Type entityType) => dbContext.GetEntityDescriptor(entityType);
+    
+
+
 
+        #region DelimitIdentifier
+        /// <summary>
+        ///     Generates the delimited SQL representation of an identifier (column name, table name, etc.).
+        /// </summary>
+        /// <param name="identifier">The identifier to delimit.</param>
+        /// <returns>
+        ///     The generated string.
+        /// </returns>
+        public virtual string DelimitIdentifier(string identifier) => $"\"{EscapeIdentifier(identifier)}\""; // Interpolation okay; strings
+
+        /// <summary>
+        ///     Generates the escaped SQL representation of an identifier (column name, table name, etc.).
+        /// </summary>
+        /// <param name="identifier">The identifier to be escaped.</param>
+        /// <returns>
+        ///     The generated string.
+        /// </returns>
+        public virtual string EscapeIdentifier(string identifier) => identifier.Replace("\"", "\"\"");
+
+        /// <summary>
+        ///     Generates a valid parameter name for the given candidate name.
+        /// </summary>
+        /// <param name="name">The candidate name for the parameter.</param>
+        /// <returns>
+        ///     A valid name based on the candidate name.
+        /// </returns>
+        public virtual string GenerateParameterName(string name) => name.StartsWith("@", StringComparison.Ordinal) ? name : "@" + name;
+        #endregion
     }
 }

+ 6 - 2
src/Vit.Orm/README.md

@@ -5,11 +5,15 @@ sqlite/transactions  https://learn.microsoft.com/en-us/dotnet/standard/data/sqli
 --------------
 # cur
 
+# support Mysql
+
+
+
 # sqlite upgrade to latest version
-# remove depency of Vit.Db and Dapper
+# remove depency of Dapper
 # try to make it clean
 
-# support Mysql
+
 # support SqlServer
 # support ElasticSearch
 # support ClickHouse