示例#1
0
 public async Task <bool> DeleteAsync(object id)
 {
     using (var db = PostgreSqlDbContext.Create(this._options))
     {
         return(await db.ExecuteAsync(this.deleteSql, new { Id = id.ToString() }) > 0);
     }
 }
示例#2
0
 public Task <bool> SaveAsync(EventCollectionStorageModel events)
 {
     using (var db = PostgreSqlDbContext.Create(this.options))
     {
         this.BinarySaveAsync(db, events.Events);
         return(Task.FromResult(true));
     }
 }
示例#3
0
 public async Task <bool> UpdateAsync <TState>(object id, TState state) where TState : IState, new()
 {
     using (var db = PostgreSqlDbContext.Create(this._options))
     {
         var data = this.GetSerializer().Serialize(state);
         return(await db.ExecuteAsync(this.updateSql, new { Id = id.ToString(), Data = data, DataType = this._options.SerializationType }) > 0);
     }
 }
示例#4
0
        public async Task <bool> SaveAsync(EventCollectionStorageModel events)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                await db.OpenAsync();

                this.BinarySaveAsync(db, events.Events);
                return(true);
            }
        }
示例#5
0
        public async Task <TState> ReadAsync <TState>(object id) where TState : IState, new()
        {
            using (var db = PostgreSqlDbContext.Create(this._options))
            {
                var list = await db.QueryAsync <dynamic>(this.selectSql, new { Id = id.ToString() });

                if (list.Count() == 0)
                {
                    return(default(TState));
                }
                var data = list.FirstOrDefault();
                return(this.GetSerializer(data.datatype).Deserialize <TState>(data.data));
            }
        }
示例#6
0
        public async Task <List <EventModel> > GetListAsync(EventQueryModel query)
        {
            var list = new List <EventModel>(query.Limit);

            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                StringBuilder sql = new StringBuilder($"COPY (SELECT typecode,data,datatype,version FROM {tableName} WHERE version > '{query.StartVersion}'");
                if (query.StateId != null)
                {
                    sql.Append($" and stateid = '{query.StateId}'");
                }
                if (query.StartVersion > 0)
                {
                    sql.Append($" and version <= '{query.EndVersion}'");
                }
                if (!string.IsNullOrEmpty(query.EventTypeCode))
                {
                    sql.Append($" and typecode = '{query.EventTypeCode}'");
                }
                if (!string.IsNullOrEmpty(query.RelationEvent))
                {
                    sql.Append($" and relationevent = '{query.RelationEvent}'");
                }
                sql.Append(" ORDER BY version ASC) TO STDOUT(FORMAT BINARY)");

                await db.OpenAsync();

                using (var reader = db.BeginBinaryExport(sql.ToString()))
                {
                    while (reader.StartRow() != -1)
                    {
                        string typeCode  = reader.Read <string>(NpgsqlDbType.Varchar);
                        byte[] dataBytes = reader.Read <byte[]>(NpgsqlDbType.Bytea);
                        string dataType  = reader.Read <string>(NpgsqlDbType.Varchar);
                        long   version   = reader.Read <Int64>(NpgsqlDbType.Bigint);

                        //Get event type
                        EventModel eventModel = this.ConversionEvent(typeCode, dataType, dataBytes, version);
                        if (eventModel != null)
                        {
                            list.Add(eventModel);
                        }
                    }
                }
            }
            return(list);
        }
示例#7
0
 private async Task CreateTable(string name, object stateId, string sql)
 {
     try
     {
         int stateIdLength = this.GetStateIdLength(stateId);
         sql = string.Format(sql, name, stateIdLength);
         using (var db = PostgreSqlDbContext.Create(this._options))
         {
             await db.ExecuteAsync(sql);
         }
     }
     catch (Exception ex)
     {
         this._logger.LogError(ex, $"[{ProviderName}] Creating table {name} failed");
         throw ex;
     }
 }
示例#8
0
        public async Task SaveAsync(List <EventStorageBufferWrap> eventWraps)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                await db.OpenAsync();

                try
                {
                    var eventList = eventWraps.Select(f => f.Value).ToList <EventStorageModel>();
                    this.BinarySaveAsync(db, eventList);
                    eventWraps.ForEach(wrap => wrap.TaskSource.TrySetResult(true));
                    return;
                }
                catch
                {
                    await this.SqlSaveAsync(db, eventWraps);
                }
            }
        }
示例#9
0
        public async Task SaveAsync(List <IDataflowBufferWrap <EventStorageModel> > eventWraps)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                await db.OpenAsync();

                try
                {
                    var eventList = eventWraps.Select(f => f.Data).ToList <EventModel>();
                    this.BinarySaveAsync(db, eventList);
                    eventWraps.ForEach(wrap => wrap.CompleteHandler(true));
                    return;
                }
                catch
                {
                    await this.SqlSaveAsync(db, eventWraps);
                }
            }
        }
示例#10
0
        public async Task <EventModel> GetAsync(object stateId, long version)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                StringBuilder sql = new StringBuilder($"COPY (SELECT typecode,data,datatype FROM {tableName} WHERE stateid = '{stateId.ToString()}' and  version = '{version}' ) TO STDOUT(FORMAT BINARY)");
                await db.OpenAsync();

                using (var reader = db.BeginBinaryExport(sql.ToString()))
                {
                    while (reader.StartRow() != -1)
                    {
                        string typeCode  = reader.Read <string>(NpgsqlDbType.Varchar);
                        byte[] dataBytes = reader.Read <byte[]>(NpgsqlDbType.Bytea);
                        string dataType  = reader.Read <string>(NpgsqlDbType.Varchar);

                        return(this.ConversionEvent(typeCode, dataType, dataBytes, version));
                    }
                    return(null);
                }
            }
        }