SQLiteConnectionStringBuilder.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Text;
  3. using System.Globalization;
  4. namespace System.Data.SQLiteClient
  5. {
  6. public enum SynchronousMode
  7. {
  8. Normal = 0,
  9. Full,
  10. Off
  11. }
  12. public enum DateTimeFormat
  13. {
  14. ISO8601 = 0,
  15. Ticks,
  16. CurrentCulture
  17. }
  18. public class SQLiteConnectionStringBuilder
  19. {
  20. private string _DataSource;
  21. private bool _NewDatabase;
  22. private bool _Emulator;
  23. private Encoding _Encoding;
  24. private int _CacheSize;
  25. private SynchronousMode _Synchronous;
  26. private DateTimeFormat _DateTimeFormat;
  27. public SQLiteConnectionStringBuilder()
  28. {
  29. _DataSource = String.Empty;
  30. _NewDatabase = false;
  31. _Emulator = false;
  32. _Encoding = Encoding.UTF8;
  33. _CacheSize = 2000;
  34. _Synchronous = SynchronousMode.Normal;
  35. _DateTimeFormat = DateTimeFormat.ISO8601;
  36. }
  37. public SQLiteConnectionStringBuilder(string connectionString)
  38. {
  39. Parse(connectionString);
  40. }
  41. public string DataSource
  42. {
  43. get { return _DataSource; }
  44. set { _DataSource = value; }
  45. }
  46. public bool NewDatabase
  47. {
  48. get { return _NewDatabase; }
  49. set { _NewDatabase = value; }
  50. }
  51. public bool Emulator
  52. {
  53. get { return _Emulator; }
  54. set { _Emulator = value; }
  55. }
  56. public Encoding Encoding
  57. {
  58. get { return _Encoding; }
  59. set { _Encoding = value; }
  60. }
  61. public int CacheSize
  62. {
  63. get { return _CacheSize; }
  64. set { _CacheSize = value; }
  65. }
  66. public SynchronousMode SynchronousMode
  67. {
  68. get { return _Synchronous; }
  69. set { _Synchronous = value; }
  70. }
  71. public DateTimeFormat DateTimeFormat
  72. {
  73. get { return _DateTimeFormat; }
  74. set { _DateTimeFormat = value; }
  75. }
  76. private void Parse(string connectionString)
  77. {
  78. String helpMessage =
  79. "Valid parameters:\n" +
  80. "Data Source=<database file> (required)\n" +
  81. "NewDatabase=True|False (default: False)\n" +
  82. "Emulator=True|False (default: False)\n" +
  83. "Encoding=UTF8|UTF16 (default: UTF8)\n" +
  84. "Cache Size=<N> (default: 2000)\n" +
  85. "Synchronous=Full|Normal|Off (default: Normal)\n" +
  86. "DateTimeFormat=ISO8601|Ticks|CurrentCulture (default: ISO8601)\n";
  87. // Parse connection string
  88. String[] parameters = connectionString.Split(";".ToCharArray());
  89. if (parameters.Length == 0) throw new SQLiteException(helpMessage);
  90. for (int i = 0; i < parameters.Length; i++)
  91. {
  92. String parameter = parameters[i].Trim();
  93. if (parameter.Length == 0) continue;
  94. int index = parameter.IndexOf('=');
  95. if (index < 0) throw new SQLiteException(helpMessage);
  96. String paramName = parameter.Substring(0, index).Trim();
  97. String paramValue = parameter.Substring(index + 1).Trim();
  98. if (paramName.Equals("Data Source"))_DataSource = paramValue;
  99. else if (paramName.Equals("NewDatabase")) _NewDatabase = paramValue.ToUpper().Equals("TRUE");
  100. else if (paramName.Equals("Emulator")) _Emulator = paramValue.ToUpper().Equals("TRUE");
  101. else if (paramName.Equals("Encoding"))
  102. {
  103. switch (paramValue.ToUpper())
  104. {
  105. case "UTF8":
  106. _Encoding = Encoding.UTF8;
  107. break;
  108. case "UTF16":
  109. _Encoding = Encoding.Unicode;
  110. break;
  111. default:
  112. throw new SQLiteException(string.Format("Unknown encoding specified: {0}", paramValue));
  113. }
  114. }
  115. else if (paramName.Equals("Synchronous"))
  116. {
  117. switch (paramValue.ToUpper())
  118. {
  119. case "FULL":
  120. _Synchronous = SynchronousMode.Full;
  121. break;
  122. case "NORMAL":
  123. _Synchronous = SynchronousMode.Normal;
  124. break;
  125. case "OFF":
  126. _Synchronous = SynchronousMode.Off;
  127. break;
  128. default:
  129. throw new SQLiteException(string.Format("Unknown synchronisation mode specified: {0}", paramValue));
  130. }
  131. }
  132. else if (paramName.Equals("Cache Size"))
  133. {
  134. try
  135. {
  136. _CacheSize = Convert.ToInt32(paramValue);
  137. }
  138. catch
  139. {
  140. throw new SQLiteException(string.Format("Invalid cache size specified: {0}", paramValue));
  141. }
  142. }
  143. else if (paramName.Equals("DateTimeFormat"))
  144. {
  145. switch (paramValue.ToUpper())
  146. {
  147. case "ISO8601":
  148. _DateTimeFormat = DateTimeFormat.ISO8601;
  149. break;
  150. case "TICKS":
  151. _DateTimeFormat = DateTimeFormat.Ticks;
  152. break;
  153. case "CURRENTCULTURE":
  154. _DateTimeFormat = DateTimeFormat.CurrentCulture;
  155. break;
  156. default:
  157. throw new SQLiteException(string.Format("Unknown DateTimeFormat specified: {0}", paramValue));
  158. }
  159. }
  160. else
  161. {
  162. throw new SQLiteException(string.Format("Unknown parameter specified: {0}.\r\n{1}", paramName, helpMessage));
  163. }
  164. }
  165. }
  166. public override string ToString()
  167. {
  168. StringBuilder builder = new StringBuilder();
  169. builder.AppendFormat(CultureInfo.InvariantCulture, "Data Source={0}; ", _DataSource);
  170. builder.AppendFormat(CultureInfo.InvariantCulture, "NewDatabase={0}; ", _NewDatabase);
  171. builder.AppendFormat(CultureInfo.InvariantCulture, "Emulator={0}; ", _Emulator);
  172. string encoding = "UTF8";
  173. if (_Encoding == Encoding.Unicode) encoding = "UTF16";
  174. builder.AppendFormat(CultureInfo.InvariantCulture, "Encoding={0}; ", encoding);
  175. builder.AppendFormat(CultureInfo.InvariantCulture, "Cache Size={0}; ", _CacheSize);
  176. builder.AppendFormat(CultureInfo.InvariantCulture, "Synchronous={0}; ", _Synchronous);
  177. builder.AppendFormat(CultureInfo.InvariantCulture, "DateTimeFormat={0}; ", _DateTimeFormat);
  178. return builder.ToString();
  179. }
  180. }
  181. }