public static void T_DropCreateInsert()
        {
            var connStr = GetMySqlConnString();
            var conn = new MySqlConnection(connStr);
            var tc = new TaskChain();
            conn.AsyncOpen(tc);
            {
                //1. drop tabled
                var cmd = new MySqlCommand("drop table if exists user_info2", conn);
                cmd.AsyncExecuteNonQuery(tc);
            } 
            {
                var cmd = new MySqlCommand("drop table if exists user_info2", conn);
                cmd.AsyncExecuteNonQuery(tc);
            }
            //2. create new one
            {
                var cmd = new MySqlCommand("create table user_info2(uid int(10),u_name varchar(45));", conn);
                cmd.AsyncExecuteNonQuery(tc);
            }
            //3. add some data
            {
                var cmd = new MySqlCommand("insert into user_info2(uid, u_name) values(?uid, 'abc')", conn);
                cmd.Parameters.AddWithValue("?uid", 10);
                cmd.AsyncExecuteNonQuery(tc);
            }

            Report.WriteLine("ok");
            conn.AsyncClose(tc);
            tc.Start();
        }
Exemple #2
0
 public static ActionTask AsyncExecuteReader(this MySqlCommand cmd, TaskChain ch, Action <MySqlDataReader> readerReady)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         cmd.InternalExecuteReader(subtable =>
         {
             //this method is respond for call next ***
             ch.AutoCallNext = true;
             //**
             //fetch data
             while (subtable.InternalRead())
             {
                 readerReady(subtable);
                 if (subtable.StopReadingNextRow)
                 {
                     break;
                 }
             }
             //
             subtable.Close(() => { });
             //
             if (ch.AutoCallNext)
             {
                 ch.Next();
             }
         });
     }));
 }
Exemple #3
0
 public static ActionTask AsyncClose(this MySqlConnection conn, TaskChain ch)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         conn.Close(ch.Next);
     }));
     //not use autocall next task, let the connection call it when ready ***
 }
Exemple #4
0
 public static ActionTask AsyncClose(this MySqlDataReader reader, TaskChain ch)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         reader.Close(ch.Next);
     }));
     //not use autocall next task, let the reader call it when ready ***
 }
Exemple #5
0
 public static ActionTask AsyncExecuteNonQuery(this MySqlCommand cmd, TaskChain ch)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         cmd.ExecuteNonQuery(ch.Next);
     }));
     //not use autocall next task, let the cmd call it when ready ***
 }
        public static ActionTask AsyncExecuteNonQuery(this MySqlCommand cmd, TaskChain ch)
        {
            return ch.AddTask(() =>
            {
                ch.AutoCallNext = false;
                cmd.ExecuteNonQuery(ch.Next);

            });
            //not use autocall next task, let the cmd call it when ready ***
        }
        public static ActionTask AsyncClose(this MySqlConnection conn, TaskChain ch)
        {
            return ch.AddTask(() =>
            {
                ch.AutoCallNext = false;
                conn.Close(ch.Next);

            });
            //not use autocall next task, let the connection call it when ready ***
        }
        public static void T_AsyncSocket2()
        {
            var connStr = GetMySqlConnString();
            var conn = new MySqlConnection(connStr);
            var tc = new TaskChain();
            conn.AsyncOpen(tc);
            conn.AsyncClose(tc);
            tc.Start();

        }
Exemple #9
0
 public static ActionTask AsyncOpen(this MySqlConnection conn, TaskChain ch)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         //open connection async
         //after finish then call next task in task chain
         conn.InternalOpen(ch.Next);
     }));
     //not use autocall next task, let the connection call it when ready ***
 }
 public static ActionTask AsyncOpen(this MySqlConnection conn, TaskChain ch)
 {
     return ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         //open connection async
         //after finish then call next task in task chain
         conn.InternalOpen(ch.Next);
     });
     //not use autocall next task, let the connection call it when ready ***
 }
        public static void T_InsertAndSelect_Async()
        {


            System.Diagnostics.Stopwatch stopW = new System.Diagnostics.Stopwatch();
            stopW.Start();
            var connStr = GetMySqlConnString();
            var conn = new MySqlConnection(connStr);
            conn.UseConnectionPool = true;

            var tc = new TaskChain();
            conn.AsyncOpen(tc);

            {
                //drop table if exist
                string sql = "drop table if exists test001";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);
            }


            {
                string sql = "create table test001(col_id  int(10) unsigned not null auto_increment, col1 int(10)," +
                   "col2 char(2),col3 varchar(255),col4 datetime, primary key(col_id) )";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);
            }

            for (int i = 0; i < 2000; ++i)
            {

                string sql = "insert into test001(col1,col2,col3,col4) values(10,'AA','123456789','0001-01-01')";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);

            }

            conn.AsyncClose(tc);
            tc.WhenFinish(() =>
            {

                stopW.Stop();
                Report.WriteLine("avg:" + stopW.ElapsedTicks);
            });
            tc.Start();


        }
