Skip to content

Commit

Permalink
sql: Make Database::DoesSchemaItemExist() case-sensitive.
Browse files Browse the repository at this point in the history
The method and its derivatives are supposed to be used in tests, so it's
good to be exact.

Change-Id: Ifc4d6c278a32a125e79c2f1bc6cb3defd01494ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1558158
Reviewed-by: Chris Mumford <cmumford@google.com>
Commit-Queue: Victor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#649633}
  • Loading branch information
pwnall authored and Commit Bot committed Apr 10, 2019
1 parent 4caa364 commit f85512e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 5 additions & 5 deletions sql/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1286,14 +1286,14 @@ bool Database::DoesViewExist(const char* view_name) const {
}

bool Database::DoesSchemaItemExist(const char* name, const char* type) const {
const char* kSql =
"SELECT name FROM sqlite_master WHERE type=? AND name=? COLLATE NOCASE";
static const char kSql[] =
"SELECT 1 FROM sqlite_master WHERE type=? AND name=?";
Statement statement(GetUntrackedStatement(kSql));

// This can happen if the database is corrupt and the error is a test
// expectation.
if (!statement.is_valid())
if (!statement.is_valid()) {
// The database is corrupt.
return false;
}

statement.BindString(0, type);
statement.BindString(1, name);
Expand Down
18 changes: 16 additions & 2 deletions sql/database_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ TEST_F(SQLDatabaseTest, DoesTableExist) {
ASSERT_TRUE(db().Execute("CREATE INDEX foo_index ON foo (a)"));
EXPECT_TRUE(db().DoesTableExist("foo"));
EXPECT_FALSE(db().DoesTableExist("foo_index"));

// DoesTableExist() is case-sensitive.
EXPECT_FALSE(db().DoesTableExist("Foo"));
EXPECT_FALSE(db().DoesTableExist("FOO"));
}

TEST_F(SQLDatabaseTest, DoesIndexExist) {
Expand All @@ -220,6 +224,11 @@ TEST_F(SQLDatabaseTest, DoesIndexExist) {
ASSERT_TRUE(db().Execute("CREATE INDEX foo_index ON foo (a)"));
EXPECT_TRUE(db().DoesIndexExist("foo_index"));
EXPECT_FALSE(db().DoesIndexExist("foo"));

// DoesIndexExist() is case-sensitive.
EXPECT_FALSE(db().DoesIndexExist("Foo_index"));
EXPECT_FALSE(db().DoesIndexExist("Foo_Index"));
EXPECT_FALSE(db().DoesIndexExist("FOO_INDEX"));
}

TEST_F(SQLDatabaseTest, DoesViewExist) {
Expand All @@ -228,6 +237,10 @@ TEST_F(SQLDatabaseTest, DoesViewExist) {
EXPECT_FALSE(db().DoesIndexExist("voo"));
EXPECT_FALSE(db().DoesTableExist("voo"));
EXPECT_TRUE(db().DoesViewExist("voo"));

// DoesTableExist() is case-sensitive.
EXPECT_FALSE(db().DoesViewExist("Voo"));
EXPECT_FALSE(db().DoesViewExist("VOO"));
}

TEST_F(SQLDatabaseTest, DoesColumnExist) {
Expand All @@ -239,9 +252,10 @@ TEST_F(SQLDatabaseTest, DoesColumnExist) {
ASSERT_FALSE(db().DoesTableExist("bar"));
EXPECT_FALSE(db().DoesColumnExist("bar", "b"));

// Names are not case sensitive.
EXPECT_TRUE(db().DoesTableExist("FOO"));
// SQLite resolves table/column names without case sensitivity.
EXPECT_TRUE(db().DoesColumnExist("FOO", "A"));
EXPECT_TRUE(db().DoesColumnExist("FOO", "a"));
EXPECT_TRUE(db().DoesColumnExist("foo", "A"));
}

TEST_F(SQLDatabaseTest, GetLastInsertRowId) {
Expand Down

0 comments on commit f85512e

Please sign in to comment.