Fix for upstream changes

RIP Index :(
This commit is contained in:
Steven Fackler 2014-07-10 12:35:57 -07:00
parent b637ce640e
commit 28744034bb
5 changed files with 92 additions and 95 deletions

View File

@ -29,7 +29,7 @@ macro_rules! make_errors(
pub fn from_code(s: &str) -> PostgresSqlState {
match STATE_MAP.find_equiv(&s) {
Some(state) => state.clone(),
None => UnknownSqlState(s.to_str())
None => UnknownSqlState(s.to_string())
}
}
}

View File

@ -29,7 +29,7 @@
//! )", []).unwrap();
//! let me = Person {
//! id: 0,
//! name: "Steven".to_str(),
//! name: "Steven".to_string(),
//! time_created: time::get_time(),
//! data: None
//! };
@ -41,10 +41,10 @@
//! .unwrap();
//! for row in stmt.query([]).unwrap() {
//! let person = Person {
//! id: row[0u],
//! name: row[1u],
//! time_created: row[2u],
//! data: row[3u]
//! id: row.get(0u),
//! name: row.get(1u),
//! time_created: row.get(2u),
//! data: row.get(3u)
//! };
//! println!("Found person {}", person.name);
//! }
@ -243,7 +243,7 @@ impl IntoConnectParams for Url {
let database = if !path.is_empty() {
// path contains the leading /
let (_, path) = path.as_slice().slice_shift_char();
Some(path.to_str())
Some(path.to_string())
} else {
None
};
@ -412,14 +412,14 @@ impl InnerPostgresConnection {
canary: CANARY,
};
options.push(("client_encoding".to_str(), "UTF8".to_str()));
options.push(("client_encoding".to_string(), "UTF8".to_string()));
// Postgres uses the value of TimeZone as the time zone for TIMESTAMP
// WITH TIME ZONE values. Timespec converts to GMT internally.
options.push(("TimeZone".to_str(), "GMT".to_str()));
options.push(("TimeZone".to_string(), "GMT".to_string()));
// We have to clone here since we need the user again for auth
options.push(("user".to_str(), user.clone()));
options.push(("user".to_string(), user.clone()));
match database {
Some(database) => options.push(("database".to_str(), database)),
Some(database) => options.push(("database".to_string(), database)),
None => {}
}
@ -450,15 +450,15 @@ impl InnerPostgresConnection {
-> IoResult<()> {
assert!(!self.desynchronized);
for message in messages.iter() {
try_desync!(self.stream.write_message(message));
try_desync!(self, self.stream.write_message(message));
}
Ok(try_desync!(self.stream.flush()))
Ok(try_desync!(self, self.stream.flush()))
}
fn read_message(&mut self) -> IoResult<BackendMessage> {
assert!(!self.desynchronized);
loop {
match try_desync!(self.stream.read_message()) {
match try_desync!(self, self.stream.read_message()) {
NoticeResponse { fields } =>
self.notice_handler.handle(PostgresDbError::new(fields)),
NotificationResponse { pid, channel, payload } =>
@ -556,13 +556,13 @@ impl InnerPostgresConnection {
try!(self.wait_for_ready());
return Err(PgDbError(PostgresDbError::new(fields)));
}
_ => bad_response!(),
_ => bad_response!(self),
}
let mut param_types: Vec<PostgresType> = match try_pg!(self.read_message()) {
ParameterDescription { types } =>
types.iter().map(|ty| PostgresType::from_oid(*ty)).collect(),
_ => bad_response!(),
_ => bad_response!(self),
};
let mut result_desc: Vec<ResultDescription> = match try_pg!(self.read_message()) {
@ -575,7 +575,7 @@ impl InnerPostgresConnection {
}
}).collect(),
NoData => vec![],
_ => bad_response!()
_ => bad_response!(self)
};
try!(self.wait_for_ready());
@ -629,7 +629,7 @@ impl InnerPostgresConnection {
fn wait_for_ready(&mut self) -> PostgresResult<()> {
match try_pg!(self.read_message()) {
ReadyForQuery { .. } => Ok(()),
_ => bad_response!()
_ => bad_response!(self)
}
}
@ -714,7 +714,7 @@ impl PostgresConnection {
/// let params = PostgresConnectParams {
/// target: TargetUnix(some_crazy_path),
/// port: None,
/// user: Some("postgres".to_str()),
/// user: Some("postgres".to_string()),
/// password: None,
/// database: None,
/// options: vec![],
@ -1234,7 +1234,7 @@ impl<'conn> PostgresStatement<'conn> {
/// Err(err) => fail!("Error running query: {}", err)
/// };
/// for row in rows {
/// let foo: i32 = row["foo"];
/// let foo: i32 = row.get("foo");
/// println!("foo: {}", foo);
/// }
/// ```
@ -1395,7 +1395,7 @@ impl<'stmt> PostgresRow<'stmt> {
///
/// Returns an `Error` value if the index does not reference a column or
/// the return type is not compatible with the Postgres type.
pub fn get<I: RowIndex, T: FromSql>(&self, idx: I) -> PostgresResult<T> {
pub fn get_opt<I: RowIndex, T: FromSql>(&self, idx: I) -> PostgresResult<T> {
let idx = match idx.idx(self.stmt) {
Some(idx) => idx,
None => return Err(PgInvalidColumn)
@ -1403,18 +1403,8 @@ impl<'stmt> PostgresRow<'stmt> {
FromSql::from_sql(&self.stmt.result_desc.get(idx).ty,
self.data.get(idx))
}
}
impl<'stmt> Collection for PostgresRow<'stmt> {
#[inline]
fn len(&self) -> uint {
self.data.len()
}
}
impl<'stmt, I: RowIndex+Clone+fmt::Show, T: FromSql> Index<I, T>
for PostgresRow<'stmt> {
/// Retreives the contents of a field of the row.
/// Retrieves the contents of a field of the row.
///
/// A field can be accessed by the name or index of its column, though
/// access by index is more efficient. Rows are 0-indexed.
@ -1432,17 +1422,24 @@ impl<'stmt, I: RowIndex+Clone+fmt::Show, T: FromSql> Index<I, T>
/// # let stmt = conn.prepare("").unwrap();
/// # let mut result = stmt.query([]).unwrap();
/// # let row = result.next().unwrap();
/// let foo: i32 = row[0u];
/// let bar: String = row["bar"];
/// let foo: i32 = row.get(0u);
/// let bar: String = row.get("bar");
/// ```
fn index(&self, idx: &I) -> T {
match self.get(idx.clone()) {
pub fn get<I: RowIndex+fmt::Show+Clone, T: FromSql>(&self, idx: I) -> T {
match self.get_opt(idx.clone()) {
Ok(ok) => ok,
Err(err) => fail!("error retrieving column {}: {}", idx, err)
}
}
}
impl<'stmt> Collection for PostgresRow<'stmt> {
#[inline]
fn len(&self) -> uint {
self.data.len()
}
}
/// A trait implemented by types that can index into columns of a row.
pub trait RowIndex {
/// Returns the index of the appropriate column, or `None` if no such

View File

@ -17,11 +17,11 @@ macro_rules! try_pg(
)
macro_rules! try_desync(
($e:expr) => (
($s:expr, $e:expr) => (
match $e {
Ok(ok) => ok,
Err(err) => {
self.desynchronized = true;
$s.desynchronized = true;
return Err(err);
}
}
@ -40,9 +40,9 @@ macro_rules! check_desync(
)
macro_rules! bad_response(
() => ({
($s:expr) => ({
debug!("Unexpected response");
self.desynchronized = true;
$s.desynchronized = true;
return Err(PgBadResponse);
})
)

View File

@ -98,7 +98,7 @@ macro_rules! make_postgres_type(
match oid {
$($oid => $variant,)+
// We have to load an empty string now, it'll get filled in later
oid => PgUnknownType { name: "".to_str(), oid: oid }
oid => PgUnknownType { name: "".to_string(), oid: oid }
}
}

View File

@ -96,7 +96,7 @@ fn test_unix_connection() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let stmt = or_fail!(conn.prepare("SHOW unix_socket_directories"));
let result = or_fail!(stmt.query([]));
let unix_socket_directories: String = result.map(|row| row[0u]).next().unwrap();
let unix_socket_directories: String = result.map(|row| row.get(0u)).next().unwrap();
if unix_socket_directories.is_empty() {
fail!("can't test connect_unix; unix_socket_directories is empty");
@ -122,7 +122,7 @@ fn test_transaction_commit() {
let stmt = or_fail!(conn.prepare("SELECT * FROM foo"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -137,7 +137,7 @@ fn test_transaction_commit_finish() {
let stmt = or_fail!(conn.prepare("SELECT * FROM foo"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -155,7 +155,7 @@ fn test_transaction_rollback() {
let stmt = or_fail!(conn.prepare("SELECT * FROM foo"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -173,7 +173,7 @@ fn test_transaction_rollback_finish() {
let stmt = or_fail!(conn.prepare("SELECT * FROM foo"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -212,7 +212,7 @@ fn test_nested_transactions() {
let stmt = or_fail!(trans1.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32, 2, 4, 6], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32, 2, 4, 6], result.map(|row| row.get(0u)).collect());
trans1.set_rollback();
}
@ -220,7 +220,7 @@ fn test_nested_transactions() {
let stmt = or_fail!(conn.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -266,7 +266,7 @@ fn test_nested_transactions_finish() {
let stmt = or_fail!(trans1.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32, 2, 4, 6], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32, 2, 4, 6], result.map(|row| row.get(0u)).collect());
}
trans1.set_rollback();
@ -276,7 +276,7 @@ fn test_nested_transactions_finish() {
let stmt = or_fail!(conn.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i32], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i32], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -330,7 +330,7 @@ fn test_batch_execute() {
let stmt = or_fail!(conn.prepare("SELECT * from foo ORDER BY id"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![10i64], result.map(|row| row[0u]).collect());
assert_eq!(vec![10i64], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -358,7 +358,7 @@ fn test_query() {
let stmt = or_fail!(conn.prepare("SELECT * from foo ORDER BY id"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![1i64, 2], result.map(|row| row[0u]).collect());
assert_eq!(vec![1i64, 2], result.map(|row| row.get(0u)).collect());
}
#[test]
@ -383,7 +383,7 @@ fn test_lazy_query() {
}
let stmt = or_fail!(trans.prepare("SELECT id FROM foo ORDER BY id"));
let result = or_fail!(trans.lazy_query(&stmt, [], 2));
assert_eq!(values, result.map(|row| row.unwrap()[0u]).collect());
assert_eq!(values, result.map(|row| row.unwrap().get(0u)).collect());
trans.set_rollback();
}
@ -414,8 +414,8 @@ fn test_result_descriptions() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let stmt = or_fail!(conn.prepare("SELECT 1::INT as a, 'hi'::VARCHAR as b"));
assert!(stmt.result_descriptions() ==
[ResultDescription { name: "a".to_str(), ty: PgInt4},
ResultDescription { name: "b".to_str(), ty: PgVarchar}]);
[ResultDescription { name: "a".to_string(), ty: PgInt4},
ResultDescription { name: "b".to_string(), ty: PgVarchar}]);
}
#[test]
@ -435,11 +435,11 @@ fn test_type<T: PartialEq+FromSql+ToSql, S: Str>(sql_type: &str, checks: &[(T, S
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
for &(ref val, ref repr) in checks.iter() {
let stmt = or_fail!(conn.prepare(format!("SELECT {:s}::{}", *repr, sql_type).as_slice()));
let result = or_fail!(stmt.query([])).next().unwrap()[0u];
let result = or_fail!(stmt.query([])).next().unwrap().get(0u);
assert!(val == &result);
let stmt = or_fail!(conn.prepare(format!("SELECT $1::{}", sql_type).as_slice()));
let result = or_fail!(stmt.query([val])).next().unwrap()[0u];
let result = or_fail!(stmt.query([val])).next().unwrap().get(0u);
assert!(val == &result);
}
}
@ -457,8 +457,8 @@ fn test_i8_params() {
#[test]
fn test_name_params() {
test_type("NAME", [(Some("hello world".to_str()), "'hello world'"),
(Some("イロハニホヘト チリヌルヲ".to_str()), "'イロハニホヘト チリヌルヲ'"),
test_type("NAME", [(Some("hello world".to_string()), "'hello world'"),
(Some("イロハニホヘト チリヌルヲ".to_string()), "'イロハニホヘト チリヌルヲ'"),
(None, "NULL")]);
}
@ -498,15 +498,15 @@ fn test_f64_params() {
#[test]
fn test_varchar_params() {
test_type("VARCHAR", [(Some("hello world".to_str()), "'hello world'"),
(Some("イロハニホヘト チリヌルヲ".to_str()), "'イロハニホヘト チリヌルヲ'"),
test_type("VARCHAR", [(Some("hello world".to_string()), "'hello world'"),
(Some("イロハニホヘト チリヌルヲ".to_string()), "'イロハニホヘト チリヌルヲ'"),
(None, "NULL")]);
}
#[test]
fn test_text_params() {
test_type("TEXT", [(Some("hello world".to_str()), "'hello world'"),
(Some("イロハニホヘト チリヌルヲ".to_str()), "'イロハニホヘト チリヌルヲ'"),
test_type("TEXT", [(Some("hello world".to_string()), "'hello world'"),
(Some("イロハニホヘト チリヌルヲ".to_string()), "'イロハニホヘト チリヌルヲ'"),
(None, "NULL")]);
}
@ -523,8 +523,8 @@ fn test_bpchar_params() {
let stmt = or_fail!(conn.prepare("SELECT b FROM foo ORDER BY id"));
let res = or_fail!(stmt.query([]));
assert_eq!(vec!(Some("12345".to_str()), Some("123 ".to_str()), None),
res.map(|row| row[0u]).collect());
assert_eq!(vec!(Some("12345".to_string()), Some("123 ".to_string()), None),
res.map(|row| row.get(0u)).collect());
}
#[test]
@ -568,7 +568,7 @@ fn test_tm_params() {
macro_rules! test_range(
($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({
let tests = [(Some(range!('(', ')')), "'(,)'".to_str()),
let tests = [(Some(range!('(', ')')), "'(,)'".to_string()),
(Some(range!('[' $low, ')')), format!("'[{},)'", $low_str)),
(Some(range!('(' $low, ')')), format!("'({},)'", $low_str)),
(Some(range!('(', $high ']')), format!("'(,{}]'", $high_str)),
@ -581,8 +581,8 @@ macro_rules! test_range(
format!("'({},{}]'", $low_str, $high_str)),
(Some(range!('(' $low, $high ')')),
format!("'({},{})'", $low_str, $high_str)),
(Some(range!(empty)), "'empty'".to_str()),
(None, "NULL".to_str())];
(Some(range!(empty)), "'empty'".to_string()),
(None, "NULL".to_string())];
test_type($name, tests);
})
)
@ -620,7 +620,7 @@ macro_rules! test_array_params(
($name:expr, $v1:expr, $s1:expr, $v2:expr, $s2:expr, $v3:expr, $s3:expr) => ({
let tests = [(Some(ArrayBase::from_vec(vec!(Some($v1), Some($v2), None), 1)),
format!("'{{{},{},NULL}}'", $s1, $s2).into_string()),
(None, "NULL".to_str())];
(None, "NULL".to_string())];
test_type(format!("{}[]", $name).as_slice(), tests);
let mut a = ArrayBase::from_vec(vec!(Some($v1), Some($v2)), 0);
a.wrap(-1);
@ -650,8 +650,8 @@ fn test_chararray_params() {
#[test]
fn test_namearray_params() {
test_array_params!("NAME", "hello".to_str(), "hello", "world".to_str(),
"world", "!".to_str(), "!");
test_array_params!("NAME", "hello".to_string(), "hello", "world".to_string(),
"world", "!".to_string(), "!");
}
#[test]
@ -666,20 +666,20 @@ fn test_int4array_params() {
#[test]
fn test_textarray_params() {
test_array_params!("TEXT", "hello".to_str(), "hello", "world".to_str(),
"world", "!".to_str(), "!");
test_array_params!("TEXT", "hello".to_string(), "hello", "world".to_string(),
"world", "!".to_string(), "!");
}
#[test]
fn test_charnarray_params() {
test_array_params!("CHAR(5)", "hello".to_str(), "hello",
"world".to_str(), "world", "! ".to_str(), "!");
test_array_params!("CHAR(5)", "hello".to_string(), "hello",
"world".to_string(), "world", "! ".to_string(), "!");
}
#[test]
fn test_varchararray_params() {
test_array_params!("VARCHAR", "hello".to_str(), "hello",
"world".to_str(), "world", "!".to_str(), "!");
test_array_params!("VARCHAR", "hello".to_string(), "hello",
"world".to_string(), "world", "!".to_string(), "!");
}
#[test]
@ -763,10 +763,10 @@ fn test_hstore_params() {
})
)
test_type("hstore",
[(Some(make_map!("a".to_str() => Some("1".to_str()))), "'a=>1'"),
(Some(make_map!("hello".to_str() => Some("world!".to_str()),
"hola".to_str() => Some("mundo!".to_str()),
"what".to_str() => None)),
[(Some(make_map!("a".to_string() => Some("1".to_string()))), "'a=>1'"),
(Some(make_map!("hello".to_string() => Some("world!".to_string()),
"hola".to_string() => Some("mundo!".to_string()),
"what".to_string() => None)),
"'hello=>world!,hola=>mundo!,what=>NULL'"),
(None, "NULL")]);
}
@ -775,13 +775,13 @@ fn test_nan_param<T: Float+ToSql+FromSql>(sql_type: &str) {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let stmt = or_fail!(conn.prepare(format!("SELECT 'NaN'::{}", sql_type).as_slice()));
let mut result = or_fail!(stmt.query([]));
let val: T = result.next().unwrap()[0u];
let val: T = result.next().unwrap().get(0u);
assert!(val.is_nan());
let nan: T = Float::nan();
let stmt = or_fail!(conn.prepare(format!("SELECT $1::{}", sql_type).as_slice()));
let mut result = or_fail!(stmt.query([&nan]));
let val: T = result.next().unwrap()[0u];
let val: T = result.next().unwrap().get(0u);
assert!(val.is_nan())
}
@ -830,7 +830,7 @@ fn test_index_named() {
let stmt = or_fail!(conn.prepare("SELECT 10::INT as val"));
let result = or_fail!(stmt.query([]));
assert_eq!(vec![10i32], result.map(|row| row["val"]).collect());
assert_eq!(vec![10i32], result.map(|row| row.get("val")).collect());
}
#[test]
@ -840,7 +840,7 @@ fn test_index_named_fail() {
let stmt = or_fail!(conn.prepare("SELECT 10::INT as id"));
let mut result = or_fail!(stmt.query([]));
let _: i32 = result.next().unwrap()["asdf"];
let _: i32 = result.next().unwrap().get("asdf");
}
#[test]
@ -849,7 +849,7 @@ fn test_get_named_err() {
let stmt = or_fail!(conn.prepare("SELECT 10::INT as id"));
let mut result = or_fail!(stmt.query([]));
match result.next().unwrap().get::<&str, i32>("asdf") {
match result.next().unwrap().get_opt::<&str, i32>("asdf") {
Err(PgInvalidColumn) => {}
res => fail!("unexpected result {}", res),
};
@ -861,7 +861,7 @@ fn test_get_was_null() {
let stmt = or_fail!(conn.prepare("SELECT NULL::INT as id"));
let mut result = or_fail!(stmt.query([]));
match result.next().unwrap().get::<uint, i32>(0) {
match result.next().unwrap().get_opt::<uint, i32>(0) {
Err(PgWasNull) => {}
res => fail!("unexpected result {}", res),
};
@ -919,21 +919,21 @@ fn test_notification_iterator_some() {
check_notification(PostgresNotification {
pid: 0,
channel: "test_notification_iterator_one_channel".to_str(),
payload: "hello".to_str()
channel: "test_notification_iterator_one_channel".to_string(),
payload: "hello".to_string()
}, it.next());
check_notification(PostgresNotification {
pid: 0,
channel: "test_notification_iterator_one_channel2".to_str(),
payload: "world".to_str()
channel: "test_notification_iterator_one_channel2".to_string(),
payload: "world".to_string()
}, it.next());
assert!(it.next().is_none());
or_fail!(conn.execute("NOTIFY test_notification_iterator_one_channel, '!'", []));
check_notification(PostgresNotification {
pid: 0,
channel: "test_notification_iterator_one_channel".to_str(),
payload: "!".to_str()
channel: "test_notification_iterator_one_channel".to_string(),
payload: "!".to_string()
}, it.next());
assert!(it.next().is_none());
}
@ -1041,6 +1041,6 @@ fn test_pg_database_datname() {
let mut result = or_fail!(stmt.query([]));
let next = result.next().unwrap();
or_fail!(next.get::<uint, String>(0));
or_fail!(next.get::<&str, String>("datname"));
or_fail!(next.get_opt::<uint, String>(0));
or_fail!(next.get_opt::<&str, String>("datname"));
}