2 // Mono.Data.Sqlite.SQLiteConnectionStringBuilder.cs
5 // Robert Simpson (robert@blackcastlesoft.com)
7 // Adapted and modified for the Mono Project by
8 // Marek Habersack (grendello@gmail.com)
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 /********************************************************
35 * ADO.NET 2.0 Data Provider for Sqlite Version 3.X
36 * Written by Robert Simpson (robert@blackcastlesoft.com)
38 * Released to the public domain, use at your own risk!
39 ********************************************************/
41 namespace Mono.Data.Sqlite
44 using System.Data.Common;
45 using System.ComponentModel;
46 using System.Collections;
47 using System.Globalization;
48 using System.Reflection;
50 #if !PLATFORM_COMPACTFRAMEWORK
51 using System.ComponentModel.Design;
54 /// Sqlite implementation of DbConnectionStringBuilder.
56 [DefaultProperty("DataSource")]
57 [DefaultMember("Item")]
58 public sealed class SqliteConnectionStringBuilder : DbConnectionStringBuilder
61 /// Properties of this class
63 private Hashtable _properties;
66 /// Constructs a new instance of the class
69 /// Default constructor
71 public SqliteConnectionStringBuilder()
77 /// Constructs a new instance of the class using the specified connection string.
79 /// <param name="connectionString">The connection string to parse</param>
80 public SqliteConnectionStringBuilder(string connectionString)
82 Initialize(connectionString);
86 /// Private initializer, which assigns the connection string and resets the builder
88 /// <param name="cnnString">The connection string to assign</param>
89 private void Initialize(string cnnString)
91 _properties = new Hashtable();
92 base.GetProperties(_properties);
94 if (String.IsNullOrEmpty(cnnString) == false)
95 ConnectionString = cnnString;
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.
107 if (ContainsKey("Version") == false) return 3;
109 return Convert.ToInt32(this["Version"], CultureInfo.CurrentCulture);
114 throw new NotSupportedException();
116 this["Version"] = value;
121 /// Gets/Sets the synchronous mode of the connection string. Default is "Normal".
123 [DisplayName("Synchronous")]
125 [DefaultValue(SynchronizationModes.Normal)]
126 public SynchronizationModes SyncMode
130 return (SynchronizationModes)TypeDescriptor.GetConverter(typeof(SynchronizationModes)).ConvertFrom(this["Synchronous"]);
134 this["Synchronous"] = value;
139 /// Gets/Sets the encoding for the connection string. The default is "False" which indicates UTF-8 encoding.
142 [DefaultValue(false)]
143 public bool UseUTF16Encoding
147 return Convert.ToBoolean(this["UseUTF16Encoding"], CultureInfo.CurrentCulture);
151 this["UseUTF16Encoding"] = value;
156 /// Gets/Sets the filename to open on the connection string.
158 [DisplayName("Data Source")]
160 public string DataSource
164 if (ContainsKey("Data Source") == false) return "";
166 return this["Data Source"].ToString();
170 this["Data Source"] = value;
174 #region Mono-specific
176 /// Gets/Sets the filename to open on the connection string (Mono-specific, uses DataSource).
178 [DisplayName("Data Source")]
194 /// Determines whether or not the connection will automatically participate
195 /// in the current distributed transaction (if one exists)
197 [DisplayName("Automatic Enlistment")]
204 if (ContainsKey("Enlist") == false) return true;
206 return (this["Enlist"].ToString() == "Y");
210 this["Enlist"] = (value == true) ? "Y" : "N";
214 /// Gets/sets the database encryption password
217 [PasswordPropertyText(true)]
218 public string Password
222 if (ContainsKey("Password") == false) return "";
224 return this["Password"].ToString();
228 this["Password"] = value;
233 /// Gets/Sets the page size for the connection.
235 [DisplayName("Page Size")]
242 if (ContainsKey("Page Size") == false) return 1024;
243 return Convert.ToInt32(this["Page Size"], CultureInfo.InvariantCulture);
247 this["Page Size"] = value;
252 /// Gets/Sets the cache size for the connection.
254 [DisplayName("Cache Size")]
261 if (ContainsKey("Cache Size") == false) return 2000;
262 return Convert.ToInt32(this["Cache Size"], CultureInfo.InvariantCulture);
266 this["Cache Size"] = value;
271 /// Gets/Sets the datetime format for the connection.
274 [DefaultValue(SqliteDateFormats.ISO8601)]
275 public SqliteDateFormats DateTimeFormat
279 if (ContainsKey("DateTimeFormat") == false) return SqliteDateFormats.ISO8601;
281 return (SqliteDateFormats)TypeDescriptor.GetConverter(typeof(SqliteDateFormats)).ConvertFrom(this["DateTimeFormat"]);
285 this["DateTimeFormat"] = value;
290 /// Helper function for retrieving values from the connectionstring
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)
297 bool b = base.TryGetValue(keyword, out value);
299 if (!_properties.ContainsKey(keyword)) return b;
301 PropertyDescriptor pd = _properties[keyword] as PropertyDescriptor;
303 if (pd == null) return b;
307 value = TypeDescriptor.GetConverter(pd.PropertyType).ConvertFrom(value);
311 DefaultValueAttribute att = pd.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;