/// <summary> /// Creates a new entry in the cache file. /// </summary> /// <param name="entry">The entry to create.</param> /// <returns>The new entry's contentId.</returns> public uint NewEntry(ContentEntry entry) { uint active = 1; if (!entry.Active) { active = 0; } uint activate = (uint)((TimeSpan)entry.Activate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds; uint expiry = 0; if (entry.Expires) { expiry = (uint)((TimeSpan)entry.Expiry.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds; } String p = entry.GetPropsAsString(); SqliteCommand query = new SqliteCommand(@"INSERT INTO [content] ([active], [activate], [expire], [dayparts], [contentType], [descriptor], [size], [viewcount], [viewlimit], [displayafter], [props], [data]) VALUES (@active, @activate, @expire, @dayparts, @contentType, @descriptor, @size, @viewcount, @viewlimit, @displayafter, @props, @data); SELECT last_insert_rowid() AS contentId;", sqlite); query.Parameters.Add(new SqliteParameter("@active", (int)active)); query.Parameters.Add(new SqliteParameter("@activate", (int)activate)); query.Parameters.Add(new SqliteParameter("@expire", (int)expiry)); query.Parameters.Add(new SqliteParameter("@dayparts", (int)entry.DayParts)); query.Parameters.Add(new SqliteParameter("@contentType", (int)entry.contentType.contentType)); query.Parameters.Add(new SqliteParameter("@descriptor", entry.Descriptor)); query.Parameters.Add(new SqliteParameter("@size", (int)entry.Size)); query.Parameters.Add(new SqliteParameter("@viewcount", (int)entry.ViewCount)); query.Parameters.Add(new SqliteParameter("@viewlimit", (int)entry.ViewLimit)); query.Parameters.Add(new SqliteParameter("@displayafter", (int)entry.DisplayAfter)); query.Parameters.Add(new SqliteParameter("@props", p)); query.Parameters.Add(new SqliteParameter("@data", entry.Data)); // get the contentId back sqlite.Open(); Object cidR = query.ExecuteScalar(); uint cid = (uint)((long)cidR); // shove cid into props p = "contentId=" + cid.ToString() + "&" + p; // reset connection sqlite.Close(); sqlite.Open(); // shove in updated props query = new SqliteCommand(@"UPDATE [content] SET [props]=@props WHERE [contentId]=@cid", sqlite); query.Parameters.Add(new SqliteParameter("@props", p)); query.Parameters.Add(new SqliteParameter("@cid", (int)cid)); if (query.ExecuteNonQuery() == 0) { sqlite.Close(); throw new DatabaseUpdateFailureException(); } // reset connection sqlite.Close(); sqlite.Open(); // shove in corresponding contentlist entry for record query = new SqliteCommand(@"INSERT INTO [contentlist] ([appId], [contentType], [contentId], [userInfo], [zoneId]) VALUES (@appid, @contentType, @cid, @userinfo, @zoneid)", sqlite); query.Parameters.Add(new SqliteParameter("@appid", this._appID)); query.Parameters.Add(new SqliteParameter("@contentType", (int)entry.contentType.contentType)); query.Parameters.Add(new SqliteParameter("@cid", (int)cid)); query.Parameters.Add(new SqliteParameter("@zoneid", entry.Properties["zoneId"])); query.Parameters.Add(new SqliteParameter("@userinfo", entry.Properties["userInfo"])); if (query.ExecuteNonQuery() == 0) { sqlite.Close(); throw new DatabaseUpdateFailureException(); } sqlite.Close(); return cid; }
/// <summary> /// Updates the entry an the specified contentId. /// </summary> /// <param name="contentID">The contentId to update.</param> /// <param name="entry">The new content of the row.</param> /// <param name="UpdateData">Should the image/video data column be updated?</param> public void EditEntry(uint contentID, ContentEntry entry, bool UpdateData) { uint active = 1; if (!entry.Active) { active = 0; } uint activate = (uint)((TimeSpan)entry.Activate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds; uint expiry = 0; if (entry.Expires) { expiry = (uint)((TimeSpan)entry.Expiry.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds; } String propS = entry.GetPropsAsString(); sqlite.Open(); SqliteCommand query = new SqliteCommand(@"UPDATE [content] SET [active]=@active, [activate]=@activate, [expire]=@expire, [dayparts]=@dayparts, [contentType]=@contentType, [descriptor]=@descriptor, [size]=@size, [viewcount]=@viewcount, [viewlimit]=@viewlimit, [displayafter]=@displayafter, [props]=@props WHERE [contentId]=@cid", sqlite); //query = new SqliteCommand(@"UPDATE [content] SET [active]=@active, [activate]=@activate, [expire]=@expire, [props]=@props WHERE [contentId]=@cid", sqlite); query.Parameters.Add(new SqliteParameter("@cid", (int)contentID)); query.Parameters.Add(new SqliteParameter("@active", (int)active)); query.Parameters.Add(new SqliteParameter("@activate", (int)activate)); query.Parameters.Add(new SqliteParameter("@expire", (int)expiry)); query.Parameters.Add(new SqliteParameter("@dayparts", (int)entry.DayParts)); query.Parameters.Add(new SqliteParameter("@contentType", (int)entry.contentType.contentType)); query.Parameters.Add(new SqliteParameter("@descriptor", entry.Descriptor)); query.Parameters.Add(new SqliteParameter("@size", (int)entry.Size)); query.Parameters.Add(new SqliteParameter("@viewcount", (int)entry.ViewCount)); query.Parameters.Add(new SqliteParameter("@viewlimit", (int)entry.ViewLimit)); query.Parameters.Add(new SqliteParameter("@displayafter", (int)entry.DisplayAfter)); query.Parameters.Add(new SqliteParameter("@props", propS)); if (query.ExecuteNonQuery() != 1) { sqlite.Close(); throw new DatabaseUpdateFailureException(); } sqlite.Close(); sqlite.Open(); // update contentlist query = new SqliteCommand(@"UPDATE [contentlist] SET [zoneId]=@zoneid, [contentType]=@contentType, [userInfo]=@userinfo WHERE [contentId]=@cid", sqlite); query.Parameters.Add(new SqliteParameter("@cid", (int)contentID)); query.Parameters.Add(new SqliteParameter("@contentType", (int)entry.contentType.contentType)); query.Parameters.Add(new SqliteParameter("@zoneid", entry.Properties["zoneId"])); query.Parameters.Add(new SqliteParameter("@userinfo", entry.Properties["userInfo"])); if (query.ExecuteNonQuery() == 0) { sqlite.Close(); throw new DatabaseUpdateFailureException(); } sqlite.Close(); if (UpdateData) { this.ImportImage(contentID, entry.Data); } }