private string ResovleCount() { var table = TableInfoCache.GetTable(typeof(T)).Name; var column = "COUNT(1)"; var where = ResolveWhere(); var group = ResolveGroup(); if (group.Length > 0) { column = group.Remove(0, 10); } else if (_countExpression != null) { column = new SelectExpressionResovle(_countExpression).Resovle(); } var sql = $"SELECT {column} FROM {table}{where}{group}"; if (group.Length > 0) { sql = $"SELECT COUNT(1) FROM ({sql}) as t"; return(sql); } return(sql); }
private string ResovleDelete() { var table = TableInfoCache.GetTable(typeof(T)).Name; var where = ResolveWhere(); var sql = $"DELETE FROM {table}{where}"; return(sql); }
public static string GetColumnName(Type type, string csharpName, bool singleTable) { var columnName = TableInfoCache.GetColumn(type, f => f.CSharpName == csharpName)?.ColumnName ?? csharpName; if (!singleTable) { var tableName = TableInfoCache.GetTable(type).TableName; columnName = string.Format("{0}.{1}", tableName, columnName); } return(columnName); }
private string ResovleExists() { var table = TableInfoCache.GetTable(typeof(T)).Name; var where = ResolveWhere(); var group = ResolveGroup(); var having = ResolveHaving(); var sql = $"SELECT 1 WHERE EXISTS(SELECT 1 FROM {table}{where}{group}{having})"; return(sql); }
private string ResovleInsert(bool identity) { var table = TableInfoCache.GetTable(typeof(T)).Name; var filters = new GroupExpressionResovle(_filterExpression).Resovle().Split(','); var columns = TableInfoCache.GetColumns(typeof(T)); var intcolumns = columns .Where(a => !filters.Contains(a.ColumnName)) .Where(a => !a.IsIdentity); var intcolumnNames = string.Join(",", intcolumns.Select(s => s.ColumnName)); var intcolumnParameters = string.Join(",", intcolumns.Select(s => $"@{s.PropertyName}")); var sql = $"INSERT INTO {table}({intcolumnNames}) VALUES ({intcolumnParameters})"; if (identity) { sql = $"{sql};SELECT @@IDENTITY"; } return(sql); }
private string ResolveSelect() { var table = TableInfoCache.GetTable(typeof(T)).Name; var column = ResolveColumns(); var where = ResolveWhere(); var group = ResolveGroup(); var having = ResolveHaving(); var order = ResolveOrder(); string sql; if (_context.DbContextType == DbContextType.SqlServer) { if (_lockname != string.Empty) { _lockname = $" WITH({_lockname})"; } if (_page.Index == 0) { sql = $"SELECT TOP {_page.Count} {column} FROM {table}{_lockname}{where}{group}{having}{order}"; } else if (_page.Index > 0) { if (order == string.Empty) { order = " ORDER BY (SELECT 1)"; } var limit = $" OFFSET {_page.Index} ROWS FETCH NEXT {_page.Count} ROWS ONLY"; sql = $"SELECT {column} FROM {_lockname}{table}{where}{group}{having}{order}{limit}"; } else { sql = $"SELECT {column} FROM {_lockname}{table}{where}{group}{having}{order}"; } } else { var limit = _page.Index > 0 || _page.Count > 0 ? $" LIMIT {_page.Index},{_page.Count}" : string.Empty; sql = $"SELECT {column} FROM {table}{where}{group}{having}{order}{limit}{_lockname}"; } return(sql); }
private string ResolveUpdate() { var table = TableInfoCache.GetTable(typeof(T)).Name; var builder = new StringBuilder(); if (_setExpressions.Count > 0) { var where = ResolveWhere(); foreach (var item in _setExpressions) { var column = new BooleanExpressionResovle(item.Column).Resovle(); var expression = new BooleanExpressionResovle(item.Expression, _parameters).Resovle(); builder.Append($"{column} = {expression},"); } var sql = $"UPDATE {table} SET {builder.ToString().Trim(',')}{where}"; return(sql); } else { var filters = new GroupExpressionResovle(_filterExpression).Resovle().Split(','); var where = ResolveWhere(); var columns = TableInfoCache.GetColumns(typeof(T)); var updcolumns = columns .Where(a => !a.IsIdentity && a.Key != Attributes.ColumnKey.Primary) .Where(a => !filters.Contains(a.ColumnName)) .Select(s => $"{s.ColumnName} = @{s.PropertyName}"); if (string.IsNullOrEmpty(where)) { var key = (columns.Where(a => a.Key == Attributes.ColumnKey.Primary).FirstOrDefault() ?? columns.First()); where = $" WHERE {key.ColumnName} = @{key.PropertyName}"; } var sql = $"UPDATE {table} SET {string.Join(",", updcolumns)}{where}"; return(sql); } }