Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
dcc026d5bc
  1. 1
      OrmTest/OrmTest.csproj
  2. 14
      OrmTest/Program.cs
  3. 52
      OrmTest/UnitTest/Insert.cs
  4. 13
      SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/DeleteBuilder.cs
  5. 57
      SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs
  6. 10
      SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/QueryBuilder.cs
  7. 13
      SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/SqlQueryBuilder.cs
  8. 13
      SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/UpdateBuilder.cs
  9. 63
      SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  10. 16
      SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  11. 6
      SqlSugar/Entities/SqlWith.cs
  12. 22
      SqlSugar/Interface/IDeleteable.cs
  13. 1
      SqlSugar/Interface/ISqlBuilder/IDMLBuilder.cs
  14. 20
      SqlSugar/Interface/IUpdateable.cs
  15. 20
      SqlSugar/Interface/Insertable.cs
  16. 9
      SqlSugar/SqlSugar.csproj
  17. 15
      SqlSugar/SqlSugarClient.cs

1
OrmTest/OrmTest.csproj

@ -62,6 +62,7 @@
<Compile Include="PerformanceTesting\SqlSugarPerformance.cs" />
<Compile Include="UnitTest\ExpressionTest\ExpTestBase.cs" />
<Compile Include="UnitTest\ExpressionTest\Field.cs" />
<Compile Include="UnitTest\Insert.cs" />
<Compile Include="UnitTest\Query\JoinQuery.cs" />
<Compile Include="UnitTest\ExpressionTest\Method.cs" />
<Compile Include="UnitTest\ExpressionTest\Select.cs" />

14
OrmTest/Program.cs

@ -18,13 +18,13 @@ namespace OrmTest
static void Main(string[] args)
{
//Unit Test
//new Field(1).Init();
//new Where(1).Init();
//new Method(1).Init();
//new JoinQuery(1).Init();
//new SingleQuery(1).Init();
//new SelectQuery(1).Init();
//new AutoClose(200).Init();
new Field(1).Init();
new Where(1).Init();
new Method(1).Init();
new JoinQuery(1).Init();
new SingleQuery(1).Init();
new SelectQuery(1).Init();
new AutoClose(1).Init();
//Performance Test
//new SqlSugarPerformance(100).Select();

52
OrmTest/UnitTest/Insert.cs

@ -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;
}
}
}

13
SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/DeleteBuilder.cs

@ -19,6 +19,19 @@ namespace SqlSugar
}
}
public List<SugarParameter> Parameters
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public StringBuilder sql
{
get

57
SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/InsertBuilder.cs

@ -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);
}
}
}

10
SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/QueryBuilder.cs

@ -13,7 +13,7 @@ namespace SqlSugar
public QueryBuilder()
{
this.QueryPars = new List<SugarParameter>();
this.Parameters = new List<SugarParameter>();
}
#region Private Fileds
@ -42,7 +42,7 @@ namespace SqlSugar
public string GroupByValue { get; set; }
public int WhereIndex { get; set; }
public int JoinIndex { get; set; }
public virtual List<SugarParameter> QueryPars { get; set; }
public virtual List<SugarParameter> Parameters { get; set; }
public virtual List<JoinQueryInfo> JoinQueryInfos
{
get
@ -191,8 +191,8 @@ namespace SqlSugar
resolveExpress.MappingTables = Context.MappingTables;
resolveExpress.IgnoreComumnList = Context.IgnoreComumns;
resolveExpress.Resolve(expression, resolveType);
this.QueryPars = new List<SugarParameter>();
this.QueryPars.AddRange(resolveExpress.Parameters);
this.Parameters = new List<SugarParameter>();
this.Parameters.AddRange(resolveExpress.Parameters);
var reval = resolveExpress.Result;
return reval;
}
@ -234,7 +234,7 @@ namespace SqlSugar
this.Take = 0;
this.sql = null;
this.WhereIndex = 0;
this.QueryPars = null;
this.Parameters = null;
this.GroupByValue = null;
this._TableNameString = null;
this.WhereInfos = null;

