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.
This commit is contained in:
parent
17da8cea2d
commit
298e4ec2e9
3 changed files with 27 additions and 14 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue