コード例 #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 = documentQuery.ToAnyCommand();

                return(_runner.Execute(conn =>
                {
                    anyCommand.Connection = conn;
                    return (T)anyCommand.ExecuteScalar();
                }));
            }

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

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

            throw new NotSupportedException();
        }
コード例 #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 = documentQuery.ToAnyCommand();

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

            if (scalarResultOperator is CountResultOperator)
            {
                var countCommand = documentQuery.ToCountCommand();

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

            if (scalarResultOperator is LongCountResultOperator)
            {
                var countCommand = documentQuery.ToCountCommand();

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

            throw new NotSupportedException();
        }