wasCSharpSQLite – Blame information for rev 3

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // Community.CsharpSqlite.SQLiteClient.SqliteConnectionStringBuilder.cs
3 //
4 // Author(s):
5 // Sureshkumar T (tsureshkumar@novell.com)
6 // Marek Habersack (grendello@gmail.com)
7 // Stewart Adcock (stewart.adcock@medit.fr)
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2007 Marek Habersack
11 // Copyright (C) 2012 MEDIT SA (http://medit-pharma.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32  
33 using System;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.Data;
37 using System.Data.Common;
38 using System.Globalization;
39 using System.Text;
40  
41 namespace Community.CsharpSqlite.SQLiteClient
42 {
43 public sealed class SqliteConnectionStringBuilder : DbConnectionStringBuilder
44 {
45 private const string DEF_URI = null;
46 private const Int32 DEF_MODE = 0644;
47 private const Int32 DEF_VERSION = 3;
48 private const Encoding DEF_ENCODING = null;
49 private const Int32 DEF_BUSYTIMEOUT = 0;
50 private const bool DEF_READONLY = false;
51 private const bool DEF_FAILIFMISSING = false;
52  
53 #region // Fields
54 private string _uri;
55 private Int32 _mode;
56 private Int32 _version;
57 private Encoding _encoding;
58 private Int32 _busy_timeout;
59 private bool _readonly;
60 private bool _failIfMissing;
61  
62 private static Dictionary <string, string> _keywords; // for mapping duplicate keywords
63 #endregion // Fields
64  
65 #region Constructors
66 public SqliteConnectionStringBuilder () : this (String.Empty)
67 {
68 }
69  
70 public SqliteConnectionStringBuilder (string connectionString)
71 {
72 Init ();
73 base.ConnectionString = connectionString;
74 }
75  
76 static SqliteConnectionStringBuilder ()
77 {
78 _keywords = new Dictionary <string, string> ();
79 _keywords ["URI"] = "Uri";
80 _keywords ["DATA SOURCE"] = "Data Source";
81 _keywords ["DATASOURCE"] = "Data Source";
82 _keywords ["URI"] = "Data Source";
83 _keywords ["MODE"] = "Mode";
84 _keywords ["VERSION"] = "Version";
85 _keywords ["BUSY TIMEOUT"] = "Busy Timeout";
86 _keywords ["BUSYTIMEOUT"] = "Busy Timeout";
87 _keywords ["ENCODING"] = "Encoding";
88 _keywords ["READ ONLY"] = "Read Only";
89 _keywords ["READONLY"] = "Read Only";
90 _keywords ["FAILIFMISSING"] = "FailIfMissing";
91 }
92 #endregion // Constructors
93  
94 #region Properties
95 public string DataSource {
96 get { return _uri; }
97 set {
98 base ["Data Source"] = value;
99 _uri = value;
100 }
101 }
102  
103 public string Uri {
104 get { return _uri; }
105 set {
106 base ["Data Source"] = value;
107 _uri = value;
108 }
109 }
110  
111 public Int32 Mode {
112 get { return _mode; }
113 set {
114 base ["Mode"] = value;
115 _mode = value;
116 }
117 }
118  
119 public Int32 Version {
120 get { return _version; }
121 set {
122 base ["Version"] = value;
123 _version = value;
124 }
125 }
126  
127 public Int32 BusyTimeout {
128 get { return _busy_timeout; }
129 set {
130 base ["Busy Timeout"] = value;
131 _busy_timeout = value;
132 }
133 }
134  
135 public Encoding Encoding {
136 get { return _encoding; }
137 set {
138 base ["Encoding"] = value;
139 _encoding = value;
140 }
141 }
142  
143 public override bool IsFixedSize {
144 get { return true; }
145 }
146  
147 public bool ReadOnly {
148 get { return _readonly; }
149 set {
150 base ["Read Only"] = value;
151 _readonly = value;
152 }
153 }
154  
155 public bool FailIfMissing {
156 get { return _failIfMissing; }
157 set {
158 base ["FailIfMissing"] = value;
159 _failIfMissing = value;
160 }
161 }
162  
163 public override object this [string keyword] {
164 get {
165 string mapped = MapKeyword (keyword);
166 return base [mapped];
167 }
168 set {SetValue (keyword, value);}
169 }
170  
171 public override ICollection Keys {
172 get { return base.Keys; }
173 }
174  
175 public override ICollection Values {
176 get { return base.Values; }
177 }
178 #endregion // Properties
179  
180 #region Methods
181 private void Init ()
182 {
183 _uri = DEF_URI;
184 _mode = DEF_MODE;
185 _version = DEF_VERSION;
186 _encoding = DEF_ENCODING;
187 _busy_timeout = DEF_BUSYTIMEOUT;
188 _readonly = DEF_READONLY;
189 _failIfMissing = DEF_FAILIFMISSING;
190 }
191  
192 public override void Clear ()
193 {
194 base.Clear ();
195 Init ();
196 }
197  
198 public override bool ContainsKey (string keyword)
199 {
200 keyword = keyword.ToUpper ().Trim ();
201 if (_keywords.ContainsKey (keyword))
202 return base.ContainsKey (_keywords [keyword]);
203 return false;
204 }
205  
206 public override bool Remove (string keyword)
207 {
208 if (!ContainsKey (keyword))
209 return false;
210 this [keyword] = null;
211 return true;
212 }
213  
214 public override bool TryGetValue (string keyword, out object value)
215 {
216 if (! ContainsKey (keyword)) {
217 value = String.Empty;
218 return false;
219 }
220 return base.TryGetValue (_keywords [keyword.ToUpper ().Trim ()], out value);
221 }
222  
223 #endregion // Methods
224  
225 #region Private Methods
226 private string MapKeyword (string keyword)
227 {
228 keyword = keyword.ToUpper ().Trim ();
229 if (! _keywords.ContainsKey (keyword))
230 throw new ArgumentException("Keyword not supported :" + keyword);
231 return _keywords [keyword];
232 }
233  
234 private void SetValue (string key, object value)
235 {
236 if (key == null)
237 throw new ArgumentNullException ("key cannot be null!");
238  
239 string mappedKey = MapKeyword (key);
240  
241 switch (mappedKey.ToUpper (CultureInfo.InvariantCulture).Trim ()) {
242 case "DATA SOURCE":
243 if (value == null) {
244 _uri = DEF_URI;
245 base.Remove (mappedKey);
246 } else
247 this.Uri = value.ToString ();
248 break;
249  
250 case "MODE":
251 if (value == null) {
252 _mode = DEF_MODE;
253 base.Remove (mappedKey);
254 } else
255 this.Mode = ConvertToInt32 (value);
256 break;
257  
258 case "VERSION":
259 if (value == null) {
260 _version = DEF_MODE;
261 base.Remove (mappedKey);
262 } else
263 this.Version = ConvertToInt32 (value);
264 break;
265  
266 case "BUSY TIMEOUT":
267 if (value == null) {
268 _busy_timeout = DEF_BUSYTIMEOUT;
269 base.Remove (mappedKey);
270 } else
271 this.BusyTimeout = ConvertToInt32 (value);
272 break;
273  
274 case "ENCODING":
275 if (value == null) {
276 _encoding = DEF_ENCODING;
277 base.Remove (mappedKey);
278 } else if (value is string) {
279 this.Encoding = Encoding.GetEncoding ((string)value);
280 } else
281 throw new ArgumentException ("Cannot set encoding from a non-string argument");
282 break;
283  
284 case "READ ONLY":
285 if (value == null) {
286 _readonly = DEF_READONLY;
287 base.Remove (mappedKey);
288 } else
289 this.ReadOnly = ConvertToBoolean (value);
290 break;
291  
292 case "FAILIFMISSING":
293 if (value == null) {
294 _failIfMissing = DEF_FAILIFMISSING;
295 base.Remove (mappedKey);
296 } else
297 this.FailIfMissing = ConvertToBoolean (value);
298 break;
299  
300 default:
301 throw new ArgumentException ("Keyword not supported :" + key);
302 }
303 }
304  
305 private static int ConvertToInt32 (object value)
306 {
307 return Int32.Parse (value.ToString (), CultureInfo.InvariantCulture);
308 }
309  
310 private static bool ConvertToBoolean (object value)
311 {
312 if (value == null)
313 throw new ArgumentNullException ("null value cannot be converted to boolean");
314 string upper = value.ToString ().ToUpper ().Trim ();
315 if (upper == "YES" || upper == "TRUE")
316 return true;
317 if (upper == "NO" || upper == "FALSE")
318 return false;
319 throw new ArgumentException (String.Format ("Invalid boolean value: {0}", value.ToString ()));
320 }
321 #endregion // Private Methods
322 }
323 }