public List <String> GetQuery(QueryDef q) { var e = TypeDict[q.EntityName].Entity; var Signature = InnerWriter.GetQuerySignature(q); var ManyName = (new QueryDef { EntityName = q.EntityName, Verb = q.Verb, Numeral = Numeral.CreateMany(), By = q.By, OrderBy = new List <KeyColumn> { } }).FriendlyName(); var AllName = (new QueryDef { EntityName = q.EntityName, Verb = q.Verb, Numeral = Numeral.CreateAll(), By = q.By, OrderBy = new List <KeyColumn> { } }).FriendlyName(); var Parameters = String.Join(", ", q.By.Select(c => "[[{0}]]".Formats(c)).ToArray()); var OrderBys = GetOrderBy(q, e.Name); List <String> Content; if (q.Verb.OnSelect || q.Verb.OnLock) { List <String> WhenEmpty; if (q.Numeral.OnOptional) { Content = GetTemplate("SelectLock_Optional").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters); WhenEmpty = GetTemplate("SelectLock_Optional_WhenEmpty").Substitute("EntityName", e.Name); } else if (q.Numeral.OnOne) { Content = GetTemplate("SelectLock_One").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters); WhenEmpty = GetTemplate("SelectLock_One_WhenEmpty").Substitute("EntityName", e.Name); } else if (q.Numeral.OnMany) { Content = GetTemplate("SelectLock_Many").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters).Substitute("OrderBys", OrderBys); WhenEmpty = GetTemplate("SelectLock_ManyRange_WhenEmpty").Substitute("EntityName", e.Name); } else if (q.Numeral.OnAll) { Content = GetTemplate("SelectLock_All").Substitute("AllName", AllName).Substitute("OrderBys", OrderBys); WhenEmpty = null; } else if (q.Numeral.OnRange) { if (q.By.Count == 0) { Content = GetTemplate("SelectLock_RangeAll").Substitute("AllName", AllName).Substitute("OrderBys", OrderBys); } else { Content = GetTemplate("SelectLock_Range").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters).Substitute("OrderBys", OrderBys); } WhenEmpty = GetTemplate("SelectLock_ManyRange_WhenEmpty").Substitute("EntityName", e.Name); } else if (q.Numeral.OnCount) { Content = GetTemplate("SelectLock_Count").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters); WhenEmpty = GetTemplate("SelectLock_Count_WhenEmpty").Substitute("EntityName", e.Name); } else { throw new InvalidOperationException(); } if (q.Numeral.OnAll) { Content = Content.Substitute("EntityName", e.Name); } else { var NondirectionalKeys = (new Key[] { e.PrimaryKey }).Concat(e.UniqueKeys).Concat(e.NonUniqueKeys).Select(kk => kk.Columns.Select(c => c.Name).ToList()).Distinct(new StringArrayComparer()).ToList(); var Key = q.By.ToList(); List <String> k = null; if (NondirectionalKeys.Contains(Key)) { k = Key; } else { foreach (var kk in NondirectionalKeys) { if (kk.Count >= Key.Count && kk.Take(Key.Count).SequenceEqual(Key)) { k = kk; break; } } if (k == null) { throw new InvalidOperationException(); } } var IndexName = e.Name + GetByIndex(k); var Fetches = new List <String>(); for (var n = 0; n < Key.Count; n += 1) { var ParentByIndex = GetByIndex(Key.Take(n)); var Column = GetEscapedIdentifier(k[n]); var ByIndex = GetByIndex(Key.Take(n + 1)); Fetches.AddRange(GetTemplate("SelectMany_Fetch").Substitute("EntityName", e.Name).Substitute("ParentByIndex", ParentByIndex).Substitute("Column", Column).Substitute("ByIndex", ByIndex).Substitute("WhenEmpty", WhenEmpty)); } var Filters = new List <String>(); for (var n = Key.Count; n < k.Count; n += 1) { Filters.Add(@">>select_many([](" + GetIndexType(e, k.Skip(n).ToList(), true) + @"::value_type _d_) { return from(*std::get<1>(_d_)); })"); } Content = Content.Substitute("EntityName", e.Name).Substitute("IndexName", IndexName).Substitute("Fetches", Fetches).Substitute("ByIndex", GetByIndex(Key)).Substitute("Filters", String.Join("", Filters.ToArray())); } } else if (q.Verb.OnInsert || q.Verb.OnUpdate || q.Verb.OnUpsert || q.Verb.OnDelete) { Content = GetTemplate("InsertUpdateUpsertDelete"); } else { throw new InvalidOperationException(); } return(GetTemplate("Query").Substitute("Signature", Signature).Substitute("Content", Content)); }
public List <String> GetQuery(QueryDef q) { var e = TypeDict[q.EntityName].Entity; var Signature = InnerWriter.GetQuerySignature(q); var ManyName = (new QueryDef { EntityName = q.EntityName, Verb = q.Verb, Numeral = Numeral.CreateMany(), By = q.By, OrderBy = new List <KeyColumn> { } }).FriendlyName(); var AllName = (new QueryDef { EntityName = q.EntityName, Verb = q.Verb, Numeral = Numeral.CreateAll(), By = q.By, OrderBy = new List <KeyColumn> { } }).FriendlyName(); var Parameters = String.Join(", ", q.By.Select(c => "[[{0}]]".Formats(c)).ToArray()); var OrderBys = GetOrderBy(q); List <String> Content; if (q.Verb.OnSelect || q.Verb.OnLock) { if (q.Numeral.OnOptional) { Content = GetTemplate("SelectLock_Optional").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters); } else if (q.Numeral.OnOne) { Content = GetTemplate("SelectLock_One").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters); } else if (q.Numeral.OnMany) { Content = GetTemplate("SelectLock_Many").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters).Substitute("OrderBys", OrderBys); } else if (q.Numeral.OnAll) { Content = GetTemplate("SelectLock_All").Substitute("AllName", AllName).Substitute("OrderBys", OrderBys); } else if (q.Numeral.OnRange) { if (q.By.Count == 0) { Content = GetTemplate("SelectLock_RangeAll").Substitute("AllName", AllName).Substitute("OrderBys", OrderBys); } else { Content = GetTemplate("SelectLock_Range").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters).Substitute("OrderBys", OrderBys); } } else if (q.Numeral.OnCount) { Content = GetTemplate("SelectLock_Count").Substitute("ManyName", ManyName).Substitute("Parameters", Parameters); } else { throw new InvalidOperationException(); } } else if (q.Verb.OnInsert || q.Verb.OnUpdate || q.Verb.OnUpsert || q.Verb.OnDelete) { Content = GetTemplate("InsertUpdateUpsertDelete"); } else { throw new InvalidOperationException(); } return(GetTemplate("Query").Substitute("Signature", Signature).Substitute("Content", Content)); }