#nullable enable using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Dapper; namespace PluralKit.Core { public static class ModelQueryExt { public static Task QuerySystem(this IPKConnection conn, SystemId id) => conn.QueryFirstOrDefaultAsync("select * from systems where id = @id", new {id}); public static Task GetSystemMemberCount(this IPKConnection conn, SystemId id, PrivacyLevel? privacyFilter = null) { var query = new StringBuilder("select count(*) from members where system = @Id"); if (privacyFilter != null) query.Append($" and member_visibility = {(int) privacyFilter.Value}"); return conn.QuerySingleAsync(query.ToString(), new {Id = id}); } public static Task> GetLinkedAccounts(this IPKConnection conn, SystemId id) => conn.QueryAsync("select uid from accounts where system = @Id", new {Id = id}); public static Task QueryMember(this IPKConnection conn, MemberId id) => conn.QueryFirstOrDefaultAsync("select * from members where id = @id", new {id}); public static Task QueryMemberByHid(this IPKConnection conn, string hid) => conn.QueryFirstOrDefaultAsync("select * from members where hid = @hid", new {hid = hid.ToLowerInvariant()}); public static Task QueryGroupByName(this IPKConnection conn, SystemId system, string name) => conn.QueryFirstOrDefaultAsync("select * from groups where system = @System and lower(Name) = lower(@Name)", new {System = system, Name = name}); public static Task QueryGroupByHid(this IPKConnection conn, string hid) => conn.QueryFirstOrDefaultAsync("select * from groups where hid = @hid", new {hid = hid.ToLowerInvariant()}); public static Task> QueryGroupsInSystem(this IPKConnection conn, SystemId system) => conn.QueryAsync("select * from groups where system = @System", new {System = system}); public static Task QueryGroupMemberCount(this IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null) { var query = new StringBuilder("select count(*) from group_members"); if (privacyFilter != null) query.Append(" inner join members on group_members.member_id = members.id"); query.Append(" where group_members.group_id = @Id"); if (privacyFilter != null) query.Append(" and members.member_visibility = @PrivacyFilter"); return conn.QuerySingleOrDefaultAsync(query.ToString(), new {Id = id, PrivacyFilter = privacyFilter}); } public static Task> QueryMemberGroups(this IPKConnection conn, MemberId id) => conn.QueryAsync( "select groups.* from group_members inner join groups on group_members.group_id = groups.id where group_members.member_id = @Id", new {Id = id}); public static Task QueryOrInsertGuildConfig(this IPKConnection conn, ulong guild) => conn.QueryFirstAsync("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild}); public static Task QueryOrInsertSystemGuildConfig(this IPKConnection conn, ulong guild, SystemId system) => conn.QueryFirstAsync( "insert into system_guild (guild, system) values (@guild, @system) on conflict (guild, system) do update set guild = @guild, system = @system returning *", new {guild, system}); public static Task QueryOrInsertMemberGuildConfig( this IPKConnection conn, ulong guild, MemberId member) => conn.QueryFirstAsync( "insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *", new {guild, member}); } }