private string ResovleCount() { var table = DbMetaCache.GetTable(typeof(T)).TableName; 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 = DbMetaCache.GetTable(typeof(T)).TableName; var where = ResolveWhere(); var sql = $"DELETE FROM {table}{where}"; return(sql); }
private string ResovleExists() { var table = DbMetaCache.GetTable(typeof(T)).TableName; 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 = DbMetaCache.GetTable(typeof(T)).TableName; var filters = new GroupExpressionResovle(_filterExpression).Resovle().Split(','); var columns = DbMetaCache.GetColumns(typeof(T)); var intcolumns = columns .Where(a => !filters.Contains(a.ColumnName) && !a.IsNotMapped && !a.IsIdentity); var columnNames = string.Join(",", intcolumns.Select(s => s.ColumnName)); var parameterNames = string.Join(",", intcolumns.Select(s => $"@{s.CsharpName}")); var sql = $"INSERT INTO {table}({columnNames}) VALUES ({parameterNames})"; if (identity) { sql = $"{sql};SELECT @@IDENTITY"; } return(sql); }
private string ResolveSelect() { var table = DbMetaCache.GetTable(typeof(T)).TableName; 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 = DbMetaCache.GetTable(typeof(T)).TableName; 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 = DbMetaCache.GetColumns(typeof(T)); var updcolumns = columns .Where(a => !filters.Contains(a.ColumnName)) .Where(a => !a.IsIdentity && !a.IsPrimaryKey && !a.IsNotMapped) .Select(s => $"{s.ColumnName} = @{s.CsharpName}"); if (string.IsNullOrEmpty(where)) { var primaryKey = columns.Where(a => a.IsPrimaryKey).FirstOrDefault() ?? columns.First(); where = $" WHERE {primaryKey.ColumnName} = @{primaryKey.CsharpName}"; } var sql = $"UPDATE {table} SET {string.Join(",", updcolumns)}{where}"; return(sql); } }