13
SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/SqlQueryBuilder.cs

@ -51,6 +51,19 @@ namespace SqlSugar
}
}
public List<SugarParameter> Parameters
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public string ToSqlString()
{
throw new NotImplementedException();

13
SqlSugar/Abstract/DbProvider/SqlBuilderProvider/DMLBuilder/UpdateBuilder.cs

@ -20,6 +20,19 @@ namespace SqlSugar
}
}
public List<SugarParameter> Parameters
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public StringBuilder sql
{
get

63
SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -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();
}
}
}

16
SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -31,12 +31,12 @@ namespace SqlSugar
public ISugarQueryable<T> AddParameters(object whereObj)
{
if (whereObj != null)
QueryBuilder.QueryPars.AddRange(Context.Database.GetParameters(whereObj));
QueryBuilder.Parameters.AddRange(Context.Database.GetParameters(whereObj));
return this;
}
public ISugarQueryable<T> AddParameters(SugarParameter[] pars)
{
QueryBuilder.QueryPars.AddRange(pars);
QueryBuilder.Parameters.AddRange(pars);
return this;
}
@ -71,7 +71,7 @@ namespace SqlSugar
var whereValue = QueryBuilder.WhereInfos;
whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString));
if (whereObj != null)
QueryBuilder.QueryPars.AddRange(Context.Database.GetParameters(whereObj));
QueryBuilder.Parameters.AddRange(Context.Database.GetParameters(whereObj));
return this;
}
public ISugarQueryable<T> Where<T2>(Expression<Func<T2, bool>> expression)
@ -109,7 +109,7 @@ namespace SqlSugar
QueryBuilder.HavingInfos = SqlBuilder.AppendHaving(whereString);
if (whereObj != null)
QueryBuilder.QueryPars.AddRange(Context.Database.GetParameters(whereObj));
QueryBuilder.Parameters.AddRange(Context.Database.GetParameters(whereObj));
return this;
}
public ISugarQueryable<T> Having<T2>(Expression<Func<T2, bool>> expression)
@ -420,7 +420,7 @@ namespace SqlSugar
{
QueryBuilder.IsCount = true;
var sql = QueryBuilder.ToSqlString();
var reval = Context.Database.GetInt(sql, QueryBuilder.QueryPars.ToArray());
var reval = Context.Database.GetInt(sql, QueryBuilder.Parameters.ToArray());
QueryBuilder.IsCount = false;
return reval;
}
@ -501,7 +501,7 @@ namespace SqlSugar
public KeyValuePair<string, List<SugarParameter>> ToSql()
{
string sql = QueryBuilder.ToSqlString();
return new KeyValuePair<string, List<SugarParameter>>(sql, QueryBuilder.QueryPars);
return new KeyValuePair<string, List<SugarParameter>>(sql, QueryBuilder.Parameters);
}
public ISugarQueryable<T> With(string withString)
@ -554,7 +554,7 @@ namespace SqlSugar
var reval = InstanceFactory.GetQueryable<TResult>(this.Context.CurrentConnectionConfig);
reval.Context = this.Context;
reval.SqlBuilder = this.SqlBuilder;
reval.SqlBuilder.QueryBuilder.QueryPars = QueryBuilder.QueryPars;
reval.SqlBuilder.QueryBuilder.Parameters = QueryBuilder.Parameters;
reval.SqlBuilder.QueryBuilder.SelectValue = expression;
return reval;
}
@ -599,7 +599,7 @@ namespace SqlSugar
}
else
{
var reval = this.Bind.DataReaderToList<TResult>(tType, dataReader, QueryBuilder.SelectCacheKey);
result = this.Bind.DataReaderToList<TResult>(tType, dataReader, QueryBuilder.SelectCacheKey);
}
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) this.Context.Close();
}

6
SqlSugar/Entities/SqlWith.cs

