Skip to content

Commit

Permalink
Added async
Browse files Browse the repository at this point in the history
  • Loading branch information
NickFox007 authored May 16, 2024
1 parent 4f08460 commit b92dedf
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
56 changes: 53 additions & 3 deletions Bases/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Data.Common;
using Dapper;

namespace AnyBaseLib.Bases
{
Expand Down Expand Up @@ -36,11 +37,13 @@ public static string _PrepareArg(string arg)
return new_arg;
}


public static List<List<string>> _Query(DbConnection conn, string q, bool non_query)
{

var sql = conn.CreateCommand();
sql.CommandText = q;
//Console.WriteLine(q);
//Console.WriteLine($"Query: {q} [Non-query: {non_query}]");
if (!non_query)
{
var list = new List<List<string>>();
Expand All @@ -66,6 +69,7 @@ public static List<List<string>> _Query(DbConnection conn, string q, bool non_qu
return null;
}


public static bool Init(DbConnection conn, string name)
{
try
Expand All @@ -80,6 +84,21 @@ public static bool Init(DbConnection conn, string name)
}
}

public static void QueryAsync(DbConnection conn, string q, Action<List<List<string>>> action = null, bool non_query = false)
{
var task = new Task<List<List<string>>>(() => Query(conn, q, non_query));
task.Start();
if (action != null) task.ContinueWith((obj) => action(obj.Result));
}

/*
public static void QueryDapperAsync(DbConnection conn, Type type, string q, Action<object> action = null, bool non_query = false)
{
var task = new Task<object>(() => QueryDapper(conn, type, q, non_query));
task.Start();
if (action != null) task.ContinueWith((obj) => action(obj.Result));
}
*/
public static List<List<string>> Query(DbConnection conn, string q, bool non_query = false)
{
try
Expand All @@ -89,11 +108,42 @@ public static List<List<string>> Query(DbConnection conn, string q, bool non_que

catch (Exception e)
{
Console.WriteLine($"Error was caused: {e.Message}");
Console.WriteLine($"[Query] Error was caused while querying \"{q}\":\n{e.Message}");
}
return null;
}

/*
public static object QueryDapper(DbConnection conn, Type type, string q, bool non_query = false)
{
try
{
return Common._QueryDapper(conn, q, non_query);
}
catch (Exception e)
{
Console.WriteLine($"[QueryDapper] Error was caused while fetching query \"{q}\":\n{e.Message}");
}
return null;
}*/

/*
public static object QueryDapper(DbConnection conn, string q, bool non_query)
{
try
{
return Common._Query2(conn, q, non_query);
}
catch (Exception e)
{
Console.WriteLine($"Error was caused while fetching query \"{q}\":\n{e.Message}");
}
return null;
}
*/

}
public enum CommitMode
{
Expand Down
18 changes: 18 additions & 0 deletions Bases/MySQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ public List<List<string>> Query(string q, List<string> args, bool non_query = fa

}

public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
{
if (commit_mode != CommitMode.AutoCommit)
{
if (!trans_started && non_query) SetTransState(true);
else
{
if (trans_started && !non_query) SetTransState(false);
}
}

Common.QueryAsync(dbConn, Common._PrepareClear(q, args), action, non_query);
}

public DbConnection GetConn()
{ return dbConn; }


private void SetTransState(bool state)
{
if (state)
Expand Down
16 changes: 16 additions & 0 deletions Bases/Postgre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,20 @@ public List<List<string>> Query(string q, List<string> args, bool non_query = fa
}

return Common.Query(dbConn, Common._PrepareClear(q, args), non_query);
}
public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
{

if (commit_mode != CommitMode.AutoCommit)
{
if (!trans_started && non_query) SetTransState(true);
else
{
if (trans_started && !non_query) SetTransState(false);
}
}

Common.QueryAsync(dbConn, Common._PrepareClear(q, args), action, non_query);
}

private void SetTransState(bool state)
Expand All @@ -69,6 +82,9 @@ private void SetTransState(bool state)
}
}

public DbConnection GetConn()
{ return dbConn; }

public bool Init()
{
return Common.Init(dbConn, "PostgreSQL");
Expand Down
36 changes: 36 additions & 0 deletions Bases/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,42 @@ public List<List<string>> Query(string q, List<string> args = null, bool non_que
}

return Common.Query(dbConn, Common._PrepareClear(_FixForSQLite(q), args), non_query);
}

public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
{
if (commit_mode != CommitMode.AutoCommit)
{
if (!trans_started && non_query) SetTransState(true);
else
{
if (trans_started && !non_query) SetTransState(false);
}
}

Common.QueryAsync(dbConn, Common._PrepareClear(_FixForSQLite(q), args), action, non_query);
}
/*
public void QueryDapperAsync(Type type, string q, List<string> args = null, Action<object> action = null)
{
if (commit_mode != CommitMode.AutoCommit)
{
if (trans_started) SetTransState(false);
}
Common.QueryDapperAsync(dbConn, type, Common._PrepareClear(_FixForSQLite(q), args), action);
}
public object QueryDapper(Type type, string q, List<string> args = null, Action<object> action = null)
{
if (commit_mode != CommitMode.AutoCommit)
{
if (trans_started) SetTransState(false);
}
return Common.QueryDapper(dbConn, type, Common._PrepareClear(_FixForSQLite(q), args));
}
*/
private void SetTransState(bool state)
{
if(state)
Expand All @@ -71,6 +104,9 @@ private void SetTransState(bool state)
}
}

public DbConnection GetConn()
{ return dbConn; }

public bool Init()
{
SQLitePCL.Batteries.Init();
Expand Down

0 comments on commit b92dedf

Please sign in to comment.