using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; namespace SQLiteRepository { internal static class SQLiteProcedures { public const string GET_LOGS_IN_LAST_X_MINUTES = "select * from TimeLogDb where " + nameof(TimeLogDb.SwipeEventDateTime) + " > ? AND " + nameof(TimeLogDb.UserId_FK) + "=?"; public const string GET_TIMELOGS = "select * from " + nameof(TimeLogDb) + " where (" + nameof(TimeLogDb.UserId_FK) + "=? AND " + nameof(TimeLogDb.CalendarWeek) + "=? and " + nameof(TimeLogDb.Year) + "=?)"; public const string GET_ALL_USERS = "select * from " + nameof(UserIdentity) + " ut " + "where " + "EXISTS( select " + nameof(GroupDb.GroupId) + " from " + nameof(GroupDb) + " where " + nameof(GroupDb.GroupName) + " = 'Archived') AND " + "NOT EXISTS( select * from " + nameof(UserGroupJoinDb) + " ugp where " + nameof(UserGroupJoinDb.UserId_FK) + " = ut.Id" + " and " + nameof(UserGroupJoinDb.GroupId_FK) + " = ( " + "select " + nameof(GroupDb.GroupId) + " from " + nameof(GroupDb) + " where " + nameof(GroupDb.GroupName) + " = 'Archived') )" + "order by " + nameof(UserIdentity.LastName) + " collate nocase, " + nameof(UserIdentity.FirstName) + " collate nocase"; public const string GET_ALL_USERS_PAGINATE = "select * from " + nameof(UserIdentity) + " ut " + "where " + "EXISTS( select " + nameof(GroupDb.GroupId) + " from " + nameof(GroupDb) + " where " + nameof(GroupDb.GroupName) + " = 'Archived') AND " + "NOT EXISTS( select * from " + nameof(UserGroupJoinDb) + " ugp where " + nameof(UserGroupJoinDb.UserId_FK) + " = ut.Id" + " and " + nameof(UserGroupJoinDb.GroupId_FK) + " = ( " + "select " + nameof(GroupDb.GroupId) + " from " + nameof(GroupDb) + " where " + nameof(GroupDb.GroupName) + " = 'Archived') )" + "order by " + nameof(UserIdentity.LastName) + " collate nocase, " + nameof(UserIdentity.FirstName) + " collate nocase " + "limit ? offset ?"; public const string GET_ALL_USERS_BY_GROUP = "select u." + nameof(UserIdentity.Id) + ", u." + nameof(UserIdentity.FirstName) + ", u." + nameof(UserIdentity.LastName) + ", u." + nameof(UserIdentity.HoursPerWeek) + ", u." + nameof(UserIdentity.IsContractor) + " from " + nameof(UserIdentity) + " u left join " + nameof(UserGroupJoinDb) + " ugj on ugj." + nameof(UserGroupJoinDb.UserId_FK) + " = u." + nameof(UserIdentity.Id) + " where ugj." + nameof(UserGroupJoinDb.GroupId_FK) + "=? order by u." + nameof(UserIdentity.LastName) + " collate nocase, u." + nameof(UserIdentity.LastName) + " collate nocase"; public const string GET_USER_BY_ID = "select * from " + nameof(UserIdentity) + " where " + nameof(UserIdentity.Id) + "=?"; public const string GET_CARDS_BY_USER_ID = "select * from " + nameof(CardUniqueId) + " where " + nameof(CardUniqueId.UserId_FK) + "=?"; public const string GET_CARDS_BY_UNIQUE_ID = "select * from " + nameof(CardUniqueId) + " where " + nameof(CardUniqueId.CardUId) + "=?"; public const string GET_CARDS_BY_UNIQUE_ID_LIST = "select * from " + nameof(CardUniqueId) + " where " + nameof(CardUniqueId.CardUId) + " in (#IN#)"; public const string GET_UNASSIGNED_CARD_LIST = "select * from " + nameof(CardUniqueId) + " where " + nameof(CardUniqueId.UserId_FK) + "=?"; public const string CLEAR_UNASSIGNED_CARDS = "delete from " + nameof(CardUniqueId) + " where " + nameof(CardUniqueId.UserId_FK) + "=?"; public const string UPDATE_CARD_USER_ID = "update " + nameof(CardUniqueId) + " set " + nameof(CardUniqueId.UserId_FK) + "=? where " + nameof(CardUniqueId.Id) + "=?"; public const string UPDATE_CARD_LAST_USED = "update " + nameof(CardUniqueId) + " set " + nameof(CardUniqueId.LastUsed) + " = ? where " + nameof(CardUniqueId.Id) + " = ?"; public const string UPDATE_USER_DETAILS = "update " + nameof(UserIdentity) + " set " + nameof(UserIdentity.FirstName) + "=?, " + nameof(UserIdentity.LastName) + "=?, " + nameof(UserIdentity.HoursPerWeek) + "=?," + nameof(UserIdentity.IsContractor) + "=? where " + nameof(UserIdentity.Id) + "=?"; public const string SEARCH_USER_LIST = "SELECT * FROM " + nameof(UserIdentity) + " where(" + nameof(UserIdentity.FirstName) + " Like ? OR " + nameof(UserIdentity.LastName) + " Like ?)"; public const string GET_LAST_TIMELOG_DIRECTION = "SELECT * FROM " + nameof(TimeLogDb) + " where " + nameof(TimeLogDb.UserId_FK) + " = ? order by " + nameof(TimeLogDb.SwipeEventDateTime) + " desc LIMIT 1"; public const string GET_TOTAL_USER_COUNT = "select Max(" + nameof(UserIdentity.Id) + ") from " + nameof(UserIdentity); public const string GET_USER_CONTRACTED_HOURS = "select " + nameof(UserIdentity.HoursPerWeek) + " From UserIdentity where " + nameof(UserIdentity.Id) + "=?"; public const string GET_GROUPS = "select gp."+ nameof(GroupDb.GroupId)+ ", gp."+nameof(GroupDb.GroupName)+", " + "sum(case when gp." + nameof(GroupDb.GroupId) + " = ujdb." + nameof(UserGroupJoinDb.GroupId_FK) + " then 1 else 0 end) as " + nameof(GroupDb.AssignedUserCount) + " from " + nameof(GroupDb) + " gp" + " left join " + nameof(UserGroupJoinDb) + " ujdb " + "on ujdb." + nameof(UserGroupJoinDb.GroupId_FK) + " = gp." + nameof(GroupDb.GroupId) + " group by gp." + nameof(GroupDb.GroupId); public const string GET_GROUPS_FOR_USER = "select gdb." + nameof(GroupDb.GroupId) + ", gdb." + nameof(GroupDb.GroupName) + ", gdb.AssignedUserCount" + " from " + nameof(GroupDb) + " gdb" + " left join " + nameof(UserGroupJoinDb) + " ujdb" + " on gdb." + nameof(GroupDb.GroupId) + " = ujdb." + nameof(UserGroupJoinDb.GroupId_FK) + " where ujdb." + nameof(UserGroupJoinDb.UserId_FK) + " = ?"; public const string GET_GROUP_BY_ID = "select * from " + nameof(GroupDb) + " where " + nameof(GroupDb.GroupId) + " =?"; public const string UPDATE_GROUP = "update " + nameof(GroupDb) + " set " + nameof(GroupDb.GroupName) + "=? where " + nameof(GroupDb.GroupId) + "=?"; public const string GET_GROUP_BY_NAME = "select 1 from " + nameof(GroupDb)+" where "+nameof(GroupDb.GroupName)+"= ?"; public const string GET_TIMELOG_ENTRY = "select * from " + nameof(TimeLogDb) + " where " + nameof(TimeLogDb.Id) + "=?"; public const string DELETE_TIMELOG_ENTRY = "delete from " + nameof(TimeLogDb) + " where " + nameof(TimeLogDb.Id) + "=?"; public const string DELETE_TIMELOG_ENTRIES = "delete from " + nameof(TimeLogDb) + " where " + nameof(TimeLogDb.Id) + "in (#IN#)"; public const string UPDATE_TIMELOG_ENTRY = "update " + nameof(TimeLogDb) + " set " + nameof(TimeLogDb.UserId_FK) + "=?, " + nameof(TimeLogDb.Direction) + "=?," + nameof(TimeLogDb.SwipeEventDateTime) + "=?," + nameof(TimeLogDb.CalendarWeek) + "=?," + nameof(TimeLogDb.Year) + "=?," + nameof(TimeLogDb.Source) + "=? " + "where " + nameof(TimeLogDb.Id) + "=?"; public const string GET_DB_VERSION = "select * from DbVersion"; /// /// This works on the tokenisation of the query string. /// where the list of params is to be inserted, the query should have #IN# /// /// the query to add the list of parameters to /// the parameters to add to the query. /// public static string FormatInQuery(string query, List args) { if (!query.Contains("#IN#")) { throw new ArgumentException("query doesnt contain any #IN# tokenisation"); } //Convert to a comma separated list e.g.: val1,val2,val3 //but we need to use string.. so.. enclose in single quotes 'val1','val2'..etc var argsFormatted = string.Join(",", args.Select(x=>$"'{x}'")); return query.Replace("#IN#", argsFormatted); } } }