@@ -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
0 commit comments