Пример #1
0
        GetCompletionResponse IProvider.GetCompletion(ConnectionBase connection, IDbTransaction transaction, string text, int position)
        {
            var response = new GetCompletionResponse();

            string[] values       = null;
            var      sqlStatement = new SqlParser(text);
            var      tokens       = sqlStatement.Tokens;

            if (tokens.Count > 0)
            {
                sqlStatement.FindToken(position, out var previousToken, out var currentToken);
                if (currentToken != null)
                {
                    response.StartPosition = currentToken.StartPosition;
                    response.Length        = currentToken.EndPosition - currentToken.StartPosition + 1;
                }
                else
                {
                    response.StartPosition = position;
                    response.Length        = 0;
                }

                if (previousToken != null)
                {
                    var token = previousToken;

                    if (token.Type == TokenType.KeyWord && string.Compare(token.Value, "exec", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        var names = new List <string>();

                        foreach (var name in TfsDataReaderFactory.Dictionary.Keys)
                        {
                            names.Add(name);
                        }

                        values = names.ToArray();
                    }
                    else if (tokens.Count > 1)
                    {
                        token = tokens[0];

                        if (token.Type == TokenType.KeyWord && string.Compare(token.Value, "exec", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            //token = tokens[1];
                            //TfsDataReaderFactory.DataReaderInfo info;
                            //bool contains = TfsDataReaderFactory.Dictionary.TryGetValue(token.Value, out info);

                            //if (contains)
                            //{
                            //    TfsParameterCollection parameters = info.Parameters;
                            //    TfsParameter parameter = parameters[index - 2];
                            //    Type type = parameter.Type;

                            //    if (type.IsEnum)
                            //    {
                            //        string[] names = Enum.GetNames(type);
                            //        values = names;
                            //    }
                            //}
                            var command  = sqlStatement.CreateCommand(this, connection, CommandType.StoredProcedure, 0);
                            var contains = TfsDataReaderFactory.Dictionary.TryGetValue(command.CommandText, out var info);
                            if (contains)
                            {
                                var parameters     = info.Parameters;
                                var parameterIndex = previousToken.Index / 2;
                                if (parameterIndex < parameters.Count)
                                {
                                    var parameter = parameters[parameterIndex];
                                    var type      = parameter.Type;
                                    if (type.IsEnum)
                                    {
                                        var names = Enum.GetNames(type);
                                        values = names;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            response.Items = values.Select(value => (IObjectName) new ObjectName(value)).ToList();
            return(response);
        }