Inheritance: tik4net.Api.ApiSentence, ITikDoneSentence
Esempio n. 1
0
        public string ExecuteScalar()
        {
            EnsureConnectionSet();
            EnsureNotRunning();

            _isRuning = true;
            try
            {
                string[] commandRows = ConstructCommandText(TikCommandParameterFormat.NameValue);
                IEnumerable <ApiSentence> response = EnsureApiSentences(_connection.CallCommandSync(commandRows));
                ThrowPossibleResponseError(response.ToArray());

                if (response.Count() == 1) //!done + =ret=result word
                {
                    ApiDoneSentence doneSentence = EnsureDoneResponse(response.Single());
                    return(doneSentence.GetResponseWord("ret"));
                }
                else if (response.Count() >= 2)
                {
                    EnsureReReponse(response.First());
                    ApiReSentence reResponse = (ApiReSentence)response.First();
                    EnsureDoneResponse(response.Last());

                    return(reResponse.Words.First().Value); //first word value from !re
                }
                else
                {
                    throw new TikConnectionException("Single !done response or at least one !re sentences expected. (1x!done or Nx!re + 1x!done )", this, response.Cast <ITikSentence>());
                }
            }
            finally
            {
                _isRuning = false;
            }
        }
Esempio n. 2
0
        private ApiDoneSentence EnsureDoneResponse(ApiSentence responseSentence)
        {
            ApiDoneSentence doneSentence = responseSentence as ApiDoneSentence;

            if (doneSentence == null)
            {
                throw new TikConnectionException("!done sentence expected as result.", this, responseSentence);
            }

            return(doneSentence);
        }
Esempio n. 3
0
        private string ExecuteScalarInternal(string target, bool allowReturnDefault, string defaultValue = null)
        {
            EnsureConnectionSet();
            EnsureNotRunning();

            _isRuning = true;
            try
            {
                var      targetParameterInArray    = target != null ? new ITikCommandParameter[] { new ApiCommandParameter(TikSpecialProperties.Proplist, target, TikCommandParameterFormat.NameValue) } : new ITikCommandParameter[] { };
                string[] commandRows               = ConstructCommandText(TikCommandParameterFormat.NameValue, targetParameterInArray);
                IEnumerable <ApiSentence> response = EnsureApiSentences(_connection.CallCommandSync(commandRows));
                ThrowPossibleResponseError(response.ToArray());

                if (response.Count() == 1) //!done + =ret=result word
                {
                    ApiDoneSentence doneSentence = EnsureDoneResponse(response.Single());
                    if (doneSentence.Words.ContainsKey(TikSpecialProperties.Ret))
                    {
                        return(doneSentence.GetResponseWord());
                    }
                    else if (allowReturnDefault)
                    {
                        return(defaultValue);
                    }
                    else
                    {
                        throw new TikNoSuchItemException(this);
                    }
                }
                else if (response.Count() == 2) //!re + !done
                {
                    EnsureOneReAndDone(response);
                    ApiReSentence reResponse = (ApiReSentence)response.First();

                    return(reResponse.Words.Single(v => v.Key != TikSpecialProperties.Tag).Value); //single word value from !re  //NOTE - .tag could be added when Connection.SendTagWithSyncCommand=true
                }
                else
                {
                    throw new TikCommandUnexpectedResponseException("Single !done response or exactly one !re sentences expected. (1x!done or 1x!re + 1x!done )", this, response.Cast <ITikSentence>());
                }
            }
            finally
            {
                _isRuning = false;
            }
        }