async Task <bool> SetCurrent()
        {
            while (readerState.ReadNextProperty(jsonTextReader))
            {
                readerState.ReadNextPropertyValue(jsonTextReader);
                CursorResult.SetFromJsonTextReader(readerState.PropertyName, readerState.Token, readerState.Value);
            }

            if (jsonTextReader.TokenType == JsonToken.EndObject)
            {
                new BaseResultAnalyzer(db).ThrowIfNeeded(CursorResult);

                if (db.Setting.ThrowForServerErrors == false && CursorResult.HasError())
                {
                    return(false);
                }
            }

            if (readerState.ReadNextArrayValue(jsonTextReader))
            {
                var documentSerializer = new DocumentSerializer(db);
                if (db.Setting.DisableChangeTracking == true || jsonTextReader.TokenType != JsonToken.StartObject)
                {
                    Current = documentSerializer.Deserialize <T>(jsonTextReader);
                }
                else
                {
                    JObject jObject = null;
                    Current = documentSerializer.DeserializeSingleResult <T>(jsonTextReader, out jObject);
                    db.ChangeTracker.TrackChanges(Current, jObject);
                }
                return(true);
            }
            else
            {
                while (readerState.ReadNextProperty(jsonTextReader))
                {
                    readerState.ReadNextPropertyValue(jsonTextReader);
                    CursorResult.SetFromJsonTextReader(readerState.PropertyName, readerState.Token, readerState.Value);
                }

                if (CursorResult.HasMore)
                {
                    Dispose();
                    await MakeNextRequest().ConfigureAwait(false);

                    readerState.InitiateRead(jsonTextReader);
                    return(await SetCurrent().ConfigureAwait(false));
                }
                else
                {
                    Dispose();
                    return(false);
                }
            }
        }
 public CursorAsyncEnumerator(IArangoDatabase db, HttpCommand command, object data = null)
 {
     this.db = db;
     this.initiateCommand = command;
     this.CursorResult    = new CursorResult {
         HasMore = true
     };
     this.data   = data;
     readerState = new ReaderState();
 }
Example #3
0
 public MsgBodyReader(IGenericBsonSerializer <T> serializer, ResponseMsgMessage message)
 {
     Serializer   = serializer;
     Message      = message;
     CursorResult = new CursorResult <T>
     {
         MongoCursor = new MongoCursor <T>
         {
             Items = CursorItemsPool <T> .Pool.Get()
         }
     };
 }
Example #4
0
 public IRemoteServerCursor Open(ref RemoteParamData paramsValue, out ProgramStatistics executeTime, ProcessCallInfo callInfo)
 {
     try
     {
         var          channel = GetServiceInterface();
         IAsyncResult result  = channel.BeginOpenPlanCursor(PlanHandle, callInfo, paramsValue, null, null);
         result.AsyncWaitHandle.WaitOne();
         CursorResult cursorResult = channel.EndOpenPlanCursor(result);
         executeTime        = cursorResult.ExecuteTime;
         _programStatistics = executeTime;
         paramsValue.Data   = cursorResult.ParamData;
         return(new ClientCursor(this, cursorResult.CursorDescriptor));
     }
     catch (FaultException <DataphorFault> fault)
     {
         throw DataphorFaultUtility.FaultToException(fault.Detail);
     }
     catch (CommunicationException ce)
     {
         ReportCommunicationError();
         throw new ServerException(ServerException.Codes.CommunicationFailure, ErrorSeverity.Environment, ce);
     }
 }
        private static async Task <CursorPageSlice <TEntity> > ExecuteBatchSliceQueryAsync <TEntity>(
            this DbConnection dbConn,
            SqlQuerySliceInfo sqlQuerySliceInfo,
            object queryParams                  = null,
            string tableName                    = null,
            int?commandTimeout                  = null,
            IDbTransaction transaction          = null,
            Action <string> logTrace            = null,
            CancellationToken cancellationToken = default
            ) where TEntity : class
        {
            var commandText = sqlQuerySliceInfo.SQL;

            logTrace?.Invoke($"Query: {commandText}");

            var timer = Stopwatch.StartNew();

            //Ensure that the DB Connection is open (RepoDb provided extension)...
            await dbConn.EnsureOpenAsync(cancellationToken).ConfigureAwait(false);

            logTrace?.Invoke($"DB Connection Established in: {timer.ToElapsedTimeDescriptiveFormat()}");

            //Re-use the RepoDb Execute Reader method to get benefits of Command & Param caches, etc.
            await using var reader = (DbDataReader) await dbConn.ExecuteReaderAsync(
                            commandText : commandText,
                            param : queryParams,
                            commandType : CommandType.Text,
                            transaction : transaction,
                            commandTimeout : commandTimeout,
                            cancellationToken : cancellationToken
                            ).ConfigureAwait(false);

            //BBernard
            //We NEED to manually process the Reader externally here!
            //Therefore, this had to be Code borrowed from RepoDb Source (DataReader.ToEnumerableAsync<TEntity>(...)
            // core code so that we clone minimal amount of logic outside of RepoDb due to 'internal' scope.
            var results    = new List <CursorResult <TEntity> >();
            int?totalCount = null;

            if (reader != null && !reader.IsClosed)
            {
                //BBernard - 08/09/2021
                //BUGFIX: Fix issue where when there are no results we still need to check and see if TotalCount has any results...
                if (reader.HasRows)
                {
                    const string cursorIndexPropName = nameof(IHaveCursor.CursorIndex);

                    //It's exposed for easy use by extensions so we use Brute Force Reflection to get the Model Mapping Function
                    //  because RepoDb does this very well already!
                    //NOTE: The complied Mapping Func (delegate) that is retrieved is lazy loaded into a static cache reference
                    //      by Generic Type <TEntity> for extremely high performance once initialized!
                    var repoDbModelMappingFunc = await GetRepoDbModelMappingFuncByBruteForce <TEntity>(dbConn, reader, tableName, transaction, cancellationToken);

                    while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                    {
                        //Dynamically read the Entity from the Results...
                        TEntity entity = repoDbModelMappingFunc(reader);

                        //Manually Process the Cursor for each record...
                        var cursorIndex = Convert.ToInt32(reader.GetValue(cursorIndexPropName));

                        //This allows us to extract the CursorIndex field and return in a Decorator class
                        //  so there's NO REQUIREMENT that the Model (TEntity) have any special fields/interfaces added.
                        var cursorResult = new CursorResult <TEntity>(entity, cursorIndex);
                        results.Add(cursorResult);
                    }
                }

                //Now attempt to step to the Total Count query result...
                //Note: We know to attempt getting the TotalCount if there is a second result set available.
                if (await reader.NextResultAsync(cancellationToken).ConfigureAwait(false) &&
                    await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    //This is a Scalar query so the first ordinal value is the Total Count!
                    totalCount = Convert.ToInt32(reader.GetValue(0));
                }
            }

            timer.Stop();
            logTrace?.Invoke($"Query Execution Time: {timer.ToElapsedTimeDescriptiveFormat()}");

            //Process the Results and determine Pagination metadata, etc.
            var cursorPage = PostProcessResultsIntoCursorPageSlice(results, sqlQuerySliceInfo, totalCount);

            return(cursorPage);
        }