1 /********************************************************
\r
2 * ADO.NET 2.0 Data Provider for SQLite Version 3.X
\r
3 * Written by Robert Simpson (robert@blackcastlesoft.com)
\r
5 * Released to the public domain, use at your own risk!
\r
6 ********************************************************/
\r
8 namespace Mono.Data.Sqlite
\r
12 using System.Data.Common;
\r
13 using System.ComponentModel;
\r
14 using System.Collections;
\r
15 using System.Globalization;
\r
16 using System.Reflection;
\r
18 #if !PLATFORM_COMPACTFRAMEWORK
\r
19 using System.ComponentModel.Design;
\r
22 /// SQLite implementation of DbConnectionStringBuilder.
\r
24 [DefaultProperty("DataSource")]
\r
25 [DefaultMember("Item")]
\r
26 public sealed class SqliteConnectionStringBuilder : DbConnectionStringBuilder
\r
29 /// Properties of this class
\r
31 private Hashtable _properties;
\r
34 /// Constructs a new instance of the class
\r
37 /// Default constructor
\r
39 public SqliteConnectionStringBuilder()
\r
45 /// Constructs a new instance of the class using the specified connection string.
\r
47 /// <param name="connectionString">The connection string to parse</param>
\r
48 public SqliteConnectionStringBuilder(string connectionString)
\r
50 Initialize(connectionString);
\r
54 /// Private initializer, which assigns the connection string and resets the builder
\r
56 /// <param name="cnnString">The connection string to assign</param>
\r
57 private void Initialize(string cnnString)
\r
59 _properties = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
\r
62 base.GetProperties(_properties);
\r
64 catch(NotImplementedException)
\r
66 FallbackGetProperties(_properties);
\r
69 if (String.IsNullOrEmpty(cnnString) == false)
\r
70 ConnectionString = cnnString;
\r
74 /// 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.
\r
83 TryGetValue("version", out value);
\r
84 return Convert.ToInt32(value, CultureInfo.CurrentCulture);
\r
89 throw new NotSupportedException();
\r
91 this["version"] = value;
\r
96 /// Gets/Sets the synchronization mode (file flushing) of the connection string. Default is "Normal".
\r
98 [DisplayName("Synchronous")]
\r
100 [DefaultValue(SynchronizationModes.Normal)]
\r
101 public SynchronizationModes SyncMode
\r
106 TryGetValue("synchronous", out value);
\r
107 if (value is string)
\r
108 return (SynchronizationModes)TypeDescriptor.GetConverter(typeof(SynchronizationModes)).ConvertFrom(value);
\r
109 else return (SynchronizationModes)value;
\r
113 this["synchronous"] = value;
\r
118 /// Gets/Sets the encoding for the connection string. The default is "False" which indicates UTF-8 encoding.
\r
121 [DefaultValue(false)]
\r
122 public bool UseUTF16Encoding
\r
127 TryGetValue("useutf16encoding", out value);
\r
128 return SqliteConvert.ToBoolean(value);
\r
132 this["useutf16encoding"] = value;
\r
137 /// Gets/Sets whether or not to use connection pooling. The default is "False"
\r
140 [DefaultValue(false)]
\r
141 public bool Pooling
\r
146 TryGetValue("pooling", out value);
\r
147 return SqliteConvert.ToBoolean(value);
\r
151 this["pooling"] = value;
\r
156 /// Gets/Sets whethor not to store GUID's in binary format. The default is True
\r
157 /// which saves space in the database.
\r
160 [DefaultValue(true)]
\r
161 public bool BinaryGUID
\r
166 TryGetValue("binaryguid", out value);
\r
167 return SqliteConvert.ToBoolean(value);
\r
171 this["binaryguid"] = value;
\r
176 /// Gets/Sets the filename to open on the connection string.
\r
178 [DisplayName("Data Source")]
\r
181 public string DataSource
\r
186 TryGetValue("data source", out value);
\r
187 return value.ToString();
\r
191 this["data source"] = value;
\r
196 /// An alternate to the data source property
\r
204 TryGetValue("uri", out value);
\r
205 return value.ToString();
\r
209 this["uri"] = value;
\r
214 /// Gets/sets the default command timeout for newly-created commands. This is especially useful for
\r
215 /// commands used internally such as inside a SqliteTransaction, where setting the timeout is not possible.
\r
217 [DisplayName("Default Timeout")]
\r
220 public int DefaultTimeout
\r
225 TryGetValue("default timeout", out value);
\r
226 return Convert.ToInt32(value, CultureInfo.CurrentCulture);
\r
230 this["default timeout"] = value;
\r
235 /// Determines whether or not the connection will automatically participate
\r
236 /// in the current distributed transaction (if one exists)
\r
239 [DefaultValue(true)]
\r
245 TryGetValue("enlist", out value);
\r
246 return SqliteConvert.ToBoolean(value);
\r
250 this["enlist"] = value;
\r
255 /// If set to true, will throw an exception if the database specified in the connection
\r
256 /// string does not exist. If false, the database will be created automatically.
\r
259 [DefaultValue(false)]
\r
260 public bool FailIfMissing
\r
265 TryGetValue("failifmissing", out value);
\r
266 return SqliteConvert.ToBoolean(value);
\r
270 this["failifmissing"] = value;
\r
275 /// If enabled, uses the legacy 3.xx format for maximum compatibility, but results in larger
\r
276 /// database sizes.
\r
278 [DisplayName("Legacy Format")]
\r
280 [DefaultValue(false)]
\r
281 public bool LegacyFormat
\r
286 TryGetValue("legacy format", out value);
\r
287 return SqliteConvert.ToBoolean(value);
\r
291 this["legacy format"] = value;
\r
296 /// When enabled, the database will be opened for read-only access and writing will be disabled.
\r
298 [DisplayName("Read Only")]
\r
300 [DefaultValue(false)]
\r
301 public bool ReadOnly
\r
306 TryGetValue("read only", out value);
\r
307 return SqliteConvert.ToBoolean(value);
\r
311 this["read only"] = value;
\r
316 /// Gets/sets the database encryption password
\r
319 [PasswordPropertyText(true)]
\r
321 public string Password
\r
326 TryGetValue("password", out value);
\r
327 return value.ToString();
\r
331 this["password"] = value;
\r
336 /// Gets/Sets the page size for the connection.
\r
338 [DisplayName("Page Size")]
\r
340 [DefaultValue(1024)]
\r
341 public int PageSize
\r
346 TryGetValue("page size", out value);
\r
347 return Convert.ToInt32(value, CultureInfo.CurrentCulture);
\r
351 this["page size"] = value;
\r
356 /// Gets/Sets the maximum number of pages the database may hold
\r
358 [DisplayName("Max Page Count")]
\r
361 public int MaxPageCount
\r
366 TryGetValue("max page count", out value);
\r
367 return Convert.ToInt32(value, CultureInfo.CurrentCulture);
\r
371 this["max page count"] = value;
\r
376 /// Gets/Sets the cache size for the connection.
\r
378 [DisplayName("Cache Size")]
\r
380 [DefaultValue(2000)]
\r
381 public int CacheSize
\r
386 TryGetValue("cache size", out value);
\r
387 return Convert.ToInt32(value, CultureInfo.CurrentCulture);
\r
391 this["cache size"] = value;
\r
396 /// Gets/Sets the datetime format for the connection.
\r
399 [DefaultValue(SQLiteDateFormats.ISO8601)]
\r
400 public SQLiteDateFormats DateTimeFormat
\r
405 TryGetValue("datetimeformat", out value);
\r
406 if (value is string)
\r
407 return (SQLiteDateFormats)TypeDescriptor.GetConverter(typeof(SQLiteDateFormats)).ConvertFrom(value);
\r
408 else return (SQLiteDateFormats)value;
\r
412 this["datetimeformat"] = value;
\r
417 /// Determines how SQLite handles the transaction journal file.
\r
420 [DefaultValue(SQLiteJournalModeEnum.Delete)]
\r
421 [DisplayName("Journal Mode")]
\r
422 public SQLiteJournalModeEnum JournalMode
\r
427 TryGetValue("journal mode", out value);
\r
428 if (value is string)
\r
429 return (SQLiteJournalModeEnum)TypeDescriptor.GetConverter(typeof(SQLiteJournalModeEnum)).ConvertFrom(value);
\r
431 return (SQLiteJournalModeEnum)value;
\r
435 this["journal mode"] = value;
\r
440 /// Sets the default isolation level for transactions on the connection.
\r
443 [DefaultValue(IsolationLevel.Serializable)]
\r
444 [DisplayName("Default Isolation Level")]
\r
445 public IsolationLevel DefaultIsolationLevel
\r
450 TryGetValue("default isolationlevel", out value);
\r
451 if (value is string)
\r
452 return (IsolationLevel)TypeDescriptor.GetConverter(typeof(IsolationLevel)).ConvertFrom(value);
\r
454 return (IsolationLevel)value;
\r
458 this["default isolationlevel"] = value;
\r
463 /// Helper function for retrieving values from the connectionstring
\r
465 /// <param name="keyword">The keyword to retrieve settings for</param>
\r
466 /// <param name="value">The resulting parameter value</param>
\r
467 /// <returns>Returns true if the value was found and returned</returns>
\r
468 public override bool TryGetValue(string keyword, out object value)
\r
470 bool b = base.TryGetValue(keyword, out value);
\r
472 if (!_properties.ContainsKey(keyword)) return b;
\r
474 PropertyDescriptor pd = _properties[keyword] as PropertyDescriptor;
\r
476 if (pd == null) return b;
\r
478 // Attempt to coerce the value into something more solid
\r
481 if (pd.PropertyType == typeof(Boolean))
\r
482 value = SqliteConvert.ToBoolean(value);
\r
484 value = TypeDescriptor.GetConverter(pd.PropertyType).ConvertFrom(value);
\r
488 DefaultValueAttribute att = pd.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
\r
499 /// Fallback method for MONO, which doesn't implement DbConnectionStringBuilder.GetProperties()
\r
501 /// <param name="propertyList">The hashtable to fill with property descriptors</param>
\r
502 private void FallbackGetProperties(Hashtable propertyList)
\r
504 foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(this, true))
\r
506 if (descriptor.Name != "ConnectionString" && propertyList.ContainsKey(descriptor.DisplayName) == false)
\r
508 propertyList.Add(descriptor.DisplayName, descriptor);
\r