void BuildDeleteSql() { var where = string.Empty; var table = TableInfoManager.GetTable(_context.ElementType); tableName = table.Name; var tableFullName = GetTableName(table); if (_context.Conditions.Any()) { where = BuildWhere(_context.Conditions); } var sql = "DELETE FROM {0} {1}"; sql = string.Format(sql, tableFullName, where); _result.CommandText = sql; }
void BuildUpdateSql() { var where = string.Empty; var table = TableInfoManager.GetTable(_context.ElementType); var keyColumnName = string.Empty; var keyColumn = table.Columns.FirstOrDefault(x => x.Value.IsKey).Value; if (keyColumn != null) { keyColumnName = keyColumn.PropertyInfo.Name; } tableName = table.Name; var tableFullName = GetTableName(table); if (_context.Conditions.Any()) { where = BuildWhere(_context.Conditions); } var setts = new List <string>(); var alias = string.Empty; foreach (var key in _context.UpdateResult.Keys) { if (key == keyColumnName) { continue; } alias = ParserUtils.GenerateAlias(key); var set = string.Format("[{0}] = @{1}", key, alias); _result.Parameters.Add(alias, _context.UpdateResult.Get(key)); setts.Add(set); } var sql = "UPDATE {0} SET {1} {2}"; sql = string.Format(sql, tableFullName, string.Join(",", setts), where); _result.CommandText = sql; }
protected string GetTableAlias(string columnName) { if (!string.IsNullOrWhiteSpace(tableName)) { return(tableName); } foreach (var join in _context.Joins.Values) { if (join.Left.Name == columnName) { return(join.Left.Table.Alias); } else if (join.Right.Name == columnName) { return(join.Right.Table.Alias); } var tableInfo = TableInfoManager.GetTable(join.Left.Table.Type); foreach (var column in tableInfo.Columns.Values) { if (column.Name == columnName) { return(join.Left.Table.Name == tableInfo.Name ? join.Left.Table.Alias : join.Right.Table.Alias); } } tableInfo = TableInfoManager.GetTable(join.Right.Table.Type); foreach (var column in tableInfo.Columns.Values) { if (column.Name == columnName) { return(join.Left.Table.Name == tableInfo.Name ? join.Left.Table.Alias : join.Right.Table.Alias); } } } throw new Exception(); }
public int SaveChanges() { Dictionary <string, object> dbSets; if (!_dbSets.TryGetValue(_dataContextType, out dbSets)) { throw new Exception("初始有问题"); } var provider = ProviderFactory.CreateProvider(ConfigManager.DataBaseType); var count = 0; using (var scope = new TransactionScope()) { var op = provider.CreateEntityOperator(); foreach (IEntityOperator dbSet in dbSets.Values) { var list = dbSet.GetAdding(); var total = op.InsertEntities(list); if (total != list.Count) { throw new Exception("批量插入失败"); } count += total; var editings = dbSet.GetEditing(); var entityType = dbSet.GetEntityType(); var table = TableInfoManager.GetTable(entityType); var keyColumn = table.Columns.FirstOrDefault(x => x.Value.IsKey).Value; if (keyColumn == null) { throw new InvalidOperationException("实体" + entityType.FullName + "不存在主键,无法更新"); } var getters = ExpressionReflector.GetGetters(entityType); var keyGetter = getters.Get(keyColumn.PropertyInfo.Name); if (keyGetter == null) { throw new InvalidOperationException("keyGetter为null"); } foreach (var editing in editings) { var iGetUpdatedValue = editing as IGetUpdatedValues; if (iGetUpdatedValue == null) { continue; } var values = iGetUpdatedValue.GetUpdatedValues(); if (values.Count <= 0) { continue; } if (values.Get(keyColumn.PropertyInfo.Name) != null) { throw new InvalidOperationException("不允许更新主键"); } var keyValue = keyGetter(editing); values.Add(keyColumn.Name, keyValue); op.UpdateValues(keyColumn, table, values); } var removings = dbSet.GetRemoving(); var ids = new List <int>(); foreach (var removing in removings) { var kv = keyGetter(removing); if (kv == null) { throw new InvalidOperationException("删除时主键必须有值"); } ids.Add(Convert.ToInt32(kv)); } if (ids.Any()) { op.Delete(keyColumn, table, ids.ToArray()); } } scope.Complete(); foreach (IEntityOperator item in dbSets.Values) { item.ClearAdding(); item.ClearEditing(); item.ClearRemoving(); } } return(count); }