switched to vanilla npgsql for more queries

This commit is contained in:
Vincent Cloutier 2022-12-30 15:49:39 -05:00
parent 4ea8868b7b
commit 621f05c186
3 changed files with 50 additions and 16 deletions

View file

@ -1,4 +1,5 @@
using BirdsiteLive.DAL.Postgres.Settings;
using BirdsiteLive.DAL.Models;
using Npgsql;
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers.Base
@ -12,7 +13,8 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers.Base
protected PostgresBase(PostgresSettings settings)
{
_settings = settings;
_dataSource = NpgsqlDataSource.Create(settings.ConnString);
var dataSourceBuilder = new NpgsqlDataSourceBuilder(settings.ConnString);
_dataSource = dataSourceBuilder.Build();
}
#endregion

View file

@ -8,6 +8,7 @@ using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
using BirdsiteLive.DAL.Postgres.Settings;
using Dapper;
using Newtonsoft.Json;
using Npgsql;
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
{
@ -78,13 +79,33 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
{
if (followedUserId == default) throw new ArgumentException("followedUserId");
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE @id=ANY(followings)";
using (var dbConnection = Connection)
var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE $1=ANY(followings)";
await using var connection = DataSource.CreateConnection();
await connection.OpenAsync();
await using var command = new NpgsqlCommand(query, connection) {
Parameters = { new() { Value = followedUserId}}
};
var reader = await command.ExecuteReaderAsync();
var followers = new List<Follower>();
while (await reader.ReadAsync())
{
var result = await dbConnection.QueryAsync<SerializedFollower>(query, new { id = followedUserId});
return result.Select(Convert).ToArray();
followers.Add(new Follower
{
Id = reader["id"] as int? ?? default,
Followings = reader["followings"] as List<int> ?? new List<int>(),
FollowingsSyncStatus = reader["followingsSyncStatus"] as Dictionary<int, long> ?? new Dictionary<int, long>(),
ActorId = reader["actorId"] as string,
Acct = reader["acct"] as string,
Host = reader["host"] as string,
InboxRoute = reader["host"] as string,
SharedInboxRoute = reader["sharedInboxRoute"] as string,
PostingErrorCount = reader["postingErrorCount"] as int? ?? default,
});
}
return followers.ToArray();
}
public async Task<Follower[]> GetAllFollowersAsync()

View file

@ -144,12 +144,14 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
if(username == default) throw new ArgumentException("id");
if(twitterUserId == default) throw new ArgumentException("twtterUserId");
var query = $"UPDATE {_settings.TwitterUserTableName} SET twitterUserId = @twitterUserId WHERE acct = @username";
var query = $"UPDATE {_settings.TwitterUserTableName} SET twitterUserId = $1 WHERE acct = $2";
await using var connection = DataSource.CreateConnection();
await connection.OpenAsync();
await using var command = new NpgsqlCommand(query, connection) {
Parameters = { new() { Value = twitterUserId}, new() { Value = username}}
};
using (var dbConnection = Connection)
{
await dbConnection.QueryAsync(query, new { username, twitterUserId });
}
await command.ExecuteNonQueryAsync();
}
public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, int fetchingErrorCount, DateTime lastSync)
{
@ -158,12 +160,21 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
if(lastTweetSynchronizedForAllFollowersId == default) throw new ArgumentException("lastTweetSynchronizedForAllFollowersId");
if(lastSync == default) throw new ArgumentException("lastSync");
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId, fetchingErrorCount = @fetchingErrorCount, lastSync = @lastSync WHERE id = @id";
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = $1, lastTweetSynchronizedForAllFollowersId = $2, fetchingErrorCount = $3, lastSync = $4 WHERE id = $5";
using (var dbConnection = Connection)
{
await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId, fetchingErrorCount, lastSync = lastSync.ToUniversalTime() });
}
await using var connection = DataSource.CreateConnection();
await connection.OpenAsync();
await using var command = new NpgsqlCommand(query, connection) {
Parameters = {
new() { Value = lastTweetPostedId},
new() { Value = lastTweetSynchronizedForAllFollowersId},
new() { Value = fetchingErrorCount},
new() { Value = lastSync.ToUniversalTime()},
new() { Value = id},
}
};
await command.ExecuteNonQueryAsync();
}
public async Task UpdateTwitterUserAsync(SyncTwitterUser user)