/// <summary> /// Builds a main query to return an Any or All result /// </summary> /// <returns>Query string</returns> private string BuildMainAnyAllQuery() { var sb = new StringBuilder(); sb.AppendFormat("SELECT {0} as result", QueryType == N1QlQueryType.MainQueryAny ? "true" : "false"); if (FromParts.Any()) { var mainFrom = FromParts.First(); sb.AppendFormat(" FROM {0} as {1}", mainFrom.Source, mainFrom.ItemName); if (!string.IsNullOrEmpty(UseKeysPart)) { sb.AppendFormat(" USE KEYS {0}", UseKeysPart); } foreach (var joinPart in FromParts.Skip(1)) { sb.AppendFormat(" {0} {1} as {2}", joinPart.JoinType, joinPart.Source, joinPart.ItemName); if (!string.IsNullOrEmpty(joinPart.OnKeys)) { sb.AppendFormat(" ON KEYS {0}", joinPart.OnKeys); } } } ApplyLetParts(sb); bool hasWhereClause = false; if (WhereParts.Any()) { sb.AppendFormat(" WHERE {0}", String.Join(" AND ", WhereParts)); hasWhereClause = true; } if (QueryType == N1QlQueryType.MainQueryAll) { sb.AppendFormat(" {0} NOT ({1})", hasWhereClause ? "AND" : "WHERE", WhereAllPart); } sb.Append(" LIMIT 1"); return(sb.ToString()); }
/// <summary> /// Builds a primary select query /// </summary> /// <returns>Query string</returns> private string BuildSelectQuery() { var sb = new StringBuilder(); if (QueryType == N1QlQueryType.Subquery) { if (!string.IsNullOrEmpty(PropertyExtractionPart)) { // Subqueries will always return a list of objects // But we need to use an ARRAY statement to convert it into an array of a particular property of that object sb.AppendFormat("ARRAY `ArrayExtent`.{0} FOR `ArrayExtent` IN (", PropertyExtractionPart); } else { sb.Append('('); } } else if (QueryType == N1QlQueryType.SubqueryAny) { sb.AppendFormat("ANY {0} IN (", PropertyExtractionPart); } else if (QueryType == N1QlQueryType.SubqueryAll) { sb.AppendFormat("EVERY {0} IN (", PropertyExtractionPart); } if (!string.IsNullOrWhiteSpace(ExplainPart)) { sb.Append(ExplainPart); } if (!string.IsNullOrEmpty(AggregateFunction)) { sb.AppendFormat("SELECT {0}({1}{2})", AggregateFunction, !string.IsNullOrWhiteSpace(DistinctPart) ? DistinctPart : string.Empty, SelectPart); } else { sb.AppendFormat("SELECT {0}{1}", !string.IsNullOrWhiteSpace(DistinctPart) ? DistinctPart : string.Empty, SelectPart); //TODO support multiple select parts: http://localhost:8093/tutorial/content/#5 } if (!IsBucketSubquery && !string.IsNullOrEmpty(PropertyExtractionPart)) { sb.AppendFormat(" as {0}", PropertyExtractionPart); } if (FromParts.Any()) { var mainFrom = FromParts.First(); sb.AppendFormat(" FROM {0} as {1}", mainFrom.Source, mainFrom.ItemName); if (!string.IsNullOrEmpty(UseKeysPart)) { sb.AppendFormat(" USE KEYS {0}", UseKeysPart); } foreach (var joinPart in FromParts.Skip(1)) { sb.AppendFormat(" {0} {1} as {2}", joinPart.JoinType, joinPart.Source, joinPart.ItemName); if (!string.IsNullOrEmpty(joinPart.OnKeys)) { sb.AppendFormat(" ON KEYS {0}", joinPart.OnKeys); } } } ApplyLetParts(sb); if (WhereParts.Any()) { sb.AppendFormat(" WHERE {0}", String.Join(" AND ", WhereParts)); } if ((GroupByParts != null) && GroupByParts.Any()) { sb.AppendFormat(" GROUP BY {0}", string.Join(", ", GroupByParts)); } if ((HavingParts != null) && HavingParts.Any()) { sb.AppendFormat(" HAVING {0}", string.Join(" AND ", HavingParts)); } if (OrderByParts.Any()) { sb.AppendFormat(" ORDER BY {0}", String.Join(", ", OrderByParts)); } if (LimitPart != null) { sb.Append(LimitPart); } if (LimitPart != null && OffsetPart != null) { sb.Append(OffsetPart); } if (QueryType == N1QlQueryType.Subquery) { if (!string.IsNullOrEmpty(PropertyExtractionPart)) { sb.Append(") END"); } else { sb.Append(')'); } } else if (QueryType == N1QlQueryType.SubqueryAny) { sb.Append(") SATISFIES true END"); } else if (QueryType == N1QlQueryType.SubqueryAll) { sb.AppendFormat(") SATISFIES {0} END", WhereAllPart); } return(sb.ToString()); }