public override StoredProcedureResult Generate() { var delim = "," + Environment.NewLine; string Schema = Atom.AdditionalInfo.Schema, Params = string.Empty, SprocSuffix = "GetAll", TableName = Atom.Name; var plan = new QueryPlan { QueryMembers = Atom.Members.Where(IsQueryableColumn).Select(AliasedAtomMemberInfo.FromAtomMemberInfo), References = { new SimpleReference(Atom) } }; var template = GenerateQuerySproc(Schema, TableName, SprocSuffix, Params, plan); var name = $"{Schema}.{TableName}_GetAll"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = QueryType.GetAll, QueryKey = null, Name = name } }); }
private IEnumerable <string> GetMembers(ProjectedAtomRoot atom) { var members = atom.Members.Where(mem => !mem.Member.HasFlag(MemberFlags.Hidden)); foreach (var member in members) { string name = new CSharpMemberNameFinder(member).MemberName(); string type = GetMemberType(member.Member); if (member.Member.HasFlag(MemberFlags.TemporalTracking)) { yield return($"public {type} {name} {{ get; private set; }}"); } else { yield return($"public {type} {name} {{ get; set; }}"); } if (member.Member.HasReference && member.Member.Reference.TargetMember.Atom.IsLookup) { yield return($"private {type} {name}Id {{ set {{ {name} = value; }} }}"); } } }
private IEnumerable<string> GetMembers(ProjectedAtomRoot atom) { var members = atom.Members.Where(mem => !mem.Member.HasFlag(MemberFlags.Hidden)); foreach (var member in members) { string name = new CSharpMemberNameFinder(member).MemberName(); string type = GetMemberType(member.Member); if (member.Member.HasFlag(MemberFlags.TemporalTracking)) { yield return $"public {type} {name} {{ get; private set; }}"; } else { yield return $"public {type} {name} {{ get; set; }}"; } if (member.Member.HasReference && member.Member.Reference.TargetMember.Atom.IsLookup) { yield return $"private {type} {name}Id {{ set {{ {name} = value; }} }}"; } } }
public override StoredProcedureResult Generate() { var delim = "," + Environment.NewLine; var queryKey = _byKey; if (_byKey.HasReference && _byKey.Reference.IsReferenceToHiddenPrimaryKey) { queryKey = queryKey.Reference.TargetAtomAlternateKey; } string sprocSuffix = _isUnique ? $"GetOneBy{queryKey.Name}" : $"GetBy{queryKey.Name}"; string Schema = Atom.AdditionalInfo.Schema, Params = GetTypedSprocParam(queryKey), TableName = Atom.Name, Key = queryKey.Name; var plan = new QueryPlan { QueryMembers = Atom.Members.Where(IsQueryableColumn).Select(AliasedAtomMemberInfo.FromAtomMemberInfo), References = { new SimpleReference(Atom) }, Filters = { new AtomFilter { FilterValue = "@" + Key, Member = queryKey } } }; if (queryKey != _byKey) { var simpleRef = new SimpleReference(queryKey.Atom); plan.References = new List <Reference> { simpleRef, new ResolvedReference(Atom, new List <Reference> { simpleRef }) }; } var template = GenerateQuerySproc(Schema, TableName, sprocSuffix, Params, plan); var name = $"{Schema}.{TableName}_{sprocSuffix}"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = _isUnique ? QueryType.GetOne : QueryType.GetBy, QueryKey = queryKey, Name = name } }); }
public static string GetClassName(ProjectedAtomRoot atom) { if (atom.BasedOn != null && atom.BasedOn.AdditionalInfo.Schema != Constants.DefaultSchema) { return atom.Name + "Record"; } return atom.Name + "Record"; }
private string GenerateClass(ProjectedAtomRoot atom) { return($@" [Serializable] public partial class {GetClassName(atom)} {{ {string.Join(Environment.NewLine, GetMembers(atom).IndentAllLines(1)).Trim()} }}"); }
public static string GetClassName(ProjectedAtomRoot atom) { if (atom.BasedOn != null && atom.BasedOn.AdditionalInfo.Schema != Constants.DefaultSchema) { return(atom.Name + "Record"); } return(atom.Name + "Record"); }
private string GenerateClass(ProjectedAtomRoot atom) { return $@" [Serializable] public partial class {GetClassName(atom)} {{ {string.Join(Environment.NewLine, GetMembers(atom).IndentAllLines(1)).Trim()} }}"; }
public override StoredProcedureResult Generate() { var delim = "," + Environment.NewLine; var inputMembers = Atom.Members.Where(i => !i.HasFlag(MemberFlags.NotUpdateable) || IsKey(i)); string Schema = Atom.AdditionalInfo.Schema, Params = string.Join( delim, inputMembers.Select(GetTypedSprocParam) .IndentAllLines(1, true)), SprocSuffix = "Update", TableName = Atom.Name, Key = GetReturnKey() .Name, SetList = string.Join( delim, Atom.Members.Select(GetUpdateValues) .Where(i => i != null) .IndentAllLines(3, true)), Temporal = GenerateTemporalNowVariable(Atom.AdditionalInfo.Temporal), Lookups = GetLookupSql(inputMembers.Where(HasHiddenReference)); var template = SprocHeader(Schema, TableName, SprocSuffix, Params) + $@" {Temporal} {Lookups} UPDATE [{Schema}].{TableName} SET {SetList} WHERE {Key} = @{Key} END GO "; var name = $"{Schema}.{TableName}_{SprocSuffix}"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { ReturnPrimitive = typeof(int), InputParams = inputMembers, BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = QueryType.Update, Name = name } }); }
protected AtomMemberInfo FindMainKey(ProjectedAtomRoot returnType) { if (returnType.BasedOn == null) { throw new Exception("Cannot generate sproc on non atom (is this a projection?)"); } if (returnType.BasedOn.AdditionalInfo.ShouldHidePrimaryKey() && !returnType.BasedOn.IsLookup) { return(returnType.BasedOn.Members.FirstOrDefault(i => i.IsAltKey)); } return(returnType.BasedOn.Members.FirstOrDefault(i => i.IsPrimary)); }
protected AtomMemberInfo FindMainKey(ProjectedAtomRoot returnType) { if (returnType.BasedOn == null) { throw new Exception("Cannot generate sproc on non atom (is this a projection?)"); } if (returnType.BasedOn.AdditionalInfo.ShouldHidePrimaryKey() && !returnType.BasedOn.IsLookup) { return returnType.BasedOn.Members.FirstOrDefault(i => i.IsAltKey); } return returnType.BasedOn.Members.FirstOrDefault(i => i.IsPrimary); }
private string EnumDefinition(ProjectedAtomRoot arg) { var members = arg.BasedOn.Lookup.Values.Select((value, idx) => GetEnumDefinitionType(value, idx, arg.BasedOn.Lookup.NamePrefix)); string enumMembers = string.Join("," + Environment.NewLine, members.IndentAllLines(1)); var type = new CSharpDefaultTypeFinder(optional: false).Visit(arg.BasedOn.Members.First().MemberType); var template = $@" [Serializable, DataContract] public enum {arg.Name} : {type} {{ {enumMembers} }} "; return(template); }
public override StoredProcedureResult Generate() { var queryKey = _byKey ?? GetReturnKey(); string Schema = Atom.AdditionalInfo.Schema, Params = "@LookupKeys [" + GetTableType(queryKey) + "] READONLY", LookupColumnName = GetLookupColumnName(queryKey), SprocSuffix = "GetMany", Description = "Batch Gets " + Atom.Name + " by " + queryKey.Name, TableName = Atom.Name, Key = queryKey.Name, Fields = string.Join("," + Environment.NewLine, Atom.Members.Where(IsListableField).Select(a => a.Name).IndentAllLines(3)), Hint = Atom.AdditionalInfo.UseWithNoLockHint.GetValueOrDefault(true) ? " with (NOLOCK)" : String.Empty; var template = SprocHeader(Schema, TableName, SprocSuffix, Params) + $@" SELECT {Fields} FROM [{Schema}].{TableName}{Hint} INNER JOIN @LookupKeys lk ON lk.{LookupColumnName} = [{Schema}].{TableName}.{Key} END GO "; var name = $"{Schema}.{TableName}_GetMany"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = QueryType.BatchList, QueryKey = queryKey, Name = name } }); }
public override StoredProcedureResult Generate() { var delim = "," + Environment.NewLine; var queryKey = GetReturnKey(); string Schema = Atom.AdditionalInfo.Schema, Params = GetTypedSprocParam(queryKey), SprocSuffix = "GetOne", TableName = Atom.Name, Key = queryKey.Name; var plan = new QueryPlan { QueryMembers = Atom.Members.Where(IsQueryableColumn).Select(AliasedAtomMemberInfo.FromAtomMemberInfo), References = { new SimpleReference(Atom) }, Filters = { new AtomFilter { FilterValue = "@" + Key, Member = queryKey } } }; var template = GenerateQuerySproc(Schema, TableName, SprocSuffix, Params, plan); var name = $"{Schema}.{TableName}_GetOne"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = QueryType.GetOne, QueryKey = queryKey, Name = name } }); }
public override StoredProcedureResult Generate() { var fields = new Dictionary <string, List <string> > { { Atom.Name, Atom.Members.Select(i => i.Name) .ToList() }, { _searchBy.Name, _searchBy.TargetMember.Atom.Members.Select(i => i.Name) .ToList() } }; var projectionAtom = new ProjectionAtom(fields); var builder = new QueryPlanBuilder(projectionAtom, _allAtoms.ToDictionary(i => i.Name)); var plan = builder.Build(); plan.QueryMembers = plan.QueryMembers.Where(i => i.Member.Atom.Name != _searchBy.Name && !i.Member.HasFlag(MemberFlags.Generated)) .ToList(); var queryPlan = new QuerySqlGenerator(plan, 2).Generate() .IndentAllLines(2, true); var sprocSuffix = $"ListBy{_searchBy.Member}"; string Params = GetTypedSprocParam(_searchBy.TargetMember), schema = Atom.AdditionalInfo.Schema, Key = _searchBy.Member, ExternalTable = _searchBy.Name, ExternalKey = _searchBy.Member, TableName = Atom.Name, SprocSuffix = sprocSuffix; var template = SprocHeader(schema, TableName, SprocSuffix, Params) + $@" {queryPlan} WHERE [{schema}].[{ExternalTable}].{Key} = @{Key} END GO "; var name = $"{schema}.{TableName}_ListBy{ExternalKey}"; var result = new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryKey = _searchBy.TargetMember, QueryType = QueryType.GetOne, Name = name } }; return(result); }
public override StoredProcedureResult Generate() { var delim = "," + Environment.NewLine; var availableFields = Atom.Members.Where(i => !i.IsPrimary) .ToList(); var inputMembers = availableFields.Where(m => MemberIsUseableAsInsertParameter(m) && !m.HasFlag(MemberFlags.SoftDeleteTracking)); var fields = availableFields.Select(i => i.Name) .ToList(); var fieldValues = availableFields.Select(GetInsertFields); string Schema = Atom.AdditionalInfo.Schema, Params = string.Join( delim, inputMembers.Select(GetTypedSprocParam) .IndentAllLines(1, true)), SprocSuffix = "Upsert", TableName = Atom.Name, Key = GetReturnKey().Name, Fields = string.Join(delim, fields.IndentAllLines(4, ignoreFirst: true)), Values = string.Join(delim, fieldValues.IndentAllLines(4, ignoreFirst: true)), SetList = string.Join( delim, availableFields.Select(GetUpdateValues) .Where(i => i != null) .IndentAllLines(4, true)), Temporal = GenerateTemporalNowVariable(Atom.AdditionalInfo.Temporal), Lookups = GetLookupSql(availableFields.Where(HasHiddenReference)); var template = SprocHeader(Schema, TableName, SprocSuffix, Params) + $@" {Temporal} {Lookups} MERGE INTO [{Schema}].[{TableName}] AS [dest] USING (VALUES ({string.Join(", ", inputMembers.Select(p => "@" + p.Name))})) AS [source] ({string.Join(", ", inputMembers.Select(p => p.Name))}) ON ({string.Join(" AND ", _onMembers.Select(m => $"[source].{m.Name} = [dest].{m.Name}"))}) WHEN MATCHED THEN UPDATE SET {SetList} WHEN NOT MATCHED BY TARGET THEN INSERT ( {Fields} ) VALUES ( {Values} ) OUTPUT inserted.{Key}; END GO "; var name = $"{Schema}.{TableName}_{SprocSuffix}"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { ReturnPrimitive = typeof(int), InputParams = inputMembers, BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = QueryType.Upsert, Name = name } }); }
public override StoredProcedureResult Generate() { var availableFields = Atom.Members.Where(i => !i.IsPrimary && !i.HasFlag(MemberFlags.SoftDeleteTracking)) .ToList(); var fields = availableFields.Select(i => i.Name) .ToList(); var fieldValues = availableFields.Select(GetInsertFields); var delim = "," + Environment.NewLine; var @params = availableFields.Where(MemberIsUseableAsInsertParameter).ToList(); string Schema = Atom.AdditionalInfo.Schema, Params = string.Join(delim, @params.Select(GetTypedSprocParam).IndentAllLines(1, true)), SprocSuffix = "Insert", TableName = Atom.Name, Fields = string.Join(delim, fields.IndentAllLines(2, ignoreFirst: true)), Values = string.Join(delim, fieldValues.IndentAllLines(2, ignoreFirst: true)), ReturnKey = GetReturnKey().Name, PrimaryKey = GetPrimaryKey().Name, Lookups = GetLookupSql(availableFields.Where(HasHiddenReference)), Temporal = GenerateTemporalNowVariable(Atom.AdditionalInfo.Temporal); var Select = Atom.AdditionalInfo.SelectAfterInsert.GetValueOrDefault(true) ? $@" SELECT {ReturnKey} FROM [{Schema}].{TableName} WHERE {PrimaryKey} = SCOPE_IDENTITY(); " : String.Empty; var template = SprocHeader(Schema, TableName, SprocSuffix, Params) + $@" {Temporal} {Lookups} INSERT INTO [{Schema}].{TableName} ( {Fields} ) VALUES ( {Values} ); {Select} END GO "; var name = $"{Schema}.{TableName}_Insert"; return(new StoredProcedureResult { Name = name, Sql = template, AccessorMetadata = new SqlAccessorMetadata { ReturnPrimitive = typeof(int), InputParams = @params, BaseAtom = ProjectedAtomRoot.FromAtom(Atom), QueryType = QueryType.Insert, Name = name } }); }
private string GetPrimitiveNumericStrongType(string primitiveTypeName) { if (_member.HasReference && _member.Reference.TargetMember.Atom.IsLookup) { return(EnumDefinition(ProjectedAtomRoot.FromAtom(_member.Reference.TargetMember.Atom))); } string TypeName = StringExt.ToTitleCase(_member.Name); string lookupTemplateExtension = string.Empty; if (_member.Atom.IsLookup) { string LookupName = StringExt.ToTitleCase(_member.Atom.Name); lookupTemplateExtension = $@" public static implicit operator {LookupName}({TypeName} id) {{ return ({LookupName}) ({primitiveTypeName}) (id); }} public static implicit operator {TypeName}({LookupName} value) {{ return ({TypeName})({primitiveTypeName})(value); }}"; } var upperCasedPrimitiveTypeName = char.ToUpper(primitiveTypeName[0]) + primitiveTypeName.Substring(1); var template = $@" [Serializable] [JsonConverter(typeof({upperCasedPrimitiveTypeName}StrongTypeConverter<{TypeName}>))] public partial struct {TypeName} : ITyped{upperCasedPrimitiveTypeName}, IStrongTypeFactory<{primitiveTypeName}, {TypeName}>, IEquatable<{TypeName}> {{ private readonly {primitiveTypeName} _{primitiveTypeName}; private {TypeName}({primitiveTypeName} @{primitiveTypeName}) {{ _{primitiveTypeName} = @{primitiveTypeName}; }} public static explicit operator {TypeName}({primitiveTypeName} value) {{ return new {TypeName}(value); }} public static implicit operator {primitiveTypeName}({TypeName} value) {{ return value._{primitiveTypeName}; }} public {primitiveTypeName} UnderlyingValue() {{ return _{primitiveTypeName}; }} public {primitiveTypeName} Value {{ get {{ return _{primitiveTypeName}; }} }} {TypeName} IStrongTypeFactory<{primitiveTypeName}, {TypeName}>.NewWithValue({primitiveTypeName} value) {{ return new {TypeName}(value); }} public TResult Accept<TResult>(IStrongTypeVistor<TResult> visitor) {{ return visitor.Visit(this); }} public override string ToString() {{ return _{primitiveTypeName}.ToString(); }} public string ToString(string format) {{ return _{primitiveTypeName}.ToString(format); }} public string ToString(string format, IFormatProvider provider) {{ return _{primitiveTypeName}.ToString(format, provider); }} public string ToString(IFormatProvider provider) {{ return _{primitiveTypeName}.ToString(provider); }} public override bool Equals(object other) {{ if (other is {TypeName}) {{ return Equals(({TypeName})other); }} return false; }} public override int GetHashCode() {{ return _{primitiveTypeName}.GetHashCode(); }} public bool Equals({TypeName} other) {{ return _{primitiveTypeName}.Equals(other._{primitiveTypeName}); }} public static bool operator ==({TypeName} left, {TypeName} right) {{ return left.Equals(right); }} public static bool operator !=({TypeName} left, {TypeName} right) {{ return !left.Equals(right); }} {lookupTemplateExtension} }}"; return(template); }