コード例 #1
0
ファイル: AsyncTests.cs プロジェクト: Emill/Npgsql
 public async void Reader()
 {
     using (var cmd = new NpgsqlCommand("SELECT 1", Conn))
     using (var reader = await cmd.ExecuteReaderAsync())
     {
         await reader.ReadAsync();
         Assert.That(reader[0], Is.EqualTo(1));
     }
 }
コード例 #2
0
ファイル: AsyncTests.cs プロジェクト: Emill/Npgsql
 public async void Columnar()
 {
     using (var cmd = new NpgsqlCommand("SELECT NULL, 2, 'Some Text'", Conn))
     using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
     {
         await reader.ReadAsync();
         Assert.That(await reader.IsDBNullAsync(0), Is.True);
         Assert.That(await reader.GetFieldValueAsync<string>(2), Is.EqualTo("Some Text"));
     }
 }
コード例 #3
0
ファイル: AsyncTests.cs プロジェクト: roji/Npgsql
 public async void Columnar()
 {
     ExecuteNonQuery("INSERT INTO DATA (field_int4, field_text) VALUES (2, 'Some Text')");
     using (var cmd = new NpgsqlCommand("SELECT field_int2, field_int4, field_text FROM data", Conn))
     using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
     {
         await reader.ReadAsync();
         Assert.That(await reader.IsDBNullAsync(0), Is.True);
         Assert.That(await reader.GetFieldValueAsync<string>(2), Is.EqualTo("Some Text"));
     }
 }
コード例 #4
0
ファイル: Fetcher.cs プロジェクト: phillip-haydon/marten
        private async Task<EventPage> buildEventPage(long lastEncountered, NpgsqlCommand cmd)
        {
            IList<IEvent> events = null;
            IList<long> sequences = new List<long>();

            using (var reader = await cmd.ExecuteReaderAsync(_token).ConfigureAwait(false))
            {
                while (await reader.ReadAsync(_token).ConfigureAwait(false))
                {
                    var seq = await reader.GetFieldValueAsync<long>(0, _token).ConfigureAwait(false);
                    sequences.Add(seq);
                }

                if (sequences.Any())
                {
                    await reader.NextResultAsync(_token).ConfigureAwait(false);

                    events = await _selector.ReadAsync(reader, _map, _token).ConfigureAwait(false);
                }
                else
                {
                    events = new List<IEvent>();
                }
            }

            return new EventPage(lastEncountered, sequences, events) {Count = events.Count};
        }
コード例 #5
0
ファイル: SpeedTests.cs プロジェクト: Tradioyes/Npgsql
 public async void ExecuteReaderAsync(int rows)
 {
     for (var i = 0; i < rows; i++)
         ExecuteNonQuery("INSERT INTO DATA (field_int4) VALUES (10)");
     using (var metrics = TestMetrics.Start(TestRunTime, true))
     {
         while (!metrics.TimesUp)
         {
             using (var cmd = new NpgsqlCommand("SELECT field_int4 FROM data", Conn))
             using (var reader = await cmd.ExecuteReaderAsync())
             {
                 while (await reader.ReadAsync()) { }
             }
             metrics.IncrementIterations();
         }
     }
 }
