sunkaixuan
8 years ago
17 changed files with 296 additions and 49 deletions
@ -0,0 +1,52 @@ |
|||
using OrmTest.Models; |
|||
using SqlSugar; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace OrmTest.UnitTest |
|||
{ |
|||
public class Insert : ExpTestBase |
|||
{ |
|||
private Insert() { } |
|||
public Insert(int eachCount) |
|||
{ |
|||
this.Count = eachCount; |
|||
} |
|||
|
|||
public void Init() { |
|||
var db = GetInstance(); |
|||
var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now }; |
|||
var insertObjs = new List<Student>() { insertObj }.ToArray(); |
|||
|
|||
//Insert reutrn identity
|
|||
db.Insertable<Student>(insertObj).ExecuteReutrnIdentity(); |
|||
|
|||
//Insert reutrn Command Count
|
|||
db.Insertable<Student>(insertObj).ExecuteCommand(); |
|||
|
|||
//Only insert Name
|
|||
db.Insertable<Student>(insertObj).InsertColumns(it => new object[] { it.Name}).ExecuteReutrnIdentity(); |
|||
|
|||
//Ignore Name and TestId
|
|||
db.Insertable<Student>(insertObj).IgnoreColumns(it => new object[] { it.Name,it.TestId }).ExecuteReutrnIdentity(); |
|||
|
|||
//Use Lock
|
|||
db.Insertable<Student>(insertObj).With(SqlWith.UpdLock).ExecuteCommand(); |
|||
|
|||
//ToSql
|
|||
db.Insertable<Student>(insertObj).With(SqlWith.UpdLock).InsertColumns(it => new object[] { it.Name }).ToSql(); |
|||
|
|||
//Insert List<T>
|
|||
db.Insertable<Student>(insertObjs).With(SqlWith.UpdLock).ExecuteCommand(); |
|||
} |
|||
|
|||
public SqlSugarClient GetInstance() |
|||
{ |
|||
SqlSugarClient db = new SqlSugarClient(new SystemTablesConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection=true }); |
|||
return db; |
|||
} |
|||
} |
|||
} |
@ -1,52 +1,57 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
using System.Linq; |
|||
namespace SqlSugar |
|||
{ |
|||
public class InsertBuilder : IDMLBuilder |
|||
{ |
|||
public SqlSugarClient Context |
|||
public InsertBuilder() { |
|||
this.sql = new StringBuilder(); |
|||
} |
|||
public SqlSugarClient Context { get; set; } |
|||
public ILambdaExpressions LambdaExpressions { get; set; } |
|||
public ISqlBuilder Builder { get; set; } |
|||
public StringBuilder sql { get; set; } |
|||
public List<SugarParameter> Parameters { get; set; } |
|||
public string EntityName { get; set; } |
|||
public string TableWithString { get; set; } |
|||
public List<string> ColumNames{ get; set; } |
|||
|
|||
public virtual string SqlTemplate |
|||
{ |
|||
get |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
set |
|||
{ |
|||
throw new NotImplementedException(); |
|||
return @"INSERT INTO {0}
|
|||
({1}) |
|||
VALUES |
|||
({2})";
|
|||
} |
|||
} |
|||
|
|||
public StringBuilder sql |
|||
public void Clear() |
|||
{ |
|||
get |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
set |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
|
|||
public string SqlTemplate |
|||
public virtual string GetTableNameString |
|||
{ |
|||
get |
|||
{ |
|||
throw new NotImplementedException(); |
|||
var result = Builder.GetTranslationTableName(EntityName); |
|||
result += PubConst.Space; |
|||
if (this.TableWithString.IsValuable()) |
|||
{ |
|||
result += TableWithString + PubConst.Space; |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
|
|||
public void Clear() |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public string ToSqlString() |
|||
{ |
|||
throw new NotImplementedException(); |
|||
string columnsString =string.Join("," ,this.ColumNames.Select(it => Builder.GetTranslationColumnName(it))); |
|||
string columnParametersString = string.Join(",", this.ColumNames.Select(it =>Builder.SqlParameterKeyWord+it)); |
|||
return string.Format(this.sql.ToString(),columnsString, columnParametersString); |
|||
} |
|||
} |
|||
} |
|||
|
@ -0,0 +1,63 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public class InsertableProvider<T> : IInsertable<T> where T : class, new() |
|||
{ |
|||
public SqlSugarClient Context { get; set; } |
|||
public IDb Db { get { return Context.Database; } } |
|||
public IDbBind Bind { get { return this.Db.DbBind; } } |
|||
public ISqlBuilder SqlBuilder { get; set; } |
|||
public InsertBuilder InsertBuilder |
|||
{ |
|||
get |
|||
{ |
|||
return this.SqlBuilder.InsertBuilder; |
|||
} |
|||
} |
|||
public int ExecuteCommand() |
|||
{ |
|||
return Db.ExecuteCommand(InsertBuilder.ToSqlString(), InsertBuilder.Parameters); |
|||
} |
|||
public KeyValuePair<string, List<SugarParameter>> ToSql() |
|||
{ |
|||
string sql = InsertBuilder.ToSqlString(); |
|||
return new KeyValuePair<string, List<SugarParameter>>(sql, InsertBuilder.Parameters); |
|||
} |
|||
|
|||
public int ExecuteReutrnIdentity() |
|||
{ |
|||
return Db.GetInt(InsertBuilder.ToSqlString(), InsertBuilder.Parameters); |
|||
} |
|||
|
|||
public IInsertable<T> IgnoreColumns(Expression<Func<T, object[]>> columns) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public IInsertable<T> Insert(T InsertObj) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public IInsertable<T> InsertColumns(Expression<Func<T, object[]>> columns) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public IInsertable<T> InsertRange(List<T> InsertObjs) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public IInsertable<T> With(string lockString) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public interface IDeleteable<T> |
|||
{ |
|||
int ExecuteCommand(); |
|||
IInsertable<T> TableName(string name); |
|||
IInsertable<T> With(string lockString); |
|||
IInsertable<T> Where(T deleteObj); |
|||
IInsertable<T> Where(Expression<Func<T, bool>> expression); |
|||
IInsertable<T> Where(List<T> deleteObjs); |
|||
IInsertable<T> Where<PkType>(PkType primaryKeyValue); |
|||
IInsertable<T> Where<PkType>(PkType [] primaryKeyValues); |
|||
IInsertable<T> Where(string whereString,object whereObj); |
|||
} |
|||
} |
@ -0,0 +1,20 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public interface IUpdateable<T> |
|||
{ |
|||
int ExecuteCommand(); |
|||
IInsertable<T> With(string lockString); |
|||
IInsertable<T> Update(T InsertObj); |
|||
IInsertable<T> Where(bool isUpdateNull); |
|||
IInsertable<T> UpdateColumns(Expression<Func<T, object[]>> columns); |
|||
IInsertable<T> IgnoreColumns(Expression<Func<T, object[]>> columns); |
|||
IInsertable<T> UpdateRange(List<T> InsertObjs); |
|||
} |
|||
} |
@ -0,0 +1,20 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public interface IInsertable<T> |
|||
{ |
|||
int ExecuteCommand(); |
|||
int ExecuteReutrnIdentity(); |
|||
IInsertable<T> With(string lockString); |
|||
IInsertable<T> InsertColumns(Expression<Func<T,object []>> columns); |
|||
IInsertable<T> IgnoreColumns(Expression<Func<T, object[]>> columns); |
|||
KeyValuePair<string, List<SugarParameter>> ToSql(); |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue