public static MemberDeclarationSyntax[] GenerateMapping(SqModelMeta meta, SqModelTableRef tableRef) { if (!HasUpdater(tableRef)) { return(Array.Empty <MemberDeclarationSyntax>()); } if (meta.HasPk()) { return(new [] { MethodDeclarationSyntax(meta, tableRef, MethodNameGetMapping, null), MethodDeclarationSyntax(meta, tableRef, MethodNameGetUpdateKeyMapping, true), MethodDeclarationSyntax(meta, tableRef, MethodNameGetUpdateMapping, false) }); } else { return(new [] { MethodDeclarationSyntax(meta, tableRef, MethodNameGetMapping, null) }); }
static MemberDeclarationSyntax MethodDeclarationSyntax(SqModelMeta sqModelMeta, SqModelTableRef tableRef, string name, bool?pkFilter) { var setter = SyntaxFactory.IdentifierName("s"); ExpressionSyntax chain = setter; foreach (var metaProperty in sqModelMeta.Properties.Where(p => pkFilter.HasValue? p.IsPrimaryKey == pkFilter.Value : !p.IsIdentity)) { var col = setter.MemberAccess(nameof(IDataMapSetter <object, object> .Target)) .MemberAccess(metaProperty.Column.First(c => c.TableRef.Equals(tableRef)).ColumnName); ExpressionSyntax prop = setter.MemberAccess(nameof(IDataMapSetter <object, object> .Source)) .MemberAccess(metaProperty.Name); if (metaProperty.CastType != null) { prop = SyntaxFactory.CastExpression(SyntaxFactory.ParseTypeName(metaProperty.Type), prop); } chain = chain.MemberAccess("Set").Invoke(col, prop); } var methodDeclarationSyntax = SyntaxFactory .MethodDeclaration(SyntaxFactory.ParseTypeName(nameof(IRecordSetterNext)), name) .WithModifiers(Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword)) .AddParameterListParameters(FuncParameter("s", $"{nameof(IDataMapSetter<object, object>)}<{ExtractTableTypeName(sqModelMeta, tableRef)},{sqModelMeta.Name}>")) .WithBody(SyntaxFactory.Block(SyntaxFactory.ReturnStatement(chain))); return(methodDeclarationSyntax); }