Selaa lähdekoodia

вставил await

Rimmon 3 vuotta sitten
vanhempi
commit
48d30a851d
5 muutettua tiedostoa jossa 33 lisäystä ja 24 poistoa
  1. 1 1
      Models/AccountModel.cs
  2. 7 7
      Models/Blockchain.cs
  3. 1 1
      Models/SmartContract.cs
  4. 20 11
      MySQLConnector.cs
  5. 4 4
      Pages/DocEdit.razor.cs

+ 1 - 1
Models/AccountModel.cs

@@ -33,7 +33,7 @@ namespace HyperCube.Models
 
         async Task bcselupdate()
         {
-            MySQLConnector.Instance().SQLInsert($"update aspnetusers set blockchain_selected = {blockchain_selected} where Id='{UUID}'");
+            await MySQLConnector.Instance().SQLInsert($"update aspnetusers set blockchain_selected = {blockchain_selected} where Id='{UUID}'");
         }
 
         public void RemoveRole(Role role)

+ 7 - 7
Models/Blockchain.cs

@@ -130,14 +130,14 @@ namespace HyperCube.Models
                     query = $"update account_wallets set uuid='{res}', password_plain='{account.PWDHash}' where account_uuid='{account.UUID}' and blockchain_id={id}";
                 account.SetActualAddress(res, this);
                 Console.WriteLine($"CreateBlockchainAccount query {query}");
-                MySQLConnector.Instance().SQLInsert(query);
+                await MySQLConnector.Instance().SQLInsert(query);
             }
             else
                 Console.WriteLine($"CreateBlockchainAccount {id}: {res}");
             query = $"update blockchains set address_main='{res}' where id={id}";
             address = res;
             Console.WriteLine($"CreateBlockchainAccount query {query}");
-            MySQLConnector.Instance().SQLInsert(query);
+            await MySQLConnector.Instance().SQLInsert(query);
             return res;
         }
 
@@ -203,7 +203,7 @@ namespace HyperCube.Models
             }
             var query = $"update blockchains set address_main='{address}' where id={id}";
             Console.WriteLine("query " + query);
-            MySQLConnector.Instance().SQLInsert(query);
+            await MySQLConnector.Instance().SQLInsert(query);
             Console.WriteLine("Json addr " + jsonDe.result[0]);
             return address;
         }
@@ -399,7 +399,7 @@ namespace HyperCube.Models
                 var blockHash = jsonDe.result.blockHash;
                 var blockNumber = jsonDe.result.blockNumber;
                 var contractAddress = jsonDe.result.contractAddress;
-                MySQLConnector.Instance().SQLInsert($"insert into transactions (result, name) values ('{Convert.ToString(jsonDe.result)}', 'eth_getTransactionReceipt')");
+                await MySQLConnector.Instance().SQLInsert($"insert into transactions (result, name) values ('{Convert.ToString(jsonDe.result)}', 'eth_getTransactionReceipt')");
                 Console.WriteLine("result " + answer);
                 if (returnAddress)
                     return contractAddress;
@@ -430,9 +430,9 @@ namespace HyperCube.Models
                 
                 if (gas == null)
                     gas = "invalid";
-                int id = (int)MySQLConnector.Instance().SQLInsert($"insert into smart_contracts (code, bytecode, name, date_add, gas_cost, blockchain_id) values ('{code}','{bytecode}','{name}',NOW(), '{gas}', {this.id})");
-                newctr.ID = id;
-                contracts.Add(id, newctr);
+                long id = await MySQLConnector.Instance().SQLInsert($"insert into smart_contracts (code, bytecode, name, date_add, gas_cost, blockchain_id) values ('{code}','{bytecode}','{name}',NOW(), '{gas}', {this.id})");
+                newctr.ID = (int) id;
+                contracts.Add((int)id, newctr);
                 contractNames.Add(name, newctr);
                 return new object[] { res, newctr };
             }

+ 1 - 1
Models/SmartContract.cs

@@ -107,7 +107,7 @@ namespace HyperCube.Models
 
         public async Task<long> AddFunction(string name_with_args, string compiled_header)
         {
-            return MySQLConnector.Instance().SQLInsert($"insert into functions (name_with_args, contract_id, compiled_header) values ('{name_with_args}',{ID},'{compiled_header}')");
+            return await MySQLConnector.Instance().SQLInsert($"insert into functions (name_with_args, contract_id, compiled_header) values ('{name_with_args}',{ID},'{compiled_header}')");
         }
     }
 

+ 20 - 11
MySQLConnector.cs

@@ -28,14 +28,16 @@ namespace HyperCube
             return _instance;
         }
 
