Skip to content

Commit

Permalink
sql-text -> sql-query
Browse files Browse the repository at this point in the history
  • Loading branch information
palfrey committed Nov 17, 2019
1 parent b51bd36 commit b170c00
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Options
-------
* `-m/--mode`: `postgres` or `odbc`
* `-c/--connection-string`: Mode-appropriate connection string. So `postgresql://<username>:<pasword>@<host>:<port>` or `Driver=<path to driver>;<various ODBC options>` depending on your driver
* `-s/--sql-text`: SQL query to run once connected. It should return at least one row, or will be regarded as failing. Default: no query, just be regarded as succeeding the moment it connects.
* `-s/--sql-query`: SQL query to run once connected. It should return at least one row, or will be regarded as failing. Default: no query, just be regarded as succeeding the moment it connects.
* `-p/--pause`: Pause between attempts for non-permanent failures. Default is 3 seconds
* `-t/--timeout`: Time to wait before failing entirely. Default is wait forever.

Expand Down
16 changes: 8 additions & 8 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl DbMode {
pub struct Opts {
pub mode: DbMode,
pub connection_string: String,
pub sql_text: Option<String>,
pub sql_query: Option<String>,
pub timeout_seconds: Option<u64>,
pub quiet: bool,
pub pause_seconds: u64,
Expand All @@ -47,9 +47,9 @@ pub fn parse_args() -> Opts {
.help("Connection string"),
)
.arg(
Arg::with_name("sql-text")
Arg::with_name("sql-query")
.short("s")
.long("sql-text")
.long("sql-query")
.takes_value(true)
.help("SQL query that should return at least one row (default: no querying)"),
)
Expand Down Expand Up @@ -78,8 +78,8 @@ pub fn parse_args() -> Opts {
Opts {
mode: DbMode::from_str(matches.value_of("mode").unwrap()),
connection_string: matches.value_of("connection-string").unwrap().to_string(),
sql_text: matches
.value_of("sql-text")
sql_query: matches
.value_of("sql-query")
.and_then(|s| Some(s.to_string())),
timeout_seconds: matches
.value_of("timeout")
Expand All @@ -98,7 +98,7 @@ impl Opts {
Opts {
mode: DbMode::Odbc,
connection_string: "".to_string(),
sql_text: None,
sql_query: None,
timeout_seconds: None,
quiet: false,
pause_seconds: 3,
Expand All @@ -113,11 +113,11 @@ impl Opts {
self
}

pub fn sql_text<I>(mut self, st: I) -> Self
pub fn sql_query<I>(mut self, st: I) -> Self
where
I: Into<String>,
{
self.sql_text = Some(st.into());
self.sql_query = Some(st.into());
self
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/odbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ impl From<DiagnosticRecord> for DbError {
pub fn connect(opts: &Opts) -> std::result::Result<Vec<HashMap<String, String>>, DbError> {
let env = create_environment_v3().map_err(|e| e.unwrap())?;
let conn = env.connect_with_connection_string(&opts.connection_string)?;
if let Some(ref sql_text) = opts.sql_text {
execute_statement(&conn, sql_text)
if let Some(ref sql_query) = opts.sql_query {
execute_statement(&conn, sql_query)
} else {
return Ok(Vec::new());
}
}

fn execute_statement<'env>(
conn: &Connection<'env, odbc_safe::AutocommitOn>,
sql_text: &String,
sql_query: &String,
) -> Result<Vec<HashMap<String, String>>, DbError> {
let stmt = Statement::with_parent(conn)?;
let mut results: Vec<HashMap<String, String>> = Vec::new();
match stmt.exec_direct(&sql_text)? {
match stmt.exec_direct(&sql_query)? {
Data(mut stmt) => {
let col_count = stmt.num_result_cols()? as u16;
let mut cols: HashMap<u16, odbc::ColumnDescriptor> = HashMap::new();
Expand Down Expand Up @@ -130,7 +130,7 @@ mod test {
"Driver={};Database=test.db;",
std::env::var("SQLITE_DRIVER").unwrap()
))
.sql_text("SELECT 1 from sqlite_master"),
.sql_query("SELECT 1 from sqlite_master"),
)
.unwrap();
}
Expand Down Expand Up @@ -159,7 +159,7 @@ mod test {
connect(
&Opts::new()
.connection_string(postgres_connect())
.sql_text("SHOW IS_SUPERUSER"),
.sql_query("SHOW IS_SUPERUSER"),
)
.unwrap();
}
Expand All @@ -170,7 +170,7 @@ mod test {
let err = connect(
&Opts::new()
.connection_string(postgres_connect())
.sql_text("foobar"),
.sql_query("foobar"),
)
.unwrap_err();
assert_eq!(err.kind, DbErrorLifetime::Permanent, "{:?}", err);
Expand Down
12 changes: 6 additions & 6 deletions src/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ impl From<Error> for DbError {

pub fn connect(opts: &Opts) -> std::result::Result<Vec<HashMap<String, String>>, DbError> {
let conn = Connection::connect(opts.connection_string.as_str(), TlsMode::None)?;
if let Some(ref sql_text) = opts.sql_text {
execute_statement(&conn, sql_text)
if let Some(ref sql_query) = opts.sql_query {
execute_statement(&conn, sql_query)
} else {
return Ok(Vec::new());
}
}

fn execute_statement<'env>(
conn: &postgres::Connection,
sql_text: &String,
sql_query: &String,
) -> Result<Vec<HashMap<String, String>>, DbError> {
let mut results: Vec<HashMap<String, String>> = Vec::new();
let rows = conn.query(&sql_text, &[])?;
let rows = conn.query(&sql_query, &[])?;
let cols = rows.columns();
for row in rows.iter() {
let mut result: HashMap<String, String> = HashMap::new();
Expand Down Expand Up @@ -83,7 +83,7 @@ mod test {
connect(
&Opts::new()
.connection_string(postgres_connect())
.sql_text("SHOW IS_SUPERUSER"),
.sql_query("SHOW IS_SUPERUSER"),
)
.unwrap();
}
Expand All @@ -94,7 +94,7 @@ mod test {
let err = connect(
&Opts::new()
.connection_string(postgres_connect())
.sql_text("foobar"),
.sql_query("foobar"),
)
.unwrap_err();
assert_eq!(err.kind, DbErrorLifetime::Permanent, "{:?}", err);
Expand Down
4 changes: 2 additions & 2 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn command_line_timeout_with_odbc() -> Result<(), Box<dyn std::error::Error>> {
"--connection-string={}",
wait_for_db::odbc::postgres_connect()
))
.arg("--sql-text=select 1 from foo");
.arg("--sql-query=select 1 from foo");
cmd.assert().failure().stdout(
predicate::str::contains("Temporary error (exiting as out of time)").and(
predicate::str::contains("ERROR: relation \"foo\" does not exist"),
Expand All @@ -34,7 +34,7 @@ fn command_line_timeout_with_postgres() -> Result<(), Box<dyn std::error::Error>
"--connection-string={}",
wait_for_db::pg::postgres_connect()
))
.arg("--sql-text=select 1 from foo");
.arg("--sql-query=select 1 from foo");
cmd.assert().failure().stdout(
predicate::str::contains("Temporary error (exiting as out of time)").and(
predicate::str::contains("message: \"relation \\\"foo\\\" does not exist\""),
Expand Down

0 comments on commit b170c00

Please sign in to comment.