@ -9,5 +9,11 @@ namespace SqlSugar
public partial class SqlWith
{
public const string NoLock = "WITH(NOLOCK) ";
public const string HoldLock = "WITH(HOLDLOCK)";
public const string PagLock = "WITH(PAGLOCK)";
public const string ReadCommitted = "WITH(READCOMMITTED)";
public const string TabLockX = "WITH(TABLOCKX)";
public const string UpdLock = "WITH(UPDLOCK)";
public const string RowLock = "WITH(ROWLOCK)";
}
}

22
SqlSugar/Interface/IDeleteable.cs

@ -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);
}
}

1
SqlSugar/Interface/ISqlBuilder/IDMLBuilder.cs

@ -8,6 +8,7 @@ namespace SqlSugar
public partial interface IDMLBuilder
{
string SqlTemplate { get; }
List<SugarParameter> Parameters { get; set; }
SqlSugarClient Context { get; set; }
StringBuilder sql { get; set; }
string ToSqlString();

20
SqlSugar/Interface/IUpdateable.cs

@ -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);
}
}

20
SqlSugar/Interface/Insertable.cs

@ -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();
}
}

9
SqlSugar/SqlSugar.csproj

@ -53,6 +53,7 @@
<Compile Include="Abstract\DbProvider\DbFirstProvider\DbFirstProvider.cs" />
<Compile Include="Abstract\DbProvider\DbMaintenanceProvider\DbMaintenanceProvider.cs" />
<Compile Include="Abstract\DbProvider\DbProvider\DbProvider.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
<Compile Include="Entities\Mapping\SugarMappingAttribute.cs" />
<Compile Include="Abstract\QueryableProvider\QueryableAccessory.cs" />
<Compile Include="Abstract\QueryableProvider\QueryableExtendsions.cs" />
@ -139,11 +140,14 @@
<Compile Include="Interface\IDbBind.cs" />
<Compile Include="Interface\IDbFirst.cs" />
<Compile Include="Interface\IDbMaintenance.cs" />
<Compile Include="Interface\IDeleteable.cs" />
<Compile Include="Interface\ILambdaExpressions.cs" />
<Compile Include="Interface\Insertable.cs" />
<Compile Include="Interface\IQueryable.cs" />
<Compile Include="Interface\ISqlBuilder\IDMLBuilder.cs" />
<Compile Include="Interface\ISqlBuilder\ISqlBuilder.cs" />
<Compile Include="Interface\IRewritableMethods.cs" />
<Compile Include="Interface\IUpdateable.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SqlSugarAccessory.cs" />
<Compile Include="SqlSugarClient.cs" />
@ -152,9 +156,8 @@
<Content Include="Lib\Newtonsoft.Json.dll" />
</ItemGroup>
<ItemGroup>
<Folder Include="Abstract\Deleteable\" />
<Folder Include="Abstract\Insertable\" />
<Folder Include="Abstract\Updateable\" />
<Folder Include="Abstract\DeleteProvider\" />
<Folder Include="Abstract\UpdateProvider\" />
<Folder Include="Databases\MySql\" />
<Folder Include="Databases\Oracle\" />
<Folder Include="Databases\Sqlite\" />

15
SqlSugar/SqlSugarClient.cs

@ -128,6 +128,21 @@ namespace SqlSugar
reval.SqlBuilder.QueryBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(base.CurrentConnectionConfig);
return reval;
}
public virtual IInsertable<T> Insertable<T>(params T [] insertObj) where T : class, new()
{
var reval = new InsertableProvider<T>();
reval.Context = this;
var sqlBuilder = InstanceFactory.GetSqlbuilder(base.CurrentConnectionConfig); ;
reval.SqlBuilder = sqlBuilder;
reval.SqlBuilder.QueryBuilder = InstanceFactory.GetQueryBuilder(base.CurrentConnectionConfig);
reval.SqlBuilder.QueryBuilder.Builder = sqlBuilder;
reval.SqlBuilder.Context = reval.SqlBuilder.QueryBuilder.Context = this;
reval.SqlBuilder.QueryBuilder.EntityName = typeof(T).Name;
reval.SqlBuilder.QueryBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(base.CurrentConnectionConfig);
return reval;
}
/// <summary>
/// Lambda Query operation
/// </summary>

Loading…
Cancel
Save