PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/GenericConnector/GenConnection.cs

http://pvbeancounter.googlecode.com/
C# | 255 lines | 184 code | 33 blank | 38 comment | 5 complexity | da08f9b41595bc17b6519d4c73d0f82d MD5 | raw file
  1. /*
  2. * Copyright (c) 2010 Dennis Mackay-Fisher
  3. *
  4. * This file is part of PV Scheduler
  5. *
  6. * PV Scheduler is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 3 or later
  8. * as published by the Free Software Foundation.
  9. *
  10. * PV Scheduler is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with PV Scheduler.
  17. * If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. using System;
  20. using System.Data;
  21. using System.Data.Common;
  22. using System.Data.OleDb;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Text;
  26. namespace GenericConnector
  27. {
  28. public class GenConnection : DbConnection
  29. {
  30. private static int NextId = 0;
  31. public int Id
  32. {
  33. get;
  34. private set;
  35. }
  36. public int CreationThread
  37. {
  38. get;
  39. private set;
  40. }
  41. public DateTime CreationTime
  42. {
  43. get;
  44. private set;
  45. }
  46. protected DbConnection DbConnection;
  47. // SQLite works best when all SQL uses the same connection
  48. // This avoids Database is Locked error messages
  49. // GenDatabase creates a single Global connection for SQLite use
  50. private bool IsGlobal;
  51. public GenDatabase GenDatabase
  52. {
  53. get;
  54. private set;
  55. }
  56. internal GenConnection(GenDatabase db, bool isGlobal = false)
  57. {
  58. DbConnection = db.DbProviderFactory.CreateConnection();
  59. DbConnection.ConnectionString = db.ConnectionString;
  60. GenDatabase = db;
  61. IsGlobal = isGlobal;
  62. Id = NextId++;
  63. CreationThread = System.Threading.Thread.CurrentThread.ManagedThreadId;
  64. CreationTime = DateTime.Now;
  65. }
  66. protected override bool CanRaiseEvents
  67. {
  68. get
  69. {
  70. // the following is a guess as I cannot find any info on the MySql
  71. // implementation of this protected member
  72. return true;
  73. }
  74. }
  75. public GenDBType DBType
  76. {
  77. get
  78. {
  79. return GenDatabase.GenDBType;
  80. }
  81. }
  82. public override int ConnectionTimeout
  83. {
  84. get
  85. {
  86. return DbConnection.ConnectionTimeout;
  87. }
  88. }
  89. public override string ConnectionString
  90. {
  91. get
  92. {
  93. throw new NotImplementedException();
  94. }
  95. set
  96. {
  97. throw new NotImplementedException();
  98. }
  99. }
  100. public override string Database
  101. {
  102. get { throw new NotImplementedException(); }
  103. }
  104. public override string DataSource
  105. {
  106. get { throw new NotImplementedException(); }
  107. }
  108. protected override DbProviderFactory DbProviderFactory
  109. {
  110. get
  111. {
  112. return base.DbProviderFactory;
  113. }
  114. }
  115. public DbConnection InnerConnection
  116. {
  117. get
  118. {
  119. return DbConnection;
  120. }
  121. }
  122. public override string ServerVersion
  123. {
  124. get { throw new NotImplementedException(); }
  125. }
  126. public override System.Data.ConnectionState State
  127. {
  128. get { throw new NotImplementedException(); }
  129. }
  130. protected override DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
  131. {
  132. return DbConnection.BeginTransaction(isolationLevel);
  133. }
  134. public new DbTransaction BeginTransaction(System.Data.IsolationLevel isolationLevel)
  135. {
  136. return DbConnection.BeginTransaction(isolationLevel);
  137. }
  138. public new DbTransaction BeginTransaction()
  139. {
  140. return DbConnection.BeginTransaction();
  141. }
  142. public override void ChangeDatabase(string databaseName)
  143. {
  144. DbConnection.ChangeDatabase(databaseName);
  145. }
  146. public override void Close()
  147. {
  148. if (!IsGlobal)
  149. {
  150. DbConnection.Close();
  151. GenDatabase.ConnectionClosed(this);
  152. }
  153. }
  154. public new void Dispose()
  155. {
  156. if (!IsGlobal)
  157. {
  158. DbConnection.Dispose();
  159. base.Dispose();
  160. }
  161. }
  162. protected override DbCommand CreateDbCommand()
  163. {
  164. return DbConnection.CreateCommand();
  165. }
  166. public override void Open()
  167. {
  168. try
  169. {
  170. DbConnection.Open();
  171. }
  172. catch (Exception e)
  173. {
  174. throw new GenException(GenExceptionType.CannotConnectToDatabase, "GenConnection.Open: Error connecting to the database: " + e.Message, e);
  175. }
  176. }
  177. public override DataTable GetSchema()
  178. {
  179. return DbConnection.GetSchema();
  180. }
  181. public override DataTable GetSchema(String collectionName)
  182. {
  183. return DbConnection.GetSchema(collectionName);
  184. }
  185. public override DataTable GetSchema(String collectionName, String[] restrictionValues)
  186. {
  187. return DbConnection.GetSchema(collectionName, restrictionValues);
  188. }
  189. public DataRow GetSchemaTable(String tableName)
  190. {
  191. DataTable tableList;
  192. tableList = GetSchema();
  193. tableList = GetSchema("Tables");
  194. foreach (DataRow table in tableList.Rows)
  195. {
  196. if (table.ToString() == tableName)
  197. return table;
  198. }
  199. return null;
  200. }
  201. /*
  202. private DataRow GetSchemaColumn(String tableName)
  203. {
  204. DataRow table = GetSchemaTable(tableName);
  205. if (table == null)
  206. return null;
  207. foreach (DataRow column in table.Columns)
  208. {
  209. if (table.ToString() == tableName)
  210. return table;
  211. }
  212. return null;
  213. }
  214. */
  215. public bool SchemaHasTable(String tableName)
  216. {
  217. DataRow table = GetSchemaTable(tableName);
  218. return table != null;
  219. }
  220. }
  221. }