using System; using System.Configuration; using System.Configuration.Provider; using Npgsql; using System.Collections.Generic; using Yavsc.Model.Blogs; namespace Npgsql.Web.Blog { /// /// Npgsql blog provider. /// public class NpgsqlBlogProvider : BlogProvider { string applicationName; string connectionString; #region implemented abstract members of BlogProvider /// /// Tag the specified postid and tag. /// /// Postid. /// Tag. public override long Tag (long postid, string tag) { using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand ()) { cmd.CommandText = "insert into bltag (blid,tag) values (@postid,@tag) returning _id"; cmd.Parameters.AddWithValue("@tag",tag); cmd.Parameters.AddWithValue("@postid",postid); return (long) cmd.ExecuteScalar (); } } /// /// Removes the tag. /// /// Tagid. public override void RemoveTag (long tagid) { using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand ()) { cmd.CommandText = "delete from bltag where _id = @tagid"; cmd.Parameters.AddWithValue("@tagid",tagid); cmd.ExecuteNonQuery (); } } /// /// Gets the post identifier. /// /// The post identifier. /// Username. /// Title. public override long GetPostId (string username, string title) { throw new NotImplementedException (); } /// /// Gets the comments. /// /// The comments. /// Postid. /// If set to true get hidden. public override Comment[] GetComments (long postid, bool getHidden) { List cmts = new List (); using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "select _id, username, bcontent, modified, posted, visible from comment " + "where applicationname = @appname and postid = @id" + ((getHidden) ? " and visible = true ":" ") + "order by posted asc" ; cmd.Parameters.AddWithValue ("@appname", applicationName); cmd.Parameters.AddWithValue ("@id", postid); cnx.Open (); using (NpgsqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read ()) { Comment c = new Comment(); c.CommentText = rdr.GetString (rdr.GetOrdinal ("bcontent")); c.From = rdr.GetString (rdr.GetOrdinal ("username")); c.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified")); c.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted")); c.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible")); c.PostId = postid; c.Id = rdr.GetInt64(rdr.GetOrdinal("_id")); cmts.Add (c); } } } return cmts.ToArray(); } /// /// Updates the post. /// /// Postid. /// Title. /// Content. /// If set to true visible. public override void UpdatePost (long postid, string title, string content, bool visible) { using (NpgsqlConnection cnx = new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { DateTime now = DateTime.Now; cmd.CommandText = "update blog set modified=@now," + " title = @title," + " bcontent=@content, " + " visible = @visible " + "where _id = @id"; cmd.Parameters.AddWithValue ("@now", now); cmd.Parameters.AddWithValue ("@title", title); cmd.Parameters.AddWithValue ("@content", content); cmd.Parameters.AddWithValue ("@visible", visible); cmd.Parameters.AddWithValue ("@id", postid); cnx.Open (); cmd.ExecuteNonQuery (); cnx.Close(); } } /// /// Removes the post. /// /// Postid. public override void RemovePost (long postid) { throw new NotImplementedException (); } /// /// Comment the specified from, postid and content. /// /// From. /// Postid. /// Content. public override long Comment (string from, long postid, string content) { if (from == null) throw new ArgumentNullException("from"); if (content == null) throw new ArgumentNullException("content"); bool visible = AutoValidateComment; using (NpgsqlConnection cnx= new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "insert into comment (postid,bcontent," + "modified,posted,visible,username,applicationname)" + "values (@postid,@bcontent,@modified,@posted," + "@visible,@username,@appname) returning _id"; cmd.Parameters.AddWithValue ("@postid", postid); cmd.Parameters.AddWithValue ("@bcontent", content); DateTime now = DateTime.Now; cmd.Parameters.AddWithValue ("@modified", now); cmd.Parameters.AddWithValue ("@posted", now); cmd.Parameters.AddWithValue ("@visible", visible); cmd.Parameters.AddWithValue ("@username", from); cmd.Parameters.AddWithValue ("@appname", applicationName); cnx.Open (); return (long) cmd.ExecuteScalar(); } } /// /// Validates the comment. /// /// Cmtid. public override void ValidateComment (long cmtid) { throw new NotImplementedException (); } /// /// Updates the comment. /// /// Cmtid. /// Content. /// If set to true visible. public override void UpdateComment (long cmtid, string content, bool visible) { throw new NotImplementedException (); } private bool autoValidateComment = true; /// /// Gets or sets a value indicating whether this auto validate comment. /// /// true if auto validate comment; otherwise, false. public override bool AutoValidateComment { get { return autoValidateComment; } set { autoValidateComment=value; } } /// /// Blogs the title. /// /// The title. /// Username. public override string BlogTitle (string username) { throw new NotImplementedException (); } #endregion /// /// Initialize the specified name and config. /// /// Name. /// Config. public override void Initialize (string name, System.Collections.Specialized.NameValueCollection config) { string cnxName = config ["connectionStringName"]; connectionString = ConfigurationManager.ConnectionStrings [cnxName].ConnectionString; config.Remove ("connectionStringName"); applicationName = config ["applicationName"]; config.Remove ("applicationName"); defaultPageSize = int.Parse ( config ["pageLen"] ?? "10") ; base.Initialize (name, config); } #region implemented abstract members of BlogProvider /// /// Gets the post. /// /// The post. /// Postid. public override BlogEntry GetPost (long postid) { BlogEntry be = null; using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "select username, title, bcontent, modified, posted, visible from blog " + "where applicationname = @appname and _id = @id"; cmd.Parameters.AddWithValue ("@appname", applicationName); cmd.Parameters.AddWithValue ("@id", postid); cnx.Open (); using (NpgsqlDataReader rdr = cmd.ExecuteReader()) { if (rdr.Read ()) { be = new BlogEntry (); be.Title = rdr.GetString (rdr.GetOrdinal ("title")); be.Content = rdr.GetString (rdr.GetOrdinal ("bcontent")); be.UserName = rdr.GetString (rdr.GetOrdinal ("username")); be.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified")); be.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted")); be.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible")); be.Id = postid; } } } return be; } /// /// Removes the comment. /// /// The comment. /// Cmtid. public override long RemoveComment (long cmtid) { long postid = 0; using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand ()) { cmd.CommandText = "delete from comment where _id = @id returning postid"; cmd.Parameters.AddWithValue ("id", cmtid); cnx.Open (); postid = (long) cmd.ExecuteScalar (); } return postid; } /// /// Gets the post. /// /// The post. /// Username. /// Title. public override BlogEntry GetPost (string username, string title) { BlogEntry be = null; using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "select _id,bcontent,modified,posted,visible from blog " + "where applicationname = @appname and username = @username and title = @title"; cmd.Parameters.AddWithValue ("@appname", applicationName); cmd.Parameters.AddWithValue ("@username", username); cmd.Parameters.AddWithValue ("@title", title); cnx.Open (); using (NpgsqlDataReader rdr = cmd.ExecuteReader()) { if (rdr.Read ()) { be = new BlogEntry (); be.Title = title; be.Content = rdr.GetString (rdr.GetOrdinal ("bcontent")); be.UserName = username; be.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified")); be.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted")); be.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible")); be.Id = rdr.GetInt64 (rdr.GetOrdinal ("_id")); } rdr.Close (); } if (be!=null) using (NpgsqlCommand cmdtags = cnx.CreateCommand()) { List tags = new List (); cmd.CommandText = "select tag.name from tag,tagged where tag._id = tagged.tagid and tagged.postid = @pid"; cmd.Parameters.AddWithValue ("@pid", be.Id); using (NpgsqlDataReader rdrt = cmd.ExecuteReader ()) { while (rdrt.Read ()) { tags.Add (rdrt.GetString (0)); } } be.Tags = tags.ToArray (); } } return be; } /// /// Post the specified username, title, content and visible. /// /// Username. /// Title. /// Content. /// If set to true visible. public override long Post (string username, string title, string content, bool visible) { if (username == null) throw new ArgumentNullException("username"); if (title == null) throw new ArgumentNullException("title"); if (content == null) throw new ArgumentNullException("content"); using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "insert into blog (title,bcontent,modified,posted,visible,username,applicationname)" + "values (@title,@bcontent,@modified,@posted,@visible,@username,@appname) returning _id"; cmd.Parameters.AddWithValue ("@title", title); cmd.Parameters.AddWithValue ("@bcontent", content); DateTime now = DateTime.Now; cmd.Parameters.AddWithValue ("@modified", now); cmd.Parameters.AddWithValue ("@posted", now); cmd.Parameters.AddWithValue ("@visible", visible); cmd.Parameters.AddWithValue ("@username", username); cmd.Parameters.AddWithValue ("@appname", applicationName); cnx.Open (); return (long) cmd.ExecuteScalar(); } } /// /// Finds the post. /// /// The post. /// Pattern. /// Searchflags. /// Page index. /// Page size. /// Total records. public override BlogEntryCollection FindPost (string pattern, FindBlogEntryFlags searchflags, int pageIndex, int pageSize, out int totalRecords) { BlogEntryCollection c = new BlogEntryCollection (); totalRecords = 0; using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "select title,bcontent,modified,posted,username,visible from blog " + "where applicationname = @appname"; cmd.Parameters.AddWithValue ("@appname", applicationName); if ((searchflags & FindBlogEntryFlags.MatchContent) > 0) { cmd.CommandText += " and bcontent like @bcontent"; cmd.Parameters.AddWithValue ("@bcontent", pattern); } if ((searchflags & FindBlogEntryFlags.MatchTitle) > 0) { cmd.CommandText += " and title like @title"; cmd.Parameters.AddWithValue ("@title", pattern); } if ((searchflags & FindBlogEntryFlags.MatchUserName) > 0) { cmd.CommandText += " and username like @username"; cmd.Parameters.AddWithValue ("@username", pattern); } if ((searchflags & FindBlogEntryFlags.MatchInvisible) == 0) { cmd.CommandText += " and visible = true"; } cmd.CommandText += " order by posted desc"; cnx.Open (); using (NpgsqlDataReader rdr = cmd.ExecuteReader()) { // pageIndex became one based int firstrec = pageIndex * pageSize; int lastrec = firstrec + pageSize - 1; while (rdr.Read()) { if (totalRecords >= firstrec && totalRecords <= lastrec) { BlogEntry be = new BlogEntry (); be.Title = rdr.GetString (rdr.GetOrdinal ("title")); be.Content = rdr.GetString (rdr.GetOrdinal ("bcontent")); be.UserName = rdr.GetString (rdr.GetOrdinal ("username")); be.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted")); be.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified")); be.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible")); c.Add (be); } totalRecords++; } } } return c; } /// /// Removes the post. /// /// Username. /// Title. public override void RemovePost (string username, string title) { using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { cmd.CommandText = "delete from blog where username = @username and applicationname = @appname and title=@title"; cmd.Parameters.AddWithValue ("@username",username); cmd.Parameters.AddWithValue ("@appname", applicationName); cmd.Parameters.AddWithValue ("@title",title); cnx.Open (); cmd.ExecuteNonQuery (); cnx.Close(); } } int defaultPageSize = 10; /// /// Lasts the posts. /// /// The posts. /// Page index. /// Page size. /// Total records. public override BlogEntryCollection LastPosts(int pageIndex, int pageSize, out int totalRecords) { BlogEntryCollection c = new BlogEntryCollection (); using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString)) using (NpgsqlCommand cmd = cnx.CreateCommand()) { /*cmd.CommandText = "select blog.* from blog, " + "(select max(posted) lpost, username " + "from blog where applicationname = @appname " + "group by username) as lblog " + "where blog.posted = lblog.lpost and blog.username = lblog.username " ; */ cmd.CommandText = "select * " + "from blog where applicationname = @appname and visible = true " + " order by posted desc limit @len" ; cmd.Parameters.AddWithValue ("@appname", applicationName); cmd.Parameters.AddWithValue ("@len", defaultPageSize*10); cnx.Open (); using (NpgsqlDataReader rdr = cmd.ExecuteReader()) { totalRecords = 0; int firstrec = pageIndex * pageSize; int lastrec = firstrec + pageSize - 1; while (rdr.Read()) { if (totalRecords >= firstrec && totalRecords <= lastrec) { BlogEntry be = new BlogEntry (); be.Title = rdr.GetString (rdr.GetOrdinal ("title")); be.Content = rdr.GetString (rdr.GetOrdinal ("bcontent")); be.UserName = rdr.GetString (rdr.GetOrdinal ("username")); be.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted")); be.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified")); be.Visible = true; // because of sql code used c.Add (be); } totalRecords++; } } } return c; } #endregion } }