Skip to content

Commit 9576c12

Browse files
authored
0.5
1 parent 28bc90a commit 9576c12

File tree

6 files changed

+65
-42
lines changed

6 files changed

+65
-42
lines changed

AnyBaseLib.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@
1010
</PropertyGroup>
1111

1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
13-
<DebugType>none</DebugType>
13+
<DebugType>portable</DebugType>
1414
</PropertyGroup>
1515

1616
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
17-
<DebugType>none</DebugType>
17+
<DebugType>portable</DebugType>
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
21-
<DebugType>none</DebugType>
21+
<DebugType>portable</DebugType>
2222
</PropertyGroup>
2323

2424
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
25-
<DebugType>none</DebugType>
25+
<DebugType>portable</DebugType>
2626
</PropertyGroup>
2727

2828
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='1|AnyCPU'">
29-
<DebugType>none</DebugType>
29+
<DebugType>portable</DebugType>
3030
</PropertyGroup>
3131

3232
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='1|x64'">
33-
<DebugType>none</DebugType>
33+
<DebugType>portable</DebugType>
3434
</PropertyGroup>
3535

3636
<ItemGroup>

Bases/Common.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@
99
using System.Runtime.CompilerServices;
1010
using System.Reflection;
1111
using System.Data;
12+
using System.Reflection.Metadata.Ecma335;
1213

1314
namespace AnyBaseLib.Bases
1415
{
1516
internal static class Common
1617
{
17-
private static string logpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"queries.txt");
18+
//private static string logpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"queries.txt");
1819
public static string _PrepareClear(string q, List<string> args)
1920
{
2021
var new_q = q;
21-
if(args != null) foreach (var arg in args)
22+
if(args != null) foreach (var arg in args.ToList())
2223
{
2324
var regex = new Regex(Regex.Escape("{ARG}"));
2425
var new_q2 = regex.Replace(new_q, _PrepareArg(arg), 1);
25-
if (new_q2 == new_q) throw new Exception("Mailformed query [Too many args]");
26+
if (new_q2 == new_q) throw new Exception("Mailformed query [Too many args in params]");
2627
new_q = new_q2;
2728
}
28-
if (new_q.Contains("{ARG}")) throw new Exception("Mailformed query [Not enough args]");
29+
if (new_q.Contains("{ARG}")) throw new Exception("Mailformed query [Not enough args in params]");
2930
return new_q;
3031
}
3132

@@ -93,9 +94,10 @@ public static bool Init(DbConnection conn, string name)
9394
}
9495
}
9596

