2 // System.Web.UI.SqlCacheDependency
5 // Marek Habersack (mhabersack@novell.com)
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.Security.Permissions;
32 using System.Data.SqlClient;
35 namespace System.Web.Caching
37 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 public sealed class SqlCacheDependency: CacheDependency
40 string uniqueId = Guid.NewGuid().ToString();
42 [MonoTODO ("What to do with the sqlCmd?")]
43 public SqlCacheDependency (SqlCommand sqlCmd)
46 throw new ArgumentNullException ("sqlCmd");
49 [MonoTODO ("What are the params good for?")]
50 public SqlCacheDependency (string databaseEntryName, string tableName)
52 if (databaseEntryName == null)
53 throw new ArgumentNullException ("databaseEntryName");
55 if (tableName == null)
56 throw new ArgumentNullException ("tableName");
59 [MonoTODO ("Needs more testing - especially the return value and database+table lookup.")]
60 public static CacheDependency CreateOutputCacheDependency (string dependency)
62 if (dependency == null)
63 throw new HttpException (InvalidDependencyFormatMessage (dependency));
65 if (dependency.Length == 0)
66 throw new ArgumentException (InvalidDependencyFormatMessage (dependency), "dependency");
69 string[] pairs = dependency.Split (';');
70 var dependencies = new List <SqlCacheDependency> ();
72 foreach (string pair in pairs) {
73 colon = pair.IndexOf (':');
75 throw new ArgumentException (InvalidDependencyFormatMessage (dependency), "dependency");
77 dependencies.Add (new SqlCacheDependency (pair.Substring (0, colon), pair.Substring (colon + 1)));
80 switch (dependencies.Count) {
85 return dependencies [0];
88 var acd = new AggregateCacheDependency ();
89 acd.Add (dependencies.ToArray ());
94 static string InvalidDependencyFormatMessage (string dependency)
96 return String.Format (@"The '' SqlDependency attribute for OutputCache directive is invalid.
98 For SQL Server 7.0 and SQL Server 2000, the valid format is ""database:tablename"", and table name must conform to the format of regular identifiers in SQL. To specify multiple pairs of values, use the ';' separator between pairs. (To specify ':', '\' or ';', prefix it with the '\' escape character.)
100 For dependencies that use SQL Server 9.0 notifications, specify the value 'CommandNotification'.", dependency);
103 protected override void DependencyDispose ()
105 // MSDN doesn't document it as being part of the class, but assembly
106 // comparison shows that it does exist in this type, so we're just calling
107 // the base class here
108 base.DependencyDispose ();
111 public override string GetUniqueID ()