コード例 #6
0
		public async Task<IEnumerable<Driver>> GetDriverByLastNameAsync(string theDriverName)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand =
					new NpgsqlCommand(
						"SELECT id, firstname, lastname, address, city, state, postalcode, country, licensenumber, licensestate, customerid from driver where id = :value1", Connection);
				var aParam = new NpgsqlParameter("value1", NpgsqlDbType.Text) { Value = theDriverName };
				aPreparedCommand.Parameters.Add(aParam);

				var aReader = await aPreparedCommand.ExecuteReaderAsync().ConfigureAwait(false);

				if (!aReader.HasRows)
					return null;

				var aReturn = new List<Driver>();
				while (await aReader.ReadAsync().ConfigureAwait(false))
				{
					aReturn.Add(ReadDriver(aReader));
				}
				return aReturn;
			}
			catch (NpgsqlException)
			{
				return null;
			}
			catch (InvalidOperationException)
			{
				return null;
			}
			catch (SqlException)
			{
				return null;
			}
			catch (ConfigurationErrorsException)
			{
				return null;
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}
コード例 #7
0
		public async Task<Customer> GetCustomerByNameAsync(string theCustomerName)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand =
					new NpgsqlCommand(
						"SELECT id, name, allowsadditionaldrivers, allowsadditions, hasmaxrentaldays, maxrentaldays from customer where name = :value1", Connection);
				var aParam = new NpgsqlParameter("value1", NpgsqlDbType.Text) { Value = theCustomerName };
				aPreparedCommand.Parameters.Add(aParam);

				var aReader = await aPreparedCommand.ExecuteReaderAsync().ConfigureAwait(false);

				if (!aReader.HasRows)
					return null;

				var aReturn = new Customer();
				while (await aReader.ReadAsync().ConfigureAwait(false))
				{
					aReturn = ReadCustomer(aReader);
				}
				return aReturn;
			}
			catch (NpgsqlException)
			{
				return null;
			}
			catch (InvalidOperationException)
			{
				return null;
			}
			catch (SqlException)
			{
				return null;
			}
			catch (ConfigurationErrorsException)
			{
				return null;
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}
コード例 #8
0
ファイル: Fetcher.cs プロジェクト: JasperFx/marten
        private async Task<EventPage> buildEventPage(long @from, NpgsqlCommand cmd)
        {
            IList<IEvent> events = null;
            IList<long> sequences = new List<long>();

            long nextKnown = 0;
            long lastKnown = 0;

            using (var reader = await cmd.ExecuteReaderAsync(_token).ConfigureAwait(false))
            {
                while (await reader.ReadAsync(_token).ConfigureAwait(false))
                {
                    var seq = await reader.GetFieldValueAsync<long>(0, _token).ConfigureAwait(false);
                    sequences.Add(seq);
                }

                if (sequences.Any())
                {
                    await reader.NextResultAsync(_token).ConfigureAwait(false);

                    events = await _selector.ReadAsync(reader, _map, null, _token).ConfigureAwait(false);
                }
                else
                {
                    events = new List<IEvent>();
                }

                nextKnown = await getLong(reader).ConfigureAwait(false);
                lastKnown = await getLong(reader).ConfigureAwait(false);
            }

            return new EventPage(@from, sequences, events)
            {
                Count = events.Count,
                NextKnownSequence = nextKnown,
                LastKnownSequence = lastKnown
            };
        }
コード例 #9
0
		public async Task<IEnumerable<Location>> GetLocationsForCustomerAsync(int theCustomerId)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand = new NpgsqlCommand("SELECT id, customerid, name, address, city, state, postalcode, country, latitude, longitude  from location where customerid=:value1", Connection);
				aPreparedCommand.Parameters.AddWithValue("value1", theCustomerId);

				var aReader = await aPreparedCommand.ExecuteReaderAsync();

				if (!aReader.HasRows)
					return Enumerable.Empty<Location>();

				var aReturn = new List<Location>();
				while (await aReader.ReadAsync().ConfigureAwait(false))
				{
					aReturn.Add(ReadLocation(aReader));
				}
				return aReturn;
			}
			catch (NpgsqlException)
			{
				return Enumerable.Empty<Location>();
			}
			catch (InvalidOperationException ex)
			{
				return Enumerable.Empty<Location>();
			}
			catch (SqlException)
			{
				return Enumerable.Empty<Location>();
			}
			catch (ConfigurationErrorsException)
			{
				return Enumerable.Empty<Location>();
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}
コード例 #10
0
		public async Task<Location> GetLocationAsync(int theLocationId)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand =
					new NpgsqlCommand(
						"SELECT id, customerid, name, address, city, state, postalcode, country, latitude, longitude from location where id = :value1", Connection);
				var aParam = new NpgsqlParameter("value1", NpgsqlDbType.Integer) { Value = theLocationId };
				aPreparedCommand.Parameters.Add(aParam);

				var aReader = await aPreparedCommand.ExecuteReaderAsync().ConfigureAwait(false);

				if (!aReader.HasRows)
					return null;

				var aReturn = new Location();
				while (await aReader.ReadAsync().ConfigureAwait(false))
				{
					aReturn = ReadLocation(aReader);
				}
				return aReturn;
			}
			catch (NpgsqlException)
			{
				return null;
			}
			catch (InvalidOperationException)
			{
				return null;
			}
			catch (SqlException)
			{
				return null;
			}
			catch (ConfigurationErrorsException)
			{
				return null;
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}
コード例 #11
0
ファイル: UpdateBatch.cs プロジェクト: danielmarbach/marten
        private static async Task executeCallbacksAsync(NpgsqlCommand cmd, CancellationToken tkn, BatchCommand batch, List<Exception> list)
        {
            using (var reader = await cmd.ExecuteReaderAsync(tkn).ConfigureAwait(false))
            {
                if (batch.Callbacks.Any())
                {
                    if (batch.Callbacks[0] != null)
                    {
                        await batch.Callbacks[0].PostprocessAsync(reader, list, tkn).ConfigureAwait(false);
                    }

                    for (int i = 1; i < batch.Callbacks.Count; i++)
                    {
                        await reader.NextResultAsync(tkn).ConfigureAwait(false);

                        if (batch.Callbacks[i] != null)
                        {
                            await batch.Callbacks[i].PostprocessAsync(reader, list, tkn).ConfigureAwait(false);
                        }
                    }
                }
            }
        }
