/// <summary> /// Downloads the specified file. /// </summary> /// <param name="request">The file to download.</param> /// <param name="async">if set to <c>true</c>, download asynchronously. Otherwise, /// a resolved promise will be returned</param> /// <returns>A promise to return a stream of the file's bytes</returns> public IPromise <Stream> Download(Command request, bool async) { var parsedAml = _conn.AmlContext.FromXml(request); var file = (IReadOnlyItem)parsedAml.AssertItem("File"); var hasVaults = file.Relationships("Located") .Select(r => r.RelatedId().AsItem()) .Any(i => i.Exists); var filePromise = hasVaults ? Promises.Resolved(file) : _conn.ItemByQuery(new Command(DownloadFileAmlFormat, file.Id()), async); // Get the file data and user data as necessary return(Promises .All(filePromise, _vaultStrategy.ReadPriority(async)) .Continue(o => { // Get the correct vault for the file var vault = GetReadVaultForFile(o.Result1, o.Result2); if (vault == null) { throw new InvalidOperationException("Vault location of the file is unknown"); } // Download the file return DownloadFileFromVault(o.Result1, vault, async, request); }) .Convert(r => r.AsStream)); }
private async Task <IEnumerable <ICompletionData> > Columns(SqlTableInfo info, bool includeAlias = true) { var result = new List <ICompletionData>(); var alias = info.Alias; if (this._tableNameColumnPrefix && info.Name != null) { alias = "[" + info.Name.Name + "]"; } if (!includeAlias) { alias = null; } if (info.Columns == null && info.AdditionalColumns == null) { var allProps = await _provider.GetColumnNames(info.Name.FullName).ToTask(); result.AddRange(new PropertyCompletionFactory().GetCompletions(allProps, alias)); } else { if (info.Columns != null) { result.AddRange(info.Columns .Select(c => ColumnCompletion(c, alias))); } if (info.AdditionalColumns != null) { var properties = (await Promises.All(info.AdditionalColumns .Select(i => _provider.GetColumnNames(i.Name.FullName)).ToArray()) .ToTask()).OfType <IEnumerable <IListValue> >(); var allProps = properties.SelectMany(p => p); result.AddRange(new PropertyCompletionFactory().GetCompletions(allProps, alias)); } } return(result); }
public IPromise <CompletionContext> Completions(string prefix, ITextSource all, int caret, string termCharacter , bool tableNameColumnPrefix = false) { try { _tableNameColumnPrefix = tableNameColumnPrefix; var lastIndex = string.IsNullOrEmpty(termCharacter) ? -1 : all.IndexOf(termCharacter, caret, all.TextLength - caret, StringComparison.Ordinal); var sql = prefix + (lastIndex < 0 ? all.GetText(caret, all.TextLength - caret) : all.GetText(caret, lastIndex - caret)); if (sql.StartsWith("(") && sql.EndsWith(")")) { sql = sql.Substring(1, sql.Length - 2); } var parseTree = new SqlTokenizer(sql).Parse(); if (!parseTree.Any()) { return(Promises.Resolved(new CompletionContext())); } var currNode = parseTree.NodeByOffset(prefix.Length); var literal = currNode as SqlLiteral; if (literal != null) { var parGroup = literal.Parent as SqlGroup; if (_overloadWin != null && (parGroup == null || !parGroup.First().TextEquals(_overloadName))) { _overloadWin.Close(); } if (SqlTokenizer.KeywordPrecedesTable(literal)) { var context = new SqlContext(parGroup); return(ContextFromData(Tables(null).Concat(Schemas()) .Concat(context.Definitions.Select(d => new SqlGeneralCompletionData() { Text = d, Description = "Locally defined table", Image = Icons.Class16.Wpf })) .OrderBy(i => i.Text))); } else if (literal.Text == "(") { var prev = literal.PreviousLiteral(); if (prev != null) { if (CurrentTextArea != null) { var overloads = from f in _coreFunctions where string.Equals(f.Name, prev.Text, StringComparison.OrdinalIgnoreCase) select new Overload(f.Usage, f.Description); if (overloads.Any()) { _overloadWin = new OverloadInsightWindow(CurrentTextArea); _overloadWin.StartOffset = caret; _overloadWin.EndOffset = caret + 1; _overloadWin.Provider = new OverloadList().AddRange(overloads); _overloadWin.Show(); _overloadWin.Closed += (s, e) => { _overloadWin = null; _overloadName = null; }; _overloadName = prev.Text; } } switch (prev.Text.ToUpperInvariant()) { case "DATEADD": case "DATEDIFF": case "DATEDIFF_BIG": case "DATEFROMPARTS": case "DATENAME": case "DATEPART": return(ContextFromData(_datePartNames.Select(n => new SqlGeneralCompletionData() { Text = n[0] + (n[1] == n[0] ? "" : " (" + n[1] + ")"), Description = n[1], Image = Icons.EnumValue16.Wpf, Action = () => n[0] }) .OrderBy(i => i.Text))); } } } else if (literal.Text == ".") { var name = literal.Parent as SqlName; if (name != null) { if (name.IsTable) { var idx = name.IndexOf(literal); var schema = name[idx - 1].Text; if (_provider.GetSchemaNames().Contains(schema, StringComparer.OrdinalIgnoreCase)) { return(ContextFromData(Tables(schema).Concat(Functions(true, schema)) .OrderBy(i => i.Text))); } } else { var group = name.Parent as SqlGroup; if (group != null) { var idx = name.IndexOf(literal); var context = new SqlContext(group); SqlTableInfo info; if (idx > 0 && name[idx - 1] is SqlLiteral && context.TryByName(((SqlLiteral)name[idx - 1]).Text.ToLowerInvariant(), out info)) { return(Columns(info, false).ToPromise().Convert(c => new CompletionContext() { Items = c })); } } } } } else if (literal.Type == SqlType.Keyword || literal.Type == SqlType.Operator) { switch (literal.Text.ToLowerInvariant()) { case "union": return(ContextFromData(MatchCase(literal.Text, "all", "select") .GetCompletions <SqlGeneralCompletionData>())); case "group": case "order": case "partition": return(ContextFromData(MatchCase(literal.Text, "by") .GetCompletions <SqlGeneralCompletionData>())); case "insert": return(ContextFromData(MatchCase(literal.Text, "into") .GetCompletions <SqlGeneralCompletionData>())); case "delete": return(ContextFromData(MatchCase(literal.Text, "from") .GetCompletions <SqlGeneralCompletionData>())); } var group = literal.Parent as SqlGroup; if (group != null) { // List of sql specific constructs for the context var sqlOthers = new List <string>(); switch (literal.Text.ToLowerInvariant()) { case "select": sqlOthers.Add("*"); sqlOthers.Add("distinct"); sqlOthers.Add("top"); break; } // Table aliases and functions var context = new SqlContext(group); var others = context.Tables .Where(t => !string.IsNullOrEmpty(t.Alias)) .Select(t => t.Alias) .Distinct() .Select(t => new SqlGeneralCompletionData() { Text = t, Image = Icons.Class16.Wpf }) .Concat(Functions(false, null)); // Table columns return(Promises.All(context.Tables.Select(t => Columns(t).ToPromise()).ToArray()) .Convert(l => new CompletionContext() { Items = l.OfType <IEnumerable <ICompletionData> >() .SelectMany(p => p) .Concat(MatchCase(literal.Text, sqlOthers).Select(o => new SqlGeneralCompletionData() { Text = o, Image = Icons.Operator16.Wpf })) .Concat(others) .Concat(Schemas()) .OrderBy(i => i.Text, StringComparer.OrdinalIgnoreCase) })); } } } return(Promises.Resolved(new CompletionContext())); } catch (Exception ex) { return(Promises.Rejected <CompletionContext>(ex)); } }