示例#1
0
        private static async Task Main(string[] args)
        {
            await SetupAsync();

            var services = new ServiceCollection();

            services.AddLogging(o => o.AddConsole());

            var connection = new MySqlConnectionOptions
            {
                Host     = Host,
                Username = "******",
                Password = "******",
                Database = "test_db"
            };

            var connFactory = new MySqlConnectionFactory(new ConnectionFactoryOptions <MySqlConnectionOptions>
            {
                Write = connection,
                Read  = connection
            });

            var serviceProvider = services.BuildServiceProvider();

            var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
            var logger        = serviceProvider.GetRequiredService <ILogger <Program> >();

            var testRepo = new TestRepo(connFactory, new MySqlRepositoryOptions
            {
                FailOverRetryCount = 10,
                LoggerFactory      = loggerFactory
            });

            logger.LogInformation("Starting...");

            if (args.Contains("-write"))
            {
                while (true)
                {
                    logger.LogInformation("Writing...");

                    await testRepo.WriteAsync("INSERT IGNORE INTO `test_table` VALUES (1);");

                    await Task.Delay(TimeSpan.FromSeconds(1));
                }
            }

            if (args.Contains("-read"))
            {
                while (true)
                {
                    logger.LogInformation("Reading...");

                    await testRepo.ReadAsync <int>("SELECT * FROM `test_table`;");

                    await Task.Delay(TimeSpan.FromSeconds(1));
                }
            }
        }
        public void GetConnectionString_Default_Sane()
        {
            var options = new MySqlConnectionOptions();

            var connString = options.GetConnectionString();

            Assert.Equal("host=localhost;maxpoolsize=25;minpoolsize=1;", connString);
        }
示例#3
0
        public static void WriteHandshakeResponse41(ref PacketWriter writer, MySqlConnectionOptions options, HandshakeV10 initialHandshake)
        {
            // writer.WriteInt32((int)CapabilitiesFlags.Protocol41); // TODO:other flags

            // currently this code is borrowed from async MySQLConnector

            writer.WriteInt32((int)(
                                  CapabilitiesFlags.Protocol41 |
                                  CapabilitiesFlags.LongPassword |
                                  CapabilitiesFlags.SecureConnection |
                                  (initialHandshake.CapabilityFlags & CapabilitiesFlags.PluginAuth) |
                                  (initialHandshake.CapabilityFlags & CapabilitiesFlags.PluginAuthLenencClientData) |
                                  CapabilitiesFlags.MultiStatements |
                                  CapabilitiesFlags.MultiResults |
                                  CapabilitiesFlags.PSMultiResults |
                                  CapabilitiesFlags.LocalFiles |
                                  (string.IsNullOrWhiteSpace(options.Database) ? 0 : CapabilitiesFlags.ConnectWithDB) // |
                                                                                                                      //(cs.UseAffectedRows ? 0 : CapabilitiesFlags.FoundRows) |
                                                                                                                      //(cs.UseCompression ? CapabilitiesFlags.Compress : CapabilitiesFlags.None) |
                                                                                                                      //additionalCapabilities
                                  ));



            writer.WriteInt32(0x40000000); // TODO:max packet size
            writer.WriteByte((byte)CharacterSet.utf8mb4_bin);
            writer.WriteBytes(Empty23Bytes);
            writer.WriteNullTerminatedString(options.UserId);

            if (initialHandshake.CapabilityFlags.HasFlag(CapabilitiesFlags.PluginAuthLenencClientData))
            {
                var auth = CraeateNative41AuthResponse(initialHandshake.AuthPluginData, options.Password);
                writer.WriteByte((byte)auth.Length);
                writer.WriteBytes(auth);
            }
            else
            {
                writer.WriteByte(1);
            }

            if (initialHandshake.CapabilityFlags.HasFlag(CapabilitiesFlags.ConnectWithDB))
            {
                writer.WriteNullTerminatedString(options.Database);
            }

            if (initialHandshake.CapabilityFlags.HasFlag(CapabilitiesFlags.PluginAuth))
            {
                writer.WriteNullTerminatedString(initialHandshake.AuthPluginName);
            }

            if (initialHandshake.CapabilityFlags.HasFlag(CapabilitiesFlags.ConnectAttrs))
            {
                // TODO:other attributes...
            }
        }
        public void GetConnectionString_AllOptionsSet_Valid()
        {
            var options = new MySqlConnectionOptions
            {
                Host            = "why",
                Database        = "do you",
                MaximumPoolSize = 11,
                MinimumPoolSize = 2,
                Password        = "******",
                Port            = 8888,
                Username        = "******",
                LoadBalance     = LoadBalance.LeastConnections
            };

            var connString = options.GetConnectionString();

            Assert.Equal(
                "host=why;maxpoolsize=11;minpoolsize=2;database=do you;password=only call me;port=8888;username=when you're high?;loadbalance=leastconnections;",
                connString);
        }