From b329fd6e5a15408a4d9b1bfb6123f0a74b06d289 Mon Sep 17 00:00:00 2001 From: "Chris.Watts90@outlook.com" Date: Wed, 6 Jun 2018 10:45:22 +0100 Subject: [PATCH] add method to use SQL In queries to retrieve lists. add queries DELETE_TIMELOG_ENTRIES and GET_CARDS_BY_UNIQUE_ID_LIST that use the IN command. this will delete all timelog entries where the ids exist in the list, or get cards in the db where the unique Id exists in the DB respectively. GET_GROUP_BY_NAME is to be used for checking whether a group already exists on create and is designed to only return one record. #95 --- .../SQLiteRepository/SQLiteProcedures.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs index c6d0e80..56611d7 100644 --- a/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs +++ b/DataCenter_Windows/WindowsDataCenter/SQLiteRepository/SQLiteProcedures.cs @@ -1,3 +1,8 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.Design; +using System.Linq; + namespace SQLiteRepository { internal static class SQLiteProcedures @@ -60,6 +65,9 @@ namespace SQLiteRepository 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) + "=?"; @@ -112,12 +120,18 @@ namespace SQLiteRepository 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) + "=?," @@ -128,5 +142,25 @@ namespace SQLiteRepository + "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); + } } } \ No newline at end of file