protected override Expression VisitNew(NewExpression node) { for (int i = 0; i < node.Arguments.Count; i++) { var item = node.Arguments[i]; var column = node.Members[i].Name; if (item is MemberExpression member) { var name = TableInfoCache .GetColumnName(member.Member.DeclaringType, member.Member.Name); if (name != column) { _textBuilder.Append($"{name} AS {column}"); } else { _textBuilder.Append($"{name}"); } } else if (item is MethodCallExpression) { var expression = new FunctionExpressionResovle(item).Resovle(); _textBuilder.Append($"{expression} AS {column}"); } if (i != node.Arguments.Count - 1) { _textBuilder.Append(","); } } return(node); }
protected override Expression VisitMember(MemberExpression node) { var name = TableInfoCache.GetColumnName(node.Member.DeclaringType, node.Member.Name); _textBuilder.Append($"{name},"); return(node); }
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 ResolveColumns() { if (_selectExpression == null) { var filters = new GroupExpressionResovle(_filterExpression).Resovle().Split(','); var columns = TableInfoCache.GetColumns(typeof(T)) .Where(a => !filters.Contains(a.ColumnName)) .Select(s => s.ColumnName != s.PropertyName ? $"{s.ColumnName} AS {s.PropertyName}" : s.PropertyName); return(string.Join(",", columns)); } else { return(new SelectExpressionResovle(_selectExpression).Resovle()); } }
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); }
protected override Expression VisitMemberInit(MemberInitExpression node) { for (int i = 0; i < node.Bindings.Count; i++) { var item = node.Bindings[i] as MemberAssignment; if (item.Expression is MemberExpression member) { var name = TableInfoCache .GetColumnName(member.Member.DeclaringType, member.Member.Name); _textBuilder.Append($"{name} AS {item.Member.Name}"); } else if (item.Expression is MethodCallExpression) { var expression = new FunctionExpressionResovle(item.Expression).Resovle(); _textBuilder.Append($"{expression} AS {item.Member.Name}"); } if (i != node.Bindings.Count - 1) { _textBuilder.Append(","); } } return(node); }
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); } }
private void LoadTableInfo() { ClearTableData(); bool staleCacheData; TableDescription table = TableInfoCache.GetValue(TableName, this.DescribeTable, out staleCacheData); this.ContainsCachedData = staleCacheData; if (this.ContainsCachedData) { LoggerInstance.InfoFormat("Description for table [{0}] loaded from SDK Cache", TableName); } foreach (var key in table.KeySchema) { string keyName = key.AttributeName; AttributeDefinition attributeDefinition = table.AttributeDefinitions .FirstOrDefault(a => string.Equals(a.AttributeName, keyName, StringComparison.Ordinal)); if (attributeDefinition == null) { throw new InvalidOperationException("No attribute definition found for key " + key.AttributeName); } KeyDescription keyDescription = new KeyDescription { IsHash = string.Equals(key.KeyType, "HASH", StringComparison.OrdinalIgnoreCase), Type = GetType(attributeDefinition.AttributeType) }; if (keyDescription.IsHash) { HashKeys.Add(keyName); } else { RangeKeys.Add(keyName); } Keys[keyName] = keyDescription; } if (table.LocalSecondaryIndexes != null) { foreach (var index in table.LocalSecondaryIndexes) { LocalSecondaryIndexes[index.IndexName] = index; LocalSecondaryIndexNames.Add(index.IndexName); } } if (table.GlobalSecondaryIndexes != null) { foreach (var index in table.GlobalSecondaryIndexes) { GlobalSecondaryIndexes[index.IndexName] = index; GlobalSecondaryIndexNames.Add(index.IndexName); } } foreach (var attribute in table.AttributeDefinitions) { Attributes.Add(attribute); } KeyNames = Keys.Keys.ToArray(); }