protected virtual string TranslateQuery([NotNull] Opcode opcode, [NotNull] Database database, [NotNull] ParametersList parameters) { Assert.ArgumentNotNull(opcode, nameof(opcode)); Assert.ArgumentNotNull(database, nameof(database)); Assert.ArgumentNotNull(parameters, nameof(parameters)); var step = opcode as Step; if (step == null || !(step is Root)) { return(null); } step = step.NextStep; var sql = string.Empty; while (step != null) { if (!(step is Children) && !(step is Descendant) && !(step is Ancestor) && !(step is Parent)) { throw new Exception("Can navigate only child, parent, descendant, and ancestor axes."); } ITranslationContext context = new BasicTranslationContext(_factory, _api, database, parameters); var predicate = GetPredicate(step); var name = GetName(step); // Dirty hack to overcome problems with numbers in Query if (name.StartsWith("_.")) { name = name.Substring(2); } Opcode expression = null; if (predicate != null) { expression = predicate.Expression; } var where = string.Empty; if (expression != null) { where = context.Factory.GetTranslator(expression).Translate(expression, context); } var builder = new StringBuilder(); AddInitialStatement(builder); AddFieldJoins(context, builder); if (!string.IsNullOrEmpty(sql)) { AddNestedQuery(step, sql, builder); } AddExtraJoins(context, builder); // Add WHERE statement where = where.Trim(); var whereAppended = false; if (where.Length > 0) { whereAppended = AddConditionJoint(false, builder); builder.Append(where); } // Add Name filter if (name.Length > 0 && name != "*") { whereAppended = AddConditionJoint(whereAppended, builder); AddNameFilter(name, builder); } if (step is Children && string.IsNullOrEmpty(sql)) { AddConditionJoint(whereAppended, builder); AddRootItemFilter(parameters, builder); } sql = builder.ToString(); step = step.NextStep; } return(sql); }
public virtual string TranslateQuery(Opcode opcode, Database database, ParametersList parameters) { var step = opcode as Step; if (!(step is Root)) { return(null); } step = step.NextStep; var sql = string.Empty; while (step != null) { if (!(step is Children) && !(step is Descendant) && !(step is Ancestor) && !(step is Parent)) { throw new Exception("Can navigate only child, parent, descendant, and ancestor axes."); } var context = new BasicTranslationContext(_factory, _api, database, parameters); ((BasicTranslatorFactory)context.Factory).Register(typeof(LinqToSitecoreFunction), new LinqToSitecoreFunctionTranslator()); var predicate = GetPredicate(step); var name = GetName(step); if (name.StartsWith("_.")) { name = name.Substring(2); } Opcode expression = null; if (predicate != null) { expression = predicate.Expression; } var where = string.Empty; if (expression != null) { where = context.Factory.GetTranslator(expression).Translate(expression, context); } var builder = new StringBuilder(); AddInitialStatement(builder); AddFieldJoins(context, builder); if (!string.IsNullOrEmpty(sql)) { AddNestedQuery(step, sql, builder); } AddExtraJoins(context, builder); where = where.Trim(); var whereAppended = false; if (where.Length > 0) { whereAppended = AddConditionJoint(false, builder); builder.Append(where); } if (name.Length > 0 && name != "*") { whereAppended = AddConditionJoint(whereAppended, builder); AddNameFilter(name, builder); } if (step is Children && string.IsNullOrEmpty(sql)) { AddConditionJoint(whereAppended, builder); AddRootItemFilter(parameters, builder); } sql = builder.ToString(); step = step.NextStep; } return(NormalizeQuery(sql)); }