Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(909)

Side by Side Diff: sql/connection.cc

Issue 24638002: [sql] Log tag with sqlite errors. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Put sql statement on same line as error. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sql/connection.h ('k') | sql/statement.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sql/connection.h" 5 #include "sql/connection.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 } 634 }
635 635
636 bool Connection::Execute(const char* sql) { 636 bool Connection::Execute(const char* sql) {
637 if (!db_) { 637 if (!db_) {
638 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; 638 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db";
639 return false; 639 return false;
640 } 640 }
641 641
642 int error = ExecuteAndReturnErrorCode(sql); 642 int error = ExecuteAndReturnErrorCode(sql);
643 if (error != SQLITE_OK) 643 if (error != SQLITE_OK)
644 error = OnSqliteError(error, NULL); 644 error = OnSqliteError(error, NULL, sql);
645 645
646 // This needs to be a FATAL log because the error case of arriving here is 646 // This needs to be a FATAL log because the error case of arriving here is
647 // that there's a malformed SQL statement. This can arise in development if 647 // that there's a malformed SQL statement. This can arise in development if
648 // a change alters the schema but not all queries adjust. This can happen 648 // a change alters the schema but not all queries adjust. This can happen
649 // in production if the schema is corrupted. 649 // in production if the schema is corrupted.
650 if (error == SQLITE_ERROR) 650 if (error == SQLITE_ERROR)
651 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage(); 651 DLOG(FATAL) << "SQL Error in " << sql << ", " << GetErrorMessage();
652 return error == SQLITE_OK; 652 return error == SQLITE_OK;
653 } 653 }
654 654
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 if (!db_) 695 if (!db_)
696 return new StatementRef(NULL, NULL, poisoned_); 696 return new StatementRef(NULL, NULL, poisoned_);
697 697
698 sqlite3_stmt* stmt = NULL; 698 sqlite3_stmt* stmt = NULL;
699 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL); 699 int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
700 if (rc != SQLITE_OK) { 700 if (rc != SQLITE_OK) {
701 // This is evidence of a syntax error in the incoming SQL. 701 // This is evidence of a syntax error in the incoming SQL.
702 DLOG(FATAL) << "SQL compile error " << GetErrorMessage(); 702 DLOG(FATAL) << "SQL compile error " << GetErrorMessage();
703 703
704 // It could also be database corruption. 704 // It could also be database corruption.
705 OnSqliteError(rc, NULL); 705 OnSqliteError(rc, NULL, sql);
706 return new StatementRef(NULL, NULL, false); 706 return new StatementRef(NULL, NULL, false);
707 } 707 }
708 return new StatementRef(this, stmt, true); 708 return new StatementRef(this, stmt, true);
709 } 709 }
710 710
711 scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement( 711 scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
712 const char* sql) const { 712 const char* sql) const {
713 // Return inactive statement. 713 // Return inactive statement.
714 if (!db_) 714 if (!db_)
715 return new StatementRef(NULL, NULL, poisoned_); 715 return new StatementRef(NULL, NULL, poisoned_);
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 int err = sqlite3_open(file_name.c_str(), &db_); 857 int err = sqlite3_open(file_name.c_str(), &db_);
858 if (err != SQLITE_OK) { 858 if (err != SQLITE_OK) {
859 // Extended error codes cannot be enabled until a handle is 859 // Extended error codes cannot be enabled until a handle is
860 // available, fetch manually. 860 // available, fetch manually.
861 err = sqlite3_extended_errcode(db_); 861 err = sqlite3_extended_errcode(db_);
862 862
863 // Histogram failures specific to initial open for debugging 863 // Histogram failures specific to initial open for debugging
864 // purposes. 864 // purposes.
865 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err); 865 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.OpenFailure", err);
866 866
867 OnSqliteError(err, NULL); 867 OnSqliteError(err, NULL, "-- sqlite3_open()");
868 bool was_poisoned = poisoned_; 868 bool was_poisoned = poisoned_;
869 Close(); 869 Close();
870 870
871 if (was_poisoned && retry_flag == RETRY_ON_POISON) 871 if (was_poisoned && retry_flag == RETRY_ON_POISON)
872 return OpenInternal(file_name, NO_RETRY); 872 return OpenInternal(file_name, NO_RETRY);
873 return false; 873 return false;
874 } 874 }
875 875
876 // TODO(shess): OS_WIN support? 876 // TODO(shess): OS_WIN support?
877 #if defined(OS_POSIX) 877 #if defined(OS_POSIX)
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 // issue, the object could be cached alongside histogram_prefix_. 1015 // issue, the object could be cached alongside histogram_prefix_.
1016 std::string full_histogram_name = name + "." + histogram_tag_; 1016 std::string full_histogram_name = name + "." + histogram_tag_;
1017 base::HistogramBase* histogram = 1017 base::HistogramBase* histogram =
1018 base::SparseHistogram::FactoryGet( 1018 base::SparseHistogram::FactoryGet(
1019 full_histogram_name, 1019 full_histogram_name,
1020 base::HistogramBase::kUmaTargetedHistogramFlag); 1020 base::HistogramBase::kUmaTargetedHistogramFlag);
1021 if (histogram) 1021 if (histogram)
1022 histogram->Add(sample); 1022 histogram->Add(sample);
1023 } 1023 }
1024 1024
1025 int Connection::OnSqliteError(int err, sql::Statement *stmt) { 1025 int Connection::OnSqliteError(int err, sql::Statement *stmt, const char* sql) {
1026 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err); 1026 UMA_HISTOGRAM_SPARSE_SLOWLY("Sqlite.Error", err);
1027 AddTaggedHistogram("Sqlite.Error", err); 1027 AddTaggedHistogram("Sqlite.Error", err);
1028 1028
1029 // Always log the error. 1029 // Always log the error.
1030 LOG(ERROR) << "sqlite error " << err 1030 if (!sql && stmt)
1031 sql = stmt->GetSQLStatement();
1032 if (!sql)
1033 sql = "-- unknown";
1034 LOG(ERROR) << histogram_tag_ << " sqlite error " << err
1031 << ", errno " << GetLastErrno() 1035 << ", errno " << GetLastErrno()
1032 << ": " << GetErrorMessage(); 1036 << ": " << GetErrorMessage()
1037 << ", sql: " << sql;
1033 1038
1034 if (!error_callback_.is_null()) { 1039 if (!error_callback_.is_null()) {
1035 // Fire from a copy of the callback in case of reentry into 1040 // Fire from a copy of the callback in case of reentry into
1036 // re/set_error_callback(). 1041 // re/set_error_callback().
1037 // TODO(shess): <http://crbug.com/254584> 1042 // TODO(shess): <http://crbug.com/254584>
1038 ErrorCallback(error_callback_).Run(err, stmt); 1043 ErrorCallback(error_callback_).Run(err, stmt);
1039 return err; 1044 return err;
1040 } 1045 }
1041 1046
1042 // The default handling is to assert on debug and to ignore on release. 1047 // The default handling is to assert on debug and to ignore on release.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 } 1079 }
1075 1080
1076 // Best effort to put things back as they were before. 1081 // Best effort to put things back as they were before.
1077 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF"; 1082 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
1078 ignore_result(Execute(kNoWritableSchema)); 1083 ignore_result(Execute(kNoWritableSchema));
1079 1084
1080 return ret; 1085 return ret;
1081 } 1086 }
1082 1087
1083 } // namespace sql 1088 } // namespace sql
OLDNEW
« no previous file with comments | « sql/connection.h ('k') | sql/statement.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698