Exemple #12
0
 public static ActionTask AsyncExecuteScalar <T>(this MySqlCommand cmd, TaskChain ch, Action <T> resultReady)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         cmd.ExecuteScalar <T>(result =>
         {
             ch.AutoCallNext = true;
             resultReady(result);
             if (ch.AutoCallNext)
             {
                 ch.Next();
             }
         });
     }));
     //not use autocall next task, let the cmd call it when ready ***
 }
 public static ActionTask AsyncExecuteScalar(this MySqlCommand cmd, TaskChain ch, Action<object> resultReady)
 {
     return ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         cmd.ExecuteScalar(result =>
         {
             ch.AutoCallNext = true;
             resultReady(result);
             if (ch.AutoCallNext)
             {
                 ch.Next();
             }
         });
     });
     //not use autocall next task, let the cmd call it when ready ***
 }
 public static ActionTask AsyncExecuteSubTableReader(this MySqlCommand cmd, TaskChain ch, Action<MySqlDataReader> readerReady)
 {
     return ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         cmd.ExecuteSubTableReader(subtable =>
         {
             //this method is respond for call next ***
             ch.AutoCallNext = true;
             readerReady(subtable);
             if (ch.AutoCallNext)
             {
                 ch.Next();
             }
         });
     });
     //not use autocall next task, let the cmd call it when ready ***
 }
Exemple #15
0
 public static ActionTask AsyncExecuteSubTableReader(this MySqlCommand cmd, TaskChain ch, Action <MySqlDataReader> readerReady)
 {
     return(ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         cmd.InternalExecuteSubTableReader(subtable =>
         {
             //this method is respond for call next ***
             ch.AutoCallNext = true;
             readerReady(subtable);
             if (ch.AutoCallNext)
             {
                 ch.Next();
             }
         });
     }));
     //not use autocall next task, let the cmd call it when ready ***
 }
        static void InsertData(MySqlConnection conn, TaskChain tc)
        {
            string sql = "insert into test001(col1,col2,col3,col4) values(10,'AA','123456789','0001-01-01')";
            var cmd = new MySqlCommand(sql, conn);
            cmd.AsyncExecuteNonQuery(tc);
            tc.AddTask(() =>
            {
                var lastInsertId = cmd.LastInsertedId;
            });

        }
        public static void T_InsertAndSelect()
        {

            try
            {
                var tc = new TaskChain();

                var connStr = GetMySqlConnString();
                var conn = new MySqlConnection(connStr);
                conn.UseConnectionPool = true;
                conn.AsyncOpen(tc);

                DropTableIfExists(conn, tc);
                CreateTable(conn, tc);
                for (int i = 0; i < 1; ++i)
                {
                    InsertData(conn, tc);
                }
                SelectDataBack(conn, tc);

                conn.AsyncClose(tc);
                //
                tc.WhenFinish(() =>
                {

                });
                tc.BeforeEachTaskBegin(() =>
                {

                });
                tc.Start();

            }
            catch (Exception ex)
            {

            }
        }
 public static ActionTask AsyncClose(this MySqlDataReader reader, TaskChain ch)
 {
     return ch.AddTask(() =>
     {
         ch.AutoCallNext = false;
         reader.Close(ch.Next);
     });
     //not use autocall next task, let the reader call it when ready ***
 }
Exemple #19
0
 public BasicTaskBase(TaskChain tc)
 {
     this.OwnerTaskChain = tc;
 }
