Browse Source

Update Updateable

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
599456fcda
  1. 6
      Src/Asp.Net/MySqlTest/Config.cs
  2. 7
      Src/Asp.Net/MySqlTest/Demos/2_Update.cs
  3. 6
      Src/Asp.Net/OracleTest/Demos/2_Update.cs
  4. 35
      Src/Asp.Net/SqlServerTest/Demos/2_Update.cs
  5. 5
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs
  6. 5
      Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlUpdateBuilder.cs
  7. 2
      Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteUpdateBuilder.cs
  8. BIN
      Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite
  9. 6
      Src/Asp.Net/SqliteTest/Demos/2_Update.cs

6
Src/Asp.Net/MySqlTest/Config.cs

@ -8,8 +8,8 @@ namespace OrmTest
{ {
public class Config public class Config
{ {
public static string ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql"; public static string ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=root";
public static string ConnectionString2 = "server=localhost;Database=SQLSUGAR4XTEST;Uid=root;Pwd=haosql"; public static string ConnectionString2 = "server=localhost;Database=SQLSUGAR4XTEST;Uid=root;Pwd=root";
public static string ConnectionString3 = "server=localhost;Database=sqlsugar4xtest;Uid=root;Pwd=haosql"; public static string ConnectionString3 = "server=localhost;Database=sqlsugar4xtest;Uid=root;Pwd=root";
} }
} }

7
Src/Asp.Net/MySqlTest/Demos/2_Update.cs

@ -58,6 +58,13 @@ namespace OrmTest.Demo
//Column is null no update //Column is null no update
db.Updateable(updateObj).Where(true).ExecuteCommand(); db.Updateable(updateObj).Where(true).ExecuteCommand();
db.Updateable(new Student[] { new Student() { Id = 2, Name = "a2" }, new Student() { Id = 1, Name = "a1" } })
.UpdateColumns(it => new { it.Name, it.Id, it.SchoolId })
.WhereColumns(it=>new { it.SchoolId})
.Where(it => it.Id == 1)
.ExecuteCommand();
} }
} }
} }

6
Src/Asp.Net/OracleTest/Demos/2_Update.cs

@ -66,6 +66,12 @@ namespace OrmTest.Demo
db.Updateable(updateObj).Where(true).ExecuteCommand(); db.Updateable(updateObj).Where(true).ExecuteCommand();
db.Updateable(new Student[] { new Student() { Id=2, Name="a2" }, new Student() { Id = 1, Name = "a1" } }).ExecuteCommand(); db.Updateable(new Student[] { new Student() { Id=2, Name="a2" }, new Student() { Id = 1, Name = "a1" } }).ExecuteCommand();
db.Updateable(new Student[] { new Student() { Id = 2, Name = "a2" }, new Student() { Id = 1, Name = "a1" } })
.UpdateColumns(it => new { it.Name, it.Id, it.SchoolId })
.WhereColumns(it => it.Name)
.Where(it => it.Id == 1)
.ExecuteCommand();
} }
} }
} }

35
Src/Asp.Net/SqlServerTest/Demos/2_Update.cs

