MySQLConnector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 UserName = "promsystem";
  14. private readonly string Password = "PrmSystem212";
  15. #if DEBUG
  16. private readonly string DatabaseName = "documents_dev";
  17. #else
  18. private readonly string DatabaseName = "documents";
  19. #endif
  20. MySqlCommand SQLcom;
  21. public MySqlConnection Connection { get; set; }
  22. private static MySQLConnector _instance = null;
  23. public static MySQLConnector Instance()
  24. {
  25. if (_instance == null)
  26. _instance = new MySQLConnector();
  27. return _instance;
  28. }
  29. public async Task<bool> IsConnect()
  30. {
  31. string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
  32. if (Connection != null)
  33. await SQLSelectComplex("select COUNT(*) from accounts", false);
  34. else
  35. Console.WriteLine($"SQL IsConnect Connection null");
  36. if (Connection == null || Connection.State != System.Data.ConnectionState.Open)
  37. {
  38. Console.WriteLine($"SQL IsConnect making NEW Connection!");
  39. Connection = new MySqlConnection(connstring);
  40. Connection.Open();
  41. }
  42. await Task.WhenAll();
  43. return true;
  44. }
  45. public async Task<long> SQLInsert(string sql)
  46. {
  47. Console.WriteLine($"SQLInsert {sql}");
  48. long lastID = 0;
  49. bool connected = await IsConnect();
  50. if (connected)
  51. {
  52. MySqlCommand SQLcom3 = new(sql, Connection);
  53. //SQLcom3.ExecuteNonQuery();
  54. await SQLcom3.ExecuteNonQueryAsync();
  55. lastID = SQLcom3.LastInsertedId;
  56. SQLcom3.Dispose();
  57. Console.WriteLine($"SQLInsert end");
  58. }
  59. else
  60. Console.WriteLine("Not connected to DB.");
  61. await Task.WhenAll();
  62. return lastID;
  63. }
  64. public async Task<Dictionary<string, AccountModel>> SQLSelectASPUsers()
  65. {
  66. Console.WriteLine($"SQLSelectASPUsers");
  67. string sql = "select * from aspnetusers";
  68. Dictionary<string, AccountModel> accs = new();
  69. bool connected = await IsConnect();
  70. if (connected)
  71. {
  72. SQLcom = new(sql, Connection);
  73. MySqlDataReader rdr = SQLcom.ExecuteReader();
  74. while (rdr.Read())
  75. {
  76. AccountModel newacc = new ();
  77. newacc.UUID = rdr.GetString(0);
  78. newacc.Name = rdr.GetString(1);
  79. newacc.Email = rdr.GetString(3);
  80. newacc.PWDHash = rdr.GetString(6);
  81. //if (!rdr.IsDBNull(15))
  82. // newacc.eth_address = rdr.GetString(15);
  83. if (!accs.ContainsKey(newacc.UUID))
  84. accs.Add(newacc.UUID, newacc);
  85. newacc.bsel = rdr.GetByte(17);
  86. }
  87. await Task.WhenAll();
  88. rdr.Close();
  89. return accs;
  90. }
  91. else
  92. Console.WriteLine("Not connected to DB.");
  93. return null;
  94. }
  95. public async Task<Dictionary<int, SmartContract>> SQLSelectContracts(int blockchainId)
  96. {
  97. Console.WriteLine($"SQLSelectContracts");
  98. var bc = Blockchain.Find(blockchainId);
  99. if (bc != null)
  100. {
  101. string sql = "select * from smart_contracts where blockchain_id =" + blockchainId;
  102. Dictionary<int, SmartContract> contracts = new();
  103. bool connected = await IsConnect();
  104. if (connected)
  105. {
  106. SQLcom = new(sql, Connection);
  107. MySqlDataReader rdr = SQLcom.ExecuteReader();
  108. while (rdr.Read())
  109. {
  110. var id = rdr.GetInt32(0);
  111. var code = rdr.GetString(1);
  112. var bytecode = rdr.GetString(2);
  113. var name = rdr.GetString(3);
  114. var gas = rdr.GetString(8);
  115. SmartContract contract;
  116. if (name == "Verify")
  117. contract = new VerifyContract(name, code, bytecode, blockchainId, gas);
  118. else
  119. contract = new SmartContract(name, code, bytecode, blockchainId, gas);
  120. contract.ID = id;
  121. if (!rdr.IsDBNull(6))
  122. {
  123. contract.Address = rdr.GetString(6);
  124. }
  125. Console.WriteLine($"SQLSelectContracts rdr Read {contract.Name}");
  126. if (!contracts.ContainsKey(contract.ID))
  127. contracts.Add(contract.ID, contract);
  128. if (!bc.contractNames.ContainsKey(name))
  129. bc.contractNames.Add(name, contract);
  130. }
  131. await Task.WhenAll();
  132. rdr.Close();
  133. return contracts;
  134. }
  135. else
  136. Console.WriteLine("Not connected to DB.");
  137. }
  138. else
  139. Console.WriteLine($"Error: blockchain {blockchainId} not found");
  140. return null;
  141. }
  142. public async Task<Dictionary<int, ArticleModel>> SQLSelectArticles(string sql)
  143. {
  144. //return null;
  145. Console.WriteLine($"SQLSelectArticles {sql}");
  146. Dictionary<int, ArticleModel> articleModels = new();
  147. ArticleModel articleModel;
  148. bool connected = await IsConnect();
  149. if (connected)
  150. {
  151. var SQLcom = new MySqlCommand(sql, Connection);
  152. var rdr = await SQLcom.ExecuteReaderAsync();
  153. int count = 0;
  154. bool stop = false;
  155. while (rdr.Read()) ///id, filename, article_name, date_publish, action_type/status
  156. {
  157. /// tmp
  158. for (int i = 0; i < rdr.FieldCount; i++)
  159. {
  160. if (rdr.IsDBNull(i))
  161. {
  162. Console.WriteLine($"SQLSelectArticles NULL detected {i}");
  163. stop = true;
  164. break;
  165. }
  166. }
  167. if (stop)
  168. {
  169. stop = false;
  170. continue;
  171. }
  172. articleModel = new();
  173. articleModel.ID = rdr.GetInt32(0);
  174. articleModel.Filename = rdr.GetString(1);
  175. if (!rdr.IsDBNull(2)) articleModel.Name = rdr.GetString(2); else articleModel.Name = "NULL";
  176. articleModel.PublishDate = rdr.GetDateTime(3);
  177. articleModel.Authors = rdr.GetString(4);
  178. if (!rdr.IsDBNull(5)) articleModel.Status = (ArticleStatus)rdr.GetInt32(5); else articleModel.Status = ArticleStatus.New;
  179. if (articleModels.ContainsKey(articleModel.ID))
  180. continue;
  181. count++;
  182. articleModels.Add(articleModel.ID, articleModel);
  183. }
  184. Console.WriteLine($"SQLSelectArticles count: {count}");
  185. rdr.Close();
  186. return articleModels;
  187. }
  188. else
  189. Console.WriteLine("Not connected to DB.");
  190. return null;
  191. }
  192. const string typeguid = "System.Guid";
  193. public async Task<List<Dictionary<string, object>>> SQLSelectComplex(string request, bool check = true)
  194. {
  195. Console.WriteLine($"SQLSelectComplex {request}");
  196. bool connected = true;
  197. if (check)
  198. {
  199. //Console.WriteLine($"SQLSelectComplex check");
  200. connected = await IsConnect();
  201. }
  202. if (connected)
  203. {
  204. //Console.WriteLine($"SQLSelectComplex connected");
  205. List<Dictionary<string, object>> retval = new();
  206. MySqlCommand SQLcom2 = new(request, Connection);
  207. //Console.WriteLine($"SQLSelectComplex new SQLcom");
  208. try
  209. {
  210. var Reader = await SQLcom2.ExecuteReaderAsync();
  211. //Console.WriteLine($"SQLSelectComplex ExecuteReader");
  212. while (Reader.Read())
  213. {
  214. //Console.WriteLine($"SQLSelectComplex Reader.Read");
  215. Dictionary<string, object> data = new();
  216. for (int i = 0; i < Reader.FieldCount; i++)
  217. {
  218. if (!Reader.IsDBNull(i))
  219. {
  220. data.Add(Reader.GetName(i), Reader.GetValue(i));
  221. }
  222. else
  223. {
  224. //Console.WriteLine(Reader.GetName(i)+": GetDataTypeName " + Reader.GetDataTypeName(i) + " GetFieldType " + Reader.GetFieldType(i).ToString());
  225. if (Reader.GetFieldType(i).ToString() == typeguid)
  226. {
  227. data.Add(Reader.GetName(i), "00000000-0000-0000-0000-000000000000");
  228. }
  229. else if (Reader.GetName(i) == "mac_address")
  230. {
  231. data.Add(Reader.GetName(i), "00-00-00-00-00-00");
  232. }
  233. else
  234. {
  235. data.Add(Reader.GetName(i), null);
  236. }
  237. }
  238. }
  239. retval.Add(data);
  240. }
  241. //Console.WriteLine($"SQLSelectComplex Reader.Close");
  242. Reader.Close();
  243. await Task.WhenAll();
  244. }
  245. catch (Exception e)
  246. {
  247. Console.WriteLine("SQL Exception 5.1 " + e.Message + " query " + request + "stack trace" + e.StackTrace);
  248. }
  249. return retval;
  250. }
  251. return null;
  252. }
  253. public async Task<ArticleModel> SQLSelectArticle(string sql)
  254. {
  255. Console.WriteLine($"SQLSelectArticle");
  256. ArticleModel article = new();
  257. bool connected = await IsConnect();
  258. if (connected)
  259. {
  260. SQLcom = new(sql, Connection);
  261. MySqlDataReader rdr = SQLcom.ExecuteReader();
  262. ///0=id, filename, article_name, authors, date_publish, annotation, keywords, action_type/status, rating, file_hash,
  263. ///10=doc_noungroups, doc_entities, doc_morph, doc_keywords1, doc_keywords2
  264. while (rdr.Read())
  265. {
  266. article.ID = rdr.GetInt32(0);
  267. article.Filename = rdr.GetString(1);
  268. article.Name = rdr.GetString(2);
  269. article.Authors = rdr.GetString(3);
  270. article.PublishDate = rdr.GetDateTime(4);
  271. article.Annotation = rdr.GetString(5);
  272. article.Keywords = rdr.GetString(6);
  273. article.Status = (ArticleStatus)rdr.GetInt16(7);
  274. if (!rdr.IsDBNull(8)) article.Rating = rdr.GetInt16(8);
  275. article.HashSum = rdr.GetString(9);
  276. if (!rdr.IsDBNull(10)) article.NounGroups = rdr.GetString(10);
  277. if (!rdr.IsDBNull(11)) article.Entities = rdr.GetString(11);
  278. if (!rdr.IsDBNull(12)) article.Morph = rdr.GetString(12);
  279. if (!rdr.IsDBNull(13)) article.Keywords1 = rdr.GetString(13);
  280. if (!rdr.IsDBNull(14)) article.Keywords2 = rdr.GetString(14);
  281. Console.WriteLine($"Got article, ID: {article.ID}.");
  282. }
  283. rdr.Close();
  284. await Task.WhenAll();
  285. return article;
  286. }
  287. else
  288. Console.WriteLine("Not connected to DB.");
  289. return null;
  290. }
  291. public async Task<string> SQLSelectUUID(string sql)
  292. {
  293. Console.WriteLine($"SQLSelectUUID {sql}");
  294. bool connected = await IsConnect();
  295. if (connected)
  296. {
  297. SQLcom = new(sql, Connection);
  298. object obj = await SQLcom.ExecuteScalarAsync();
  299. return obj.ToString();
  300. }
  301. else return "";
  302. }
  303. public async Task<int> SQLSelectCount(string sql)
  304. {
  305. Console.WriteLine($"SQLSelectCount");
  306. bool connected = await IsConnect();
  307. if (connected)
  308. {
  309. SQLcom = new(sql, Connection);
  310. object obj = await SQLcom.ExecuteScalarAsync();
  311. int count = int.Parse(obj.ToString());
  312. return count;
  313. }
  314. else return 0;
  315. }
  316. public void Close()
  317. {
  318. Connection.Close();
  319. }
  320. }
  321. }