Esempio n. 1
0
 public async Task SaveAsync(IAddress key, BotStoreType botStoreType, BotData data, CancellationToken cancellationToken)
 {
     var model = new BotDataStoreDao(key, botStoreType, data)
     {
         TimeStamp = DateTime.Now
     };
     var service = new SqlDataStoreService(ConnectionString);
     await service.InsertOrUpdateAsync(model);
 }
Esempio n. 2
0
        public virtual async Task <bool> UpdateAsync(BotDataStoreDao data)
        {
            int count = 0;

            using (var cn = new SQLiteConnection(ConnectionString))
            {
                cn.Open();

                var sql       = new StringBuilder(@"UPDATE BotState
	                                            SET Data = @Data,
		                                            ETag = @ETag,
		                                            Timestamp = @TimeStamp
                                                WHERE Type = @Type");
                var sqlParams = new DynamicParameters();
                sqlParams.Add("Data", data.Data);
                sqlParams.Add("ETag", data.ETag);
                sqlParams.Add("TimeStamp", data.TimeStamp);
                sqlParams.Add("Type", (int)data.Type);

                switch (data.Type)
                {
                case BotStoreType.BotUserData:
                    sql.Append(@" AND ChannelId = @ChannelId 
                                      AND UserId = @UserId");
                    sqlParams.Add("ChannelId", data.ChannelId);
                    sqlParams.Add("UserId", data.UserId);
                    break;

                case BotStoreType.BotConversationData:
                    sql.Append(@" AND ChannelId = @ChannelId 
                                      AND ConversationId = @ConversationId;");
                    sqlParams.Add("ChannelId", data.ChannelId);
                    sqlParams.Add("ConversationId", data.ConversationId);
                    break;

                case BotStoreType.BotPrivateConversationData:
                    sql.Append(@" AND ChannelId = @ChannelId 
                                      AND ConversationId = @ConversationId 
                                      AND UserId = @UserId;");
                    sqlParams.Add("ChannelId", data.ChannelId);
                    sqlParams.Add("UserId", data.UserId);
                    sqlParams.Add("ConversationId", data.ConversationId);
                    break;

                default:
                    throw new ArgumentException("Unsupported bot store type!");
                }

                count = await cn.ExecuteAsync(sql.ToString(), sqlParams);

                cn.Close();
            }

            return(count != 0);
        }
Esempio n. 3
0
        public virtual async Task <bool> InsertOrUpdateAsync(BotDataStoreDao data)
        {
            var result = Select(data, data.Type);

            if (result == null)
            {
                return(await InsertAsync(data));
            }
            else
            {
                return(await UpdateAsync(data));
            }
        }
Esempio n. 4
0
        public virtual BotDataStoreDao Select(IAddress data, BotStoreType type)
        {
            BotDataStoreDao botState = null;

            using (var cn = new SQLiteConnection(ConnectionString))
            {
                cn.Open();

                var sql       = new StringBuilder(@"SELECT * FROM BotState 
                                                WHERE Type = @Type ");
                var sqlParams = new DynamicParameters();
                sqlParams.Add("Type", (int)type);

                switch (type)
                {
                case BotStoreType.BotUserData:
                    sql.Append(@" AND ChannelId = @ChannelId 
                                      AND UserId = @UserId");
                    sqlParams.Add("ChannelId", data.ChannelId);
                    sqlParams.Add("UserId", data.UserId);
                    break;

                case BotStoreType.BotConversationData:
                    sql.Append(@" AND ChannelId = @ChannelId 
                                      AND ConversationId = @ConversationId;");
                    sqlParams.Add("ChannelId", data.ChannelId);
                    sqlParams.Add("ConversationId", data.ConversationId);
                    break;

                case BotStoreType.BotPrivateConversationData:
                    sql.Append(@" AND ChannelId = @ChannelId 
                                      AND ConversationId = @ConversationId 
                                      AND UserId = @UserId;");
                    sqlParams.Add("ChannelId", data.ChannelId);
                    sqlParams.Add("UserId", data.UserId);
                    sqlParams.Add("ConversationId", data.ConversationId);
                    break;

                default:
                    throw new ArgumentException("Unsupported bot store type!");
                }

                botState = cn.Query <BotDataStoreDao>(sql.ToString(), sqlParams).FirstOrDefault();

                cn.Close();
            }

            return(botState);
        }
Esempio n. 5
0
        public virtual async Task <bool> InsertAsync(BotDataStoreDao data)
        {
            int count = 0;

            using (var cn = new SQLiteConnection(ConnectionString))
            {
                cn.Open();

                var sql = @"INSERT INTO BotState
	                            (Type, BotId, ConversationId, ChannelId, UserId, Data, ETag, ServiceUrl, Timestamp)
	                            VALUES
	                            (@Type, @BotId, @ConversationId, @ChannelId, @UserId, @Data, @ETag, @ServiceUrl, @TimeStamp)"    ;
                count = await cn.ExecuteAsync(sql, data);

                cn.Close();
            }

            return(count != 0);
        }