-        public bool IsConnect()
+        public async Task<bool> IsConnect()
         {
+           
             string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
             if (Connection != null)
             {
+                Console.WriteLine($"IsConnect {Connection.State}");
                 //try
                 //{
-                    SQLSelectComplex("select COUNT(*) from accounts", false);
+                    await SQLSelectComplex("select COUNT(*) from accounts", false);
                 //    }
                 //    catch (Exception e)
                 //    {
@@ -62,12 +64,12 @@ namespace HyperCube
             return true;
         }
 
-        public long SQLInsert(string sql)
+        public async Task<long> SQLInsert(string sql)
         {
             Console.WriteLine($"SQLInsert {sql}");
             long lastID = 0;
 
-            bool connected = IsConnect();
+            bool connected = await IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);
@@ -78,15 +80,17 @@ namespace HyperCube
             else
                 Console.WriteLine("Not connected to DB.");
 
+            await Task.WhenAll();
             return lastID;
         }        
 
         public async Task<Dictionary<string, AccountModel>> SQLSelectASPUsers()
         {
+            Console.WriteLine($"SQLSelectASPUsers");
             string sql = "select * from aspnetusers";
             Dictionary<string, AccountModel> accs = new();
 
-            bool connected = IsConnect();
+            bool connected = await IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);
@@ -120,13 +124,14 @@ namespace HyperCube
 
         public async Task<Dictionary<int, SmartContract>> SQLSelectContracts(int blockchainId)
         {
+            Console.WriteLine($"SQLSelectContracts");
             var bc = Blockchain.Find(blockchainId);
             if (bc != null)
             {
                 string sql = "select * from smart_contracts where blockchain_id =" + blockchainId;
                 Dictionary<int, SmartContract> contracts = new();
 
-                bool connected = IsConnect();
+                bool connected = await IsConnect();
                 if (connected)
                 {
                     SQLcom = new(sql, Connection);
@@ -171,10 +176,11 @@ namespace HyperCube
 
         public async Task<Dictionary<int, ArticleModel>> SQLSelectArticles(string sql)
         {
+            Console.WriteLine($"SQLSelectArticles");
             Dictionary<int, ArticleModel> articleModels = new();
             Models.ArticleModel articleModel;
 
-            bool connected = IsConnect();
+            bool connected = await IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);
@@ -214,7 +220,7 @@ namespace HyperCube
             if (check)
             {
                 Console.WriteLine($"SQLSelectComplex check");
-                connected = IsConnect();
+                connected = await IsConnect();
             }
             if (connected)
             {
@@ -270,9 +276,10 @@ namespace HyperCube
 
         public async Task<ArticleModel> SQLSelectArticle(string sql)
         {
+            Console.WriteLine($"SQLSelectArticle");
             ArticleModel articleModel = new();
 
-            bool connected = IsConnect();
+            bool connected = await IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);
@@ -304,7 +311,8 @@ namespace HyperCube
 
         public async Task<string> SQLSelectUUID(string sql)
         {
-            bool connected = IsConnect();
+            Console.WriteLine($"SQLSelectUUID");
+            bool connected = await IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);
@@ -316,7 +324,8 @@ namespace HyperCube
 
         public async Task<int> SQLSelectCount(string sql)
         {
-            bool connected = IsConnect();
+            Console.WriteLine($"SQLSelectCount");
+            bool connected = await IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);

+ 4 - 4
Pages/DocEdit.razor.cs

@@ -124,14 +124,14 @@ namespace HyperCube.Pages
                         $"date_publish='{articleModel.PublishDate.ToString("yyyy-MM-dd")}', annotation='{articleModel.Annotation}', " +
                         $"keywords='{articleModel.Keywords}', rating={articleModel.Rating} " +
                     $"WHERE id={docID}";
-                dbCon.SQLInsert(stringSQL);
+                await dbCon.SQLInsert(stringSQL);
             }
             else
             {
                 stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords) " +
                     $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
                     $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
-                id = dbCon.SQLInsert(stringSQL);
+                id = await dbCon.SQLInsert(stringSQL);
                 NewProjectSmopp(id);
             }
 
@@ -140,7 +140,7 @@ namespace HyperCube.Pages
 
             stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
                 $"VALUES ('{id}', '{action_type}', '{currentAcc.UUID}')";
-            dbCon.SQLInsert(stringSQL);
+            await dbCon.SQLInsert(stringSQL);
 
             Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(articleModel, articleModelClone);
             foreach (KeyValuePair<string, PropertyInfo> prop in propDict)
@@ -149,7 +149,7 @@ namespace HyperCube.Pages
 
                 stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
                     $"VALUES ('{id}', '{currentAcc.UUID}', '{prop.Key}', '{prop.Value.GetValue(articleModelClone, null)}', '{prop.Value.GetValue(articleModel, null)}')";
-                dbCon.SQLInsert(stringSQL);
+                await dbCon.SQLInsert(stringSQL);
             }
 
             dbCon.Close();