More ~str -> StrBuf

This commit is contained in:
Steven Fackler 2014-05-15 19:27:19 -07:00
parent 1909bd46b4
commit dcd47041d0
3 changed files with 24 additions and 19 deletions

View File

@ -405,7 +405,7 @@ struct InnerPostgresConnection {
notice_handler: Box<PostgresNoticeHandler:Send>, notice_handler: Box<PostgresNoticeHandler:Send>,
notifications: RingBuf<PostgresNotification>, notifications: RingBuf<PostgresNotification>,
cancel_data: PostgresCancelData, cancel_data: PostgresCancelData,
unknown_types: HashMap<Oid, ~str>, unknown_types: HashMap<Oid, StrBuf>,
desynchronized: bool, desynchronized: bool,
finished: bool, finished: bool,
canary: u32, canary: u32,
@ -638,7 +638,7 @@ impl InnerPostgresConnection {
Ok(()) Ok(())
} }
fn get_type_name(&mut self, oid: Oid) -> PostgresResult<~str> { fn get_type_name(&mut self, oid: Oid) -> PostgresResult<StrBuf> {
match self.unknown_types.find(&oid) { match self.unknown_types.find(&oid) {
Some(name) => return Ok(name.clone()), Some(name) => return Ok(name.clone()),
None => {} None => {}
@ -666,7 +666,7 @@ impl InnerPostgresConnection {
} }
fn quick_query(&mut self, query: &str) fn quick_query(&mut self, query: &str)
-> PostgresResult<Vec<Vec<Option<~str>>>> { -> PostgresResult<Vec<Vec<Option<StrBuf>>>> {
check_desync!(self); check_desync!(self);
try_pg!(self.write_messages([Query { query: query }])); try_pg!(self.write_messages([Query { query: query }]));
@ -676,7 +676,7 @@ impl InnerPostgresConnection {
ReadyForQuery { .. } => break, ReadyForQuery { .. } => break,
DataRow { row } => { DataRow { row } => {
result.push(row.move_iter().map(|opt| { result.push(row.move_iter().map(|opt| {
opt.map(|b| StrBuf::from_utf8(b).unwrap().into_owned()) opt.map(|b| StrBuf::from_utf8(b).unwrap())
}).collect()); }).collect());
} }
ErrorResponse { fields } => { ErrorResponse { fields } => {
@ -746,7 +746,7 @@ impl PostgresConnection {
/// let params = PostgresConnectParams { /// let params = PostgresConnectParams {
/// target: TargetUnix(some_crazy_path), /// target: TargetUnix(some_crazy_path),
/// port: None, /// port: None,
/// user: Some("postgres".to_owned()), /// user: Some("postgres".to_strbuf()),
/// password: None, /// password: None,
/// database: None, /// database: None,
/// options: Vec::new(), /// options: Vec::new(),
@ -882,7 +882,7 @@ impl PostgresConnection {
} }
fn quick_query(&self, query: &str) fn quick_query(&self, query: &str)
-> PostgresResult<Vec<Vec<Option<~str>>>> { -> PostgresResult<Vec<Vec<Option<StrBuf>>>> {
self.conn.borrow_mut().quick_query(query) self.conn.borrow_mut().quick_query(query)
} }
@ -1403,7 +1403,7 @@ impl<'stmt, I: RowIndex+Clone+fmt::Show, T: FromSql> Index<I, T>
/// # let mut result = stmt.query([]).unwrap(); /// # let mut result = stmt.query([]).unwrap();
/// # let row = result.next().unwrap(); /// # let row = result.next().unwrap();
/// let foo: i32 = row[1]; /// let foo: i32 = row[1];
/// let bar: ~str = row["bar"]; /// let bar: StrBuf = row["bar"];
/// ``` /// ```
fn index(&self, idx: &I) -> T { fn index(&self, idx: &I) -> T {
match self.get(idx.clone()) { match self.get(idx.clone()) {

View File

@ -112,13 +112,14 @@ fn test_unix_connection() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl)); let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let stmt = or_fail!(conn.prepare("SHOW unix_socket_directories")); let stmt = or_fail!(conn.prepare("SHOW unix_socket_directories"));
let result = or_fail!(stmt.query([])); let result = or_fail!(stmt.query([]));
let unix_socket_directories: ~str = result.map(|row| row[1]).next().unwrap(); let unix_socket_directories: StrBuf = result.map(|row| row[1]).next().unwrap();
if unix_socket_directories.is_empty() { if unix_socket_directories.is_empty() {
fail!("can't test connect_unix; unix_socket_directories is empty"); fail!("can't test connect_unix; unix_socket_directories is empty");
} }
let unix_socket_directory = unix_socket_directories.splitn(',', 1).next().unwrap(); let unix_socket_directory = unix_socket_directories.as_slice()
.splitn(',', 1).next().unwrap();
let url = format!("postgres://postgres@{}", url::encode_component(unix_socket_directory)); let url = format!("postgres://postgres@{}", url::encode_component(unix_socket_directory));
let conn = or_fail!(PostgresConnection::connect(url.as_slice(), &NoSsl)); let conn = or_fail!(PostgresConnection::connect(url.as_slice(), &NoSsl));
@ -469,7 +470,7 @@ fn test_bpchar_params() {
)", [])); )", []));
or_fail!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)", or_fail!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)",
[&Some("12345") as &ToSql, &Some("123") as &ToSql, [&Some("12345") as &ToSql, &Some("123") as &ToSql,
&None::<~str> as &ToSql])); &None::<&'static str> as &ToSql]));
let stmt = or_fail!(conn.prepare("SELECT b FROM foo ORDER BY id")); let stmt = or_fail!(conn.prepare("SELECT b FROM foo ORDER BY id"));
let res = or_fail!(stmt.query([])); let res = or_fail!(stmt.query([]));
@ -989,6 +990,6 @@ fn test_pg_database_datname() {
let mut result = or_fail!(stmt.query([])); let mut result = or_fail!(stmt.query([]));
let next = result.next().unwrap(); let next = result.next().unwrap();
or_fail!(next.get::<uint, ~str>(1)); or_fail!(next.get::<uint, StrBuf>(1));
or_fail!(next.get::<&str, ~str>("datname")); or_fail!(next.get::<&str, StrBuf>("datname"));
} }

View File

@ -86,7 +86,7 @@ macro_rules! make_postgres_type(
/// An unknown type /// An unknown type
PgUnknownType { PgUnknownType {
/// The name of the type /// The name of the type
pub name: ~str, pub name: StrBuf,
/// The OID of the type /// The OID of the type
pub oid: Oid pub oid: Oid
} }
@ -98,7 +98,7 @@ macro_rules! make_postgres_type(
match oid { match oid {
$($oid => $variant,)+ $($oid => $variant,)+
// We have to load an empty string now, it'll get filled in later // We have to load an empty string now, it'll get filled in later
oid => PgUnknownType { name: "".to_owned(), oid: oid } oid => PgUnknownType { name: "".to_strbuf(), oid: oid }
} }
} }
@ -122,7 +122,8 @@ macro_rules! make_postgres_type(
/// Returns the wire format needed for the value of `self`. /// Returns the wire format needed for the value of `self`.
pub fn result_format(&self) -> Format { pub fn result_format(&self) -> Format {
match *self { match *self {
PgUnknownType { name: ref name, .. } if "hstore" == *name => Binary, PgUnknownType { name: ref name, .. }
if "hstore" == name.as_slice() => Binary,
PgUnknownType { .. } => Text, PgUnknownType { .. } => Text,
_ => Binary _ => Binary
} }
@ -477,7 +478,8 @@ impl FromSql for Option<HashMap<~str, Option<~str>>> {
fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>) fn from_sql(ty: &PostgresType, raw: &Option<Vec<u8>>)
-> PostgresResult<Option<HashMap<~str, Option<~str>>>> { -> PostgresResult<Option<HashMap<~str, Option<~str>>>> {
match *ty { match *ty {
PgUnknownType { name: ref name, .. } if "hstore" == *name => {} PgUnknownType { name: ref name, .. }
if "hstore" == name.as_slice() => {}
_ => return Err(PgWrongType(ty.clone())) _ => return Err(PgWrongType(ty.clone()))
} }
@ -792,7 +794,8 @@ impl ToSql for HashMap<~str, Option<~str>> {
fn to_sql(&self, ty: &PostgresType) fn to_sql(&self, ty: &PostgresType)
-> PostgresResult<(Format, Option<Vec<u8>>)> { -> PostgresResult<(Format, Option<Vec<u8>>)> {
match *ty { match *ty {
PgUnknownType { name: ref name, .. } if "hstore" == *name => {} PgUnknownType { name: ref name, .. }
if "hstore" == name.as_slice() => {}
_ => return Err(PgWrongType(ty.clone())) _ => return Err(PgWrongType(ty.clone()))
} }
@ -821,7 +824,8 @@ impl ToSql for Option<HashMap<~str, Option<~str>>> {
fn to_sql(&self, ty: &PostgresType) fn to_sql(&self, ty: &PostgresType)
-> PostgresResult<(Format, Option<Vec<u8>>)> { -> PostgresResult<(Format, Option<Vec<u8>>)> {
match *ty { match *ty {
PgUnknownType { name: ref name, .. } if "hstore" == *name => {} PgUnknownType { name: ref name, .. }
if "hstore" == name.as_slice() => {}
_ => return Err(PgWrongType(ty.clone())) _ => return Err(PgWrongType(ty.clone()))
} }