From dfe4f4c213fd8259ede855783f28a79d315a4fc7 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 25 Jun 2017 20:59:53 +0800 Subject: [PATCH] Update MySql --- Src/Asp.Net/MySqlTest/Program.cs | 2 +- Src/Asp.Net/MySqlTest/UnitTest/Insert.cs | 18 ++++++++-------- .../DeleteProvider/DeleteableProvider.cs | 3 ++- .../InsertableProvider/InsertableProvider.cs | 6 ++++-- .../UpdateProvider/UpdateableProvider.cs | 3 ++- .../MySql/SqlBuilder/MySqlInsertBuilder.cs | 21 +++++++++++++++++++ 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Src/Asp.Net/MySqlTest/Program.cs b/Src/Asp.Net/MySqlTest/Program.cs index 2c4bd35d7..54acdef3a 100644 --- a/Src/Asp.Net/MySqlTest/Program.cs +++ b/Src/Asp.Net/MySqlTest/Program.cs @@ -26,7 +26,7 @@ namespace OrmTest new SelectQuery(1).Init(); new AutoClose(1).Init(); new Insert(1).Init(); - // new Delete(1).Init(); + new Delete(1).Init(); // new Update(1).Init(); // new Mapping(1).Init(); // new DataTest(1).Init(); diff --git a/Src/Asp.Net/MySqlTest/UnitTest/Insert.cs b/Src/Asp.Net/MySqlTest/UnitTest/Insert.cs index 954bf1d6c..251fe8b5b 100644 --- a/Src/Asp.Net/MySqlTest/UnitTest/Insert.cs +++ b/Src/Asp.Net/MySqlTest/UnitTest/Insert.cs @@ -27,7 +27,7 @@ namespace OrmTest.UnitTest base.Check(@"INSERT INTO `STudent` (`SchoolId`,`Name`,`CreateTime`) VALUES - (@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();", + (@SchoolId,@Name,@CreateTime) ;SELECT LAST_INSERT_ID();", new List() { new SugarParameter("@SchoolId",0), new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")), @@ -45,7 +45,7 @@ namespace OrmTest.UnitTest base.Check(@"INSERT INTO `STudent` (`Name`) VALUES - (@Name) ;SELECT SCOPE_IDENTITY();", new List() { + (@Name) ;SELECT LAST_INSERT_ID();", new List() { new SugarParameter("@Name","jack") }, t3.Key, t3.Value, "Insert t3 error"); @@ -55,7 +55,7 @@ namespace OrmTest.UnitTest base.Check(@"INSERT INTO `STudent` (`SchoolId`,`CreateTime`) VALUES - (@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();", + (@SchoolId,@CreateTime) ;SELECT LAST_INSERT_ID();", new List() { new SugarParameter("@SchoolId",0), new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")), @@ -64,10 +64,10 @@ namespace OrmTest.UnitTest //Ignore Name and TestId var t5 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql(); - base.Check(@"INSERT INTO `STudent` WITH(UPDLOCK) + base.Check(@"INSERT INTO `STudent` (`SchoolId`,`CreateTime`) VALUES - (@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();", + (@SchoolId,@CreateTime) ;SELECT LAST_INSERT_ID();", new List() { new SugarParameter("@SchoolId",0), new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")), @@ -75,10 +75,10 @@ new List() { ); //Use Lock var t6 = db.Insertable(insertObj).With(SqlWith.UpdLock).ToSql(); - base.Check(@"INSERT INTO `STudent` WITH(UPDLOCK) + base.Check(@"INSERT INTO `STudent` (`SchoolId`,`Name`,`CreateTime`) VALUES - (@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();", + (@SchoolId,@Name,@CreateTime) ;SELECT LAST_INSERT_ID();", new List() { new SugarParameter("@SchoolId",0), new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")), @@ -91,7 +91,7 @@ new List() { base.Check(@"INSERT INTO `STudent` (`ID`,`SchoolId`,`CreateTime`) VALUES - (@ID,@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();", + (@ID,@SchoolId,@CreateTime) ;SELECT LAST_INSERT_ID();", new List() { new SugarParameter("@SchoolId", 0), new SugarParameter("@ID", 0), @@ -120,7 +120,7 @@ new List() { public SqlSugarClient GetInstance() { - SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.MySql, IsAutoCloseConnection = true }); return db; } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index 57f1dadb3..8c6dabd73 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -171,7 +171,8 @@ namespace SqlSugar public IDeleteable With(string lockString) { - DeleteBuilder.TableWithString = lockString; + if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer) + DeleteBuilder.TableWithString = lockString; return this; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 37b474300..8d75a9650 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -92,7 +92,8 @@ namespace SqlSugar public IInsertable With(string lockString) { - this.InsertBuilder.TableWithString = lockString; + if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer) + this.InsertBuilder.TableWithString = lockString; return this; } @@ -165,7 +166,8 @@ namespace SqlSugar PropertyType = PubMethod.GetUnderType(column.PropertyInfo), TableId = i }; - if (columnInfo.PropertyType.IsEnum) { + if (columnInfo.PropertyType.IsEnum) + { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } insertItem.Add(columnInfo); diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 1c3c004bb..51fcc0a66 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -135,7 +135,8 @@ namespace SqlSugar } public IUpdateable With(string lockString) { - this.UpdateBuilder.TableWithString = lockString; + if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer) + this.UpdateBuilder.TableWithString = lockString; return this; } diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlInsertBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlInsertBuilder.cs index aea9adaa9..d75fa0804 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlInsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlInsertBuilder.cs @@ -2,5 +2,26 @@ { public class MySqlInsertBuilder : InsertBuilder { + public override string SqlTemplate + { + get + { + if (IsReturnIdentity) + { + return @"INSERT INTO {0} + ({1}) + VALUES + ({2}) ;SELECT LAST_INSERT_ID();"; + } + else + { + return @"INSERT INTO {0} + ({1}) + VALUES + ({2}) ;"; + + } + } + } } }