Exemple #20
0
 public ActionTask(TaskChain tc, Action action)
     : base(tc)
 {
     this.action = action;
 }
 static void DropTableIfExists(MySqlConnection conn, TaskChain tc)
 {
     string sql = "drop table if exists test001";
     var cmd = new MySqlCommand(sql, conn);
     cmd.AsyncExecuteNonQuery(tc);
 }
 static void CreateTable(MySqlConnection conn, TaskChain tc)
 {
     string sql = "create table test001(col_id  int(10) unsigned not null auto_increment, col1 int(10)," +
         "col2 char(2),col3 varchar(255),col4 datetime, primary key(col_id) )";
     var cmd = new MySqlCommand(sql, conn);
     cmd.AsyncExecuteNonQuery(tc);
 }
        public static void T_InsertAndSelect_Async3()
        {
            System.Diagnostics.Stopwatch stopW = new System.Diagnostics.Stopwatch();
            stopW.Start();
            var connStr = GetMySqlConnString();
            var conn = new MySqlConnection(connStr);
            conn.UseConnectionPool = true;

            var tc = new TaskChain();
            //add task chain too connection object 
            conn.AsyncOpen(tc);
            //-----------------------------------------
            {
                //drop table if exist
                string sql = "drop table if exists test001";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);
            }
            //----------------------------------------- 
            {
                //drop table if exist
                string sql = "drop table if exists test001";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);
            }
            //----------------------------------------- 
            {
                string sql = "create table test001(col_id  int(10) unsigned not null auto_increment, col1 int(10)," +
                   "col2 char(2),col3 varchar(255),col4 datetime, primary key(col_id) )";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);
            }
            //-----------------------------------------
            for (int i = 0; i < 100; ++i)
            {
                string sql = "insert into test001(col1,col2,col3,col4) values(10,'AA','123456789','0001-01-01')";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteNonQuery(tc);
            }
            //-----------------------------------------
            {
                string sql = "select * from test001";
                var cmd = new MySqlCommand(sql, conn);

                cmd.AsyncExecuteSubTableReader(tc, subtable =>
                {
                    //when new task is add after tc is started
                    //then this new task is immmediately insert 
                    //after current task
                   
                });
            }
            {
                string sql = "select sysdate()";
                var cmd = new MySqlCommand(sql, conn);
                cmd.AsyncExecuteScalar(tc, obj =>
                {

                });
            }
            //-----------------------------------------
            conn.AsyncClose(tc);
            tc.WhenFinish(() =>
            {
                stopW.Stop();
                Report.WriteLine("avg:" + stopW.ElapsedTicks);
            });
            tc.BeforeEachTaskBegin(() =>
            {
                Console.WriteLine(tc.CurrentTaskIndex + "/" + tc.TaskCount);
            });
            //----------------------------------------
            tc.Start();
        }
        public static void T_Select_sysdate3()
        {
            //prefer this

            var connStr = GetMySqlConnString();
            var conn = new MySqlConnection(connStr);
            var tc = new TaskChain();
            conn.AsyncOpen(tc);
            var cmd = new MySqlCommand("select sysdate()", conn);
            cmd.AsyncExecuteSubTableReader(tc, reader =>
            {
                if (reader.Read())
                {
                    var dtm = reader.GetDateTime(0);
                }
            });
            conn.AsyncClose(tc);
            tc.WhenFinish(() =>
            {

            });
            tc.Start();
        }
        public ActionTask(TaskChain tc, Action action)
            : base(tc)
        {

            this.action = action;
        }
        static void SelectDataBack(MySqlConnection conn, TaskChain tc)
        {


            string sql = "select * from test001";
            var cmd = new MySqlCommand(sql, conn);

            tc.AddTask(() =>
            {
#if DEBUG
                conn.dbugPleaseBreak = true;
#endif
            });

            //this is very basic mapper***
            var mapper = Mapper.Map((SimpleInfo t, int col_id, string col2, string col3) =>
            {
                t.col1 = col_id;
                t.col2 = col2;
            });

            cmd.AsyncExecuteSubTableReader(tc, reader =>
            {

                mapper.DataReader = reader;
                while (reader.Read())
                {
                    var simpleInfo = mapper.Map(new SimpleInfo());
                }

                ////simple map query result to member of the target object  
                ////we create simpleinfo and use mapper to map field 


                tc.AutoCallNext = reader.CurrentSubTable.IsLastTable;

            });
        }
 public BasicTaskBase(TaskChain tc)
 {
     this.OwnerTaskChain = tc;
 }