MySQLConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using HyperCube.Models;
  5. using MySql.Data.MySqlClient;
  6. using Console = HyperCube.Utils.AdvConsole;
  7. namespace HyperCube
  8. {
  9. public class MySQLConnector
  10. {
  11. private MySQLConnector(){ }
  12. private readonly string Server = "dmatter.net";
  13. private readonly string DatabaseName = "documents";
  14. private readonly string UserName = "promsystem";
  15. private readonly string Password = "PrmSystem212";
  16. MySqlCommand SQLcom;
  17. public MySqlConnection Connection { get; set; }
  18. private static MySQLConnector _instance = null;
  19. public static MySQLConnector Instance()
  20. {
  21. if (_instance == null)
  22. _instance = new MySQLConnector();
  23. return _instance;
  24. }
  25. public bool IsConnect()
  26. {
  27. if (Connection == null)
  28. {
  29. string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
  30. Connection = new MySqlConnection(connstring);
  31. Connection.Open();
  32. }
  33. else if (Connection.State != System.Data.ConnectionState.Open)
  34. Connection.Open();
  35. return true;
  36. }
  37. public long SQLInsert(string sql)
  38. {
  39. long lastID = 0;
  40. bool connected = IsConnect();
  41. if (connected)
  42. {
  43. SQLcom = new(sql, Connection);
  44. SQLcom.ExecuteNonQuery();
  45. SQLcom.Dispose();
  46. lastID = SQLcom.LastInsertedId;
  47. }
  48. else
  49. Console.WriteLine("Not connected to DB.");
  50. return lastID;
  51. }
  52. public async Task<Dictionary<string, AccountModel>> SQLSelectASPUsers()
  53. {
  54. string sql = "select * from aspnetusers";
  55. Dictionary<string, AccountModel> accs = new();
  56. bool connected = IsConnect();
  57. if (connected)
  58. {
  59. SQLcom = new(sql, Connection);
  60. MySqlDataReader rdr = SQLcom.ExecuteReader();
  61. while (rdr.Read())
  62. {
  63. AccountModel newacc = new ();
  64. newacc.UUID = rdr.GetString(0);
  65. newacc.Name = rdr.GetString(1);
  66. newacc.Email = rdr.GetString(3);
  67. if (!rdr.IsDBNull(15))
  68. newacc.eth_address = rdr.GetString(15);
  69. if (!accs.ContainsKey(newacc.UUID))
  70. accs.Add(newacc.UUID, newacc);
  71. }
  72. await Task.WhenAll();
  73. rdr.Close();
  74. return accs;
  75. }
  76. else
  77. Console.WriteLine("Not connected to DB.");
  78. return null;
  79. }
  80. public async Task<Dictionary<int, SmartContract>> SQLSelectContracts()
  81. {
  82. string sql = "select * from smart_contracts";
  83. Dictionary<int, SmartContract> contracts = new();
  84. bool connected = IsConnect();
  85. if (connected)
  86. {
  87. SQLcom = new(sql, Connection);
  88. MySqlDataReader rdr = SQLcom.ExecuteReader();
  89. while (rdr.Read())
  90. {
  91. var id = rdr.GetInt32(0);
  92. var code = rdr.GetString(1);
  93. var bytecode = rdr.GetString(2);
  94. var name = rdr.GetString(3);
  95. SmartContract contract;
  96. if (name == "Verify")
  97. contract = new VerifyContract(id, name, code, bytecode);
  98. else
  99. contract = new SmartContract(id, name, code, bytecode);
  100. if (!rdr.IsDBNull(6))
  101. {
  102. contract.Address = rdr.GetString(6);
  103. }
  104. Console.WriteLine($"SQLSelectContracts rdr Read {contract.Name}");
  105. if (!contracts.ContainsKey(contract.ID))
  106. contracts.Add(contract.ID, contract);
  107. if (!Blockchain.GetMain().contractNames.ContainsKey(name))
  108. Blockchain.GetMain().contractNames.Add(name, contract);
  109. }
  110. await Task.WhenAll();
  111. rdr.Close();
  112. return contracts;
  113. }
  114. else
  115. Console.WriteLine("Not connected to DB.");
  116. return null;
  117. }
  118. public async Task<Dictionary<int, ArticleModel>> SQLSelectArticles(string sql)
  119. {
  120. Dictionary<int, ArticleModel> articleModels = new();
  121. Models.ArticleModel articleModel;
  122. bool connected = IsConnect();
  123. if (connected)
  124. {
  125. SQLcom = new(sql, Connection);
  126. MySqlDataReader rdr = SQLcom.ExecuteReader();
  127. while (rdr.Read()) ///id, filename, article_name, date_publish, action_type/status
  128. {
  129. articleModel = new();
  130. articleModel.ID = rdr.GetInt32(0);
  131. articleModel.Filename = rdr.GetString(1);
  132. articleModel.Name = rdr.GetString(2);
  133. articleModel.PublishDate = rdr.GetDateTime(3);
  134. articleModel.Authors = rdr.GetString(4);
  135. articleModel.Status = (ArticleStatus)rdr.GetInt32(5);
  136. if (articleModels.ContainsKey(articleModel.ID))
  137. continue;
  138. articleModels.Add(articleModel.ID, articleModel);
  139. }
  140. await Task.WhenAll();
  141. rdr.Close();
  142. return articleModels;
  143. }
  144. else
  145. Console.WriteLine("Not connected to DB.");
  146. return null;
  147. }
  148. const string typeguid = "System.Guid";
  149. public async Task<List<Dictionary<string, object>>> SQLSelectComplex(string request)
  150. {
  151. bool connected = IsConnect();
  152. if (connected)
  153. {
  154. List<Dictionary<string, object>> retval = new List<Dictionary<string, object>>();
  155. SQLcom = new(request, Connection);
  156. try
  157. {
  158. MySqlDataReader Reader = SQLcom.ExecuteReader();
  159. while (Reader.Read())
  160. {
  161. Dictionary<string, object> data = new Dictionary<string, object>();
  162. for (int i = 0; i < Reader.FieldCount; i++)
  163. {
  164. if (!Reader.IsDBNull(i))
  165. {
  166. data.Add(Reader.GetName(i), Reader.GetValue(i));
  167. }
  168. else
  169. {
  170. //Console.WriteLine(Reader.GetName(i)+": GetDataTypeName " + Reader.GetDataTypeName(i) + " GetFieldType " + Reader.GetFieldType(i).ToString());
  171. if (Reader.GetFieldType(i).ToString() == typeguid)
  172. {
  173. data.Add(Reader.GetName(i), "00000000-0000-0000-0000-000000000000");
  174. }
  175. else if (Reader.GetName(i) == "mac_address")
  176. {
  177. data.Add(Reader.GetName(i), "00-00-00-00-00-00");
  178. }
  179. else
  180. {
  181. data.Add(Reader.GetName(i), null);
  182. }
  183. }
  184. }
  185. retval.Add(data);
  186. }
  187. await Task.WhenAll();
  188. Reader.Close();
  189. }
  190. catch (Exception e)
  191. {
  192. Console.WriteLine("SQL Exception 5.1 " + e.Message + " query " + request + "stack trace" + e.StackTrace);
  193. }
  194. return retval;
  195. }
  196. return null;
  197. }
  198. public async Task<ArticleModel> SQLSelectArticle(string sql)
  199. {
  200. ArticleModel articleModel = new();
  201. bool connected = IsConnect();
  202. if (connected)
  203. {
  204. SQLcom = new(sql, Connection);
  205. MySqlDataReader rdr = SQLcom.ExecuteReader();
  206. while (rdr.Read()) ///id, filename, article_name, authors, date_publish, annotation, keywords, action_type/status, rating
  207. {
  208. articleModel.ID = rdr.GetInt32(0);
  209. articleModel.Filename = rdr.GetString(1);
  210. articleModel.Name = rdr.GetString(2);
  211. articleModel.Authors = rdr.GetString(3);
  212. articleModel.PublishDate = rdr.GetDateTime(4);
  213. articleModel.Annotation = rdr.GetString(5);
  214. articleModel.Keywords = rdr.GetString(6);
  215. articleModel.Status = (ArticleStatus)rdr.GetInt16(7);
  216. if (!rdr.IsDBNull(8)) articleModel.Rating = rdr.GetInt16(8);
  217. }
  218. await Task.WhenAll();
  219. rdr.Close();
  220. return articleModel;
  221. }
  222. else
  223. Console.WriteLine("Not connected to DB.");
  224. return null;
  225. }
  226. public async Task<string> SQLSelectUUID(string sql)
  227. {
  228. bool connected = IsConnect();
  229. if (connected)
  230. {
  231. SQLcom = new(sql, Connection);
  232. object obj = await SQLcom.ExecuteScalarAsync();
  233. return obj.ToString();
  234. }
  235. else return "";
  236. }
  237. public async Task<int> SQLSelectCount(string sql)
  238. {
  239. bool connected = IsConnect();
  240. if (connected)
  241. {
  242. SQLcom = new(sql, Connection);
  243. object obj = await SQLcom.ExecuteScalarAsync();
  244. int count = int.Parse(obj.ToString());
  245. return count;
  246. }
  247. else return 0;
  248. }
  249. public void Close()
  250. {
  251. Connection.Close();
  252. }
  253. }
  254. }