Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include affected rows in logging even when query is done #1754

Merged
merged 4 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions sqlx-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::time::Instant;

pub(crate) struct QueryLogger<'q> {
sql: &'q str,
rows: usize,
rows_returned: u64,
rows_affected: u64,
start: Instant,
settings: LogSettings,
}
Expand All @@ -12,14 +13,19 @@ impl<'q> QueryLogger<'q> {
pub(crate) fn new(sql: &'q str, settings: LogSettings) -> Self {
Self {
sql,
rows: 0,
rows_returned: 0,
rows_affected: 0,
start: Instant::now(),
settings,
}
}

pub(crate) fn increment_rows(&mut self) {
self.rows += 1;
pub(crate) fn increment_rows_returned(&mut self) {
self.rows_returned += 1;
}

pub(crate) fn increase_rows_affected(&mut self, n: u64) {
self.rows_affected += n;
}

pub(crate) fn finish(&self) {
Expand Down Expand Up @@ -51,13 +57,11 @@ impl<'q> QueryLogger<'q> {
String::new()
};

let rows = self.rows;

log::logger().log(
&log::Record::builder()
.args(format_args!(
"{}; rows: {}, elapsed: {:.3?}{}",
summary, rows, elapsed, sql
"{}; rows affected: {}, rows returned: {}, elapsed: {:.3?}{}",
summary, self.rows_affected, self.rows_returned, elapsed, sql
))
.level(lvl)
.module_path_static(Some("sqlx::query"))
Expand Down
10 changes: 7 additions & 3 deletions sqlx-core/src/mssql/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<'c> Executor<'c> for &'c mut MssqlConnection {
let columns = Arc::clone(&self.stream.columns);
let column_names = Arc::clone(&self.stream.column_names);

logger.increment_rows();
logger.increment_rows_returned();

r#yield!(Either::Right(MssqlRow { row, column_names, columns }));
}
Expand All @@ -103,8 +103,10 @@ impl<'c> Executor<'c> for &'c mut MssqlConnection {
}

if done.status.contains(Status::DONE_COUNT) {
let rows_affected = done.affected_rows;
logger.increase_rows_affected(rows_affected);
r#yield!(Either::Left(MssqlQueryResult {
rows_affected: done.affected_rows,
rows_affected,
}));
}

Expand All @@ -115,8 +117,10 @@ impl<'c> Executor<'c> for &'c mut MssqlConnection {

Message::DoneInProc(done) => {
if done.status.contains(Status::DONE_COUNT) {
let rows_affected = done.affected_rows;
logger.increase_rows_affected(rows_affected);
r#yield!(Either::Left(MssqlQueryResult {
rows_affected: done.affected_rows,
rows_affected,
}));
}
}
Expand Down
8 changes: 4 additions & 4 deletions sqlx-core/src/mssql/protocol/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,14 @@ impl TypeInfo {
4 => "INT",
8 => "BIGINT",

_ => unreachable!("invalid size {} for int"),
n => unreachable!("invalid size {} for int", n),
},

DataType::FloatN => match self.size {
4 => "REAL",
8 => "FLOAT",

_ => unreachable!("invalid size {} for float"),
n => unreachable!("invalid size {} for float", n),
},

DataType::VarChar => "VARCHAR",
Expand Down Expand Up @@ -536,14 +536,14 @@ impl TypeInfo {
4 => "int",
8 => "bigint",

_ => unreachable!("invalid size {} for int"),
n => unreachable!("invalid size {} for int", n),
}),

DataType::FloatN => s.push_str(match self.size {
4 => "real",
8 => "float",

_ => unreachable!("invalid size {} for float"),
n => unreachable!("invalid size {} for float", n),
}),

DataType::VarChar
Expand Down
6 changes: 4 additions & 2 deletions sqlx-core/src/mysql/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ impl MySqlConnection {
// this indicates either a successful query with no rows at all or a failed query
let ok = packet.ok()?;

let rows_affected = ok.affected_rows;
logger.increase_rows_affected(rows_affected);
let done = MySqlQueryResult {
rows_affected: ok.affected_rows,
rows_affected,
last_insert_id: ok.last_insert_id,
};

Expand Down Expand Up @@ -199,7 +201,7 @@ impl MySqlConnection {
column_names: Arc::clone(&column_names),
});

logger.increment_rows();
logger.increment_rows_returned();

r#yield!(v);
}
Expand Down
6 changes: 4 additions & 2 deletions sqlx-core/src/postgres/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ impl PgConnection {
// a SQL command completed normally
let cc: CommandComplete = message.decode()?;

let rows_affected = cc.rows_affected();
logger.increase_rows_affected(rows_affected);
r#yield!(Either::Left(PgQueryResult {
rows_affected: cc.rows_affected(),
rows_affected,
}));
}

Expand All @@ -301,7 +303,7 @@ impl PgConnection {
}

MessageFormat::DataRow => {
logger.increment_rows();
logger.increment_rows_returned();

// one of the set of rows returned by a SELECT, FETCH, etc query
let data: DataRow = message.decode()?;
Expand Down
7 changes: 5 additions & 2 deletions sqlx-core/src/sqlite/connection/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Iterator for ExecuteIter<'_> {

match statement.handle.step() {
Ok(true) => {
self.logger.increment_rows();
self.logger.increment_rows_returned();

Some(Ok(Either::Right(SqliteRow::current(
&statement.handle,
Expand All @@ -96,8 +96,11 @@ impl Iterator for ExecuteIter<'_> {
Ok(false) => {
let last_insert_rowid = self.handle.last_insert_rowid();

let changes = statement.handle.changes();
self.logger.increase_rows_affected(changes);

let done = SqliteQueryResult {
changes: statement.handle.changes(),
changes,
last_insert_rowid,
};

Expand Down