mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 优化未发布的功能 IFreeSql.InsertOrUpdate
This commit is contained in:
		@@ -1,4 +1,7 @@
 | 
			
		||||
using FreeSql.Internal;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data.Common;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql.Sqlite.Curd
 | 
			
		||||
@@ -15,16 +18,35 @@ namespace FreeSql.Sqlite.Curd
 | 
			
		||||
        {
 | 
			
		||||
            if (_source?.Any() != true) return null;
 | 
			
		||||
 | 
			
		||||
            var insert = _orm.Insert<T1>()
 | 
			
		||||
                .AsTable(_tableRule).AsType(_table.Type)
 | 
			
		||||
                .WithConnection(_connection)
 | 
			
		||||
                .WithTransaction(_transaction)
 | 
			
		||||
                .NoneParameter(true) as Internal.CommonProvider.InsertProvider<T1>;
 | 
			
		||||
            insert._source = _source;
 | 
			
		||||
            var sql = insert.ToSql();
 | 
			
		||||
            if (sql.StartsWith("INSERT INTO ") == false) return null;
 | 
			
		||||
            _params = insert._params;
 | 
			
		||||
            return $"REPLACE INTO {sql.Substring("INSERT INTO ".Length)}";
 | 
			
		||||
            var sqls = new string[2];
 | 
			
		||||
            var dbParams = new List<DbParameter>();
 | 
			
		||||
            var ds = SplitSourceByIdentityValueIsNull(_source);
 | 
			
		||||
            if (ds.Item1.Any()) sqls[0] = getInsertSql(ds.Item1, false);
 | 
			
		||||
            if (ds.Item2.Any()) sqls[1] = getInsertSql(ds.Item2, true);
 | 
			
		||||
            _params = dbParams.ToArray();
 | 
			
		||||
            if (ds.Item2.Any() == false) return sqls[0];
 | 
			
		||||
            if (ds.Item1.Any() == false) return sqls[1];
 | 
			
		||||
            return string.Join("\r\n\r\n;\r\n\r\n", sqls);
 | 
			
		||||
 | 
			
		||||
            string getInsertSql(List<T1> data, bool flagInsert)
 | 
			
		||||
            {
 | 
			
		||||
                var insert = _orm.Insert<T1>()
 | 
			
		||||
                    .AsTable(_tableRule).AsType(_table.Type)
 | 
			
		||||
                    .WithConnection(_connection)
 | 
			
		||||
                    .WithTransaction(_transaction)
 | 
			
		||||
                    .NoneParameter(true) as Internal.CommonProvider.InsertProvider<T1>;
 | 
			
		||||
                insert._source = data;
 | 
			
		||||
 | 
			
		||||
                if (IdentityColumn != null && flagInsert == false) insert.InsertIdentity();
 | 
			
		||||
 | 
			
		||||
                var sql = insert.ToSql();
 | 
			
		||||
                if (string.IsNullOrEmpty(sql)) return null;
 | 
			
		||||
                if (insert._params?.Any() == true) dbParams.AddRange(insert._params);
 | 
			
		||||
                if (IdentityColumn != null && flagInsert) return sql;
 | 
			
		||||
 | 
			
		||||
                if (sql.StartsWith("INSERT INTO ") == false) return null;
 | 
			
		||||
                return $"REPLACE INTO {sql.Substring("INSERT INTO ".Length)}";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user