Beispiel #1
0
        public static async Task <List <Fortune> > LoadFortunesRows()
        {
            var fortunes = new List <Fortune>();

            var pooledConnection = await PooledConnections.GetConnection(DataProvider.ConnectionString);

            pooledConnection.Open();

            var pooledCommand = new PooledCommand("SELECT * FROM fortune", pooledConnection);
            var dataReader    = await pooledCommand.ExecuteReaderAsync(CommandBehavior.SingleResult& CommandBehavior.SequentialAccess);

            while (dataReader.Read())
            {
                fortunes.Add(new Fortune
                             (
                                 id: dataReader.GetInt32(0),
#if MYSQL
                                 //MariaDB ODBC connector does not correctly support Japanese characters in combination with default ADO.NET;
                                 //as a solution we custom read this string
                                 message: ReadColumn(dataReader, 1)
#else
                                 message: dataReader.GetString(1)
#endif
                             ));
            }

            dataReader.Close();
            pooledCommand.Release();
            pooledConnection.Release();

            fortunes.Add(new Fortune(id: 0, message: "Additional fortune added at request time."));
            fortunes.Sort();

            return(fortunes);
        }
Beispiel #2
0
        public static async Task <World[]> ReadMultipleRows(int count)
        {
            int    j      = 0;
            var    ids    = PlatformBenchmarks.BatchUpdateString.Ids;
            var    worlds = new World[count];
            string queryString;

            if (_queriesMultipleRows[count] != null)
            {
                queryString = _queriesMultipleRows[count];
            }
            else
            {
                var stringBuilder = PlatformBenchmarks.StringBuilderCache.Acquire();

                for (int i = 0; i < count; i++)
                {
                    stringBuilder.Append("SELECT * FROM world WHERE id=?;");
                }

                queryString = _queriesMultipleRows[count] = PlatformBenchmarks.StringBuilderCache.GetStringAndRelease(stringBuilder);
            }

            var pooledConnection = await PooledConnections.GetConnection(DataProvider.ConnectionString);

            pooledConnection.Open();

            var pooledCommand = new PooledCommand(queryString, pooledConnection);

            for (int i = 0; i < count; i++)
            {
                pooledCommand.CreateParameter(ids[i], DbType.Int32, _random.Next(1, 10001));
            }

            var dataReader = await pooledCommand.ExecuteReaderAsync(CommandBehavior.Default& CommandBehavior.SequentialAccess);

            do
            {
                dataReader.Read();

                worlds[j] = new World
                {
                    Id           = dataReader.GetInt32(0),
                    RandomNumber = dataReader.GetInt32(1)
                };

                j++;
            } while (await dataReader.NextResultAsync());

            dataReader.Close();
            pooledCommand.Release();
            pooledConnection.Release();

            return(worlds);
        }
Beispiel #3
0
        public static async Task <World[]> LoadMultipleUpdatesRows(int count)
        {
            var worlds = new World[count];

            var pooledConnection = await PooledConnections.GetConnection(DataProvider.ConnectionString);

            pooledConnection.Open();

            var(queryCommand, dbDataParameter) = CreateReadCommand(pooledConnection);

            for (int i = 0; i < count; i++)
            {
                worlds[i] = await ReadSingleRow(queryCommand);

                dbDataParameter.Value = _random.Next(1, 10001);
            }

            queryCommand.Release();

            var updateCommand = new PooledCommand(PlatformBenchmarks.BatchUpdateString.Query(count), pooledConnection);

            var ids     = PlatformBenchmarks.BatchUpdateString.Ids;
            var randoms = PlatformBenchmarks.BatchUpdateString.Randoms;

#if !MYSQL
            var jds = PlatformBenchmarks.BatchUpdateString.Jds;
#endif

            for (int i = 0; i < count; i++)
            {
                var randomNumber = _random.Next(1, 10001);

                updateCommand.CreateParameter(ids[i], DbType.Int32, worlds[i].Id);
                updateCommand.CreateParameter(randoms[i], DbType.Int32, randomNumber);

                worlds[i].RandomNumber = randomNumber;
            }

#if !MYSQL
            for (int i = 0; i < count; i++)
            {
                updateCommand.CreateParameter(jds[i], DbType.Int32, worlds[i].Id);
            }
#endif

            await updateCommand.ExecuteNonQueryAsync();

            updateCommand.Release();
            pooledConnection.Release();

            return(worlds);
        }
Beispiel #4
0
        public static async Task PopulateCache()
        {
            var pooledConnection = await PooledConnections.GetConnection(DataProvider.ConnectionString);

            pooledConnection.Open();

            var(pooledCommand, dbDataParameter) = CreateReadCommand(pooledConnection);

            using (pooledCommand)
            {
                var cacheKeys = _cacheKeys;
                var cache     = _cache;

                for (var i = 1; i < 10001; i++)
                {
                    dbDataParameter.Value = i;
                    cache.Set <CachedWorld>(cacheKeys[i], await ReadSingleRow(pooledCommand));
                }
            }

            pooledCommand.Release();
            pooledConnection.Release();
        }