示例#1
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string searchQuery         = request.DataStore.GetValue("SearchQuery") ?? string.Empty;
            string sqlConnectionString = request.DataStore.GetValueAtIndex("SqlConnectionString", "SqlServerIndex");

            searchQuery = searchQuery.Replace("'", "''");
            searchQuery = searchQuery.Replace("“", "\"");
            searchQuery = searchQuery.Replace("”", "\"");

            bool isAdvanced = IsAdvancedTwitterQuery(searchQuery);

            SqlParameter[] parameters = SqlUtility.MapValuesToSqlParameters(1, isAdvanced, searchQuery);
            SqlUtility.ExecuteQueryWithParameters(sqlConnectionString, INSERT_INTO_TWITTER_QUERY, parameters);

            if (!isAdvanced)
            {
                List <string> conditions = SplitQueryByOr(searchQuery);
                int           detailId   = 1;
                for (int i = 0; i < conditions.Count; i++)
                {
                    ReadableQueryPart readableQueryPart = GetReadableQueryPart(conditions[i]);
                    parameters = SqlUtility.MapValuesToSqlParameters(i + 1, 1, readableQueryPart.Query, conditions[i]);
                    SqlUtility.ExecuteQueryWithParameters(sqlConnectionString, INSERT_INTO_TWITTER_QUERY_READABLE, parameters);

                    for (int j = 0; j < readableQueryPart.Details.Count; j++)
                    {
                        string detail = readableQueryPart.Details[j];
                        if (detail[0] == 'C')
                        {
                            parameters = SqlUtility.MapValuesToSqlParameters(detailId, i + 1, TWITTER_OPERATOR_CONTAINS, detail.Substring(TWITTER_OPERATOR_CONTAINS.Length + 2).TrimEnd('"'));
                            SqlUtility.ExecuteQueryWithParameters(sqlConnectionString, INSERT_INTO_TWITTER_QUERY_DETAILS, parameters);
                        }
                        else
                        {
                            parameters = SqlUtility.MapValuesToSqlParameters(detailId, i + 1, TWITTER_OPERATOR_DOES_NOT_CONTAIN, detail.Substring(TWITTER_OPERATOR_DOES_NOT_CONTAIN.Length + 2).TrimEnd('"'));
                            SqlUtility.ExecuteQueryWithParameters(sqlConnectionString, INSERT_INTO_TWITTER_QUERY_DETAILS, parameters);
                        }
                        detailId++;
                    }
                }
            }

            return(new ActionResponse(ActionStatus.Success));
        }
        private ReadableQueryPart GetReadableQueryPart(string query)
        {
            ReadableQueryPart readableQueryPart = new ReadableQueryPart();

            StringBuilder sbPart = new StringBuilder();
            StringBuilder sbRead = new StringBuilder();

            bool isOpenQuote  = false;
            bool isTerminated = false;

            for (int i = 0; i < query.Length; i++)
            {
                switch (query[i])
                {
                case '"':
                    if (isOpenQuote)
                    {
                        isTerminated = true;
                    }
                    isOpenQuote = !isOpenQuote;
                    break;

                case ' ':
                    if (isOpenQuote)
                    {
                        sbPart.Append(query[i]);
                    }
                    else
                    {
                        isTerminated = true;
                    }
                    break;

                case '(':
                    if (isOpenQuote)
                    {
                        sbPart.Append(query[i]);
                    }
                    break;

                case ')':
                    if (isOpenQuote)
                    {
                        sbPart.Append(query[i]);
                    }
                    else
                    {
                        isTerminated = true;
                    }
                    break;

                default:
                    sbPart.Append(query[i]);
                    break;
                }

                if (isTerminated || i == query.Length - 1)
                {
                    string read = sbPart.ToString();
                    if (read.Length > 0)
                    {
                        if (read[0] == '-')
                        {
                            string detail = $"{TWITTER_OPERATOR_DOES_NOT_CONTAIN} \"{read.Substring(1)}\"";
                            readableQueryPart.Details.Add(detail);
                            sbRead.Append(detail);
                        }
                        else
                        {
                            string detail = $"{TWITTER_OPERATOR_CONTAINS} \"{read}\"";
                            readableQueryPart.Details.Add(detail);
                            sbRead.Append(detail);
                        }
                        if (i < query.Length - 2)
                        {
                            sbRead.Append(TWITTER_OPERATOR_AND);
                        }
                    }
                    sbPart.Clear();
                    isTerminated = false;
                }
            }

            readableQueryPart.Query = sbRead.ToString();

            return(readableQueryPart);
        }