Skip to content

Commit 2de4e3b

Browse files
authored
0.9.3
1 parent e3802c4 commit 2de4e3b

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed

Bases/Common.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,8 @@ public static string _PrepareClear(string q, List<string> args)
3131
var new_q = q;
3232
if(args != null) foreach (var arg in args.ToList())
3333
{
34-
//var regex = new Regex(Regex.Escape("{ARG}"));
35-
36-
//var new_q2 = regex.Replace(new_q, _PrepareArg(arg), 1);
37-
3834
var new_q2 = ReplaceFirst(new_q, "{ARG}", _PrepareArg(arg));
3935

40-
4136
if (new_q2 == new_q) throw new Exception("Mailformed query [Too many args in params]");
4237
new_q = new_q2;
4338
}
@@ -65,17 +60,16 @@ public static string _PrepareArg(string arg)
6560

6661
public static List<List<string>> _Query(DbConnection conn, string q, bool non_query)
6762
{
68-
//File.AppendAllText(logpath, $"[{DateTime.Now}] {q} (Non-query: {non_query})\n");
6963
if (conn.State != ConnectionState.Open) conn.Open();
7064
var sql = conn.CreateCommand();
7165
sql.CommandText = q;
72-
//Console.WriteLine($"Query: {q} [Non-query: {non_query}]");
7366
if (!non_query)
7467
{
7568
var list = new List<List<string>>();
7669

77-
using (var reader = sql.ExecuteReader())
70+
using (var readerAs = sql.ExecuteReaderAsync())
7871
{
72+
var reader = readerAs.Result;
7973
while (reader.Read())
8074
{
8175
var fields = new List<string>();
@@ -116,23 +110,26 @@ public static bool Init(DbConnection conn, string name)
116110

117111
public static void QueryAsync(DbConnection conn, string q, Action<List<List<string>>> action = null, bool non_query = false, bool close = true)
118112
{
119-
if(action != null)
120-
Task.Run(() => Query(conn, q, non_query, close)).ContinueWith((obj) => action(obj.Result));
121-
else
122-
Task.Run(() => Query(conn, q, non_query, close));
113+
int wait_opened = 10;
114+
while(wait_opened > 0)
115+
{
116+
if (conn.State == ConnectionState.Open)
117+
break;
118+
wait_opened--;
119+
Task.Delay(150).Wait();
120+
}
123121

124-
}
122+
if (wait_opened == 0) throw new Exception("Error caused while open database connection");
125123

126-
/*
127-
public static void QueryDapperAsync(DbConnection conn, Type type, string q, Action<object> action = null, bool non_query = false)
128-
{
129-
var task = new Task<object>(() => QueryDapper(conn, type, q, non_query));
124+
var task = new Task<List<List<string>>>(() => Query(conn, q, non_query, close));
125+
if (action != null) task.ContinueWith(obj => action(obj.Result));
130126
task.Start();
131-
if (action != null) task.ContinueWith((obj) => action(obj.Result));
127+
132128
}
133-
*/
129+
134130
public static List<List<string>> Query(DbConnection conn, string q, bool non_query = false, bool close = false)
135131
{
132+
//Console.WriteLine("[ !!! DEBUG !!! ] Sync query . . .");
136133
try
137134
{
138135

@@ -144,7 +141,7 @@ public static List<List<string>> Query(DbConnection conn, string q, bool non_que
144141

145142
catch (Exception e)
146143
{
147-
//if (close) conn.Close();
144+
if (close) conn.Close();
148145
Console.WriteLine($"[Query] Error was caused while querying \"{q}\":\n{e.Message}\n\n{e.StackTrace}");
149146
}
150147

Bases/MySQL.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.Data.Common;
45
using System.Linq;
56
using System.Text;
@@ -54,7 +55,6 @@ private void TimerCommit()
5455
if (trans_started)
5556
SetTransState(false);
5657
Thread.Sleep(5000);
57-
//Task.Delay(5000);
5858
}
5959
}
6060

@@ -69,32 +69,24 @@ public List<List<string>> Query(string q, List<string> args, bool non_query = fa
6969
}
7070
}
7171

72-
return Common.Query(dbConn, Common._PrepareClear(q, args), non_query);
72+
return Common.Query(GetNewConn(), Common._PrepareClear(q, args), non_query);
7373

7474
}
7575

7676
public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
7777
{
78-
/*
79-
if (commit_mode == CommitMode.TimerCommit)
80-
{
81-
if (!trans_started && non_query) SetTransState(true);
82-
else
83-
{
84-
if (trans_started && !non_query) SetTransState(false);
85-
}
86-
}
87-
*/
88-
8978

90-
Common.QueryAsync(GetNewConn(), Common._PrepareClear(q, args), action, non_query);
79+
var task = new Task<MySqlConnection>(() => GetNewConn());
80+
task.ContinueWith((conn) => Common.QueryAsync(conn.Result, Common._PrepareClear(q, args), action, non_query));
81+
task.Start();
9182
}
9283

9384
private MySqlConnection GetNewConn(bool open = true)
9485
{
95-
var conn = new MySqlConnection(builder);
96-
97-
if (open) conn.Open();
86+
var conn = new MySqlConnection(builder);
87+
//Console.WriteLine("[ !!! DEBUG !!! ] Get new conn...");
88+
if(conn != null && conn.State != ConnectionState.Open && open) conn.Open();
89+
dbConn = conn;
9890
return conn;
9991
}
10092

@@ -103,7 +95,8 @@ public DbConnection GetConn()
10395

10496
public void Close()
10597
{
106-
dbConn.Close();
98+
if(dbConn != null && dbConn.State == ConnectionState.Open)
99+
dbConn.Close();
107100
}
108101
private void SetTransState(bool state)
109102
{

0 commit comments

Comments
 (0)