Skip to content

Commit de573ff

Browse files
committed
Move IsolationLevel into the transaction module
1 parent 50b2395 commit de573ff

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

src/lib.rs

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ extern crate net2;
5555

5656
use bufstream::BufStream;
5757
use md5::Md5;
58-
use std::ascii::AsciiExt;
5958
use std::cell::{Cell, RefCell};
6059
use std::collections::{VecDeque, HashMap};
6160
use std::error::Error as StdError;
@@ -70,7 +69,7 @@ use std::time::Duration;
7069
use std::path::PathBuf;
7170

7271
// FIXME remove in 0.12
73-
pub use transaction::Transaction;
72+
pub use transaction::{Transaction, IsolationLevel};
7473

7574
use error::{Error, ConnectError, SqlState, DbError};
7675
use io::{StreamWrapper, NegotiateSsl};
@@ -303,52 +302,6 @@ fn desynchronized() -> std_io::Error {
303302
error")
304303
}
305304

306-
/// An enumeration of transaction isolation levels.
307-
///
308-
/// See the [Postgres documentation](http://www.postgresql.org/docs/9.4/static/transaction-iso.html)
309-
/// for full details on the semantics of each level.
310-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
311-
pub enum IsolationLevel {
312-
/// The "read uncommitted" level.
313-
///
314-
/// In current versions of Postgres, this behaves identically to
315-
/// `ReadCommitted`.
316-
ReadUncommitted,
317-
/// The "read committed" level.
318-
///
319-
/// This is the default isolation level in Postgres.
320-
ReadCommitted,
321-
/// The "repeatable read" level.
322-
RepeatableRead,
323-
/// The "serializable" level.
324-
Serializable,
325-
}
326-
327-
impl IsolationLevel {
328-
fn to_sql(&self) -> &'static str {
329-
match *self {
330-
IsolationLevel::ReadUncommitted => "READ UNCOMMITTED",
331-
IsolationLevel::ReadCommitted => "READ COMMITTED",
332-
IsolationLevel::RepeatableRead => "REPEATABLE READ",
333-
IsolationLevel::Serializable => "SERIALIZABLE",
334-
}
335-
}
336-
337-
fn parse(raw: &str) -> Result<IsolationLevel> {
338-
if raw.eq_ignore_ascii_case("READ UNCOMMITTED") {
339-
Ok(IsolationLevel::ReadUncommitted)
340-
} else if raw.eq_ignore_ascii_case("READ COMMITTED") {
341-
Ok(IsolationLevel::ReadCommitted)
342-
} else if raw.eq_ignore_ascii_case("REPEATABLE READ") {
343-
Ok(IsolationLevel::RepeatableRead)
344-
} else if raw.eq_ignore_ascii_case("SERIALIZABLE") {
345-
Ok(IsolationLevel::Serializable)
346-
} else {
347-
Err(Error::Io(bad_response()))
348-
}
349-
}
350-
}
351-
352305
/// Specifies the SSL support requested for a new connection.
353306
#[derive(Debug)]
354307
pub enum SslMode<'a> {
@@ -1239,7 +1192,7 @@ impl Connection {
12391192
let mut conn = self.conn.borrow_mut();
12401193
check_desync!(conn);
12411194
let result = try!(conn.quick_query("SHOW TRANSACTION ISOLATION LEVEL"));
1242-
IsolationLevel::parse(result[0][0].as_ref().unwrap())
1195+
IsolationLevel::new(result[0][0].as_ref().unwrap())
12431196
}
12441197

12451198
/// # Deprecated
@@ -1502,3 +1455,7 @@ trait TransactionInternals<'conn> {
15021455
trait ConfigInternals {
15031456
fn build_command(&self, s: &mut String);
15041457
}
1458+
1459+
trait IsolationLevelNew {
1460+
fn new(level: &str) -> Result<IsolationLevel>;
1461+
}

src/transaction.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,63 @@
22
33
use std::cell::Cell;
44
use std::fmt;
5+
use std::ascii::AsciiExt;
56

6-
use {Result, Connection, TransactionInternals, ConfigInternals, IsolationLevel};
7-
use stmt::Statement;
7+
use {bad_response, Result, Connection, TransactionInternals, ConfigInternals,
8+
IsolationLevelNew};
9+
use error::Error;
810
use rows::Rows;
11+
use stmt::Statement;
912
use types::ToSql;
1013

14+
/// An enumeration of transaction isolation levels.
15+
///
16+
/// See the [Postgres documentation](http://www.postgresql.org/docs/9.4/static/transaction-iso.html)
17+
/// for full details on the semantics of each level.
18+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19+
pub enum IsolationLevel {
20+
/// The "read uncommitted" level.
21+
///
22+
/// In current versions of Postgres, this behaves identically to
23+
/// `ReadCommitted`.
24+
ReadUncommitted,
25+
/// The "read committed" level.
26+
///
27+
/// This is the default isolation level in Postgres.
28+
ReadCommitted,
29+
/// The "repeatable read" level.
30+
RepeatableRead,
31+
/// The "serializable" level.
32+
Serializable,
33+
}
34+
35+
impl IsolationLevelNew for IsolationLevel {
36+
fn new(raw: &str) -> Result<IsolationLevel> {
37+
if raw.eq_ignore_ascii_case("READ UNCOMMITTED") {
38+
Ok(IsolationLevel::ReadUncommitted)
39+
} else if raw.eq_ignore_ascii_case("READ COMMITTED") {
40+
Ok(IsolationLevel::ReadCommitted)
41+
} else if raw.eq_ignore_ascii_case("REPEATABLE READ") {
42+
Ok(IsolationLevel::RepeatableRead)
43+
} else if raw.eq_ignore_ascii_case("SERIALIZABLE") {
44+
Ok(IsolationLevel::Serializable)
45+
} else {
46+
Err(Error::Io(bad_response()))
47+
}
48+
}
49+
}
50+
51+
impl IsolationLevel {
52+
fn to_sql(&self) -> &'static str {
53+
match *self {
54+
IsolationLevel::ReadUncommitted => "READ UNCOMMITTED",
55+
IsolationLevel::ReadCommitted => "READ COMMITTED",
56+
IsolationLevel::RepeatableRead => "REPEATABLE READ",
57+
IsolationLevel::Serializable => "SERIALIZABLE",
58+
}
59+
}
60+
}
61+
1162
/// Configuration of a transaction.
1263
#[derive(Debug)]
1364
pub struct Config {

0 commit comments

Comments
 (0)