コード例 #12
0
		public async Task<User> GetUserByEmailAsync(string theEmail)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand =
					new NpgsqlCommand(
						"SELECT id, firstname, lastname, email, customerid, isemployee from appuser where email = :value1", Connection);
				var aParam = new NpgsqlParameter("value1", NpgsqlDbType.Text) { Value = theEmail };
				aPreparedCommand.Parameters.Add(aParam);

				var aReader = await aPreparedCommand.ExecuteReaderAsync().ConfigureAwait(false);

				if (!aReader.HasRows)
					return null;

				var aReturn = new User();
				while (await aReader.ReadAsync().ConfigureAwait(false))
				{
					aReturn = ReadUser(aReader);
				}
				return aReturn;
			}
			catch (NpgsqlException)
			{
				return null;
			}
			catch (InvalidOperationException)
			{
				return null;
			}
			catch (SqlException)
			{
				return null;
			}
			catch (ConfigurationErrorsException)
			{
				return null;
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}
コード例 #13
0
		public async Task<IEnumerable<Automobile>> GetAutomobilesAsync(IEnumerable<int> theAutomobileIds)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand =
					new NpgsqlCommand(
						"SELECT id, vin, vehiclenumber, name, class, style, color, manufacturer, model, code, locationid from automobile where id = :value1", Connection);
				aPreparedCommand.Parameters.AddWithValue("value1", string.Join(",", theAutomobileIds));

				var aReader = await aPreparedCommand.ExecuteReaderAsync();

				if (!aReader.HasRows)
					return Enumerable.Empty<Automobile>().ToList();

				var aReturnList = new List<Automobile>();
				while (await aReader.ReadAsync())
				{
					aReturnList.Add(ReadAutomobile(aReader));
				}
				return aReturnList;
			}
			catch (NpgsqlException)
			{
				return Enumerable.Empty<Automobile>().ToList();
			}
			catch (InvalidOperationException)
			{
				return Enumerable.Empty<Automobile>().ToList();
			}
			catch (SqlException)
			{
				return Enumerable.Empty<Automobile>().ToList();
			}
			catch (ConfigurationErrorsException)
			{
				return Enumerable.Empty<Automobile>().ToList();
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}
コード例 #14
0
		public async Task<Automobile> GetAutomobileAsync(int theAutomobileId)
		{
			try
			{
				await Connection.OpenAsync().ConfigureAwait(false);

				var aPreparedCommand =
					new NpgsqlCommand(
						"SELECT id, vin, vehiclenumber, name, class, style, color, manufacturer, model, code, locationid from automobile where id = :value1", Connection);
				var aParam = new NpgsqlParameter("value1", NpgsqlDbType.Integer) { Value = theAutomobileId };
				aPreparedCommand.Parameters.Add(aParam);

				var aReader = await aPreparedCommand.ExecuteReaderAsync().ConfigureAwait(false);

				if (!aReader.HasRows)
					return null;

				var aReturn = new Automobile();
				while (await aReader.ReadAsync().ConfigureAwait(false))
				{
					aReturn = ReadAutomobile(aReader);
				}
				return aReturn;
			}
			catch (NpgsqlException)
			{
				return null;
			}
			catch (InvalidOperationException)
			{
				return null;
			}
			catch (SqlException)
			{
				return null;
			}
			catch (ConfigurationErrorsException)
			{
				return null;
			}
			finally
			{
				if (Connection.State == ConnectionState.Open)
					Connection.Close();
			}
		}