Skip to content

Commit

Permalink
Improved name of new function, documentation and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schmidt committed Mar 26, 2020
1 parent 9dee407 commit 0c9d4a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
8 changes: 5 additions & 3 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,12 @@ class Statement
*
* @param[in] aIndex Index of the column in the range [0, getColumnCount()).
*
* Throw an exception if the specified index is out of the [0, getColumnCount()) range
* or if the current statement is not a SELECT statement.
* Throw an exception if the type can't be determined because:
* - the specified index is out of the [0, getColumnCount()) range
* - the statement is not a SELECT query
* - the column at aIndex is not a table column but an expression or subquery
*/
const char * getDeclaredType(const int aIndex) const;
const char * getColumnDeclaredType(const int aIndex) const;



Expand Down
2 changes: 1 addition & 1 deletion src/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ int Statement::getColumnIndex(const char* apName) const
return (*iIndex).second;
}

const char * Statement::getDeclaredType(const int aIndex) const
const char * Statement::getColumnDeclaredType(const int aIndex) const
{
checkIndex(aIndex);
const char * result = sqlite3_column_decltype(mStmtPtr, aIndex);
Expand Down
25 changes: 14 additions & 11 deletions tests/Statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,27 +918,30 @@ TEST(Statement, getName)
#endif
}

TEST(Statement, getDeclaredType)
TEST(Statement, getColumnDeclaredType)
{
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, value DOUBLE)"));
SQLite::Statement query(db, "SELECT * FROM test");
const std::string decltype0 = query.getDeclaredType(0);
const std::string decltype1 = query.getDeclaredType(1);
const std::string decltype2 = query.getDeclaredType(2);

SQLite::Statement query(db, "SELECT *, 1 FROM test");

const std::string decltype0 = query.getColumnDeclaredType(0);
const std::string decltype1 = query.getColumnDeclaredType(1);
const std::string decltype2 = query.getColumnDeclaredType(2);
EXPECT_EQ("INTEGER", decltype0);
EXPECT_EQ("TEXT", decltype1);
EXPECT_EQ("DOUBLE", decltype2);

// The column at index 3 is not a table column.
EXPECT_THROW(query.getColumnDeclaredType(3), SQLite::Exception);

// Index out of bounds.
EXPECT_THROW(query.getDeclaredType(3), SQLite::Exception);
EXPECT_THROW(query.getColumnDeclaredType(4), SQLite::Exception);

// Not a SELECT statement.
SQLite::Statement insert(db, "INSERT INTO test VALUES (1, 'Hello', 3.1415)");
EXPECT_THROW(insert.getDeclaredType(0), SQLite::Exception);
// Not a select statement.
SQLite::Statement pragma(db,"PRAGMA compile_options");
EXPECT_THROW(pragma.getColumnDeclaredType(0), SQLite::Exception);
}

#if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900)
Expand Down

0 comments on commit 0c9d4a3

Please sign in to comment.