Ejemplo n.º 1
0
        T IQueryExecutor.ExecuteScalar <T>(QueryModel queryModel)
        {
            var mapping       = _schema.MappingFor(queryModel.SelectClause.Selector.Type);
            var documentQuery = new DocumentQuery(mapping, queryModel, _serializer);

            _schema.EnsureStorageExists(mapping.DocumentType);

            if (queryModel.ResultOperators.OfType <AnyResultOperator>().Any())
            {
                var anyCommand = new NpgsqlCommand();
                documentQuery.ConfigureForAny(anyCommand);

                return(_runner.Execute(anyCommand, c => (T)c.ExecuteScalar()));
            }

            if (queryModel.ResultOperators.OfType <CountResultOperator>().Any())
            {
                var countCommand = new NpgsqlCommand();
                documentQuery.ConfigureForCount(countCommand);

                return(_runner.Execute(countCommand, c =>
                {
                    var returnValue = c.ExecuteScalar();
                    return Convert.ToInt32(returnValue).As <T>();
                }));
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 2
0
        private Task <T> ExecuteScalar <T>(ResultOperatorBase scalarResultOperator, QueryModel queryModel, CancellationToken token)
        {
            var mapping       = _schema.MappingFor(queryModel.SelectClause.Selector.Type);
            var documentQuery = new DocumentQuery(mapping, queryModel, _serializer);

            _schema.EnsureStorageExists(mapping.DocumentType);

            if (scalarResultOperator is AnyResultOperator)
            {
                var anyCommand = new NpgsqlCommand();
                documentQuery.ConfigureForAny(anyCommand);

                return(_runner.ExecuteAsync(anyCommand, async(c, tkn) =>
                {
                    var result = await c.ExecuteScalarAsync(tkn).ConfigureAwait(false);
                    return (T)result;
                }, token));
            }

            if (scalarResultOperator is CountResultOperator)
            {
                var countCommand = new NpgsqlCommand();
                documentQuery.ConfigureForCount(countCommand);

                return(_runner.ExecuteAsync(countCommand, async(c, tkn) =>
                {
                    var returnValue = await c.ExecuteScalarAsync(tkn).ConfigureAwait(false);
                    return Convert.ToInt32(returnValue).As <T>();
                }, token));
            }

            if (scalarResultOperator is LongCountResultOperator)
            {
                var countCommand = new NpgsqlCommand();
                documentQuery.ConfigureForCount(countCommand);

                return(_runner.ExecuteAsync(countCommand, async(c, tkn) =>
                {
                    var returnValue = await c.ExecuteScalarAsync(tkn).ConfigureAwait(false);
                    return Convert.ToInt64(returnValue).As <T>();
                }, token));
            }

            throw new NotSupportedException();
        }