96-
public static void QueryAsync(DbConnection conn, string q, Action<List<List<string>>> action = null, bool non_query = false)
97+
public static void QueryAsync(DbConnection conn, string q, Action<List<List<string>>> action = null, bool non_query = false, bool close = true)
9798
{
98-
var task = new Task<List<List<string>>>(() => Query(conn, q, non_query));
99+
var task = new Task<List<List<string>>>(() => Query(conn, q, non_query, close));
100+
//var task = Task.Run<List<List<string>>>(() => Query(conn, q, non_query, close));
99101
task.Start();
100102
if (action != null) task.ContinueWith((obj) => action(obj.Result));
101103
}
@@ -108,17 +110,23 @@ public static void QueryDapperAsync(DbConnection conn, Type type, string q, Acti
108110
if (action != null) task.ContinueWith((obj) => action(obj.Result));
109111
}
110112
*/
111-
public static List<List<string>> Query(DbConnection conn, string q, bool non_query = false)
113+
public static List<List<string>> Query(DbConnection conn, string q, bool non_query = false, bool close = false)
112114
{
113115
try
114116
{
115-
return Common._Query(conn, q, non_query);
117+
118+
var ret = _Query(conn, q, non_query);
119+
if (close) conn.Close();
120+
return ret;
121+
116122
}
117123

118124
catch (Exception e)
119125
{
126+
//if (close) conn.Close();
120127
Console.WriteLine($"[Query] Error was caused while querying \"{q}\":\n{e.Message}\n\n{e.StackTrace}");
121128
}
129+
122130
return null;
123131
}
124132
/*

Bases/MySQL.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ internal class MySQLDriver : IAnyBase
1616
private CommitMode commit_mode;
1717
private bool trans_started;
1818
private DbTransaction transaction;
19+
private string builder;
20+
1921

2022
public void Set(CommitMode commit_mode, string db_name, string db_host, string db_user = "", string db_pass = "")
2123
{
@@ -28,7 +30,7 @@ public void Set(CommitMode commit_mode, string db_name, string db_host, string d
2830
db_port = uint.Parse(db_host_arr[1]);
2931

3032

31-
var builder = new MySqlConnectionStringBuilder
33+
builder = new MySqlConnectionStringBuilder
3234
{
3335
Server = db_server,
3436
Database = db_name,
@@ -37,9 +39,9 @@ public void Set(CommitMode commit_mode, string db_name, string db_host, string d
3739
SslMode = MySqlSslMode.Preferred,
3840
Port = db_port
3941

40-
};
42+
}.ConnectionString;
4143

42-
dbConn = new MySqlConnection(builder.ConnectionString);
44+
dbConn = GetNewConn(false);
4345

4446
if (commit_mode != CommitMode.AutoCommit)
4547
new Task(TimerCommit).Start();
@@ -52,12 +54,13 @@ private void TimerCommit()
5254
if (trans_started)
5355
SetTransState(false);
5456
Thread.Sleep(5000);
57+
//Task.Delay(5000);
5558
}
5659
}
5760

5861
public List<List<string>> Query(string q, List<string> args, bool non_query = false)
5962
{
60-
if (commit_mode != CommitMode.AutoCommit)
63+
if (commit_mode == CommitMode.TimerCommit)
6164
{
6265
if (!trans_started && non_query) SetTransState(true);
6366
else
@@ -72,16 +75,27 @@ public List<List<string>> Query(string q, List<string> args, bool non_query = fa
7275

7376
public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
7477
{
75-
if (commit_mode != CommitMode.AutoCommit)
78+
/*
79+
if (commit_mode == CommitMode.TimerCommit)
7680
{
7781
if (!trans_started && non_query) SetTransState(true);
7882
else
7983
{
8084
if (trans_started && !non_query) SetTransState(false);
8185
}
8286
}
87+
*/
88+
8389

84-
Common.QueryAsync(dbConn, Common._PrepareClear(q, args), action, non_query);
90+
Common.QueryAsync(GetNewConn(), Common._PrepareClear(q, args), action, non_query);
91+
}
92+
93+
private MySqlConnection GetNewConn(bool open = true)
94+
{
95+
var conn = new MySqlConnection(builder);
96+
97+
if (open) conn.Open();
98+
return conn;
8599
}
86100

87101
public DbConnection GetConn()
@@ -101,7 +115,7 @@ private void SetTransState(bool state)
101115
transaction.Rollback();
102116
else
103117
transaction.Commit();
104-
transaction.Dispose();
118+
//transaction.Dispose();
105119
trans_started = false;
106120
}
107121
}

Bases/Postgre.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Data.Common;
66
using System.Linq;
7+
using System.Reflection.Metadata.Ecma335;
78
using System.Text;
89
using System.Threading.Tasks;
910
using System.Transactions;
@@ -16,6 +17,7 @@ internal class PostgreDriver: IAnyBase
1617
private CommitMode commit_mode;
1718
private bool trans_started;
1819
private DbTransaction transaction;
20+
private string builder;
1921

