CSQLiteDBStore.cs 7.5 KB

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