[geary/mjog/db-sql-log-extended] Geary.Db.Statement: Log SQL statements with params where possible
- From: Michael Gratton <mjog src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [geary/mjog/db-sql-log-extended] Geary.Db.Statement: Log SQL statements with params where possible
- Date: Fri, 15 Jan 2021 23:09:37 +0000 (UTC)
commit 298e4ec2e9b474c58facd7daf9d397c8c0b76ed0
Author: Michael Gratton <mike vee net>
Date: Sat Jan 16 10:07:04 2021 +1100
Geary.Db.Statement: Log SQL statements with params where possible
Ensure when SQL logging is enabled, that SQL statements are logged
with parameter values filled in where possible.
src/engine/db/db-database-connection.vala | 3 ---
src/engine/db/db-statement.vala | 22 ++++++++++++++++------
src/engine/db/db.vala | 16 +++++++++++-----
3 files changed, 27 insertions(+), 14 deletions(-)
---
diff --git a/src/engine/db/db-database-connection.vala b/src/engine/db/db-database-connection.vala
index 29d52fd16..da7d98600 100644
--- a/src/engine/db/db-database-connection.vala
+++ b/src/engine/db/db-database-connection.vala
@@ -123,9 +123,6 @@ public class Geary.Db.DatabaseConnection : Context, Connection {
/** {@inheritDoc} */
public Statement prepare(string sql) throws DatabaseError {
- if (Db.Context.enable_sql_logging) {
- debug(sql);
- }
return new Statement(this, sql);
}
diff --git a/src/engine/db/db-statement.vala b/src/engine/db/db-statement.vala
index 072692ffb..d3d3b0e72 100644
--- a/src/engine/db/db-statement.vala
+++ b/src/engine/db/db-statement.vala
@@ -4,8 +4,6 @@
* (version 2.1 or later). See the COPYING file in this distribution.
*/
-private extern string? sqlite3_expanded_sql(Sqlite.Statement stmt);
-
public class Geary.Db.Statement : Context {
@@ -47,14 +45,15 @@ public class Geary.Db.Statement : Context {
this.sql = sql;
throw_on_error(
"Statement.ctor",
- connection.db.prepare_v2(sql, -1, out stmt, null),
- sql
+ connection.db.prepare_v2(sql, -1, out stmt, null)
);
}
/** Returns SQL for the statement with bound parameters expanded. */
public string? get_expanded_sql() {
- return this.stmt.expanded_sql();
+ // The statement may be null if throw_on_error() in the ctor
+ // does actually throw an error
+ return (this.stmt != null) ? this.stmt.expanded_sql() : null;
}
/**
@@ -122,8 +121,11 @@ public class Geary.Db.Statement : Context {
* row in the result set. If empty, Result.finished will be true.
*/
public Result exec(Cancellable? cancellable = null) throws Error {
- Result results = new Result(this, cancellable);
+ if (Db.Context.enable_sql_logging) {
+ debug(this.get_expanded_sql());
+ }
+ Result results = new Result(this, cancellable);
executed();
return results;
@@ -136,6 +138,10 @@ public class Geary.Db.Statement : Context {
* See Connection.last_insert_rowid.
*/
public int64 exec_insert(Cancellable? cancellable = null) throws Error {
+ if (Db.Context.enable_sql_logging) {
+ debug(this.get_expanded_sql());
+ }
+
new Result(this, cancellable);
int64 rowid = connection.last_insert_rowid;
@@ -153,6 +159,10 @@ public class Geary.Db.Statement : Context {
* See Connection.last_modified_rows.
*/
public int exec_get_modified(Cancellable? cancellable = null) throws Error {
+ if (Db.Context.enable_sql_logging) {
+ debug(this.get_expanded_sql());
+ }
+
new Result(this, cancellable);
int modified = connection.last_modified_rows;
diff --git a/src/engine/db/db.vala b/src/engine/db/db.vala
index f83fd668e..438a1cd90 100644
--- a/src/engine/db/db.vala
+++ b/src/engine/db/db.vala
@@ -114,13 +114,19 @@ private int throw_on_error(Context ctx, string? method, int result, string? raw
? "(%s %s) ".printf(method, ctx.get_database().path)
: "(%s) ".printf(ctx.get_database().path);
string errmsg = (ctx.get_connection() != null) ? " - %s".printf(ctx.get_connection().db.errmsg()) : "";
- string sql;
- if (ctx.get_statement() != null)
- sql = " (%s)".printf(ctx.get_statement().sql);
- else if (!String.is_empty(raw))
+ string? sql = null;
+ Statement statement = ctx.get_statement();
+ if (statement != null) {
+ sql = statement.get_expanded_sql();
+ if (sql == null) {
+ sql = statement.sql;
+ }
+ sql = " (%s)".printf(sql);
+ } else if (!String.is_empty(raw)) {
sql = " (%s)".printf(raw);
- else
+ } else {
sql = "";
+ }
string msg = "%s[err=%d]%s%s".printf(location, result, errmsg, sql);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]