Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
soul-soft committed Aug 25, 2019
1 parent f3d1b4f commit 5ca6fce
Show file tree
Hide file tree
Showing 33 changed files with 387 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/Dapper.Linq/Dapper.Linq/Dapper.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>1.0.0.1</Version>
<Version>1.0.0.2</Version>
<Description>dapper,mysql,sqlserver,sqlite,linq to sql,subquery,windows function</Description>
<Authors>花间岛</Authors>
<PackageProjectUrl>https://github.com/1448376744/Dapper.Linq</PackageProjectUrl>
Expand Down
32 changes: 31 additions & 1 deletion src/Dapper.Linq/Dapper.Linq/ExpressionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,38 @@ public static Dictionary<string, string> BuildColumn(Expression expression, Dict
return column;
}
}
public static Dictionary<string,string> BuildColumnAndValues(Expression expression,Dictionary<string,object> param,string prefix)
{
var columns = new Dictionary<string,string>();
expression = (expression as LambdaExpression).Body;
var initExpression = (expression as MemberInitExpression);
var type = initExpression.Type;
for (int i = 0; i < initExpression.Bindings.Count; i++)
{
Expression argument = (initExpression.Bindings[i] as MemberAssignment).Expression;
var name = initExpression.Bindings[i].Member.Name;
var columnName = EntityUtil.GetColumn(type, f => f.CSharpName == name).ColumnName;
var value = GetValue(argument);
if (value is Enum)
{
value = Convert.ToInt32(value);
}
else if (value is bool)
{
value = Convert.ToBoolean(value) ? 1 : 0;
}
var key = string.Format("@{0}", name);
columns.Add(columnName,key);
param.Add(key, value);
}
return columns;
}
public static object GetValue(Expression expression)
{
if ((expression is UnaryExpression unaryExpression) && unaryExpression.NodeType == ExpressionType.Convert)
{
expression = unaryExpression.Operand;
}
var names = new Stack<string>();
var exps = new Stack<Expression>();
var mifs = new Stack<System.Reflection.MemberInfo>();
Expand Down Expand Up @@ -393,7 +423,7 @@ public static object GetValue(Expression expression)
else if (expression is ConstantExpression constant)
{
return constant.Value;
}
}
else
{
return Expression.Lambda(expression).Compile().DynamicInvoke();
Expand Down
6 changes: 6 additions & 0 deletions src/Dapper.Linq/Dapper.Linq/IQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public interface IQueryable<T>
IEnumerable<TResult> Select<TResult>(Expression<Func<T, TResult>> columns, bool buffered = true, int? timeout = null);
Task<IEnumerable<TResult>> SelectAsync<TResult>(Expression<Func<T, TResult>> columns, int? timeout = null);
int Insert(T entity, bool condition = true, int? timeout = null);
int Insert(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null);
Task<int> InsertAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null);
long InsertReturnId(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null);
Task<long> InsertReturnIdAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null);
Task<int> InsertAsync(T entity, bool condition = true, int? timeout = null);
long InsertReturnId(T entity, bool condition = true, int? timeout = null);
Task<long> InsertReturnIdAsync(T entity, bool condition = true, int? timeout = null);
Expand All @@ -49,6 +53,8 @@ public interface IQueryable<T>
int Update(bool condition = true, int? timeout = null);
Task<int> UpdateAsync(bool condition = true, int? timeout = null);
int Update(T entity, bool condition = true, int? timeout = null);
int Update(Expression<Func<T,T>> expression, bool condition = true, int? timeout = null);
Task<int> UpdateAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null);
Task<int> UpdateAsync(T entity, bool condition = true, int? timeout = null);
int Update(IEnumerable<T> entitys, bool condition = true, int? timeout = null);
Task<int> UpdateAsync(IEnumerable<T> entitys, bool condition = true, int? timeout = null);
Expand Down
109 changes: 94 additions & 15 deletions src/Dapper.Linq/Dapper.Linq/MySqlQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,44 @@ public async Task<int> DeleteAsync(bool condition = true, int? timeout = null)
}
return 0;
}
public int Insert(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
return _dbcontext.Execute(sql, _param, timeout);
}
return 0;
}
public long InsertReturnId(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
sql = string.Format("{0};SELECT @@IDENTITY;", sql);
return _dbcontext.ExecuteScalar<long>(sql, _param, timeout);
}
return 0;
}
public async Task<int> InsertAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
return await _dbcontext.ExecuteAsync(sql, _param, timeout);
}
return 0;
}
public async Task<long> InsertReturnIdAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
sql = string.Format("{0};SELECT @@IDENTITY;", sql);
return await _dbcontext.ExecuteScalarAsync<long>(sql, _param, timeout);
}
return 0;
}
public int Insert(T entity, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
Expand Down Expand Up @@ -320,6 +358,24 @@ public int Update(T entity, bool condition = true, int? timeout = null)
}
return 0;
}
public int Update(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildUpdate(expression);
return _dbcontext.Execute(sql, _param, timeout);
}
return 0;
}
public async Task<int> UpdateAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildUpdate(expression);
return await _dbcontext.ExecuteAsync(sql, _param, timeout);
}
return 0;
}
public async Task<int> UpdateAsync(T entity, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
Expand Down Expand Up @@ -550,37 +606,60 @@ public async Task<TResult> SumAsync<TResult>(Expression<Func<T, TResult>> expres
#endregion

#region build
public string BuildInsert()
public string BuildInsert(Expression expression = null)
{
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})",
_table.TableName,
string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => s.ColumnName))
, string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => string.Format("@{0}", s.CSharpName))));
return sql;
if (expression == null)
{
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})"
, _table.TableName
, string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => s.ColumnName))
, string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => string.Format("@{0}", s.CSharpName))));
return sql;
}
else
{
var columns = ExpressionUtil.BuildColumnAndValues(expression, _param, _prefix);
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})"
, _table.TableName
, string.Join(",", columns.Keys)
, string.Join(",", columns.Values));
return sql;
}
}
public string BuildUpdate(bool allColumn = true)
{
if (allColumn)
{
var keyColumn = _table.Columns.Find(f => f.ColumnKey == ColumnKey.Primary);
var colums = _table.Columns.FindAll(f => f.ColumnKey != ColumnKey.Primary && !_filters.Exists(e => e == f.ColumnName));
var sql = string.Format("UPDATE {0} SET {1} WHERE {2}",
_table.TableName,
string.Join(",", colums.Select(s => string.Format("{0} = @{1}", s.ColumnName, s.CSharpName))),
_whereBuffer.Length > 0 ? _whereBuffer.ToString() : string.Format("{0} = @{1}", keyColumn.ColumnName, keyColumn.CSharpName)
var columns = _table.Columns.FindAll(f => f.ColumnKey != ColumnKey.Primary && !_filters.Exists(e => e == f.ColumnName));
var sql = string.Format("UPDATE {0} SET {1} WHERE {2}"
, _table.TableName
, string.Join(",", columns.Select(s => string.Format("{0} = @{1}", s.ColumnName, s.CSharpName)))
, _whereBuffer.Length > 0 ? _whereBuffer.ToString() : string.Format("{0} = @{1}", keyColumn.ColumnName, keyColumn.CSharpName)
);
return sql;
}
else
{
var sql = string.Format("UPDATE {0} SET {1}{2}",
_table.TableName,
_setBuffer,
_whereBuffer.Length > 0 ? string.Format(" WHERE {0}", _whereBuffer) : "");
var sql = string.Format("UPDATE {0} SET {1}{2}"
, _table.TableName
, _setBuffer
, _whereBuffer.Length > 0 ? string.Format(" WHERE {0}", _whereBuffer) : "");
return sql;
}

}
public string BuildUpdate(Expression expression)
{
var keyColumn = _table.Columns.Find(f => f.ColumnKey == ColumnKey.Primary);
var columns = ExpressionUtil.BuildColumnAndValues(expression, _param, _prefix).Where(a => a.Key != keyColumn.ColumnName);
var sql = string.Format("UPDATE {0} SET {1} WHERE {2}",
_table.TableName,
string.Join(",", columns.Select(s => string.Format("{0} = {1}", s.Key, s.Value))),
_whereBuffer.Length > 0 ? _whereBuffer.ToString() : string.Format("{0} = @{1}", keyColumn.ColumnName, keyColumn.CSharpName)
);
return sql;
}
public string BuildDelete()
{
var sql = string.Format("DELETE FROM {0}{1}",
Expand Down
113 changes: 96 additions & 17 deletions src/Dapper.Linq/Dapper.Linq/SQLiteQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,44 @@ public async Task<int> DeleteAsync(bool condition = true, int? timeout = null)
}
return 0;
}
public int Insert(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
return _dbcontext.Execute(sql, _param, timeout);
}
return 0;
}
public long InsertReturnId(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
sql = string.Format("{0};SELECT LAST_INSERT_ROWID();", sql);
return _dbcontext.ExecuteScalar<long>(sql, _param, timeout);
}
return 0;
}
public async Task<int> InsertAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
return await _dbcontext.ExecuteAsync(sql, _param, timeout);
}
return 0;
}
public async Task<long> InsertReturnIdAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildInsert(expression);
sql = string.Format("{0};SELECT LAST_INSERT_ROWID();", sql);
return await _dbcontext.ExecuteScalarAsync<long>(sql, _param, timeout);
}
return 0;
}
public int Insert(T entity, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
Expand Down Expand Up @@ -311,6 +349,24 @@ public async Task<int> UpdateAsync(bool condition = true, int? timeout = null)
}
return 0;
}
public int Update(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildUpdate(expression);
return _dbcontext.Execute(sql, _param, timeout);
}
return 0;
}
public async Task<int> UpdateAsync(Expression<Func<T, T>> expression, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
{
var sql = BuildUpdate(expression);
return await _dbcontext.ExecuteAsync(sql, _param, timeout);
}
return 0;
}
public int Update(T entity, bool condition = true, int? timeout = null)
{
if (condition && _dbcontext != null)
Expand Down Expand Up @@ -550,42 +606,65 @@ public async Task<TResult> SumAsync<TResult>(Expression<Func<T, TResult>> expres
#endregion

#region build
public string BuildInsert()
public string BuildInsert(Expression expression = null)
{
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})",
_table.TableName,
string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => s.ColumnName))
, string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => string.Format("@{0}", s.CSharpName))));
return sql;
if (expression == null)
{
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})"
, _table.TableName
, string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => s.ColumnName))
, string.Join(",", _table.Columns.FindAll(f => f.Identity == false && !_filters.Exists(e => e == f.ColumnName)).Select(s => string.Format("@{0}", s.CSharpName))));
return sql;
}
else
{
var columns = ExpressionUtil.BuildColumnAndValues(expression, _param, _prefix);
var sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})"
, _table.TableName
, string.Join(",", columns.Keys)
, string.Join(",", columns.Values));
return sql;
}
}
public string BuildUpdate(bool allColumn = true)
{
if (allColumn)
{
var keyColumn = _table.Columns.Find(f => f.ColumnKey == ColumnKey.Primary);
var colums = _table.Columns.FindAll(f => f.ColumnKey != ColumnKey.Primary && !_filters.Exists(e => e == f.ColumnName));
var sql = string.Format("UPDATE {0} SET {1} WHERE {2}",
_table.TableName,
string.Join(",", colums.Select(s => string.Format("{0} = @{1}", s.ColumnName, s.CSharpName))),
_whereBuffer.Length > 0 ? _whereBuffer.ToString() : string.Format("{0} = @{1}", keyColumn.ColumnName, keyColumn.CSharpName)
var sql = string.Format("UPDATE {0} SET {1} WHERE {2}"
, _table.TableName
, string.Join(",", colums.Select(s => string.Format("{0} = {1}", s.ColumnName, s.CSharpName)))
, _whereBuffer.Length > 0 ? _whereBuffer.ToString() : string.Format("{0} = @{1}", keyColumn.ColumnName, keyColumn.CSharpName)
);
return sql;
}
else
{
var sql = string.Format("UPDATE {0} SET {1}{2}",
_table.TableName,
_setBuffer,
_whereBuffer.Length > 0 ? string.Format(" WHERE {0}", _whereBuffer) : "");
var sql = string.Format("UPDATE {0} SET {1}{2}"
, _table.TableName
, _setBuffer
, _whereBuffer.Length > 0 ? string.Format(" WHERE {0}", _whereBuffer) : "");
return sql;
}

}
public string BuildUpdate(Expression expression)
{
var keyColumn = _table.Columns.Find(f => f.ColumnKey == ColumnKey.Primary);
var columns = ExpressionUtil.BuildColumnAndValues(expression, _param, _prefix).Where(a => a.Key != keyColumn.ColumnName);
var sql = string.Format("UPDATE {0} SET {1} WHERE {2}"
, _table.TableName
, string.Join(",", columns.Select(s => string.Format("{0} = {1}", s.Key, s.Value)))
, _whereBuffer.Length > 0 ? _whereBuffer.ToString() : string.Format("{0} = @{1}", keyColumn.ColumnName, keyColumn.CSharpName)
);
return sql;
}
public string BuildDelete()
{
var sql = string.Format("DELETE FROM {0}{1}",
_table.TableName,
_whereBuffer.Length > 0 ? string.Format(" WHERE {0}", _whereBuffer) : "");
var sql = string.Format("DELETE FROM {0}{1}"
, _table.TableName
, _whereBuffer.Length > 0 ? string.Format(" WHERE {0}", _whereBuffer) : "");
return sql;
}
public string BuildSelect()
Expand Down
Loading

0 comments on commit 5ca6fce

Please sign in to comment.