From c6333f09a4a02733f8f14f346fc2668a9ac8532b Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 29 Jul 2014 19:48:56 -0700 Subject: [PATCH] Fix transaction batch_execute --- src/lib/lib.rs | 29 +++++++++++++++-------------- src/lib/pool.rs | 2 +- src/lib/types/array.rs | 12 ++++++------ src/lib/types/range.rs | 2 +- src/test/test.rs | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/lib/lib.rs b/src/lib/lib.rs index 1fc9b33d..38d92493 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -300,7 +300,7 @@ pub struct PostgresNotifications<'conn> { impl<'conn> Iterator for PostgresNotifications<'conn> { /// Returns the oldest pending notification or `None` if there are none. /// - /// # Note + /// ## Note /// /// `next` may return `Some` notification after returning `None` if a new /// notification was received. @@ -330,7 +330,7 @@ pub struct PostgresCancelData { /// Only the host and port of the connetion info are used. See /// `PostgresConnection::connect` for details of the `params` argument. /// -/// # Example +/// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; @@ -694,7 +694,7 @@ impl PostgresConnection { /// should be created manually and passed in. Note that Postgres does not /// support SSL over Unix sockets. /// - /// # Examples + /// ## Examples /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; @@ -759,7 +759,7 @@ impl PostgresConnection { /// The statement is associated with the connection that created it and may /// not outlive that connection. /// - /// # Example + /// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; @@ -784,11 +784,11 @@ impl PostgresConnection { /// the connection for the duration of the transaction. The transaction /// is active until the `PostgresTransaction` object falls out of scope. /// - /// # Note + /// ## Note /// A transaction will roll back by default. Use the `set_commit` method to /// set the transaction to commit. /// - /// # Example + /// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; @@ -837,14 +837,14 @@ impl PostgresConnection { /// execution of batches of non-dynamic statements - for example, creation /// of a schema for a fresh database. /// - /// # Warning + /// ## Warning /// /// Prepared statements should be used for any SQL statement which contains /// user-specified data, as it provides functionality to safely embed that /// data in the statment. Do not form statements via string concatenation /// and feed them into this method. /// - /// # Example + /// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, PostgresResult}; @@ -982,10 +982,11 @@ impl<'conn> PostgresTransaction<'conn> { /// Like `PostgresConnection::batch_execute`. pub fn batch_execute(&self, query: &str) -> PostgresResult<()> { - if self.conn.conn.borrow().trans_depth != self.depth { + let mut conn = self.conn.conn.borrow_mut(); + if conn.trans_depth != self.depth { return Err(PgWrongTransaction); } - self.conn.batch_execute(query) + conn.quick_query(query).map(|_| ()) } /// Like `PostgresConnection::transaction`. @@ -1184,7 +1185,7 @@ impl<'conn> PostgresStatement<'conn> { /// /// If the statement does not modify any rows (e.g. SELECT), 0 is returned. /// - /// # Example + /// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; @@ -1231,7 +1232,7 @@ impl<'conn> PostgresStatement<'conn> { /// Executes the prepared statement, returning an iterator over the /// resulting rows. /// - /// # Example + /// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; @@ -1422,12 +1423,12 @@ impl<'stmt> PostgresRow<'stmt> { /// A field can be accessed by the name or index of its column, though /// access by index is more efficient. Rows are 0-indexed. /// - /// # Failure + /// ## Failure /// /// Fails if the index does not reference a column or the return type is /// not compatible with the Postgres type. /// - /// # Example + /// ## Example /// /// ```rust,no_run /// # use postgres::{PostgresConnection, NoSsl}; diff --git a/src/lib/pool.rs b/src/lib/pool.rs index 0ccc05d9..810440fb 100644 --- a/src/lib/pool.rs +++ b/src/lib/pool.rs @@ -22,7 +22,7 @@ impl InnerConnectionPool { /// /// It can be shared across tasks. /// -/// # Example +/// ## Example /// /// ```rust,no_run /// # use postgres::NoSsl; diff --git a/src/lib/types/array.rs b/src/lib/types/array.rs index 7e9b512d..4bfb5764 100644 --- a/src/lib/types/array.rs +++ b/src/lib/types/array.rs @@ -19,7 +19,7 @@ pub trait Array { /// Slices into this array, returning an immutable view of a subarray. /// - /// # Failure + /// ## Failure /// /// Fails if the array is one-dimensional or the index is out of bounds. fn slice<'a>(&'a self, idx: int) -> ArraySlice<'a, T>; @@ -27,7 +27,7 @@ pub trait Array { /// Retrieves an immutable reference to a value in this array. /// /// - /// # Failure + /// ## Failure /// /// Fails if the array is multi-dimensional or the index is out of bounds. fn get<'a>(&'a self, idx: int) -> &'a T; @@ -37,7 +37,7 @@ pub trait Array { pub trait MutableArray : Array { /// Slices into this array, returning a mutable view of a subarray. /// - /// # Failure + /// ## Failure /// /// Fails if the array is one-dimensional or the index is out of bounds. fn slice_mut<'a>(&'a mut self, idx: int) -> MutArraySlice<'a, T>; @@ -45,7 +45,7 @@ pub trait MutableArray : Array { /// Retrieves a mutable reference to a value in this array. /// /// - /// # Failure + /// ## Failure /// /// Fails if the array is multi-dimensional or the index is out of bounds. fn get_mut<'a>(&'a mut self, idx: int) -> &'a mut T; @@ -82,7 +82,7 @@ impl ArrayBase { /// The data array should be provided in the higher-dimensional equivalent /// of row-major order. /// - /// # Failure + /// ## Failure /// /// Fails if there are 0 dimensions or the number of elements provided does /// not match the number of elements specified. @@ -129,7 +129,7 @@ impl ArrayBase { /// For example, if `[3,4]` is pushed onto `[[1,2]]`, the result is /// `[[1,2],[3,4]]`. /// - /// # Failure + /// ## Failure /// /// Fails if the other array does not have dimensions identical to the /// dimensions of a slice of this array. diff --git a/src/lib/types/range.rs b/src/lib/types/range.rs index 24f0a754..f71fad44 100644 --- a/src/lib/types/range.rs +++ b/src/lib/types/range.rs @@ -9,7 +9,7 @@ use time::Timespec; /// The `quote!` macro can make it easier to create ranges. It roughly mirrors /// traditional mathematic range syntax. /// -/// # Example +/// ## Example /// /// ```rust /// #[feature(phase)]; diff --git a/src/test/test.rs b/src/test/test.rs index 49499ed2..a5c543e2 100644 --- a/src/test/test.rs +++ b/src/test/test.rs @@ -355,6 +355,20 @@ fn test_batch_execute_error() { } } +#[test] +fn test_transaction_batch_execute() { + let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl)); + let trans = or_fail!(conn.transaction()); + let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY); + INSERT INTO foo (id) VALUES (10);"; + or_fail!(trans.batch_execute(query)); + + let stmt = or_fail!(trans.prepare("SELECT * from foo ORDER BY id")); + let result = or_fail!(stmt.query([])); + + assert_eq!(vec![10i64], result.map(|row| row.get(0u)).collect()); +} + #[test] fn test_query() { let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));