Fix transaction batch_execute

This commit is contained in:
Steven Fackler 2014-07-29 19:48:56 -07:00
parent d094aa615b
commit c6333f09a4
5 changed files with 37 additions and 22 deletions

View File

@ -300,7 +300,7 @@ pub struct PostgresNotifications<'conn> {
impl<'conn> Iterator<PostgresNotification> for PostgresNotifications<'conn> { impl<'conn> Iterator<PostgresNotification> for PostgresNotifications<'conn> {
/// Returns the oldest pending notification or `None` if there are none. /// Returns the oldest pending notification or `None` if there are none.
/// ///
/// # Note /// ## Note
/// ///
/// `next` may return `Some` notification after returning `None` if a new /// `next` may return `Some` notification after returning `None` if a new
/// notification was received. /// notification was received.
@ -330,7 +330,7 @@ pub struct PostgresCancelData {
/// Only the host and port of the connetion info are used. See /// Only the host and port of the connetion info are used. See
/// `PostgresConnection::connect` for details of the `params` argument. /// `PostgresConnection::connect` for details of the `params` argument.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # use postgres::{PostgresConnection, NoSsl};
@ -694,7 +694,7 @@ impl PostgresConnection {
/// should be created manually and passed in. Note that Postgres does not /// should be created manually and passed in. Note that Postgres does not
/// support SSL over Unix sockets. /// support SSL over Unix sockets.
/// ///
/// # Examples /// ## Examples
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # use postgres::{PostgresConnection, NoSsl};
@ -759,7 +759,7 @@ impl PostgresConnection {
/// The statement is associated with the connection that created it and may /// The statement is associated with the connection that created it and may
/// not outlive that connection. /// not outlive that connection.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # use postgres::{PostgresConnection, NoSsl};
@ -784,11 +784,11 @@ impl PostgresConnection {
/// the connection for the duration of the transaction. The transaction /// the connection for the duration of the transaction. The transaction
/// is active until the `PostgresTransaction` object falls out of scope. /// 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 /// A transaction will roll back by default. Use the `set_commit` method to
/// set the transaction to commit. /// set the transaction to commit.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # use postgres::{PostgresConnection, NoSsl};
@ -837,14 +837,14 @@ impl PostgresConnection {
/// execution of batches of non-dynamic statements - for example, creation /// execution of batches of non-dynamic statements - for example, creation
/// of a schema for a fresh database. /// of a schema for a fresh database.
/// ///
/// # Warning /// ## Warning
/// ///
/// Prepared statements should be used for any SQL statement which contains /// Prepared statements should be used for any SQL statement which contains
/// user-specified data, as it provides functionality to safely embed that /// user-specified data, as it provides functionality to safely embed that
/// data in the statment. Do not form statements via string concatenation /// data in the statment. Do not form statements via string concatenation
/// and feed them into this method. /// and feed them into this method.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, PostgresResult}; /// # use postgres::{PostgresConnection, PostgresResult};
@ -982,10 +982,11 @@ impl<'conn> PostgresTransaction<'conn> {
/// Like `PostgresConnection::batch_execute`. /// Like `PostgresConnection::batch_execute`.
pub fn batch_execute(&self, query: &str) -> PostgresResult<()> { 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); return Err(PgWrongTransaction);
} }
self.conn.batch_execute(query) conn.quick_query(query).map(|_| ())
} }
/// Like `PostgresConnection::transaction`. /// 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. /// If the statement does not modify any rows (e.g. SELECT), 0 is returned.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # use postgres::{PostgresConnection, NoSsl};
@ -1231,7 +1232,7 @@ impl<'conn> PostgresStatement<'conn> {
/// Executes the prepared statement, returning an iterator over the /// Executes the prepared statement, returning an iterator over the
/// resulting rows. /// resulting rows.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # 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 /// A field can be accessed by the name or index of its column, though
/// access by index is more efficient. Rows are 0-indexed. /// 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 /// Fails if the index does not reference a column or the return type is
/// not compatible with the Postgres type. /// not compatible with the Postgres type.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{PostgresConnection, NoSsl}; /// # use postgres::{PostgresConnection, NoSsl};

View File

@ -22,7 +22,7 @@ impl InnerConnectionPool {
/// ///
/// It can be shared across tasks. /// It can be shared across tasks.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::NoSsl; /// # use postgres::NoSsl;

View File

@ -19,7 +19,7 @@ pub trait Array<T> {
/// Slices into this array, returning an immutable view of a subarray. /// 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. /// Fails if the array is one-dimensional or the index is out of bounds.
fn slice<'a>(&'a self, idx: int) -> ArraySlice<'a, T>; fn slice<'a>(&'a self, idx: int) -> ArraySlice<'a, T>;
@ -27,7 +27,7 @@ pub trait Array<T> {
/// Retrieves an immutable reference to a value in this 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. /// Fails if the array is multi-dimensional or the index is out of bounds.
fn get<'a>(&'a self, idx: int) -> &'a T; fn get<'a>(&'a self, idx: int) -> &'a T;
@ -37,7 +37,7 @@ pub trait Array<T> {
pub trait MutableArray<T> : Array<T> { pub trait MutableArray<T> : Array<T> {
/// Slices into this array, returning a mutable view of a subarray. /// 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. /// 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>; fn slice_mut<'a>(&'a mut self, idx: int) -> MutArraySlice<'a, T>;
@ -45,7 +45,7 @@ pub trait MutableArray<T> : Array<T> {
/// Retrieves a mutable reference to a value in this 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. /// 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; fn get_mut<'a>(&'a mut self, idx: int) -> &'a mut T;
@ -82,7 +82,7 @@ impl<T> ArrayBase<T> {
/// The data array should be provided in the higher-dimensional equivalent /// The data array should be provided in the higher-dimensional equivalent
/// of row-major order. /// of row-major order.
/// ///
/// # Failure /// ## Failure
/// ///
/// Fails if there are 0 dimensions or the number of elements provided does /// Fails if there are 0 dimensions or the number of elements provided does
/// not match the number of elements specified. /// not match the number of elements specified.
@ -129,7 +129,7 @@ impl<T> ArrayBase<T> {
/// For example, if `[3,4]` is pushed onto `[[1,2]]`, the result is /// For example, if `[3,4]` is pushed onto `[[1,2]]`, the result is
/// `[[1,2],[3,4]]`. /// `[[1,2],[3,4]]`.
/// ///
/// # Failure /// ## Failure
/// ///
/// Fails if the other array does not have dimensions identical to the /// Fails if the other array does not have dimensions identical to the
/// dimensions of a slice of this array. /// dimensions of a slice of this array.

View File

@ -9,7 +9,7 @@ use time::Timespec;
/// The `quote!` macro can make it easier to create ranges. It roughly mirrors /// The `quote!` macro can make it easier to create ranges. It roughly mirrors
/// traditional mathematic range syntax. /// traditional mathematic range syntax.
/// ///
/// # Example /// ## Example
/// ///
/// ```rust /// ```rust
/// #[feature(phase)]; /// #[feature(phase)];

View File

@ -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] #[test]
fn test_query() { fn test_query() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl)); let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));