Ejemplo n.º 1
0
        public async Task <object> ExecuteQueryAsync(Query query, IDictionary <string, object> parameters)
        {
            var sqlQuery = query as SqlQuery;

            var templateContext = new TemplateContext();

            foreach (var parameter in parameters)
            {
                templateContext.SetValue(parameter.Key, parameter.Value);
            }

            var tokenizedQuery = await _liquidTemplateManager.RenderAsync(sqlQuery.Template, templateContext);

            var connection = _store.Configuration.ConnectionFactory.CreateConnection();
            var dialect    = SqlDialectFactory.For(connection);

            var results = new List <JObject>();

            if (!SqlParser.TryParse(tokenizedQuery, dialect, _store.Configuration.TablePrefix, out var rawQuery, out var rawParameters, out var messages))
            {
                return(results);
            }

            if (sqlQuery.ReturnDocuments)
            {
                IEnumerable <int> documentIds;

                using (connection)
                {
                    connection.Open();
                    documentIds = await connection.QueryAsync <int>(rawQuery, rawParameters);
                }

                var documents = await _session.GetAsync <object>(documentIds.ToArray());

                foreach (var document in documents)
                {
                    results.Add(JObject.FromObject(document));
                }
            }
            else
            {
                IEnumerable <dynamic> queryResults;

                using (connection)
                {
                    connection.Open();
                    queryResults = await connection.QueryAsync(rawQuery, rawParameters);
                }

                foreach (var document in queryResults)
                {
                    results.Add(JObject.FromObject(document));
                }
            }

            return(results.ToArray());
        }
Ejemplo n.º 2
0
        public static bool TryParse(string sql, ISqlDialect dialect, string tablePrefix, out string query, out Dictionary <string, object> parameters, out IEnumerable <string> messages)
        {
            try
            {
                var tree = new Irony.Parsing.Parser(language).Parse(sql);

                if (tree.HasErrors())
                {
                    query      = null;
                    parameters = null;

                    messages = tree
                               .ParserMessages
                               .Select(x => $"{x.Message} at line:{x.Location.Line}, col:{x.Location.Column}")
                               .ToArray();

                    return(false);
                }

                var sqlParser = new SqlParser(tree, dialect, tablePrefix);
                sqlParser.Evaluate();

                query      = sqlParser._builder.ToString();
                parameters = sqlParser._parameters;
                messages   = Array.Empty <string>();

                return(true);
            }
            catch (Exception e)
            {
                query      = null;
                parameters = null;
                messages   = new string[] { "Unexpected error: " + e.Message };
            }

            return(false);
        }