private static int? UpdateInfo(int progid, string episodeExtId) { Guid pluginId; string progExtId; ProgrammeInfo progInfo; using (SQLiteCommand command = new SQLiteCommand("select pluginid, extid, name, description, singleepisode from programmes where progid=@progid", FetchDbConn())) { command.Parameters.Add(new SQLiteParameter("@progid", progid)); using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { if (!reader.Read()) { throw new DataNotFoundException(progid, "Programme does not exist"); } pluginId = new Guid(reader.GetString(reader.GetOrdinal("pluginid"))); progExtId = reader.GetString(reader.GetOrdinal("extid")); progInfo = new ProgrammeInfo(); progInfo.Name = reader.GetString(reader.GetOrdinal("name")); int descriptionOrdinal = reader.GetOrdinal("description"); if (!reader.IsDBNull(descriptionOrdinal)) { progInfo.Description = reader.GetString(descriptionOrdinal); } progInfo.SingleEpisode = reader.GetBoolean(reader.GetOrdinal("singleepisode")); } } IRadioProvider providerInst = Provider.GetFromId(pluginId).CreateInstance(); EpisodeInfo episodeInfo; try { episodeInfo = providerInst.GetEpisodeInfo(progExtId, progInfo, episodeExtId); if (episodeInfo == null) { return null; } if (string.IsNullOrEmpty(episodeInfo.Name)) { throw new InvalidDataException("Episode name cannot be null or an empty string"); } } catch (Exception provExp) { provExp.Data.Add("Programme", progInfo.ToString() + "\r\nExtID: " + progExtId); provExp.Data.Add("Episode ExtID", episodeExtId); throw new ProviderException("Call to GetEpisodeInfo failed", provExp, pluginId); } if (episodeInfo.Date == null) { // The date of the episode isn't known, so use the current date episodeInfo.Date = DateTime.Now; } lock (DbUpdateLock) { using (SQLiteMonTransaction transMon = new SQLiteMonTransaction(FetchDbConn().BeginTransaction())) { int? epid = null; using (SQLiteCommand command = new SQLiteCommand("select epid from episodes where progid=@progid and extid=@extid", FetchDbConn(), transMon.Trans)) { command.Parameters.Add(new SQLiteParameter("@progid", progid)); command.Parameters.Add(new SQLiteParameter("@extid", episodeExtId)); using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { if (reader.Read()) { epid = reader.GetInt32(reader.GetOrdinal("epid")); } } } if (epid == null) { using (SQLiteCommand command = new SQLiteCommand("insert into episodes (progid, extid, name, date) values (@progid, @extid, @name, @date)", FetchDbConn(), transMon.Trans)) { command.Parameters.Add(new SQLiteParameter("@progid", progid)); command.Parameters.Add(new SQLiteParameter("@extid", episodeExtId)); command.Parameters.Add(new SQLiteParameter("@name", episodeInfo.Name)); command.Parameters.Add(new SQLiteParameter("@date", episodeInfo.Date)); command.ExecuteNonQuery(); } using (SQLiteCommand command = new SQLiteCommand("select last_insert_rowid()", FetchDbConn(), transMon.Trans)) { epid = (int)(long)command.ExecuteScalar(); } } using (SQLiteCommand command = new SQLiteCommand("update episodes set name=@name, description=@description, duration=@duration, date=@date, image=@image, available=1 where epid=@epid", FetchDbConn(), transMon.Trans)) { command.Parameters.Add(new SQLiteParameter("@name", episodeInfo.Name)); command.Parameters.Add(new SQLiteParameter("@description", episodeInfo.Description)); command.Parameters.Add(new SQLiteParameter("@duration", episodeInfo.Duration)); command.Parameters.Add(new SQLiteParameter("@date", episodeInfo.Date)); command.Parameters.Add(new SQLiteParameter("@image", StoreImage(episodeInfo.Image))); command.Parameters.Add(new SQLiteParameter("@epid", epid)); command.ExecuteNonQuery(); } using (SQLiteCommand command = new SQLiteCommand("insert or replace into episodeext (epid, name, value) values (@epid, @name, @value)", FetchDbConn(), transMon.Trans)) { foreach (KeyValuePair<string, string> extItem in episodeInfo.ExtInfo) { command.Parameters.Add(new SQLiteParameter("@epid", epid)); command.Parameters.Add(new SQLiteParameter("@name", extItem.Key)); command.Parameters.Add(new SQLiteParameter("@value", extItem.Value)); command.ExecuteNonQuery(); } } transMon.Trans.Commit(); return epid; } } }
private static int?UpdateInfo(int progid, string episodeExtId) { Guid pluginId; string progExtId; ProgrammeInfo progInfo; using (SQLiteCommand command = new SQLiteCommand("select pluginid, extid, name, description, singleepisode from programmes where progid=@progid", FetchDbConn())) { command.Parameters.Add(new SQLiteParameter("@progid", progid)); using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { if (!reader.Read()) { throw new DataNotFoundException(progid, "Programme does not exist"); } pluginId = new Guid(reader.GetString(reader.GetOrdinal("pluginid"))); progExtId = reader.GetString(reader.GetOrdinal("extid")); progInfo = new ProgrammeInfo(); progInfo.Name = reader.GetString(reader.GetOrdinal("name")); int descriptionOrdinal = reader.GetOrdinal("description"); if (!reader.IsDBNull(descriptionOrdinal)) { progInfo.Description = reader.GetString(descriptionOrdinal); } progInfo.SingleEpisode = reader.GetBoolean(reader.GetOrdinal("singleepisode")); } } IRadioProvider providerInst = Provider.GetFromId(pluginId).CreateInstance(); EpisodeInfo episodeInfo; try { episodeInfo = providerInst.GetEpisodeInfo(progExtId, progInfo, episodeExtId); if (episodeInfo == null) { return(null); } if (string.IsNullOrEmpty(episodeInfo.Name)) { throw new InvalidDataException("Episode name cannot be null or an empty string"); } } catch (Exception provExp) { provExp.Data.Add("Programme", progInfo.ToString() + "\r\nExtID: " + progExtId); provExp.Data.Add("Episode ExtID", episodeExtId); throw new ProviderException("Call to GetEpisodeInfo failed", provExp, pluginId); } if (episodeInfo.Date == null) { // The date of the episode isn't known, so use the current date episodeInfo.Date = DateTime.Now; } lock (Database.DbUpdateLock) { using (SQLiteMonTransaction transMon = new SQLiteMonTransaction(FetchDbConn().BeginTransaction())) { int?epid = null; using (SQLiteCommand command = new SQLiteCommand("select epid from episodes where progid=@progid and extid=@extid", FetchDbConn(), transMon.Trans)) { command.Parameters.Add(new SQLiteParameter("@progid", progid)); command.Parameters.Add(new SQLiteParameter("@extid", episodeExtId)); using (SQLiteMonDataReader reader = new SQLiteMonDataReader(command.ExecuteReader())) { if (reader.Read()) { epid = reader.GetInt32(reader.GetOrdinal("epid")); } } } if (epid == null) { using (SQLiteCommand command = new SQLiteCommand("insert into episodes (progid, extid, name, date) values (@progid, @extid, @name, @date)", FetchDbConn(), transMon.Trans)) { command.Parameters.Add(new SQLiteParameter("@progid", progid)); command.Parameters.Add(new SQLiteParameter("@extid", episodeExtId)); command.Parameters.Add(new SQLiteParameter("@name", episodeInfo.Name)); command.Parameters.Add(new SQLiteParameter("@date", episodeInfo.Date)); command.ExecuteNonQuery(); } using (SQLiteCommand command = new SQLiteCommand("select last_insert_rowid()", FetchDbConn(), transMon.Trans)) { epid = (int)(long)command.ExecuteScalar(); } } using (SQLiteCommand command = new SQLiteCommand("update episodes set name=@name, description=@description, duration=@duration, date=@date, image=@image, available=1 where epid=@epid", FetchDbConn(), transMon.Trans)) { command.Parameters.Add(new SQLiteParameter("@name", episodeInfo.Name)); command.Parameters.Add(new SQLiteParameter("@description", episodeInfo.Description)); command.Parameters.Add(new SQLiteParameter("@duration", episodeInfo.Duration)); command.Parameters.Add(new SQLiteParameter("@date", episodeInfo.Date)); command.Parameters.Add(new SQLiteParameter("@image", StoreImage(episodeInfo.Image))); command.Parameters.Add(new SQLiteParameter("@epid", epid)); command.ExecuteNonQuery(); } using (SQLiteCommand command = new SQLiteCommand("insert or replace into episodeext (epid, name, value) values (@epid, @name, @value)", FetchDbConn(), transMon.Trans)) { foreach (KeyValuePair <string, string> extItem in episodeInfo.ExtInfo) { command.Parameters.Add(new SQLiteParameter("@epid", epid)); command.Parameters.Add(new SQLiteParameter("@name", extItem.Key)); command.Parameters.Add(new SQLiteParameter("@value", extItem.Value)); command.ExecuteNonQuery(); } } transMon.Trans.Commit(); return(epid); } } }