/// <summary> /// 创建视图语句片段。 /// </summary> /// <param name="context">生成上下文。</param> /// <returns>语句片段。</returns> protected virtual SqlFragment GenerateForMaintenanceCreateView(GenerateContext context) { var operate = (DbCreateViewOperate)context.Data.Operate; var name = context.ConvertName(operate.Name); return(new CreateViewFragment(context, name, operate.Statement)); }
/// <summary> /// 创建数据表语句片段。 /// </summary> /// <param name="context">生成上下文。</param> /// <returns>语句片段。</returns> protected virtual SqlFragment GenerateForMaintenanceCreateTable(GenerateContext context) { var operate = (DbCreateTableOperate)context.Data.Operate; var metadata = context.Metadata.Table(context.Data.Operate.ClrType); if (operate.Type == EOperateType.CreateTable) { var name = context.ConvertName(operate.Name); return(new CreateTableFragment(context, metadata, name)); } else { var members = metadata.InheritSets.SelectMany(a => a.Members).Concat(metadata.Members); INameFragment name = null; if (operate.Type == EOperateType.CreateTempTable) { name = new TempTableNameFragment(context, operate.Name.Name); return(new CreateTempTableFragment(context, members, name) { IsVariable = false }); } else { name = new VariableFragment(context, operate.Name.Name); return(new CreateTempTableFragment(context, members, name) { IsVariable = true }); } } }
/// <summary> /// 生成删除关系语句片段。 /// </summary> /// <param name="context">生成上下文。</param> /// <returns>语句片段。</returns> protected virtual SqlFragment GenerateForMaintenanceDropRelation(GenerateContext context) { var operate = (DbCreateDropRelationOperate)context.Data.Operate; var name = context.ConvertName(operate.Name); var fragment = new DropRelationFragment(context, name) { Foreign = new ObjectNameFragment(context, operate.Foreign.Name, operate.Foreign.Schema), }; return(fragment); }
/// <summary> /// 删除对象语句片段。 /// </summary> /// <param name="context">生成上下文。</param> /// <returns>语句片段。</returns> protected virtual SqlFragment GenerateForMaintenanceDropObject(GenerateContext context) { var operate = (DbDropObjectOperate)context.Data.Operate; var name = context.ConvertName(operate.Name); var kind = EDatabaseObject.Table; switch (operate.Type) { case EOperateType.DropTable: kind = EDatabaseObject.Table; break; case EOperateType.DropView: kind = EDatabaseObject.View; break; default: throw new NotSupportedException(string.Format(Res.NotSupportedGenerateMaintenance, operate.Type)); } return(new DropObjectFragment(context, name, kind)); }
/// <summary> /// 表达式生成删除语句。 /// </summary> /// <param name="context">生成上下文。</param> /// <param name="content">生成表达式。</param> /// <returns>语句片段。</returns> protected virtual SqlFragment GenerateForDeleteStatement(GenerateContext context, DbExpression content) { var data = (StatementContent)context.Data; var item = data.ItemEpxression; var source = CreateSource(context, content) as QueryBaseFragment; var table = data.Table; if (table.InheritSets.Length == 0) { var target = (TableFragment)GetSource(context, item); target.Name = context.ConvertName(data.TargetName); var delete = new DeleteFragment(context, target) { Where = source.Where, Take = source.Take }; delete.AddSource(source.Sources.Where(a => a != target)); return(delete); } else { var createtable = new CreateTempTableFragment(context, table.Keys); var insert = new InsertFragment(context, createtable.Table, source); insert.Members.AddRange(createtable.Table.Members); foreach (var key in table.Keys) { RetrievalMember(context, source, new DbMemberExpression(key.Member, item), null, false); } var block = new BlockFragment(context, createtable, insert); foreach (var subtable in table.InheritSets.Concat(new TableMetadata[] { table }).Reverse()) { var current = new TableFragment(context, subtable); var delete = new DeleteFragment(context, current); var temptable = new TemporaryTableFragment(context, table.Keys, createtable.Table.Name); current.Join(temptable, table.Keys); delete.AddSource(temptable); block.Add(delete); } return(block); } }
/// <summary> /// 生成创建关系语句片段。 /// </summary> /// <param name="context">生成上下文。</param> /// <returns>语句片段。</returns> protected virtual SqlFragment GenerateForMaintenanceCreateRelation(GenerateContext context) { var operate = (DbCreateDropRelationOperate)context.Data.Operate; var name = context.ConvertName(operate.Name); var fragment = new CreateRelationFragment(context, name) { Foreign = new ObjectNameFragment(context, operate.Foreign.Name, operate.Foreign.Schema), Principal = new ObjectNameFragment(context, operate.Principal.Name, operate.Principal.Schema), ForeignKeys = operate.Pairs.Select(a => a.ForeignKey.Name).ToArray(), PrincipalKeys = operate.Pairs.Select(a => a.PrincipalKey.Name).ToArray() }; if (operate.Action != null) { var action = operate.Action; fragment.Update = action.Update; fragment.Delete = action.Delete; } return(fragment); }