[jit] Fix the saving of the 'cfg->ret_var_set' flag when inlining, it was set to...
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite_2.0 / SQLiteConnectionStringBuilder.cs
1 /********************************************************\r
2  * ADO.NET 2.0 Data Provider for SQLite Version 3.X\r
3  * Written by Robert Simpson (robert@blackcastlesoft.com)\r
4  * \r
5  * Released to the public domain, use at your own risk!\r
6  ********************************************************/\r
7 \r
8 namespace Mono.Data.Sqlite\r
9 {\r
10   using System;\r
11   using System.Data;\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
17 \r
18 #if !PLATFORM_COMPACTFRAMEWORK\r
19   using System.ComponentModel.Design;\r
20 \r
21   /// <summary>\r
22   /// SQLite implementation of DbConnectionStringBuilder.\r
23   /// </summary>\r
24   [DefaultProperty("DataSource")]\r
25   [DefaultMember("Item")]\r
26   public sealed class SqliteConnectionStringBuilder : DbConnectionStringBuilder\r
27   {\r
28     /// <summary>\r
29     /// Properties of this class\r
30     /// </summary>\r
31     private Hashtable _properties;\r
32 \r
33     /// <overloads>\r
34     /// Constructs a new instance of the class\r
35     /// </overloads>\r
36     /// <summary>\r
37     /// Default constructor\r
38     /// </summary>\r
39     public SqliteConnectionStringBuilder()\r
40     {\r
41       Initialize(null);\r
42     }\r
43 \r
44     /// <summary>\r
45     /// Constructs a new instance of the class using the specified connection string.\r
46     /// </summary>\r
47     /// <param name="connectionString">The connection string to parse</param>\r
48     public SqliteConnectionStringBuilder(string connectionString)\r
49     {\r
50       Initialize(connectionString);\r
51     }\r
52 \r
53     /// <summary>\r
54     /// Private initializer, which assigns the connection string and resets the builder\r
55     /// </summary>\r
56     /// <param name="cnnString">The connection string to assign</param>\r
57     private void Initialize(string cnnString)\r
58     {\r
59       _properties = new Hashtable(StringComparer.InvariantCultureIgnoreCase);\r
60       try\r
61       {\r
62         base.GetProperties(_properties);\r
63       }\r
64       catch(NotImplementedException)\r
65       {\r
66         FallbackGetProperties(_properties);\r
67       }\r
68 \r
69       if (String.IsNullOrEmpty(cnnString) == false)\r
70         ConnectionString = cnnString;\r
71     }\r
72 \r
73     /// <summary>\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
75     /// </summary>\r
76     [Browsable(true)]\r
77     [DefaultValue(3)]\r
78     public int Version\r
79     {\r
80       get\r
81       {\r
82         object value;\r
83         TryGetValue("version", out value);\r
84         return Convert.ToInt32(value, CultureInfo.CurrentCulture);\r
85       }\r
86       set\r
87       {\r
88         if (value != 3)\r
89           throw new NotSupportedException();\r
90 \r
91         this["version"] = value;\r
92       }\r
93     }\r
94 \r
95     /// <summary>\r
96     /// Gets/Sets the synchronization mode (file flushing) of the connection string.  Default is "Normal".\r
97     /// </summary>\r
98     [DisplayName("Synchronous")]\r
99     [Browsable(true)]\r
100     [DefaultValue(SynchronizationModes.Normal)]\r
101     public SynchronizationModes SyncMode\r
102     {\r
103       get\r
104       {\r
105         object value;\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
110       }\r
111       set\r
112       {\r
113         this["synchronous"] = value;\r
114       }\r
115     }\r
116 \r
117     /// <summary>\r
118     /// Gets/Sets the encoding for the connection string.  The default is "False" which indicates UTF-8 encoding.\r
119     /// </summary>\r
120     [Browsable(true)]\r
121     [DefaultValue(false)]\r
122     public bool UseUTF16Encoding\r
123     {\r
124       get\r
125       {\r
126         object value;\r
127         TryGetValue("useutf16encoding", out value);\r
128         return SqliteConvert.ToBoolean(value);\r
129       }\r
130       set\r
131       {\r
132         this["useutf16encoding"] = value;\r
133       }\r
134     }\r
135 \r
136     /// <summary>\r
137     /// Gets/Sets whether or not to use connection pooling.  The default is "False"\r
138     /// </summary>\r
139     [Browsable(true)]\r
140     [DefaultValue(false)]\r
141     public bool Pooling\r
142     {\r
143       get\r
144       {\r
145         object value;\r
146         TryGetValue("pooling", out value);\r
147         return SqliteConvert.ToBoolean(value);\r
148       }\r
149       set\r
150       {\r
151         this["pooling"] = value;\r
152       }\r
153     }\r
154 \r
155     /// <summary>\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
158     /// </summary>\r
159     [Browsable(true)]\r
160     [DefaultValue(true)]\r
161     public bool BinaryGUID\r
162     {\r
163       get\r
164       {\r
165         object value;\r
166         TryGetValue("binaryguid", out value);\r
167         return SqliteConvert.ToBoolean(value);\r
168       }\r
169       set\r
170       {\r
171         this["binaryguid"] = value;\r
172       }\r
173     }\r
174 \r
175     /// <summary>\r
176     /// Gets/Sets the filename to open on the connection string.\r
177     /// </summary>\r
178     [DisplayName("Data Source")]\r
179     [Browsable(true)]\r
180     [DefaultValue("")]\r
181     public string DataSource\r
182     {\r
183       get\r
184       {\r
185         object value;\r
186         TryGetValue("data source", out value);\r
187         return value.ToString();\r
188       }\r
189       set\r
190       {\r
191         this["data source"] = value;\r
192       }\r
193     }\r
194 \r
195     /// <summary>\r
196     /// An alternate to the data source property\r
197     /// </summary>\r
198     [Browsable(false)]\r
199     public string Uri\r
200     {\r
201       get\r
202       {\r
203         object value;\r
204         TryGetValue("uri", out value);\r
205         return value.ToString();\r
206       }\r
207       set\r
208       {\r
209         this["uri"] = value;\r
210       }\r
211     }\r
212 \r
213     /// <summary>\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
216     /// </summary>\r
217     [DisplayName("Default Timeout")]\r
218     [Browsable(true)]\r
219     [DefaultValue(30)]\r
220     public int DefaultTimeout\r
221     {\r
222       get\r
223       {\r
224         object value;\r
225         TryGetValue("default timeout", out value);\r
226         return Convert.ToInt32(value, CultureInfo.CurrentCulture);\r
227       }\r
228       set\r
229       {\r
230         this["default timeout"] = value;\r
231       }\r
232     }\r
233 \r
234     /// <summary>\r
235     /// Determines whether or not the connection will automatically participate\r
236     /// in the current distributed transaction (if one exists)\r
237     /// </summary>\r
238     [Browsable(true)]\r
239     [DefaultValue(true)]\r
240     public bool Enlist\r
241     {\r
242       get\r
243       {\r
244         object value;\r
245         TryGetValue("enlist", out value);\r
246         return SqliteConvert.ToBoolean(value);\r
247       }\r
248       set\r
249       {\r
250         this["enlist"] = value;\r
251       }\r
252     }\r
253 \r
254     /// <summary>\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
257     /// </summary>\r
258     [Browsable(true)]\r
259     [DefaultValue(false)]\r
260     public bool FailIfMissing\r
261     {\r
262       get\r
263       {\r
264         object value;\r
265         TryGetValue("failifmissing", out value);\r
266         return SqliteConvert.ToBoolean(value);\r
267       }\r
268       set\r
269       {\r
270         this["failifmissing"] = value;\r
271       }\r
272     }\r
273 \r
274     /// <summary>\r
275     /// If enabled, uses the legacy 3.xx format for maximum compatibility, but results in larger\r
276     /// database sizes.\r
277     /// </summary>\r
278     [DisplayName("Legacy Format")]\r
279     [Browsable(true)]\r
280     [DefaultValue(false)]\r
281     public bool LegacyFormat\r
282     {\r
283       get\r
284       {\r
285         object value;\r
286         TryGetValue("legacy format", out value);\r
287         return SqliteConvert.ToBoolean(value);\r
288       }\r
289       set\r
290       {\r
291         this["legacy format"] = value;\r
292       }\r
293     }\r
294 \r
295     /// <summary>\r
296     /// When enabled, the database will be opened for read-only access and writing will be disabled.\r
297     /// </summary>\r
298     [DisplayName("Read Only")]\r
299     [Browsable(true)]\r
300     [DefaultValue(false)]\r
301     public bool ReadOnly\r
302     {\r
303       get\r
304       {\r
305         object value;\r
306         TryGetValue("read only", out value);\r
307         return SqliteConvert.ToBoolean(value);\r
308       }\r
309       set\r
310       {\r
311         this["read only"] = value;\r
312       }\r
313     }\r
314 \r
315     /// <summary>\r
316     /// Gets/sets the database encryption password\r
317     /// </summary>\r
318     [Browsable(true)]\r
319     [PasswordPropertyText(true)]\r
320     [DefaultValue("")]\r
321     public string Password\r
322     {\r
323       get\r
324       {\r
325         object value;\r
326         TryGetValue("password", out value);\r
327         return value.ToString();\r
328       }\r
329       set\r
330       {\r
331         this["password"] = value;\r
332       }\r
333     }\r
334 \r
335     /// <summary>\r
336     /// Gets/Sets the page size for the connection.\r
337     /// </summary>\r
338     [DisplayName("Page Size")]\r
339     [Browsable(true)]\r
340     [DefaultValue(1024)]\r
341     public int PageSize\r
342     {\r
343       get\r
344       {\r
345         object value;\r
346         TryGetValue("page size", out value);\r
347         return Convert.ToInt32(value, CultureInfo.CurrentCulture);\r
348       }\r
349       set\r
350       {\r
351         this["page size"] = value;\r
352       }\r
353     }\r
354 \r
355     /// <summary>\r
356     /// Gets/Sets the maximum number of pages the database may hold\r
357     /// </summary>\r
358     [DisplayName("Max Page Count")]\r
359     [Browsable(true)]\r
360     [DefaultValue(0)]\r
361     public int MaxPageCount\r
362     {\r
363       get\r
364       {\r
365         object value;\r
366         TryGetValue("max page count", out value);\r
367         return Convert.ToInt32(value, CultureInfo.CurrentCulture);\r
368       }\r
369       set\r
370       {\r
371         this["max page count"] = value;\r
372       }\r
373     }\r
374 \r
375     /// <summary>\r
376     /// Gets/Sets the cache size for the connection.\r
377     /// </summary>\r
378     [DisplayName("Cache Size")]\r
379     [Browsable(true)]\r
380     [DefaultValue(2000)]\r
381     public int CacheSize\r
382     {\r
383       get\r
384       {\r
385         object value;\r
386         TryGetValue("cache size", out value);\r
387         return Convert.ToInt32(value, CultureInfo.CurrentCulture);\r
388       }\r
389       set\r
390       {\r
391         this["cache size"] = value;\r
392       }\r
393     }\r
394 \r
395     /// <summary>\r
396     /// Gets/Sets the datetime format for the connection.\r
397     /// </summary>\r
398     [Browsable(true)]\r
399     [DefaultValue(SQLiteDateFormats.ISO8601)]\r
400     public SQLiteDateFormats DateTimeFormat\r
401     {\r
402       get\r
403       {\r
404         object value;\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
409       }\r
410       set\r
411       {\r
412         this["datetimeformat"] = value;\r
413       }\r
414     }\r
415 \r
416     /// <summary>\r
417     /// Determines how SQLite handles the transaction journal file.\r
418     /// </summary>\r
419     [Browsable(true)]\r
420     [DefaultValue(SQLiteJournalModeEnum.Delete)]\r
421     [DisplayName("Journal Mode")]\r
422     public SQLiteJournalModeEnum JournalMode\r
423     {\r
424       get\r
425       {\r
426         object value;\r
427         TryGetValue("journal mode", out value);\r
428         if (value is string)\r
429           return (SQLiteJournalModeEnum)TypeDescriptor.GetConverter(typeof(SQLiteJournalModeEnum)).ConvertFrom(value);\r
430         else\r
431           return (SQLiteJournalModeEnum)value;\r
432       }\r
433       set\r
434       {\r
435         this["journal mode"] = value;\r
436       }\r
437     }\r
438 \r
439     /// <summary>\r
440     /// Sets the default isolation level for transactions on the connection.\r
441     /// </summary>\r
442     [Browsable(true)]\r
443     [DefaultValue(IsolationLevel.Serializable)]\r
444     [DisplayName("Default Isolation Level")]\r
445     public IsolationLevel DefaultIsolationLevel\r
446     {\r
447       get\r
448       {\r
449         object value;\r
450         TryGetValue("default isolationlevel", out value);\r
451         if (value is string)\r
452           return (IsolationLevel)TypeDescriptor.GetConverter(typeof(IsolationLevel)).ConvertFrom(value);\r
453         else\r
454           return (IsolationLevel)value;\r
455       }\r
456       set\r
457       {\r
458         this["default isolationlevel"] = value;\r
459       }\r
460     }\r
461 \r
462     /// <summary>\r
463     /// Helper function for retrieving values from the connectionstring\r
464     /// </summary>\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
469     {\r
470       bool b = base.TryGetValue(keyword, out value);\r
471 \r
472       if (!_properties.ContainsKey(keyword)) return b;\r
473 \r
474       PropertyDescriptor pd = _properties[keyword] as PropertyDescriptor;\r
475 \r
476       if (pd == null) return b;\r
477 \r
478       // Attempt to coerce the value into something more solid\r
479       if (b)\r
480       {\r
481         if (pd.PropertyType == typeof(Boolean))\r
482           value = SqliteConvert.ToBoolean(value);\r
483         else\r
484           value = TypeDescriptor.GetConverter(pd.PropertyType).ConvertFrom(value);\r
485       }\r
486       else\r
487       {\r
488         DefaultValueAttribute att = pd.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;\r
489         if (att != null)\r
490         {\r
491           value = att.Value;\r
492           b = true;\r
493         }\r
494       }\r
495       return b;\r
496     }\r
497 \r
498     /// <summary>\r
499     /// Fallback method for MONO, which doesn't implement DbConnectionStringBuilder.GetProperties()\r
500     /// </summary>\r
501     /// <param name="propertyList">The hashtable to fill with property descriptors</param>\r
502     private void FallbackGetProperties(Hashtable propertyList)\r
503     {\r
504       foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(this, true))\r
505       {\r
506         if (descriptor.Name != "ConnectionString" && propertyList.ContainsKey(descriptor.DisplayName) == false)\r
507         {\r
508           propertyList.Add(descriptor.DisplayName, descriptor);\r
509         }\r
510       }\r
511     }\r
512   }\r
513 #endif\r
514 }\r