Added Mono.Data.Sqlite to build. Restored the sealed classes
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite_2.0 / SQLiteConnectionStringBuilder.cs
1 //
2 // Mono.Data.Sqlite.SQLiteConnectionStringBuilder.cs
3 //
4 // Author(s):
5 //   Robert Simpson (robert@blackcastlesoft.com)
6 //
7 // Adapted and modified for the Mono Project by
8 //   Marek Habersack (grendello@gmail.com)
9 //
10 //
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 /********************************************************
35  * ADO.NET 2.0 Data Provider for Sqlite Version 3.X
36  * Written by Robert Simpson (robert@blackcastlesoft.com)
37  * 
38  * Released to the public domain, use at your own risk!
39  ********************************************************/
40 #if NET_2_0
41 namespace Mono.Data.Sqlite
42 {
43   using System;
44   using System.Data.Common;
45   using System.ComponentModel;
46   using System.Collections;
47   using System.Globalization;
48   using System.Reflection;
49
50 #if !PLATFORM_COMPACTFRAMEWORK
51   using System.ComponentModel.Design;
52
53   /// <summary>
54   /// Sqlite implementation of DbConnectionStringBuilder.
55   /// </summary>
56   [DefaultProperty("DataSource")]
57   [DefaultMember("Item")]
58   public sealed class SqliteConnectionStringBuilder : DbConnectionStringBuilder
59   {
60     /// <summary>
61     /// Properties of this class
62     /// </summary>
63     private Hashtable _properties;
64
65     /// <overloads>
66     /// Constructs a new instance of the class
67     /// </overloads>
68     /// <summary>
69     /// Default constructor
70     /// </summary>
71     public SqliteConnectionStringBuilder()
72     {
73       Initialize(null);
74     }
75
76     /// <summary>
77     /// Constructs a new instance of the class using the specified connection string.
78     /// </summary>
79     /// <param name="connectionString">The connection string to parse</param>
80     public SqliteConnectionStringBuilder(string connectionString)
81     {
82       Initialize(connectionString);
83     }
84
85     /// <summary>
86     /// Private initializer, which assigns the connection string and resets the builder
87     /// </summary>
88     /// <param name="cnnString">The connection string to assign</param>
89     private void Initialize(string cnnString)
90     {
91       _properties = new Hashtable();
92       base.GetProperties(_properties);
93
94       if (String.IsNullOrEmpty(cnnString) == false)
95         ConnectionString = cnnString;
96     }
97
98     /// <summary>
99     /// Gets/Sets the default version of the Sqlite engine to instantiate.  Currently the only valid value is 3, indicating version 3 of the sqlite library.
100     /// </summary>
101     [Browsable(true)]
102     [DefaultValue(3)]
103     public int Version
104     {
105       get
106       {
107         if (ContainsKey("Version") == false) return 3;
108
109         return Convert.ToInt32(this["Version"], CultureInfo.CurrentCulture);
110       }
111       set
112       {
113         if (value != 3)
114           throw new NotSupportedException();
115
116         this["Version"] = value;
117       }
118     }
119
120     /// <summary>
121     /// Gets/Sets the synchronous mode of the connection string.  Default is "Normal".
122     /// </summary>
123     [DisplayName("Synchronous")]
124     [Browsable(true)]
125     [DefaultValue(SynchronizationModes.Normal)]
126     public SynchronizationModes SyncMode
127     {
128       get
129       {
130         return (SynchronizationModes)TypeDescriptor.GetConverter(typeof(SynchronizationModes)).ConvertFrom(this["Synchronous"]);
131       }
132       set
133       {
134         this["Synchronous"] = value;
135       }
136     }
137
138     /// <summary>
139     /// Gets/Sets the encoding for the connection string.  The default is "False" which indicates UTF-8 encoding.
140     /// </summary>
141     [Browsable(true)]
142     [DefaultValue(false)]
143     public bool UseUTF16Encoding
144     {
145       get
146       {
147         return Convert.ToBoolean(this["UseUTF16Encoding"], CultureInfo.CurrentCulture);
148       }
149       set
150       {
151         this["UseUTF16Encoding"] = value;
152       }
153     }
154
155     /// <summary>
156     /// Gets/Sets the filename to open on the connection string.
157     /// </summary>
158     [DisplayName("Data Source")]
159     [Browsable(true)]
160     public string DataSource
161     {
162       get
163       {
164         if (ContainsKey("Data Source") == false) return "";
165
166         return this["Data Source"].ToString();
167       }
168       set
169       {
170         this["Data Source"] = value;
171       }
172     }
173
174 #region Mono-specific
175     /// <summary>
176     /// Gets/Sets the filename to open on the connection string (Mono-specific, uses DataSource).
177     /// </summary>
178     [DisplayName("Data Source")]
179     [Browsable(true)]
180     public string Uri
181     {
182       get
183       {
184         return DataSource;
185       }
186       set
187       {
188         DataSource = value;
189       }
190     }
191 #endregion
192     
193     /// <summary>
194     /// Determines whether or not the connection will automatically participate
195     /// in the current distributed transaction (if one exists)
196     /// </summary>
197     [DisplayName("Automatic Enlistment")]
198     [Browsable(true)]
199     [DefaultValue(true)]
200     public bool Enlist
201     {
202       get
203       {
204         if (ContainsKey("Enlist") == false) return true;
205
206         return (this["Enlist"].ToString() == "Y");
207       }
208       set
209       {
210         this["Enlist"] = (value == true) ? "Y" : "N";
211       }
212     }
213     /// <summary>
214     /// Gets/sets the database encryption password
215     /// </summary>
216     [Browsable(true)]
217     [PasswordPropertyText(true)]
218     public string Password
219     {
220       get
221       {
222         if (ContainsKey("Password") == false) return "";
223
224         return this["Password"].ToString();
225       }
226       set
227       {
228         this["Password"] = value;
229       }
230     }
231
232     /// <summary>
233     /// Gets/Sets the page size for the connection.
234     /// </summary>
235     [DisplayName("Page Size")]
236     [Browsable(true)]
237     [DefaultValue(1024)]
238     public int PageSize
239     {
240       get
241       {
242         if (ContainsKey("Page Size") == false) return 1024;
243         return Convert.ToInt32(this["Page Size"], CultureInfo.InvariantCulture);
244       }
245       set
246       {
247         this["Page Size"] = value;
248       }
249     }
250
251     /// <summary>
252     /// Gets/Sets the cache size for the connection.
253     /// </summary>
254     [DisplayName("Cache Size")]
255     [Browsable(true)]
256     [DefaultValue(2000)]
257     public int CacheSize
258     {
259       get
260       {
261         if (ContainsKey("Cache Size") == false) return 2000;
262         return Convert.ToInt32(this["Cache Size"], CultureInfo.InvariantCulture);
263       }
264       set
265       {
266         this["Cache Size"] = value;
267       }
268     }
269
270     /// <summary>
271     /// Gets/Sets the datetime format for the connection.
272     /// </summary>
273     [Browsable(true)]
274     [DefaultValue(SqliteDateFormats.ISO8601)]
275     public SqliteDateFormats DateTimeFormat
276     {
277       get
278       {
279         if (ContainsKey("DateTimeFormat") == false) return SqliteDateFormats.ISO8601;
280
281         return (SqliteDateFormats)TypeDescriptor.GetConverter(typeof(SqliteDateFormats)).ConvertFrom(this["DateTimeFormat"]);
282       }
283       set
284       {
285         this["DateTimeFormat"] = value;
286       }
287     }
288
289     /// <summary>
290     /// Helper function for retrieving values from the connectionstring
291     /// </summary>
292     /// <param name="keyword">The keyword to retrieve settings for</param>
293     /// <param name="value">The resulting parameter value</param>
294     /// <returns>Returns true if the value was found and returned</returns>
295     public override bool TryGetValue(string keyword, out object value)
296     {
297       bool b = base.TryGetValue(keyword, out value);
298
299       if (!_properties.ContainsKey(keyword)) return b;
300
301       PropertyDescriptor pd = _properties[keyword] as PropertyDescriptor;
302
303       if (pd == null) return b;
304
305       if (b)
306       {
307         value = TypeDescriptor.GetConverter(pd.PropertyType).ConvertFrom(value);
308       }
309       else
310       {
311         DefaultValueAttribute att = pd.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
312         if (att != null)
313         {
314           value = att.Value;
315           b = true;
316         }
317       }
318       return b;
319     }
320   }
321 #endif
322 }
323 #endif