public Connection(ConnectionConfig userConfig) { config = userConfig; recvBufferSize = userConfig.recvBufferSize; sendBufferSize = userConfig.sendBufferSize; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // switch ((CharSets)config.charsetNumber) { case CharSets.UTF8_GENERAL_CI: //_parser = new PacketParser(Encoding.UTF8); _writer = new MySqlStreamWriter(Encoding.UTF8); break; case CharSets.ASCII: //_parser = new PacketParser(Encoding.ASCII); _writer = new MySqlStreamWriter(Encoding.ASCII); break; default: throw new NotImplementedException(); } //------------------ //we share recvSendArgs between recvIO and sendIO //similar to simple http //it is simple, (NOT duplex like web socket) //------------------ recvSendArgs = new SocketAsyncEventArgs(); recvSendArgs.SetBuffer(new byte[recvBufferSize + sendBufferSize], 0, recvBufferSize + sendBufferSize); recvIO = new RecvIO(recvSendArgs, recvSendArgs.Offset, recvBufferSize, HandleReceive); sendIO = new SendIO(recvSendArgs, recvSendArgs.Offset + recvBufferSize, sendBufferSize, HandleSend); //------------------ //common(shared) event listener*** recvSendArgs.Completed += (object sender, SocketAsyncEventArgs e) => { switch (e.LastOperation) { case SocketAsyncOperation.Receive: recvIO.ProcessReceivedData(); break; case SocketAsyncOperation.Send: sendIO.ProcessWaitingData(); break; default: throw new ArgumentException("The last operation completed on the socket was not a receive or send"); } }; //------------------ recvSendArgs.AcceptSocket = socket; _mysqlParserMx = new MySqlParserMx(config); }
public static void Test1_Delete() { ConnectionConfig config = new ConnectionConfig("root", "root"); config.database = "test"; MySqlConnection sqlConn = new MySqlConnection(config.host, config.user, config.password, config.database); sqlConn.UseConnectionPool = true; sqlConn.InternalOpen(); string sql = "select * from ??t1 where ??c1 > ?n1 and ??c1 < ?n2"; int testN1 = 4510; int testN2 = 4520; MySqlCommand command = new MySqlCommand(sql, sqlConn); command.Parameters.SetSqlPart("??t1", "saveimage"); command.Parameters.SetSqlPart("??c1", "idsaveImage"); command.Parameters.AddWithValue("?n1", testN1); command.Parameters.AddWithValue("?n2", testN2); }
public static void Test1_Insert() { string filename = "TestMe.png";//216,362 bytes //filename = "Colorful.jpg";//885,264 bytes //filename = "TestJpg.jpg";//2,066 bytes byte[] buffer = File.ReadAllBytes("D:\\[]Photo\\" + filename); ConnectionConfig config = new ConnectionConfig("root", "root"); config.database = "test"; MySqlConnection sqlConn = new MySqlConnection(config.host, config.user, config.password, config.database); sqlConn.UseConnectionPool = true; sqlConn.InternalOpen(); string sql = "INSERT INTO ??t1 SET ??c2 = ?buffer1"; MySqlCommand command = new MySqlCommand(sql, sqlConn); command.Parameters.SetSqlPart("??t1", "saveimage"); command.Parameters.SetSqlPart("??c2", "saveImagecol"); command.Parameters.AddWithValue("?buffer1", buffer); command.ExecuteNonQuery(); }
public static void Test1_Select() { ConnectionConfig config = new ConnectionConfig("root", "root"); config.database = "test"; MySqlConnection sqlConn = new MySqlConnection(config.host, config.user, config.password, config.database); sqlConn.UseConnectionPool = true; sqlConn.InternalOpen(); string sql = "select * from ??t1 where ??c1 > ?n1 and ??c1 < ?n2"; int testN1 = 4510; int testN2 = 4520; MySqlCommand command = new MySqlCommand(sql, sqlConn); command.Parameters.SetSqlPart("??t1", "saveimage"); command.Parameters.SetSqlPart("??c1", "idsaveImage"); command.Parameters.AddWithValue("?n1", testN1); command.Parameters.AddWithValue("?n2", testN2); var reader = command.ExecuteReader(); int count = 0; while (reader.Read()) { Console.WriteLine("Id : " + reader.GetInt32(0)); Console.WriteLine("Buffer size : " + reader.GetBuffer(1).Length); ++count; if (count >= 3) { break; } } reader.Close(); }
public static void Test1_OldVersionTest() { string filename; filename = "TestMe.png";//216,362 bytes //filename = "Colorful.jpg";//885,264 bytes //filename = "TestJpg.jpg";//2,066 bytes byte[] buffer; buffer = File.ReadAllBytes("D:\\[]Photo\\" + filename); //buffer = new byte[500500]; //Stream stReader = new Stream("D:\\[]Photo\\TestJpg.jpg"); //BinaryReader binaryReader = new BinaryReader(stReader); var ss = new System.Diagnostics.Stopwatch(); ss.Start(); string sql; string sql2; //please note the //field or column binding is extension, must start with ?? //sql = "INSERT INTO ??t1 (??c1, ??c2) VALUES (?n1 , ?buffer1)"; sql = "INSERT INTO ??t1 SET ??c2 = ?buffer1"; //sql = "select * from ??t1 where ??c1 > ?n1 and ?c1 < ?n2"; //sql = "select * from ??t1 where ??c1 = 4579"; //sql = "select 1+?n3 as test1"; //sql = "select concat(?s1,?s2,?s1,?s2,?s1,?s2,?s1,?s2,?s1,?s2) as test1"; //sql = "select concat(?s1,?s2,?s1,?s2) as test1"; //sql = "SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate" // + " FROM Orders INNER JOIN Customers" // + " ON Orders.CustomerID = Customers.CustomerID;"; //sql2 = "select * from ?t1 where ?c1 > ?n1 and ?c1 < ?n2"; //sql = "INSERT INTO ?t1 ( ?c2, ?c3) VALUES ( ?s1, ?s2)"; //sql = "DELETE FROM ?t1 WHERE ?c1=?n1"; //sql = "UPDATE ?t1 SET ?c2=?s1 WHERE ?c1=?n1"; //CommandParameters cmdValues = new CommandParameters(); //sql = "select ?n1+?n2 as test1"; int testN1 = 4520; int testN2 = 4530; sql = "select * from ??t1 where ??c1 > ?n1 and ??c1 < ?n2"; //sql = "select * from ?t1 where ?c1 = ?n2"; //sql = "select ?n1+?n2 as test1"; CommandParams cmd2Values = new CommandParams(); cmd2Values.SetSqlPart("??t1", "saveimage"); cmd2Values.SetSqlPart("??c1", "idsaveImage"); //cmd2Values.AddField("c2", "saveImagecol"); cmd2Values.AddWithValue("?n1", testN1); cmd2Values.AddWithValue("?n2", testN2); //cmd2Values.AddValue("n3", 29.5); //cmd2Values.AddValue("s1", "foo"); //cmd2Values.AddValue("s2", "bar"); //cmd2Values.AddValue("buffer1", buffer); ConnectionConfig config = new ConnectionConfig("root", "root"); config.database = "test"; //MySqlConnection sqlConn = new MySqlConnection(config.host, config.user, config.password, config.database); //sqlConn.UseConnectionPool = true; //sqlConn.Open(); //MySqlCommand command = new MySqlCommand(sql, sqlConn); //command.Parameters.AddTable("t1", "saveimage"); //command.Parameters.AddField("c1", "idsaveImage"); //command.Parameters.AddValue("n1", testN1); //command.Parameters.AddValue("n2", testN2); //var reader = command.ExecuteReader(); //reader.Read(); //Connection connection = sqlConn.Conn;/*ConnectionPool.GetConnection(new MySqlConnectionString(config.host, config.user, config.password, config.database));*/ Connection connection = new Connection(config); if (connection == null) { connection = new Connection(config); connection.IsStoredInConnPool = false; connection.Connect(); } int count = 3; int fCase = 1; for (int i = 0; i < count; i++) { int j = 0; //query = connection.CreateQuery(sql, cmdValues); //query = connection.CreateQuery(cmd2Values); //query.ExecutePrepareQuery(cmd2Values); var query = new Query(connection, sql, cmd2Values); query.SetResultListener(tableResult => { if (query.LoadError != null) { Console.WriteLine("Error : " + query.LoadError.message); } else if (query.OkPacket != null) { Console.WriteLine("i : " + i + ", OkPacket : [affectedRow] >> " + query.OkPacket.affectedRows); Console.WriteLine("i : " + i + ", OkPacket : [insertId] >> " + query.OkPacket.insertId); } else { var thead = tableResult.tableHeader; int col_idsaveImage = thead.GetFieldIndex("idsaveImage"); int col_saveImageCol = thead.GetFieldIndex("saveImagecol"); int col_test = thead.GetFieldIndex("test1"); //if (col_idsaveImage < 0 || col_saveImageCol < 0) //{ // throw new Exception(); //} Console.WriteLine("Result : "); //while (query.ReadRow()) //{ // if (col_test == 0) // { // Console.WriteLine("Result of " + "test1 : >> " + query.Cells[col_test] + " <<"); // } // else // { // Console.WriteLine("Id : " + query.Cells[col_idsaveImage]); // Console.WriteLine("Buffer size : " + query.Cells[col_saveImageCol].myBuffer.Length); // } // //Console.WriteLine(query.GetFieldData("myusercol1")); // if (++j > 3) // { // break; // } //} } }); testN1 += 10; testN2 += 10; cmd2Values.AddWithValue("?n1", testN1); cmd2Values.AddWithValue("?n2", testN2); query.Execute(true); //*** query.Close(); connection.Disconnect(); connection = new Connection(config); connection.Connect(); //j = 0; //query = connection.CreateQuery(sql2, prepare); //query.ExecuteQuery(); //if (query.loadError != null) //{ // Console.WriteLine("Error : " + query.loadError.message); //} //else //{ // while (query.ReadRow() && j < 3) // { // Console.WriteLine(query.GetFieldData("idsaveImage")); // Console.WriteLine(query.GetFieldData("saveImagecol")); // //Console.WriteLine(query.GetFieldData("myusercol1")); // j++; // } //} //query.Close(); } ss.Stop(); long avg = ss.ElapsedMilliseconds / count; Console.WriteLine("Counting : " + count + " rounds. \r\nAverage Time : " + avg + " ms"); connection.Disconnect(); }
//------------------------- public MySqlParserMx(ConnectionConfig userConfig) { this.userConfig = userConfig; connParser = new MySqlConnectionPacketParser(); //tableHeader.TypeCast = this.config.typeCast; }