Merge pull request #1695 from gregoryyoung/master
[mono.git] / mcs / class / System.Web / System.Web.Caching / SqlCacheDependency.cs
1 //
2 // System.Web.UI.SqlCacheDependency
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Security.Permissions;
32 using System.Data.SqlClient;
33 using System.Web;
34
35 namespace System.Web.Caching
36 {
37         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         public sealed class SqlCacheDependency: CacheDependency
39         {
40                 string uniqueId = Guid.NewGuid().ToString();
41
42                 [MonoTODO ("What to do with the sqlCmd?")]
43                 public SqlCacheDependency (SqlCommand sqlCmd)
44                 {
45                         if (sqlCmd == null)
46                                 throw new ArgumentNullException ("sqlCmd");
47                 }
48
49                 [MonoTODO ("What are the params good for?")]
50                 public SqlCacheDependency (string databaseEntryName, string tableName)
51                 {
52                         if (databaseEntryName == null)
53                                 throw new ArgumentNullException ("databaseEntryName");
54
55                         if (tableName == null)
56                                 throw new ArgumentNullException ("tableName");
57                 }
58
59                 [MonoTODO ("Needs more testing - especially the return value and database+table lookup.")]
60                 public static CacheDependency CreateOutputCacheDependency (string dependency)
61                 {
62                         if (dependency == null)
63                                 throw new HttpException (InvalidDependencyFormatMessage (dependency));
64
65                         if (dependency.Length == 0)
66                                 throw new ArgumentException (InvalidDependencyFormatMessage (dependency), "dependency");
67
68                         int colon;
69                         string[] pairs = dependency.Split (';');
70                         var dependencies = new List <SqlCacheDependency> ();
71
72                         foreach (string pair in pairs) {
73                                 colon = pair.IndexOf (':');
74                                 if (colon == -1)
75                                         throw new ArgumentException (InvalidDependencyFormatMessage (dependency), "dependency");
76
77                                 dependencies.Add (new SqlCacheDependency (pair.Substring (0, colon), pair.Substring (colon + 1)));
78                         }
79
80                         switch (dependencies.Count) {
81                                 case 0:
82                                         return null;
83
84                                 case 1:
85                                         return dependencies [0];
86
87                                 default:
88                                         var acd = new AggregateCacheDependency ();
89                                         acd.Add (dependencies.ToArray ());
90                                         return acd;
91                         }
92                 }
93
94                 static string InvalidDependencyFormatMessage (string dependency)
95                 {
96                         return String.Format (@"The '' SqlDependency attribute for OutputCache directive is invalid.
97
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.)
99
100 For dependencies that use SQL Server 9.0 notifications, specify the value 'CommandNotification'.", dependency);
101                 }
102                 
103                 protected override void DependencyDispose ()
104                 {
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 ();
109                 }
110                 
111                 public override string GetUniqueID ()
112                 {
113                         return uniqueId;
114                 }
115         }
116 }