CSQLiteDBStore.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OTSModelSharp.DTLBase
  9. {
  10. public class CSQLiteDBStore : IDBStoreBase,IDBFileBase
  11. {
  12. SQLiteHelper helper;
  13. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  14. string m_DBFileName;
  15. SQLiteConnection connForTrans;
  16. SQLiteTransaction trans;
  17. public CSQLiteDBStore(string dbFileName)
  18. {
  19. helper = new SQLiteHelper(dbFileName);
  20. connForTrans = helper.GetDBConnection();
  21. m_DBFileName = dbFileName;
  22. }
  23. public CSQLiteDBStore()
  24. {
  25. }
  26. public bool BeginTransaction()
  27. {
  28. if (connForTrans.State != ConnectionState.Open)
  29. {
  30. connForTrans.Open();
  31. }
  32. trans=connForTrans.BeginTransaction();
  33. //RunCommandForTrans("begin transaction;", connForTrans); //cSQLiteStore.BeginTransaction();
  34. return true;
  35. }
  36. public void Close()
  37. {
  38. connForTrans.Close();
  39. }
  40. public bool CloseSynchronous()
  41. {
  42. RunCommand("PRAGMA synchronous = OFF; "); //cSQLiteStore.CloseSynchronous();
  43. return true;
  44. }
  45. public bool CommitTransaction()
  46. {
  47. //RunCommandForTrans("commit transaction;",connForTrans); //cSQLiteStore.CloseSynchronous();
  48. trans.Commit();
  49. connForTrans.Close();
  50. return true;
  51. }
  52. public bool Create(string a_sFileName, bool a_bOverwrite)
  53. {
  54. if (System.IO.File.Exists(a_sFileName))
  55. {
  56. helper = new SQLiteHelper(a_sFileName );
  57. return true;
  58. }
  59. else
  60. {
  61. SQLiteConnection.CreateFile(a_sFileName);
  62. //conStr = a_sFileName;
  63. helper = new SQLiteHelper(a_sFileName );
  64. return true;
  65. }
  66. }
  67. public bool DeleteTable(string a_sTableName)
  68. {
  69. if (!IsTableExists(a_sTableName))
  70. {
  71. return true;
  72. }
  73. string sSQLCommand ;
  74. sSQLCommand=string.Format("DROP TABLE \'{0}\'", a_sTableName);
  75. return RunCommand(sSQLCommand);
  76. }
  77. public string GetFileName()
  78. {
  79. return m_DBFileName;
  80. }
  81. public long GetLastRowId(string a_sTableName = "")
  82. {
  83. string sSQLCommand;
  84. sSQLCommand=string.Format("SELECT max(ROWID) FROM {0}", a_sTableName);
  85. var nRet =helper.ExecuteQuery(sSQLCommand);
  86. return (long)nRet.Columns[0].DefaultValue;
  87. }
  88. public List<string> GetTableList()
  89. {
  90. List<string> tableList=new List<string>();
  91. string sSQLCommand=string.Format("SELECT name FROM sqlite_master WHERE type = \'table\';");
  92. var query = helper.ExecuteQuery(sSQLCommand);
  93. for (int i = 0; i < query.Rows.Count; i++)
  94. {
  95. tableList.Add(query.Rows[i][0].ToString());
  96. }
  97. return tableList;
  98. }
  99. public bool InsertBlobData(string szSql, byte[] pBlobData, int iBlobDataLen)
  100. {
  101. byte[] data = new byte[iBlobDataLen];
  102. if (connForTrans.State == ConnectionState.Open)
  103. {
  104. SQLiteCommand sqlCommand = new SQLiteCommand();
  105. sqlCommand.Connection = connForTrans;
  106. sqlCommand.CommandText = szSql; //"INSERT INTO sequence(name,type,code,defaultParFile) VALUES (@name,@type,@code,@defaultFile)";
  107. sqlCommand.Parameters.Clear();
  108. sqlCommand.Parameters.AddWithValue(@"blob", pBlobData);
  109. sqlCommand.ExecuteNonQuery();
  110. }
  111. else
  112. {
  113. using (var dbconnection = helper.GetDBConnection())
  114. {
  115. dbconnection.Open();
  116. SQLiteCommand sqlCommand = new SQLiteCommand();
  117. sqlCommand.Connection = dbconnection;
  118. sqlCommand.CommandText = szSql; //"INSERT INTO sequence(name,type,code,defaultParFile) VALUES (@name,@type,@code,@defaultFile)";
  119. sqlCommand.Parameters.Clear();
  120. sqlCommand.Parameters.AddWithValue(@"blob", pBlobData);
  121. sqlCommand.ExecuteNonQuery();
  122. }
  123. }
  124. return true;
  125. }
  126. public bool IsTableExists(string a_sTableName)
  127. {
  128. return helper.IsExist(a_sTableName);
  129. }
  130. public bool Open(string a_sFileName, bool a_bForce)
  131. {
  132. try
  133. {
  134. m_DBFileName = a_sFileName;
  135. helper = new SQLiteHelper(a_sFileName);
  136. return true;
  137. }
  138. catch (Exception e)
  139. {
  140. return false;
  141. }
  142. }
  143. public System.Data.DataTable QueryByCmdForDataTable(string a_sQueryString)
  144. {
  145. var t = helper.ExecuteQuery(a_sQueryString);
  146. return t;
  147. }
  148. public IDBQueryBase QueryByCommand(string a_sQueryString)
  149. {
  150. var t=helper.ExecuteReader(a_sQueryString);
  151. return new CSQLiteQuery(t);
  152. }
  153. public IDBQueryBase QueryByTableName(string a_sTableName, string a_sOrderColumnName="")
  154. {
  155. string sSQLCommand;
  156. if (a_sOrderColumnName!="")
  157. {
  158. sSQLCommand=string.Format("SELECT * FROM \'{0}\' ORDER BY \'{1}\'", a_sTableName, a_sOrderColumnName);
  159. }
  160. else
  161. {
  162. sSQLCommand=string.Format("SELECT * FROM \'{0}\'", a_sTableName);
  163. }
  164. return QueryByCommand(sSQLCommand);
  165. }
  166. public System.Data.DataTable QueryByTableNameForDataTable(string a_sTableName, string a_sOrderColumnName)
  167. {
  168. string sSQLCommand;
  169. if (a_sOrderColumnName != "")
  170. {
  171. sSQLCommand = string.Format("SELECT * FROM \'{0}\' ORDER BY \'{1}\'", a_sTableName, a_sOrderColumnName);
  172. }
  173. else
  174. {
  175. sSQLCommand = string.Format("SELECT * FROM \'{0}\'", a_sTableName);
  176. }
  177. return QueryByCmdForDataTable(sSQLCommand);
  178. }
  179. public bool RunCommand(string a_sCommandString, bool a_bIgnoreExist = false)
  180. {
  181. try
  182. {
  183. if (connForTrans.State == ConnectionState.Open)
  184. {
  185. RunCommandForTrans(a_sCommandString, connForTrans);
  186. }
  187. else
  188. {
  189. helper.ExecuteNonQuery(a_sCommandString);
  190. }
  191. return true;
  192. }
  193. catch (Exception e)
  194. {
  195. log.Error(e.Message);
  196. return false;
  197. }
  198. }
  199. private bool RunCommandForTrans(string a_sCommandString,SQLiteConnection connForTrans, bool a_bIgnoreExist = false)
  200. {
  201. try
  202. {
  203. helper.ExecuteNonQueryForTransaction(a_sCommandString,connForTrans);
  204. return true;
  205. }
  206. catch (Exception e)
  207. {
  208. log.Error(e.Message);
  209. return false;
  210. }
  211. }
  212. }
  213. }