Exemplo n.º 1
0
        public IDbCommand CreateCommand(IProvider provider, ConnectionBase connection, CommandType commandType, int commandTimeout)
        {
            var command = connection.CreateCommand();

            command.CommandType    = commandType;
            command.CommandTimeout = commandTimeout;
            var commandType2 = commandType;

            if (Tokens.Count > 0)
            {
                var firstToken      = Tokens[0];
                var startTokenIndex = 0;
                var isVbScript      = false;

                if (firstToken.Type == TokenType.KeyWord)
                {
                    var keyWord = firstToken.Value.ToLower();

                    switch (keyWord)
                    {
                    case "exec":
                        commandType2    = CommandType.StoredProcedure;
                        startTokenIndex = 1;
                        break;

                    case "load":
                        commandType2 = CommandType.Text;
                        break;

                    case "main":
                        commandType2 = CommandType.StoredProcedure;
                        isVbScript   = true;
                        break;

                    case "select":
                        commandType2 = CommandType.Text;
                        break;

                    default:
                        break;
                    }
                }

                command.CommandType = commandType2;

                switch (commandType2)
                {
                case CommandType.Text:
                    command.CommandText = _text;
                    break;

                default:
                    if (isVbScript)
                    {
                        //string commandText = query.Substring(firstLine.Length);
                        //command.CommandText = commandText;
                    }
                    else
                    {
                        command.CommandText = Tokens[startTokenIndex].Value;
                    }

                    startTokenIndex++;
                    provider.DeriveParameters(command);

                    var tokens          = Tokens.GetRange(startTokenIndex, Tokens.Count - startTokenIndex);
                    var parameterTokens = tokens.Split(t => t.Type == TokenType.OperatorOrPunctuator && t.Value == ",").ToList();
                    var parameters      = parameterTokens.Select(t => ToParameter(t.ToList())).ToList();

                    var defaultValues = new List <IDataParameter>();
                    foreach (IDataParameter parameter in command.Parameters)
                    {
                        switch (parameter.Direction)
                        {
                        case ParameterDirection.Input:
                        case ParameterDirection.InputOutput:
                            var dataParameter = provider.GetDataParameter(parameter);
                            var first         = parameters.FirstOrDefault(
                                p => string.Compare(p.Name, parameter.ParameterName,
                                                    StringComparison.InvariantCultureIgnoreCase) == 0);
                            if (first == null)
                            {
                                first = parameters.FirstOrDefault(p => p.Name == null);
                                if (first != null)
                                {
                                    parameters.Remove(first);
                                }
                            }

                            if (first != null)
                            {
                                var value = GetParameterValue(dataParameter, first.Value);
                                if (value != null)
                                {
                                    parameter.Value = value;
                                }
                                else
                                {
                                    defaultValues.Add(parameter);
                                }
                            }

                            break;
                        }
                    }

                    foreach (var parameter in defaultValues)
                    {
                        command.Parameters.Remove(parameter);
                    }
                    break;
                }
            }
            else
            {
                command.CommandText = _text;
            }

            return(command);
        }