/******************************************************** * ADO.NET 2.0 Data Provider for SQLite Version 3.X * Written by Robert Simpson (robert@blackcastlesoft.com) * * Released to the public domain, use at your own risk! ********************************************************/ namespace System.Data.SQLite { using System; using System.Data.Common; using System.ComponentModel; using System.Collections; using System.Globalization; using System.Reflection; #if !PLATFORM_COMPACTFRAMEWORK using System.ComponentModel.Design; /// /// SQLite implementation of DbConnectionStringBuilder. /// [DefaultProperty("DataSource")] [DefaultMember("Item")] public sealed class SQLiteConnectionStringBuilder : DbConnectionStringBuilder { /// /// Properties of this class /// private Hashtable _properties; /// /// Constructs a new instance of the class /// /// /// Default constructor /// public SQLiteConnectionStringBuilder() { Initialize(null); } /// /// Constructs a new instance of the class using the specified connection string. /// /// The connection string to parse public SQLiteConnectionStringBuilder(string connectionString) { Initialize(connectionString); } /// /// Private initializer, which assigns the connection string and resets the builder /// /// The connection string to assign private void Initialize(string cnnString) { _properties = new Hashtable(); base.GetProperties(_properties); if (String.IsNullOrEmpty(cnnString) == false) ConnectionString = cnnString; } /// /// 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. /// [Browsable(true)] [DefaultValue(3)] public int Version { get { object value; TryGetValue("Version", out value); return Convert.ToInt32(value, CultureInfo.CurrentCulture); } set { if (value != 3) throw new NotSupportedException(); this["Version"] = value; } } /// /// Gets/Sets the synchronous mode of the connection string. Default is "Normal". /// [DisplayName("Synchronous")] [Browsable(true)] [DefaultValue(SynchronizationModes.Normal)] public SynchronizationModes SyncMode { get { object value; TryGetValue("Synchronous", out value); if (value is string) return (SynchronizationModes)TypeDescriptor.GetConverter(typeof(SynchronizationModes)).ConvertFrom(value); else return (SynchronizationModes)value; } set { this["Synchronous"] = value; } } /// /// Gets/Sets the encoding for the connection string. The default is "False" which indicates UTF-8 encoding. /// [Browsable(true)] [DefaultValue(false)] public bool UseUTF16Encoding { get { object value; TryGetValue("UseUTF16Encoding", out value); return Convert.ToBoolean(value, CultureInfo.CurrentCulture); } set { this["UseUTF16Encoding"] = value; } } /// /// Gets/Sets whether or not to use connection pooling. The default is "False" /// [Browsable(true)] [DefaultValue(false)] public bool Pooling { get { object value; TryGetValue("Pooling", out value); return Convert.ToBoolean(value, CultureInfo.CurrentCulture); } set { this["Pooling"] = value; } } /// /// Gets/Sets whethor not to store GUID's in binary format. The default is True /// which saves space in the database. /// [Browsable(true)] [DefaultValue(true)] public bool BinaryGUID { get { object value; TryGetValue("BinaryGUID", out value); return Convert.ToBoolean(value, CultureInfo.CurrentCulture); } set { this["BinaryGUID"] = value; } } /// /// Gets/Sets the filename to open on the connection string. /// [DisplayName("Data Source")] [Browsable(true)] [DefaultValue("")] public string DataSource { get { object value; TryGetValue("Data Source", out value); return value.ToString(); } set { this["Data Source"] = value; } } /// /// Gets/sets the default command timeout for newly-created commands. This is especially useful for /// commands used internally such as inside a SQLiteTransaction, where setting the timeout is not possible. /// [DisplayName("Default Timeout")] [Browsable(true)] [DefaultValue(30)] public int DefaultTimeout { get { object value; TryGetValue("Default Timeout", out value); return Convert.ToInt32(value, CultureInfo.CurrentCulture); } set { this["Default Timeout"] = value; } } /// /// Determines whether or not the connection will automatically participate /// in the current distributed transaction (if one exists) /// [Browsable(true)] [DefaultValue(true)] public bool Enlist { get { object value; TryGetValue("Enlist", out value); return Convert.ToBoolean(value, CultureInfo.CurrentCulture); } set { this["Enlist"] = value; } } /// /// If set to true, will throw an exception if the database specified in the connection /// string does not exist. If false, the database will be created automatically. /// [Browsable(true)] [DefaultValue(false)] [DisplayName("Fail If Missing")] public bool FailIfMissing { get { object value; TryGetValue("FailIfMissing", out value); return Convert.ToBoolean(value, CultureInfo.CurrentCulture); } set { this["FailIfMissing"] = value; } } /// /// If enabled, uses the legacy 3.xx format for maximum compatibility, but results in larger /// database sizes. /// [DisplayName("Legacy Format")] [Browsable(true)] [DefaultValue(true)] public bool LegacyFormat { get { object value; TryGetValue("Legacy Format", out value); return Convert.ToBoolean(value, CultureInfo.CurrentCulture); } set { this["Legacy Format"] = value; } } /// /// Gets/sets the database encryption password /// [Browsable(true)] [PasswordPropertyText(true)] [DefaultValue("")] public string Password { get { object value; TryGetValue("Password", out value); return value.ToString(); } set { this["Password"] = value; } } /// /// Gets/Sets the page size for the connection. /// [DisplayName("Page Size")] [Browsable(true)] [DefaultValue(1024)] public int PageSize { get { object value; TryGetValue("Page Size", out value); return Convert.ToInt32(value, CultureInfo.CurrentCulture); } set { this["Page Size"] = value; } } /// /// Gets/Sets the maximum number of pages the database may hold /// [DisplayName("Max Page Count")] [Browsable(true)] [DefaultValue(0)] public int MaxPageCount { get { object value; TryGetValue("Max Page Count", out value); return Convert.ToInt32(value, CultureInfo.CurrentCulture); } set { this["Max Page Count"] = value; } } /// /// Gets/Sets the cache size for the connection. /// [DisplayName("Cache Size")] [Browsable(true)] [DefaultValue(2000)] public int CacheSize { get { object value; TryGetValue("Cache Size", out value); return Convert.ToInt32(value, CultureInfo.CurrentCulture); } set { this["Cache Size"] = value; } } /// /// Gets/Sets the datetime format for the connection. /// [Browsable(true)] [DefaultValue(SQLiteDateFormats.ISO8601)] public SQLiteDateFormats DateTimeFormat { get { object value; TryGetValue("DateTimeFormat", out value); if (value is string) return (SQLiteDateFormats)TypeDescriptor.GetConverter(typeof(SQLiteDateFormats)).ConvertFrom(value); else return (SQLiteDateFormats)value; } set { this["DateTimeFormat"] = value; } } /// /// Helper function for retrieving values from the connectionstring /// /// The keyword to retrieve settings for /// The resulting parameter value /// Returns true if the value was found and returned public override bool TryGetValue(string keyword, out object value) { bool b = base.TryGetValue(keyword, out value); if (!_properties.ContainsKey(keyword)) return b; PropertyDescriptor pd = _properties[keyword] as PropertyDescriptor; if (pd == null) return b; if (b) { value = TypeDescriptor.GetConverter(pd.PropertyType).ConvertFrom(value); } else { DefaultValueAttribute att = pd.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute; if (att != null) { value = att.Value; b = true; } } return b; } } #endif }