2022
public void Set(CommitMode commit_mode, string db_name, string db_host, string db_user = "", string db_pass = "")
2123
{
@@ -27,7 +29,7 @@ public void Set(CommitMode commit_mode, string db_name, string db_host, string d
2729
if (db_host_arr.Length > 1)
2830
db_port = int.Parse(db_host_arr[1]);
2931

30-
var builder = new NpgsqlConnectionStringBuilder
32+
builder = new NpgsqlConnectionStringBuilder
3133
{
3234
Host = db_server,
3335
Database = db_name,
@@ -36,9 +38,9 @@ public void Set(CommitMode commit_mode, string db_name, string db_host, string d
3638
SslMode = SslMode.Prefer,
3739
Port = db_port
3840

39-
};
41+
}.ConnectionString;
4042

41-
dbConn = new NpgsqlConnection(builder.ConnectionString);
43+
dbConn = GetNewConn();
4244

4345
if (commit_mode != CommitMode.AutoCommit)
4446
new Task(TimerCommit).Start();
@@ -70,17 +72,24 @@ public List<List<string>> Query(string q, List<string> args, bool non_query = fa
7072
}
7173
public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
7274
{
73-
75+
/*
7476
if (commit_mode != CommitMode.AutoCommit)
7577
{
7678
if (!trans_started && non_query) SetTransState(true);
7779
else
7880
{
7981
if (trans_started && !non_query) SetTransState(false);
8082
}
81-
}
83+
}*/
8284

83-
Common.QueryAsync(dbConn, Common._PrepareClear(q, args), action, non_query);
85+
Common.QueryAsync(GetNewConn(), Common._PrepareClear(q, args), action, non_query);
86+
}
87+
88+
private NpgsqlConnection GetNewConn()
89+
{
90+
var conn = new NpgsqlConnection(builder);
91+
conn.Open();
92+
return conn;
8493
}
8594

8695
private void SetTransState(bool state)
@@ -96,7 +105,7 @@ private void SetTransState(bool state)
96105
transaction.Rollback();
97106
else
98107
transaction.Commit();
99-
transaction.Dispose();
108+
//transaction.Dispose();
100109
trans_started = false;
101110
}
102111
}

Bases/SQLite.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class SQLiteDriver : IAnyBase
1919

2020
private SqliteConnection dbConn;
2121
private CommitMode commit_mode;
22-
private bool trans_started;
22+
private bool trans_started = false;
2323
private DbTransaction transaction;
2424

2525
public void Set(CommitMode commit_mode, string db_name, string db_host = "", string db_user = "", string db_pass = "")
@@ -38,6 +38,7 @@ private void TimerCommit()
3838
if (trans_started)
3939
SetTransState(false);
4040
Thread.Sleep(5000);
41+
//Task.Delay(5000);
4142
}
4243
}
4344
private string _FixForSQLite(string q)
@@ -62,16 +63,7 @@ public List<List<string>> Query(string q, List<string> args = null, bool non_que
6263

6364
public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
6465
{
65-
if (commit_mode != CommitMode.AutoCommit)
66-
{
67-
if (!trans_started && non_query) SetTransState(true);
68-
else
69-
{
70-
if (trans_started && !non_query) SetTransState(false);
71-
}
72-
}
73-
74-
Common.QueryAsync(dbConn, Common._PrepareClear(_FixForSQLite(q), args), action, non_query);
66+
Common.QueryAsync(dbConn, Common._PrepareClear(_FixForSQLite(q), args), action, non_query, false);
7567
}
7668
/*
7769
public void QueryDapperAsync(Type type, string q, List<string> args = null, Action<object> action = null)
@@ -104,11 +96,11 @@ private void SetTransState(bool state)
10496
}
10597
else
10698
{
107-
if(commit_mode == CommitMode.NoCommit)
99+
if (commit_mode == CommitMode.NoCommit)
108100
transaction.Rollback();
109101
else
110102
transaction.Commit();
111-
transaction.Dispose();
103+
//transaction.Dispose();
112104
trans_started = false;
113105
}
114106
}

CAnyBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public static IAnyBase Base(string name)
2121
}
2222

2323
public static int Version()
24-
{ return 4; }
24+
{ return 5; }
2525
}
2626
}

0 commit comments

Comments
 (0)