@ -57,25 +57,25 @@ namespace OrmTest.Demo
//Update Student set Name='jack' Where Id=1 //Update Student set Name='jack' Where Id=1
//Column is null no update //Column is null no update
db.Updateable(updateObj).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand(); db.Updateable(updateObj).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
//sql //sql
db.Updateable(updateObj).Where("id=@x",new { x="1"}).ExecuteCommand(); db.Updateable(updateObj).Where("id=@x", new { x = "1" }).ExecuteCommand();
db.Updateable(updateObj).Where("id","=",1).ExecuteCommand(); db.Updateable(updateObj).Where("id", "=", 1).ExecuteCommand();
var t12 = db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommandAsync(); var t12 = db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommandAsync();
t12.Wait(); t12.Wait();
//update one columns //update one columns
var count = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId).Where(it => it.Id == it.Id+1).ExecuteCommand(); var count = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId).Where(it => it.Id == it.Id + 1).ExecuteCommand();
var count1 = db.Updateable<Student>() var count1 = db.Updateable<Student>()
.UpdateColumnsIF(false,it => it.SchoolId == it.SchoolId)//ignore .UpdateColumnsIF(false, it => it.SchoolId == it.SchoolId)//ignore
.UpdateColumnsIF(true, it => it.SchoolId == 2).//ok .UpdateColumnsIF(true, it => it.SchoolId == 2).//ok
Where(it => it.Id == it.Id + 1).ExecuteCommand(); Where(it => it.Id == it.Id + 1).ExecuteCommand();
//update one columns //update one columns
var count2 = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId+1).Where(it => it.Id == it.Id + 1).ExecuteCommand(); var count2 = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId + 1).Where(it => it.Id == it.Id + 1).ExecuteCommand();
var dt = new Dictionary<string, object>(); var dt = new Dictionary<string, object>();
dt.Add("id", 1); dt.Add("id", 1);
@ -83,7 +83,7 @@ namespace OrmTest.Demo
dt.Add("createTime", DateTime.Now); dt.Add("createTime", DateTime.Now);
var t66 = db.Updateable(dt).AS("student").WhereColumns("id").With(SqlWith.UpdLock).ExecuteCommand(); var t66 = db.Updateable(dt).AS("student").WhereColumns("id").With(SqlWith.UpdLock).ExecuteCommand();
db.Updateable(new Student() { Id = 1 }).UpdateColumns(new string[] { "name" }).Where(it=>it.Name=="1").ExecuteCommand(); db.Updateable(new Student() { Id = 1 }).UpdateColumns(new string[] { "name" }).Where(it => it.Name == "1").ExecuteCommand();
var dt2 = new Dictionary<string, object>(); var dt2 = new Dictionary<string, object>();
dt2.Add("id", 2); dt2.Add("id", 2);
@ -99,29 +99,34 @@ namespace OrmTest.Demo
{ {
SchoolId = SqlFunc.IIF(p.Id == 1, 2, 3) SchoolId = SqlFunc.IIF(p.Id == 1, 2, 3)
}).Where(p => p.Id == 10000).ExecuteCommand(); }).Where(p => p.Id == 10000).ExecuteCommand();
var t21= db.Updateable<Student>().UpdateColumns(p => new Student() var t21 = db.Updateable<Student>().UpdateColumns(p => new Student()
{ {
SchoolId = SqlFunc.IF(p.Id==1).Return(1).End(p.Id) SchoolId = SqlFunc.IF(p.Id == 1).Return(1).End(p.Id)
}).Where(p => p.Id == 10000).ExecuteCommand(); }).Where(p => p.Id == 10000).ExecuteCommand();
var t22= db.Updateable<Student>().UpdateColumns(p => new Student() var t22 = db.Updateable<Student>().UpdateColumns(p => new Student()
{ {
SchoolId = SqlFunc.Subqueryable<Student>().Where(s=>s.SchoolId==p.Id).Select(s=>s.Id) SchoolId = SqlFunc.Subqueryable<Student>().Where(s => s.SchoolId == p.Id).Select(s => s.Id)
}).Where(p => p.Id == 10000).ExecuteCommand(); }).Where(p => p.Id == 10000).ExecuteCommand();
var t23= db.Updateable<Student>(new Student() { }) var t23 = db.Updateable<Student>(new Student() { })
.Where(p => p.SchoolId == SqlFunc.Subqueryable<Student>().Where(s => s.SchoolId == p.Id).Select(s => s.Id)).ExecuteCommand(); .Where(p => p.SchoolId == SqlFunc.Subqueryable<Student>().Where(s => s.SchoolId == p.Id).Select(s => s.Id)).ExecuteCommand();
var t24 = db.Updateable(new Student() { }).WhereColumns(it=>it.CreateTime).ExecuteCommand(); var t24 = db.Updateable(new Student() { }).WhereColumns(it => it.CreateTime).ExecuteCommand();
var t25 = db.Updateable(new Student() { }).UpdateColumns(it=> new { it.Name,it.CreateTime}).WhereColumns(it => it.CreateTime).ExecuteCommand(); var t25 = db.Updateable(new Student() { }).UpdateColumns(it => new { it.Name, it.CreateTime }).WhereColumns(it => it.CreateTime).ExecuteCommand();
var t26 = db.Updateable(new List<Student>() { new Student() { }, new Student() { } }).UpdateColumns(it => new { it.Name, it.CreateTime }).WhereColumns(it => it.CreateTime).ExecuteCommand(); var t26 = db.Updateable(new List<Student>() { new Student() { }, new Student() { } }).UpdateColumns(it => new { it.Name, it.CreateTime }).WhereColumns(it => it.CreateTime).ExecuteCommand();
db.Updateable<Student>().UpdateColumns(it => new Student { SchoolId = GeneratePassword(2, 1), Name =SqlFunc.ToString(it.Name), CreateTime = DateTime.Now.AddDays(1) }).Where(it => it.Id == 1).ExecuteCommand(); db.Updateable<Student>().UpdateColumns(it => new Student { SchoolId = GeneratePassword(2, 1), Name = SqlFunc.ToString(it.Name), CreateTime = DateTime.Now.AddDays(1) }).Where(it => it.Id == 1).ExecuteCommand();
db.Updateable(new Student[] { new Student() { Id = 2, Name = "a2" }, new Student() { Id = 1, Name = "a1" } })
.UpdateColumns(it => new { it.Name, it.Id, it.SchoolId })
.WhereColumns(it => it.Name)
.Where(it => it.Id == 1)
.ExecuteCommand();
} }
private static int GeneratePassword(int v1, int v2) private static int GeneratePassword(int v1, int v2)

