Change "update" to "execute"

Closes #24
This commit is contained in:
Steven Fackler 2013-12-28 20:24:52 -07:00
parent 40f6a4ebc0
commit 219d10fa96
3 changed files with 63 additions and 63 deletions

46
lib.rs
View File

@ -24,7 +24,7 @@ fn main() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", let conn = PostgresConnection::connect("postgres://postgres@localhost",
&NoSsl); &NoSsl);
conn.update("CREATE TABLE person ( conn.execute("CREATE TABLE person (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL, name VARCHAR NOT NULL,
time_created TIMESTAMP NOT NULL, time_created TIMESTAMP NOT NULL,
@ -36,7 +36,7 @@ fn main() {
time_created: time::get_time(), time_created: time::get_time(),
data: None data: None
}; };
conn.update("INSERT INTO person (name, time_created, data) conn.execute("INSERT INTO person (name, time_created, data)
VALUES ($1, $2, $3)", VALUES ($1, $2, $3)",
[&me.name as &ToSql, &me.time_created as &ToSql, [&me.name as &ToSql, &me.time_created as &ToSql,
&me.data as &ToSql]); &me.data as &ToSql]);
@ -697,26 +697,26 @@ impl PostgresConnection {
} }
} }
/// A convenience function for update queries that are only run once. /// A convenience function for queries that are only run once.
/// ///
/// If an error is returned, it could have come from either the preparation /// If an error is returned, it could have come from either the preparation
/// or execution of the statement. /// or execution of the statement.
/// ///
/// On success, returns the number of rows modified or 0 if not applicable. /// On success, returns the number of rows modified or 0 if not applicable.
pub fn try_update(&self, query: &str, params: &[&ToSql]) pub fn try_execute(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresDbError> { -> Result<uint, PostgresDbError> {
self.try_prepare(query).and_then(|stmt| stmt.try_update(params)) self.try_prepare(query).and_then(|stmt| stmt.try_execute(params))
} }
/// A convenience wrapper around `try_update`. /// A convenience wrapper around `try_execute`.
/// ///
/// # Failure /// # Failure
/// ///
/// Fails if there was an error preparing or executing the statement. /// Fails if there was an error preparing or executing the statement.
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint { pub fn execute(&self, query: &str, params: &[&ToSql]) -> uint {
match self.try_update(query, params) { match self.try_execute(query, params) {
Ok(res) => res, Ok(res) => res,
Err(err) => fail!("Error running update:\n{}", Err(err) => fail!("Error running query:\n{}",
err.pretty_error(query)) err.pretty_error(query))
} }
} }
@ -803,15 +803,15 @@ impl<'conn> PostgresTransaction<'conn> {
} }
} }
/// Like `PostgresConnection::try_update`. /// Like `PostgresConnection::try_execute`.
pub fn try_update(&self, query: &str, params: &[&ToSql]) pub fn try_execute(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresDbError> { -> Result<uint, PostgresDbError> {
self.conn.try_update(query, params) self.conn.try_execute(query, params)
} }
/// Like `PostgresConnection::update`. /// Like `PostgresConnection::execute`.
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint { pub fn execute(&self, query: &str, params: &[&ToSql]) -> uint {
self.conn.update(query, params) self.conn.execute(query, params)
} }
/// Like `PostgresConnection::transaction`. /// Like `PostgresConnection::transaction`.
@ -862,17 +862,17 @@ pub trait PostgresStatement {
/// ///
/// Fails if the number or types of the provided parameters do not match /// Fails if the number or types of the provided parameters do not match
/// the parameters of the statement. /// the parameters of the statement.
fn try_update(&self, params: &[&ToSql]) -> Result<uint, PostgresDbError>; fn try_execute(&self, params: &[&ToSql]) -> Result<uint, PostgresDbError>;
/// A convenience function wrapping `try_update`. /// A convenience function wrapping `try_execute`.
/// ///
/// # Failure /// # Failure
/// ///
/// Fails if there was an error executing the statement. /// Fails if there was an error executing the statement.
fn update(&self, params: &[&ToSql]) -> uint { fn execute(&self, params: &[&ToSql]) -> uint {
match self.try_update(params) { match self.try_execute(params) {
Ok(count) => count, Ok(count) => count,
Err(err) => fail!("Error running update\n{}", err.to_str()) Err(err) => fail!("Error running query\n{}", err.to_str())
} }
} }
@ -1002,7 +1002,7 @@ impl<'conn> PostgresStatement for NormalPostgresStatement<'conn> {
self.result_desc.as_slice() self.result_desc.as_slice()
} }
fn try_update(&self, params: &[&ToSql]) fn try_execute(&self, params: &[&ToSql])
-> Result<uint, PostgresDbError> { -> Result<uint, PostgresDbError> {
match self.execute("", 0, params) { match self.execute("", 0, params) {
Some(err) => return Err(err), Some(err) => return Err(err),
@ -1080,8 +1080,8 @@ impl<'conn> PostgresStatement for TransactionalPostgresStatement<'conn> {
self.stmt.result_descriptions() self.stmt.result_descriptions()
} }
fn try_update(&self, params: &[&ToSql]) -> Result<uint, PostgresDbError> { fn try_execute(&self, params: &[&ToSql]) -> Result<uint, PostgresDbError> {
self.stmt.try_update(params) self.stmt.try_execute(params)
} }
fn try_query<'a>(&'a self, params: &[&ToSql]) fn try_query<'a>(&'a self, params: &[&ToSql])

12
pool.rs
View File

@ -130,15 +130,15 @@ impl PooledPostgresConnection {
self.conn.get_ref().prepare(query) self.conn.get_ref().prepare(query)
} }
/// Like `PostgresConnection::try_update`. /// Like `PostgresConnection::try_execute`.
pub fn try_update(&self, query: &str, params: &[&ToSql]) pub fn try_execute(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresDbError> { -> Result<uint, PostgresDbError> {
self.conn.get_ref().try_update(query, params) self.conn.get_ref().try_execute(query, params)
} }
/// Like `PostgresConnection::update`. /// Like `PostgresConnection::execute`.
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint { pub fn execute(&self, query: &str, params: &[&ToSql]) -> uint {
self.conn.get_ref().update(query, params) self.conn.get_ref().execute(query, params)
} }
/// Like `PostgresConnection::transaction`. /// Like `PostgresConnection::transaction`.

62
test.rs
View File

@ -97,10 +97,10 @@ fn test_unknown_database() {
#[test] #[test]
fn test_transaction_commit() { fn test_transaction_commit() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.update("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []); conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
let trans = conn.transaction(); let trans = conn.transaction();
trans.update("INSERT INTO foo (id) VALUES ($1)", [&1i32 as &ToSql]); trans.execute("INSERT INTO foo (id) VALUES ($1)", [&1i32 as &ToSql]);
drop(trans); drop(trans);
let stmt = conn.prepare("SELECT * FROM foo"); let stmt = conn.prepare("SELECT * FROM foo");
@ -112,12 +112,12 @@ fn test_transaction_commit() {
#[test] #[test]
fn test_transaction_rollback() { fn test_transaction_rollback() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.update("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []); conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
conn.update("INSERT INTO foo (id) VALUES ($1)", [&1i32 as &ToSql]); conn.execute("INSERT INTO foo (id) VALUES ($1)", [&1i32 as &ToSql]);
let trans = conn.transaction(); let trans = conn.transaction();
trans.update("INSERT INTO foo (id) VALUES ($1)", [&2i32 as &ToSql]); trans.execute("INSERT INTO foo (id) VALUES ($1)", [&2i32 as &ToSql]);
trans.set_rollback(); trans.set_rollback();
drop(trans); drop(trans);
@ -130,33 +130,33 @@ fn test_transaction_rollback() {
#[test] #[test]
fn test_nested_transactions() { fn test_nested_transactions() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.update("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []); conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
conn.update("INSERT INTO foo (id) VALUES (1)", []); conn.execute("INSERT INTO foo (id) VALUES (1)", []);
{ {
let trans1 = conn.transaction(); let trans1 = conn.transaction();
trans1.update("INSERT INTO foo (id) VALUES (2)", []); trans1.execute("INSERT INTO foo (id) VALUES (2)", []);
{ {
let trans2 = trans1.transaction(); let trans2 = trans1.transaction();
trans2.update("INSERT INTO foo (id) VALUES (3)", []); trans2.execute("INSERT INTO foo (id) VALUES (3)", []);
trans2.set_rollback(); trans2.set_rollback();
} }
{ {
let trans2 = trans1.transaction(); let trans2 = trans1.transaction();
trans2.update("INSERT INTO foo (id) VALUES (4)", []); trans2.execute("INSERT INTO foo (id) VALUES (4)", []);
{ {
let trans3 = trans2.transaction(); let trans3 = trans2.transaction();
trans3.update("INSERT INTO foo (id) VALUES (5)", []); trans3.execute("INSERT INTO foo (id) VALUES (5)", []);
trans3.set_rollback(); trans3.set_rollback();
} }
{ {
let trans3 = trans2.transaction(); let trans3 = trans2.transaction();
trans3.update("INSERT INTO foo (id) VALUES (6)", []); trans3.execute("INSERT INTO foo (id) VALUES (6)", []);
} }
} }
@ -177,8 +177,8 @@ fn test_nested_transactions() {
#[test] #[test]
fn test_query() { fn test_query() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.update("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", []); conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", []);
conn.update("INSERT INTO foo (id) VALUES ($1), ($2)", conn.execute("INSERT INTO foo (id) VALUES ($1), ($2)",
[&1i64 as &ToSql, &2i64 as &ToSql]); [&1i64 as &ToSql, &2i64 as &ToSql]);
let stmt = conn.prepare("SELECT * from foo ORDER BY id"); let stmt = conn.prepare("SELECT * from foo ORDER BY id");
let result = stmt.query([]); let result = stmt.query([]);
@ -192,11 +192,11 @@ fn test_lazy_query() {
{ {
let trans = conn.transaction(); let trans = conn.transaction();
trans.update("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []); trans.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
let stmt = trans.prepare("INSERT INTO foo (id) VALUES ($1)"); let stmt = trans.prepare("INSERT INTO foo (id) VALUES ($1)");
let values = ~[0i32, 1, 2, 3, 4, 5]; let values = ~[0i32, 1, 2, 3, 4, 5];
for value in values.iter() { for value in values.iter() {
stmt.update([value as &ToSql]); stmt.execute([value as &ToSql]);
} }
let stmt = trans.prepare("SELECT id FROM foo ORDER BY id"); let stmt = trans.prepare("SELECT id FROM foo ORDER BY id");
@ -298,11 +298,11 @@ fn test_text_params() {
#[test] #[test]
fn test_bpchar_params() { fn test_bpchar_params() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.update("CREATE TEMPORARY TABLE foo ( conn.execute("CREATE TEMPORARY TABLE foo (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
b CHAR(5) b CHAR(5)
)", []); )", []);
conn.update("INSERT INTO foo (b) VALUES ($1), ($2), ($3)", 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::<~str> as &ToSql]);
let stmt = conn.prepare("SELECT b FROM foo ORDER BY id"); let stmt = conn.prepare("SELECT b FROM foo ORDER BY id");
@ -587,21 +587,21 @@ fn test_f64_nan_param() {
#[should_fail] #[should_fail]
fn test_wrong_param_type() { fn test_wrong_param_type() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.try_update("SELECT $1::VARCHAR", [&1i32 as &ToSql]); conn.try_execute("SELECT $1::VARCHAR", [&1i32 as &ToSql]);
} }
#[test] #[test]
#[should_fail] #[should_fail]
fn test_too_few_params() { fn test_too_few_params() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.try_update("SELECT $1::INT, $2::INT", [&1i32 as &ToSql]); conn.try_execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql]);
} }
#[test] #[test]
#[should_fail] #[should_fail]
fn test_too_many_params() { fn test_too_many_params() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
conn.try_update("SELECT $1::INT, $2::INT", [&1i32 as &ToSql, conn.try_execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql,
&2i32 as &ToSql, &2i32 as &ToSql,
&3i32 as &ToSql]); &3i32 as &ToSql]);
} }
@ -638,12 +638,12 @@ fn test_custom_notice_handler() {
let conn = PostgresConnection::connect("postgres://postgres@localhost?client_min_messages=NOTICE", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost?client_min_messages=NOTICE", &NoSsl);
conn.set_notice_handler(~Handler as ~PostgresNoticeHandler); conn.set_notice_handler(~Handler as ~PostgresNoticeHandler);
conn.update("CREATE FUNCTION pg_temp.note() RETURNS INT AS $$ conn.execute("CREATE FUNCTION pg_temp.note() RETURNS INT AS $$
BEGIN BEGIN
RAISE NOTICE 'note'; RAISE NOTICE 'note';
RETURN 1; RETURN 1;
END; $$ LANGUAGE plpgsql", []); END; $$ LANGUAGE plpgsql", []);
conn.update("SELECT pg_temp.note()", []); conn.execute("SELECT pg_temp.note()", []);
assert_eq!(unsafe { count }, 1); assert_eq!(unsafe { count }, 1);
} }
@ -669,10 +669,10 @@ fn test_notification_iterator_some() {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl); let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
let mut it = conn.notifications(); let mut it = conn.notifications();
conn.update("LISTEN test_notification_iterator_one_channel", []); conn.execute("LISTEN test_notification_iterator_one_channel", []);
conn.update("LISTEN test_notification_iterator_one_channel2", []); conn.execute("LISTEN test_notification_iterator_one_channel2", []);
conn.update("NOTIFY test_notification_iterator_one_channel, 'hello'", []); conn.execute("NOTIFY test_notification_iterator_one_channel, 'hello'", []);
conn.update("NOTIFY test_notification_iterator_one_channel2, 'world'", []); conn.execute("NOTIFY test_notification_iterator_one_channel2, 'world'", []);
check_notification(PostgresNotification { check_notification(PostgresNotification {
pid: 0, pid: 0,
@ -686,7 +686,7 @@ fn test_notification_iterator_some() {
}, it.next()); }, it.next());
assert!(it.next().is_none()); assert!(it.next().is_none());
conn.update("NOTIFY test_notification_iterator_one_channel, '!'", []); conn.execute("NOTIFY test_notification_iterator_one_channel, '!'", []);
check_notification(PostgresNotification { check_notification(PostgresNotification {
pid: 0, pid: 0,
channel: ~"test_notification_iterator_one_channel", channel: ~"test_notification_iterator_one_channel",
@ -707,7 +707,7 @@ fn test_cancel_query() {
cancel_data).is_ok()); cancel_data).is_ok());
} }
match conn.try_update("SELECT pg_sleep(10)", []) { match conn.try_execute("SELECT pg_sleep(10)", []) {
Err(PostgresDbError { code: QueryCanceled, .. }) => {} Err(PostgresDbError { code: QueryCanceled, .. }) => {}
res => fail!("Unexpected result {:?}", res) res => fail!("Unexpected result {:?}", res)
} }
@ -718,7 +718,7 @@ fn test_require_ssl_conn() {
let ctx = SslContext::new(Sslv3); let ctx = SslContext::new(Sslv3);
let conn = PostgresConnection::connect("postgres://postgres@localhost", let conn = PostgresConnection::connect("postgres://postgres@localhost",
&RequireSsl(ctx)); &RequireSsl(ctx));
conn.update("SELECT 1::VARCHAR", []); conn.execute("SELECT 1::VARCHAR", []);
} }
#[test] #[test]
@ -726,7 +726,7 @@ fn test_prefer_ssl_conn() {
let ctx = SslContext::new(Sslv3); let ctx = SslContext::new(Sslv3);
let conn = PostgresConnection::connect("postgres://postgres@localhost", let conn = PostgresConnection::connect("postgres://postgres@localhost",
&PreferSsl(ctx)); &PreferSsl(ctx));
conn.update("SELECT 1::VARCHAR", []); conn.execute("SELECT 1::VARCHAR", []);
} }
#[test] #[test]