5
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar namespace SqlSugar
{ {
@ -188,10 +189,10 @@ namespace SqlSugar
{ {
var isFirst = whereString == null; var isFirst = whereString == null;
whereString += (isFirst ? null : " AND "); whereString += (isFirst ? null : " AND ");
whereString += item; whereString += Regex.Replace(item,"\\"+this.Builder.SqlTranslationLeft,"T."+ this.Builder.SqlTranslationLeft);
} }
} }
else if (PrimaryKeys.HasValue()) if (PrimaryKeys.HasValue())
{ {
foreach (var item in PrimaryKeys) foreach (var item in PrimaryKeys)
{ {

5
Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlUpdateBuilder.cs

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar namespace SqlSugar
{ {
@ -70,10 +71,10 @@ namespace SqlSugar
{ {
var isFirst = whereString == null; var isFirst = whereString == null;
whereString += (isFirst ? null : " AND "); whereString += (isFirst ? null : " AND ");
whereString += item; whereString += Regex.Replace(item, " \\" + this.Builder.SqlTranslationLeft, "T." + this.Builder.SqlTranslationLeft);
} }
} }
else if (PrimaryKeys.HasValue()) if (PrimaryKeys.HasValue())
{ {
foreach (var item in PrimaryKeys) foreach (var item in PrimaryKeys)
{ {

2
Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteUpdateBuilder.cs

@ -19,7 +19,7 @@ namespace SqlSugar
foreach (var item in pkList) foreach (var item in pkList)
{ {
var isFirst = pkList.First() == item; var isFirst = pkList.First() == item;
var whereString = isFirst ? " " : " AND "; var whereString = "";
whereString += GetOracleUpdateColums(item); whereString += GetOracleUpdateColums(item);
whereList.Add(whereString); whereList.Add(whereString);
} }

BIN
Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite

Binary file not shown.

6
Src/Asp.Net/SqliteTest/Demos/2_Update.cs

@ -60,6 +60,12 @@ namespace OrmTest.Demo
db.Updateable(updateObj).Where(true).ExecuteCommand(); db.Updateable(updateObj).Where(true).ExecuteCommand();
db.Updateable(new Student[] { new Student() { Id = 2, Name = "a2" }, new Student() { Id = 1, Name = "a1" } }).ExecuteCommand(); db.Updateable(new Student[] { new Student() { Id = 2, Name = "a2" }, new Student() { Id = 1, Name = "a1" } }).ExecuteCommand();
db.Updateable(new Student[] { new Student() { Id = 2, Name = "a2" }, new Student() { Id = 1, Name = "a1" } })
.UpdateColumns(it => new { it.Name, it.Id, it.SchoolId })
.WhereColumns(it => it.Name)
.Where(it => it.Id == 1)
.ExecuteCommand();
} }
} }
} }

Loading…
Cancel
Save